From 7424be8e0b033fd6466d517d6a8e3f0fb545dd59 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 31 Mar 2025 16:01:23 -0500 Subject: Decouple the checksodiumcb function from ALWAYS_CHECK_LIBSODIUM into its own macro --- src/encryption.c | 41 +++++++++++++++++++++++++---------------- src/encryption.h | 21 +++++++++++++++------ src/shared.h | 1 + 3 files changed, 41 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/encryption.c b/src/encryption.c index a01d4ff..606be03 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -16,6 +16,8 @@ #include #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 +#if ___VXGG___USE_CLS_CALLBACK___ > 0 + void naclfaildefault(void *none) { none = none; // Makes gcc happy if(___VXGG___VERBOSE_ERRORS___) @@ -23,31 +25,36 @@ void naclfaildefault(void *none) { exit(EXIT_FAILURE); } -int checksodiumcb(const vxgg_naclfailcb callback, void *data) { +int checksodiumcb(const vxgg_naclfailcb callback, void *data, unsigned char set) { static vxgg_naclfailcb cb = naclfaildefault; static void *usr = NULL; - - if(callback != NULL) { + int ret; + + if(set) { 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) - cb(usr); + if((ret = sodium_init()) < 0) { + if(cb == NULL) + error(0, EINVAL, " refusing to run a null callback"); + else + cb(usr); + } return ret; } void vxgg_setsodiumfailcb(vxgg_naclfailcb cb, void *data) { - checksodiumcb(cb, data); + checksodiumcb(cb, data, 1); } + #endif void checksodium(void) { - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodiumcb(NULL, NULL); + #if ___VXGG___USE_CLS_CALLBACK___ > 0 + checksodiumcb(NULL, NULL, 0); #else if(sodium_init() < 0) { @@ -60,13 +67,15 @@ void checksodium(void) { return; } +#endif + int maketmp(const char * const dest) { return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); } int encrypttotmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); + checksodium(); #endif int fd = -1, tfd = -1; @@ -163,6 +172,9 @@ int decrypttofile(FILE *dst, FILE *src, unsigned char key[crypto_secretstream_xc FILE *dst, *src; int eof; + #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 + checksodium(); + #endif if(!src) RETURNWERR(EINVAL, -1); @@ -244,9 +256,9 @@ void* xsodium_malloc(size_t size) { } -#ifdef TESTING +/* int main(void) { - /*// Example code for creating a temp file, writing to it, then linking it back into the fs + // 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; @@ -313,9 +325,6 @@ int main(void) { sodium_munlock(uin, strlen(uin) + 1); free(uin); - //*/// - return 0; } - -#endif \ No newline at end of file +*/ \ No newline at end of file diff --git a/src/encryption.h b/src/encryption.h index 1bdd2b9..1f04dae 100644 --- a/src/encryption.h +++ b/src/encryption.h @@ -3,21 +3,33 @@ #include +#define CHUNKSIZE (1 << 9) + + // 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___ 0 +#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1 + +// 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 + + -#define CHUNKSIZE (1 << 9) -// TODO: What the fuck was I thinking when I did any of this callback shit? Make this a different macro and decouple it from ALWAYS_CHECK #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 +// 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); + +#if ___VXGG___USE_CLS_CALLBACK___ > 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 #endif @@ -78,9 +90,6 @@ void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data); ) #define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS)) -// 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 * const dest); diff --git a/src/shared.h b/src/shared.h index 7d2db27..b7fe7c3 100644 --- a/src/shared.h +++ b/src/shared.h @@ -29,6 +29,7 @@ exit(EXIT_FAILURE); /* Makes gcc happy */\ } while (0) +// Error macro that gcc will not complain about #define ERROR(status, errnum, format, ...) do {error((status), (errnum), (format)__VA_ARGS__); exit((status));} while (0) // `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` -- cgit v1.2.3