summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-03-31 16:01:23 -0500
committer@syxhe <https://t.me/syxhe>2025-03-31 16:01:23 -0500
commit7424be8e0b033fd6466d517d6a8e3f0fb545dd59 (patch)
treead37e1c2840b331cebb68aae430b15d9df896044
parent833cb417608eb138d3a9b99dc995c5c1aeffd1cd (diff)
Decouple the checksodiumcb function from ALWAYS_CHECK_LIBSODIUM into its own macro
-rw-r--r--src/encryption.c41
-rw-r--r--src/encryption.h21
-rw-r--r--src/shared.h1
3 files changed, 41 insertions, 22 deletions
diff --git a/src/encryption.c b/src/encryption.c
index a01d4ff..606be03 100644
--- a/src/encryption.c
+++ b/src/encryption.c
@@ -16,6 +16,8 @@
16#include <stdio.h> 16#include <stdio.h>
17 17
18#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 18#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
19#if ___VXGG___USE_CLS_CALLBACK___ > 0
20
19void naclfaildefault(void *none) { 21void naclfaildefault(void *none) {
20 none = none; // Makes gcc happy 22 none = none; // Makes gcc happy
21 if(___VXGG___VERBOSE_ERRORS___) 23 if(___VXGG___VERBOSE_ERRORS___)
@@ -23,31 +25,36 @@ void naclfaildefault(void *none) {
23 exit(EXIT_FAILURE); 25 exit(EXIT_FAILURE);
24} 26}
25 27
26int checksodiumcb(const vxgg_naclfailcb callback, void *data) { 28int checksodiumcb(const vxgg_naclfailcb callback, void *data, unsigned char set) {
27 static vxgg_naclfailcb cb = naclfaildefault; 29 static vxgg_naclfailcb cb = naclfaildefault;
28 static void *usr = NULL; 30 static void *usr = NULL;
29 31 int ret;
30 if(callback != NULL) { 32
33 if(set) {
31 cb = callback; 34 cb = callback;
32 usr = data; 35 usr = data;
33 return 2; // libsodium normally returns 1 if the library is already initialized, so this is to signal that the callback has been updated 36 return 2; // libsodium normally returns 1 if the library is already initialized, so this is to signal that the callback has been updated
34 } 37 }
35 38
36 int ret = sodium_init(); 39 if((ret = sodium_init()) < 0) {
37 if(ret < 0) 40 if(cb == NULL)
38 cb(usr); 41 error(0, EINVAL, "<checksodiumcb> refusing to run a null callback");
42 else
43 cb(usr);
44 }
39 45
40 return ret; 46 return ret;
41} 47}
42 48
43void vxgg_setsodiumfailcb(vxgg_naclfailcb cb, void *data) { 49void vxgg_setsodiumfailcb(vxgg_naclfailcb cb, void *data) {
44 checksodiumcb(cb, data); 50 checksodiumcb(cb, data, 1);
45} 51}
52
46#endif 53#endif
47 54
48void checksodium(void) { 55void checksodium(void) {
49 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 56 #if ___VXGG___USE_CLS_CALLBACK___ > 0
50 checksodiumcb(NULL, NULL); 57 checksodiumcb(NULL, NULL, 0);
51 #else 58 #else
52 59
53 if(sodium_init() < 0) { 60 if(sodium_init() < 0) {
@@ -60,13 +67,15 @@ void checksodium(void) {
60 return; 67 return;
61} 68}
62 69
70#endif
71
63int maketmp(const char * const dest) { 72int maketmp(const char * const dest) {
64 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));
65} 74}
66 75
67int encrypttotmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) { 76int encrypttotmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
68 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 77 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
69 checksodium(); 78 checksodium();
70 #endif 79 #endif
71 80
72 int fd = -1, tfd = -1; 81 int fd = -1, tfd = -1;
@@ -163,6 +172,9 @@ int decrypttofile(FILE *dst, FILE *src, unsigned char key[crypto_secretstream_xc
163 FILE *dst, *src; 172 FILE *dst, *src;
164 int eof; 173 int eof;
165 174
175 #if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
176 checksodium();
177 #endif
166 178
167 if(!src) 179 if(!src)
168 RETURNWERR(EINVAL, -1); 180 RETURNWERR(EINVAL, -1);
@@ -244,9 +256,9 @@ void* xsodium_malloc(size_t size) {
244} 256}
245 257
246 258
247#ifdef TESTING 259/*
248int main(void) { 260int main(void) {
249 /*// Example code for creating a temp file, writing to it, then linking it back into the fs 261 // Example code for creating a temp file, writing to it, then linking it back into the fs
250 const char *dir = ".", *testmsg = "we do a little testing\n"; 262 const char *dir = ".", *testmsg = "we do a little testing\n";
251 char *path = NULL; 263 char *path = NULL;
252 264
@@ -313,9 +325,6 @@ int main(void) {
313 sodium_munlock(uin, strlen(uin) + 1); 325 sodium_munlock(uin, strlen(uin) + 1);
314 free(uin); 326 free(uin);
315 327
316 //*///
317
318 return 0; 328 return 0;
319} 329}
320 330*/ \ No newline at end of file
321#endif \ No newline at end of file
diff --git a/src/encryption.h b/src/encryption.h
index 1bdd2b9..1f04dae 100644
--- a/src/encryption.h
+++ b/src/encryption.h
@@ -3,21 +3,33 @@
3 3
4#include <sodium.h> 4#include <sodium.h>
5 5
6#define CHUNKSIZE (1 << 9)
7
8
6// Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May 9// Determines whether any function that calls libsodium functions also checks to make sure libsodium is actually initialized. May
7// cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just 10// cause unexpected issues with early exiting due to libsodium failing to initialize properly. It's recommended that you just
8// manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself 11// manually run `sodium_init()` in some main or init function of your own so that you can deal with a potential error yourself
9#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 0 12#define ___VXGG___ALWAYS_CHECK_LIBSODIUM___ 1
13
14// Grants access to the `vxgg_setsodiumfailcb` function, which can be used to set a custom callback for what to do when libsodium
15// fails upon initialization
16#define ___VXGG___USE_CLS_CALLBACK___ 1
17
18
10 19
11#define CHUNKSIZE (1 << 9)
12 20
13// TODO: What the fuck was I thinking when I did any of this callback shit? Make this a different macro and decouple it from ALWAYS_CHECK
14#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 21#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
15 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
24void checksodium(void);
25
26#if ___VXGG___USE_CLS_CALLBACK___ > 0
16// Definition for the callback function that fires when a call to checksodium fails 27// Definition for the callback function that fires when a call to checksodium fails
17typedef void (*vxgg_naclfailcb)(void*); 28typedef void (*vxgg_naclfailcb)(void*);
18 29
19// Sets the error callback for when libsodium fails. Runs `cb(data)` if `(sodium_init() < 0)` 30// Sets the error callback for when libsodium fails. Runs `cb(data)` if `(sodium_init() < 0)`
20void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data); 31void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data);
32#endif
21 33
22#endif 34#endif
23 35
@@ -78,9 +90,6 @@ void vxgg_setsodiumfailcb(const vxgg_naclfailcb cb, void *data);
78) 90)
79#define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS)) 91#define PASSWORD_WORDS_LEN (STATIC_ARRAY_LEN(PASSWORD_WORDS))
80 92
81// 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
82void checksodium(void);
83
84// open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR 93// open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR
85int maketmp(const char * const dest); 94int maketmp(const char * const dest);
86 95
diff --git a/src/shared.h b/src/shared.h
index 7d2db27..b7fe7c3 100644
--- a/src/shared.h
+++ b/src/shared.h
@@ -29,6 +29,7 @@
29 exit(EXIT_FAILURE); /* Makes gcc happy */\ 29 exit(EXIT_FAILURE); /* Makes gcc happy */\
30} while (0) 30} while (0)
31 31
32// Error macro that gcc will not complain about
32#define ERROR(status, errnum, format, ...) do {error((status), (errnum), (format)__VA_ARGS__); exit((status));} while (0) 33#define ERROR(status, errnum, format, ...) do {error((status), (errnum), (format)__VA_ARGS__); exit((status));} while (0)
33 34
34// `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` 35// `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___`