summaryrefslogtreecommitdiff
path: root/src/encryption.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/encryption.c')
-rw-r--r--src/encryption.c68
1 files changed, 66 insertions, 2 deletions
diff --git a/src/encryption.c b/src/encryption.c
index 828bde2..0cd032f 100644
--- a/src/encryption.c
+++ b/src/encryption.c
@@ -14,14 +14,45 @@
14#include <fcntl.h> 14#include <fcntl.h>
15#include <stdio.h> 15#include <stdio.h>
16 16
17int checkSodium(void) { 17#if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
18void naclfaildefault(void *none) {
19 none = none; // Makes gcc happy
20 error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting...");
21}
22
23int checksodiumcb(const vxgg_naclfailcb callback, void *data) {
24 static vxgg_naclfailcb cb = naclfaildefault;
25 static void *usr = NULL;
26
27 if(callback != NULL) {
28 cb = callback;
29 usr = data;
30 return 2; // libsodium normally returns 1 if the library is already initialized, so this is to signal that the callback has been updated
31 }
32
18 int ret = sodium_init(); 33 int ret = sodium_init();
19 if(ret < 0) 34 if(ret < 0)
20 error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); 35 cb(usr);
21 36
22 return ret; 37 return ret;
23} 38}
24 39
40void vxgg_setsodiumfailcb(vxgg_naclfailcb cb, void *data) {
41 checksodiumcb(cb, data);
42}
43#endif
44
45void checksodium(void) {
46 #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
47 checksodiumcb(NULL, NULL);
48 #else
49 if(sodium_init() < 0)
50 error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting...");
51 #endif
52
53 return;
54}
55
25// To encrypt: 56// To encrypt:
26// 1- Create a temp file with the correct name in the root folder of the partition being encrypted -- 57// 1- Create a temp file with the correct name in the root folder of the partition being encrypted --
27 // 1.1- Detect the partition and find the root folder -- DONE || NOT NECESSARY 58 // 1.1- Detect the partition and find the root folder -- DONE || NOT NECESSARY
@@ -40,6 +71,10 @@ int maketmp(const char *dest) {
40} 71}
41 72
42int encrypttotmp(const char *toencrypt) { 73int encrypttotmp(const char *toencrypt) {
74 #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
75 checksodium();
76 #endif
77
43 struct stat esb; 78 struct stat esb;
44 int efd = -1; 79 int efd = -1;
45 80
@@ -58,12 +93,36 @@ int encrypttotmp(const char *toencrypt) {
58 return 0; 93 return 0;
59} 94}
60 95
96int genpassword(char **str, unsigned int words) {
97 #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
98 checksodium();
99 #endif
100
101 unsigned int i = 0;
102 char *lstr = NULL;
103
104 if(words < 1)
105 return 0;
106
107 asprintf(&lstr, "%s", PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]);
108 for(; i < words; i++) {
109 asprintf(&lstr, "%s %s", lstr, PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]);
110 }
111
112 *str = lstr;
113
114 return 0;
115
116 // TODO: I feel like this is / should be leaking memory like a mofo. Figure out if it is or not (look at malloc_stats())
117}
118
61#define TESTING 119#define TESTING
62#ifdef TESTING 120#ifdef TESTING
63 121
64#include <string.h> 122#include <string.h>
65 123
66int main(void) { 124int main(void) {
125 /*// Example code for creating a temp file, writing to it, then linking it back into the fs
67 const char *dir = ".", *testmsg = "we do a little testing\n"; 126 const char *dir = ".", *testmsg = "we do a little testing\n";
68 char *path = NULL; 127 char *path = NULL;
69 128
@@ -82,6 +141,11 @@ int main(void) {
82 141
83 if(close(fd) < 0) 142 if(close(fd) < 0)
84 error(1, errno, "close broke"); 143 error(1, errno, "close broke");
144 //*///
145
146 char *password = NULL;
147 genpassword(&password, 20);
148 printf("%s\n", password);
85 149
86 return 0; 150 return 0;
87} 151}