diff options
| author | @syxhe <https://t.me/syxhe> | 2025-06-14 17:27:58 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-06-14 17:27:58 -0500 |
| commit | d26eeecfc7e4c997bc441a0879ddb8c2aba3898f (patch) | |
| tree | 277089b39c33618ac177b0a06de5dca1bf1e72dd | |
| parent | 1536f1e0287b8281014200ef6911b294272c4773 (diff) | |
Fix encrypt and decrypttofile functions
| -rw-r--r-- | src/encryption.c | 65 |
1 files changed, 42 insertions, 23 deletions
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 | |||
| 285 | return 0; | 285 | return 0; |
| 286 | } | 286 | } |
| 287 | 287 | ||
| 288 | // TODO: Fix this mess | ||
| 289 | int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 288 | int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 290 | if(!src || !dst || !key) ERRRET(EINVAL, -1); | 289 | if(!src || !dst || !key) ERRRET(EINVAL, -1); |
| 291 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 290 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| @@ -302,28 +301,34 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr | |||
| 302 | 301 | ||
| 303 | // Write the header | 302 | // Write the header |
| 304 | crypto_secretstream_xchacha20poly1305_init_push(&state, header, key); | 303 | crypto_secretstream_xchacha20poly1305_init_push(&state, header, key); |
| 305 | if(fwrite(header, 1, sizeof(header), dst) < sizeof(header)) | 304 | if(fwrite(header, 1, sizeof(header), dst) < sizeof(header)) { |
| 306 | if(ferror(dst)) | 305 | if(ferror(dst)) { |
| 307 | ERROR(1, errno, "<encrypttofile> Could not write header",); | 306 | WARN(errno, "<encrypttofile> Could not write header",); |
| 307 | return -1; | ||
| 308 | } | ||
| 309 | } | ||
| 308 | 310 | ||
| 309 | // Encrypt each chunk | 311 | // Encrypt each chunk |
| 310 | do { | 312 | do { |
| 311 | if((bytesread = fread(buf, 1, sizeof(buf), src)) < sizeof(buf)) | 313 | if((bytesread = fread(buf, 1, sizeof(buf), src)) < sizeof(buf)) |
| 312 | if(ferror(src)) | 314 | if(ferror(src)) { |
| 313 | ERROR(1, errno, "<encrypttofile> Could not read from source",); | 315 | WARN(errno, "<encrypttofile> Could not read from source",); |
| 316 | return -1; | ||
| 317 | } | ||
| 314 | eof = feof(src); | 318 | eof = feof(src); |
| 315 | tag = eof ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; | 319 | tag = eof ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0; |
| 316 | 320 | ||
| 317 | crypto_secretstream_xchacha20poly1305_push(&state, cbuf, &cbuflen, buf, bytesread, NULL, 0, tag); | 321 | crypto_secretstream_xchacha20poly1305_push(&state, cbuf, &cbuflen, buf, bytesread, NULL, 0, tag); |
| 318 | if(fwrite(cbuf, 1, (size_t)cbuflen, dst) < (size_t)cbuflen) | 322 | if(fwrite(cbuf, 1, (size_t)cbuflen, dst) < (size_t)cbuflen) |
| 319 | if(ferror(dst)) | 323 | if(ferror(dst)) { |
| 320 | ERROR(1, errno, "<encrypttofile> Could not write to target",); | 324 | WARN(errno, "<encrypttofile> Could not write to target",); |
| 325 | return -1; | ||
| 326 | } | ||
| 321 | } while (!eof); | 327 | } while (!eof); |
| 322 | 328 | ||
| 323 | return 0; | 329 | return 0; |
| 324 | } | 330 | } |
| 325 | 331 | ||
| 326 | // TODO: Fix this as well | ||
| 327 | int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 332 | int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 328 | if(!src || !dst || !key) ERRRET(EINVAL, -1); | 333 | if(!src || !dst || !key) ERRRET(EINVAL, -1); |
| 329 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 334 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| @@ -339,28 +344,42 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr | |||
| 339 | int eof; | 344 | int eof; |
| 340 | 345 | ||
| 341 | // Read the header | 346 | // Read the header |
| 342 | if(fread(header, 1, sizeof(header), src) < sizeof(header)) | 347 | if(fread(header, 1, sizeof(header), src) < sizeof(header)) { |
| 343 | if(ferror(src)) | 348 | if(ferror(src)) { |
| 344 | ERROR(1, errno, "<decrypttofile> Couldn't read header", ); | 349 | WARN(errno, "<decrypttofile> Couldn't read header", ); |
| 350 | return -1; | ||
| 351 | } | ||
| 352 | } | ||
| 345 | 353 | ||
| 346 | // Make sure the header isn't fuckey | 354 | // Make sure the header isn't fuckey |
| 347 | if(crypto_secretstream_xchacha20poly1305_init_pull(&state, header, key) != 0) | 355 | if(crypto_secretstream_xchacha20poly1305_init_pull(&state, header, key) != 0) { |
| 348 | ERROR(1, errno, "<decrypttofile> Incomplete header", ); | 356 | WARN(errno, "<decrypttofile> Incomplete header", ); |
| 357 | return -1; | ||
| 358 | } | ||
| 349 | 359 | ||
| 350 | // Decrypt each chunk | 360 | // Decrypt each chunk |
| 351 | do { | 361 | do { |
| 352 | if((bytesread = fread(cbuf, 1, sizeof(cbuf), src)) < sizeof(cbuf)) | 362 | if((bytesread = fread(cbuf, 1, sizeof(cbuf), src)) < sizeof(cbuf)) { |
| 353 | if(ferror(src)) | 363 | if(ferror(src)) { |
| 354 | ERROR(1, errno, "<decrypttofile> Ran into problem reading for decryption", ); | 364 | WARN(errno, "<decrypttofile> Ran into problem reading for decryption", ); |
| 365 | return -1; | ||
| 366 | } | ||
| 367 | } | ||
| 355 | eof = feof(src); | 368 | eof = feof(src); |
| 356 | 369 | ||
| 357 | if (crypto_secretstream_xchacha20poly1305_pull(&state, buf, &buflen, &tag, cbuf, bytesread, NULL, 0) != 0) | 370 | if (crypto_secretstream_xchacha20poly1305_pull(&state, buf, &buflen, &tag, cbuf, bytesread, NULL, 0) != 0) { |
| 358 | ERROR(1, errno, "<decrypttofile> Corrupted chunk", ); | 371 | WARN(errno, "<decrypttofile> Corrupted chunk", ); |
| 372 | return -1; | ||
| 373 | } | ||
| 359 | 374 | ||
| 360 | if(tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !eof) | 375 | if(tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !eof) { |
| 361 | ERROR(1, errno, "<decrypttofile> End of stream before end of file", ); | 376 | WARN(errno, "<decrypttofile> End of stream before end of file", ); |
| 362 | if(eof && tag != crypto_secretstream_xchacha20poly1305_TAG_FINAL) | 377 | return -1; |
| 363 | ERROR(1, errno, "<decrypttofile> End of file before end of stream", ); | 378 | } |
| 379 | if(eof && tag != crypto_secretstream_xchacha20poly1305_TAG_FINAL) { | ||
| 380 | WARN(errno, "<decrypttofile> End of file before end of stream", ); | ||
| 381 | return -1; | ||
| 382 | } | ||
| 364 | 383 | ||
| 365 | fwrite(buf, 1, (size_t)buflen, dst); | 384 | fwrite(buf, 1, (size_t)buflen, dst); |
| 366 | } while(! eof); | 385 | } while(! eof); |
