summaryrefslogtreecommitdiff
path: root/src/encryption.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/encryption.c')
-rw-r--r--src/encryption.c67
1 files changed, 32 insertions, 35 deletions
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:
138 * @param dst Destination to write encrypted file 138 * @param dst Destination to write encrypted file
139 * @param key Key for encryption 139 * @param key Key for encryption
140 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error 140 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
141 * @todo Rewrite this into being one of my own functions instead of copying from libsodium
141 */ 142 */
142int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { 143int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
143 if(!src || !dst || !key) ERRRET(EINVAL, -1); 144 if(!src || !dst || !key) ERRRET(EINVAL, -1);
@@ -187,6 +188,7 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr
187 * @param dst Destination to write decrypted file 188 * @param dst Destination to write decrypted file
188 * @param key Key used to encrypt 189 * @param key Key used to encrypt
189 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error 190 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
191 * @todo Rewrite this into being one of my own functions instead of copying from libsodium
190 */ 192 */
191int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { 193int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
192 if(!src || !dst || !key) ERRRET(EINVAL, -1); 194 if(!src || !dst || !key) ERRRET(EINVAL, -1);
@@ -250,42 +252,55 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr
250 * @param output 252 * @param output
251 * @param key 253 * @param key
252 * @retval (int)[,] 254 * @retval (int)[,]
255 * @todo Fill out warning messages & documentation
253 */ 256 */
254int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { 257int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
255 if(!target || !output || !key) ERRRET(EINVAL, -1); 258 if(!target || !output || !key) ERRRET(EINVAL, -1);
256 259
257 int fd = -1, tfd = -1, res = -1; 260 int fd = -1, tfd = -1, res = -1, eflag = 0;
258 FILE *src, *dst; 261 FILE *src, *dst;
259 char *targetdir; 262 char *targetdir;
260 263
261 // Open the target file 264 // Open the target file
262 if((fd = open(target, O_RDONLY)) < 0) return -1; 265 if((fd = open(target, O_RDONLY)) < 0) {eflag = 1; goto CLEANUP_encryptviatmp;}
263 266
264 // Create a temp file for writing 267 // Create a temp file for writing
265 targetdir = vxdirname(output); 268 targetdir = vxdirname(output);
266 if(!targetdir) goto CLEANUP_encryptviatmp; 269 if(!targetdir) {eflag = 2; goto CLEANUP_encryptviatmp;}
267 270
268 // Actually get the file descriptor for the temp file 271 // Actually get the file descriptor for the temp file
269 tfd = maketmp(targetdir); 272 tfd = maketmp(targetdir);
270 if(tfd < 0) goto CLEANUP_encryptviatmp; 273 if(tfd < 0) {eflag = 3; goto CLEANUP_encryptviatmp;}
271 274
272 // Create a FILE* version of the source fd 275 // Create a FILE* version of the source fd
273 if(!(src = fdopen(fd, "rb"))) goto CLEANUP_encryptviatmp; 276 if(!(src = fdopen(fd, "rb"))) {eflag = 4; goto CLEANUP_encryptviatmp;}
274 277
275 // Create a FILE* version of the target fd 278 // Create a FILE* version of the target fd
276 if(!(dst = fdopen(tfd, "wb"))) goto CLEANUP_encryptviatmp; 279 if(!(dst = fdopen(tfd, "wb"))) {eflag = 5; goto CLEANUP_encryptviatmp;}
277 280
278 // Do the encryption now that everything has been set up 281 // Do the encryption now that everything has been set up
279 if(encrypttofile(src, dst, key) < 0) // Not going to bother changing this, it probably is catastrophic if an error happens when it shouldn't 282 if(encrypttofile(src, dst, key) < 0) {eflag = 6; goto CLEANUP_encryptviatmp;}
280 ERROR(1, ENOTRECOVERABLE, "<encryptviatmp> I don't even have a way to cause an error here. How did you do it?",);
281 283
282 // Link the temp file into the system 284 // Link the temp file into the system
283 if(linkto(output, tfd) < 0) 285 if(linkto(output, tfd) < 0) {eflag = 7; goto CLEANUP_encryptviatmp;}
284 WARN(errno, "<encryptviatmp> Could not link \"%s\" into system after encryption", , output);
285 286
286 res = 0; 287 res = 0;
287 288
288CLEANUP_encryptviatmp: 289CLEANUP_encryptviatmp:
290 // TODO: Add warning messages for verbose errors
291 if(___VXGG___VERBOSE_ERRORS___) {
292 switch (eflag) {
293 case 1: WARN(errno, "<encryptviatmp> Warning: Could not open target fd \"%s\"",, target);
294 case 2: WARN(errno, "<encryptviatmp> Warning: Could not get real dirname for \"%s\"",, output);
295 case 3: WARN(errno, "<encryptviatmp> Warning: Could not make temp file in target dir \"%s\"",, targetdir);
296 case 4: WARN(errno, "<encryptviatmp> Warning: Could not get FILE* handle for source file \"%s\"",, target);
297 case 5: WARN(errno, "<encryptviatmp> Warning: Could not get FILE* handle for output file",);
298 case 6: ERROR(1, ENOTRECOVERABLE, "<encryptviatmp> ERROR: I don't even have a way to cause an error here. How did you do it?",);
299 case 7: WARN(errno, "<encryptviatmp> Warning: Could not link \"%s\" into system after encryption",, output);
300 }
301 }
302
303
289 free(targetdir); 304 free(targetdir);
290 fclose(src); 305 fclose(src);
291 fclose(dst); 306 fclose(dst);
@@ -302,6 +317,7 @@ CLEANUP_encryptviatmp:
302 * @param target 317 * @param target
303 * @param key 318 * @param key
304 * @retval (int)[,] 319 * @retval (int)[,]
320 * @todo Fill out documentation
305 */ 321 */
306int decryptto(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { 322int decryptto(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
307 if(!target || !output || !key) ERRRET(EINVAL, -1); 323 if(!target || !output || !key) ERRRET(EINVAL, -1);
@@ -333,12 +349,12 @@ CLEANUP_decryptto:
333 349
334 if(___VXGG___VERBOSE_ERRORS___) { 350 if(___VXGG___VERBOSE_ERRORS___) {
335 switch (eflag) { 351 switch (eflag) {
336 case 0: WARN(errno, "<decryptto> Could not open \"%s\" for decryption", , target); break; 352 case 0: WARN(errno, "<decryptto> Could not open \"%s\" for decryption",, target); break;
337 case 1: WARN(errno, "<decryptto> Could not get temp file for decryption", ); break; 353 case 1: WARN(errno, "<decryptto> Could not get temp file for decryption",); break;
338 case 2: WARN(errno, "<decryptto> Could not open \"%s\" for writing decrypted data", , output); break; 354 case 2: WARN(errno, "<decryptto> Could not open \"%s\" for writing decrypted data",, output); break;
339 case 3: ERROR(1, errno, "<decryptto> How did you even cause an error?",); break; 355 case 3: ERROR(1, errno, "<decryptto> How did you even cause an error?",); break;
340 case 4: WARN(errno, "<decryptto> Could not link \"%s\" into system", , output); break; 356 case 4: WARN(errno, "<decryptto> Could not link \"%s\" into system",, output); break;
341 default: WARN(errno, "<decryptto> Ran into an error", ); break; 357 default: WARN(errno, "<decryptto> Ran into an error",); break;
342 } 358 }
343 } 359 }
344 360
@@ -353,7 +369,6 @@ CLEANUP_decryptto:
353 * @retval (int)[-1, words] On success, returns the number of words requested. On error, returns -1 and sets errno 369 * @retval (int)[-1, words] On success, returns the number of words requested. On error, returns -1 and sets errno
354 */ 370 */
355int genpassword(char **str, unsigned int words) { 371int genpassword(char **str, unsigned int words) {
356 // Early returns
357 if(words < 1) return 0; 372 if(words < 1) return 0;
358 if(!str) ERRRET(EINVAL, -1); 373 if(!str) ERRRET(EINVAL, -1);
359 374
@@ -378,24 +393,6 @@ int genpassword(char **str, unsigned int words) {
378 return words; 393 return words;
379} 394}
380 395
381/**
382 * @brief sodium_malloc wrapper.
383 *
384 * 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`
385 *
386 * @param size
387 * @retval (void*) A pointer to some data allocated via `sodium_malloc()`
388 */
389void* xsodium_malloc(size_t size) {
390 void *mem = sodium_malloc(size);
391 if(mem == NULL)
392 XALLOC_EXIT("<xsodium_malloc> could not allocate memory... Quitting", );
393
394 return mem;
395}
396
397
398
399// TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task 396// TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task
400 397
401// #include <dirent.h> 398// #include <dirent.h>