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
|
#define _GNU_SOURCE 1
#include "shared.c"
#include "encryption.c"
#include "threadpool.c"
void test_maketmp(void) {
// Example code for creating a temp file, writing to it, then linking it back into the fs
const char * const dir = ".", * const testmsg = "we do a little testing\n";
int fd = maketmp(dir);
if(fd < 0) ERROR(1, errno, "<test_maketmp> FAIL: Couldn't make temp file at %s", , dir);
if(write(fd, testmsg, strlen(testmsg)) < 0) error(1, errno, "<test_maketmp> FAIL: write broke");
if(linkto("./test_maketmp.test", fd)) ERROR(1, errno, "<test_maketmp> FAIL: Could not link into filesystem",);
// 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",);
return;
}
void test_genpassword(void) {
// Example code for getting a password using genpassword
char *password = NULL;
genpassword(&password, 20);
if(!password) ERROR(1, EINVAL, "<test_genpassword> FAIL: Couldn't get a password",);
printf("%s\n", password);
free(password);
return;
}
void test_libsodium_password(void) {
// Example code for generating a password, derriving a secret key from it, and storing things properly
// Initialization
char *pass = NULL, hpass[crypto_pwhash_STRBYTES];
if(genpassword(&pass, 20) < 0) ERROR(1, 0, "<test_libsodium_password> FAIL: Could not generate password", );
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, "<test_libsodium_password> FAIL: Couldn't hash generated password",);
// 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;
printf("Please enter your password: ");
if((size = rwbuf(&uin, 1, STDIN_FILENO)) < 0)
ERROR(1, errno, "<test_libsodium_password> FAIL: 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;
}
int main(void) {
test_maketmp();
test_genpassword();
test_libsodium_password();
return 0;
}
|