From 08fb644c7d101551edfe8fc2608e0ac501b3df9f Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 8 Jun 2025 17:25:13 -0500 Subject: Trim the fat --- src/encryption.c | 56 ++++++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 34 deletions(-) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index 3bf9dd4..b6832ed 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -1,4 +1,7 @@ // 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 @@ -27,7 +30,7 @@ void naclfaildefault(void *none) { exit(EXIT_FAILURE); } -int checksodiumcb(const vxgg_naclfailcb callback, void *data, unsigned char set) { +int checksodiumcb(vxgg_naclfailcb const callback, void *data, unsigned char set) { static vxgg_naclfailcb cb = naclfaildefault; static void *usr = NULL; int ret; @@ -84,7 +87,9 @@ int linkto(const char * const target, int tgfd) { if(!path) ERROR(1, errno, " Couldn't get path to move file into system",); 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) - return linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); + int res = linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); + free(path); + return res; } int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { @@ -127,17 +132,11 @@ int encryptviatmp(const char * const target, const char * const output, const un } int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { + if(!encrypted || !target || !key) ERRRET(EINVAL, -1); #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 checksodium(); #endif - - if(!encrypted) - ERRRET(EINVAL, -1); - if(!target) - ERRRET(EINVAL, -1); - if(!key) - ERRRET(EINVAL, -1); - + FILE *src, *dst; if(!(src = fopen(encrypted, "rb"))) ERROR(1, errno, " Could not open \"%s\" for decryption", , encrypted); @@ -164,6 +163,11 @@ int decryptto(const char * const encrypted, const char * const target, const uns } 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]; crypto_secretstream_xchacha20poly1305_state state; @@ -172,17 +176,6 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr size_t bytesread; int eof; - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif - - if(!src) - ERRRET(EINVAL, -1); - if(!dst) - ERRRET(EINVAL, -1); - if(!key) - ERRRET(EINVAL, -1); - // Write the header crypto_secretstream_xchacha20poly1305_init_push(&state, header, key); if(fwrite(header, 1, sizeof(header), dst) < sizeof(header)) @@ -206,7 +199,12 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr return 0; } -int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { +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]; crypto_secretstream_xchacha20poly1305_state state; @@ -215,17 +213,6 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr size_t bytesread; int eof; - #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 - checksodium(); - #endif - - if(!src) - ERRRET(EINVAL, -1); - if(!dst) - ERRRET(EINVAL, -1); - if(!key) - ERRRET(EINVAL, -1); - // Read the header if(fread(header, 1, sizeof(header), src) < sizeof(header)) if(ferror(src)) @@ -258,7 +245,8 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr int genpassword(char **str, unsigned int words) { // Early returns - if(words < 1) {return 0;} + if(words < 1) return 0; + if(!str) ERRRET(EINVAL, -1); #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 checksodium(); #endif -- cgit v1.2.3