-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhaclnacl.c
343 lines (298 loc) · 11 KB
/
haclnacl.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
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
#include "haclnacl.h"
#include "kremlib.h"
#include "Hacl_Curve25519.h"
#include "Hacl_Chacha20.h"
#include "Hacl_Salsa20.h"
#define Hacl_Impl_Poly1305_64_State_poly1305_state Hacl_Impl_Poly1305_64_State_poly1305_state_poly
#include "Hacl_Poly1305_64.h"
#undef Hacl_Impl_Poly1305_64_State_poly1305_state
#define Hacl_Impl_Poly1305_64_State_poly1305_state Hacl_Impl_Poly1305_64_State_poly1305_state_aead
#include "Hacl_Chacha20Poly1305.h"
#undef Hacl_Impl_Poly1305_64_State_poly1305_state
#define K___uint32_t_uint8_t_ K___uint32_t_uint8_t_ed
#include "Hacl_Ed25519.h"
#undef K___uint32_t_uint8_t_
#define K___uint32_t_uint8_t_ K___uint32_t_uint8_t_sha256
#include "Hacl_SHA2_256.h"
#undef K___uint32_t_uint8_t_
#define K___uint32_t_uint8_t_ K___uint32_t_uint8_t_sha512
#include "Hacl_SHA2_512.h"
#undef K___uint32_t_uint8_t_
#include "NaCl.h"
extern void randombytes(uint8_t *bytes, uint64_t bytes_len);
void curve25519_scalarmult(uint8_t *out, uint8_t *secret, uint8_t *point){
Hacl_Curve25519_crypto_scalarmult(out, secret, point);
}
void
chacha20(
uint8_t *output,
uint8_t *plain,
uint32_t plain_len,
uint8_t *key,
uint8_t *nonce,
uint32_t ctr
){
Hacl_Chacha20_chacha20(output, plain, plain_len, key, nonce, ctr);
}
void
salsa20(
uint8_t *output,
uint8_t *plain,
uint32_t len,
uint8_t *key,
uint8_t *nonce,
uint64_t ctr
){
Hacl_Salsa20_salsa20(output, plain, len, key, nonce, ctr);
}
void
poly1305_onetimeauth(uint8_t *output, uint8_t *input, uint64_t input_len, uint8_t *key){
Hacl_Poly1305_64_crypto_onetimeauth(output, input, input_len, key);
}
uint32_t
aead_chacha20_poly1305_encrypt(
uint8_t *cipher,
uint8_t *mac,
uint8_t *msg,
uint32_t msg_len,
uint8_t *aad,
uint32_t aad_len,
uint8_t *key,
uint8_t *nonce
){
return Hacl_Chacha20Poly1305_aead_encrypt(cipher, mac, msg, msg_len, aad, aad_len, key, nonce);
}
uint32_t
aead_chacha20_poly1305_decrypt(
uint8_t *msg,
uint8_t *cipher,
uint32_t msg_len,
uint8_t *mac,
uint8_t *aad,
uint32_t aad_len,
uint8_t *key,
uint8_t *nonce
)
{
return Hacl_Chacha20Poly1305_aead_decrypt(msg, cipher, msg_len, mac, aad, aad_len, key, nonce);
}
void ed25519_secret_to_public(uint8_t *public_key, uint8_t *secret_key){
Hacl_Ed25519_secret_to_public(public_key, secret_key);
}
void ed25519_sign(uint8_t *signature, uint8_t *secret, uint8_t *msg, uint32_t msg_len){
Hacl_Ed25519_sign(signature, secret, msg, msg_len);
}
bool ed25519_verify(uint8_t *public, uint8_t *msg, uint32_t msg_len, uint8_t *signature){
return Hacl_Ed25519_verify(public, msg, msg_len, signature);
}
void sha2_512_hash(uint8_t *hash, uint8_t *input, uint32_t len){
Hacl_SHA2_512_hash(hash, input, len);
}
/* NaCl-like API */
int
crypto_onetimeauth(uint8_t *output, uint8_t *input, uint64_t input_len, uint8_t *key){
poly1305_onetimeauth(output, input, input_len, key);
return 0;
}
int
crypto_onetimeauth_verify(uint8_t *auth, uint8_t *msg, uint64_t msg_len, uint8_t *key){
uint8_t tag[16], tmp = 0xff;
poly1305_onetimeauth(tag, msg, msg_len, key);
for (int i = 0; i < 16; i++){
tmp = tmp & FStar_UInt8_eq_mask(tag[i], auth[i]);
}
tmp >>= 7;
return (int)tmp - 1;
}
int crypto_box_keypair(unsigned char *pk, unsigned char *sk){
randombytes(sk, 32);
uint8_t basepoint[32] = {9};
curve25519_scalarmult(pk, sk, basepoint);
return 0;
}
int crypto_box_easy(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *pk, const unsigned char *sk){
return NaCl_crypto_box_easy(c, (uint8_t*)m, mlen, (uint8_t*)n, (uint8_t*)pk, (uint8_t*)sk);
}
int crypto_box_open_easy(unsigned char *m, const unsigned char *c,
unsigned long long clen, const unsigned char *n,
const unsigned char *pk, const unsigned char *sk){
return NaCl_crypto_box_open_easy(m, (uint8_t*)c, clen, (uint8_t*)n, (uint8_t*)pk, (uint8_t*)sk);
}
int crypto_box_beforenm(unsigned char *k, const unsigned char *pk,
const unsigned char *sk){
return NaCl_crypto_box_beforenm(k, (uint8_t*)pk, (uint8_t*)sk);
}
int crypto_box_easy_afternm(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *k){
return NaCl_crypto_box_easy_afternm(c, (uint8_t*)m, mlen, (uint8_t*)n, (uint8_t*)k);
}
int crypto_box_open_easy_afternm(unsigned char *m, const unsigned char *c,
unsigned long long clen, const unsigned char *n,
const unsigned char *k){
return NaCl_crypto_box_open_easy_afternm(m, (uint8_t*)c, clen, (uint8_t*)n, (uint8_t*)k);
}
int crypto_scalarmult_base(unsigned char *q, const unsigned char *n){
/* This leaves room for improvements with precomputations */
uint8_t basepoint[32] = {9};
Hacl_Curve25519_crypto_scalarmult(q, (uint8_t*)n, basepoint);
return 0;
}
int crypto_scalarmult(unsigned char *q, const unsigned char *n,
const unsigned char *p){
Hacl_Curve25519_crypto_scalarmult(q, (uint8_t*)n, (uint8_t*)p);
return 0;
}
uint32_t
crypto_secretbox_detached(
uint8_t *c,
uint8_t *mac,
uint8_t *m,
uint64_t mlen,
uint8_t *n,
uint8_t *k){
return NaCl_crypto_secretbox_detached(c, mac, m, mlen, n, k);
}
uint32_t
crypto_secretbox_open_detached(
uint8_t *m,
uint8_t *c,
uint8_t *mac,
uint64_t clen,
uint8_t *n,
uint8_t *k
){
return NaCl_crypto_secretbox_open_detached(m, c, mac, clen, n, k);
}
uint32_t
crypto_secretbox_easy(uint8_t *c, uint8_t *m, uint64_t mlen, uint8_t *n, uint8_t *k){
return NaCl_crypto_secretbox_easy(c, m, mlen, n, k);
}
uint32_t
crypto_secretbox_open_easy(
uint8_t *m,
uint8_t *c,
uint64_t clen,
uint8_t *n,
uint8_t *k){
return NaCl_crypto_secretbox_open_easy(m, c, clen, n, k);
}
uint32_t
crypto_box_detached_afternm(
uint8_t *c,
uint8_t *mac,
uint8_t *m,
uint64_t mlen,
uint8_t *n,
uint8_t *k){
return NaCl_crypto_box_detached_afternm(c, mac, m, mlen, n, k);
}
uint32_t
crypto_box_detached(
uint8_t *c,
uint8_t *mac,
uint8_t *m,
uint64_t mlen,
uint8_t *n,
uint8_t *pk,
uint8_t *sk){
return NaCl_crypto_box_detached(c, mac, m, mlen, n, pk, sk);
}
uint32_t
crypto_box_open_detached(
uint8_t *m,
uint8_t *c,
uint8_t *mac,
uint64_t mlen,
uint8_t *n,
uint8_t *pk,
uint8_t *sk){
return NaCl_crypto_box_open_detached(m, c, mac, mlen, n, pk, sk);
}
uint32_t
crypto_box_open_detached_afternm(
uint8_t *m,
uint8_t *c,
uint8_t *mac,
uint64_t mlen,
uint8_t *n,
uint8_t *k){
return NaCl_crypto_box_detached_afternm(m, c, mac, mlen, n, k);
}
int
crypto_sign(
uint8_t *signed_msg,
long long unsigned int *signed_len,
uint8_t *msg,
uint64_t msg_len,
uint8_t *sk
){
Hacl_Ed25519_sign(signed_msg, sk, msg, msg_len);
memmove(signed_msg+64, msg, msg_len * sizeof(uint8_t));
*signed_len = msg_len + 64;
return 0;
}
int crypto_sign_open(
uint8_t *unsigned_msg,
long long unsigned int *unsigned_msg_len,
uint8_t *msg,
uint64_t msg_len,
uint8_t *pk
){
uint32_t res;
res = Hacl_Ed25519_verify(pk, msg+64, msg_len - 64, msg);
if (res == true){
memmove(unsigned_msg, msg+64, sizeof(uint8_t) * (msg_len-64));
*unsigned_msg_len = msg_len - 64;
return true;
} else {
return false;
}
}
int crypto_sign_keypair(
uint8_t pk[32],
uint8_t sk[64]
){
randombytes(sk, 32 * sizeof(uint8_t));
Hacl_Ed25519_secret_to_public(pk, sk);
for (int i = 0; i < 32; i++) sk[32+i] = pk[i];
return 0;
}
int crypto_sign_secret_to_public(uint8_t *public_key, uint8_t *secret_key){
Hacl_Ed25519_secret_to_public(public_key, secret_key);
return 0;
}
int crypto_box(uint8_t *cipher, uint8_t *msg, uint64_t msg_len, uint8_t *nonce, uint8_t *pk, uint8_t *sk){
return crypto_box_easy(cipher, msg, msg_len - 32, nonce, pk, sk);
}
int crypto_box_open(uint8_t *msg, uint8_t *cipher, uint64_t cipher_len, uint8_t *nonce, uint8_t *pk, uint8_t *sk){
return crypto_box_open_easy(msg, cipher, cipher_len - 32, nonce, pk, sk);
}
int crypto_box_afternm(uint8_t *cipher, uint8_t *msg, uint64_t msg_len, uint8_t *nonce, uint8_t *key){
return NaCl_crypto_box_easy_afternm(cipher, msg, msg_len, nonce, key);
}
int crypto_box_open_afternm(uint8_t *msg, uint8_t *cipher, uint64_t cipher_len, uint8_t *nonce, uint8_t *key){
return NaCl_crypto_box_open_easy_afternm(msg, cipher, cipher_len, nonce, key);
}
int crypto_secretbox(uint8_t *cipher, uint8_t *msg, uint64_t msg_len, uint8_t *nonce, uint8_t *key){
return crypto_secretbox_easy(cipher, msg, msg_len - 32, nonce, key);
}
int crypto_secretbox_open(uint8_t *msg, uint8_t *cipher, uint64_t cipher_len, uint8_t *nonce, uint8_t *key){
return crypto_secretbox_open_detached(msg, cipher, cipher + 16, cipher_len - 32, nonce, key);
}
int crypto_stream(uint8_t *cipher, uint64_t cipher_len, uint8_t *nonce, uint8_t *key){
uint8_t subkey[32];
memset(cipher, 0, cipher_len * sizeof(uint8_t));
Hacl_Salsa20_hsalsa20(subkey, key, nonce);
Hacl_Salsa20_salsa20(cipher, cipher, cipher_len, subkey, nonce + 16, 0);
return 0;
}
int crypto_stream_xor(uint8_t *cipher, uint8_t *msg, uint64_t cipher_len, uint8_t *nonce, uint8_t *key){
uint8_t subkey[32];
memset(cipher, 0, cipher_len * sizeof(uint8_t));
Hacl_Salsa20_hsalsa20(subkey, key, nonce);
Hacl_Salsa20_salsa20(cipher, msg, cipher_len, subkey, nonce + 16, 0);
return 0;
}