From 31f211a5d0969b07e98414fb47a5b5945200ddb6 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 31 Mar 2025 16:20:42 -0500 Subject: Create decryptto function --- src/encryption.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index 606be03..c176d6e 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -29,7 +29,7 @@ int checksodiumcb(const vxgg_naclfailcb callback, void *data, unsigned char set) static vxgg_naclfailcb cb = naclfaildefault; static void *usr = NULL; int ret; - + if(set) { cb = callback; usr = data; @@ -73,7 +73,7 @@ int maketmp(const char * const dest) { return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); } -int encrypttotmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { +int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 checksodium(); #endif @@ -106,7 +106,6 @@ int encrypttotmp(const char * const target, const char * const output, const uns asprintf(&path, "/proc/self/fd/%d", tfd); if(!path) return -1; - remove(output); // Make sure an old version isn't sticking around linkat(AT_FDCWD, path, AT_FDCWD, output, AT_SYMLINK_FOLLOW); @@ -118,6 +117,33 @@ int encrypttotmp(const char * const target, const char * const output, const uns return 0; } +int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { + #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 + checksodium(); + #endif + + if(!encrypted) + RETURNWERR(EINVAL, -1); + if(!target) + RETURNWERR(EINVAL, -1); + if(!key) + RETURNWERR(EINVAL, -1); + + FILE *src, *dst; + if(!(src = fopen(encrypted, "rb"))) + ERROR(1, errno, "Could not open \"%s\" for decryption", , encrypted); + if(!(dst = fopen(target, "wb"))) + ERROR(1, errno, "Could not open \"%s\" for writing decrypted data", , target); + + if(decrypttofile(src, dst, key) < 0) + ERROR(1, errno, "How did you even cause an error?",); + + fclose(dst); + fclose(src); + + return 0; +} + int encrypttofile(FILE *dst, FILE *src, unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; -- cgit v1.2.3