summaryrefslogtreecommitdiff
path: root/src/encryption.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/encryption.c')
-rw-r--r--src/encryption.c139
1 files changed, 64 insertions, 75 deletions
diff --git a/src/encryption.c b/src/encryption.c
index c3f77ce..c86adfa 100644
--- a/src/encryption.c
+++ b/src/encryption.c
@@ -17,7 +17,7 @@
17#define _GNU_SOURCE 1 17#define _GNU_SOURCE 1
18 18
19#ifndef __VXGG_REWRITE___ENCRYPTION_C___1481879318188___ 19#ifndef __VXGG_REWRITE___ENCRYPTION_C___1481879318188___
20#define __VXGG_REWRITE___ENCRYPTION_C___1481879318188___ 20#define __VXGG_REWRITE___ENCRYPTION_C___1481879318188___ 1
21 21
22#define TPSIZE (1<<13) 22#define TPSIZE (1<<13)
23 23
@@ -40,75 +40,68 @@
40#include <errno.h> 40#include <errno.h>
41#include <error.h> 41#include <error.h>
42 42
43/// Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May 43/// Runs sodium_init() before every call of a sodium function. Use is discouraged as this may cause unexpected early exits
44/// cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just 44#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 0
45/// manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself
46#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1
47 45
48/// Grants access to the `vxgg_setsodiumfailcb` function, which can be used to set a custom callback for what to do when libsodium 46/// Defines `vxgg_setsodiumfailcb` function, which is used to set a custom callback for handling a failed libsodium init
49/// fails upon initialization 47#define ___VXGG___USE_CLS_CALLBACK___ 0
50#define ___VXGG___USE_CLS_CALLBACK___ 1
51 48
52/// Chunk size for en/decryption. I originally wanted to use st_blksize from stat(), but given that those chunks may be of different 49/// Chunk size for encryption/decryption
53/// sizes between computers / filesystems / architectures / files, it's easier to just have this be a consistent macro
54#define CHUNKSIZE (1 << 9) 50#define CHUNKSIZE (1 << 9)
55 51
56// Fuck reading from a file. Even if someone ran strings on the binary and got this they wouldn't be able to regenerate the key
57//! A list of possible words for password creation 52//! A list of possible words for password creation
58#define PASSWORD_WORDS (\ 53#define PASSWORD_WORDS ((const char * const []){\
59 (const char * const []){\ 54 "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", \
60 "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", \ 55 "us", "people", "which", "all", "is", "with", "laws", "be", "are", "his", "states", "on", "they", "right", "it", "from", \
61 "us", "people", "which", "all", "is", "with", "laws", "be", "are", "his", "states", "on", "they", "right", "it", "from", \ 56 "government", "such", "among", "powers", "most", "an", "time", "should", "new", "as", "been", "colonies", "assent", \
62 "government", "such", "among", "powers", "most", "an", "time", "should", "new", "as", "been", "colonies", "assent", \ 57 "large", "at", "independent", "free", "united", "when", "mankind", "hold", "rights", "governments", "consent", "its", \
63 "large", "at", "independent", "free", "united", "when", "mankind", "hold", "rights", "governments", "consent", "its", \ 58 "long", "themselves", "abolishing", "usurpations", "absolute", "repeated", "this", "world", "refused", "pass", "other", \
64 "long", "themselves", "abolishing", "usurpations", "absolute", "repeated", "this", "world", "refused", "pass", "other", \ 59 "others", "without", "justice", "peace", "power", "seas", "war", "do", "declaration", "america", "becomes", "necessary", \
65 "others", "without", "justice", "peace", "power", "seas", "war", "do", "declaration", "america", "becomes", "necessary", \ 60 "political", "equal", "declare", "causes", "separation", "men", "happiness", "any", "form", "alter", "or", "will", \
66 "political", "equal", "declare", "causes", "separation", "men", "happiness", "any", "form", "alter", "or", "will", \ 61 "forms", "same", "object", "off", "necessity", "history", "great", "britain", "tyranny", "over", "public", "good", \
67 "forms", "same", "object", "off", "necessity", "history", "great", "britain", "tyranny", "over", "public", "good", \ 62 "unless", "suspended", "so", "would", "legislature", "only", "legislative", "bodies", "purpose", "into", "dissolved", \
68 "unless", "suspended", "so", "would", "legislature", "only", "legislative", "bodies", "purpose", "into", "dissolved", \ 63 "state", "endeavoured", "refusing", "hither", "conditions", "establishing", "offices", "out", "armies", "legislatures", \
69 "state", "endeavoured", "refusing", "hither", "conditions", "establishing", "offices", "out", "armies", "legislatures", \ 64 "render", "jurisdiction", "foreign", "acts", "pretended", "trial", "inhabitants", "cases", "transporting", "rule", \
70 "render", "jurisdiction", "foreign", "acts", "pretended", "trial", "inhabitants", "cases", "transporting", "rule", \ 65 "declaring", "here", "protection", "against", "lives", "circumstances", "ages", "totally", "friends", "brethren", "whose", \
71 "declaring", "here", "protection", "against", "lives", "circumstances", "ages", "totally", "friends", "brethren", "whose", \ 66 "every", "may", "therefore", "ought", "unanimous", "thirteen", "course", "human", "events", "one", "dissolve", "bands", \
72 "every", "may", "therefore", "ought", "unanimous", "thirteen", "course", "human", "events", "one", "dissolve", "bands", \ 67 "connected", "another", "assume", "earth", "separate", "station", "nature", "natures", "god", "entitle", "decent", \
73 "connected", "another", "assume", "earth", "separate", "station", "nature", "natures", "god", "entitle", "decent", \ 68 "respect", "opinions", "requires", "impel", "truths", "self", "evident", "created", "endowed", "creator", "certain", \
74 "respect", "opinions", "requires", "impel", "truths", "self", "evident", "created", "endowed", "creator", "certain", \ 69 "unalienable", "life", "liberty", "pursuit", "secure", "instituted", "deriving", "just", "governed", "whenever", \
75 "unalienable", "life", "liberty", "pursuit", "secure", "instituted", "deriving", "just", "governed", "whenever", \ 70 "destructive", "ends", "abolish", "institute", "laying", "foundation", "principles", "organizing", "shall", "seem", \
76 "destructive", "ends", "abolish", "institute", "laying", "foundation", "principles", "organizing", "shall", "seem", \ 71 "likely", "effect", "safety", "prudence", "indeed", "dictate", "established", "not", "changed", "light", "transient", \
77 "likely", "effect", "safety", "prudence", "indeed", "dictate", "established", "not", "changed", "light", "transient", \ 72 "accordingly", "experience", "hath", "shewn", "more", "disposed", "suffer", "while", "evils", "sufferable", "than", \
78 "accordingly", "experience", "hath", "shewn", "more", "disposed", "suffer", "while", "evils", "sufferable", "than", \ 73 "accustomed", "but", "train", "abuses", "pursuing", "invariably", "evinces", "design", "reduce", "under", "despotism", \
79 "accustomed", "but", "train", "abuses", "pursuing", "invariably", "evinces", "design", "reduce", "under", "despotism", \ 74 "duty", "throw", "provide", "guards", "future", "security", "patient", "sufferance", "now", "constrains", "former", \
80 "duty", "throw", "provide", "guards", "future", "security", "patient", "sufferance", "now", "constrains", "former", \ 75 "systems", "present", "king", "injuries", "having", "direct", "establishment", "prove", "let", "facts", "submitted", \
81 "systems", "present", "king", "injuries", "having", "direct", "establishment", "prove", "let", "facts", "submitted", \ 76 "candid", "wholesome", "forbidden", "governors", "immediate", "pressing", "importance", "operation", "till", "obtained", \
82 "candid", "wholesome", "forbidden", "governors", "immediate", "pressing", "importance", "operation", "till", "obtained", \ 77 "utterly", "neglected", "attend", "accommodation", "districts", "those", "relinquish", "representation", "inestimable", \
83 "utterly", "neglected", "attend", "accommodation", "districts", "those", "relinquish", "representation", "inestimable", \ 78 "formidable", "tyrants", "called", "together", "places", "unusual", "uncomfortable", "distant", "depository", "records", \
84 "formidable", "tyrants", "called", "together", "places", "unusual", "uncomfortable", "distant", "depository", "records", \ 79 "sole", "fatiguing", "compliance", "measures", "representative", "houses", "repeatedly", "opposing", "manly", "firmness", \
85 "sole", "fatiguing", "compliance", "measures", "representative", "houses", "repeatedly", "opposing", "manly", "firmness", \ 80 "invasions", "after", "dissolutions", "cause", "elected", "whereby", "incapable", "annihilation", "returned", "exercise", \
86 "invasions", "after", "dissolutions", "cause", "elected", "whereby", "incapable", "annihilation", "returned", "exercise", \ 81 "remaining", "mean", "exposed", "dangers", "invasion", "convulsions", "within", "prevent", "population", "obstructing", \
87 "remaining", "mean", "exposed", "dangers", "invasion", "convulsions", "within", "prevent", "population", "obstructing", \ 82 "naturalization", "foreigners", "encourage", "migrations", "raising", "appropriations", "lands", "obstructed", \
88 "naturalization", "foreigners", "encourage", "migrations", "raising", "appropriations", "lands", "obstructed", \ 83 "administration", "judiciary", "made", "judges", "dependent", "alone", "tenure", "amount", "payment", "salaries", \
89 "administration", "judiciary", "made", "judges", "dependent", "alone", "tenure", "amount", "payment", "salaries", \ 84 "erected", "multitude", "sent", "swarms", "officers", "harrass", "eat", "substance", "kept", "times", "standing", \
90 "erected", "multitude", "sent", "swarms", "officers", "harrass", "eat", "substance", "kept", "times", "standing", \ 85 "affected", "military", "superior", "civil", "combined", "subject", "constitution", "unacknowledged", "giving", \
91 "affected", "military", "superior", "civil", "combined", "subject", "constitution", "unacknowledged", "giving", \ 86 "legislation", "quartering", "armed", "troops", "protecting", "mock", "punishment", "murders", "commit", "cutting", \
92 "legislation", "quartering", "armed", "troops", "protecting", "mock", "punishment", "murders", "commit", "cutting", \ 87 "trade", "parts", "imposing", "taxes", "depriving", "many", "benefits", "jury", "beyond", "tried", "offences", "system", \
93 "trade", "parts", "imposing", "taxes", "depriving", "many", "benefits", "jury", "beyond", "tried", "offences", "system", \ 88 "english", "neighbouring", "province", "therein", "arbitrary", "enlarging", "boundaries", "once", "example", "fit", \
94 "english", "neighbouring", "province", "therein", "arbitrary", "enlarging", "boundaries", "once", "example", "fit", \ 89 "instrument", "introducing", "taking", "away", "charters", "valuable", "altering", "fundamentally", "suspending", "own", \
95 "instrument", "introducing", "taking", "away", "charters", "valuable", "altering", "fundamentally", "suspending", "own", \ 90 "invested", "legislate", "whatsoever", "abdicated", "waging", "plundered", "ravaged", "coasts", "burnt", "towns", \
96 "invested", "legislate", "whatsoever", "abdicated", "waging", "plundered", "ravaged", "coasts", "burnt", "towns", \ 91 "destroyed", "mercenaries", "compleat", "works", "death", "desolation", "already", "begun", "cruelty", "perfidy", \
97 "destroyed", "mercenaries", "compleat", "works", "death", "desolation", "already", "begun", "cruelty", "perfidy", \ 92 "scarcely", "paralleled", "barbarous", "unworthy", "head", "civilized", "nation", "constrained", "fellow", "citizens", \
98 "scarcely", "paralleled", "barbarous", "unworthy", "head", "civilized", "nation", "constrained", "fellow", "citizens", \ 93 "taken", "captive", "high", "bear", "arms", "country", "become", "executioners", "fall", "hands", "excited", "domestic", \
99 "taken", "captive", "high", "bear", "arms", "country", "become", "executioners", "fall", "hands", "excited", "domestic", \ 94 "insurrections", "amongst", "bring", "frontiers", "merciless", "indian", "savages", "known", "warfare", "undistinguished", \
100 "insurrections", "amongst", "bring", "frontiers", "merciless", "indian", "savages", "known", "warfare", "undistinguished", \ 95 "destruction", "sexes", "stage", "oppressions", "petitioned", "redress", "humble", "terms", "petitions", "answered", \
101 "destruction", "sexes", "stage", "oppressions", "petitioned", "redress", "humble", "terms", "petitions", "answered", \ 96 "injury", "prince", "character", "thus", "marked", "act", "define", "tyrant", "unfit", "ruler", "nor", "wanting", \
102 "injury", "prince", "character", "thus", "marked", "act", "define", "tyrant", "unfit", "ruler", "nor", "wanting", \ 97 "attentions", "brittish", "warned", "attempts", "extend", "unwarrantable", "reminded", "emigration", "settlement", \
103 "attentions", "brittish", "warned", "attempts", "extend", "unwarrantable", "reminded", "emigration", "settlement", \ 98 "appealed", "native", "magnanimity", "conjured", "ties", "common", "kindred", "disavow", "inevitably", "interrupt", \
104 "appealed", "native", "magnanimity", "conjured", "ties", "common", "kindred", "disavow", "inevitably", "interrupt", \ 99 "connections", "correspondence", "too", "deaf", "voice", "consanguinity", "must", "acquiesce", "denounces", "rest", \
105 "connections", "correspondence", "too", "deaf", "voice", "consanguinity", "must", "acquiesce", "denounces", "rest", \ 100 "enemies", "representatives", "general", "congress", "assembled", "appealing", "supreme", "judge", "rectitude", \
106 "enemies", "representatives", "general", "congress", "assembled", "appealing", "supreme", "judge", "rectitude", \ 101 "intentions", "name", "authority", "solemnly", "publish", "absolved", "allegiance", "british", "crown", "connection", \
107 "intentions", "name", "authority", "solemnly", "publish", "absolved", "allegiance", "british", "crown", "connection", \ 102 "between", "full", "levy", "conclude", "contract", "alliances", "establish", "commerce", "things", "support", "firm", \
108 "between", "full", "levy", "conclude", "contract", "alliances", "establish", "commerce", "things", "support", "firm", \ 103 "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\
109 "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\ 104})
110 }\
111)
112//! Short macro for getting the `PASSWORD_WORDS` array size 105//! Short macro for getting the `PASSWORD_WORDS` array size
113#define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS)) 106#define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS))
114 107
@@ -219,17 +212,14 @@ int maketmp(const char * const dest) {
219 * @retval (int)[-1, 0] 0 on success, -1 on error 212 * @retval (int)[-1, 0] 0 on success, -1 on error
220 */ 213 */
221int linkto(const char * const target, int tgfd) { 214int linkto(const char * const target, int tgfd) {
222 if(!target) ERRRET(EINVAL, -1); 215 if(!target || tgfd < 0 || access(target, F_OK) != -1) ERRRET(EINVAL, -1);
223
224 char *path = NULL; 216 char *path = NULL;
217
225 asprintf(&path, "/proc/self/fd/%d", tgfd); 218 asprintf(&path, "/proc/self/fd/%d", tgfd);
226 if(!path) 219 if(!path) ERROR(1, errno, "<linkto> Couldn't get path to move file into system",);
227 ERROR(1, errno, "<linkto> Couldn't get path to move file into system",);
228 remove(target); // Make sure an old version isn't sticking around (it's not catastrophic if this fails, but it should be noted or logged somewhere)
229 // TODO: This is bad. If a file gets deleted and the program crashes before the new one can get linked into the fs, the data is lost.
230 // I really should write a function entirely dedicated to dealing with linking
231 220
232 int res = linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); 221 int res = linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW);
222
233 free(path); 223 free(path);
234 return res; 224 return res;
235} 225}
@@ -238,7 +228,6 @@ int linkto(const char * const target, int tgfd) {
238static void __ucl_close(void *fd) { 228static void __ucl_close(void *fd) {
239 if(!fd) return; 229 if(!fd) return;
240 close(*(int*)fd); 230 close(*(int*)fd);
241 *(int*)fd = -1;
242 return; 231 return;
243} 232}
244 233
@@ -262,8 +251,8 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr
262 checksodium(); 251 checksodium();
263 #endif 252 #endif
264 253
265 unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; 254 unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
266 unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; 255 unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
267 crypto_secretstream_xchacha20poly1305_state state; 256 crypto_secretstream_xchacha20poly1305_state state;
268 unsigned long long cbuflen; 257 unsigned long long cbuflen;
269 unsigned char tag; 258 unsigned char tag;