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