From 0c541f74d346625618dc3ea8974bfb2cf042c000 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 2 Nov 2025 20:46:33 -0600 Subject: Rip out weird compile-time callback thing --- src/encryption.c | 126 +++---------------------------------------------------- 1 file changed, 6 insertions(+), 120 deletions(-) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index 8526f15..2d48e4e 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -9,11 +9,6 @@ * */ -// TODO: Go back and make sure every function has proper error handling -// Oh fucking christ what have I done -// I need to make sure every single function in this file returns with an indicated error instead of nuking the whole program with -// error() - #define _GNU_SOURCE 1 #ifndef __VXGG_REWRITE___ENCRYPTION_C___1481879318188___ @@ -40,12 +35,6 @@ #include #include -/// 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 - -/// 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 encryption/decryption #define CHUNKSIZE (1 << 9) @@ -105,94 +94,6 @@ //! Short macro for getting the `PASSWORD_WORDS` array size #define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS)) -#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 -#if ___VXGG___USE_CLS_CALLBACK___ > 0 - -//! Definition for the callback function that fires when a call to checksodium fails -typedef void (*vxgg_naclfailcb)(void*); - -/** - * @brief Default sodium init fail callback for use in `checksodiumcb()` - * - * @param none Unused param to fit callback spec - */ -static void naclfaildefault(void *none) { - none = none; // Makes gcc happy - if(___VXGG___VERBOSE_ERRORS___) - error(1, ENOTSUP, " Couldn't initialize sodium for some reason. Quitting..."); - exit(EXIT_FAILURE); -} - -/** - * @brief Internal function to deal with the `___VXGG___USE_CLS_CALLBACK___` macro - * - * `checksodiumcb()` runs the sodium function `sodium_init()` and on error calls the provided callback with the provided data. The - * callback and data default to `naclfaildefault` and `NULL`, but can be changed when the `set` parameter is non-zero. When `set` - * is zero, the sodium init check is preformed - * - * @note `checksodiumcb()` is ran when these conditions are true - 1: The `___VXGG___ALWAYS_CHECK_LIBSODIUM___` and `___VXGG___USE_CLS_CALLBACK___` - * macros are both greater than 0 when compiling, and 2: a function in `encryption.c` calls a function originating from sodium. This - * function exists as a way to deal with sodium failing yourself, instead of instantly calling `exit()`. If you don't care to handle - * it, or are initializing sodium yourself, this is unnecessary - * - * @param callback A callback to be ran when sodium fails to initialize itself. Ignored if `set` is zero. Must be non-null when `set` is non-zero - * @param data Data to be passed to the callback when it is fired. Ignored if `set` is zero. May be null - * @param set Flag on whether to check sodium or to set a new callback and data pair. Checks sodium when zero, sets callback & data when non-zero - * @retval int - */ -static int checksodiumcb(vxgg_naclfailcb const callback, void *data, unsigned char set) { - static vxgg_naclfailcb cb = naclfaildefault; - static void *usr = NULL; - int ret; - - if(set) { - if(cb == NULL) ERRRET(EINVAL, -1); - 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 - } - - 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, 1); -} - -#endif - -/** - * @brief Simple function to check if sodium has been properly initialized - * - * `checksodium()` will run in functions located in `encryption.h` only when the macro `___VXGG___ALWAYS_CHECK_LIBSODIUM___` is greater - * than zero when compiling. It will call the `checksodiumcb()` function if compiled with the `___VXGG___USE_CLS_CALLBACK___` macro. - * When called, checksodium will run `sodium_init()`, and will either run the user-defined callback or `XALLOC_EXIT`. - * - */ -static void checksodium(void) { - #if ___VXGG___USE_CLS_CALLBACK___ > 0 - checksodiumcb(NULL, NULL, 0); - #else - - if(sodium_init() < 0) { - errno = ENOTSUP; - XALLOC_EXIT(" Couldn't initialize sodium for some reason. Quitting..."); - } - - #endif - - return; -} - -#endif - /** * @brief open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR * @@ -214,13 +115,16 @@ int maketmp(const char * const dest) { int linkto(const char * const target, int tgfd) { if(!target || tgfd < 0) ERRRET(EINVAL, -1); if(access(target, F_OK) != -1) ERRRET(EEXIST, -1); + char *path = NULL; + int res = -1; asprintf(&path, "/proc/self/fd/%d", tgfd); - if(!path) ERROR(1, errno, " Couldn't get path to move file into system",); + if(!path) {WARN(errno, " Couldn't get path to move file into system",); goto CLEANUP_linkto;} - int res = linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); + res = linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); +CLEANUP_linkto: free(path); return res; } @@ -237,9 +141,6 @@ int linkto(const char * const target, int tgfd) { */ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!src || !dst || !key) ERRRET(EINVAL, -1); - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; @@ -289,9 +190,6 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr */ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!src || !dst || !key) ERRRET(EINVAL, -1); - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif unsigned char cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES], buf[CHUNKSIZE]; unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; @@ -354,9 +252,7 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr * @retval (int)[,] */ int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif + if(!target || !output || !key) ERRRET(EINVAL, -1); int fd = -1, tfd = -1, res = -1; FILE *src, *dst; @@ -409,9 +305,6 @@ CLEANUP_encryptviatmp: */ int decryptto(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!target || !output || !key) ERRRET(EINVAL, -1); - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif FILE *src, *dst; int fdst, eflag = -1, res = -1; @@ -463,9 +356,6 @@ int genpassword(char **str, unsigned int words) { // Early returns if(words < 1) return 0; if(!str) ERRRET(EINVAL, -1); - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif // Bootstrap the first word char *lstr = NULL, *tmp = NULL; @@ -497,10 +387,6 @@ int genpassword(char **str, unsigned int words) { * @retval (void*) A pointer to some data allocated via `sodium_malloc()` */ void* xsodium_malloc(size_t size) { - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif - void *mem = sodium_malloc(size); if(mem == NULL) XALLOC_EXIT(" could not allocate memory... Quitting", ); -- cgit v1.2.3