diff options
Diffstat (limited to 'src/encryption.c')
| -rw-r--r-- | src/encryption.c | 56 |
1 files changed, 22 insertions, 34 deletions
diff --git a/src/encryption.c b/src/encryption.c index 3bf9dd4..b6832ed 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -1,4 +1,7 @@ | |||
| 1 | // TODO: Go back and make sure every function has proper error handling | 1 | // TODO: Go back and make sure every function has proper error handling |
| 2 | // Oh fucking christ what have I done | ||
| 3 | // I need to make sure every single function in this file returns with an indicated error instead of nuking the whole program with | ||
| 4 | // error() | ||
| 2 | 5 | ||
| 3 | #define _GNU_SOURCE | 6 | #define _GNU_SOURCE |
| 4 | 7 | ||
| @@ -27,7 +30,7 @@ void naclfaildefault(void *none) { | |||
| 27 | exit(EXIT_FAILURE); | 30 | exit(EXIT_FAILURE); |
| 28 | } | 31 | } |
| 29 | 32 | ||
| 30 | int checksodiumcb(const vxgg_naclfailcb callback, void *data, unsigned char set) { | 33 | int checksodiumcb(vxgg_naclfailcb const callback, void *data, unsigned char set) { |
| 31 | static vxgg_naclfailcb cb = naclfaildefault; | 34 | static vxgg_naclfailcb cb = naclfaildefault; |
| 32 | static void *usr = NULL; | 35 | static void *usr = NULL; |
| 33 | int ret; | 36 | int ret; |
| @@ -84,7 +87,9 @@ int linkto(const char * const target, int tgfd) { | |||
| 84 | if(!path) | 87 | if(!path) |
| 85 | ERROR(1, errno, "<linkto> Couldn't get path to move file into system",); | 88 | ERROR(1, errno, "<linkto> Couldn't get path to move file into system",); |
| 86 | remove(target); // Make sure an old version isn't sticking around (it's not catastrophic if this fails, but it should be noted or logged somewhere) | 89 | remove(target); // Make sure an old version isn't sticking around (it's not catastrophic if this fails, but it should be noted or logged somewhere) |
| 87 | return linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); | 90 | int res = linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW); |
| 91 | free(path); | ||
| 92 | return res; | ||
| 88 | } | 93 | } |
| 89 | 94 | ||
| 90 | int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 95 | int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| @@ -127,17 +132,11 @@ int encryptviatmp(const char * const target, const char * const output, const un | |||
| 127 | } | 132 | } |
| 128 | 133 | ||
| 129 | int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 134 | int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 135 | if(!encrypted || !target || !key) ERRRET(EINVAL, -1); | ||
| 130 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 136 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 131 | checksodium(); | 137 | checksodium(); |
| 132 | #endif | 138 | #endif |
| 133 | 139 | ||
| 134 | if(!encrypted) | ||
| 135 | ERRRET(EINVAL, -1); | ||
| 136 | if(!target) | ||
| 137 | ERRRET(EINVAL, -1); | ||
| 138 | if(!key) | ||
| 139 | ERRRET(EINVAL, -1); | ||
| 140 | |||
| 141 | FILE *src, *dst; | 140 | FILE *src, *dst; |
| 142 | if(!(src = fopen(encrypted, "rb"))) | 141 | if(!(src = fopen(encrypted, "rb"))) |
| 143 | ERROR(1, errno, "<decryptto> Could not open \"%s\" for decryption", , encrypted); | 142 | ERROR(1, errno, "<decryptto> Could not open \"%s\" for decryption", , encrypted); |
| @@ -164,6 +163,11 @@ int decryptto(const char * const encrypted, const char * const target, const uns | |||
| 164 | } | 163 | } |
| 165 | 164 | ||
| 166 | int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 165 | int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 166 | if(!src || !dst || !key) ERRRET(EINVAL, -1); | ||
| 167 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 168 | checksodium(); | ||
| 169 | #endif | ||
| 170 | |||
| 167 | unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; | 171 | unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES]; |
| 168 | unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; | 172 | unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; |
| 169 | crypto_secretstream_xchacha20poly1305_state state; | 173 | crypto_secretstream_xchacha20poly1305_state state; |
| @@ -172,17 +176,6 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr | |||
| 172 | size_t bytesread; | 176 | size_t bytesread; |
| 173 | int eof; | 177 | int eof; |
| 174 | 178 | ||
| 175 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 176 | checksodium(); | ||
| 177 | #endif | ||
| 178 | |||
| 179 | if(!src) | ||
| 180 | ERRRET(EINVAL, -1); | ||
| 181 | if(!dst) | ||
| 182 | ERRRET(EINVAL, -1); | ||
| 183 | if(!key) | ||
| 184 | ERRRET(EINVAL, -1); | ||
| 185 | |||
| 186 | // Write the header | 179 | // Write the header |
| 187 | crypto_secretstream_xchacha20poly1305_init_push(&state, header, key); | 180 | crypto_secretstream_xchacha20poly1305_init_push(&state, header, key); |
| 188 | if(fwrite(header, 1, sizeof(header), dst) < sizeof(header)) | 181 | if(fwrite(header, 1, sizeof(header), dst) < sizeof(header)) |
| @@ -206,7 +199,12 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr | |||
| 206 | return 0; | 199 | return 0; |
| 207 | } | 200 | } |
| 208 | 201 | ||
| 209 | int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { | 202 | int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { |
| 203 | if(!src || !dst || !key) ERRRET(EINVAL, -1); | ||
| 204 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 205 | checksodium(); | ||
| 206 | #endif | ||
| 207 | |||
| 210 | unsigned char cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES], buf[CHUNKSIZE]; | 208 | unsigned char cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES], buf[CHUNKSIZE]; |
| 211 | unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; | 209 | unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES]; |
| 212 | crypto_secretstream_xchacha20poly1305_state state; | 210 | crypto_secretstream_xchacha20poly1305_state state; |
| @@ -215,17 +213,6 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr | |||
| 215 | size_t bytesread; | 213 | size_t bytesread; |
| 216 | int eof; | 214 | int eof; |
| 217 | 215 | ||
| 218 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | ||
| 219 | checksodium(); | ||
| 220 | #endif | ||
| 221 | |||
| 222 | if(!src) | ||
| 223 | ERRRET(EINVAL, -1); | ||
| 224 | if(!dst) | ||
| 225 | ERRRET(EINVAL, -1); | ||
| 226 | if(!key) | ||
| 227 | ERRRET(EINVAL, -1); | ||
| 228 | |||
| 229 | // Read the header | 216 | // Read the header |
| 230 | if(fread(header, 1, sizeof(header), src) < sizeof(header)) | 217 | if(fread(header, 1, sizeof(header), src) < sizeof(header)) |
| 231 | if(ferror(src)) | 218 | if(ferror(src)) |
| @@ -258,7 +245,8 @@ int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr | |||
| 258 | 245 | ||
| 259 | int genpassword(char **str, unsigned int words) { | 246 | int genpassword(char **str, unsigned int words) { |
| 260 | // Early returns | 247 | // Early returns |
| 261 | if(words < 1) {return 0;} | 248 | if(words < 1) return 0; |
| 249 | if(!str) ERRRET(EINVAL, -1); | ||
| 262 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 250 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 263 | checksodium(); | 251 | checksodium(); |
| 264 | #endif | 252 | #endif |
