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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/encryption.h | 20 +++++++++++++++-- src/shared.c | 4 ++-- src/shared.h | 14 ++++++------ 4 files changed, 93 insertions(+), 13 deletions(-) (limited to 'src') 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; } diff --git a/src/encryption.h b/src/encryption.h index 675ef18..2c8f976 100644 --- a/src/encryption.h +++ b/src/encryption.h @@ -1,6 +1,21 @@ #ifndef __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ #define __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ +// 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 + +#if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 + +// Definition for the callback function that fires when a call to checksodium fails +typedef void (*vxgg_naclfailcb)(void*); + +// Sets the error callback for when libsodium fails. Runs `cb(data)` if `(sodium_init() < 0)` +void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data); + +#endif + // I need to store a dictionary of valid words for generating a password, and I don't want to read it in from another file, so I'm experimenting with this #define PASSWORD_WORDS (\ (const char *[]){\ @@ -56,9 +71,10 @@ "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\ }\ ) +#define PASSWORD_WORDS_LEN (STATICARR_SIZE(PASSWORD_WORDS)) -// Checks if sodium is initialized. Initializes it if not -int checkSodium(void); +// Checks if sodium is initialized. Initializes it if not. If `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0`, it's possible to set an error callback to avoid exiting the entire program. Otherwise calls `error()` if libsodium can't initialize +void checksodium(void); // open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR int maketmp(const char *dest); diff --git a/src/shared.c b/src/shared.c index 7be3c28..250b348 100644 --- a/src/shared.c +++ b/src/shared.c @@ -10,7 +10,7 @@ void* xcalloc(size_t nmemb, size_t size) { void *mem = calloc(nmemb, size); if(mem == NULL) { - #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 + #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 error(1, errno, " Could not allocate memory"); #endif @@ -24,7 +24,7 @@ void* xcalloc(size_t nmemb, size_t size) { void* xreallocarray(void *ptr, size_t nmemb, size_t size) { void *mem = reallocarray(ptr, nmemb, size); if(mem == NULL) { - #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 + #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 error(1, errno, " Could not allocate memory"); #endif diff --git a/src/shared.h b/src/shared.h index 05ab58c..5894b41 100644 --- a/src/shared.h +++ b/src/shared.h @@ -4,17 +4,17 @@ #include #include -#define STATICARRAY_SIZE(arr) (sizeof((arr))/sizeof((arr)[0])) +#define STATICARR_SIZE(arr) (sizeof((arr))/sizeof((arr)[0])) -// Defines how `x___alloc()` functions should exit. `___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions -// registered with `atexit()` and `on_exit()`. `___VXGG___XCALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `xcalloc()` -// will ALWAYS 'abort', doing otherwise defeats the purpose of the function -#define ___VXGG___XCALLOC_EXIT_ON_ERROR___ 1 +// Defines how `x___alloc()` functions should exit. `___VXGG___XALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions +// registered with `atexit()` and `on_exit()`. `___VXGG___XALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `x___alloc()` +// type functions will ALWAYS 'abort', doing otherwise defeats the purpose of the function type +#define ___VXGG___XALLOC_EXIT_ON_ERROR___ 1 -// `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` +// `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` void* xcalloc(size_t nmemb, size_t size); -// `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` +// `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` void* xreallocarray(void *ptr, size_t nmemb, size_t size); #if !defined _GNU_SOURCE -- cgit v1.2.3