summaryrefslogtreecommitdiff
path: root/src/encryption.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/encryption.c')
-rw-r--r--src/encryption.c41
1 files changed, 25 insertions, 16 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