diff options
Diffstat (limited to 'src/encryption.c')
| -rw-r--r-- | src/encryption.c | 81 |
1 files changed, 18 insertions, 63 deletions
diff --git a/src/encryption.c b/src/encryption.c index 32a5ce0..e4fed1f 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -19,7 +19,11 @@ | |||
| 19 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 19 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 20 | void naclfaildefault(void *none) { | 20 | void naclfaildefault(void *none) { |
| 21 | none = none; // Makes gcc happy | 21 | none = none; // Makes gcc happy |
| 22 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | 22 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 |
| 23 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | ||
| 24 | #else | ||
| 25 | exit(EXIT_FAILURE); | ||
| 26 | #endif | ||
| 23 | } | 27 | } |
| 24 | 28 | ||
| 25 | int checksodiumcb(const vxgg_naclfailcb callback, void *data) { | 29 | int checksodiumcb(const vxgg_naclfailcb callback, void *data) { |
| @@ -49,7 +53,11 @@ void checksodium(void) { | |||
| 49 | checksodiumcb(NULL, NULL); | 53 | checksodiumcb(NULL, NULL); |
| 50 | #else | 54 | #else |
| 51 | if(sodium_init() < 0) | 55 | if(sodium_init() < 0) |
| 52 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | 56 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 |
| 57 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | ||
| 58 | #else | ||
| 59 | exit(EXIT_FAILURE); | ||
| 60 | #endif | ||
| 53 | #endif | 61 | #endif |
| 54 | 62 | ||
| 55 | return; | 63 | return; |
| @@ -75,25 +83,12 @@ int maketmp(const char * const dest) { | |||
| 75 | return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); | 83 | return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); |
| 76 | } | 84 | } |
| 77 | 85 | ||
| 78 | int encrypttotmp(const char * const toencrypt) { | 86 | int encrypttotmp(const char * const target, const char * const output, const char * const password, int chunksize) { |
| 79 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 87 | #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 80 | checksodium(); | 88 | checksodium(); |
| 81 | #endif | 89 | #endif |
| 82 | 90 | ||
| 83 | struct stat esb; | 91 | |
| 84 | int efd = -1; | ||
| 85 | |||
| 86 | // Make sure the file is real and an actual file that can be encrypted | ||
| 87 | if(stat(toencrypt, &esb) < 0) | ||
| 88 | return -1; | ||
| 89 | if(!S_ISREG(esb.st_mode)) | ||
| 90 | return -2; | ||
| 91 | |||
| 92 | // Open the file as read-only | ||
| 93 | if((efd = open(toencrypt, O_RDONLY)) < 0) | ||
| 94 | return -3; | ||
| 95 | |||
| 96 | // Need to get a secret key from a password and then set up cryptostream from libsodium | ||
| 97 | 92 | ||
| 98 | return 0; | 93 | return 0; |
| 99 | } | 94 | } |
| @@ -136,7 +131,11 @@ void* xsodium_malloc(size_t size) { | |||
| 136 | void *mem = sodium_malloc(size); | 131 | void *mem = sodium_malloc(size); |
| 137 | if(mem == NULL) { | 132 | if(mem == NULL) { |
| 138 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 | 133 | #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 |
| 139 | error(1, errno, "xsodium_malloc: could not allocate memory... Quitting"); | 134 | #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 |
| 135 | error(1, errno, "<xsodium_malloc> could not allocate memory... Quitting"); | ||
| 136 | #else | ||
| 137 | exit(EXIT_FAILURE); | ||
| 138 | #endif | ||
| 140 | #endif | 139 | #endif |
| 141 | 140 | ||
| 142 | abort(); | 141 | abort(); |
| @@ -218,7 +217,7 @@ int main(void) { | |||
| 218 | 217 | ||
| 219 | //*/// | 218 | //*/// |
| 220 | 219 | ||
| 221 | //*// Example code for generating a key from a password and encrypting a test file | 220 | /*// Example code for generating a key from a password and encrypting a test file |
| 222 | 221 | ||
| 223 | const char *dir = ".", *fname = "toBeEncrypted.test.txt", *pass = "this is a password"; | 222 | const char *dir = ".", *fname = "toBeEncrypted.test.txt", *pass = "this is a password"; |
| 224 | char *path = NULL, *message = NULL, *efname = NULL; | 223 | char *path = NULL, *message = NULL, *efname = NULL; |
| @@ -302,50 +301,6 @@ int main(void) { | |||
| 302 | free(buf); | 301 | free(buf); |
| 303 | free(cbuf); | 302 | free(cbuf); |
| 304 | 303 | ||
| 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); | ||
| 348 | |||
| 349 | //*/// | 304 | //*/// |
| 350 | 305 | ||
| 351 | return 0; | 306 | return 0; |
