summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/encryption.c81
-rw-r--r--src/shared.c115
-rw-r--r--src/shared.h4
3 files changed, 130 insertions, 70 deletions
diff --git a/src/encryption.c b/src/encryption.c
index 32a5ce0..e4fed1f 100644
--- a/src/encryption.c
+++ b/src/encryption.c
@@ -19,7 +19,11 @@
19#if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 19#if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
20void naclfaildefault(void *none) { 20void naclfaildefault(void *none) {
21 none = none; // Makes gcc happy 21 none = none; // Makes gcc happy
22 error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); 22 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
23 error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting...");
24 #else
25 exit(EXIT_FAILURE);
26 #endif
23} 27}
24 28
25int checksodiumcb(const vxgg_naclfailcb callback, void *data) { 29int checksodiumcb(const vxgg_naclfailcb callback, void *data) {
@@ -49,7 +53,11 @@ void checksodium(void) {
49 checksodiumcb(NULL, NULL); 53 checksodiumcb(NULL, NULL);
50 #else 54 #else
51 if(sodium_init() < 0) 55 if(sodium_init() < 0)
52 error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); 56 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
57 error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting...");
58 #else
59 exit(EXIT_FAILURE);
60 #endif
53 #endif 61 #endif
54 62
55 return; 63 return;
@@ -75,25 +83,12 @@ int maketmp(const char * const dest) {
75 return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); 83 return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR));
76} 84}
77 85
78int encrypttotmp(const char * const toencrypt) { 86int encrypttotmp(const char * const target, const char * const output, const char * const password, int chunksize) {
79 #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0 87 #if defined ___VXGG___ALWAYS_CHECK_LIBSODIUM___ && ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
80 checksodium(); 88 checksodium();
81 #endif 89 #endif
82 90
83 struct stat esb; 91
84 int efd = -1;
85
86 // Make sure the file is real and an actual file that can be encrypted
87 if(stat(toencrypt, &esb) < 0)
88 return -1;
89 if(!S_ISREG(esb.st_mode))
90 return -2;
91
92 // Open the file as read-only
93 if((efd = open(toencrypt, O_RDONLY)) < 0)
94 return -3;
95
96 // Need to get a secret key from a password and then set up cryptostream from libsodium
97 92
98 return 0; 93 return 0;
99} 94}
@@ -136,7 +131,11 @@ void* xsodium_malloc(size_t size) {
136 void *mem = sodium_malloc(size); 131 void *mem = sodium_malloc(size);
137 if(mem == NULL) { 132 if(mem == NULL) {
138 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 133 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
139 error(1, errno, "xsodium_malloc: could not allocate memory... Quitting"); 134 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
135 error(1, errno, "<xsodium_malloc> could not allocate memory... Quitting");
136 #else
137 exit(EXIT_FAILURE);
138 #endif
140 #endif 139 #endif
141 140
142 abort(); 141 abort();
@@ -218,7 +217,7 @@ int main(void) {
218 217
219 //*/// 218 //*///
220 219
221 //*// Example code for generating a key from a password and encrypting a test file 220 /*// Example code for generating a key from a password and encrypting a test file
222 221
223 const char *dir = ".", *fname = "toBeEncrypted.test.txt", *pass = "this is a password"; 222 const char *dir = ".", *fname = "toBeEncrypted.test.txt", *pass = "this is a password";
224 char *path = NULL, *message = NULL, *efname = NULL; 223 char *path = NULL, *message = NULL, *efname = NULL;
@@ -302,50 +301,6 @@ int main(void) {
302 free(buf); 301 free(buf);
303 free(cbuf); 302 free(cbuf);
304 303
305
306 // Ok now decryption to make sure I didn't fuck it
307 unsigned char dheader[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
308 crypto_secretstream_xchacha20poly1305_state dstate;
309
310 if((fd = open(efname, O_RDONLY)) < 0)
311 error(1, errno, "Could not open file for decryption");
312 if((tfd = open("lmao.test.dec", O_WRONLY | O_CREAT | O_TRUNC, (S_IRUSR | S_IWUSR))) < 0)
313 error(1, errno, "Could not open file for result of decryption");
314
315 if(read(fd, header, sizeof(header)) < 0)
316 error(1, errno, "Could not read header of encrypted file");
317
318 if(crypto_secretstream_xchacha20poly1305_init_pull(&dstate, dheader, key) != 0)
319 error(1, EINVAL, "Incomplete header");
320
321 bytesread = -1;
322 unsigned char tag = 255;
323 buf = xcalloc(CHUNK_SIZE + 1, sizeof(*buf));
324 cbuf = xcalloc((CHUNK_SIZE + 1) + crypto_secretstream_xchacha20poly1305_ABYTES, sizeof(*cbuf));
325 while((bytesread = read(fd, cbuf, (CHUNK_SIZE + 1) + crypto_secretstream_xchacha20poly1305_ABYTES)) >= 0) {
326 if(crypto_secretstream_xchacha20poly1305_pull(&dstate, buf, NULL, &tag, cbuf, bytesread, NULL, 0) < 0)
327 error(1, errno, "Found a corrupted chunk");
328
329 if(tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && bytesread != 0)
330 error(1, errno, "End tag before end of file");
331
332 if(bytesread == 0 && tag != crypto_secretstream_xchacha20poly1305_TAG_FINAL)
333 error(1, errno, "End of file before end tag");
334
335 if(writewholebuffer(tfd, buf, bytesread) < 0)
336 error(1, errno, "write() error");
337
338 if(bytesread == 0)
339 break;
340 }
341 if(bytesread < 0)
342 error(1, errno, "read() error");
343
344 close(fd);
345 close(tfd);
346 free(buf);
347 free(cbuf);
348
349 //*/// 304 //*///
350 305
351 return 0; 306 return 0;
diff --git a/src/shared.c b/src/shared.c
index 40240d5..c46b001 100644
--- a/src/shared.c
+++ b/src/shared.c
@@ -13,7 +13,11 @@ void* xcalloc(size_t nmemb, size_t size) {
13 13
14 if(!mem) { 14 if(!mem) {
15 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 15 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
16 error(1, errno, "<xcalloc> Could not allocate memory"); 16 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
17 error(1, errno, "<xcalloc> Could not allocate memory");
18 #else
19 exit(EXIT_FAILURE);
20 #endif
17 #endif 21 #endif
18 22
19 abort(); 23 abort();
@@ -27,8 +31,11 @@ void* xreallocarray(void *ptr, size_t nmemb, size_t size) {
27 void *mem = reallocarray(ptr, nmemb, size); 31 void *mem = reallocarray(ptr, nmemb, size);
28 if(mem == NULL) { 32 if(mem == NULL) {
29 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 33 #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
30 error(1, errno, "<xreallocarray> Could not allocate memory"); 34 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
31 35 error(1, errno, "<xreallocarray> Could not allocate memory");
36 #else
37 exit(EXIT_FAILURE);
38 #endif
32 #endif 39 #endif
33 40
34 abort(); 41 abort();
@@ -56,7 +63,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) {
56 63
57 tmp = realloc(lstr, csize * sizeof(char)); 64 tmp = realloc(lstr, csize * sizeof(char));
58 if(!tmp) { 65 if(!tmp) {
59 error(0, errno, "Could not reallocate enough space for lstr"); 66 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
67 error(0, errno, "Could not reallocate enough space for lstr");
68 #endif
60 free(lstr); 69 free(lstr);
61 lstr = NULL; // Need to set this because of the break 70 lstr = NULL; // Need to set this because of the break
62 bytesread = -100; 71 bytesread = -100;
@@ -66,7 +75,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) {
66 } 75 }
67 } 76 }
68 if(bytesread < 0 && bytesread != -100) { 77 if(bytesread < 0 && bytesread != -100) {
69 error(0, errno, "Ran into a read() error"); 78 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
79 error(0, errno, "Ran into a read() error");
80 #endif
70 free(lstr); 81 free(lstr);
71 lstr = NULL; 82 lstr = NULL;
72 } 83 }
@@ -74,7 +85,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) {
74 if(lstr) { 85 if(lstr) {
75 tmp = realloc(lstr, csize - ccap + 1); 86 tmp = realloc(lstr, csize - ccap + 1);
76 if(!tmp) { 87 if(!tmp) {
77 error(0, errno, "Could not shrink lstr after reading buffer"); 88 #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
89 error(0, errno, "Could not shrink lstr after reading buffer");
90 #endif
78 free(lstr); 91 free(lstr);
79 bytesread = -100; 92 bytesread = -100;
80 } 93 }
@@ -107,4 +120,92 @@ int writewholebuffer(int fd, const unsigned char *buf, int len) {
107} 120}
108// Adapted from Beej's `sendall()` function 121// Adapted from Beej's `sendall()` function
109// https://beej.us/guide/bgnet/html/split/slightly-advanced-techniques.html#sendall 122// https://beej.us/guide/bgnet/html/split/slightly-advanced-techniques.html#sendall
110 // Thanks Beej! \ No newline at end of file 123 // Thanks Beej!
124
125// dirname but less retarded hopefully
126char *xdirname(const char * const path) {
127 char *tmp = NULL;
128 if(!path) { // Path being null is a special case which should return super early, before anything else
129 tmp = strdup(".");
130 if(!tmp)
131 abort();
132
133 return tmp;
134 }
135
136 unsigned char flag = 0;
137 if(strcmp(path, ".") == 0) {tmp = strdup("."); flag++;}
138 if(strcmp(path, "/") == 0 && !flag) {tmp = strdup("/"); flag++;}
139 if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;}
140
141 if(flag) {
142 if(!tmp)
143 abort();
144
145 return tmp;
146 }
147
148
149 /* From the manpages: (man 3 dirname)
150 // +=======================================+
151 // | path dirname basename |
152 // +=======================================+
153 // | /usr/lib /usr lib |
154 // | /usr/ / usr |
155 // | usr . usr |
156 // +=======================================+
157 */
158
159 // Get a temp copy of the path for manipulation purposes
160 tmp = strdup(path);
161 if(!tmp)
162 abort();
163
164 // If there's a trailing '/', delete it
165 size_t pathlen = strlen(path);
166 if(tmp[pathlen - 1] == '/') {
167 tmp[pathlen - 1] = '\0';
168 pathlen--;
169 }
170
171 // Ok, I think the easiest way to do this (if maybe a bit slow) is to count the number of '/'s in the string
172 // If there's only one, return '/'
173 // If there are 2 or more, find the last one in the list and set it to '\0'
174
175 size_t count = 0;
176 for(size_t i = 0; i < pathlen; i++) {
177 if(tmp[i] == '/')
178 count++;
179 }
180
181 if(count == 0) {
182 free(tmp);
183 tmp = strdup(".");
184 if(!tmp)
185 abort();
186
187 return tmp;
188 }
189 if(count == 1) {
190 free(tmp);
191 tmp = strdup("/");
192 if(!tmp)
193 abort();
194
195 return tmp;
196 }
197
198 for(size_t i = 0, c2 = 0; i < pathlen; i++) {
199 if(tmp[i] == '/')
200 c2++;
201 if(c2 == count)
202 tmp[i] = '\0';
203 }
204
205 char * const actual = strdup(tmp);
206 if(!actual)
207 abort();
208 free(tmp);
209
210 return actual;
211} \ No newline at end of file
diff --git a/src/shared.h b/src/shared.h
index 3602a4e..9cdbd33 100644
--- a/src/shared.h
+++ b/src/shared.h
@@ -11,6 +11,10 @@
11// type functions will ALWAYS 'abort', doing otherwise defeats the purpose of the function type 11// type functions will ALWAYS 'abort', doing otherwise defeats the purpose of the function type
12#define ___VXGG___XALLOC_EXIT_ON_ERROR___ 1 12#define ___VXGG___XALLOC_EXIT_ON_ERROR___ 1
13 13
14// Defines whether vxgg functions that can error print out a short warning of the error when one is encountered.
15// `___VXGG___VERBOSE_ERRORS___ > 0` will print diagnostic error messages, and will do nothing otherwise
16#define ___VXGG___VERBOSE_ERRORS___ 1
17
14// `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` 18// `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___`
15void* xcalloc(size_t nmemb, size_t size); 19void* xcalloc(size_t nmemb, size_t size);
16 20