/src/gnutls/lib/nettle/cipher.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2010-2012 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2014 Red Hat, Inc. |
4 | | * |
5 | | * Author: Nikos Mavrogiannopoulos |
6 | | * |
7 | | * This file is part of GNUTLS. |
8 | | * |
9 | | * The GNUTLS library is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public License |
11 | | * as published by the Free Software Foundation; either version 2.1 of |
12 | | * the License, or (at your option) any later version. |
13 | | * |
14 | | * This library is distributed in the hope that it will be useful, but |
15 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
21 | | * |
22 | | */ |
23 | | |
24 | | /* Here lie nettle's wrappers for cipher support. |
25 | | */ |
26 | | |
27 | | #include "gnutls_int.h" |
28 | | #include "errors.h" |
29 | | #include "cipher_int.h" |
30 | | #include <nettle/aes.h> |
31 | | #include <nettle/camellia.h> |
32 | | #include <nettle/arcfour.h> |
33 | | #include <nettle/arctwo.h> |
34 | | #include <nettle/salsa20.h> |
35 | | #include <nettle/des.h> |
36 | | #include <nettle/version.h> |
37 | | #if ENABLE_GOST |
38 | | #ifndef HAVE_NETTLE_GOST28147_SET_KEY |
39 | | #include "gost/gost28147.h" |
40 | | #else |
41 | | #include <nettle/gost28147.h> |
42 | | #endif |
43 | | #ifndef HAVE_NETTLE_MAGMA_SET_KEY |
44 | | #include "gost/magma.h" |
45 | | #else |
46 | | #include <nettle/magma.h> |
47 | | #endif |
48 | | #ifndef HAVE_NETTLE_KUZNYECHIK_SET_KEY |
49 | | #include "gost/kuznyechik.h" |
50 | | #else |
51 | | #include <nettle/kuznyechik.h> |
52 | | #endif |
53 | | #include "gost/acpkm.h" |
54 | | #include <nettle/ctr.h> |
55 | | #endif |
56 | | #include <nettle/nettle-meta.h> |
57 | | #include <nettle/cbc.h> |
58 | | #include <nettle/gcm.h> |
59 | | #include <nettle/ccm.h> |
60 | | #include <nettle/chacha.h> |
61 | | #include <nettle/chacha-poly1305.h> |
62 | | #include <nettle/cfb.h> |
63 | | #include <nettle/xts.h> |
64 | | #include <nettle/siv-cmac.h> |
65 | | #ifdef HAVE_NETTLE_SIV_GCM_ENCRYPT_MESSAGE |
66 | | #include <nettle/siv-gcm.h> |
67 | | #else |
68 | | #include "backport/siv-gcm.h" |
69 | | #endif |
70 | | #include "fips.h" |
71 | | #include <intprops.h> |
72 | | |
73 | | struct nettle_cipher_ctx; |
74 | | |
75 | | /* Functions that refer to the nettle library. |
76 | | */ |
77 | | typedef void (*encrypt_func)(struct nettle_cipher_ctx *, size_t length, |
78 | | uint8_t *dst, const uint8_t *src); |
79 | | typedef void (*decrypt_func)(struct nettle_cipher_ctx *, size_t length, |
80 | | uint8_t *dst, const uint8_t *src); |
81 | | |
82 | | typedef void (*aead_encrypt_func)(struct nettle_cipher_ctx *, size_t nonce_size, |
83 | | const void *nonce, size_t auth_size, |
84 | | const void *auth, size_t tag_size, |
85 | | size_t length, uint8_t *dst, |
86 | | const uint8_t *src); |
87 | | typedef int (*aead_decrypt_func)(struct nettle_cipher_ctx *, size_t nonce_size, |
88 | | const void *nonce, size_t auth_size, |
89 | | const void *auth, size_t tag_size, |
90 | | size_t length, uint8_t *dst, |
91 | | const uint8_t *src); |
92 | | |
93 | | typedef void (*setiv_func)(void *ctx, size_t length, const uint8_t *); |
94 | | typedef void (*gen_setkey_func)(void *ctx, size_t length, const uint8_t *); |
95 | | |
96 | | struct nettle_cipher_st { |
97 | | gnutls_cipher_algorithm_t algo; |
98 | | unsigned ctx_size; |
99 | | nettle_cipher_func *encrypt_block; |
100 | | nettle_cipher_func *decrypt_block; |
101 | | unsigned block_size; |
102 | | unsigned key_size; |
103 | | unsigned max_iv_size; |
104 | | |
105 | | encrypt_func encrypt; |
106 | | decrypt_func decrypt; |
107 | | aead_encrypt_func aead_encrypt; |
108 | | aead_decrypt_func aead_decrypt; |
109 | | nettle_hash_update_func *auth; |
110 | | nettle_hash_digest_func *tag; |
111 | | nettle_set_key_func *set_encrypt_key; |
112 | | nettle_set_key_func *set_decrypt_key; |
113 | | gen_setkey_func gen_set_key; /* for arcfour which has variable key size */ |
114 | | setiv_func set_iv; |
115 | | }; |
116 | | |
117 | | struct nettle_cipher_ctx { |
118 | | const struct nettle_cipher_st *cipher; |
119 | | void *ctx_ptr; /* always 16-aligned */ |
120 | | uint8_t iv[MAX_CIPHER_BLOCK_SIZE]; |
121 | | unsigned iv_size; |
122 | | |
123 | | bool enc; |
124 | | size_t rekey_counter; |
125 | | }; |
126 | | |
127 | 0 | #define AES_GCM_ENCRYPT_MAX_BYTES ((1ULL << 36) - 32) |
128 | | static inline int record_aes_gcm_encrypt_size(size_t *counter, size_t size) |
129 | 0 | { |
130 | 0 | size_t sum; |
131 | |
|
132 | 0 | if (!INT_ADD_OK(*counter, size, &sum) || |
133 | 0 | sum > AES_GCM_ENCRYPT_MAX_BYTES) { |
134 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
135 | 0 | } |
136 | 0 | *counter = sum; |
137 | |
|
138 | 0 | return 0; |
139 | 0 | } |
140 | | |
141 | | static void _stream_encrypt(struct nettle_cipher_ctx *ctx, size_t length, |
142 | | uint8_t *dst, const uint8_t *src) |
143 | 0 | { |
144 | 0 | ctx->cipher->encrypt_block(ctx->ctx_ptr, length, dst, src); |
145 | 0 | } |
146 | | |
147 | | static void _stream_decrypt(struct nettle_cipher_ctx *ctx, size_t length, |
148 | | uint8_t *dst, const uint8_t *src) |
149 | 0 | { |
150 | 0 | ctx->cipher->decrypt_block(ctx->ctx_ptr, length, dst, src); |
151 | 0 | } |
152 | | |
153 | | static void _cbc_encrypt(struct nettle_cipher_ctx *ctx, size_t length, |
154 | | uint8_t *dst, const uint8_t *src) |
155 | 0 | { |
156 | 0 | cbc_encrypt(ctx->ctx_ptr, ctx->cipher->encrypt_block, ctx->iv_size, |
157 | 0 | ctx->iv, length, dst, src); |
158 | 0 | } |
159 | | |
160 | | static void _cbc_decrypt(struct nettle_cipher_ctx *ctx, size_t length, |
161 | | uint8_t *dst, const uint8_t *src) |
162 | 0 | { |
163 | 0 | cbc_decrypt(ctx->ctx_ptr, ctx->cipher->decrypt_block, ctx->iv_size, |
164 | 0 | ctx->iv, length, dst, src); |
165 | 0 | } |
166 | | |
167 | | #if ENABLE_GOST |
168 | | struct magma_acpkm_ctx { |
169 | | uint8_t iv[MAGMA_BLOCK_SIZE]; |
170 | | struct acpkm_ctx ctx; |
171 | | struct magma_ctx cipher; |
172 | | }; |
173 | | |
174 | | struct kuznyechik_acpkm_ctx { |
175 | | uint8_t iv[KUZNYECHIK_BLOCK_SIZE]; |
176 | | struct acpkm_ctx ctx; |
177 | | struct kuznyechik_ctx cipher; |
178 | | }; |
179 | | |
180 | | static void _cfb_encrypt(struct nettle_cipher_ctx *ctx, size_t length, |
181 | | uint8_t *dst, const uint8_t *src) |
182 | 0 | { |
183 | 0 | cfb_encrypt(ctx->ctx_ptr, ctx->cipher->encrypt_block, ctx->iv_size, |
184 | 0 | ctx->iv, length, dst, src); |
185 | 0 | } |
186 | | |
187 | | static void _cfb_decrypt(struct nettle_cipher_ctx *ctx, size_t length, |
188 | | uint8_t *dst, const uint8_t *src) |
189 | 0 | { |
190 | 0 | cfb_decrypt(ctx->ctx_ptr, ctx->cipher->encrypt_block, ctx->iv_size, |
191 | 0 | ctx->iv, length, dst, src); |
192 | 0 | } |
193 | | |
194 | | static void _ctr_acpkm_crypt(struct nettle_cipher_ctx *ctx, size_t length, |
195 | | uint8_t *dst, const uint8_t *src) |
196 | 0 | { |
197 | | /* Use context-specific IV which comes as a first field */ |
198 | 0 | ctr_crypt(ctx->ctx_ptr, ctx->cipher->encrypt_block, |
199 | 0 | ctx->cipher->block_size, ctx->ctx_ptr, length, dst, src); |
200 | 0 | } |
201 | | |
202 | | static void _gost28147_set_key_tc26z(void *ctx, const uint8_t *key) |
203 | 0 | { |
204 | 0 | gost28147_set_param(ctx, &gost28147_param_TC26_Z); |
205 | 0 | gost28147_set_key(ctx, key); |
206 | 0 | } |
207 | | |
208 | | static void _gost28147_set_key_cpa(void *ctx, const uint8_t *key) |
209 | 0 | { |
210 | 0 | gost28147_set_param(ctx, &gost28147_param_CryptoPro_A); |
211 | 0 | gost28147_set_key(ctx, key); |
212 | 0 | } |
213 | | |
214 | | static void _gost28147_set_key_cpb(void *ctx, const uint8_t *key) |
215 | 0 | { |
216 | 0 | gost28147_set_param(ctx, &gost28147_param_CryptoPro_B); |
217 | 0 | gost28147_set_key(ctx, key); |
218 | 0 | } |
219 | | |
220 | | static void _gost28147_set_key_cpc(void *ctx, const uint8_t *key) |
221 | 0 | { |
222 | 0 | gost28147_set_param(ctx, &gost28147_param_CryptoPro_C); |
223 | 0 | gost28147_set_key(ctx, key); |
224 | 0 | } |
225 | | |
226 | | static void _gost28147_set_key_cpd(void *ctx, const uint8_t *key) |
227 | 0 | { |
228 | 0 | gost28147_set_param(ctx, &gost28147_param_CryptoPro_D); |
229 | 0 | gost28147_set_key(ctx, key); |
230 | 0 | } |
231 | | |
232 | | static void _gost28147_cnt_set_key_tc26z(void *ctx, const uint8_t *key) |
233 | 0 | { |
234 | 0 | gost28147_cnt_init(ctx, key, &gost28147_param_TC26_Z); |
235 | 0 | } |
236 | | |
237 | | static void _gost28147_cnt_set_nonce(void *ctx, size_t length, |
238 | | const uint8_t *nonce) |
239 | 0 | { |
240 | 0 | gost28147_cnt_set_iv(ctx, nonce); |
241 | 0 | } |
242 | | |
243 | | static void _gost28147_cnt_crypt(struct nettle_cipher_ctx *ctx, size_t length, |
244 | | uint8_t *dst, const uint8_t *src) |
245 | 0 | { |
246 | 0 | gost28147_cnt_crypt((void *)ctx->ctx_ptr, length, dst, src); |
247 | 0 | } |
248 | | |
249 | | static void _magma_acpkm_crypt(struct magma_acpkm_ctx *ctx, size_t length, |
250 | | uint8_t *dst, const uint8_t *src) |
251 | 0 | { |
252 | 0 | acpkm_crypt(&ctx->ctx, &ctx->cipher, |
253 | 0 | (nettle_cipher_func *)magma_encrypt, |
254 | 0 | (nettle_set_key_func *)magma_set_key, length, dst, src); |
255 | 0 | } |
256 | | |
257 | | static void _kuznyechik_acpkm_crypt(struct kuznyechik_acpkm_ctx *ctx, |
258 | | size_t length, uint8_t *dst, |
259 | | const uint8_t *src) |
260 | 0 | { |
261 | 0 | acpkm_crypt(&ctx->ctx, &ctx->cipher, |
262 | 0 | (nettle_cipher_func *)kuznyechik_encrypt, |
263 | 0 | (nettle_set_key_func *)kuznyechik_set_key, length, dst, |
264 | 0 | src); |
265 | 0 | } |
266 | | |
267 | | static void _magma_ctr_acpkm_set_key(struct magma_acpkm_ctx *ctx, |
268 | | const uint8_t *key) |
269 | 0 | { |
270 | 0 | magma_set_key(&ctx->cipher, key); |
271 | 0 | ctx->ctx.pos = 0; |
272 | 0 | ctx->ctx.N = 1024; |
273 | 0 | } |
274 | | |
275 | | static void _magma_ctr_acpkm_set_iv(struct magma_acpkm_ctx *ctx, size_t length, |
276 | | const uint8_t *iv) |
277 | 0 | { |
278 | 0 | memcpy(ctx->iv, iv, length); |
279 | 0 | memset(ctx->iv + length, 0, MAGMA_BLOCK_SIZE - length); |
280 | 0 | } |
281 | | |
282 | | static void _kuznyechik_ctr_acpkm_set_key(struct kuznyechik_acpkm_ctx *ctx, |
283 | | const uint8_t *key) |
284 | 0 | { |
285 | 0 | kuznyechik_set_key(&ctx->cipher, key); |
286 | 0 | ctx->ctx.pos = 0; |
287 | 0 | ctx->ctx.N = 4096; |
288 | 0 | } |
289 | | |
290 | | static void _kuznyechik_ctr_acpkm_set_iv(struct kuznyechik_acpkm_ctx *ctx, |
291 | | size_t length, const uint8_t *iv) |
292 | 0 | { |
293 | 0 | memcpy(ctx->iv, iv, length); |
294 | 0 | memset(ctx->iv + length, 0, KUZNYECHIK_BLOCK_SIZE - length); |
295 | 0 | } |
296 | | #endif |
297 | | |
298 | | static void _ccm_encrypt(struct nettle_cipher_ctx *ctx, size_t nonce_size, |
299 | | const void *nonce, size_t auth_size, const void *auth, |
300 | | size_t tag_size, size_t length, uint8_t *dst, |
301 | | const uint8_t *src) |
302 | 0 | { |
303 | 0 | ccm_encrypt_message((void *)ctx->ctx_ptr, ctx->cipher->encrypt_block, |
304 | 0 | nonce_size, nonce, auth_size, auth, tag_size, |
305 | 0 | length, dst, src); |
306 | 0 | } |
307 | | |
308 | | static int _ccm_decrypt(struct nettle_cipher_ctx *ctx, size_t nonce_size, |
309 | | const void *nonce, size_t auth_size, const void *auth, |
310 | | size_t tag_size, size_t length, uint8_t *dst, |
311 | | const uint8_t *src) |
312 | 0 | { |
313 | 0 | return ccm_decrypt_message((void *)ctx->ctx_ptr, |
314 | 0 | ctx->cipher->encrypt_block, nonce_size, |
315 | 0 | nonce, auth_size, auth, tag_size, length, |
316 | 0 | dst, src); |
317 | 0 | } |
318 | | |
319 | | static void _siv_cmac_aes128_encrypt_message(struct nettle_cipher_ctx *ctx, |
320 | | size_t nonce_size, |
321 | | const void *nonce, |
322 | | size_t auth_size, const void *auth, |
323 | | size_t tag_size, size_t length, |
324 | | uint8_t *dst, const uint8_t *src) |
325 | 0 | { |
326 | 0 | siv_cmac_aes128_encrypt_message((void *)ctx->ctx_ptr, nonce_size, nonce, |
327 | 0 | auth_size, auth, length, dst, src); |
328 | 0 | } |
329 | | |
330 | | static int _siv_cmac_aes128_decrypt_message(struct nettle_cipher_ctx *ctx, |
331 | | size_t nonce_size, |
332 | | const void *nonce, size_t auth_size, |
333 | | const void *auth, size_t tag_size, |
334 | | size_t length, uint8_t *dst, |
335 | | const uint8_t *src) |
336 | 0 | { |
337 | 0 | return siv_cmac_aes128_decrypt_message((void *)ctx->ctx_ptr, nonce_size, |
338 | 0 | nonce, auth_size, auth, length, |
339 | 0 | dst, src); |
340 | 0 | } |
341 | | |
342 | | static void _siv_cmac_aes256_encrypt_message(struct nettle_cipher_ctx *ctx, |
343 | | size_t nonce_size, |
344 | | const void *nonce, |
345 | | size_t auth_size, const void *auth, |
346 | | size_t tag_size, size_t length, |
347 | | uint8_t *dst, const uint8_t *src) |
348 | 0 | { |
349 | 0 | siv_cmac_aes256_encrypt_message((void *)ctx->ctx_ptr, nonce_size, nonce, |
350 | 0 | auth_size, auth, length, dst, src); |
351 | 0 | } |
352 | | |
353 | | static int _siv_cmac_aes256_decrypt_message(struct nettle_cipher_ctx *ctx, |
354 | | size_t nonce_size, |
355 | | const void *nonce, size_t auth_size, |
356 | | const void *auth, size_t tag_size, |
357 | | size_t length, uint8_t *dst, |
358 | | const uint8_t *src) |
359 | 0 | { |
360 | 0 | return siv_cmac_aes256_decrypt_message((void *)ctx->ctx_ptr, nonce_size, |
361 | 0 | nonce, auth_size, auth, length, |
362 | 0 | dst, src); |
363 | 0 | } |
364 | | |
365 | | static void _siv_gcm_aes128_encrypt_message(struct nettle_cipher_ctx *ctx, |
366 | | size_t nonce_size, |
367 | | const void *nonce, size_t auth_size, |
368 | | const void *auth, size_t tag_size, |
369 | | size_t length, uint8_t *dst, |
370 | | const uint8_t *src) |
371 | 0 | { |
372 | 0 | siv_gcm_aes128_encrypt_message((void *)ctx->ctx_ptr, nonce_size, nonce, |
373 | 0 | auth_size, auth, length, dst, src); |
374 | 0 | } |
375 | | |
376 | | static int _siv_gcm_aes128_decrypt_message(struct nettle_cipher_ctx *ctx, |
377 | | size_t nonce_size, const void *nonce, |
378 | | size_t auth_size, const void *auth, |
379 | | size_t tag_size, size_t length, |
380 | | uint8_t *dst, const uint8_t *src) |
381 | 0 | { |
382 | 0 | return siv_gcm_aes128_decrypt_message((void *)ctx->ctx_ptr, nonce_size, |
383 | 0 | nonce, auth_size, auth, length, |
384 | 0 | dst, src); |
385 | 0 | } |
386 | | |
387 | | static void _siv_gcm_aes256_encrypt_message(struct nettle_cipher_ctx *ctx, |
388 | | size_t nonce_size, |
389 | | const void *nonce, size_t auth_size, |
390 | | const void *auth, size_t tag_size, |
391 | | size_t length, uint8_t *dst, |
392 | | const uint8_t *src) |
393 | 0 | { |
394 | 0 | siv_gcm_aes256_encrypt_message((void *)ctx->ctx_ptr, nonce_size, nonce, |
395 | 0 | auth_size, auth, length, dst, src); |
396 | 0 | } |
397 | | |
398 | | static int _siv_gcm_aes256_decrypt_message(struct nettle_cipher_ctx *ctx, |
399 | | size_t nonce_size, const void *nonce, |
400 | | size_t auth_size, const void *auth, |
401 | | size_t tag_size, size_t length, |
402 | | uint8_t *dst, const uint8_t *src) |
403 | 0 | { |
404 | 0 | return siv_gcm_aes256_decrypt_message((void *)ctx->ctx_ptr, nonce_size, |
405 | 0 | nonce, auth_size, auth, length, |
406 | 0 | dst, src); |
407 | 0 | } |
408 | | |
409 | | static void _chacha_set_nonce(struct chacha_ctx *ctx, size_t length, |
410 | | const uint8_t *nonce) |
411 | 0 | { |
412 | 0 | chacha_set_nonce(ctx, nonce + CHACHA_COUNTER_SIZE); |
413 | 0 | chacha_set_counter(ctx, nonce); |
414 | 0 | } |
415 | | |
416 | | static void _chacha_set_nonce96(struct chacha_ctx *ctx, size_t length, |
417 | | const uint8_t *nonce) |
418 | 0 | { |
419 | 0 | chacha_set_nonce96(ctx, nonce + CHACHA_COUNTER32_SIZE); |
420 | 0 | chacha_set_counter32(ctx, nonce); |
421 | 0 | } |
422 | | |
423 | | static void _chacha_poly1305_set_nonce(struct chacha_poly1305_ctx *ctx, |
424 | | size_t length, const uint8_t *nonce) |
425 | 0 | { |
426 | 0 | chacha_poly1305_set_nonce(ctx, nonce); |
427 | 0 | } |
428 | | |
429 | | struct gcm_cast_st { |
430 | | struct gcm_key key; |
431 | | struct gcm_ctx gcm; |
432 | | unsigned long xx[1]; |
433 | | }; |
434 | 0 | #define GCM_CTX_GET_KEY(ptr) (&((struct gcm_cast_st *)ptr)->key) |
435 | 0 | #define GCM_CTX_GET_CTX(ptr) (&((struct gcm_cast_st *)ptr)->gcm) |
436 | 0 | #define GCM_CTX_GET_CIPHER(ptr) ((void *)&((struct gcm_cast_st *)ptr)->xx) |
437 | | |
438 | | static void _gcm_encrypt(struct nettle_cipher_ctx *ctx, size_t length, |
439 | | uint8_t *dst, const uint8_t *src) |
440 | 0 | { |
441 | 0 | gcm_encrypt(GCM_CTX_GET_CTX(ctx->ctx_ptr), |
442 | 0 | GCM_CTX_GET_KEY(ctx->ctx_ptr), |
443 | 0 | GCM_CTX_GET_CIPHER(ctx->ctx_ptr), |
444 | 0 | ctx->cipher->encrypt_block, length, dst, src); |
445 | 0 | } |
446 | | |
447 | | static void _gcm_decrypt(struct nettle_cipher_ctx *ctx, size_t length, |
448 | | uint8_t *dst, const uint8_t *src) |
449 | 0 | { |
450 | 0 | gcm_decrypt(GCM_CTX_GET_CTX(ctx->ctx_ptr), |
451 | 0 | GCM_CTX_GET_KEY(ctx->ctx_ptr), |
452 | 0 | GCM_CTX_GET_CIPHER(ctx->ctx_ptr), |
453 | 0 | ctx->cipher->encrypt_block, length, dst, src); |
454 | 0 | } |
455 | | |
456 | | static void _des_set_key(struct des_ctx *ctx, const uint8_t *key) |
457 | 0 | { |
458 | 0 | des_set_key(ctx, key); |
459 | 0 | } |
460 | | |
461 | | static void _des3_set_key(struct des3_ctx *ctx, const uint8_t *key) |
462 | 0 | { |
463 | 0 | des3_set_key(ctx, key); |
464 | 0 | } |
465 | | |
466 | | static void _cfb8_encrypt(struct nettle_cipher_ctx *ctx, size_t length, |
467 | | uint8_t *dst, const uint8_t *src) |
468 | 0 | { |
469 | 0 | cfb8_encrypt(ctx->ctx_ptr, ctx->cipher->encrypt_block, ctx->iv_size, |
470 | 0 | ctx->iv, length, dst, src); |
471 | 0 | } |
472 | | |
473 | | static void _cfb8_decrypt(struct nettle_cipher_ctx *ctx, size_t length, |
474 | | uint8_t *dst, const uint8_t *src) |
475 | 0 | { |
476 | 0 | cfb8_decrypt(ctx->ctx_ptr, ctx->cipher->encrypt_block, ctx->iv_size, |
477 | 0 | ctx->iv, length, dst, src); |
478 | 0 | } |
479 | | |
480 | | static void _xts_aes128_encrypt(struct nettle_cipher_ctx *ctx, size_t length, |
481 | | uint8_t *dst, const uint8_t *src) |
482 | 0 | { |
483 | 0 | xts_aes128_encrypt_message(ctx->ctx_ptr, ctx->iv, length, dst, src); |
484 | 0 | } |
485 | | |
486 | | static void _xts_aes128_decrypt(struct nettle_cipher_ctx *ctx, size_t length, |
487 | | uint8_t *dst, const uint8_t *src) |
488 | 0 | { |
489 | 0 | xts_aes128_decrypt_message(ctx->ctx_ptr, ctx->iv, length, dst, src); |
490 | 0 | } |
491 | | |
492 | | static void _xts_aes256_encrypt(struct nettle_cipher_ctx *ctx, size_t length, |
493 | | uint8_t *dst, const uint8_t *src) |
494 | 0 | { |
495 | 0 | xts_aes256_encrypt_message(ctx->ctx_ptr, ctx->iv, length, dst, src); |
496 | 0 | } |
497 | | |
498 | | static void _xts_aes256_decrypt(struct nettle_cipher_ctx *ctx, size_t length, |
499 | | uint8_t *dst, const uint8_t *src) |
500 | 0 | { |
501 | 0 | xts_aes256_decrypt_message(ctx->ctx_ptr, ctx->iv, length, dst, src); |
502 | 0 | } |
503 | | |
504 | | static const struct nettle_cipher_st builtin_ciphers[] = { |
505 | | { |
506 | | .algo = GNUTLS_CIPHER_AES_128_GCM, |
507 | | .block_size = AES_BLOCK_SIZE, |
508 | | .key_size = AES128_KEY_SIZE, |
509 | | .encrypt_block = (nettle_cipher_func *)aes128_encrypt, |
510 | | .decrypt_block = (nettle_cipher_func *)aes128_decrypt, |
511 | | |
512 | | .ctx_size = sizeof(struct gcm_aes128_ctx), |
513 | | .encrypt = _gcm_encrypt, |
514 | | .decrypt = _gcm_decrypt, |
515 | | .set_encrypt_key = (nettle_set_key_func *)gcm_aes128_set_key, |
516 | | .set_decrypt_key = (nettle_set_key_func *)gcm_aes128_set_key, |
517 | | |
518 | | .tag = (nettle_hash_digest_func *)gcm_aes128_digest, |
519 | | .auth = (nettle_hash_update_func *)gcm_aes128_update, |
520 | | .set_iv = (setiv_func)gcm_aes128_set_iv, |
521 | | .max_iv_size = GCM_IV_SIZE, |
522 | | }, |
523 | | { |
524 | | .algo = GNUTLS_CIPHER_AES_192_GCM, |
525 | | .block_size = AES_BLOCK_SIZE, |
526 | | .key_size = AES192_KEY_SIZE, |
527 | | .encrypt_block = (nettle_cipher_func *)aes192_encrypt, |
528 | | .decrypt_block = (nettle_cipher_func *)aes192_decrypt, |
529 | | |
530 | | .ctx_size = sizeof(struct gcm_aes192_ctx), |
531 | | .encrypt = _gcm_encrypt, |
532 | | .decrypt = _gcm_decrypt, |
533 | | .set_encrypt_key = (nettle_set_key_func *)gcm_aes192_set_key, |
534 | | .set_decrypt_key = (nettle_set_key_func *)gcm_aes192_set_key, |
535 | | |
536 | | .tag = (nettle_hash_digest_func *)gcm_aes192_digest, |
537 | | .auth = (nettle_hash_update_func *)gcm_aes192_update, |
538 | | .set_iv = (setiv_func)gcm_aes192_set_iv, |
539 | | .max_iv_size = GCM_IV_SIZE, |
540 | | }, |
541 | | { |
542 | | .algo = GNUTLS_CIPHER_AES_256_GCM, |
543 | | .block_size = AES_BLOCK_SIZE, |
544 | | .key_size = AES256_KEY_SIZE, |
545 | | .encrypt_block = (nettle_cipher_func *)aes256_encrypt, |
546 | | .decrypt_block = (nettle_cipher_func *)aes256_decrypt, |
547 | | |
548 | | .ctx_size = sizeof(struct gcm_aes256_ctx), |
549 | | .encrypt = _gcm_encrypt, |
550 | | .decrypt = _gcm_decrypt, |
551 | | .set_encrypt_key = (nettle_set_key_func *)gcm_aes256_set_key, |
552 | | .set_decrypt_key = (nettle_set_key_func *)gcm_aes256_set_key, |
553 | | |
554 | | .tag = (nettle_hash_digest_func *)gcm_aes256_digest, |
555 | | .auth = (nettle_hash_update_func *)gcm_aes256_update, |
556 | | .set_iv = (setiv_func)gcm_aes256_set_iv, |
557 | | .max_iv_size = GCM_IV_SIZE, |
558 | | }, |
559 | | { |
560 | | .algo = GNUTLS_CIPHER_AES_128_CCM, |
561 | | .block_size = AES_BLOCK_SIZE, |
562 | | .key_size = AES128_KEY_SIZE, |
563 | | .encrypt_block = (nettle_cipher_func *)aes128_encrypt, |
564 | | .decrypt_block = (nettle_cipher_func *)aes128_decrypt, |
565 | | |
566 | | .ctx_size = sizeof(struct aes128_ctx), |
567 | | .aead_encrypt = _ccm_encrypt, |
568 | | .aead_decrypt = _ccm_decrypt, |
569 | | .set_encrypt_key = |
570 | | (nettle_set_key_func *)aes128_set_encrypt_key, |
571 | | .set_decrypt_key = |
572 | | (nettle_set_key_func *)aes128_set_encrypt_key, |
573 | | .max_iv_size = CCM_MAX_NONCE_SIZE, |
574 | | }, |
575 | | { |
576 | | .algo = GNUTLS_CIPHER_AES_128_CCM_8, |
577 | | .block_size = AES_BLOCK_SIZE, |
578 | | .key_size = AES128_KEY_SIZE, |
579 | | .encrypt_block = (nettle_cipher_func *)aes128_encrypt, |
580 | | .decrypt_block = (nettle_cipher_func *)aes128_decrypt, |
581 | | |
582 | | .ctx_size = sizeof(struct aes128_ctx), |
583 | | .aead_encrypt = _ccm_encrypt, |
584 | | .aead_decrypt = _ccm_decrypt, |
585 | | .set_encrypt_key = |
586 | | (nettle_set_key_func *)aes128_set_encrypt_key, |
587 | | .set_decrypt_key = |
588 | | (nettle_set_key_func *)aes128_set_encrypt_key, |
589 | | .max_iv_size = CCM_MAX_NONCE_SIZE, |
590 | | }, |
591 | | { |
592 | | .algo = GNUTLS_CIPHER_AES_256_CCM, |
593 | | .block_size = AES_BLOCK_SIZE, |
594 | | .key_size = AES256_KEY_SIZE, |
595 | | .encrypt_block = (nettle_cipher_func *)aes256_encrypt, |
596 | | .decrypt_block = (nettle_cipher_func *)aes256_decrypt, |
597 | | |
598 | | .ctx_size = sizeof(struct aes256_ctx), |
599 | | .aead_encrypt = _ccm_encrypt, |
600 | | .aead_decrypt = _ccm_decrypt, |
601 | | .set_encrypt_key = |
602 | | (nettle_set_key_func *)aes256_set_encrypt_key, |
603 | | .set_decrypt_key = |
604 | | (nettle_set_key_func *)aes256_set_encrypt_key, |
605 | | .max_iv_size = CCM_MAX_NONCE_SIZE, |
606 | | }, |
607 | | { |
608 | | .algo = GNUTLS_CIPHER_AES_256_CCM_8, |
609 | | .block_size = AES_BLOCK_SIZE, |
610 | | .key_size = AES256_KEY_SIZE, |
611 | | .encrypt_block = (nettle_cipher_func *)aes256_encrypt, |
612 | | .decrypt_block = (nettle_cipher_func *)aes256_decrypt, |
613 | | |
614 | | .ctx_size = sizeof(struct aes256_ctx), |
615 | | .aead_encrypt = _ccm_encrypt, |
616 | | .aead_decrypt = _ccm_decrypt, |
617 | | .set_encrypt_key = |
618 | | (nettle_set_key_func *)aes256_set_encrypt_key, |
619 | | .set_decrypt_key = |
620 | | (nettle_set_key_func *)aes256_set_encrypt_key, |
621 | | .max_iv_size = CCM_MAX_NONCE_SIZE, |
622 | | }, |
623 | | { .algo = GNUTLS_CIPHER_CAMELLIA_128_GCM, |
624 | | .block_size = CAMELLIA_BLOCK_SIZE, |
625 | | .key_size = CAMELLIA128_KEY_SIZE, |
626 | | .encrypt_block = (nettle_cipher_func *)camellia128_crypt, |
627 | | .decrypt_block = (nettle_cipher_func *)camellia128_crypt, |
628 | | |
629 | | .ctx_size = sizeof(struct gcm_camellia128_ctx), |
630 | | .encrypt = _gcm_encrypt, |
631 | | .decrypt = _gcm_decrypt, |
632 | | .set_encrypt_key = (nettle_set_key_func *)gcm_camellia128_set_key, |
633 | | .set_decrypt_key = (nettle_set_key_func *)gcm_camellia128_set_key, |
634 | | .tag = (nettle_hash_digest_func *)gcm_camellia128_digest, |
635 | | .auth = (nettle_hash_update_func *)gcm_camellia128_update, |
636 | | .max_iv_size = GCM_IV_SIZE, |
637 | | .set_iv = (setiv_func)gcm_camellia128_set_iv }, |
638 | | { .algo = GNUTLS_CIPHER_CAMELLIA_256_GCM, |
639 | | .block_size = CAMELLIA_BLOCK_SIZE, |
640 | | .key_size = CAMELLIA256_KEY_SIZE, |
641 | | .encrypt_block = (nettle_cipher_func *)camellia256_crypt, |
642 | | .decrypt_block = (nettle_cipher_func *)camellia256_crypt, |
643 | | |
644 | | .ctx_size = sizeof(struct gcm_camellia256_ctx), |
645 | | .encrypt = _gcm_encrypt, |
646 | | .decrypt = _gcm_decrypt, |
647 | | .set_encrypt_key = (nettle_set_key_func *)gcm_camellia256_set_key, |
648 | | .set_decrypt_key = (nettle_set_key_func *)gcm_camellia256_set_key, |
649 | | .tag = (nettle_hash_digest_func *)gcm_camellia256_digest, |
650 | | .auth = (nettle_hash_update_func *)gcm_camellia256_update, |
651 | | .max_iv_size = GCM_IV_SIZE, |
652 | | .set_iv = (setiv_func)gcm_camellia256_set_iv }, |
653 | | { |
654 | | .algo = GNUTLS_CIPHER_AES_128_CBC, |
655 | | .block_size = AES_BLOCK_SIZE, |
656 | | .key_size = AES128_KEY_SIZE, |
657 | | .encrypt_block = (nettle_cipher_func *)aes128_encrypt, |
658 | | .decrypt_block = (nettle_cipher_func *)aes128_decrypt, |
659 | | |
660 | | .ctx_size = sizeof( |
661 | | struct CBC_CTX(struct aes128_ctx, AES_BLOCK_SIZE)), |
662 | | .encrypt = _cbc_encrypt, |
663 | | .decrypt = _cbc_decrypt, |
664 | | .set_encrypt_key = |
665 | | (nettle_set_key_func *)aes128_set_encrypt_key, |
666 | | .set_decrypt_key = |
667 | | (nettle_set_key_func *)aes128_set_decrypt_key, |
668 | | .max_iv_size = AES_BLOCK_SIZE, |
669 | | }, |
670 | | { |
671 | | .algo = GNUTLS_CIPHER_AES_192_CBC, |
672 | | .block_size = AES_BLOCK_SIZE, |
673 | | .key_size = AES192_KEY_SIZE, |
674 | | .encrypt_block = (nettle_cipher_func *)aes192_encrypt, |
675 | | .decrypt_block = (nettle_cipher_func *)aes192_decrypt, |
676 | | |
677 | | .ctx_size = sizeof( |
678 | | struct CBC_CTX(struct aes192_ctx, AES_BLOCK_SIZE)), |
679 | | .encrypt = _cbc_encrypt, |
680 | | .decrypt = _cbc_decrypt, |
681 | | .set_encrypt_key = |
682 | | (nettle_set_key_func *)aes192_set_encrypt_key, |
683 | | .set_decrypt_key = |
684 | | (nettle_set_key_func *)aes192_set_decrypt_key, |
685 | | .max_iv_size = AES_BLOCK_SIZE, |
686 | | }, |
687 | | { |
688 | | .algo = GNUTLS_CIPHER_AES_256_CBC, |
689 | | .block_size = AES_BLOCK_SIZE, |
690 | | .key_size = AES256_KEY_SIZE, |
691 | | .encrypt_block = (nettle_cipher_func *)aes256_encrypt, |
692 | | .decrypt_block = (nettle_cipher_func *)aes256_decrypt, |
693 | | |
694 | | .ctx_size = sizeof( |
695 | | struct CBC_CTX(struct aes256_ctx, AES_BLOCK_SIZE)), |
696 | | .encrypt = _cbc_encrypt, |
697 | | .decrypt = _cbc_decrypt, |
698 | | .set_encrypt_key = |
699 | | (nettle_set_key_func *)aes256_set_encrypt_key, |
700 | | .set_decrypt_key = |
701 | | (nettle_set_key_func *)aes256_set_decrypt_key, |
702 | | .max_iv_size = AES_BLOCK_SIZE, |
703 | | }, |
704 | | { |
705 | | .algo = GNUTLS_CIPHER_CAMELLIA_128_CBC, |
706 | | .block_size = CAMELLIA_BLOCK_SIZE, |
707 | | .key_size = CAMELLIA128_KEY_SIZE, |
708 | | .encrypt_block = (nettle_cipher_func *)camellia128_crypt, |
709 | | .decrypt_block = (nettle_cipher_func *)camellia128_crypt, |
710 | | |
711 | | .ctx_size = sizeof(struct CBC_CTX(struct camellia128_ctx, |
712 | | CAMELLIA_BLOCK_SIZE)), |
713 | | .encrypt = _cbc_encrypt, |
714 | | .decrypt = _cbc_decrypt, |
715 | | .set_encrypt_key = |
716 | | (nettle_set_key_func *)camellia128_set_encrypt_key, |
717 | | .set_decrypt_key = |
718 | | (nettle_set_key_func *)camellia128_set_decrypt_key, |
719 | | .max_iv_size = CAMELLIA_BLOCK_SIZE, |
720 | | }, |
721 | | { |
722 | | .algo = GNUTLS_CIPHER_CAMELLIA_192_CBC, |
723 | | .block_size = CAMELLIA_BLOCK_SIZE, |
724 | | .key_size = CAMELLIA192_KEY_SIZE, |
725 | | .encrypt_block = (nettle_cipher_func *)camellia192_crypt, |
726 | | .decrypt_block = (nettle_cipher_func *)camellia192_crypt, |
727 | | |
728 | | .ctx_size = sizeof(struct CBC_CTX(struct camellia192_ctx, |
729 | | CAMELLIA_BLOCK_SIZE)), |
730 | | .encrypt = _cbc_encrypt, |
731 | | .decrypt = _cbc_decrypt, |
732 | | .set_encrypt_key = |
733 | | (nettle_set_key_func *)camellia192_set_encrypt_key, |
734 | | .set_decrypt_key = |
735 | | (nettle_set_key_func *)camellia192_set_decrypt_key, |
736 | | .max_iv_size = CAMELLIA_BLOCK_SIZE, |
737 | | }, |
738 | | { |
739 | | .algo = GNUTLS_CIPHER_CAMELLIA_256_CBC, |
740 | | .block_size = CAMELLIA_BLOCK_SIZE, |
741 | | .key_size = CAMELLIA256_KEY_SIZE, |
742 | | .encrypt_block = (nettle_cipher_func *)camellia256_crypt, |
743 | | .decrypt_block = (nettle_cipher_func *)camellia256_crypt, |
744 | | |
745 | | .ctx_size = sizeof(struct CBC_CTX(struct camellia256_ctx, |
746 | | CAMELLIA_BLOCK_SIZE)), |
747 | | .encrypt = _cbc_encrypt, |
748 | | .decrypt = _cbc_decrypt, |
749 | | .set_encrypt_key = |
750 | | (nettle_set_key_func *)camellia256_set_encrypt_key, |
751 | | .set_decrypt_key = |
752 | | (nettle_set_key_func *)camellia256_set_decrypt_key, |
753 | | .max_iv_size = CAMELLIA_BLOCK_SIZE, |
754 | | }, |
755 | | { |
756 | | .algo = GNUTLS_CIPHER_RC2_40_CBC, |
757 | | .block_size = ARCTWO_BLOCK_SIZE, |
758 | | .key_size = 5, |
759 | | .encrypt_block = (nettle_cipher_func *)arctwo_encrypt, |
760 | | .decrypt_block = (nettle_cipher_func *)arctwo_decrypt, |
761 | | |
762 | | .ctx_size = sizeof( |
763 | | struct CBC_CTX(struct arctwo_ctx, ARCTWO_BLOCK_SIZE)), |
764 | | .encrypt = _cbc_encrypt, |
765 | | .decrypt = _cbc_decrypt, |
766 | | .set_encrypt_key = (nettle_set_key_func *)arctwo40_set_key, |
767 | | .set_decrypt_key = (nettle_set_key_func *)arctwo40_set_key, |
768 | | .max_iv_size = ARCTWO_BLOCK_SIZE, |
769 | | }, |
770 | | { |
771 | | .algo = GNUTLS_CIPHER_DES_CBC, |
772 | | .block_size = DES_BLOCK_SIZE, |
773 | | .key_size = DES_KEY_SIZE, |
774 | | .encrypt_block = (nettle_cipher_func *)des_encrypt, |
775 | | .decrypt_block = (nettle_cipher_func *)des_decrypt, |
776 | | |
777 | | .ctx_size = |
778 | | sizeof(struct CBC_CTX(struct des_ctx, DES_BLOCK_SIZE)), |
779 | | .encrypt = _cbc_encrypt, |
780 | | .decrypt = _cbc_decrypt, |
781 | | .set_encrypt_key = (nettle_set_key_func *)_des_set_key, |
782 | | .set_decrypt_key = (nettle_set_key_func *)_des_set_key, |
783 | | .max_iv_size = DES_BLOCK_SIZE, |
784 | | }, |
785 | | { |
786 | | .algo = GNUTLS_CIPHER_3DES_CBC, |
787 | | .block_size = DES3_BLOCK_SIZE, |
788 | | .key_size = DES3_KEY_SIZE, |
789 | | .encrypt_block = (nettle_cipher_func *)des3_encrypt, |
790 | | .decrypt_block = (nettle_cipher_func *)des3_decrypt, |
791 | | |
792 | | .ctx_size = sizeof( |
793 | | struct CBC_CTX(struct des3_ctx, DES3_BLOCK_SIZE)), |
794 | | .encrypt = _cbc_encrypt, |
795 | | .decrypt = _cbc_decrypt, |
796 | | .set_encrypt_key = (nettle_set_key_func *)_des3_set_key, |
797 | | .set_decrypt_key = (nettle_set_key_func *)_des3_set_key, |
798 | | .max_iv_size = DES_BLOCK_SIZE, |
799 | | }, |
800 | | { |
801 | | .algo = GNUTLS_CIPHER_ARCFOUR_128, |
802 | | .block_size = 1, |
803 | | .key_size = 0, |
804 | | .encrypt_block = (nettle_cipher_func *)arcfour_crypt, |
805 | | .decrypt_block = (nettle_cipher_func *)arcfour_crypt, |
806 | | |
807 | | .ctx_size = sizeof(struct arcfour_ctx), |
808 | | .encrypt = _stream_encrypt, |
809 | | .decrypt = _stream_encrypt, |
810 | | .gen_set_key = (gen_setkey_func)arcfour_set_key, |
811 | | .set_encrypt_key = (nettle_set_key_func *)arcfour128_set_key, |
812 | | .set_decrypt_key = (nettle_set_key_func *)arcfour128_set_key, |
813 | | }, |
814 | | { |
815 | | .algo = GNUTLS_CIPHER_SALSA20_256, |
816 | | .block_size = 1, |
817 | | .key_size = SALSA20_256_KEY_SIZE, |
818 | | .encrypt_block = (nettle_cipher_func *)salsa20_crypt, |
819 | | .decrypt_block = (nettle_cipher_func *)salsa20_crypt, |
820 | | |
821 | | .ctx_size = sizeof(struct salsa20_ctx), |
822 | | .encrypt = _stream_encrypt, |
823 | | .decrypt = _stream_encrypt, |
824 | | .set_encrypt_key = (nettle_set_key_func *)salsa20_256_set_key, |
825 | | .set_decrypt_key = (nettle_set_key_func *)salsa20_256_set_key, |
826 | | .max_iv_size = SALSA20_NONCE_SIZE, |
827 | | }, |
828 | | { |
829 | | .algo = GNUTLS_CIPHER_ESTREAM_SALSA20_256, |
830 | | .block_size = 1, |
831 | | .key_size = SALSA20_256_KEY_SIZE, |
832 | | .encrypt_block = (nettle_cipher_func *)salsa20r12_crypt, |
833 | | .decrypt_block = (nettle_cipher_func *)salsa20r12_crypt, |
834 | | |
835 | | .ctx_size = sizeof(struct salsa20_ctx), |
836 | | .encrypt = _stream_encrypt, |
837 | | .decrypt = _stream_encrypt, |
838 | | .set_encrypt_key = (nettle_set_key_func *)salsa20_256_set_key, |
839 | | .set_decrypt_key = (nettle_set_key_func *)salsa20_256_set_key, |
840 | | .max_iv_size = SALSA20_NONCE_SIZE, |
841 | | }, |
842 | | { |
843 | | .algo = GNUTLS_CIPHER_CHACHA20_32, |
844 | | .block_size = 1, |
845 | | .key_size = CHACHA_KEY_SIZE, |
846 | | .encrypt_block = (nettle_cipher_func *)chacha_crypt32, |
847 | | .decrypt_block = (nettle_cipher_func *)chacha_crypt32, |
848 | | |
849 | | .ctx_size = sizeof(struct chacha_ctx), |
850 | | .encrypt = _stream_encrypt, |
851 | | .decrypt = _stream_encrypt, |
852 | | .set_encrypt_key = (nettle_set_key_func *)chacha_set_key, |
853 | | .set_decrypt_key = (nettle_set_key_func *)chacha_set_key, |
854 | | .set_iv = (setiv_func)_chacha_set_nonce96, |
855 | | /* we allow setting the initial block counter as part of nonce */ |
856 | | .max_iv_size = CHACHA_NONCE96_SIZE + CHACHA_COUNTER32_SIZE, |
857 | | }, |
858 | | { |
859 | | .algo = GNUTLS_CIPHER_CHACHA20_64, |
860 | | .block_size = 1, |
861 | | .key_size = CHACHA_KEY_SIZE, |
862 | | .encrypt_block = (nettle_cipher_func *)chacha_crypt, |
863 | | .decrypt_block = (nettle_cipher_func *)chacha_crypt, |
864 | | |
865 | | .ctx_size = sizeof(struct chacha_ctx), |
866 | | .encrypt = _stream_encrypt, |
867 | | .decrypt = _stream_encrypt, |
868 | | .set_encrypt_key = (nettle_set_key_func *)chacha_set_key, |
869 | | .set_decrypt_key = (nettle_set_key_func *)chacha_set_key, |
870 | | .set_iv = (setiv_func)_chacha_set_nonce, |
871 | | /* we allow setting the initial block counter as part of nonce */ |
872 | | .max_iv_size = CHACHA_NONCE_SIZE + CHACHA_COUNTER_SIZE, |
873 | | }, |
874 | | { |
875 | | .algo = GNUTLS_CIPHER_CHACHA20_POLY1305, |
876 | | .block_size = CHACHA_POLY1305_BLOCK_SIZE, |
877 | | .key_size = CHACHA_POLY1305_KEY_SIZE, |
878 | | |
879 | | .ctx_size = sizeof(struct chacha_poly1305_ctx), |
880 | | .encrypt_block = (nettle_cipher_func *)chacha_poly1305_encrypt, |
881 | | .decrypt_block = (nettle_cipher_func *)chacha_poly1305_decrypt, |
882 | | .encrypt = _stream_encrypt, |
883 | | .decrypt = _stream_decrypt, |
884 | | .auth = (nettle_hash_update_func *)chacha_poly1305_update, |
885 | | .tag = (nettle_hash_digest_func *)chacha_poly1305_digest, |
886 | | .set_encrypt_key = |
887 | | (nettle_set_key_func *)chacha_poly1305_set_key, |
888 | | .set_decrypt_key = |
889 | | (nettle_set_key_func *)chacha_poly1305_set_key, |
890 | | .set_iv = (setiv_func)_chacha_poly1305_set_nonce, |
891 | | .max_iv_size = CHACHA_POLY1305_NONCE_SIZE, |
892 | | }, |
893 | | #if ENABLE_GOST |
894 | | { |
895 | | .algo = GNUTLS_CIPHER_GOST28147_TC26Z_CFB, |
896 | | .block_size = GOST28147_BLOCK_SIZE, |
897 | | .key_size = GOST28147_KEY_SIZE, |
898 | | .encrypt_block = |
899 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
900 | | .decrypt_block = |
901 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
902 | | |
903 | | .ctx_size = sizeof(struct CFB_CTX(struct gost28147_ctx, |
904 | | GOST28147_BLOCK_SIZE)), |
905 | | .encrypt = _cfb_encrypt, |
906 | | .decrypt = _cfb_decrypt, |
907 | | .set_encrypt_key = _gost28147_set_key_tc26z, |
908 | | .set_decrypt_key = _gost28147_set_key_tc26z, |
909 | | }, |
910 | | { |
911 | | .algo = GNUTLS_CIPHER_GOST28147_CPA_CFB, |
912 | | .block_size = GOST28147_BLOCK_SIZE, |
913 | | .key_size = GOST28147_KEY_SIZE, |
914 | | .encrypt_block = |
915 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
916 | | .decrypt_block = |
917 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
918 | | |
919 | | .ctx_size = sizeof(struct CFB_CTX(struct gost28147_ctx, |
920 | | GOST28147_BLOCK_SIZE)), |
921 | | .encrypt = _cfb_encrypt, |
922 | | .decrypt = _cfb_decrypt, |
923 | | .set_encrypt_key = _gost28147_set_key_cpa, |
924 | | .set_decrypt_key = _gost28147_set_key_cpa, |
925 | | }, |
926 | | { |
927 | | .algo = GNUTLS_CIPHER_GOST28147_CPB_CFB, |
928 | | .block_size = GOST28147_BLOCK_SIZE, |
929 | | .key_size = GOST28147_KEY_SIZE, |
930 | | .encrypt_block = |
931 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
932 | | .decrypt_block = |
933 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
934 | | |
935 | | .ctx_size = sizeof(struct CFB_CTX(struct gost28147_ctx, |
936 | | GOST28147_BLOCK_SIZE)), |
937 | | .encrypt = _cfb_encrypt, |
938 | | .decrypt = _cfb_decrypt, |
939 | | .set_encrypt_key = _gost28147_set_key_cpb, |
940 | | .set_decrypt_key = _gost28147_set_key_cpb, |
941 | | }, |
942 | | { |
943 | | .algo = GNUTLS_CIPHER_GOST28147_CPC_CFB, |
944 | | .block_size = GOST28147_BLOCK_SIZE, |
945 | | .key_size = GOST28147_KEY_SIZE, |
946 | | .encrypt_block = |
947 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
948 | | .decrypt_block = |
949 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
950 | | |
951 | | .ctx_size = sizeof(struct CFB_CTX(struct gost28147_ctx, |
952 | | GOST28147_BLOCK_SIZE)), |
953 | | .encrypt = _cfb_encrypt, |
954 | | .decrypt = _cfb_decrypt, |
955 | | .set_encrypt_key = _gost28147_set_key_cpc, |
956 | | .set_decrypt_key = _gost28147_set_key_cpc, |
957 | | }, |
958 | | { |
959 | | .algo = GNUTLS_CIPHER_GOST28147_CPD_CFB, |
960 | | .block_size = GOST28147_BLOCK_SIZE, |
961 | | .key_size = GOST28147_KEY_SIZE, |
962 | | .encrypt_block = |
963 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
964 | | .decrypt_block = |
965 | | (nettle_cipher_func *)gost28147_encrypt_for_cfb, |
966 | | |
967 | | .ctx_size = sizeof(struct CFB_CTX(struct gost28147_ctx, |
968 | | GOST28147_BLOCK_SIZE)), |
969 | | .encrypt = _cfb_encrypt, |
970 | | .decrypt = _cfb_decrypt, |
971 | | .set_encrypt_key = _gost28147_set_key_cpd, |
972 | | .set_decrypt_key = _gost28147_set_key_cpd, |
973 | | }, |
974 | | { |
975 | | .algo = GNUTLS_CIPHER_GOST28147_TC26Z_CNT, |
976 | | .block_size = GOST28147_BLOCK_SIZE, |
977 | | .key_size = GOST28147_KEY_SIZE, |
978 | | .encrypt_block = |
979 | | (nettle_cipher_func *)gost28147_encrypt, /* unused */ |
980 | | .decrypt_block = |
981 | | (nettle_cipher_func *)gost28147_decrypt, /* unused */ |
982 | | |
983 | | .ctx_size = sizeof(struct gost28147_cnt_ctx), |
984 | | .encrypt = _gost28147_cnt_crypt, |
985 | | .decrypt = _gost28147_cnt_crypt, |
986 | | .set_encrypt_key = _gost28147_cnt_set_key_tc26z, |
987 | | .set_decrypt_key = _gost28147_cnt_set_key_tc26z, |
988 | | .set_iv = (setiv_func)_gost28147_cnt_set_nonce, |
989 | | }, |
990 | | { |
991 | | .algo = GNUTLS_CIPHER_MAGMA_CTR_ACPKM, |
992 | | .block_size = MAGMA_BLOCK_SIZE, |
993 | | .key_size = MAGMA_KEY_SIZE, |
994 | | .encrypt_block = (nettle_cipher_func *)_magma_acpkm_crypt, |
995 | | .decrypt_block = (nettle_cipher_func *)_magma_acpkm_crypt, |
996 | | |
997 | | .ctx_size = sizeof(struct magma_acpkm_ctx), |
998 | | .encrypt = _ctr_acpkm_crypt, |
999 | | .decrypt = _ctr_acpkm_crypt, |
1000 | | .set_encrypt_key = |
1001 | | (nettle_set_key_func *)_magma_ctr_acpkm_set_key, |
1002 | | .set_decrypt_key = |
1003 | | (nettle_set_key_func *)_magma_ctr_acpkm_set_key, |
1004 | | .set_iv = (setiv_func)_magma_ctr_acpkm_set_iv, |
1005 | | }, |
1006 | | { |
1007 | | .algo = GNUTLS_CIPHER_KUZNYECHIK_CTR_ACPKM, |
1008 | | .block_size = KUZNYECHIK_BLOCK_SIZE, |
1009 | | .key_size = KUZNYECHIK_KEY_SIZE, |
1010 | | .encrypt_block = (nettle_cipher_func *)_kuznyechik_acpkm_crypt, |
1011 | | .decrypt_block = (nettle_cipher_func *)_kuznyechik_acpkm_crypt, |
1012 | | |
1013 | | .ctx_size = sizeof(struct kuznyechik_acpkm_ctx), |
1014 | | .encrypt = _ctr_acpkm_crypt, |
1015 | | .decrypt = _ctr_acpkm_crypt, |
1016 | | .set_encrypt_key = |
1017 | | (nettle_set_key_func *)_kuznyechik_ctr_acpkm_set_key, |
1018 | | .set_decrypt_key = |
1019 | | (nettle_set_key_func *)_kuznyechik_ctr_acpkm_set_key, |
1020 | | .set_iv = (setiv_func)_kuznyechik_ctr_acpkm_set_iv, |
1021 | | }, |
1022 | | #endif |
1023 | | { |
1024 | | .algo = GNUTLS_CIPHER_AES_128_CFB8, |
1025 | | .block_size = AES_BLOCK_SIZE, |
1026 | | .key_size = AES128_KEY_SIZE, |
1027 | | .encrypt_block = (nettle_cipher_func *)aes128_encrypt, |
1028 | | .decrypt_block = (nettle_cipher_func *)aes128_encrypt, |
1029 | | |
1030 | | .ctx_size = sizeof( |
1031 | | struct CFB8_CTX(struct aes128_ctx, AES_BLOCK_SIZE)), |
1032 | | .encrypt = _cfb8_encrypt, |
1033 | | .decrypt = _cfb8_decrypt, |
1034 | | .set_encrypt_key = |
1035 | | (nettle_set_key_func *)aes128_set_encrypt_key, |
1036 | | .set_decrypt_key = |
1037 | | (nettle_set_key_func *)aes128_set_encrypt_key, |
1038 | | .max_iv_size = AES_BLOCK_SIZE, |
1039 | | }, |
1040 | | { |
1041 | | .algo = GNUTLS_CIPHER_AES_192_CFB8, |
1042 | | .block_size = AES_BLOCK_SIZE, |
1043 | | .key_size = AES192_KEY_SIZE, |
1044 | | .encrypt_block = (nettle_cipher_func *)aes192_encrypt, |
1045 | | .decrypt_block = (nettle_cipher_func *)aes192_encrypt, |
1046 | | |
1047 | | .ctx_size = sizeof( |
1048 | | struct CFB8_CTX(struct aes192_ctx, AES_BLOCK_SIZE)), |
1049 | | .encrypt = _cfb8_encrypt, |
1050 | | .decrypt = _cfb8_decrypt, |
1051 | | .set_encrypt_key = |
1052 | | (nettle_set_key_func *)aes192_set_encrypt_key, |
1053 | | .set_decrypt_key = |
1054 | | (nettle_set_key_func *)aes192_set_encrypt_key, |
1055 | | .max_iv_size = AES_BLOCK_SIZE, |
1056 | | }, |
1057 | | { |
1058 | | .algo = GNUTLS_CIPHER_AES_256_CFB8, |
1059 | | .block_size = AES_BLOCK_SIZE, |
1060 | | .key_size = AES256_KEY_SIZE, |
1061 | | .encrypt_block = (nettle_cipher_func *)aes256_encrypt, |
1062 | | .decrypt_block = (nettle_cipher_func *)aes256_encrypt, |
1063 | | |
1064 | | .ctx_size = sizeof( |
1065 | | struct CFB8_CTX(struct aes256_ctx, AES_BLOCK_SIZE)), |
1066 | | .encrypt = _cfb8_encrypt, |
1067 | | .decrypt = _cfb8_decrypt, |
1068 | | .set_encrypt_key = |
1069 | | (nettle_set_key_func *)aes256_set_encrypt_key, |
1070 | | .set_decrypt_key = |
1071 | | (nettle_set_key_func *)aes256_set_encrypt_key, |
1072 | | .max_iv_size = AES_BLOCK_SIZE, |
1073 | | }, |
1074 | | { |
1075 | | .algo = GNUTLS_CIPHER_AES_128_XTS, |
1076 | | .block_size = AES_BLOCK_SIZE, |
1077 | | .key_size = AES128_KEY_SIZE * 2, |
1078 | | |
1079 | | .ctx_size = sizeof(struct xts_aes128_key), |
1080 | | .encrypt = _xts_aes128_encrypt, |
1081 | | .decrypt = _xts_aes128_decrypt, |
1082 | | .set_encrypt_key = |
1083 | | (nettle_set_key_func *)xts_aes128_set_encrypt_key, |
1084 | | .set_decrypt_key = |
1085 | | (nettle_set_key_func *)xts_aes128_set_decrypt_key, |
1086 | | .max_iv_size = AES_BLOCK_SIZE, |
1087 | | }, |
1088 | | { |
1089 | | .algo = GNUTLS_CIPHER_AES_256_XTS, |
1090 | | .block_size = AES_BLOCK_SIZE, |
1091 | | .key_size = AES256_KEY_SIZE * 2, |
1092 | | |
1093 | | .ctx_size = sizeof(struct xts_aes256_key), |
1094 | | .encrypt = _xts_aes256_encrypt, |
1095 | | .decrypt = _xts_aes256_decrypt, |
1096 | | .set_encrypt_key = |
1097 | | (nettle_set_key_func *)xts_aes256_set_encrypt_key, |
1098 | | .set_decrypt_key = |
1099 | | (nettle_set_key_func *)xts_aes256_set_decrypt_key, |
1100 | | .max_iv_size = AES_BLOCK_SIZE, |
1101 | | }, |
1102 | | { |
1103 | | .algo = GNUTLS_CIPHER_AES_128_SIV, |
1104 | | .block_size = SIV_BLOCK_SIZE, |
1105 | | .key_size = SIV_CMAC_AES128_KEY_SIZE, |
1106 | | |
1107 | | .ctx_size = sizeof(struct siv_cmac_aes128_ctx), |
1108 | | .aead_encrypt = |
1109 | | (aead_encrypt_func)_siv_cmac_aes128_encrypt_message, |
1110 | | .aead_decrypt = |
1111 | | (aead_decrypt_func)_siv_cmac_aes128_decrypt_message, |
1112 | | .set_encrypt_key = |
1113 | | (nettle_set_key_func *)siv_cmac_aes128_set_key, |
1114 | | .set_decrypt_key = |
1115 | | (nettle_set_key_func *)siv_cmac_aes128_set_key, |
1116 | | .max_iv_size = SIV_DIGEST_SIZE, |
1117 | | }, |
1118 | | { |
1119 | | .algo = GNUTLS_CIPHER_AES_256_SIV, |
1120 | | .block_size = SIV_BLOCK_SIZE, |
1121 | | .key_size = SIV_CMAC_AES256_KEY_SIZE, |
1122 | | |
1123 | | .ctx_size = sizeof(struct siv_cmac_aes256_ctx), |
1124 | | .aead_encrypt = |
1125 | | (aead_encrypt_func)_siv_cmac_aes256_encrypt_message, |
1126 | | .aead_decrypt = |
1127 | | (aead_decrypt_func)_siv_cmac_aes256_decrypt_message, |
1128 | | .set_encrypt_key = |
1129 | | (nettle_set_key_func *)siv_cmac_aes256_set_key, |
1130 | | .set_decrypt_key = |
1131 | | (nettle_set_key_func *)siv_cmac_aes256_set_key, |
1132 | | .max_iv_size = SIV_DIGEST_SIZE, |
1133 | | }, |
1134 | | { |
1135 | | .algo = GNUTLS_CIPHER_AES_128_SIV_GCM, |
1136 | | .block_size = SIV_GCM_BLOCK_SIZE, |
1137 | | .key_size = AES128_KEY_SIZE, |
1138 | | |
1139 | | .ctx_size = sizeof(struct aes128_ctx), |
1140 | | .aead_encrypt = |
1141 | | (aead_encrypt_func)_siv_gcm_aes128_encrypt_message, |
1142 | | .aead_decrypt = |
1143 | | (aead_decrypt_func)_siv_gcm_aes128_decrypt_message, |
1144 | | .set_encrypt_key = |
1145 | | (nettle_set_key_func *)aes128_set_encrypt_key, |
1146 | | .set_decrypt_key = |
1147 | | (nettle_set_key_func *)aes128_set_encrypt_key, |
1148 | | .max_iv_size = SIV_GCM_NONCE_SIZE, |
1149 | | }, |
1150 | | { |
1151 | | .algo = GNUTLS_CIPHER_AES_256_SIV_GCM, |
1152 | | .block_size = SIV_GCM_BLOCK_SIZE, |
1153 | | .key_size = AES256_KEY_SIZE, |
1154 | | |
1155 | | .ctx_size = sizeof(struct aes256_ctx), |
1156 | | .aead_encrypt = |
1157 | | (aead_encrypt_func)_siv_gcm_aes256_encrypt_message, |
1158 | | .aead_decrypt = |
1159 | | (aead_decrypt_func)_siv_gcm_aes256_decrypt_message, |
1160 | | .set_encrypt_key = |
1161 | | (nettle_set_key_func *)aes256_set_encrypt_key, |
1162 | | .set_decrypt_key = |
1163 | | (nettle_set_key_func *)aes256_set_encrypt_key, |
1164 | | .max_iv_size = SIV_GCM_NONCE_SIZE, |
1165 | | }, |
1166 | | }; |
1167 | | |
1168 | | static int wrap_nettle_cipher_exists(gnutls_cipher_algorithm_t algo) |
1169 | 0 | { |
1170 | 0 | unsigned i; |
1171 | 0 | for (i = 0; i < sizeof(builtin_ciphers) / sizeof(builtin_ciphers[0]); |
1172 | 0 | i++) { |
1173 | 0 | if (algo == builtin_ciphers[i].algo) { |
1174 | 0 | return 1; |
1175 | 0 | } |
1176 | 0 | } |
1177 | 0 | return 0; |
1178 | 0 | } |
1179 | | |
1180 | | static int wrap_nettle_cipher_init(gnutls_cipher_algorithm_t algo, void **_ctx, |
1181 | | int enc) |
1182 | 0 | { |
1183 | 0 | struct nettle_cipher_ctx *ctx; |
1184 | 0 | uintptr_t cur_alignment; |
1185 | 0 | int idx = -1; |
1186 | 0 | unsigned i; |
1187 | 0 | uint8_t *ctx_ptr; |
1188 | |
|
1189 | 0 | for (i = 0; i < sizeof(builtin_ciphers) / sizeof(builtin_ciphers[0]); |
1190 | 0 | i++) { |
1191 | 0 | if (algo == builtin_ciphers[i].algo) { |
1192 | 0 | idx = i; |
1193 | 0 | break; |
1194 | 0 | } |
1195 | 0 | } |
1196 | |
|
1197 | 0 | if (idx == -1) |
1198 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1199 | | |
1200 | 0 | ctx = gnutls_calloc(1, |
1201 | 0 | sizeof(*ctx) + builtin_ciphers[idx].ctx_size + 16); |
1202 | 0 | if (ctx == NULL) { |
1203 | 0 | gnutls_assert(); |
1204 | 0 | return GNUTLS_E_MEMORY_ERROR; |
1205 | 0 | } |
1206 | | |
1207 | 0 | ctx->enc = enc; |
1208 | 0 | ctx_ptr = ((uint8_t *)ctx) + sizeof(*ctx); |
1209 | |
|
1210 | 0 | cur_alignment = ((uintptr_t)ctx_ptr) % 16; |
1211 | 0 | if (cur_alignment > 0) |
1212 | 0 | ctx_ptr += 16 - cur_alignment; |
1213 | |
|
1214 | 0 | ctx->ctx_ptr = ctx_ptr; |
1215 | 0 | ctx->cipher = &builtin_ciphers[idx]; |
1216 | |
|
1217 | 0 | *_ctx = ctx; |
1218 | |
|
1219 | 0 | return 0; |
1220 | 0 | } |
1221 | | |
1222 | | static int wrap_nettle_cipher_setkey(void *_ctx, const void *key, |
1223 | | size_t keysize) |
1224 | 0 | { |
1225 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1226 | |
|
1227 | 0 | if (ctx->cipher->key_size > 0 && |
1228 | 0 | unlikely(keysize != ctx->cipher->key_size)) { |
1229 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1230 | 0 | } else if (ctx->cipher->key_size == 0) { |
1231 | 0 | ctx->cipher->gen_set_key(ctx->ctx_ptr, keysize, key); |
1232 | 0 | return 0; |
1233 | 0 | } |
1234 | | |
1235 | 0 | switch (ctx->cipher->algo) { |
1236 | 0 | case GNUTLS_CIPHER_AES_128_XTS: |
1237 | 0 | if (_gnutls_fips_mode_enabled() && |
1238 | 0 | gnutls_memcmp(key, (char *)key + AES128_KEY_SIZE, |
1239 | 0 | AES128_KEY_SIZE) == 0) |
1240 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1241 | 0 | break; |
1242 | 0 | case GNUTLS_CIPHER_AES_256_XTS: |
1243 | 0 | if (_gnutls_fips_mode_enabled() && |
1244 | 0 | gnutls_memcmp(key, (char *)key + AES256_KEY_SIZE, |
1245 | 0 | AES256_KEY_SIZE) == 0) |
1246 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1247 | 0 | break; |
1248 | 0 | default: |
1249 | 0 | break; |
1250 | 0 | } |
1251 | | |
1252 | 0 | if (ctx->enc) |
1253 | 0 | ctx->cipher->set_encrypt_key(ctx->ctx_ptr, key); |
1254 | 0 | else |
1255 | 0 | ctx->cipher->set_decrypt_key(ctx->ctx_ptr, key); |
1256 | |
|
1257 | 0 | switch (ctx->cipher->algo) { |
1258 | 0 | case GNUTLS_CIPHER_AES_128_GCM: |
1259 | 0 | case GNUTLS_CIPHER_AES_192_GCM: |
1260 | 0 | case GNUTLS_CIPHER_AES_256_GCM: |
1261 | 0 | ctx->rekey_counter = 0; |
1262 | 0 | break; |
1263 | 0 | default: |
1264 | 0 | break; |
1265 | 0 | } |
1266 | | |
1267 | 0 | return 0; |
1268 | 0 | } |
1269 | | |
1270 | | static int wrap_nettle_cipher_setiv(void *_ctx, const void *iv, size_t iv_size) |
1271 | 0 | { |
1272 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1273 | 0 | unsigned max_iv; |
1274 | |
|
1275 | 0 | switch (ctx->cipher->algo) { |
1276 | 0 | case GNUTLS_CIPHER_AES_128_GCM: |
1277 | 0 | case GNUTLS_CIPHER_AES_192_GCM: |
1278 | 0 | case GNUTLS_CIPHER_AES_256_GCM: |
1279 | 0 | FIPS_RULE(iv_size < GCM_IV_SIZE, GNUTLS_E_INVALID_REQUEST, |
1280 | 0 | "access to short GCM nonce size\n"); |
1281 | 0 | ctx->rekey_counter = 0; |
1282 | 0 | break; |
1283 | 0 | case GNUTLS_CIPHER_SALSA20_256: |
1284 | 0 | case GNUTLS_CIPHER_ESTREAM_SALSA20_256: |
1285 | 0 | if (iv_size != SALSA20_IV_SIZE) |
1286 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1287 | 0 | break; |
1288 | 0 | default: |
1289 | 0 | break; |
1290 | 0 | } |
1291 | | |
1292 | 0 | max_iv = ctx->cipher->max_iv_size; |
1293 | 0 | if (max_iv == 0) |
1294 | 0 | max_iv = MAX_CIPHER_BLOCK_SIZE; |
1295 | |
|
1296 | 0 | if (iv_size > max_iv) |
1297 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1298 | | |
1299 | 0 | if (ctx->cipher->set_iv) { |
1300 | 0 | ctx->cipher->set_iv(ctx->ctx_ptr, iv_size, iv); |
1301 | 0 | } else { |
1302 | 0 | if (iv) |
1303 | 0 | memcpy(ctx->iv, iv, iv_size); |
1304 | 0 | ctx->iv_size = iv_size; |
1305 | 0 | } |
1306 | |
|
1307 | 0 | return 0; |
1308 | 0 | } |
1309 | | |
1310 | | static int wrap_nettle_cipher_getiv(void *_ctx, void *iv, size_t iv_size) |
1311 | 0 | { |
1312 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1313 | |
|
1314 | 0 | if (iv_size < ctx->iv_size) |
1315 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1316 | | |
1317 | 0 | memcpy(iv, ctx->iv, ctx->iv_size); |
1318 | |
|
1319 | 0 | return (int)ctx->iv_size; |
1320 | 0 | } |
1321 | | |
1322 | | static int wrap_nettle_cipher_decrypt(void *_ctx, const void *encr, |
1323 | | size_t encr_size, void *plain, |
1324 | | size_t plain_size) |
1325 | 0 | { |
1326 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1327 | |
|
1328 | 0 | if (unlikely(ctx->cipher->decrypt == NULL)) |
1329 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1330 | | |
1331 | 0 | ctx->cipher->decrypt(ctx, encr_size, plain, encr); |
1332 | |
|
1333 | 0 | return 0; |
1334 | 0 | } |
1335 | | |
1336 | | static int wrap_nettle_cipher_encrypt(void *_ctx, const void *plain, |
1337 | | size_t plain_size, void *encr, |
1338 | | size_t encr_size) |
1339 | 0 | { |
1340 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1341 | 0 | int ret; |
1342 | |
|
1343 | 0 | if (unlikely(ctx->cipher->encrypt == NULL)) |
1344 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1345 | | |
1346 | 0 | switch (ctx->cipher->algo) { |
1347 | 0 | case GNUTLS_CIPHER_AES_128_GCM: |
1348 | 0 | case GNUTLS_CIPHER_AES_192_GCM: |
1349 | 0 | case GNUTLS_CIPHER_AES_256_GCM: |
1350 | 0 | ret = record_aes_gcm_encrypt_size(&ctx->rekey_counter, |
1351 | 0 | plain_size); |
1352 | 0 | if (ret < 0) { |
1353 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1354 | 0 | } |
1355 | 0 | break; |
1356 | 0 | default: |
1357 | 0 | break; |
1358 | 0 | } |
1359 | | |
1360 | 0 | ctx->cipher->encrypt(ctx, plain_size, encr, plain); |
1361 | |
|
1362 | 0 | return 0; |
1363 | 0 | } |
1364 | | |
1365 | | static int wrap_nettle_cipher_aead_encrypt(void *_ctx, const void *nonce, |
1366 | | size_t nonce_size, const void *auth, |
1367 | | size_t auth_size, size_t tag_size, |
1368 | | const void *plain, size_t plain_size, |
1369 | | void *encr, size_t encr_size) |
1370 | 0 | { |
1371 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1372 | |
|
1373 | 0 | if (ctx->cipher->aead_encrypt == NULL) { |
1374 | | /* proper AEAD cipher */ |
1375 | 0 | unsigned max_iv; |
1376 | |
|
1377 | 0 | if (encr_size < plain_size + tag_size) |
1378 | 0 | return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); |
1379 | | |
1380 | 0 | max_iv = ctx->cipher->max_iv_size; |
1381 | 0 | if (max_iv == 0) |
1382 | 0 | max_iv = MAX_CIPHER_BLOCK_SIZE; |
1383 | |
|
1384 | 0 | if (nonce_size > max_iv) |
1385 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1386 | | |
1387 | 0 | ctx->cipher->set_iv(ctx->ctx_ptr, nonce_size, nonce); |
1388 | 0 | ctx->cipher->auth(ctx->ctx_ptr, auth_size, auth); |
1389 | |
|
1390 | 0 | ctx->cipher->encrypt(ctx, plain_size, encr, plain); |
1391 | |
|
1392 | 0 | ctx->cipher->tag(ctx->ctx_ptr, tag_size, |
1393 | 0 | ((uint8_t *)encr) + plain_size); |
1394 | 0 | } else { |
1395 | | /* CCM-style cipher */ |
1396 | |
|
1397 | 0 | switch (ctx->cipher->algo) { |
1398 | 0 | case GNUTLS_CIPHER_AES_128_CCM: |
1399 | 0 | case GNUTLS_CIPHER_AES_256_CCM: |
1400 | | /* SP800-38C A.1 says Tlen must be a multiple of 16 |
1401 | | * between 32 and 128. |
1402 | | */ |
1403 | 0 | switch (tag_size) { |
1404 | 0 | case 4: |
1405 | 0 | case 6: |
1406 | | /* SP800-38C B.2 says Tlen smaller than 64 |
1407 | | * should not be used under sufficient |
1408 | | * restriction. We simply allow those for now. |
1409 | | */ |
1410 | 0 | FALLTHROUGH; |
1411 | 0 | case 8: |
1412 | 0 | case 10: |
1413 | 0 | case 12: |
1414 | 0 | case 14: |
1415 | 0 | case 16: |
1416 | 0 | break; |
1417 | 0 | default: |
1418 | 0 | if (_gnutls_fips_mode_enabled()) { |
1419 | 0 | _gnutls_switch_fips_state( |
1420 | 0 | GNUTLS_FIPS140_OP_ERROR); |
1421 | 0 | return gnutls_assert_val( |
1422 | 0 | GNUTLS_E_INVALID_REQUEST); |
1423 | 0 | } |
1424 | 0 | break; |
1425 | 0 | } |
1426 | 0 | break; |
1427 | 0 | default: |
1428 | 0 | break; |
1429 | 0 | } |
1430 | | |
1431 | 0 | ctx->cipher->aead_encrypt(ctx, nonce_size, nonce, auth_size, |
1432 | 0 | auth, tag_size, tag_size + plain_size, |
1433 | 0 | encr, plain); |
1434 | 0 | } |
1435 | 0 | return 0; |
1436 | 0 | } |
1437 | | |
1438 | | static int wrap_nettle_cipher_aead_decrypt(void *_ctx, const void *nonce, |
1439 | | size_t nonce_size, const void *auth, |
1440 | | size_t auth_size, size_t tag_size, |
1441 | | const void *encr, size_t encr_size, |
1442 | | void *plain, size_t plain_size) |
1443 | 0 | { |
1444 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1445 | 0 | int ret; |
1446 | |
|
1447 | 0 | if (unlikely(encr_size < tag_size)) |
1448 | 0 | return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED); |
1449 | | |
1450 | 0 | if (ctx->cipher->aead_decrypt == NULL) { |
1451 | | /* proper AEAD cipher */ |
1452 | 0 | uint8_t tag[MAX_HASH_SIZE]; |
1453 | 0 | unsigned max_iv; |
1454 | |
|
1455 | 0 | max_iv = ctx->cipher->max_iv_size; |
1456 | 0 | if (max_iv == 0) |
1457 | 0 | max_iv = MAX_CIPHER_BLOCK_SIZE; |
1458 | |
|
1459 | 0 | if (nonce_size > max_iv) |
1460 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1461 | | |
1462 | 0 | ctx->cipher->set_iv(ctx->ctx_ptr, nonce_size, nonce); |
1463 | 0 | ctx->cipher->auth(ctx->ctx_ptr, auth_size, auth); |
1464 | |
|
1465 | 0 | encr_size -= tag_size; |
1466 | |
|
1467 | 0 | if (unlikely(plain_size < encr_size)) |
1468 | 0 | return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); |
1469 | | |
1470 | 0 | ctx->cipher->decrypt(ctx, encr_size, plain, encr); |
1471 | |
|
1472 | 0 | ctx->cipher->tag(ctx->ctx_ptr, tag_size, tag); |
1473 | |
|
1474 | 0 | if (gnutls_memcmp(((uint8_t *)encr) + encr_size, tag, |
1475 | 0 | tag_size) != 0) |
1476 | 0 | return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED); |
1477 | 0 | } else { |
1478 | | /* CCM-style cipher */ |
1479 | 0 | encr_size -= tag_size; |
1480 | |
|
1481 | 0 | if (unlikely(plain_size < encr_size)) |
1482 | 0 | return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER); |
1483 | | |
1484 | 0 | switch (ctx->cipher->algo) { |
1485 | 0 | case GNUTLS_CIPHER_AES_128_CCM: |
1486 | 0 | case GNUTLS_CIPHER_AES_256_CCM: |
1487 | | /* SP800-38C A.1 says Tlen must be a multiple of 16 |
1488 | | * between 32 and 128. |
1489 | | */ |
1490 | 0 | switch (tag_size) { |
1491 | 0 | case 4: |
1492 | 0 | case 6: |
1493 | | /* SP800-38C B.2 says Tlen smaller than 64 |
1494 | | * should not be used under sufficient |
1495 | | * restriction. We simply allow those for now. |
1496 | | */ |
1497 | 0 | FALLTHROUGH; |
1498 | 0 | case 8: |
1499 | 0 | case 10: |
1500 | 0 | case 12: |
1501 | 0 | case 14: |
1502 | 0 | case 16: |
1503 | 0 | break; |
1504 | 0 | default: |
1505 | 0 | if (_gnutls_fips_mode_enabled()) { |
1506 | 0 | _gnutls_switch_fips_state( |
1507 | 0 | GNUTLS_FIPS140_OP_ERROR); |
1508 | 0 | return gnutls_assert_val( |
1509 | 0 | GNUTLS_E_INVALID_REQUEST); |
1510 | 0 | } |
1511 | 0 | break; |
1512 | 0 | } |
1513 | 0 | break; |
1514 | 0 | default: |
1515 | 0 | break; |
1516 | 0 | } |
1517 | | |
1518 | 0 | ret = ctx->cipher->aead_decrypt(ctx, nonce_size, nonce, |
1519 | 0 | auth_size, auth, tag_size, |
1520 | 0 | encr_size, plain, encr); |
1521 | 0 | if (unlikely(ret == 0)) |
1522 | 0 | return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED); |
1523 | 0 | } |
1524 | 0 | return 0; |
1525 | 0 | } |
1526 | | |
1527 | | static int wrap_nettle_cipher_auth(void *_ctx, const void *plain, |
1528 | | size_t plain_size) |
1529 | 0 | { |
1530 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1531 | |
|
1532 | 0 | ctx->cipher->auth(ctx->ctx_ptr, plain_size, plain); |
1533 | |
|
1534 | 0 | return 0; |
1535 | 0 | } |
1536 | | |
1537 | | static void wrap_nettle_cipher_tag(void *_ctx, void *tag, size_t tag_size) |
1538 | 0 | { |
1539 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1540 | |
|
1541 | 0 | ctx->cipher->tag(ctx->ctx_ptr, tag_size, tag); |
1542 | 0 | } |
1543 | | |
1544 | | static void wrap_nettle_cipher_close(void *_ctx) |
1545 | 0 | { |
1546 | 0 | struct nettle_cipher_ctx *ctx = _ctx; |
1547 | |
|
1548 | 0 | zeroize_temp_key(ctx->ctx_ptr, ctx->cipher->ctx_size); |
1549 | 0 | gnutls_free(ctx); |
1550 | 0 | } |
1551 | | |
1552 | | gnutls_crypto_cipher_st _gnutls_cipher_ops = { |
1553 | | .init = wrap_nettle_cipher_init, |
1554 | | .exists = wrap_nettle_cipher_exists, |
1555 | | .setiv = wrap_nettle_cipher_setiv, |
1556 | | .getiv = wrap_nettle_cipher_getiv, |
1557 | | .setkey = wrap_nettle_cipher_setkey, |
1558 | | .encrypt = wrap_nettle_cipher_encrypt, |
1559 | | .decrypt = wrap_nettle_cipher_decrypt, |
1560 | | .aead_encrypt = wrap_nettle_cipher_aead_encrypt, |
1561 | | .aead_decrypt = wrap_nettle_cipher_aead_decrypt, |
1562 | | .deinit = wrap_nettle_cipher_close, |
1563 | | .auth = wrap_nettle_cipher_auth, |
1564 | | .tag = wrap_nettle_cipher_tag, |
1565 | | }; |