1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
/**
* @file encryption.c
* @author syxhe (https://t.me/syxhe)
* @brief *Implementing `encryption.h`*
* @version 0.1
* @date 2025-06-09
*
* @copyright Copyright (c) 2025
*
*/
// TODO: Go back and make sure every function has proper error handling
// Oh fucking christ what have I done
// I need to make sure every single function in this file returns with an indicated error instead of nuking the whole program with
// error()
#define _GNU_SOURCE
#include "shared.h"
#include "encryption.h"
#include "threadpool.h"
#include <sodium.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <stdio.h>
#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
#if ___VXGG___USE_CLS_CALLBACK___ > 0
/**
* @brief Default sodium init fail callback for use in `checksodiumcb()`
*
* @param none Unused param to fit callback spec
*/
static void naclfaildefault(void *none) {
none = none; // Makes gcc happy
if(___VXGG___VERBOSE_ERRORS___)
error(1, ENOTSUP, "<naclfaildefault> Couldn't initialize sodium for some reason. Quitting...");
exit(EXIT_FAILURE);
}
/**
* @brief Internal function to deal with the `___VXGG___USE_CLS_CALLBACK___` macro
*
* `checksodiumcb()` runs the sodium function `sodium_init()` and on error calls the provided callback with the provided data. The
* callback and data default to `naclfaildefault` and `NULL`, but can be changed when the `set` parameter is non-zero. When `set`
* is zero, the sodium init check is preformed
*
* @note `checksodiumcb()` is ran when these conditions are true - 1: The `___VXGG___ALWAYS_CHECK_LIBSODIUM___` and `___VXGG___USE_CLS_CALLBACK___`
* macros are both greater than 0 when compiling, and 2: a function in `encryption.c` calls a function originating from sodium. This
* function exists as a way to deal with sodium failing yourself, instead of instantly calling `exit()`. If you don't care to handle
* it, or are initializing sodium yourself, this is unnecessary
*
* @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
* @param data Data to be passed to the callback when it is fired. Ignored if `set` is zero. May be null
* @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
* @retval int
*/
static int checksodiumcb(vxgg_naclfailcb const callback, void *data, unsigned char set) {
static vxgg_naclfailcb cb = naclfaildefault;
static void *usr = NULL;
int ret;
if(set) {
if(cb == NULL) ERRRET(EINVAL, -1);
cb = callback;
usr = data;
return 2; // libsodium normally returns 1 if the library is already initialized, so this is to signal that the callback has been updated
}
if((ret = sodium_init()) < 0) {
if(cb == NULL)
error(0, EINVAL, "<checksodiumcb> refusing to run a null callback");
else
cb(usr);
}
return ret;
}
void vxgg_setsodiumfailcb(vxgg_naclfailcb cb, void *data) {
checksodiumcb(cb, data, 1);
}
#endif
/**
* @brief Simple function to check if sodium has been properly initialized
*
* `checksodium()` will run in functions located in `encryption.h` only when the macro `___VXGG___ALWAYS_CHECK_LIBSODIUM___` is greater
* than zero when compiling. It will call the `checksodiumcb()` function if compiled with the `___VXGG___USE_CLS_CALLBACK___` macro.
* When called, checksodium will run `sodium_init()`, and will either run the user-defined callback or `XALLOC_EXIT`.
*
*/
static void checksodium(void) {
#if ___VXGG___USE_CLS_CALLBACK___ > 0
checksodiumcb(NULL, NULL, 0);
#else
if(sodium_init() < 0) {
errno = ENOTSUP;
XALLOC_EXIT("<checksodium> Couldn't initialize sodium for some reason. Quitting...");
}
#endif
return;
}
#endif
int maketmp(const char * const dest) {
if(!dest) ERRRET(EINVAL, -1);
return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR));
}
int linkto(const char * const target, int tgfd) {
if(!target) ERRRET(EINVAL, -1);
char *path = NULL;
asprintf(&path, "/proc/self/fd/%d", tgfd);
if(!path)
ERROR(1, errno, "<linkto> Couldn't get path to move file into system",);
remove(target); // Make sure an old version isn't sticking around (it's not catastrophic if this fails, but it should be noted or logged somewhere)
int res = linkat(AT_FDCWD, path, AT_FDCWD, target, AT_SYMLINK_FOLLOW);
free(path);
return res;
}
static void __ucl_close(void *fd) {
if(!fd) return;
close(*(int*)fd);
*(int*)fd = -1;
return;
}
static void __ucl_fclose(void *file) {
if(!file) return;
fclose((FILE*)file);
return;
}
int encryptviatmp(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
checksodium();
#endif
cleanup_CREATE(10);
int fd = -1, tfd = -1;
FILE *src, *dst;
char *targetdir;
// Open the target file
if((fd = open(target, O_RDONLY)) < 0)
return -1;
cleanup_REGISTER(__ucl_close, (void*)&fd);
// Create a temp file for writing
targetdir = vxdirname(output);
if(!targetdir)
cleanup_MARK();
cleanup_CNDREGISTER(free, targetdir);
// Actually get the file descriptor for the temp file
cleanup_CNDEXEC(
tfd = maketmp(targetdir);
if(tfd < 0)
cleanup_MARK();
cleanup_CNDREGISTER(__ucl_close, (void*)&tfd);
);
// Create a FILE* version of the source fd
cleanup_CNDEXEC(
if(!(src = fdopen(fd, "rb"))) {
WARN(errno, "<encryptviatmp> Couldn't open \"%s\"", , target);
cleanup_MARK();
}
cleanup_CNDREGISTER(__ucl_fclose, (void*)&src);
)
// Create a FILE* version of the target fd
cleanup_CNDEXEC(
if(!(dst = fdopen(tfd, "wb"))) {
WARN(errno, "<encryptviatmp> Couldn't open \"%s\"", , output);
cleanup_MARK();
}
cleanup_CNDREGISTER(__ucl_fclose, (void*)dst);
);
// Do the encryption now that everything has been set up
cleanup_CNDEXEC(
// Not going to bother changing this, it probably is catastrophic if an error happens when it shouldn't
if(encrypttofile(src, dst, key) < 0)
ERROR(1, ENOTRECOVERABLE, "<encryptviatmp> I don't even have a way to cause an error here. How did you do it?",);
// Link the temp file into the system
if(linkto(output, tfd) < 0)
WARN(errno, "<encryptviatmp> Could not link \"%s\" into system after encryption", , output);
free(targetdir);
fclose(dst);
fclose(src);
// fclose alco closes fd and tfd, as fdopen does not dup the file descriptors
);
cleanup_CNDFIRE();
if(cleanup_ERRORFLAGGED)
return -1;
return 0;
}
int decryptto(const char * const target, const char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
if(!target || !output || !key) ERRRET(EINVAL, -1);
#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
checksodium();
#endif
cleanup_CREATE(10);
FILE *src, *dst;
int fdst;
// Open the source file
if(!(src = fopen(target, "rb"))) {
WARN(errno, "<decryptto> Could not open \"%s\" for decryption", , target);
return -1;
}
cleanup_REGISTER(__ucl_fclose, src);
// Get a temp descriptor for the temp file
cleanup_CNDEXEC(
fdst = maketmp(output);
if(!fdst) {
WARN(errno, "<decryptto> Could not get temp file for decryption", );
cleanup_MARK();
}
cleanup_CNDREGISTER(__ucl_close, (void*)&fdst);
);
// Open a FILE* version of the temp file
cleanup_CNDEXEC(
if(!(dst = fdopen(fdst, "wb"))) {
WARN(errno, "<decryptto> Could not open \"%s\" for writing decrypted data", , output);
cleanup_MARK();
}
cleanup_CNDREGISTER(__ucl_fclose, dst);
);
// Follow through with the rest of the decryption
cleanup_CNDEXEC(
if(decrypttofile(src, dst, key) < 0)
ERROR(1, errno, "<decryptto> How did you even cause an error?",);
// Link temp into system
if(linkto(output, fdst) < 0)
WARN(errno, "<decryptto> Could not link \"%s\" into system", , output);
fclose(dst);
fclose(src);
// fclose alco closes fd and tfd, as fdopen does not dup the file descriptors
);
cleanup_CNDFIRE();
if(cleanup_ERRORFLAGGED)
return -1;
// Note: If an error were to theoretically occur, which shouldn't be possible but I'm covering my bases here, after the
// `dst = fdopen` line, a double close on the temp file descriptor would occur. I've been told that this is not catastrophic,
// and considering how my multithreading works it *should* be fine, but it very well could cause problems. The easy solution is
// 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
// actually trigger so it's fine for now
return 0;
}
int encrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
if(!src || !dst || !key) ERRRET(EINVAL, -1);
#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
checksodium();
#endif
unsigned char buf[CHUNKSIZE], cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
crypto_secretstream_xchacha20poly1305_state state;
unsigned long long cbuflen;
unsigned char tag;
size_t bytesread;
int eof;
// Write the header
crypto_secretstream_xchacha20poly1305_init_push(&state, header, key);
if(fwrite(header, 1, sizeof(header), dst) < sizeof(header)) {
if(ferror(dst)) {
WARN(errno, "<encrypttofile> Could not write header",);
return -1;
}
}
// Encrypt each chunk
do {
if((bytesread = fread(buf, 1, sizeof(buf), src)) < sizeof(buf))
if(ferror(src)) {
WARN(errno, "<encrypttofile> Could not read from source",);
return -1;
}
eof = feof(src);
tag = eof ? crypto_secretstream_xchacha20poly1305_TAG_FINAL : 0;
crypto_secretstream_xchacha20poly1305_push(&state, cbuf, &cbuflen, buf, bytesread, NULL, 0, tag);
if(fwrite(cbuf, 1, (size_t)cbuflen, dst) < (size_t)cbuflen)
if(ferror(dst)) {
WARN(errno, "<encrypttofile> Could not write to target",);
return -1;
}
} while (!eof);
return 0;
}
int decrypttofile(FILE *src, FILE *dst, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
if(!src || !dst || !key) ERRRET(EINVAL, -1);
#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
checksodium();
#endif
unsigned char cbuf[CHUNKSIZE + crypto_secretstream_xchacha20poly1305_ABYTES], buf[CHUNKSIZE];
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
crypto_secretstream_xchacha20poly1305_state state;
unsigned long long buflen;
unsigned char tag;
size_t bytesread;
int eof;
// Read the header
if(fread(header, 1, sizeof(header), src) < sizeof(header)) {
if(ferror(src)) {
WARN(errno, "<decrypttofile> Couldn't read header", );
return -1;
}
}
// Make sure the header isn't fuckey
if(crypto_secretstream_xchacha20poly1305_init_pull(&state, header, key) != 0) {
WARN(errno, "<decrypttofile> Incomplete header", );
return -1;
}
// Decrypt each chunk
do {
if((bytesread = fread(cbuf, 1, sizeof(cbuf), src)) < sizeof(cbuf)) {
if(ferror(src)) {
WARN(errno, "<decrypttofile> Ran into problem reading for decryption", );
return -1;
}
}
eof = feof(src);
if (crypto_secretstream_xchacha20poly1305_pull(&state, buf, &buflen, &tag, cbuf, bytesread, NULL, 0) != 0) {
WARN(errno, "<decrypttofile> Corrupted chunk", );
return -1;
}
if(tag == crypto_secretstream_xchacha20poly1305_TAG_FINAL && !eof) {
WARN(errno, "<decrypttofile> End of stream before end of file", );
return -1;
}
if(eof && tag != crypto_secretstream_xchacha20poly1305_TAG_FINAL) {
WARN(errno, "<decrypttofile> End of file before end of stream", );
return -1;
}
fwrite(buf, 1, (size_t)buflen, dst);
} while(! eof);
return 0;
}
int genpassword(char **str, unsigned int words) {
// Early returns
if(words < 1) return 0;
if(!str) ERRRET(EINVAL, -1);
#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
checksodium();
#endif
// Bootstrap the first word
char *lstr = NULL, *tmp = NULL;
if(asprintf(&lstr, "%s", PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]) < 0)
return -1;
// Concat the rest of the words into the password (without leaking memory)
int ret;
for(unsigned int i = 1; i < words; i++) {
ret = asprintf(&tmp, "%s %s", lstr, PASSWORD_WORDS[randombytes_uniform(PASSWORD_WORDS_LEN)]);
sodium_memzero(lstr, strlen(lstr) + 1);
free(lstr);
if(ret < 0)
return -1;
lstr = tmp;
}
*str = lstr;
return words;
}
void* xsodium_malloc(size_t size) {
#if ___VXGG___ALWAYS_CHECK_LIBSODIUM___ > 0
checksodium();
#endif
void *mem = sodium_malloc(size);
if(mem == NULL)
XALLOC_EXIT("<xsodium_malloc> could not allocate memory... Quitting", );
return mem;
}
// TODO: Rewrite this to use the threadpool. Each newly scanned file should be pushed onto the threadpool as an encryption task
// #include <dirent.h>
// dlinkedlist * scandirlist(const char * const dir, int (*selector)(const struct dirent *), int (*cmp)(const struct dirent **, const struct dirent **)) {
// if(!dir || selector == NULL || cmp == NULL) ERRRET(EINVAL, NULL);
// struct dirent **namelist = NULL;
// dlinkedlist *list = NULL;
// int numentries = -1;
// if((numentries = scandir(dir, &namelist, selector, cmp)) < 0)
// ERRRET(errno, NULL);
// list = dlinkedlist_init();
// for(int i = 0; i < numentries; i++)
// if(dlinkedlist_append(list, (void *)(namelist[i]), free) < 0) {
// dlinkedlist_free(list);
// for(int j = i; j < numentries; j++)
// free(namelist[j]);
// free(namelist);
// ERRRET(errno, NULL);
// }
// free(namelist);
// return list;
// }
struct __FLC_FKP {
char *target;
char *output;
const unsigned char *key;
};
struct __FLC_FKP * fkp_init(char * const target, char * const output, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
if(!target || !output || !key) ERRRET(EINVAL, NULL);
struct __FLC_FKP *fkp = calloc(1, sizeof(*fkp));
if(!fkp)
return NULL;
fkp->key = key;
fkp->output = output;
fkp->target = target;
return fkp;
}
void fkp_free(void *fkp) {
if(!fkp) return;
struct __FLC_FKP *real = (struct __FLC_FKP *)fkp;
free(real->output);
free(real->target);
free(real);
return;
}
static int __FLC_TASK_ENCRYPT(void *fkp) {
if(!fkp)
return -1;
struct __FLC_FKP *real = (struct __FLC_FKP *)fkp;
return encryptviatmp(real->target, real->output, real->key);
}
static int __FLC_TASK_DECRYPT(void *fkp) {
if(!fkp)
return -1;
struct __FLC_FKP *real = (struct __FLC_FKP *)fkp;
return decryptto(real->target, real->output, real->key);
}
enum VXGG_FLC {
VXGG_FLC__INVALID,
VXGG_FLC__ENCRYPT,
VXGG_FLC__DECRYPT,
VXGG_FLC__TOOBIG
};
struct __ucl_namelist_vals {
struct dirent **namelist;
int entries;
};
static void __ucl_namelist(void *namelist) {
if(!namelist) return;
struct __ucl_namelist_vals *real = namelist;
for(int i = 0; i > real->entries; i++)
free(real->namelist[i]);
free(real->namelist);
return;
}
// TODO: Write these
static int encryption_filter(const struct dirent *de) {
}
static int decryption_filter(const struct dirent *de) {
}
ctqueue * getfilelist(enum VXGG_FLC mode, const char * const dir, int threads, const unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES]) {
if(mode <= VXGG_FLC__INVALID || mode >= VXGG_FLC__TOOBIG || !dir || threads <= 0 || !key) ERRRET(EINVAL, NULL);
cleanup_CREATE(10);
ctqueue *ctq = NULL;
int files = -1;
// Create the ctqueue for later
ctq = ctqueue_init(threads);
if(!ctq)
return NULL;
cleanup_REGISTER(ctqueue_free, ctq);
// Get the scandir list
struct dirent **namelist;
if((files = scandir(dir, &namelist, ((mode == VXGG_FLC__ENCRYPT) ? encryption_filter : decryption_filter), alphasort)) < 0)
cleanup_MARK();
cleanup_CNDREGISTER(__ucl_namelist, (void*)(&(struct __ucl_namelist_vals){.namelist = namelist, .entries = files}));
// Push everything onto the ctqueue
// TODO: Write task* compatible callbacks for encryption and decryption, then populate the ctqueue based on the specified mode
cleanup_CNDEXEC(
for(int i = 0; i < files; i++) {
// Target is either "filename" or "filename.vxggr"
// Output is either "filename.vxggr" or "filename"
struct __FLC_FKP *fkp = fkp_init(target, output, key);
if(!fkp) {
WARN(errno, "<getfilelist> Could not create file-key pair for \"%s\"'s %scryption task, skipping...",, namelist[i]->d_name, ((mode == VXGG_FLC__ENCRYPT) ? "en" : "de"));
free(target);
free(output);
}
task *ctask = task_init((mode == VXGG_FLC__ENCRYPT) ? __FLC_TASK_ENCRYPT : __FLC_TASK_DECRYPT, fkp_free, fkp);
if(!ctask) {
WARN(errno, "<getfilelist> Could not push \"%s\"'s %scryption task, skipping...",, namelist[i]->d_name, ((mode == VXGG_FLC__ENCRYPT) ? "en" : "de"));
free(namelist[i]);
continue;
}
ctqueue_waitpush(ctq, ctask);
}
free(namelist);
// namelist is the array that holds each pointer to each dirent, so it needs to be free'd separately from the other elements
);
cleanup_CNDFIRE();
if(cleanup_ERRORFLAGGED)
return NULL;
return ctq;
}
/*
int main(void) {
// Example code for creating a temp file, writing to it, then linking it back into the fs
const char *dir = ".", *testmsg = "we do a little testing\n";
char *path = NULL;
int fd = maketmp(dir);
if(fd < 0)
error(1, errno, "Couldn't make temp file at %s", dir);
if(write(fd, testmsg, strlen(testmsg)) < 0)
error(1, errno, "write broke");
asprintf(&path, "/proc/self/fd/%d", fd);
linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW);
free(path);
// Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages
if(close(fd) < 0)
error(1, errno, "close broke");
//*///
/*// Example code for getting a password using genpassword
checksodium();
char *password = NULL;
genpassword(&password, 20);
printf("%s\n", (password != NULL) ? password : "Couldn't get a password");
free(password);
//*///
/*// Example code for generating a password, derriving a secret key from it, and storing things properly
// Initialization
checksodium();
char *pass = NULL, hpass[crypto_pwhash_STRBYTES];
if(genpassword(&pass, 20) < 0) {
error(1, 0, "Could not generate password, quitting...");
abort(); // Makes gcc happy. Not sure why gcc randomly decides that error() isn't a proper exit, but hey whatever
}
sodium_mlock(pass, strlen(pass) + 1);
printf("Password:%s\n", pass);
// Store the password
if(crypto_pwhash_str(hpass, pass, strlen(pass) + 1, crypto_pwhash_OPSLIMIT_MODERATE, crypto_pwhash_MEMLIMIT_MODERATE) != 0)
error(1, errno, "Couldn't generate password, quitting...");
// Don't know if I want to use MODERATE or SENSITIVE for this. SENSITIVE takes a little bit on my laptop, which honestly
// shouldn't be a problem, but it annoys me. MODERATE is quick and snappy, or at least quick enough that the slowdown is
// barely noticable. I might do MODERATE for testing and SENSITIVE for release
sodium_munlock(pass, strlen(pass) + 1);
free(pass);
printf("Hashed password: %s\n", hpass);
// Check if the password from the user is correct
char *uin = NULL; int size = -1;
if((size = readwholebuffer(&uin, 1, STDIN_FILENO)) < 0)
error(1, errno, "Could not read from stdin");
sodium_mlock(uin, size);
printf("Valid password? %s\n", (crypto_pwhash_str_verify(hpass, uin, size) == 0) ? "True" : "False");
sodium_munlock(uin, strlen(uin) + 1);
free(uin);
return 0;
}
*/
|