From 129b7312400a69b995b67fd2feb3f915b7f20555 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 21 Oct 2025 16:33:55 -0500 Subject: Reformat tests.c --- src/encryption.c | 139 +++++++++++++++++++++++++------------------------------ 1 file changed, 64 insertions(+), 75 deletions(-) (limited to 'src/encryption.c') 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 @@ #define _GNU_SOURCE 1 #ifndef __VXGG_REWRITE___ENCRYPTION_C___1481879318188___ -#define __VXGG_REWRITE___ENCRYPTION_C___1481879318188___ +#define __VXGG_REWRITE___ENCRYPTION_C___1481879318188___ 1 #define TPSIZE (1<<13) @@ -40,75 +40,68 @@ #include #include -/// Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May -/// cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just -/// manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself -#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1 +/// Runs sodium_init() before every call of a sodium function. Use is discouraged as this may cause unexpected early exits +#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 0 -/// Grants access to the `vxgg_setsodiumfailcb` function, which can be used to set a custom callback for what to do when libsodium -/// fails upon initialization -#define ___VXGG___USE_CLS_CALLBACK___ 1 +/// Defines `vxgg_setsodiumfailcb` function, which is used to set a custom callback for handling a failed libsodium init +#define ___VXGG___USE_CLS_CALLBACK___ 0 -/// Chunk size for en/decryption. I originally wanted to use st_blksize from stat(), but given that those chunks may be of different -/// sizes between computers / filesystems / architectures / files, it's easier to just have this be a consistent macro +/// Chunk size for encryption/decryption #define CHUNKSIZE (1 << 9) -// 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 //! A list of possible words for password creation -#define PASSWORD_WORDS (\ - (const char * const []){\ - "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", \ - "us", "people", "which", "all", "is", "with", "laws", "be", "are", "his", "states", "on", "they", "right", "it", "from", \ - "government", "such", "among", "powers", "most", "an", "time", "should", "new", "as", "been", "colonies", "assent", \ - "large", "at", "independent", "free", "united", "when", "mankind", "hold", "rights", "governments", "consent", "its", \ - "long", "themselves", "abolishing", "usurpations", "absolute", "repeated", "this", "world", "refused", "pass", "other", \ - "others", "without", "justice", "peace", "power", "seas", "war", "do", "declaration", "america", "becomes", "necessary", \ - "political", "equal", "declare", "causes", "separation", "men", "happiness", "any", "form", "alter", "or", "will", \ - "forms", "same", "object", "off", "necessity", "history", "great", "britain", "tyranny", "over", "public", "good", \ - "unless", "suspended", "so", "would", "legislature", "only", "legislative", "bodies", "purpose", "into", "dissolved", \ - "state", "endeavoured", "refusing", "hither", "conditions", "establishing", "offices", "out", "armies", "legislatures", \ - "render", "jurisdiction", "foreign", "acts", "pretended", "trial", "inhabitants", "cases", "transporting", "rule", \ - "declaring", "here", "protection", "against", "lives", "circumstances", "ages", "totally", "friends", "brethren", "whose", \ - "every", "may", "therefore", "ought", "unanimous", "thirteen", "course", "human", "events", "one", "dissolve", "bands", \ - "connected", "another", "assume", "earth", "separate", "station", "nature", "natures", "god", "entitle", "decent", \ - "respect", "opinions", "requires", "impel", "truths", "self", "evident", "created", "endowed", "creator", "certain", \ - "unalienable", "life", "liberty", "pursuit", "secure", "instituted", "deriving", "just", "governed", "whenever", \ - "destructive", "ends", "abolish", "institute", "laying", "foundation", "principles", "organizing", "shall", "seem", \ - "likely", "effect", "safety", "prudence", "indeed", "dictate", "established", "not", "changed", "light", "transient", \ - "accordingly", "experience", "hath", "shewn", "more", "disposed", "suffer", "while", "evils", "sufferable", "than", \ - "accustomed", "but", "train", "abuses", "pursuing", "invariably", "evinces", "design", "reduce", "under", "despotism", \ - "duty", "throw", "provide", "guards", "future", "security", "patient", "sufferance", "now", "constrains", "former", \ - "systems", "present", "king", "injuries", "having", "direct", "establishment", "prove", "let", "facts", "submitted", \ - "candid", "wholesome", "forbidden", "governors", "immediate", "pressing", "importance", "operation", "till", "obtained", \ - "utterly", "neglected", "attend", "accommodation", "districts", "those", "relinquish", "representation", "inestimable", \ - "formidable", "tyrants", "called", "together", "places", "unusual", "uncomfortable", "distant", "depository", "records", \ - "sole", "fatiguing", "compliance", "measures", "representative", "houses", "repeatedly", "opposing", "manly", "firmness", \ - "invasions", "after", "dissolutions", "cause", "elected", "whereby", "incapable", "annihilation", "returned", "exercise", \ - "remaining", "mean", "exposed", "dangers", "invasion", "convulsions", "within", "prevent", "population", "obstructing", \ - "naturalization", "foreigners", "encourage", "migrations", "raising", "appropriations", "lands", "obstructed", \ - "administration", "judiciary", "made", "judges", "dependent", "alone", "tenure", "amount", "payment", "salaries", \ - "erected", "multitude", "sent", "swarms", "officers", "harrass", "eat", "substance", "kept", "times", "standing", \ - "affected", "military", "superior", "civil", "combined", "subject", "constitution", "unacknowledged", "giving", \ - "legislation", "quartering", "armed", "troops", "protecting", "mock", "punishment", "murders", "commit", "cutting", \ - "trade", "parts", "imposing", "taxes", "depriving", "many", "benefits", "jury", "beyond", "tried", "offences", "system", \ - "english", "neighbouring", "province", "therein", "arbitrary", "enlarging", "boundaries", "once", "example", "fit", \ - "instrument", "introducing", "taking", "away", "charters", "valuable", "altering", "fundamentally", "suspending", "own", \ - "invested", "legislate", "whatsoever", "abdicated", "waging", "plundered", "ravaged", "coasts", "burnt", "towns", \ - "destroyed", "mercenaries", "compleat", "works", "death", "desolation", "already", "begun", "cruelty", "perfidy", \ - "scarcely", "paralleled", "barbarous", "unworthy", "head", "civilized", "nation", "constrained", "fellow", "citizens", \ - "taken", "captive", "high", "bear", "arms", "country", "become", "executioners", "fall", "hands", "excited", "domestic", \ - "insurrections", "amongst", "bring", "frontiers", "merciless", "indian", "savages", "known", "warfare", "undistinguished", \ - "destruction", "sexes", "stage", "oppressions", "petitioned", "redress", "humble", "terms", "petitions", "answered", \ - "injury", "prince", "character", "thus", "marked", "act", "define", "tyrant", "unfit", "ruler", "nor", "wanting", \ - "attentions", "brittish", "warned", "attempts", "extend", "unwarrantable", "reminded", "emigration", "settlement", \ - "appealed", "native", "magnanimity", "conjured", "ties", "common", "kindred", "disavow", "inevitably", "interrupt", \ - "connections", "correspondence", "too", "deaf", "voice", "consanguinity", "must", "acquiesce", "denounces", "rest", \ - "enemies", "representatives", "general", "congress", "assembled", "appealing", "supreme", "judge", "rectitude", \ - "intentions", "name", "authority", "solemnly", "publish", "absolved", "allegiance", "british", "crown", "connection", \ - "between", "full", "levy", "conclude", "contract", "alliances", "establish", "commerce", "things", "support", "firm", \ - "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\ - }\ -) +#define PASSWORD_WORDS ((const char * const []){\ + "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", \ + "us", "people", "which", "all", "is", "with", "laws", "be", "are", "his", "states", "on", "they", "right", "it", "from", \ + "government", "such", "among", "powers", "most", "an", "time", "should", "new", "as", "been", "colonies", "assent", \ + "large", "at", "independent", "free", "united", "when", "mankind", "hold", "rights", "governments", "consent", "its", \ + "long", "themselves", "abolishing", "usurpations", "absolute", "repeated", "this", "world", "refused", "pass", "other", \ + "others", "without", "justice", "peace", "power", "seas", "war", "do", "declaration", "america", "becomes", "necessary", \ + "political", "equal", "declare", "causes", "separation", "men", "happiness", "any", "form", "alter", "or", "will", \ + "forms", "same", "object", "off", "necessity", "history", "great", "britain", "tyranny", "over", "public", "good", \ + "unless", "suspended", "so", "would", "legislature", "only", "legislative", "bodies", "purpose", "into", "dissolved", \ + "state", "endeavoured", "refusing", "hither", "conditions", "establishing", "offices", "out", "armies", "legislatures", \ + "render", "jurisdiction", "foreign", "acts", "pretended", "trial", "inhabitants", "cases", "transporting", "rule", \ + "declaring", "here", "protection", "against", "lives", "circumstances", "ages", "totally", "friends", "brethren", "whose", \ + "every", "may", "therefore", "ought", "unanimous", "thirteen", "course", "human", "events", "one", "dissolve", "bands", \ + "connected", "another", "assume", "earth", "separate", "station", "nature", "natures", "god", "entitle", "decent", \ + "respect", "opinions", "requires", "impel", "truths", "self", "evident", "created", "endowed", "creator", "certain", \ + "unalienable", "life", "liberty", "pursuit", "secure", "instituted", "deriving", "just", "governed", "whenever", \ + "destructive", "ends", "abolish", "institute", "laying", "foundation", "principles", "organizing", "shall", "seem", \ + "likely", "effect", "safety", "prudence", "indeed", "dictate", "established", "not", "changed", "light", "transient", \ + "accordingly", "experience", "hath", "shewn", "more", "disposed", "suffer", "while", "evils", "sufferable", "than", \ + "accustomed", "but", "train", "abuses", "pursuing", "invariably", "evinces", "design", "reduce", "under", "despotism", \ + "duty", "throw", "provide", "guards", "future", "security", "patient", "sufferance", "now", "constrains", "former", \ + "systems", "present", "king", "injuries", "having", "direct", "establishment", "prove", "let", "facts", "submitted", \ + "candid", "wholesome", "forbidden", "governors", "immediate", "pressing", "importance", "operation", "till", "obtained", \ + "utterly", "neglected", "attend", "accommodation", "districts", "those", "relinquish", "representation", "inestimable", \ + "formidable", "tyrants", "called", "together", "places", "unusual", "uncomfortable", "distant", "depository", "records", \ + "sole", "fatiguing", "compliance", "measures", "representative", "houses", "repeatedly", "opposing", "manly", "firmness", \ + "invasions", "after", "dissolutions", "cause", "elected", "whereby", "incapable", "annihilation", "returned", "exercise", \ + "remaining", "mean", "exposed", "dangers", "invasion", "convulsions", "within", "prevent", "population", "obstructing", \ + "naturalization", "foreigners", "encourage", "migrations", "raising", "appropriations", "lands", "obstructed", \ + "administration", "judiciary", "made", "judges", "dependent", "alone", "tenure", "amount", "payment", "salaries", \ + "erected", "multitude", "sent", "swarms", "officers", "harrass", "eat", "substance", "kept", "times", "standing", \ + "affected", "military", "superior", "civil", "combined", "subject", "constitution", "unacknowledged", "giving", \ + "legislation", "quartering", "armed", "troops", "protecting", "mock", "punishment", "murders", "commit", "cutting", \ + "trade", "parts", "imposing", "taxes", "depriving", "many", "benefits", "jury", "beyond", "tried", "offences", "system", \ + "english", "neighbouring", "province", "therein", "arbitrary", "enlarging", "boundaries", "once", "example", "fit", \ + "instrument", "introducing", "taking", "away", "charters", "valuable", "altering", "fundamentally", "suspending", "own", \ + "invested", "legislate", "whatsoever", "abdicated", "waging", "plundered", "ravaged", "coasts", "burnt", "towns", \ + "destroyed", "mercenaries", "compleat", "works", "death", "desolation", "already", "begun", "cruelty", "perfidy", \ + "scarcely", "paralleled", "barbarous", "unworthy", "head", "civilized", "nation", "constrained", "fellow", "citizens", \ + "taken", "captive", "high", "bear", "arms", "country", "become", "executioners", "fall", "hands", "excited", "domestic", \ + "insurrections", "amongst", "bring", "frontiers", "merciless", "indian", "savages", "known", "warfare", "undistinguished", \ + "destruction", "sexes", "stage", "oppressions", "petitioned", "redress", "humble", "terms", "petitions", "answered", \ + "injury", "prince", "character", "thus", "marked", "act", "define", "tyrant", "unfit", "ruler", "nor", "wanting", \ + "attentions", "brittish", "warned", "attempts", "extend", "unwarrantable", "reminded", "emigration", "settlement", \ + "appealed", "native", "magnanimity", "conjured", "ties", "common", "kindred", "disavow", "inevitably", "interrupt", \ + "connections", "correspondence", "too", "deaf", "voice", "consanguinity", "must", "acquiesce", "denounces", "rest", \ + "enemies", "representatives", "general", "congress", "assembled", "appealing", "supreme", "judge", "rectitude", \ + "intentions", "name", "authority", "solemnly", "publish", "absolved", "allegiance", "british", "crown", "connection", \ + "between", "full", "levy", "conclude", "contract", "alliances", "establish", "commerce", "things", "support", "firm", \ + "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\ +}) //! Short macro for getting the `PASSWORD_WORDS` array size #define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS)) @@ -219,17 +212,14 @@ int maketmp(const char * const dest) { * @retval (int)[-1, 0] 0 on success, -1 on error */ int linkto(const char * const target, int tgfd) { - if(!target) ERRRET(EINVAL, -1); - + if(!target || tgfd < 0 || access(target, F_OK) != -1) ERRRET(EINVAL, -1); char *path = NULL; + asprintf(&path, "/proc/self/fd/%d", tgfd); - if(!path) - ERROR(1, errno, " Couldn't get path to move file into system",); - 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) - // 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. - // I really should write a function entirely dedicated to dealing with linking + if(!path) ERROR(1, errno, " Couldn't get path to move file into system",); int res = linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); + free(path); return res; } @@ -238,7 +228,6 @@ int linkto(const char * const target, int tgfd) { static void __ucl_close(void *fd) { if(!fd) return; close(*(int*)fd); - *(int*)fd = -1; return; } @@ -262,8 +251,8 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr checksodium(); #endif - unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; - unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; + unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; + unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; crypto_secretstream_xchacha20poly1305_state state; unsigned long long cbuflen; unsigned char tag; -- cgit v1.2.3