From d8e03b1a1d929f6afeac72d475183d0218656b48 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sat, 11 Jan 2025 16:57:57 -0600 Subject: First pass at genpassword --- src/encryption.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index 828bde2..0cd032f 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -14,14 +14,45 @@ #include #include -int checkSodium(void) { +#if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 +void naclfaildefault(void *none) { + none = none; // Makes gcc happy + error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); +} + +int checksodiumcb(const vxgg_naclfailcb callback, void *data) { + static vxgg_naclfailcb cb = naclfaildefault; + static void *usr = NULL; + + if(callback != NULL) { + cb = callback; + usr = data; + return 2; // libsodium normally returns 1 if the library is already initialized, so this is to signal that the callback has been updated + } + int ret = sodium_init(); if(ret < 0) - error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); + cb(usr); return ret; } +void vxgg_setsodiumfailcb(vxgg_naclfailcb cb, void *data) { + checksodiumcb(cb, data); +} +#endif + +void checksodium(void) { + #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 + checksodiumcb(NULL, NULL); + #else + if(sodium_init() < 0) + error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); + #endif + + return; +} + // To encrypt: // 1- Create a temp file with the correct name in the root folder of the partition being encrypted -- // 1.1- Detect the partition and find the root folder -- DONE || NOT NECESSARY @@ -40,6 +71,10 @@ int maketmp(const char *dest) { } int encrypttotmp(const char *toencrypt) { + #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 + checksodium(); + #endif + struct stat esb; int efd = -1; @@ -58,12 +93,36 @@ int encrypttotmp(const char *toencrypt) { return 0; } +int genpassword(char **str, unsigned int words) { + #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 + checksodium(); + #endif + + unsigned int i = 0; + char *lstr = NULL; + + if(words < 1) + return 0; + + asprintf(&lstr, "%s", PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); + for(; i < words; i++) { + asprintf(&lstr, "%s %s", lstr, PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]); + } + + *str = lstr; + + return 0; + + // TODO: I feel like this is / should be leaking memory like a mofo. Figure out if it is or not (look at malloc_stats()) +} + #define TESTING #ifdef TESTING #include int main(void) { + /*// Example code for creating a temp file, writing to it, then linking it back into the fs const char *dir = ".", *testmsg = "we do a little testing\n"; char *path = NULL; @@ -82,6 +141,11 @@ int main(void) { if(close(fd) < 0) error(1, errno, "close broke"); + //*/// + + char *password = NULL; + genpassword(&password, 20); + printf("%s\n", password); return 0; } -- cgit v1.2.3