diff options
| author | @syxhe <https://t.me/syxhe> | 2025-03-31 16:20:42 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-03-31 16:20:42 -0500 |
| commit | 31f211a5d0969b07e98414fb47a5b5945200ddb6 (patch) | |
| tree | 3ee57a12b5dcf83395c2f258b9950e5786c12968 /src/encryption.c | |
| parent | 7424be8e0b033fd6466d517d6a8e3f0fb545dd59 (diff) | |
Create decryptto function
Diffstat (limited to 'src/encryption.c')
| -rw-r--r-- | src/encryption.c | 32 |
1 files changed, 29 insertions, 3 deletions
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) | |||
| 29 | static vxgg_naclfailcb cb = naclfaildefault; | 29 | static vxgg_naclfailcb cb = naclfaildefault; |
| 30 | static void *usr = NULL; | 30 | static void *usr = NULL; |
| 31 | int ret; | 31 | int ret; |
| 32 | 32 | ||
| 33 | if(set) { | 33 | if(set) { |
| 34 | cb = callback; | 34 | cb = callback; |
| 35 | usr = data; | 35 | usr = data; |
| @@ -73,7 +73,7 @@ int maketmp(const char * const dest) { | |||
| 73 | return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); | 73 | return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | int encrypttotmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 76 | int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 77 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 77 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 78 | checksodium(); | 78 | checksodium(); |
| 79 | #endif | 79 | #endif |
| @@ -106,7 +106,6 @@ int encrypttotmp(const char * const target, const char * const output, const uns | |||
| 106 | asprintf(&path, "/proc/self/fd/%d", tfd); | 106 | asprintf(&path, "/proc/self/fd/%d", tfd); |
| 107 | if(!path) | 107 | if(!path) |
| 108 | return -1; | 108 | return -1; |
| 109 | |||
| 110 | remove(output); // Make sure an old version isn't sticking around | 109 | remove(output); // Make sure an old version isn't sticking around |
| 111 | linkat(AT_FDCWD, path, AT_FDCWD, output, AT_SYMLINK_FOLLOW); | 110 | linkat(AT_FDCWD, path, AT_FDCWD, output, AT_SYMLINK_FOLLOW); |
| 112 | 111 | ||
| @@ -118,6 +117,33 @@ int encrypttotmp(const char * const target, const char * const output, const uns | |||
| 118 | return 0; | 117 | return 0; |
| 119 | } | 118 | } |
| 120 | 119 | ||
| 120 | int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | ||
| 121 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 122 | checksodium(); | ||
| 123 | #endif | ||
| 124 | |||
| 125 | if(!encrypted) | ||
| 126 | RETURNWERR(EINVAL, -1); | ||
| 127 | if(!target) | ||
| 128 | RETURNWERR(EINVAL, -1); | ||
| 129 | if(!key) | ||
| 130 | RETURNWERR(EINVAL, -1); | ||
| 131 | |||
| 132 | FILE *src, *dst; | ||
| 133 | if(!(src = fopen(encrypted, "rb"))) | ||
| 134 | ERROR(1, errno, "Could not open \"%s\" for decryption", , encrypted); | ||
| 135 | if(!(dst = fopen(target, "wb"))) | ||
| 136 | ERROR(1, errno, "Could not open \"%s\" for writing decrypted data", , target); | ||
| 137 | |||
| 138 | if(decrypttofile(src, dst, key) < 0) | ||
| 139 | ERROR(1, errno, "How did you even cause an error?",); | ||
| 140 | |||
| 141 | fclose(dst); | ||
| 142 | fclose(src); | ||
| 143 | |||
| 144 | return 0; | ||
| 145 | } | ||
| 146 | |||
| 121 | int encrypttofile(FILE *dst, FILE *src, unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 147 | int encrypttofile(FILE *dst, FILE *src, unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 122 | unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; | 148 | unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; |
| 123 | unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; | 149 | unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; |
