diff options
| author | @syxhe <https://t.me/syxhe> | 2025-01-21 17:45:04 -0600 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-01-21 17:45:04 -0600 |
| commit | e3916f57bd8c1176ade570c1125d4eacd4d6362d (patch) | |
| tree | 38de65ebfed2a5a60a7a6e27d64568d6a23a8ec7 | |
| parent | 5d3068832c6094cf3b3ffce89d2398134e939b1f (diff) | |
This shit corrupted !
| -rw-r--r-- | src/encryption.c | 58 |
1 files changed, 50 insertions, 8 deletions
diff --git a/src/encryption.c b/src/encryption.c index 100b50f..32a5ce0 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -272,15 +272,12 @@ int main(void) { | |||
| 272 | if((tfd = maketmp(dir)) < 0) // Only because linking temp files is being annoying | 272 | if((tfd = maketmp(dir)) < 0) // Only because linking temp files is being annoying |
| 273 | error(1, errno, "Could not open temp file for encryption"); | 273 | error(1, errno, "Could not open temp file for encryption"); |
| 274 | 274 | ||
| 275 | struct stat sb; | ||
| 276 | if(fstat(fd, &sb) < 0) | ||
| 277 | error(1, errno, "stat() error"); | ||
| 278 | |||
| 279 | // Read chunks of the file, encrypt them, then write them into the tmp file | 275 | // Read chunks of the file, encrypt them, then write them into the tmp file |
| 280 | ssize_t bytesread = -1; | 276 | ssize_t bytesread = -1; |
| 281 | unsigned char *buf = xcalloc(sb.st_blksize + 1, sizeof(*buf)); | 277 | const int CHUNK_SIZE = 4096; |
| 282 | unsigned char *cbuf = xcalloc((sb.st_blksize + 1) + crypto_secretstream_xchacha20poly1305_ABYTES, sizeof(*buf)); | 278 | unsigned char *buf = xcalloc(CHUNK_SIZE + 1, sizeof(*buf)); |
| 283 | while((bytesread = read(fd, buf, sb.st_blksize)) >= 0) { | 279 | unsigned char *cbuf = xcalloc((CHUNK_SIZE + 1) + crypto_secretstream_xchacha20poly1305_ABYTES, sizeof(*cbuf)); |
| 280 | while((bytesread = read(fd, buf, CHUNK_SIZE)) >= 0) { | ||
| 284 | crypto_secretstream_xchacha20poly1305_push(&state, cbuf, NULL, buf, bytesread, NULL, 0, (bytesread > 0) ? 0 : crypto_secretstream_xchacha20poly1305_TAG_FINAL); | 281 | crypto_secretstream_xchacha20poly1305_push(&state, cbuf, NULL, buf, bytesread, NULL, 0, (bytesread > 0) ? 0 : crypto_secretstream_xchacha20poly1305_TAG_FINAL); |
| 285 | if(writewholebuffer(tfd, cbuf, bytesread) < 0) | 282 | if(writewholebuffer(tfd, cbuf, bytesread) < 0) |
| 286 | error(1, errno, "write() error"); | 283 | error(1, errno, "write() error"); |
| @@ -302,7 +299,52 @@ int main(void) { | |||
| 302 | 299 | ||
| 303 | close(tfd); | 300 | close(tfd); |
| 304 | free(path); | 301 | free(path); |
| 305 | free(efname); | 302 | free(buf); |
| 303 | free(cbuf); | ||
| 304 | |||
| 305 | |||
| 306 | // Ok now decryption to make sure I didn't fuck it | ||
| 307 | unsigned char dheader[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; | ||
| 308 | crypto_secretstream_xchacha20poly1305_state dstate; | ||
| 309 | |||
| 310 | if((fd = open(efname, O_RDONLY)) < 0) | ||
| 311 | error(1, errno, "Could not open file for decryption"); | ||
| 312 | if((tfd = open("lmao.test.dec", O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR | S_IWUSR))) < 0) | ||
| 313 | error(1, errno, "Could not open file for result of decryption"); | ||
| 314 | |||
| 315 | if(read(fd, header, sizeof(header)) < 0) | ||
| 316 | error(1, errno, "Could not read header of encrypted file"); | ||
| 317 | |||
| 318 | if(crypto_secretstream_xchacha20poly1305_init_pull(&dstate, dheader, key) != 0) | ||
| 319 | error(1, EINVAL, "Incomplete header"); | ||
| 320 | |||
| 321 | bytesread = -1; | ||
| 322 | unsigned char tag = 255; | ||
| 323 | buf = xcalloc(CHUNK_SIZE + 1, sizeof(*buf)); | ||
| 324 | cbuf = xcalloc((CHUNK_SIZE + 1) + crypto_secretstream_xchacha20poly1305_ABYTES, sizeof(*cbuf)); | ||
| 325 | while((bytesread = read(fd, cbuf, (CHUNK_SIZE + 1) + crypto_secretstream_xchacha20poly1305_ABYTES)) >= 0) { | ||
| 326 | if(crypto_secretstream_xchacha20poly1305_pull(&dstate, buf, NULL, &tag, cbuf, bytesread, NULL, 0) < 0) | ||
| 327 | error(1, errno, "Found a corrupted chunk"); | ||
| 328 | |||
| 329 | if(tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && bytesread != 0) | ||
| 330 | error(1, errno, "End tag before end of file"); | ||
| 331 | |||
| 332 | if(bytesread == 0 && tag != crypto_secretstream_xchacha20poly1305_TAG_FINAL) | ||
| 333 | error(1, errno, "End of file before end tag"); | ||
| 334 | |||
| 335 | if(writewholebuffer(tfd, buf, bytesread) < 0) | ||
| 336 | error(1, errno, "write() error"); | ||
| 337 | |||
| 338 | if(bytesread == 0) | ||
| 339 | break; | ||
| 340 | } | ||
| 341 | if(bytesread < 0) | ||
| 342 | error(1, errno, "read() error"); | ||
| 343 | |||
| 344 | close(fd); | ||
| 345 | close(tfd); | ||
| 346 | free(buf); | ||
| 347 | free(cbuf); | ||
| 306 | 348 | ||
| 307 | //*/// | 349 | //*/// |
| 308 | 350 | ||
