From c8ab26e9f5aa398a208fcac3d8d11335f6c72632 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 10 Nov 2025 16:09:10 -0600 Subject: Touchup --- src/encryption.c | 67 +++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 35 deletions(-) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index 2d48e4e..9b264dc 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -138,6 +138,7 @@ CLEANUP_linkto: * @param dst Destination to write encrypted file * @param key Key for encryption * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error + * @todo Rewrite this into being one of my own functions instead of copying from libsodium */ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!src || !dst || !key) ERRRET(EINVAL, -1); @@ -187,6 +188,7 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr * @param dst Destination to write decrypted file * @param key Key used to encrypt * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error + * @todo Rewrite this into being one of my own functions instead of copying from libsodium */ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!src || !dst || !key) ERRRET(EINVAL, -1); @@ -250,42 +252,55 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr * @param output * @param key * @retval (int)[,] + * @todo Fill out warning messages & documentation */ int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { if(!target || !output || !key) ERRRET(EINVAL, -1); - int fd = -1, tfd = -1, res = -1; + int fd = -1, tfd = -1, res = -1, eflag = 0; FILE *src, *dst; char *targetdir; // Open the target file - if((fd = open(target, O_RDONLY)) < 0) return -1; + if((fd = open(target, O_RDONLY)) < 0) {eflag = 1; goto CLEANUP_encryptviatmp;} // Create a temp file for writing targetdir = vxdirname(output); - if(!targetdir) goto CLEANUP_encryptviatmp; + if(!targetdir) {eflag = 2; goto CLEANUP_encryptviatmp;} // Actually get the file descriptor for the temp file tfd = maketmp(targetdir); - if(tfd < 0) goto CLEANUP_encryptviatmp; + if(tfd < 0) {eflag = 3; goto CLEANUP_encryptviatmp;} // Create a FILE* version of the source fd - if(!(src = fdopen(fd, "rb"))) goto CLEANUP_encryptviatmp; + if(!(src = fdopen(fd, "rb"))) {eflag = 4; goto CLEANUP_encryptviatmp;} // Create a FILE* version of the target fd - if(!(dst = fdopen(tfd, "wb"))) goto CLEANUP_encryptviatmp; + if(!(dst = fdopen(tfd, "wb"))) {eflag = 5; goto CLEANUP_encryptviatmp;} // Do the encryption now that everything has been set up - if(encrypttofile(src, dst, key) < 0) // Not going to bother changing this, it probably is catastrophic if an error happens when it shouldn't - ERROR(1, ENOTRECOVERABLE, " I don't even have a way to cause an error here. How did you do it?",); + if(encrypttofile(src, dst, key) < 0) {eflag = 6; goto CLEANUP_encryptviatmp;} // Link the temp file into the system - if(linkto(output, tfd) < 0) - WARN(errno, " Could not link \"%s\" into system after encryption", , output); + if(linkto(output, tfd) < 0) {eflag = 7; goto CLEANUP_encryptviatmp;} res = 0; CLEANUP_encryptviatmp: + // TODO: Add warning messages for verbose errors + if(___VXGG___VERBOSE_ERRORS___) { + switch (eflag) { + case 1: WARN(errno, " Warning: Could not open target fd \"%s\"",, target); + case 2: WARN(errno, " Warning: Could not get real dirname for \"%s\"",, output); + case 3: WARN(errno, " Warning: Could not make temp file in target dir \"%s\"",, targetdir); + case 4: WARN(errno, " Warning: Could not get FILE* handle for source file \"%s\"",, target); + case 5: WARN(errno, " Warning: Could not get FILE* handle for output file",); + case 6: ERROR(1, ENOTRECOVERABLE, " ERROR: I don't even have a way to cause an error here. How did you do it?",); + case 7: WARN(errno, " Warning: Could not link \"%s\" into system after encryption",, output); + } + } + + free(targetdir); fclose(src); fclose(dst); @@ -302,6 +317,7 @@ CLEANUP_encryptviatmp: * @param target * @param key * @retval (int)[,] + * @todo Fill out documentation */ 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); @@ -333,12 +349,12 @@ CLEANUP_decryptto: if(___VXGG___VERBOSE_ERRORS___) { switch (eflag) { - case 0: WARN(errno, " Could not open \"%s\" for decryption", , target); break; - case 1: WARN(errno, " Could not get temp file for decryption", ); break; - case 2: WARN(errno, " Could not open \"%s\" for writing decrypted data", , output); break; - case 3: ERROR(1, errno, " How did you even cause an error?",); break; - case 4: WARN(errno, " Could not link \"%s\" into system", , output); break; - default: WARN(errno, " Ran into an error", ); break; + case 0: WARN(errno, " Could not open \"%s\" for decryption",, target); break; + case 1: WARN(errno, " Could not get temp file for decryption",); break; + case 2: WARN(errno, " Could not open \"%s\" for writing decrypted data",, output); break; + case 3: ERROR(1, errno, " How did you even cause an error?",); break; + case 4: WARN(errno, " Could not link \"%s\" into system",, output); break; + default: WARN(errno, " Ran into an error",); break; } } @@ -353,7 +369,6 @@ CLEANUP_decryptto: * @retval (int)[-1, words] On success, returns the number of words requested. On error, returns -1 and sets errno */ int genpassword(char **str, unsigned int words) { - // Early returns if(words < 1) return 0; if(!str) ERRRET(EINVAL, -1); @@ -378,24 +393,6 @@ int genpassword(char **str, unsigned int words) { return words; } -/** - * @brief sodium_malloc wrapper. - * - * Calls `error()` or `abort()` depnding on the value of `___VXGG___VXALLOC_EXIT_ON_ERROR___`. Will make sure libsodium is initialized if `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0` - * - * @param size - * @retval (void*) A pointer to some data allocated via `sodium_malloc()` - */ -void* xsodium_malloc(size_t size) { - void *mem = sodium_malloc(size); - if(mem == NULL) - XALLOC_EXIT(" could not allocate memory... Quitting", ); - - return mem; -} - - - // TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task // #include -- cgit v1.2.3