diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/encryption.c | 32 | ||||
| -rw-r--r-- | src/encryption.h | 20 |
2 files changed, 39 insertions, 13 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]; |
diff --git a/src/encryption.h b/src/encryption.h index 1f04dae..b5fe04e 100644 --- a/src/encryption.h +++ b/src/encryption.h | |||
| @@ -3,9 +3,6 @@ | |||
| 3 | 3 | ||
| 4 | #include <sodium.h> | 4 | #include <sodium.h> |
| 5 | 5 | ||
| 6 | #define CHUNKSIZE (1 << 9) | ||
| 7 | |||
| 8 | |||
| 9 | // Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May | 6 | // Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May |
| 10 | // cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just | 7 | // cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just |
| 11 | // manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself | 8 | // manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself |
| @@ -16,10 +13,7 @@ | |||
| 16 | #define ___VXGG___USE_CLS_CALLBACK___ 1 | 13 | #define ___VXGG___USE_CLS_CALLBACK___ 1 |
| 17 | 14 | ||
| 18 | 15 | ||
| 19 | |||
| 20 | |||
| 21 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 | 16 | #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 |
| 22 | |||
| 23 | // Checks if sodium is initialized. Initializes it if not. If `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0`, it's possible to set an error callback to avoid exiting the entire program. Otherwise calls `error()` if libsodium can't initialize | 17 | // Checks if sodium is initialized. Initializes it if not. If `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0`, it's possible to set an error callback to avoid exiting the entire program. Otherwise calls `error()` if libsodium can't initialize |
| 24 | void checksodium(void); | 18 | void checksodium(void); |
| 25 | 19 | ||
| @@ -30,14 +24,17 @@ typedef void (*vxgg_naclfailcb)(void*); | |||
| 30 | // Sets the error callback for when libsodium fails. Runs `cb(data)` if `(sodium_init() < 0)` | 24 | // Sets the error callback for when libsodium fails. Runs `cb(data)` if `(sodium_init() < 0)` |
| 31 | void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data); | 25 | void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data); |
| 32 | #endif | 26 | #endif |
| 33 | |||
| 34 | #endif | 27 | #endif |
| 35 | 28 | ||
| 29 | // Chunk size for en/de-cryption. I originally wanted to use st_blksize from stat(), but given that those chunks may be of different | ||
| 30 | // sizes between computers / filesystems / architectures / files, it's easier to just have this be a consistent macro | ||
| 31 | #define CHUNKSIZE (1 << 9) | ||
| 32 | |||
| 36 | // Fuck reading from a file. Even if someone ran strings on the binary and got this they wouldn't be able to regenerate the key | 33 | // Fuck reading from a file. Even if someone ran strings on the binary and got this they wouldn't be able to regenerate the key |
| 37 | #define PASSWORD_WORDS (\ | 34 | #define PASSWORD_WORDS (\ |
| 38 | (const char *[]){\ | 35 | (const char *[]){\ |
| 39 | "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", "us",\ | 36 | "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", \ |
| 40 | "people", "which", "all", "is", "with", "laws", "be", "are", "his", "states", "on", "they", "right", "it", "from", \ | 37 | "us", "people", "which", "all", "is", "with", "laws", "be", "are", "his", "states", "on", "they", "right", "it", "from", \ |
| 41 | "government", "such", "among", "powers", "most", "an", "time", "should", "new", "as", "been", "colonies", "assent", \ | 38 | "government", "such", "among", "powers", "most", "an", "time", "should", "new", "as", "been", "colonies", "assent", \ |
| 42 | "large", "at", "independent", "free", "united", "when", "mankind", "hold", "rights", "governments", "consent", "its", \ | 39 | "large", "at", "independent", "free", "united", "when", "mankind", "hold", "rights", "governments", "consent", "its", \ |
| 43 | "long", "themselves", "abolishing", "usurpations", "absolute", "repeated", "this", "world", "refused", "pass", "other", \ | 40 | "long", "themselves", "abolishing", "usurpations", "absolute", "repeated", "this", "world", "refused", "pass", "other", \ |
| @@ -96,6 +93,9 @@ int maketmp(const char * const dest); | |||
| 96 | int encrypttofile(FILE *dst, FILE *src, unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); | 93 | int encrypttofile(FILE *dst, FILE *src, unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); |
| 97 | int decrypttofile(FILE *dst, FILE *src, unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); | 94 | int decrypttofile(FILE *dst, FILE *src, unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); |
| 98 | 95 | ||
| 99 | int encrypttotmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); | 96 | int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); |
| 97 | int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); | ||
| 98 | |||
| 99 | int genpassword(char **str, unsigned int words); | ||
| 100 | 100 | ||
| 101 | #endif \ No newline at end of file | 101 | #endif \ No newline at end of file |
