diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 8 | ||||
| -rw-r--r-- | src/encryption.c | 139 | ||||
| -rw-r--r-- | src/tests.c | 56 |
3 files changed, 92 insertions, 111 deletions
diff --git a/src/Makefile b/src/Makefile index 0a72955..7be2382 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -35,12 +35,10 @@ main tests: %: %.c $(TIMESTAMPS) | |||
| 35 | # Phony rules | 35 | # Phony rules |
| 36 | 36 | ||
| 37 | c clean: | 37 | c clean: |
| 38 | -rm -rvf main $(TIMESTAMP_DIR) $(wildcard *.test*) $(wildcard *.enc) | 38 | -rm -rvf main tests $(TIMESTAMP_DIR) $(wildcard *.test*) $(wildcard *.enc) |
| 39 | 39 | ||
| 40 | v val: | 40 | v val: main |
| 41 | $(MAKE) all | ||
| 42 | valgrind --leak-check=yes ./main | 41 | valgrind --leak-check=yes ./main |
| 43 | 42 | ||
| 44 | t test: | 43 | t test: tests |
| 45 | $(MAKE) tests | ||
| 46 | valgrind --leak-check=yes ./tests \ No newline at end of file | 44 | valgrind --leak-check=yes ./tests \ No newline at end of file |
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 | */ |
| 221 | int linkto(const char * const target, int tgfd) { | 214 | int 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) { | |||
| 238 | static void __ucl_close(void *fd) { | 228 | static 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; |
diff --git a/src/tests.c b/src/tests.c index 2b98b3d..a7531e8 100644 --- a/src/tests.c +++ b/src/tests.c | |||
| @@ -4,56 +4,47 @@ | |||
| 4 | #include "encryption.c" | 4 | #include "encryption.c" |
| 5 | #include "threadpool.c" | 5 | #include "threadpool.c" |
| 6 | 6 | ||
| 7 | void test_encryption(void) { | 7 | void test_maketmp(void) { |
| 8 | // TODO: Figure out if I care about this test existing or not. Currently, this has just been | ||
| 9 | // copied from ecryption.c & slapped | ||
| 10 | |||
| 11 | // Example code for creating a temp file, writing to it, then linking it back into the fs | 8 | // Example code for creating a temp file, writing to it, then linking it back into the fs |
| 12 | const char *dir = ".", *testmsg = "we do a little testing\n"; | 9 | const char * const dir = ".", * const testmsg = "we do a little testing\n"; |
| 13 | char *path = NULL; | ||
| 14 | 10 | ||
| 15 | int fd = maketmp(dir); | 11 | int fd = maketmp(dir); |
| 16 | if(fd < 0) | 12 | if(fd < 0) ERROR(1, errno, "<test_maketmp> FAIL: Couldn't make temp file at %s", , dir); |
| 17 | error(1, errno, "Couldn't make temp file at %s", dir); | 13 | if(write(fd, testmsg, strlen(testmsg)) < 0) error(1, errno, "<test_maketmp> FAIL: write broke"); |
| 18 | |||
| 19 | if(write(fd, testmsg, strlen(testmsg)) < 0) | ||
| 20 | error(1, errno, "write broke"); | ||
| 21 | 14 | ||
| 22 | asprintf(&path, "/proc/self/fd/%d", fd); | 15 | if(linkto("./test_maketmp.test", fd)) ERROR(1, errno, "<test_maketmp> FAIL: Could not link into filesystem",); |
| 23 | linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); | ||
| 24 | free(path); | ||
| 25 | 16 | ||
| 26 | // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages | 17 | // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages |
| 18 | if(close(fd) < 0) ERROR(1, errno, "close broke",); | ||
| 27 | 19 | ||
| 28 | if(close(fd) < 0) | 20 | return; |
| 29 | error(1, errno, "close broke"); | 21 | } |
| 30 | //*/// | ||
| 31 | |||
| 32 | //*// Example code for getting a password using genpassword | ||
| 33 | checksodium(); | ||
| 34 | 22 | ||
| 23 | void test_genpassword(void) { | ||
| 24 | // Example code for getting a password using genpassword | ||
| 35 | char *password = NULL; | 25 | char *password = NULL; |
| 36 | genpassword(&password, 20); | 26 | genpassword(&password, 20); |
| 37 | printf("%s\n", (password != NULL) ? password : "Couldn't get a password"); | 27 | if(!password) ERROR(1, EINVAL, "<test_genpassword> FAIL: Couldn't get a password",); |
| 28 | |||
| 29 | printf("%s\n", password); | ||
| 38 | free(password); | 30 | free(password); |
| 39 | /*/// | ||
| 40 | 31 | ||
| 41 | //*/// Example code for generating a password, derriving a secret key from it, and storing things properly | 32 | return; |
| 33 | } | ||
| 34 | |||
| 35 | void test_libsodium_password(void) { | ||
| 36 | // Example code for generating a password, derriving a secret key from it, and storing things properly | ||
| 42 | 37 | ||
| 43 | // Initialization | 38 | // Initialization |
| 44 | checksodium(); | ||
| 45 | char *pass = NULL, hpass[crypto_pwhash_STRBYTES]; | 39 | char *pass = NULL, hpass[crypto_pwhash_STRBYTES]; |
| 40 | if(genpassword(&pass, 20) < 0) ERROR(1, 0, "<test_libsodium_password> FAIL: Could not generate password", ); | ||
| 46 | 41 | ||
| 47 | if(genpassword(&pass, 20) < 0) { | ||
| 48 | error(1, 0, "Could not generate password, quitting..."); | ||
| 49 | abort(); // Makes gcc happy. Not sure why gcc randomly decides that error() isn't a proper exit, but hey whatever | ||
| 50 | } | ||
| 51 | sodium_mlock(pass, strlen(pass) + 1); | 42 | sodium_mlock(pass, strlen(pass) + 1); |
| 52 | printf("Password:%s\n", pass); | 43 | printf("Password:%s\n", pass); |
| 53 | 44 | ||
| 54 | // Store the password | 45 | // Store the password |
| 55 | if(crypto_pwhash_str(hpass, pass, strlen(pass) + 1, crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0) | 46 | if(crypto_pwhash_str(hpass, pass, strlen(pass) + 1, crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0) |
| 56 | error(1, errno, "Couldn't generate password, quitting..."); | 47 | ERROR(1, errno, "<test_libsodium_password> FAIL: Couldn't hash generated password",); |
| 57 | // Don't know if I want to use MODERATE or SENSITIVE for this. SENSITIVE takes a little bit on my laptop, which honestly | 48 | // Don't know if I want to use MODERATE or SENSITIVE for this. SENSITIVE takes a little bit on my laptop, which honestly |
| 58 | // shouldn't be a problem, but it annoys me. MODERATE is quick and snappy, or at least quick enough that the slowdown is | 49 | // shouldn't be a problem, but it annoys me. MODERATE is quick and snappy, or at least quick enough that the slowdown is |
| 59 | // barely noticable. I might do MODERATE for testing and SENSITIVE for release | 50 | // barely noticable. I might do MODERATE for testing and SENSITIVE for release |
| @@ -65,8 +56,9 @@ void test_encryption(void) { | |||
| 65 | 56 | ||
| 66 | // Check if the password from the user is correct | 57 | // Check if the password from the user is correct |
| 67 | char *uin = NULL; int size = -1; | 58 | char *uin = NULL; int size = -1; |
| 59 | printf("Please enter your password: "); | ||
| 68 | if((size = rwbuf(&uin, 1, STDIN_FILENO)) < 0) | 60 | if((size = rwbuf(&uin, 1, STDIN_FILENO)) < 0) |
| 69 | error(1, errno, "Could not read from stdin"); | 61 | ERROR(1, errno, "<test_libsodium_password> FAIL: Could not read from stdin",); |
| 70 | sodium_mlock(uin, size); | 62 | sodium_mlock(uin, size); |
| 71 | 63 | ||
| 72 | printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); | 64 | printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False"); |
| @@ -79,6 +71,8 @@ void test_encryption(void) { | |||
| 79 | } | 71 | } |
| 80 | 72 | ||
| 81 | int main(void) { | 73 | int main(void) { |
| 82 | test_encryption(); | 74 | test_maketmp(); |
| 75 | test_genpassword(); | ||
| 76 | test_libsodium_password(); | ||
| 83 | return 0; | 77 | return 0; |
| 84 | } \ No newline at end of file | 78 | } \ No newline at end of file |
