-
Notifications
You must be signed in to change notification settings - Fork 4
/
sodium.c
185 lines (153 loc) · 4.84 KB
/
sodium.c
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
#include "sodium.h"
#include <sodium.h>
typedef struct SodiumParam {
unsigned char key[KEY_SIZE];
unsigned char iv[IV_SIZE];
unsigned char tag[TAG_SIZE];
const char *current_cipher;
crypto_aead_aes256gcm_state ctx;
} SodiumParam;
const char *sodium_name() {
return "libsodium";
}
const char **sodium_ciphers() {
static const char *names[] = {
CIPHER_AEGIS_128L,
CIPHER_AEGIS_256,
CIPHER_AES_256_GCM,
CIPHER_CHACHA20_POLY1305,
NULL
};
return names;
}
bool sodium_init_(void **param) {
if (!param) {
return false;
}
if (sodium_init() == -1) {
printf("sodium_init_(): sodium_init() failed!\n");
return false;
}
*param = malloc(sizeof(SodiumParam));
return true;
}
bool sodium_free_(void *param) {
if (!param) {
return false;
}
free(param);
return true;
}
bool sodium_random(void *param, const size_t size, void *dst) {
if (!param || !dst) {
return false;
}
randombytes_buf(dst, size);
return true;
}
bool sodium_set_cipher(void *param, const char *cipher) {
if (!param || !cipher) {
return false;
}
SodiumParam *sp = param;
if (cipher == CIPHER_AEGIS_128L) {
sp->current_cipher = CIPHER_AEGIS_128L;
crypto_aead_aegis128l_keygen(sp->key);
} else if (cipher == CIPHER_AEGIS_256) {
sp->current_cipher = CIPHER_AEGIS_256;
crypto_aead_aegis256_keygen(sp->key);
} else if (cipher == CIPHER_AES_256_GCM) {
if (!crypto_aead_aes256gcm_is_available()) {
printf("sodium_set_cipher(): AES-256-GCM requires SSSE3 extensions + \"aesni\" and \"pclmul\" instructions.\n");
return false;
}
sp->current_cipher = CIPHER_AES_256_GCM;
crypto_aead_aes256gcm_keygen(sp->key);
crypto_aead_aes256gcm_beforenm(&sp->ctx, sp->key);
} else if (cipher == CIPHER_CHACHA20_POLY1305) {
sp->current_cipher = CIPHER_CHACHA20_POLY1305;
crypto_aead_chacha20poly1305_ietf_keygen(sp->key);
} else {
printf("sodium_set_cipher(): \"%s\" is not a recognized cipher!\n", cipher);
return false;
}
sodium_random(param, sizeof(sp->iv), sp->iv);
return true;
}
size_t sodium_buffer_size(const size_t size) {
return size;
}
size_t sodium_encrypt(void *param, const size_t size, void *dst, const void *src) {
if (!param || !dst || !src) {
return 0;
}
SodiumParam *sp = param;
if (sp->current_cipher == CIPHER_AEGIS_128L) {
if (crypto_aead_aegis128l_encrypt_detached(dst, sp->tag, NULL, src, size, NULL, 0, NULL, sp->iv, sp->key) != 0) {
printf("sodium_encrypt(): crypto_aead_aegis128l_encrypt_detached() failed!\n");
return 0;
}
} else if (sp->current_cipher == CIPHER_AEGIS_256) {
if (crypto_aead_aegis256_encrypt_detached(dst, sp->tag, NULL, src, size, NULL, 0, NULL, sp->iv, sp->key) != 0) {
printf("sodium_encrypt(): crypto_aead_aegis256_encrypt_detached() failed!\n");
return 0;
}
} else if (sp->current_cipher == CIPHER_AES_256_GCM) {
if (crypto_aead_aes256gcm_encrypt_detached_afternm(dst, sp->tag, NULL, src, size, NULL, 0, NULL, sp->iv, &sp->ctx) != 0) {
printf("sodium_encrypt(): crypto_aead_aes256gcm_encrypt_detached_afternm() failed!\n");
return 0;
}
} else if (sp->current_cipher == CIPHER_CHACHA20_POLY1305) {
if (crypto_aead_chacha20poly1305_ietf_encrypt_detached(dst, sp->tag, NULL, src, size, NULL, 0, NULL, sp->iv, sp->key) != 0) {
printf("sodium_encrypt(): crypto_aead_chacha20poly1305_ietf_encrypt_detached() failed!\n");
return 0;
}
} else {
return 0;
}
return size;
}
size_t sodium_decrypt(void *param, const size_t size, void *dst, const void *src) {
if (!param || !dst || !src) {
return 0;
}
SodiumParam *sp = param;
if (sp->current_cipher == CIPHER_AEGIS_128L) {
if (crypto_aead_aegis128l_decrypt_detached(dst, NULL, dst, size, sp->tag, NULL, 0, sp->iv, sp->key) != 0) {
printf("sodium_decrypt(): crypto_aead_aegis128l_decrypt_detached() failed!\n");
return 0;
}
} else if (sp->current_cipher == CIPHER_AEGIS_256) {
if (crypto_aead_aegis256_decrypt_detached(dst, NULL, dst, size, sp->tag, NULL, 0, sp->iv, sp->key) != 0) {
printf("sodium_decrypt(): crypto_aead_aegis256_decrypt_detached() failed!\n");
return 0;
}
} else if (sp->current_cipher == CIPHER_AES_256_GCM) {
if (crypto_aead_aes256gcm_decrypt_detached_afternm(dst, NULL, dst, size, sp->tag, NULL, 0, sp->iv, &sp->ctx) != 0) {
printf("sodium_decrypt(): crypto_aead_aes256gcm_decrypt_detached_afternm() failed!\n");
return 0;
}
} else if (sp->current_cipher == CIPHER_CHACHA20_POLY1305) {
if (crypto_aead_chacha20poly1305_ietf_decrypt_detached(dst, NULL, dst, size, sp->tag, NULL, 0, sp->iv, sp->key) != 0) {
printf("sodium_decrypt(): crypto_aead_chacha20poly1305_ietf_decrypt_detached() failed!\n");
return 0;
}
} else {
return 0;
}
return size;
}
const Crypto *sodium_get() {
static const Crypto crypto = {
sodium_name,
sodium_ciphers,
sodium_init_,
sodium_free_,
sodium_random,
sodium_set_cipher,
sodium_buffer_size,
sodium_encrypt,
sodium_decrypt
};
return &crypto;
}