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
|
#include "hybbx/crypto.h"
#include "crypto_backends.h"
#include "monocypher.h"
#include <string.h>
#define HYBBX_CRYPTO_MAX_BYTES (256u * 1024u)
static hybbx_result_t crypto_len_ok(size_t len)
{
if (len > HYBBX_CRYPTO_MAX_BYTES) {
return HYBBX_ERR_INVALID;
}
return HYBBX_OK;
}
hybbx_result_t hybbx_crypto_random(uint8_t *buf, size_t len)
{
if (buf == NULL || len == 0) {
return HYBBX_ERR_INVALID;
}
return hybbx_backend_random(buf, len);
}
const char *hybbx_crypto_alg_name(hybbx_crypto_alg_t alg)
{
switch (alg) {
case HYBBX_CRYPTO_AES_256_GCM:
return "aes-256-gcm";
case HYBBX_CRYPTO_XCHACHA20_POLY1305:
return "xchacha20-poly1305";
case HYBBX_CRYPTO_X25519_AEAD:
return "x25519-xchacha20-poly1305";
default:
return "unknown";
}
}
size_t hybbx_crypto_nonce_size(hybbx_crypto_alg_t alg)
{
switch (alg) {
case HYBBX_CRYPTO_AES_256_GCM:
return HYBBX_CRYPTO_AES_GCM_NONCE;
case HYBBX_CRYPTO_XCHACHA20_POLY1305:
return HYBBX_CRYPTO_XCHACHA_NONCE;
default:
return 0;
}
}
static hybbx_result_t derive_x25519_key(const uint8_t shared[32],
uint8_t key[32])
{
static const uint8_t label[] = "hybbx-x25519-aead-key-v1";
hybbx_backend_blake2b_keyed(key, 32, shared, 32, label, sizeof(label) - 1);
return HYBBX_OK;
}
hybbx_result_t hybbx_crypto_encrypt(hybbx_crypto_alg_t alg,
const uint8_t key[HYBBX_CRYPTO_KEY_SIZE],
const uint8_t *nonce, size_t nonce_len,
const uint8_t *aad, size_t aad_len,
const uint8_t *plaintext,
size_t plaintext_len,
uint8_t *ciphertext,
uint8_t tag[HYBBX_CRYPTO_TAG_SIZE])
{
hybbx_result_t rc;
if (key == NULL || nonce == NULL || tag == NULL ||
(plaintext_len > 0 && (plaintext == NULL || ciphertext == NULL))) {
return HYBBX_ERR_INVALID;
}
rc = crypto_len_ok(plaintext_len);
if (rc != HYBBX_OK) {
return rc;
}
if (alg == HYBBX_CRYPTO_AES_256_GCM) {
if (nonce_len != HYBBX_CRYPTO_AES_GCM_NONCE) {
return HYBBX_ERR_INVALID;
}
if (hybbx_backend_aes256gcm_encrypt(key, nonce, aad, aad_len, plaintext,
plaintext_len, ciphertext, tag) !=
0) {
return HYBBX_ERR_IO;
}
return HYBBX_OK;
}
if (alg == HYBBX_CRYPTO_XCHACHA20_POLY1305) {
uint8_t nonce24[HYBBX_CRYPTO_XCHACHA_NONCE];
if (nonce_len != HYBBX_CRYPTO_XCHACHA_NONCE) {
return HYBBX_ERR_INVALID;
}
memcpy(nonce24, nonce, sizeof(nonce24));
hybbx_backend_chacha_lock(ciphertext, tag, key, nonce24, aad, aad_len,
plaintext, plaintext_len);
crypto_wipe(nonce24, sizeof(nonce24));
return HYBBX_OK;
}
return HYBBX_ERR_INVALID;
}
hybbx_result_t hybbx_crypto_decrypt(hybbx_crypto_alg_t alg,
const uint8_t key[HYBBX_CRYPTO_KEY_SIZE],
const uint8_t *nonce, size_t nonce_len,
const uint8_t *aad, size_t aad_len,
const uint8_t *ciphertext,
size_t ciphertext_len,
uint8_t *plaintext,
const uint8_t tag[HYBBX_CRYPTO_TAG_SIZE])
{
hybbx_result_t rc;
int ok;
if (key == NULL || nonce == NULL || tag == NULL ||
(ciphertext_len > 0 && (ciphertext == NULL || plaintext == NULL))) {
return HYBBX_ERR_INVALID;
}
rc = crypto_len_ok(ciphertext_len);
if (rc != HYBBX_OK) {
return rc;
}
if (alg == HYBBX_CRYPTO_AES_256_GCM) {
if (nonce_len != HYBBX_CRYPTO_AES_GCM_NONCE) {
return HYBBX_ERR_INVALID;
}
ok = hybbx_backend_aes256gcm_decrypt(key, nonce, aad, aad_len,
ciphertext, ciphertext_len,
plaintext, tag);
return ok == 0 ? HYBBX_OK : HYBBX_ERR_DENIED;
}
if (alg == HYBBX_CRYPTO_XCHACHA20_POLY1305) {
uint8_t nonce24[HYBBX_CRYPTO_XCHACHA_NONCE];
if (nonce_len != HYBBX_CRYPTO_XCHACHA_NONCE) {
return HYBBX_ERR_INVALID;
}
memcpy(nonce24, nonce, sizeof(nonce24));
ok = hybbx_backend_chacha_unlock(plaintext, tag, key, nonce24, aad,
aad_len, ciphertext, ciphertext_len);
crypto_wipe(nonce24, sizeof(nonce24));
return ok == 0 ? HYBBX_OK : HYBBX_ERR_DENIED;
}
return HYBBX_ERR_INVALID;
}
hybbx_result_t hybbx_crypto_x25519_keypair(
uint8_t public_key[HYBBX_CRYPTO_X25519_PUBLIC_KEY],
uint8_t secret_key[HYBBX_CRYPTO_X25519_SECRET_KEY])
{
if (public_key == NULL || secret_key == NULL) {
return HYBBX_ERR_INVALID;
}
if (hybbx_crypto_random(secret_key, HYBBX_CRYPTO_X25519_SECRET_KEY) !=
HYBBX_OK) {
return HYBBX_ERR_IO;
}
secret_key[0] &= 248;
secret_key[31] &= 127;
secret_key[31] |= 64;
hybbx_backend_x25519_public_key(public_key, secret_key);
return HYBBX_OK;
}
hybbx_result_t hybbx_crypto_x25519_seal(
const uint8_t recipient_pk[HYBBX_CRYPTO_X25519_PUBLIC_KEY],
const uint8_t *aad, size_t aad_len,
const uint8_t *plaintext, size_t plaintext_len,
uint8_t *sealed, size_t *sealed_len, size_t sealed_cap)
{
uint8_t ep_sk[32];
uint8_t ep_pk[32];
uint8_t shared[32];
uint8_t key[32];
uint8_t nonce[HYBBX_CRYPTO_XCHACHA_NONCE];
uint8_t *cipher;
uint8_t *tag;
size_t need;
hybbx_result_t rc;
if (recipient_pk == NULL || sealed == NULL || sealed_len == NULL) {
return HYBBX_ERR_INVALID;
}
rc = crypto_len_ok(plaintext_len);
if (rc != HYBBX_OK) {
return rc;
}
need = HYBBX_CRYPTO_X25519_SEAL_OVERHEAD + plaintext_len;
if (sealed_cap < need) {
return HYBBX_ERR_INVALID;
}
if (hybbx_crypto_random(ep_sk, sizeof(ep_sk)) != HYBBX_OK) {
return HYBBX_ERR_IO;
}
ep_sk[0] &= 248;
ep_sk[31] &= 127;
ep_sk[31] |= 64;
hybbx_backend_x25519_public_key(ep_pk, ep_sk);
hybbx_backend_x25519_shared(shared, ep_sk, recipient_pk);
derive_x25519_key(shared, key);
if (hybbx_crypto_random(nonce, sizeof(nonce)) != HYBBX_OK) {
crypto_wipe(ep_sk, sizeof(ep_sk));
crypto_wipe(shared, sizeof(shared));
crypto_wipe(key, sizeof(key));
return HYBBX_ERR_IO;
}
memcpy(sealed, ep_pk, 32);
memcpy(sealed + 32, nonce, sizeof(nonce));
cipher = sealed + 32 + sizeof(nonce);
tag = sealed + 32 + sizeof(nonce) + plaintext_len;
rc = hybbx_crypto_encrypt(HYBBX_CRYPTO_XCHACHA20_POLY1305, key, nonce,
sizeof(nonce), aad, aad_len, plaintext,
plaintext_len, cipher, tag);
crypto_wipe(ep_sk, sizeof(ep_sk));
crypto_wipe(shared, sizeof(shared));
crypto_wipe(key, sizeof(key));
if (rc != HYBBX_OK) {
return rc;
}
*sealed_len = need;
return HYBBX_OK;
}
hybbx_result_t hybbx_crypto_x25519_open(
const uint8_t recipient_sk[HYBBX_CRYPTO_X25519_SECRET_KEY],
const uint8_t *aad, size_t aad_len,
const uint8_t *sealed, size_t sealed_len,
uint8_t *plaintext, size_t *plaintext_len, size_t plaintext_cap)
{
const uint8_t *ep_pk;
const uint8_t *nonce;
const uint8_t *cipher;
const uint8_t *tag;
size_t cipher_len;
uint8_t shared[32];
uint8_t key[32];
uint8_t tag_buf[HYBBX_CRYPTO_TAG_SIZE];
hybbx_result_t rc;
if (recipient_sk == NULL || sealed == NULL || plaintext == NULL ||
plaintext_len == NULL) {
return HYBBX_ERR_INVALID;
}
if (sealed_len < HYBBX_CRYPTO_X25519_SEAL_OVERHEAD) {
return HYBBX_ERR_INVALID;
}
cipher_len = sealed_len - HYBBX_CRYPTO_X25519_SEAL_OVERHEAD;
if (cipher_len > plaintext_cap) {
return HYBBX_ERR_INVALID;
}
ep_pk = sealed;
nonce = sealed + 32;
cipher = sealed + 32 + HYBBX_CRYPTO_XCHACHA_NONCE;
tag = cipher + cipher_len;
memcpy(tag_buf, tag, sizeof(tag_buf));
hybbx_backend_x25519_shared(shared, recipient_sk, ep_pk);
derive_x25519_key(shared, key);
rc = hybbx_crypto_decrypt(HYBBX_CRYPTO_XCHACHA20_POLY1305, key, nonce,
HYBBX_CRYPTO_XCHACHA_NONCE, aad, aad_len,
cipher, cipher_len, plaintext, tag_buf);
crypto_wipe(shared, sizeof(shared));
crypto_wipe(key, sizeof(key));
if (rc != HYBBX_OK) {
return rc;
}
*plaintext_len = cipher_len;
return HYBBX_OK;
}
|