From d26eeecfc7e4c997bc441a0879ddb8c2aba3898f Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sat, 14 Jun 2025 17:27:58 -0500 Subject: Fix encrypt and decrypttofile functions --- src/encryption.c | 65 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/encryption.c b/src/encryption.c index a3f75d1..02abccd 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -285,7 +285,6 @@ int decryptto(const char * const encrypted, const char * const target, const uns return 0; } -// TODO: Fix this mess 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 @@ -302,28 +301,34 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr // Write the header crypto_secretstream_xchacha20poly1305_init_push(&state, header, key); - if(fwrite(header, 1, sizeof(header), dst) < sizeof(header)) - if(ferror(dst)) - ERROR(1, errno, " Could not write header",); + if(fwrite(header, 1, sizeof(header), dst) < sizeof(header)) { + if(ferror(dst)) { + WARN(errno, " Could not write header",); + return -1; + } + } // Encrypt each chunk do { if((bytesread = fread(buf, 1, sizeof(buf), src)) < sizeof(buf)) - if(ferror(src)) - ERROR(1, errno, " Could not read from source",); + if(ferror(src)) { + WARN(errno, " Could not read from source",); + return -1; + } eof = feof(src); tag = eof ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; crypto_secretstream_xchacha20poly1305_push(&state, cbuf, &cbuflen, buf, bytesread, NULL, 0, tag); if(fwrite(cbuf, 1, (size_t)cbuflen, dst) < (size_t)cbuflen) - if(ferror(dst)) - ERROR(1, errno, " Could not write to target",); + if(ferror(dst)) { + WARN(errno, " Could not write to target",); + return -1; + } } while (!eof); return 0; } -// TODO: Fix this as well 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 @@ -339,28 +344,42 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr int eof; // Read the header - if(fread(header, 1, sizeof(header), src) < sizeof(header)) - if(ferror(src)) - ERROR(1, errno, " Couldn't read header", ); + if(fread(header, 1, sizeof(header), src) < sizeof(header)) { + if(ferror(src)) { + WARN(errno, " Couldn't read header", ); + return -1; + } + } // Make sure the header isn't fuckey - if(crypto_secretstream_xchacha20poly1305_init_pull(&state, header, key) != 0) - ERROR(1, errno, " Incomplete header", ); + if(crypto_secretstream_xchacha20poly1305_init_pull(&state, header, key) != 0) { + WARN(errno, " Incomplete header", ); + return -1; + } // Decrypt each chunk do { - if((bytesread = fread(cbuf, 1, sizeof(cbuf), src)) < sizeof(cbuf)) - if(ferror(src)) - ERROR(1, errno, " Ran into problem reading for decryption", ); + if((bytesread = fread(cbuf, 1, sizeof(cbuf), src)) < sizeof(cbuf)) { + if(ferror(src)) { + WARN(errno, " Ran into problem reading for decryption", ); + return -1; + } + } eof = feof(src); - if (crypto_secretstream_xchacha20poly1305_pull(&state, buf, &buflen, &tag, cbuf, bytesread, NULL, 0) != 0) - ERROR(1, errno, " Corrupted chunk", ); + if (crypto_secretstream_xchacha20poly1305_pull(&state, buf, &buflen, &tag, cbuf, bytesread, NULL, 0) != 0) { + WARN(errno, " Corrupted chunk", ); + return -1; + } - if(tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !eof) - ERROR(1, errno, " End of stream before end of file", ); - if(eof && tag != crypto_secretstream_xchacha20poly1305_TAG_FINAL) - ERROR(1, errno, " End of file before end of stream", ); + if(tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !eof) { + WARN(errno, " End of stream before end of file", ); + return -1; + } + if(eof && tag != crypto_secretstream_xchacha20poly1305_TAG_FINAL) { + WARN(errno, " End of file before end of stream", ); + return -1; + } fwrite(buf, 1, (size_t)buflen, dst); } while(! eof); -- cgit v1.2.3