summaryrefslogtreecommitdiff
path: root/src/encryption.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/encryption.h')
-rw-r--r--src/encryption.h101
1 files changed, 82 insertions, 19 deletions
diff --git a/src/encryption.h b/src/encryption.h
index 5c6a08c..a23cbdf 100644
--- a/src/encryption.h
+++ b/src/encryption.h
@@ -13,35 +13,41 @@
13#define __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ 13#define __VXGG_REWRITE___ENCRYPTION_H___1481879318188___
14 14
15#include <sodium.h> 15#include <sodium.h>
16#include "shared.h"
16 17
17// Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May 18/// Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May
18// cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just 19/// cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just
19// manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself 20/// manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself
20#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1 21#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1
21 22
22// Grants access to the `vxgg_setsodiumfailcb` function, which can be used to set a custom callback for what to do when libsodium 23/// Grants access to the `vxgg_setsodiumfailcb` function, which can be used to set a custom callback for what to do when libsodium
23// fails upon initialization 24/// fails upon initialization
24#define ___VXGG___USE_CLS_CALLBACK___ 1 25#define ___VXGG___USE_CLS_CALLBACK___ 1
25 26
26 27
27#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 28#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
28// 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
29void checksodium(void);
30
31#if ___VXGG___USE_CLS_CALLBACK___ > 0 29#if ___VXGG___USE_CLS_CALLBACK___ > 0
32// Definition for the callback function that fires when a call to checksodium fails 30//! Definition for the callback function that fires when a call to checksodium fails
33typedef void (*vxgg_naclfailcb)(void*); 31typedef void (*vxgg_naclfailcb)(void*);
34 32
35// Sets the error callback for when libsodium fails. Runs `cb(data)` if `(sodium_init() < 0)` 33/**
34 * @brief Sets a callback and data pair to be ran if/when sodium fails to initialize
35 *
36 * @param cb The new callback to set. Must be non-null
37 * @param data The data to be fed to the callback. May be null
38 */
36void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data); 39void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data);
37#endif 40#endif
38#endif 41#endif
39 42
40// Chunk size for en/de-cryption. I originally wanted to use st_blksize from stat(), but given that those chunks may be of different 43/// Chunk size for en/decryption. I originally wanted to use st_blksize from stat(), but given that those chunks may be of different
41// sizes between computers / filesystems / architectures / files, it's easier to just have this be a consistent macro 44/// sizes between computers / filesystems / architectures / files, it's easier to just have this be a consistent macro
42#define CHUNKSIZE (1 << 9) 45#define CHUNKSIZE (1 << 9)
43 46
47const static char * test = "this is a test";
48
44// 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 49// 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
50//! A list of possible words for password creation
45#define PASSWORD_WORDS (\ 51#define PASSWORD_WORDS (\
46 (const char * const []){\ 52 (const char * const []){\
47 "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", \ 53 "the", "of", "to", "and", "for", "our", "their", "has", "in", "he", "a", "them", "that", "these", "by", "have", "we", \
@@ -96,27 +102,84 @@ void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data);
96 "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\ 102 "reliance", "divine", "providence", "mutually", "pledge", "each", "fortunes", "sacred", "honor"\
97 }\ 103 }\
98) 104)
105//! Short macro for getting the `PASSWORD_WORDS` array size
99#define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS)) 106#define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS))
100 107
101// open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR 108/**
109 * @brief open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR
110 *
111 * @param dest The filename the new descriptor should have. Must be non-null
112 * @retval (int)[-1,int] A new file descriptor. -1 on error
113 */
102int maketmp(const char * const dest); 114int maketmp(const char * const dest);
103 115
104// Encrypt src to dst using libsodium's xchacha encryption suite 116/**
117 * @brief Encrypt src to dst using libsodium's xchacha encryption suite
118 *
119 * @param src File to encrypt
120 * @param dst Destination to write encrypted file
121 * @param key Key for encryption
122 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
123 */
105int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); 124int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
106 125
107// Decrypt src to dst using libsodium's xchacha encryption suite 126/**
127 * @brief Decrypt src to dst using libsodium's xchacha encryption suite
128 *
129 * @param src File to decrypt
130 * @param dst Destination to write decrypted file
131 * @param key Key used to encrypt
132 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
133 */
108int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); 134int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
109 135
110// Encrypt file at `target` to `output` using Linux's named temp file system to do it in the background 136/**
137 * @brief Encrypt file at `target` to `output` using Linux's named temp file system to do it in the background
138 *
139 * @param target
140 * @param output
141 * @param key
142 * @retval (int)[,]
143 */
111int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); 144int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
112 145
113// Decrypt the file at `encrypted` to `target` 146/**
147 * @brief Decrypt the file at `encrypted` to `target`
148 *
149 * @param encrypted
150 * @param target
151 * @param key
152 * @retval (int)[,]
153 */
114int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]); 154int decryptto(const char * const encrypted, const char * const target, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]);
115 155
116// 156/**
157 * @brief Link a file descriptor into the filesystem
158 *
159 * @param target New filename the descriptor should have
160 * @param tgfd The file descriptor to link
161 * @retval (int)[-1, 0] 0 on success, -1 on error
162 */
117int linkto(const char * const target, int tgfd); 163int linkto(const char * const target, int tgfd);
118 164
119// 165/**
166 * @brief Generate a password viable for use in the derivation of a key
167 *
168 * @param str Pointer to a string. This will be filled by a malloc'ed string of words (the password). Must be non-null
169 * @param words The number of words to include in the password. A password of at least 20 words and probably not more than 40 is recommended
170 * @retval (int)[-1, words] On success, returns the number of words requested. On error, returns -1 and sets errno
171 */
120int genpassword(char **str, unsigned int words); 172int genpassword(char **str, unsigned int words);
121 173
174/**
175 * @brief sodium_malloc wrapper.
176 *
177 * Calls `error()` or `abort()` depnding on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___`. Will make sure libsodium is initialized if `___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0`
178 *
179 * @param size
180 * @retval (void*) A pointer to some data allocated via `sodium_malloc()`
181 */
182void* xsodium_malloc(size_t size);
183
184
122#endif \ No newline at end of file 185#endif \ No newline at end of file