1 /**
2 * \file cipher_wrap.c
3 *
4 * \brief Generic cipher wrapper for Mbed TLS
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 */
11
12 #include "common.h"
13
14 #if defined(MBEDTLS_CIPHER_C)
15
16 #include "cipher_wrap.h"
17 #include "mbedtls/error.h"
18
19 #if defined(MBEDTLS_CHACHAPOLY_C)
20 #include "mbedtls/chachapoly.h"
21 #endif
22
23 #if defined(MBEDTLS_AES_C)
24 #include "mbedtls/aes.h"
25 #endif
26
27 #if defined(MBEDTLS_CAMELLIA_C)
28 #include "mbedtls/camellia.h"
29 #endif
30
31 #if defined(MBEDTLS_ARIA_C)
32 #include "mbedtls/aria.h"
33 #endif
34
35 #if defined(MBEDTLS_DES_C)
36 #include "mbedtls/des.h"
37 #endif
38
39 #if defined(MBEDTLS_CHACHA20_C)
40 #include "mbedtls/chacha20.h"
41 #endif
42
43 #if defined(MBEDTLS_GCM_C)
44 #include "mbedtls/gcm.h"
45 #endif
46
47 #if defined(MBEDTLS_CCM_C)
48 #include "mbedtls/ccm.h"
49 #endif
50
51 #if defined(MBEDTLS_NIST_KW_C)
52 #include "mbedtls/nist_kw.h"
53 #endif
54
55 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
56 #include <string.h>
57 #endif
58
59 #include "mbedtls/platform.h"
60
61 enum mbedtls_cipher_base_index {
62 #if defined(MBEDTLS_AES_C)
63 MBEDTLS_CIPHER_BASE_INDEX_AES,
64 #endif
65 #if defined(MBEDTLS_ARIA_C)
66 MBEDTLS_CIPHER_BASE_INDEX_ARIA,
67 #endif
68 #if defined(MBEDTLS_CAMELLIA_C)
69 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA,
70 #endif
71 #if defined(MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA)
72 MBEDTLS_CIPHER_BASE_INDEX_CCM_AES,
73 #endif
74 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_ARIA_C)
75 MBEDTLS_CIPHER_BASE_INDEX_CCM_ARIA,
76 #endif
77 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_CAMELLIA_C)
78 MBEDTLS_CIPHER_BASE_INDEX_CCM_CAMELLIA,
79 #endif
80 #if defined(MBEDTLS_CHACHA20_C)
81 MBEDTLS_CIPHER_BASE_INDEX_CHACHA20_BASE,
82 #endif
83 #if defined(MBEDTLS_CHACHAPOLY_C)
84 MBEDTLS_CIPHER_BASE_INDEX_CHACHAPOLY_BASE,
85 #endif
86 #if defined(MBEDTLS_DES_C)
87 MBEDTLS_CIPHER_BASE_INDEX_DES_EDE3,
88 #endif
89 #if defined(MBEDTLS_DES_C)
90 MBEDTLS_CIPHER_BASE_INDEX_DES_EDE,
91 #endif
92 #if defined(MBEDTLS_DES_C)
93 MBEDTLS_CIPHER_BASE_INDEX_DES,
94 #endif
95 #if defined(MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA)
96 MBEDTLS_CIPHER_BASE_INDEX_GCM_AES,
97 #endif
98 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_ARIA_C)
99 MBEDTLS_CIPHER_BASE_INDEX_GCM_ARIA,
100 #endif
101 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_CAMELLIA_C)
102 MBEDTLS_CIPHER_BASE_INDEX_GCM_CAMELLIA,
103 #endif
104 #if defined(MBEDTLS_NIST_KW_C)
105 MBEDTLS_CIPHER_BASE_INDEX_KW_AES,
106 #endif
107 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
108 MBEDTLS_CIPHER_BASE_INDEX_NULL_BASE,
109 #endif
110 #if defined(MBEDTLS_CIPHER_MODE_XTS) && defined(MBEDTLS_AES_C)
111 MBEDTLS_CIPHER_BASE_INDEX_XTS_AES,
112 #endif
113 /* Prevent compile failure due to empty enum */
114 MBEDTLS_CIPHER_BASE_PREVENT_EMPTY_ENUM
115 };
116
117 #if defined(MBEDTLS_GCM_C) && \
118 (defined(MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA) || \
119 defined(MBEDTLS_ARIA_C) || defined(MBEDTLS_CAMELLIA_C))
120 /* shared by all GCM ciphers */
gcm_ctx_alloc(void)121 static void *gcm_ctx_alloc(void)
122 {
123 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_gcm_context));
124
125 if (ctx != NULL) {
126 mbedtls_gcm_init((mbedtls_gcm_context *) ctx);
127 }
128
129 return ctx;
130 }
131
gcm_ctx_free(void * ctx)132 static void gcm_ctx_free(void *ctx)
133 {
134 mbedtls_gcm_free(ctx);
135 mbedtls_free(ctx);
136 }
137 #endif /* MBEDTLS_GCM_C */
138
139 #if defined(MBEDTLS_CCM_C) && \
140 (defined(MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA) || \
141 defined(MBEDTLS_ARIA_C) || defined(MBEDTLS_CAMELLIA_C))
142 /* shared by all CCM ciphers */
ccm_ctx_alloc(void)143 static void *ccm_ctx_alloc(void)
144 {
145 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ccm_context));
146
147 if (ctx != NULL) {
148 mbedtls_ccm_init((mbedtls_ccm_context *) ctx);
149 }
150
151 return ctx;
152 }
153
ccm_ctx_free(void * ctx)154 static void ccm_ctx_free(void *ctx)
155 {
156 mbedtls_ccm_free(ctx);
157 mbedtls_free(ctx);
158 }
159 #endif /* MBEDTLS_CCM_C */
160
161 #if defined(MBEDTLS_AES_C)
162
aes_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)163 static int aes_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
164 const unsigned char *input, unsigned char *output)
165 {
166 return mbedtls_aes_crypt_ecb((mbedtls_aes_context *) ctx, operation, input, output);
167 }
168
169 #if defined(MBEDTLS_CIPHER_MODE_CBC)
aes_crypt_cbc_wrap(void * ctx,mbedtls_operation_t operation,size_t length,unsigned char * iv,const unsigned char * input,unsigned char * output)170 static int aes_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
171 unsigned char *iv, const unsigned char *input, unsigned char *output)
172 {
173 return mbedtls_aes_crypt_cbc((mbedtls_aes_context *) ctx, operation, length, iv, input,
174 output);
175 }
176 #endif /* MBEDTLS_CIPHER_MODE_CBC */
177
178 #if defined(MBEDTLS_CIPHER_MODE_CFB)
aes_crypt_cfb128_wrap(void * ctx,mbedtls_operation_t operation,size_t length,size_t * iv_off,unsigned char * iv,const unsigned char * input,unsigned char * output)179 static int aes_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
180 size_t length, size_t *iv_off, unsigned char *iv,
181 const unsigned char *input, unsigned char *output)
182 {
183 return mbedtls_aes_crypt_cfb128((mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
184 input, output);
185 }
186 #endif /* MBEDTLS_CIPHER_MODE_CFB */
187
188 #if defined(MBEDTLS_CIPHER_MODE_OFB)
aes_crypt_ofb_wrap(void * ctx,size_t length,size_t * iv_off,unsigned char * iv,const unsigned char * input,unsigned char * output)189 static int aes_crypt_ofb_wrap(void *ctx, size_t length, size_t *iv_off,
190 unsigned char *iv, const unsigned char *input, unsigned char *output)
191 {
192 return mbedtls_aes_crypt_ofb((mbedtls_aes_context *) ctx, length, iv_off,
193 iv, input, output);
194 }
195 #endif /* MBEDTLS_CIPHER_MODE_OFB */
196
197 #if defined(MBEDTLS_CIPHER_MODE_CTR)
aes_crypt_ctr_wrap(void * ctx,size_t length,size_t * nc_off,unsigned char * nonce_counter,unsigned char * stream_block,const unsigned char * input,unsigned char * output)198 static int aes_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
199 unsigned char *nonce_counter, unsigned char *stream_block,
200 const unsigned char *input, unsigned char *output)
201 {
202 return mbedtls_aes_crypt_ctr((mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
203 stream_block, input, output);
204 }
205 #endif /* MBEDTLS_CIPHER_MODE_CTR */
206
207 #if defined(MBEDTLS_CIPHER_MODE_XTS)
aes_crypt_xts_wrap(void * ctx,mbedtls_operation_t operation,size_t length,const unsigned char data_unit[16],const unsigned char * input,unsigned char * output)208 static int aes_crypt_xts_wrap(void *ctx, mbedtls_operation_t operation,
209 size_t length,
210 const unsigned char data_unit[16],
211 const unsigned char *input,
212 unsigned char *output)
213 {
214 mbedtls_aes_xts_context *xts_ctx = ctx;
215 int mode;
216
217 switch (operation) {
218 case MBEDTLS_ENCRYPT:
219 mode = MBEDTLS_AES_ENCRYPT;
220 break;
221 case MBEDTLS_DECRYPT:
222 mode = MBEDTLS_AES_DECRYPT;
223 break;
224 default:
225 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
226 }
227
228 return mbedtls_aes_crypt_xts(xts_ctx, mode, length,
229 data_unit, input, output);
230 }
231 #endif /* MBEDTLS_CIPHER_MODE_XTS */
232
233 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
aes_setkey_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)234 static int aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
235 unsigned int key_bitlen)
236 {
237 return mbedtls_aes_setkey_dec((mbedtls_aes_context *) ctx, key, key_bitlen);
238 }
239 #endif
240
aes_setkey_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)241 static int aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
242 unsigned int key_bitlen)
243 {
244 return mbedtls_aes_setkey_enc((mbedtls_aes_context *) ctx, key, key_bitlen);
245 }
246
aes_ctx_alloc(void)247 static void *aes_ctx_alloc(void)
248 {
249 mbedtls_aes_context *aes = mbedtls_calloc(1, sizeof(mbedtls_aes_context));
250
251 if (aes == NULL) {
252 return NULL;
253 }
254
255 mbedtls_aes_init(aes);
256
257 return aes;
258 }
259
aes_ctx_free(void * ctx)260 static void aes_ctx_free(void *ctx)
261 {
262 mbedtls_aes_free((mbedtls_aes_context *) ctx);
263 mbedtls_free(ctx);
264 }
265
266 static const mbedtls_cipher_base_t aes_info = {
267 MBEDTLS_CIPHER_ID_AES,
268 aes_crypt_ecb_wrap,
269 #if defined(MBEDTLS_CIPHER_MODE_CBC)
270 aes_crypt_cbc_wrap,
271 #endif
272 #if defined(MBEDTLS_CIPHER_MODE_CFB)
273 aes_crypt_cfb128_wrap,
274 #endif
275 #if defined(MBEDTLS_CIPHER_MODE_OFB)
276 aes_crypt_ofb_wrap,
277 #endif
278 #if defined(MBEDTLS_CIPHER_MODE_CTR)
279 aes_crypt_ctr_wrap,
280 #endif
281 #if defined(MBEDTLS_CIPHER_MODE_XTS)
282 NULL,
283 #endif
284 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
285 NULL,
286 #endif
287 aes_setkey_enc_wrap,
288 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
289 aes_setkey_dec_wrap,
290 #endif
291 aes_ctx_alloc,
292 aes_ctx_free
293 };
294
295 static const mbedtls_cipher_info_t aes_128_ecb_info = {
296 "AES-128-ECB",
297 16,
298 0 >> MBEDTLS_IV_SIZE_SHIFT,
299 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
300 MBEDTLS_MODE_ECB,
301 MBEDTLS_CIPHER_AES_128_ECB,
302 0,
303 MBEDTLS_CIPHER_BASE_INDEX_AES
304 };
305
306 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
307 static const mbedtls_cipher_info_t aes_192_ecb_info = {
308 "AES-192-ECB",
309 16,
310 0 >> MBEDTLS_IV_SIZE_SHIFT,
311 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
312 MBEDTLS_MODE_ECB,
313 MBEDTLS_CIPHER_AES_192_ECB,
314 0,
315 MBEDTLS_CIPHER_BASE_INDEX_AES
316 };
317
318 static const mbedtls_cipher_info_t aes_256_ecb_info = {
319 "AES-256-ECB",
320 16,
321 0 >> MBEDTLS_IV_SIZE_SHIFT,
322 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
323 MBEDTLS_MODE_ECB,
324 MBEDTLS_CIPHER_AES_256_ECB,
325 0,
326 MBEDTLS_CIPHER_BASE_INDEX_AES
327 };
328 #endif
329
330 #if defined(MBEDTLS_CIPHER_MODE_CBC)
331 static const mbedtls_cipher_info_t aes_128_cbc_info = {
332 "AES-128-CBC",
333 16,
334 16 >> MBEDTLS_IV_SIZE_SHIFT,
335 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
336 MBEDTLS_MODE_CBC,
337 MBEDTLS_CIPHER_AES_128_CBC,
338 0,
339 MBEDTLS_CIPHER_BASE_INDEX_AES
340 };
341
342 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
343 static const mbedtls_cipher_info_t aes_192_cbc_info = {
344 "AES-192-CBC",
345 16,
346 16 >> MBEDTLS_IV_SIZE_SHIFT,
347 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
348 MBEDTLS_MODE_CBC,
349 MBEDTLS_CIPHER_AES_192_CBC,
350 0,
351 MBEDTLS_CIPHER_BASE_INDEX_AES
352 };
353
354 static const mbedtls_cipher_info_t aes_256_cbc_info = {
355 "AES-256-CBC",
356 16,
357 16 >> MBEDTLS_IV_SIZE_SHIFT,
358 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
359 MBEDTLS_MODE_CBC,
360 MBEDTLS_CIPHER_AES_256_CBC,
361 0,
362 MBEDTLS_CIPHER_BASE_INDEX_AES
363 };
364 #endif
365 #endif /* MBEDTLS_CIPHER_MODE_CBC */
366
367 #if defined(MBEDTLS_CIPHER_MODE_CFB)
368 static const mbedtls_cipher_info_t aes_128_cfb128_info = {
369 "AES-128-CFB128",
370 16,
371 16 >> MBEDTLS_IV_SIZE_SHIFT,
372 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
373 MBEDTLS_MODE_CFB,
374 MBEDTLS_CIPHER_AES_128_CFB128,
375 0,
376 MBEDTLS_CIPHER_BASE_INDEX_AES
377 };
378
379 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
380 static const mbedtls_cipher_info_t aes_192_cfb128_info = {
381 "AES-192-CFB128",
382 16,
383 16 >> MBEDTLS_IV_SIZE_SHIFT,
384 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
385 MBEDTLS_MODE_CFB,
386 MBEDTLS_CIPHER_AES_192_CFB128,
387 0,
388 MBEDTLS_CIPHER_BASE_INDEX_AES
389 };
390
391 static const mbedtls_cipher_info_t aes_256_cfb128_info = {
392 "AES-256-CFB128",
393 16,
394 16 >> MBEDTLS_IV_SIZE_SHIFT,
395 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
396 MBEDTLS_MODE_CFB,
397 MBEDTLS_CIPHER_AES_256_CFB128,
398 0,
399 MBEDTLS_CIPHER_BASE_INDEX_AES
400 };
401 #endif
402 #endif /* MBEDTLS_CIPHER_MODE_CFB */
403
404 #if defined(MBEDTLS_CIPHER_MODE_OFB)
405 static const mbedtls_cipher_info_t aes_128_ofb_info = {
406 "AES-128-OFB",
407 16,
408 16 >> MBEDTLS_IV_SIZE_SHIFT,
409 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
410 MBEDTLS_MODE_OFB,
411 MBEDTLS_CIPHER_AES_128_OFB,
412 0,
413 MBEDTLS_CIPHER_BASE_INDEX_AES
414 };
415
416 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
417 static const mbedtls_cipher_info_t aes_192_ofb_info = {
418 "AES-192-OFB",
419 16,
420 16 >> MBEDTLS_IV_SIZE_SHIFT,
421 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
422 MBEDTLS_MODE_OFB,
423 MBEDTLS_CIPHER_AES_192_OFB,
424 0,
425 MBEDTLS_CIPHER_BASE_INDEX_AES
426 };
427
428 static const mbedtls_cipher_info_t aes_256_ofb_info = {
429 "AES-256-OFB",
430 16,
431 16 >> MBEDTLS_IV_SIZE_SHIFT,
432 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
433 MBEDTLS_MODE_OFB,
434 MBEDTLS_CIPHER_AES_256_OFB,
435 0,
436 MBEDTLS_CIPHER_BASE_INDEX_AES
437 };
438 #endif
439 #endif /* MBEDTLS_CIPHER_MODE_OFB */
440
441 #if defined(MBEDTLS_CIPHER_MODE_CTR)
442 static const mbedtls_cipher_info_t aes_128_ctr_info = {
443 "AES-128-CTR",
444 16,
445 16 >> MBEDTLS_IV_SIZE_SHIFT,
446 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
447 MBEDTLS_MODE_CTR,
448 MBEDTLS_CIPHER_AES_128_CTR,
449 0,
450 MBEDTLS_CIPHER_BASE_INDEX_AES
451 };
452
453 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
454 static const mbedtls_cipher_info_t aes_192_ctr_info = {
455 "AES-192-CTR",
456 16,
457 16 >> MBEDTLS_IV_SIZE_SHIFT,
458 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
459 MBEDTLS_MODE_CTR,
460 MBEDTLS_CIPHER_AES_192_CTR,
461 0,
462 MBEDTLS_CIPHER_BASE_INDEX_AES
463 };
464
465 static const mbedtls_cipher_info_t aes_256_ctr_info = {
466 "AES-256-CTR",
467 16,
468 16 >> MBEDTLS_IV_SIZE_SHIFT,
469 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
470 MBEDTLS_MODE_CTR,
471 MBEDTLS_CIPHER_AES_256_CTR,
472 0,
473 MBEDTLS_CIPHER_BASE_INDEX_AES
474 };
475 #endif
476 #endif /* MBEDTLS_CIPHER_MODE_CTR */
477
478 #if defined(MBEDTLS_CIPHER_MODE_XTS)
xts_aes_setkey_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)479 static int xts_aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
480 unsigned int key_bitlen)
481 {
482 mbedtls_aes_xts_context *xts_ctx = ctx;
483 return mbedtls_aes_xts_setkey_enc(xts_ctx, key, key_bitlen);
484 }
485
xts_aes_setkey_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)486 static int xts_aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
487 unsigned int key_bitlen)
488 {
489 mbedtls_aes_xts_context *xts_ctx = ctx;
490 return mbedtls_aes_xts_setkey_dec(xts_ctx, key, key_bitlen);
491 }
492
xts_aes_ctx_alloc(void)493 static void *xts_aes_ctx_alloc(void)
494 {
495 mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc(1, sizeof(*xts_ctx));
496
497 if (xts_ctx != NULL) {
498 mbedtls_aes_xts_init(xts_ctx);
499 }
500
501 return xts_ctx;
502 }
503
xts_aes_ctx_free(void * ctx)504 static void xts_aes_ctx_free(void *ctx)
505 {
506 mbedtls_aes_xts_context *xts_ctx = ctx;
507
508 if (xts_ctx == NULL) {
509 return;
510 }
511
512 mbedtls_aes_xts_free(xts_ctx);
513 mbedtls_free(xts_ctx);
514 }
515
516 static const mbedtls_cipher_base_t xts_aes_info = {
517 MBEDTLS_CIPHER_ID_AES,
518 NULL,
519 #if defined(MBEDTLS_CIPHER_MODE_CBC)
520 NULL,
521 #endif
522 #if defined(MBEDTLS_CIPHER_MODE_CFB)
523 NULL,
524 #endif
525 #if defined(MBEDTLS_CIPHER_MODE_OFB)
526 NULL,
527 #endif
528 #if defined(MBEDTLS_CIPHER_MODE_CTR)
529 NULL,
530 #endif
531 #if defined(MBEDTLS_CIPHER_MODE_XTS)
532 aes_crypt_xts_wrap,
533 #endif
534 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
535 NULL,
536 #endif
537 xts_aes_setkey_enc_wrap,
538 xts_aes_setkey_dec_wrap,
539 xts_aes_ctx_alloc,
540 xts_aes_ctx_free
541 };
542
543 static const mbedtls_cipher_info_t aes_128_xts_info = {
544 "AES-128-XTS",
545 16,
546 16 >> MBEDTLS_IV_SIZE_SHIFT,
547 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
548 MBEDTLS_MODE_XTS,
549 MBEDTLS_CIPHER_AES_128_XTS,
550 0,
551 MBEDTLS_CIPHER_BASE_INDEX_XTS_AES
552 };
553
554 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
555 static const mbedtls_cipher_info_t aes_256_xts_info = {
556 "AES-256-XTS",
557 16,
558 16 >> MBEDTLS_IV_SIZE_SHIFT,
559 512 >> MBEDTLS_KEY_BITLEN_SHIFT,
560 MBEDTLS_MODE_XTS,
561 MBEDTLS_CIPHER_AES_256_XTS,
562 0,
563 MBEDTLS_CIPHER_BASE_INDEX_XTS_AES
564 };
565 #endif
566 #endif /* MBEDTLS_CIPHER_MODE_XTS */
567 #endif /* MBEDTLS_AES_C */
568
569 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_CCM_GCM_CAN_AES)
gcm_aes_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)570 static int gcm_aes_setkey_wrap(void *ctx, const unsigned char *key,
571 unsigned int key_bitlen)
572 {
573 return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
574 key, key_bitlen);
575 }
576 #endif /* MBEDTLS_GCM_C && MBEDTLS_CCM_GCM_CAN_AES */
577
578 #if defined(MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA)
579 static const mbedtls_cipher_base_t gcm_aes_info = {
580 MBEDTLS_CIPHER_ID_AES,
581 NULL,
582 #if defined(MBEDTLS_CIPHER_MODE_CBC)
583 NULL,
584 #endif
585 #if defined(MBEDTLS_CIPHER_MODE_CFB)
586 NULL,
587 #endif
588 #if defined(MBEDTLS_CIPHER_MODE_OFB)
589 NULL,
590 #endif
591 #if defined(MBEDTLS_CIPHER_MODE_CTR)
592 NULL,
593 #endif
594 #if defined(MBEDTLS_CIPHER_MODE_XTS)
595 NULL,
596 #endif
597 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
598 NULL,
599 #endif
600 #if defined(MBEDTLS_GCM_C)
601 gcm_aes_setkey_wrap,
602 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
603 gcm_aes_setkey_wrap,
604 #endif
605 gcm_ctx_alloc,
606 gcm_ctx_free,
607 #else
608 NULL,
609 NULL,
610 NULL,
611 NULL,
612 #endif /* MBEDTLS_GCM_C */
613 };
614 #endif /* MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA */
615
616 #if defined(MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA)
617 static const mbedtls_cipher_info_t aes_128_gcm_info = {
618 "AES-128-GCM",
619 16,
620 12 >> MBEDTLS_IV_SIZE_SHIFT,
621 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
622 MBEDTLS_MODE_GCM,
623 MBEDTLS_CIPHER_AES_128_GCM,
624 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
625 MBEDTLS_CIPHER_BASE_INDEX_GCM_AES
626 };
627
628 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
629 static const mbedtls_cipher_info_t aes_192_gcm_info = {
630 "AES-192-GCM",
631 16,
632 12 >> MBEDTLS_IV_SIZE_SHIFT,
633 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
634 MBEDTLS_MODE_GCM,
635 MBEDTLS_CIPHER_AES_192_GCM,
636 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
637 MBEDTLS_CIPHER_BASE_INDEX_GCM_AES
638 };
639
640 static const mbedtls_cipher_info_t aes_256_gcm_info = {
641 "AES-256-GCM",
642 16,
643 12 >> MBEDTLS_IV_SIZE_SHIFT,
644 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
645 MBEDTLS_MODE_GCM,
646 MBEDTLS_CIPHER_AES_256_GCM,
647 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
648 MBEDTLS_CIPHER_BASE_INDEX_GCM_AES
649 };
650 #endif
651 #endif /* MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA */
652
653 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_CCM_GCM_CAN_AES)
ccm_aes_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)654 static int ccm_aes_setkey_wrap(void *ctx, const unsigned char *key,
655 unsigned int key_bitlen)
656 {
657 return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
658 key, key_bitlen);
659 }
660 #endif /* MBEDTLS_CCM_C && MBEDTLS_CCM_GCM_CAN_AES */
661
662 #if defined(MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA)
663 static const mbedtls_cipher_base_t ccm_aes_info = {
664 MBEDTLS_CIPHER_ID_AES,
665 NULL,
666 #if defined(MBEDTLS_CIPHER_MODE_CBC)
667 NULL,
668 #endif
669 #if defined(MBEDTLS_CIPHER_MODE_CFB)
670 NULL,
671 #endif
672 #if defined(MBEDTLS_CIPHER_MODE_OFB)
673 NULL,
674 #endif
675 #if defined(MBEDTLS_CIPHER_MODE_CTR)
676 NULL,
677 #endif
678 #if defined(MBEDTLS_CIPHER_MODE_XTS)
679 NULL,
680 #endif
681 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
682 NULL,
683 #endif
684 #if defined(MBEDTLS_CCM_C)
685 ccm_aes_setkey_wrap,
686 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
687 ccm_aes_setkey_wrap,
688 #endif
689 ccm_ctx_alloc,
690 ccm_ctx_free,
691 #else
692 NULL,
693 NULL,
694 NULL,
695 NULL,
696 #endif
697 };
698 #endif /* MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA */
699
700 #if defined(MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA)
701 static const mbedtls_cipher_info_t aes_128_ccm_info = {
702 "AES-128-CCM",
703 16,
704 12 >> MBEDTLS_IV_SIZE_SHIFT,
705 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
706 MBEDTLS_MODE_CCM,
707 MBEDTLS_CIPHER_AES_128_CCM,
708 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
709 MBEDTLS_CIPHER_BASE_INDEX_CCM_AES
710 };
711
712 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
713 static const mbedtls_cipher_info_t aes_192_ccm_info = {
714 "AES-192-CCM",
715 16,
716 12 >> MBEDTLS_IV_SIZE_SHIFT,
717 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
718 MBEDTLS_MODE_CCM,
719 MBEDTLS_CIPHER_AES_192_CCM,
720 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
721 MBEDTLS_CIPHER_BASE_INDEX_CCM_AES
722 };
723
724 static const mbedtls_cipher_info_t aes_256_ccm_info = {
725 "AES-256-CCM",
726 16,
727 12 >> MBEDTLS_IV_SIZE_SHIFT,
728 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
729 MBEDTLS_MODE_CCM,
730 MBEDTLS_CIPHER_AES_256_CCM,
731 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
732 MBEDTLS_CIPHER_BASE_INDEX_CCM_AES
733 };
734 #endif
735 #endif /* MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA */
736
737 #if defined(MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_AES_VIA_LEGACY_OR_USE_PSA)
738 static const mbedtls_cipher_info_t aes_128_ccm_star_no_tag_info = {
739 "AES-128-CCM*-NO-TAG",
740 16,
741 12 >> MBEDTLS_IV_SIZE_SHIFT,
742 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
743 MBEDTLS_MODE_CCM_STAR_NO_TAG,
744 MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG,
745 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
746 MBEDTLS_CIPHER_BASE_INDEX_CCM_AES
747 };
748
749 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
750 static const mbedtls_cipher_info_t aes_192_ccm_star_no_tag_info = {
751 "AES-192-CCM*-NO-TAG",
752 16,
753 12 >> MBEDTLS_IV_SIZE_SHIFT,
754 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
755 MBEDTLS_MODE_CCM_STAR_NO_TAG,
756 MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG,
757 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
758 MBEDTLS_CIPHER_BASE_INDEX_CCM_AES
759 };
760
761 static const mbedtls_cipher_info_t aes_256_ccm_star_no_tag_info = {
762 "AES-256-CCM*-NO-TAG",
763 16,
764 12 >> MBEDTLS_IV_SIZE_SHIFT,
765 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
766 MBEDTLS_MODE_CCM_STAR_NO_TAG,
767 MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG,
768 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
769 MBEDTLS_CIPHER_BASE_INDEX_CCM_AES
770 };
771 #endif
772 #endif /* MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_AES_VIA_LEGACY_OR_USE_PSA */
773
774
775 #if defined(MBEDTLS_CAMELLIA_C)
776
camellia_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)777 static int camellia_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
778 const unsigned char *input, unsigned char *output)
779 {
780 return mbedtls_camellia_crypt_ecb((mbedtls_camellia_context *) ctx, operation, input,
781 output);
782 }
783
784 #if defined(MBEDTLS_CIPHER_MODE_CBC)
camellia_crypt_cbc_wrap(void * ctx,mbedtls_operation_t operation,size_t length,unsigned char * iv,const unsigned char * input,unsigned char * output)785 static int camellia_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
786 size_t length, unsigned char *iv,
787 const unsigned char *input, unsigned char *output)
788 {
789 return mbedtls_camellia_crypt_cbc((mbedtls_camellia_context *) ctx, operation, length, iv,
790 input, output);
791 }
792 #endif /* MBEDTLS_CIPHER_MODE_CBC */
793
794 #if defined(MBEDTLS_CIPHER_MODE_CFB)
camellia_crypt_cfb128_wrap(void * ctx,mbedtls_operation_t operation,size_t length,size_t * iv_off,unsigned char * iv,const unsigned char * input,unsigned char * output)795 static int camellia_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
796 size_t length, size_t *iv_off, unsigned char *iv,
797 const unsigned char *input, unsigned char *output)
798 {
799 return mbedtls_camellia_crypt_cfb128((mbedtls_camellia_context *) ctx, operation, length,
800 iv_off, iv, input, output);
801 }
802 #endif /* MBEDTLS_CIPHER_MODE_CFB */
803
804 #if defined(MBEDTLS_CIPHER_MODE_CTR)
camellia_crypt_ctr_wrap(void * ctx,size_t length,size_t * nc_off,unsigned char * nonce_counter,unsigned char * stream_block,const unsigned char * input,unsigned char * output)805 static int camellia_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
806 unsigned char *nonce_counter, unsigned char *stream_block,
807 const unsigned char *input, unsigned char *output)
808 {
809 return mbedtls_camellia_crypt_ctr((mbedtls_camellia_context *) ctx, length, nc_off,
810 nonce_counter, stream_block, input, output);
811 }
812 #endif /* MBEDTLS_CIPHER_MODE_CTR */
813
814 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
camellia_setkey_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)815 static int camellia_setkey_dec_wrap(void *ctx, const unsigned char *key,
816 unsigned int key_bitlen)
817 {
818 return mbedtls_camellia_setkey_dec((mbedtls_camellia_context *) ctx, key, key_bitlen);
819 }
820 #endif
821
camellia_setkey_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)822 static int camellia_setkey_enc_wrap(void *ctx, const unsigned char *key,
823 unsigned int key_bitlen)
824 {
825 return mbedtls_camellia_setkey_enc((mbedtls_camellia_context *) ctx, key, key_bitlen);
826 }
827
camellia_ctx_alloc(void)828 static void *camellia_ctx_alloc(void)
829 {
830 mbedtls_camellia_context *ctx;
831 ctx = mbedtls_calloc(1, sizeof(mbedtls_camellia_context));
832
833 if (ctx == NULL) {
834 return NULL;
835 }
836
837 mbedtls_camellia_init(ctx);
838
839 return ctx;
840 }
841
camellia_ctx_free(void * ctx)842 static void camellia_ctx_free(void *ctx)
843 {
844 mbedtls_camellia_free((mbedtls_camellia_context *) ctx);
845 mbedtls_free(ctx);
846 }
847
848 static const mbedtls_cipher_base_t camellia_info = {
849 MBEDTLS_CIPHER_ID_CAMELLIA,
850 camellia_crypt_ecb_wrap,
851 #if defined(MBEDTLS_CIPHER_MODE_CBC)
852 camellia_crypt_cbc_wrap,
853 #endif
854 #if defined(MBEDTLS_CIPHER_MODE_CFB)
855 camellia_crypt_cfb128_wrap,
856 #endif
857 #if defined(MBEDTLS_CIPHER_MODE_OFB)
858 NULL,
859 #endif
860 #if defined(MBEDTLS_CIPHER_MODE_CTR)
861 camellia_crypt_ctr_wrap,
862 #endif
863 #if defined(MBEDTLS_CIPHER_MODE_XTS)
864 NULL,
865 #endif
866 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
867 NULL,
868 #endif
869 camellia_setkey_enc_wrap,
870 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
871 camellia_setkey_dec_wrap,
872 #endif
873 camellia_ctx_alloc,
874 camellia_ctx_free
875 };
876
877 static const mbedtls_cipher_info_t camellia_128_ecb_info = {
878 "CAMELLIA-128-ECB",
879 16,
880 0 >> MBEDTLS_IV_SIZE_SHIFT,
881 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
882 MBEDTLS_MODE_ECB,
883 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
884 0,
885 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
886 };
887
888 static const mbedtls_cipher_info_t camellia_192_ecb_info = {
889 "CAMELLIA-192-ECB",
890 16,
891 0 >> MBEDTLS_IV_SIZE_SHIFT,
892 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
893 MBEDTLS_MODE_ECB,
894 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
895 0,
896 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
897 };
898
899 static const mbedtls_cipher_info_t camellia_256_ecb_info = {
900 "CAMELLIA-256-ECB",
901 16,
902 0 >> MBEDTLS_IV_SIZE_SHIFT,
903 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
904 MBEDTLS_MODE_ECB,
905 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
906 0,
907 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
908 };
909
910 #if defined(MBEDTLS_CIPHER_MODE_CBC)
911 static const mbedtls_cipher_info_t camellia_128_cbc_info = {
912 "CAMELLIA-128-CBC",
913 16,
914 16 >> MBEDTLS_IV_SIZE_SHIFT,
915 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
916 MBEDTLS_MODE_CBC,
917 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
918 0,
919 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
920 };
921
922 static const mbedtls_cipher_info_t camellia_192_cbc_info = {
923 "CAMELLIA-192-CBC",
924 16,
925 16 >> MBEDTLS_IV_SIZE_SHIFT,
926 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
927 MBEDTLS_MODE_CBC,
928 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
929 0,
930 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
931 };
932
933 static const mbedtls_cipher_info_t camellia_256_cbc_info = {
934 "CAMELLIA-256-CBC",
935 16,
936 16 >> MBEDTLS_IV_SIZE_SHIFT,
937 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
938 MBEDTLS_MODE_CBC,
939 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
940 0,
941 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
942 };
943 #endif /* MBEDTLS_CIPHER_MODE_CBC */
944
945 #if defined(MBEDTLS_CIPHER_MODE_CFB)
946 static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
947 "CAMELLIA-128-CFB128",
948 16,
949 16 >> MBEDTLS_IV_SIZE_SHIFT,
950 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
951 MBEDTLS_MODE_CFB,
952 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
953 0,
954 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
955 };
956
957 static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
958 "CAMELLIA-192-CFB128",
959 16,
960 16 >> MBEDTLS_IV_SIZE_SHIFT,
961 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
962 MBEDTLS_MODE_CFB,
963 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
964 0,
965 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
966 };
967
968 static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
969 "CAMELLIA-256-CFB128",
970 16,
971 16 >> MBEDTLS_IV_SIZE_SHIFT,
972 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
973 MBEDTLS_MODE_CFB,
974 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
975 0,
976 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
977 };
978 #endif /* MBEDTLS_CIPHER_MODE_CFB */
979
980 #if defined(MBEDTLS_CIPHER_MODE_CTR)
981 static const mbedtls_cipher_info_t camellia_128_ctr_info = {
982 "CAMELLIA-128-CTR",
983 16,
984 16 >> MBEDTLS_IV_SIZE_SHIFT,
985 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
986 MBEDTLS_MODE_CTR,
987 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
988 0,
989 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
990 };
991
992 static const mbedtls_cipher_info_t camellia_192_ctr_info = {
993 "CAMELLIA-192-CTR",
994 16,
995 16 >> MBEDTLS_IV_SIZE_SHIFT,
996 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
997 MBEDTLS_MODE_CTR,
998 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
999 0,
1000 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
1001 };
1002
1003 static const mbedtls_cipher_info_t camellia_256_ctr_info = {
1004 "CAMELLIA-256-CTR",
1005 16,
1006 16 >> MBEDTLS_IV_SIZE_SHIFT,
1007 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1008 MBEDTLS_MODE_CTR,
1009 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
1010 0,
1011 MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA
1012 };
1013 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1014
1015 #if defined(MBEDTLS_GCM_C)
gcm_camellia_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1016 static int gcm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
1017 unsigned int key_bitlen)
1018 {
1019 return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
1020 key, key_bitlen);
1021 }
1022
1023 static const mbedtls_cipher_base_t gcm_camellia_info = {
1024 MBEDTLS_CIPHER_ID_CAMELLIA,
1025 NULL,
1026 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1027 NULL,
1028 #endif
1029 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1030 NULL,
1031 #endif
1032 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1033 NULL,
1034 #endif
1035 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1036 NULL,
1037 #endif
1038 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1039 NULL,
1040 #endif
1041 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1042 NULL,
1043 #endif
1044 gcm_camellia_setkey_wrap,
1045 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
1046 gcm_camellia_setkey_wrap,
1047 #endif
1048 gcm_ctx_alloc,
1049 gcm_ctx_free,
1050 };
1051
1052 static const mbedtls_cipher_info_t camellia_128_gcm_info = {
1053 "CAMELLIA-128-GCM",
1054 16,
1055 12 >> MBEDTLS_IV_SIZE_SHIFT,
1056 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1057 MBEDTLS_MODE_GCM,
1058 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
1059 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1060 MBEDTLS_CIPHER_BASE_INDEX_GCM_CAMELLIA
1061 };
1062
1063 static const mbedtls_cipher_info_t camellia_192_gcm_info = {
1064 "CAMELLIA-192-GCM",
1065 16,
1066 12 >> MBEDTLS_IV_SIZE_SHIFT,
1067 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1068 MBEDTLS_MODE_GCM,
1069 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
1070 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1071 MBEDTLS_CIPHER_BASE_INDEX_GCM_CAMELLIA
1072 };
1073
1074 static const mbedtls_cipher_info_t camellia_256_gcm_info = {
1075 "CAMELLIA-256-GCM",
1076 16,
1077 12 >> MBEDTLS_IV_SIZE_SHIFT,
1078 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1079 MBEDTLS_MODE_GCM,
1080 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
1081 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1082 MBEDTLS_CIPHER_BASE_INDEX_GCM_CAMELLIA
1083 };
1084 #endif /* MBEDTLS_GCM_C */
1085
1086 #if defined(MBEDTLS_CCM_C)
ccm_camellia_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1087 static int ccm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
1088 unsigned int key_bitlen)
1089 {
1090 return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
1091 key, key_bitlen);
1092 }
1093
1094 static const mbedtls_cipher_base_t ccm_camellia_info = {
1095 MBEDTLS_CIPHER_ID_CAMELLIA,
1096 NULL,
1097 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1098 NULL,
1099 #endif
1100 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1101 NULL,
1102 #endif
1103 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1104 NULL,
1105 #endif
1106 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1107 NULL,
1108 #endif
1109 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1110 NULL,
1111 #endif
1112 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1113 NULL,
1114 #endif
1115 ccm_camellia_setkey_wrap,
1116 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
1117 ccm_camellia_setkey_wrap,
1118 #endif
1119 ccm_ctx_alloc,
1120 ccm_ctx_free,
1121 };
1122
1123 static const mbedtls_cipher_info_t camellia_128_ccm_info = {
1124 "CAMELLIA-128-CCM",
1125 16,
1126 12 >> MBEDTLS_IV_SIZE_SHIFT,
1127 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1128 MBEDTLS_MODE_CCM,
1129 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
1130 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1131 MBEDTLS_CIPHER_BASE_INDEX_CCM_CAMELLIA
1132 };
1133
1134 static const mbedtls_cipher_info_t camellia_192_ccm_info = {
1135 "CAMELLIA-192-CCM",
1136 16,
1137 12 >> MBEDTLS_IV_SIZE_SHIFT,
1138 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1139 MBEDTLS_MODE_CCM,
1140 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
1141 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1142 MBEDTLS_CIPHER_BASE_INDEX_CCM_CAMELLIA
1143 };
1144
1145 static const mbedtls_cipher_info_t camellia_256_ccm_info = {
1146 "CAMELLIA-256-CCM",
1147 16,
1148 12 >> MBEDTLS_IV_SIZE_SHIFT,
1149 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1150 MBEDTLS_MODE_CCM,
1151 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
1152 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1153 MBEDTLS_CIPHER_BASE_INDEX_CCM_CAMELLIA
1154 };
1155
1156 static const mbedtls_cipher_info_t camellia_128_ccm_star_no_tag_info = {
1157 "CAMELLIA-128-CCM*-NO-TAG",
1158 16,
1159 12 >> MBEDTLS_IV_SIZE_SHIFT,
1160 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1161 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1162 MBEDTLS_CIPHER_CAMELLIA_128_CCM_STAR_NO_TAG,
1163 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1164 MBEDTLS_CIPHER_BASE_INDEX_CCM_CAMELLIA
1165 };
1166
1167 static const mbedtls_cipher_info_t camellia_192_ccm_star_no_tag_info = {
1168 "CAMELLIA-192-CCM*-NO-TAG",
1169 16,
1170 12 >> MBEDTLS_IV_SIZE_SHIFT,
1171 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1172 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1173 MBEDTLS_CIPHER_CAMELLIA_192_CCM_STAR_NO_TAG,
1174 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1175 MBEDTLS_CIPHER_BASE_INDEX_CCM_CAMELLIA
1176 };
1177
1178 static const mbedtls_cipher_info_t camellia_256_ccm_star_no_tag_info = {
1179 "CAMELLIA-256-CCM*-NO-TAG",
1180 16,
1181 12 >> MBEDTLS_IV_SIZE_SHIFT,
1182 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1183 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1184 MBEDTLS_CIPHER_CAMELLIA_256_CCM_STAR_NO_TAG,
1185 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1186 MBEDTLS_CIPHER_BASE_INDEX_CCM_CAMELLIA
1187 };
1188 #endif /* MBEDTLS_CCM_C */
1189
1190 #endif /* MBEDTLS_CAMELLIA_C */
1191
1192 #if defined(MBEDTLS_ARIA_C)
1193
aria_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)1194 static int aria_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1195 const unsigned char *input, unsigned char *output)
1196 {
1197 (void) operation;
1198 return mbedtls_aria_crypt_ecb((mbedtls_aria_context *) ctx, input,
1199 output);
1200 }
1201
1202 #if defined(MBEDTLS_CIPHER_MODE_CBC)
aria_crypt_cbc_wrap(void * ctx,mbedtls_operation_t operation,size_t length,unsigned char * iv,const unsigned char * input,unsigned char * output)1203 static int aria_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
1204 size_t length, unsigned char *iv,
1205 const unsigned char *input, unsigned char *output)
1206 {
1207 return mbedtls_aria_crypt_cbc((mbedtls_aria_context *) ctx, operation, length, iv,
1208 input, output);
1209 }
1210 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1211
1212 #if defined(MBEDTLS_CIPHER_MODE_CFB)
aria_crypt_cfb128_wrap(void * ctx,mbedtls_operation_t operation,size_t length,size_t * iv_off,unsigned char * iv,const unsigned char * input,unsigned char * output)1213 static int aria_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
1214 size_t length, size_t *iv_off, unsigned char *iv,
1215 const unsigned char *input, unsigned char *output)
1216 {
1217 return mbedtls_aria_crypt_cfb128((mbedtls_aria_context *) ctx, operation, length,
1218 iv_off, iv, input, output);
1219 }
1220 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1221
1222 #if defined(MBEDTLS_CIPHER_MODE_CTR)
aria_crypt_ctr_wrap(void * ctx,size_t length,size_t * nc_off,unsigned char * nonce_counter,unsigned char * stream_block,const unsigned char * input,unsigned char * output)1223 static int aria_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
1224 unsigned char *nonce_counter, unsigned char *stream_block,
1225 const unsigned char *input, unsigned char *output)
1226 {
1227 return mbedtls_aria_crypt_ctr((mbedtls_aria_context *) ctx, length, nc_off,
1228 nonce_counter, stream_block, input, output);
1229 }
1230 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1231
1232 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
aria_setkey_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1233 static int aria_setkey_dec_wrap(void *ctx, const unsigned char *key,
1234 unsigned int key_bitlen)
1235 {
1236 return mbedtls_aria_setkey_dec((mbedtls_aria_context *) ctx, key, key_bitlen);
1237 }
1238 #endif
1239
aria_setkey_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1240 static int aria_setkey_enc_wrap(void *ctx, const unsigned char *key,
1241 unsigned int key_bitlen)
1242 {
1243 return mbedtls_aria_setkey_enc((mbedtls_aria_context *) ctx, key, key_bitlen);
1244 }
1245
aria_ctx_alloc(void)1246 static void *aria_ctx_alloc(void)
1247 {
1248 mbedtls_aria_context *ctx;
1249 ctx = mbedtls_calloc(1, sizeof(mbedtls_aria_context));
1250
1251 if (ctx == NULL) {
1252 return NULL;
1253 }
1254
1255 mbedtls_aria_init(ctx);
1256
1257 return ctx;
1258 }
1259
aria_ctx_free(void * ctx)1260 static void aria_ctx_free(void *ctx)
1261 {
1262 mbedtls_aria_free((mbedtls_aria_context *) ctx);
1263 mbedtls_free(ctx);
1264 }
1265
1266 static const mbedtls_cipher_base_t aria_info = {
1267 MBEDTLS_CIPHER_ID_ARIA,
1268 aria_crypt_ecb_wrap,
1269 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1270 aria_crypt_cbc_wrap,
1271 #endif
1272 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1273 aria_crypt_cfb128_wrap,
1274 #endif
1275 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1276 NULL,
1277 #endif
1278 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1279 aria_crypt_ctr_wrap,
1280 #endif
1281 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1282 NULL,
1283 #endif
1284 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1285 NULL,
1286 #endif
1287 aria_setkey_enc_wrap,
1288 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
1289 aria_setkey_dec_wrap,
1290 #endif
1291 aria_ctx_alloc,
1292 aria_ctx_free
1293 };
1294
1295 static const mbedtls_cipher_info_t aria_128_ecb_info = {
1296 "ARIA-128-ECB",
1297 16,
1298 0 >> MBEDTLS_IV_SIZE_SHIFT,
1299 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1300 MBEDTLS_MODE_ECB,
1301 MBEDTLS_CIPHER_ARIA_128_ECB,
1302 0,
1303 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1304 };
1305
1306 static const mbedtls_cipher_info_t aria_192_ecb_info = {
1307 "ARIA-192-ECB",
1308 16,
1309 0 >> MBEDTLS_IV_SIZE_SHIFT,
1310 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1311 MBEDTLS_MODE_ECB,
1312 MBEDTLS_CIPHER_ARIA_192_ECB,
1313 0,
1314 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1315 };
1316
1317 static const mbedtls_cipher_info_t aria_256_ecb_info = {
1318 "ARIA-256-ECB",
1319 16,
1320 0 >> MBEDTLS_IV_SIZE_SHIFT,
1321 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1322 MBEDTLS_MODE_ECB,
1323 MBEDTLS_CIPHER_ARIA_256_ECB,
1324 0,
1325 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1326 };
1327
1328 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1329 static const mbedtls_cipher_info_t aria_128_cbc_info = {
1330 "ARIA-128-CBC",
1331 16,
1332 16 >> MBEDTLS_IV_SIZE_SHIFT,
1333 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1334 MBEDTLS_MODE_CBC,
1335 MBEDTLS_CIPHER_ARIA_128_CBC,
1336 0,
1337 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1338 };
1339
1340 static const mbedtls_cipher_info_t aria_192_cbc_info = {
1341 "ARIA-192-CBC",
1342 16,
1343 16 >> MBEDTLS_IV_SIZE_SHIFT,
1344 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1345 MBEDTLS_MODE_CBC,
1346 MBEDTLS_CIPHER_ARIA_192_CBC,
1347 0,
1348 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1349 };
1350
1351 static const mbedtls_cipher_info_t aria_256_cbc_info = {
1352 "ARIA-256-CBC",
1353 16,
1354 16 >> MBEDTLS_IV_SIZE_SHIFT,
1355 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1356 MBEDTLS_MODE_CBC,
1357 MBEDTLS_CIPHER_ARIA_256_CBC,
1358 0,
1359 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1360 };
1361 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1362
1363 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1364 static const mbedtls_cipher_info_t aria_128_cfb128_info = {
1365 "ARIA-128-CFB128",
1366 16,
1367 16 >> MBEDTLS_IV_SIZE_SHIFT,
1368 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1369 MBEDTLS_MODE_CFB,
1370 MBEDTLS_CIPHER_ARIA_128_CFB128,
1371 0,
1372 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1373 };
1374
1375 static const mbedtls_cipher_info_t aria_192_cfb128_info = {
1376 "ARIA-192-CFB128",
1377 16,
1378 16 >> MBEDTLS_IV_SIZE_SHIFT,
1379 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1380 MBEDTLS_MODE_CFB,
1381 MBEDTLS_CIPHER_ARIA_192_CFB128,
1382 0,
1383 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1384 };
1385
1386 static const mbedtls_cipher_info_t aria_256_cfb128_info = {
1387 "ARIA-256-CFB128",
1388 16,
1389 16 >> MBEDTLS_IV_SIZE_SHIFT,
1390 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1391 MBEDTLS_MODE_CFB,
1392 MBEDTLS_CIPHER_ARIA_256_CFB128,
1393 0,
1394 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1395 };
1396 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1397
1398 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1399 static const mbedtls_cipher_info_t aria_128_ctr_info = {
1400 "ARIA-128-CTR",
1401 16,
1402 16 >> MBEDTLS_IV_SIZE_SHIFT,
1403 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1404 MBEDTLS_MODE_CTR,
1405 MBEDTLS_CIPHER_ARIA_128_CTR,
1406 0,
1407 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1408 };
1409
1410 static const mbedtls_cipher_info_t aria_192_ctr_info = {
1411 "ARIA-192-CTR",
1412 16,
1413 16 >> MBEDTLS_IV_SIZE_SHIFT,
1414 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1415 MBEDTLS_MODE_CTR,
1416 MBEDTLS_CIPHER_ARIA_192_CTR,
1417 0,
1418 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1419 };
1420
1421 static const mbedtls_cipher_info_t aria_256_ctr_info = {
1422 "ARIA-256-CTR",
1423 16,
1424 16 >> MBEDTLS_IV_SIZE_SHIFT,
1425 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1426 MBEDTLS_MODE_CTR,
1427 MBEDTLS_CIPHER_ARIA_256_CTR,
1428 0,
1429 MBEDTLS_CIPHER_BASE_INDEX_ARIA
1430 };
1431 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1432
1433 #if defined(MBEDTLS_GCM_C)
gcm_aria_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1434 static int gcm_aria_setkey_wrap(void *ctx, const unsigned char *key,
1435 unsigned int key_bitlen)
1436 {
1437 return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1438 key, key_bitlen);
1439 }
1440
1441 static const mbedtls_cipher_base_t gcm_aria_info = {
1442 MBEDTLS_CIPHER_ID_ARIA,
1443 NULL,
1444 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1445 NULL,
1446 #endif
1447 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1448 NULL,
1449 #endif
1450 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1451 NULL,
1452 #endif
1453 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1454 NULL,
1455 #endif
1456 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1457 NULL,
1458 #endif
1459 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1460 NULL,
1461 #endif
1462 gcm_aria_setkey_wrap,
1463 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
1464 gcm_aria_setkey_wrap,
1465 #endif
1466 gcm_ctx_alloc,
1467 gcm_ctx_free,
1468 };
1469
1470 static const mbedtls_cipher_info_t aria_128_gcm_info = {
1471 "ARIA-128-GCM",
1472 16,
1473 12 >> MBEDTLS_IV_SIZE_SHIFT,
1474 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1475 MBEDTLS_MODE_GCM,
1476 MBEDTLS_CIPHER_ARIA_128_GCM,
1477 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1478 MBEDTLS_CIPHER_BASE_INDEX_GCM_ARIA
1479 };
1480
1481 static const mbedtls_cipher_info_t aria_192_gcm_info = {
1482 "ARIA-192-GCM",
1483 16,
1484 12 >> MBEDTLS_IV_SIZE_SHIFT,
1485 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1486 MBEDTLS_MODE_GCM,
1487 MBEDTLS_CIPHER_ARIA_192_GCM,
1488 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1489 MBEDTLS_CIPHER_BASE_INDEX_GCM_ARIA
1490 };
1491
1492 static const mbedtls_cipher_info_t aria_256_gcm_info = {
1493 "ARIA-256-GCM",
1494 16,
1495 12 >> MBEDTLS_IV_SIZE_SHIFT,
1496 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1497 MBEDTLS_MODE_GCM,
1498 MBEDTLS_CIPHER_ARIA_256_GCM,
1499 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1500 MBEDTLS_CIPHER_BASE_INDEX_GCM_ARIA
1501 };
1502 #endif /* MBEDTLS_GCM_C */
1503
1504 #if defined(MBEDTLS_CCM_C)
ccm_aria_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1505 static int ccm_aria_setkey_wrap(void *ctx, const unsigned char *key,
1506 unsigned int key_bitlen)
1507 {
1508 return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1509 key, key_bitlen);
1510 }
1511
1512 static const mbedtls_cipher_base_t ccm_aria_info = {
1513 MBEDTLS_CIPHER_ID_ARIA,
1514 NULL,
1515 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1516 NULL,
1517 #endif
1518 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1519 NULL,
1520 #endif
1521 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1522 NULL,
1523 #endif
1524 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1525 NULL,
1526 #endif
1527 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1528 NULL,
1529 #endif
1530 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1531 NULL,
1532 #endif
1533 ccm_aria_setkey_wrap,
1534 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
1535 ccm_aria_setkey_wrap,
1536 #endif
1537 ccm_ctx_alloc,
1538 ccm_ctx_free,
1539 };
1540
1541 static const mbedtls_cipher_info_t aria_128_ccm_info = {
1542 "ARIA-128-CCM",
1543 16,
1544 12 >> MBEDTLS_IV_SIZE_SHIFT,
1545 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1546 MBEDTLS_MODE_CCM,
1547 MBEDTLS_CIPHER_ARIA_128_CCM,
1548 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1549 MBEDTLS_CIPHER_BASE_INDEX_CCM_ARIA
1550 };
1551
1552 static const mbedtls_cipher_info_t aria_192_ccm_info = {
1553 "ARIA-192-CCM",
1554 16,
1555 12 >> MBEDTLS_IV_SIZE_SHIFT,
1556 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1557 MBEDTLS_MODE_CCM,
1558 MBEDTLS_CIPHER_ARIA_192_CCM,
1559 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1560 MBEDTLS_CIPHER_BASE_INDEX_CCM_ARIA
1561 };
1562
1563 static const mbedtls_cipher_info_t aria_256_ccm_info = {
1564 "ARIA-256-CCM",
1565 16,
1566 12 >> MBEDTLS_IV_SIZE_SHIFT,
1567 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1568 MBEDTLS_MODE_CCM,
1569 MBEDTLS_CIPHER_ARIA_256_CCM,
1570 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1571 MBEDTLS_CIPHER_BASE_INDEX_CCM_ARIA
1572 };
1573
1574 static const mbedtls_cipher_info_t aria_128_ccm_star_no_tag_info = {
1575 "ARIA-128-CCM*-NO-TAG",
1576 16,
1577 12 >> MBEDTLS_IV_SIZE_SHIFT,
1578 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
1579 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1580 MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG,
1581 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1582 MBEDTLS_CIPHER_BASE_INDEX_CCM_ARIA
1583 };
1584
1585 static const mbedtls_cipher_info_t aria_192_ccm_star_no_tag_info = {
1586 "ARIA-192-CCM*-NO-TAG",
1587 16,
1588 12 >> MBEDTLS_IV_SIZE_SHIFT,
1589 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
1590 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1591 MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG,
1592 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1593 MBEDTLS_CIPHER_BASE_INDEX_CCM_ARIA
1594 };
1595
1596 static const mbedtls_cipher_info_t aria_256_ccm_star_no_tag_info = {
1597 "ARIA-256-CCM*-NO-TAG",
1598 16,
1599 12 >> MBEDTLS_IV_SIZE_SHIFT,
1600 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1601 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1602 MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG,
1603 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1604 MBEDTLS_CIPHER_BASE_INDEX_CCM_ARIA
1605 };
1606 #endif /* MBEDTLS_CCM_C */
1607
1608 #endif /* MBEDTLS_ARIA_C */
1609
1610 #if defined(MBEDTLS_DES_C)
1611
des_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)1612 static int des_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1613 const unsigned char *input, unsigned char *output)
1614 {
1615 ((void) operation);
1616 return mbedtls_des_crypt_ecb((mbedtls_des_context *) ctx, input, output);
1617 }
1618
des3_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)1619 static int des3_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1620 const unsigned char *input, unsigned char *output)
1621 {
1622 ((void) operation);
1623 return mbedtls_des3_crypt_ecb((mbedtls_des3_context *) ctx, input, output);
1624 }
1625
1626 #if defined(MBEDTLS_CIPHER_MODE_CBC)
des_crypt_cbc_wrap(void * ctx,mbedtls_operation_t operation,size_t length,unsigned char * iv,const unsigned char * input,unsigned char * output)1627 static int des_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
1628 unsigned char *iv, const unsigned char *input, unsigned char *output)
1629 {
1630 return mbedtls_des_crypt_cbc((mbedtls_des_context *) ctx, operation, length, iv, input,
1631 output);
1632 }
1633 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1634
1635 #if defined(MBEDTLS_CIPHER_MODE_CBC)
des3_crypt_cbc_wrap(void * ctx,mbedtls_operation_t operation,size_t length,unsigned char * iv,const unsigned char * input,unsigned char * output)1636 static int des3_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
1637 unsigned char *iv, const unsigned char *input, unsigned char *output)
1638 {
1639 return mbedtls_des3_crypt_cbc((mbedtls_des3_context *) ctx, operation, length, iv, input,
1640 output);
1641 }
1642 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1643
des_setkey_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1644 static int des_setkey_dec_wrap(void *ctx, const unsigned char *key,
1645 unsigned int key_bitlen)
1646 {
1647 ((void) key_bitlen);
1648
1649 return mbedtls_des_setkey_dec((mbedtls_des_context *) ctx, key);
1650 }
1651
des_setkey_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1652 static int des_setkey_enc_wrap(void *ctx, const unsigned char *key,
1653 unsigned int key_bitlen)
1654 {
1655 ((void) key_bitlen);
1656
1657 return mbedtls_des_setkey_enc((mbedtls_des_context *) ctx, key);
1658 }
1659
des3_set2key_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1660 static int des3_set2key_dec_wrap(void *ctx, const unsigned char *key,
1661 unsigned int key_bitlen)
1662 {
1663 ((void) key_bitlen);
1664
1665 return mbedtls_des3_set2key_dec((mbedtls_des3_context *) ctx, key);
1666 }
1667
des3_set2key_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1668 static int des3_set2key_enc_wrap(void *ctx, const unsigned char *key,
1669 unsigned int key_bitlen)
1670 {
1671 ((void) key_bitlen);
1672
1673 return mbedtls_des3_set2key_enc((mbedtls_des3_context *) ctx, key);
1674 }
1675
des3_set3key_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1676 static int des3_set3key_dec_wrap(void *ctx, const unsigned char *key,
1677 unsigned int key_bitlen)
1678 {
1679 ((void) key_bitlen);
1680
1681 return mbedtls_des3_set3key_dec((mbedtls_des3_context *) ctx, key);
1682 }
1683
des3_set3key_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1684 static int des3_set3key_enc_wrap(void *ctx, const unsigned char *key,
1685 unsigned int key_bitlen)
1686 {
1687 ((void) key_bitlen);
1688
1689 return mbedtls_des3_set3key_enc((mbedtls_des3_context *) ctx, key);
1690 }
1691
des_ctx_alloc(void)1692 static void *des_ctx_alloc(void)
1693 {
1694 mbedtls_des_context *des = mbedtls_calloc(1, sizeof(mbedtls_des_context));
1695
1696 if (des == NULL) {
1697 return NULL;
1698 }
1699
1700 mbedtls_des_init(des);
1701
1702 return des;
1703 }
1704
des_ctx_free(void * ctx)1705 static void des_ctx_free(void *ctx)
1706 {
1707 mbedtls_des_free((mbedtls_des_context *) ctx);
1708 mbedtls_free(ctx);
1709 }
1710
des3_ctx_alloc(void)1711 static void *des3_ctx_alloc(void)
1712 {
1713 mbedtls_des3_context *des3;
1714 des3 = mbedtls_calloc(1, sizeof(mbedtls_des3_context));
1715
1716 if (des3 == NULL) {
1717 return NULL;
1718 }
1719
1720 mbedtls_des3_init(des3);
1721
1722 return des3;
1723 }
1724
des3_ctx_free(void * ctx)1725 static void des3_ctx_free(void *ctx)
1726 {
1727 mbedtls_des3_free((mbedtls_des3_context *) ctx);
1728 mbedtls_free(ctx);
1729 }
1730
1731 static const mbedtls_cipher_base_t des_info = {
1732 MBEDTLS_CIPHER_ID_DES,
1733 des_crypt_ecb_wrap,
1734 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1735 des_crypt_cbc_wrap,
1736 #endif
1737 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1738 NULL,
1739 #endif
1740 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1741 NULL,
1742 #endif
1743 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1744 NULL,
1745 #endif
1746 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1747 NULL,
1748 #endif
1749 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1750 NULL,
1751 #endif
1752 des_setkey_enc_wrap,
1753 des_setkey_dec_wrap,
1754 des_ctx_alloc,
1755 des_ctx_free
1756 };
1757
1758 static const mbedtls_cipher_info_t des_ecb_info = {
1759 "DES-ECB",
1760 8,
1761 0 >> MBEDTLS_IV_SIZE_SHIFT,
1762 MBEDTLS_KEY_LENGTH_DES >> MBEDTLS_KEY_BITLEN_SHIFT,
1763 MBEDTLS_MODE_ECB,
1764 MBEDTLS_CIPHER_DES_ECB,
1765 0,
1766 MBEDTLS_CIPHER_BASE_INDEX_DES
1767 };
1768
1769 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1770 static const mbedtls_cipher_info_t des_cbc_info = {
1771 "DES-CBC",
1772 8,
1773 8 >> MBEDTLS_IV_SIZE_SHIFT,
1774 MBEDTLS_KEY_LENGTH_DES >> MBEDTLS_KEY_BITLEN_SHIFT,
1775 MBEDTLS_MODE_CBC,
1776 MBEDTLS_CIPHER_DES_CBC,
1777 0,
1778 MBEDTLS_CIPHER_BASE_INDEX_DES
1779 };
1780 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1781
1782 static const mbedtls_cipher_base_t des_ede_info = {
1783 MBEDTLS_CIPHER_ID_DES,
1784 des3_crypt_ecb_wrap,
1785 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1786 des3_crypt_cbc_wrap,
1787 #endif
1788 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1789 NULL,
1790 #endif
1791 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1792 NULL,
1793 #endif
1794 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1795 NULL,
1796 #endif
1797 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1798 NULL,
1799 #endif
1800 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1801 NULL,
1802 #endif
1803 des3_set2key_enc_wrap,
1804 des3_set2key_dec_wrap,
1805 des3_ctx_alloc,
1806 des3_ctx_free
1807 };
1808
1809 static const mbedtls_cipher_info_t des_ede_ecb_info = {
1810 "DES-EDE-ECB",
1811 8,
1812 0 >> MBEDTLS_IV_SIZE_SHIFT,
1813 MBEDTLS_KEY_LENGTH_DES_EDE >> MBEDTLS_KEY_BITLEN_SHIFT,
1814 MBEDTLS_MODE_ECB,
1815 MBEDTLS_CIPHER_DES_EDE_ECB,
1816 0,
1817 MBEDTLS_CIPHER_BASE_INDEX_DES_EDE
1818 };
1819
1820 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1821 static const mbedtls_cipher_info_t des_ede_cbc_info = {
1822 "DES-EDE-CBC",
1823 8,
1824 8 >> MBEDTLS_IV_SIZE_SHIFT,
1825 MBEDTLS_KEY_LENGTH_DES_EDE >> MBEDTLS_KEY_BITLEN_SHIFT,
1826 MBEDTLS_MODE_CBC,
1827 MBEDTLS_CIPHER_DES_EDE_CBC,
1828 0,
1829 MBEDTLS_CIPHER_BASE_INDEX_DES_EDE
1830 };
1831 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1832
1833 static const mbedtls_cipher_base_t des_ede3_info = {
1834 MBEDTLS_CIPHER_ID_3DES,
1835 des3_crypt_ecb_wrap,
1836 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1837 des3_crypt_cbc_wrap,
1838 #endif
1839 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1840 NULL,
1841 #endif
1842 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1843 NULL,
1844 #endif
1845 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1846 NULL,
1847 #endif
1848 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1849 NULL,
1850 #endif
1851 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1852 NULL,
1853 #endif
1854 des3_set3key_enc_wrap,
1855 des3_set3key_dec_wrap,
1856 des3_ctx_alloc,
1857 des3_ctx_free
1858 };
1859
1860 static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1861 "DES-EDE3-ECB",
1862 8,
1863 0 >> MBEDTLS_IV_SIZE_SHIFT,
1864 MBEDTLS_KEY_LENGTH_DES_EDE3 >> MBEDTLS_KEY_BITLEN_SHIFT,
1865 MBEDTLS_MODE_ECB,
1866 MBEDTLS_CIPHER_DES_EDE3_ECB,
1867 0,
1868 MBEDTLS_CIPHER_BASE_INDEX_DES_EDE3
1869 };
1870 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1871 static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1872 "DES-EDE3-CBC",
1873 8,
1874 8 >> MBEDTLS_IV_SIZE_SHIFT,
1875 MBEDTLS_KEY_LENGTH_DES_EDE3 >> MBEDTLS_KEY_BITLEN_SHIFT,
1876 MBEDTLS_MODE_CBC,
1877 MBEDTLS_CIPHER_DES_EDE3_CBC,
1878 0,
1879 MBEDTLS_CIPHER_BASE_INDEX_DES_EDE3
1880 };
1881 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1882 #endif /* MBEDTLS_DES_C */
1883
1884 #if defined(MBEDTLS_CHACHA20_C)
1885
chacha20_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1886 static int chacha20_setkey_wrap(void *ctx, const unsigned char *key,
1887 unsigned int key_bitlen)
1888 {
1889 if (key_bitlen != 256U) {
1890 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1891 }
1892
1893 if (0 != mbedtls_chacha20_setkey((mbedtls_chacha20_context *) ctx, key)) {
1894 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1895 }
1896
1897 return 0;
1898 }
1899
chacha20_stream_wrap(void * ctx,size_t length,const unsigned char * input,unsigned char * output)1900 static int chacha20_stream_wrap(void *ctx, size_t length,
1901 const unsigned char *input,
1902 unsigned char *output)
1903 {
1904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1905
1906 ret = mbedtls_chacha20_update(ctx, length, input, output);
1907 if (ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA) {
1908 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1909 }
1910
1911 return ret;
1912 }
1913
chacha20_ctx_alloc(void)1914 static void *chacha20_ctx_alloc(void)
1915 {
1916 mbedtls_chacha20_context *ctx;
1917 ctx = mbedtls_calloc(1, sizeof(mbedtls_chacha20_context));
1918
1919 if (ctx == NULL) {
1920 return NULL;
1921 }
1922
1923 mbedtls_chacha20_init(ctx);
1924
1925 return ctx;
1926 }
1927
chacha20_ctx_free(void * ctx)1928 static void chacha20_ctx_free(void *ctx)
1929 {
1930 mbedtls_chacha20_free((mbedtls_chacha20_context *) ctx);
1931 mbedtls_free(ctx);
1932 }
1933
1934 static const mbedtls_cipher_base_t chacha20_base_info = {
1935 MBEDTLS_CIPHER_ID_CHACHA20,
1936 NULL,
1937 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1938 NULL,
1939 #endif
1940 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1941 NULL,
1942 #endif
1943 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1944 NULL,
1945 #endif
1946 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1947 NULL,
1948 #endif
1949 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1950 NULL,
1951 #endif
1952 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1953 chacha20_stream_wrap,
1954 #endif
1955 chacha20_setkey_wrap,
1956 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
1957 chacha20_setkey_wrap,
1958 #endif
1959 chacha20_ctx_alloc,
1960 chacha20_ctx_free
1961 };
1962 static const mbedtls_cipher_info_t chacha20_info = {
1963 "CHACHA20",
1964 1,
1965 12 >> MBEDTLS_IV_SIZE_SHIFT,
1966 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
1967 MBEDTLS_MODE_STREAM,
1968 MBEDTLS_CIPHER_CHACHA20,
1969 0,
1970 MBEDTLS_CIPHER_BASE_INDEX_CHACHA20_BASE
1971 };
1972 #endif /* MBEDTLS_CHACHA20_C */
1973
1974 #if defined(MBEDTLS_CHACHAPOLY_C)
1975
chachapoly_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1976 static int chachapoly_setkey_wrap(void *ctx,
1977 const unsigned char *key,
1978 unsigned int key_bitlen)
1979 {
1980 if (key_bitlen != 256U) {
1981 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1982 }
1983
1984 if (0 != mbedtls_chachapoly_setkey((mbedtls_chachapoly_context *) ctx, key)) {
1985 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1986 }
1987
1988 return 0;
1989 }
1990
chachapoly_ctx_alloc(void)1991 static void *chachapoly_ctx_alloc(void)
1992 {
1993 mbedtls_chachapoly_context *ctx;
1994 ctx = mbedtls_calloc(1, sizeof(mbedtls_chachapoly_context));
1995
1996 if (ctx == NULL) {
1997 return NULL;
1998 }
1999
2000 mbedtls_chachapoly_init(ctx);
2001
2002 return ctx;
2003 }
2004
chachapoly_ctx_free(void * ctx)2005 static void chachapoly_ctx_free(void *ctx)
2006 {
2007 mbedtls_chachapoly_free((mbedtls_chachapoly_context *) ctx);
2008 mbedtls_free(ctx);
2009 }
2010
2011 static const mbedtls_cipher_base_t chachapoly_base_info = {
2012 MBEDTLS_CIPHER_ID_CHACHA20,
2013 NULL,
2014 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2015 NULL,
2016 #endif
2017 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2018 NULL,
2019 #endif
2020 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2021 NULL,
2022 #endif
2023 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2024 NULL,
2025 #endif
2026 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2027 NULL,
2028 #endif
2029 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
2030 NULL,
2031 #endif
2032 chachapoly_setkey_wrap,
2033 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
2034 chachapoly_setkey_wrap,
2035 #endif
2036 chachapoly_ctx_alloc,
2037 chachapoly_ctx_free
2038 };
2039 static const mbedtls_cipher_info_t chachapoly_info = {
2040 "CHACHA20-POLY1305",
2041 1,
2042 12 >> MBEDTLS_IV_SIZE_SHIFT,
2043 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
2044 MBEDTLS_MODE_CHACHAPOLY,
2045 MBEDTLS_CIPHER_CHACHA20_POLY1305,
2046 0,
2047 MBEDTLS_CIPHER_BASE_INDEX_CHACHAPOLY_BASE
2048 };
2049 #endif /* MBEDTLS_CHACHAPOLY_C */
2050
2051 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
null_crypt_stream(void * ctx,size_t length,const unsigned char * input,unsigned char * output)2052 static int null_crypt_stream(void *ctx, size_t length,
2053 const unsigned char *input,
2054 unsigned char *output)
2055 {
2056 ((void) ctx);
2057 memmove(output, input, length);
2058 return 0;
2059 }
2060
null_setkey(void * ctx,const unsigned char * key,unsigned int key_bitlen)2061 static int null_setkey(void *ctx, const unsigned char *key,
2062 unsigned int key_bitlen)
2063 {
2064 ((void) ctx);
2065 ((void) key);
2066 ((void) key_bitlen);
2067
2068 return 0;
2069 }
2070
null_ctx_alloc(void)2071 static void *null_ctx_alloc(void)
2072 {
2073 return (void *) 1;
2074 }
2075
null_ctx_free(void * ctx)2076 static void null_ctx_free(void *ctx)
2077 {
2078 ((void) ctx);
2079 }
2080
2081 static const mbedtls_cipher_base_t null_base_info = {
2082 MBEDTLS_CIPHER_ID_NULL,
2083 NULL,
2084 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2085 NULL,
2086 #endif
2087 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2088 NULL,
2089 #endif
2090 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2091 NULL,
2092 #endif
2093 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2094 NULL,
2095 #endif
2096 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2097 NULL,
2098 #endif
2099 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
2100 null_crypt_stream,
2101 #endif
2102 null_setkey,
2103 #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
2104 null_setkey,
2105 #endif
2106 null_ctx_alloc,
2107 null_ctx_free
2108 };
2109
2110 static const mbedtls_cipher_info_t null_cipher_info = {
2111 "NULL",
2112 1,
2113 0 >> MBEDTLS_IV_SIZE_SHIFT,
2114 0 >> MBEDTLS_KEY_BITLEN_SHIFT,
2115 MBEDTLS_MODE_STREAM,
2116 MBEDTLS_CIPHER_NULL,
2117 0,
2118 MBEDTLS_CIPHER_BASE_INDEX_NULL_BASE
2119 };
2120 #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
2121
2122 #if defined(MBEDTLS_NIST_KW_C)
kw_ctx_alloc(void)2123 static void *kw_ctx_alloc(void)
2124 {
2125 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_nist_kw_context));
2126
2127 if (ctx != NULL) {
2128 mbedtls_nist_kw_init((mbedtls_nist_kw_context *) ctx);
2129 }
2130
2131 return ctx;
2132 }
2133
kw_ctx_free(void * ctx)2134 static void kw_ctx_free(void *ctx)
2135 {
2136 mbedtls_nist_kw_free(ctx);
2137 mbedtls_free(ctx);
2138 }
2139
kw_aes_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)2140 static int kw_aes_setkey_wrap(void *ctx, const unsigned char *key,
2141 unsigned int key_bitlen)
2142 {
2143 return mbedtls_nist_kw_setkey((mbedtls_nist_kw_context *) ctx,
2144 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 1);
2145 }
2146
kw_aes_setkey_unwrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)2147 static int kw_aes_setkey_unwrap(void *ctx, const unsigned char *key,
2148 unsigned int key_bitlen)
2149 {
2150 return mbedtls_nist_kw_setkey((mbedtls_nist_kw_context *) ctx,
2151 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 0);
2152 }
2153
2154 static const mbedtls_cipher_base_t kw_aes_info = {
2155 MBEDTLS_CIPHER_ID_AES,
2156 NULL,
2157 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2158 NULL,
2159 #endif
2160 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2161 NULL,
2162 #endif
2163 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2164 NULL,
2165 #endif
2166 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2167 NULL,
2168 #endif
2169 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2170 NULL,
2171 #endif
2172 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
2173 NULL,
2174 #endif
2175 kw_aes_setkey_wrap,
2176 kw_aes_setkey_unwrap,
2177 kw_ctx_alloc,
2178 kw_ctx_free,
2179 };
2180
2181 static const mbedtls_cipher_info_t aes_128_nist_kw_info = {
2182 "AES-128-KW",
2183 16,
2184 0 >> MBEDTLS_IV_SIZE_SHIFT,
2185 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
2186 MBEDTLS_MODE_KW,
2187 MBEDTLS_CIPHER_AES_128_KW,
2188 0,
2189 MBEDTLS_CIPHER_BASE_INDEX_KW_AES
2190 };
2191
2192 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2193 static const mbedtls_cipher_info_t aes_192_nist_kw_info = {
2194 "AES-192-KW",
2195 16,
2196 0 >> MBEDTLS_IV_SIZE_SHIFT,
2197 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
2198 MBEDTLS_MODE_KW,
2199 MBEDTLS_CIPHER_AES_192_KW,
2200 0,
2201 MBEDTLS_CIPHER_BASE_INDEX_KW_AES
2202 };
2203
2204 static const mbedtls_cipher_info_t aes_256_nist_kw_info = {
2205 "AES-256-KW",
2206 16,
2207 0 >> MBEDTLS_IV_SIZE_SHIFT,
2208 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
2209 MBEDTLS_MODE_KW,
2210 MBEDTLS_CIPHER_AES_256_KW,
2211 0,
2212 MBEDTLS_CIPHER_BASE_INDEX_KW_AES
2213 };
2214 #endif
2215
2216 static const mbedtls_cipher_info_t aes_128_nist_kwp_info = {
2217 "AES-128-KWP",
2218 16,
2219 0 >> MBEDTLS_IV_SIZE_SHIFT,
2220 128 >> MBEDTLS_KEY_BITLEN_SHIFT,
2221 MBEDTLS_MODE_KWP,
2222 MBEDTLS_CIPHER_AES_128_KWP,
2223 0,
2224 MBEDTLS_CIPHER_BASE_INDEX_KW_AES
2225 };
2226
2227 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2228 static const mbedtls_cipher_info_t aes_192_nist_kwp_info = {
2229 "AES-192-KWP",
2230 16,
2231 0 >> MBEDTLS_IV_SIZE_SHIFT,
2232 192 >> MBEDTLS_KEY_BITLEN_SHIFT,
2233 MBEDTLS_MODE_KWP,
2234 MBEDTLS_CIPHER_AES_192_KWP,
2235 0,
2236 MBEDTLS_CIPHER_BASE_INDEX_KW_AES
2237 };
2238
2239 static const mbedtls_cipher_info_t aes_256_nist_kwp_info = {
2240 "AES-256-KWP",
2241 16,
2242 0 >> MBEDTLS_IV_SIZE_SHIFT,
2243 256 >> MBEDTLS_KEY_BITLEN_SHIFT,
2244 MBEDTLS_MODE_KWP,
2245 MBEDTLS_CIPHER_AES_256_KWP,
2246 0,
2247 MBEDTLS_CIPHER_BASE_INDEX_KW_AES
2248 };
2249 #endif
2250 #endif /* MBEDTLS_NIST_KW_C */
2251
2252 const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
2253 {
2254 #if defined(MBEDTLS_AES_C)
2255 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
2256 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2257 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
2258 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
2259 #endif
2260 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2261 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
2262 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2263 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
2264 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
2265 #endif
2266 #endif
2267 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2268 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
2269 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2270 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
2271 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
2272 #endif
2273 #endif
2274 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2275 { MBEDTLS_CIPHER_AES_128_OFB, &aes_128_ofb_info },
2276 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2277 { MBEDTLS_CIPHER_AES_192_OFB, &aes_192_ofb_info },
2278 { MBEDTLS_CIPHER_AES_256_OFB, &aes_256_ofb_info },
2279 #endif
2280 #endif
2281 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2282 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
2283 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2284 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
2285 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
2286 #endif
2287 #endif
2288 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2289 { MBEDTLS_CIPHER_AES_128_XTS, &aes_128_xts_info },
2290 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2291 { MBEDTLS_CIPHER_AES_256_XTS, &aes_256_xts_info },
2292 #endif
2293 #endif
2294 #endif /* MBEDTLS_AES_C */
2295 #if defined(MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA)
2296 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
2297 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2298 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
2299 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
2300 #endif
2301 #endif
2302 #if defined(MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA)
2303 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
2304 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2305 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
2306 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
2307 #endif
2308 #endif
2309 #if defined(MBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_AES_VIA_LEGACY_OR_USE_PSA)
2310 { MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG, &aes_128_ccm_star_no_tag_info },
2311 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2312 { MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG, &aes_192_ccm_star_no_tag_info },
2313 { MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG, &aes_256_ccm_star_no_tag_info },
2314 #endif
2315 #endif
2316
2317 #if defined(MBEDTLS_CAMELLIA_C)
2318 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
2319 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
2320 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
2321 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2322 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
2323 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
2324 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
2325 #endif
2326 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2327 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
2328 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
2329 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
2330 #endif
2331 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2332 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
2333 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
2334 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
2335 #endif
2336 #if defined(MBEDTLS_GCM_C)
2337 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
2338 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
2339 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
2340 #endif
2341 #if defined(MBEDTLS_CCM_C)
2342 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
2343 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
2344 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
2345 { MBEDTLS_CIPHER_CAMELLIA_128_CCM_STAR_NO_TAG, &camellia_128_ccm_star_no_tag_info },
2346 { MBEDTLS_CIPHER_CAMELLIA_192_CCM_STAR_NO_TAG, &camellia_192_ccm_star_no_tag_info },
2347 { MBEDTLS_CIPHER_CAMELLIA_256_CCM_STAR_NO_TAG, &camellia_256_ccm_star_no_tag_info },
2348 #endif
2349 #endif /* MBEDTLS_CAMELLIA_C */
2350
2351 #if defined(MBEDTLS_ARIA_C)
2352 { MBEDTLS_CIPHER_ARIA_128_ECB, &aria_128_ecb_info },
2353 { MBEDTLS_CIPHER_ARIA_192_ECB, &aria_192_ecb_info },
2354 { MBEDTLS_CIPHER_ARIA_256_ECB, &aria_256_ecb_info },
2355 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2356 { MBEDTLS_CIPHER_ARIA_128_CBC, &aria_128_cbc_info },
2357 { MBEDTLS_CIPHER_ARIA_192_CBC, &aria_192_cbc_info },
2358 { MBEDTLS_CIPHER_ARIA_256_CBC, &aria_256_cbc_info },
2359 #endif
2360 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2361 { MBEDTLS_CIPHER_ARIA_128_CFB128, &aria_128_cfb128_info },
2362 { MBEDTLS_CIPHER_ARIA_192_CFB128, &aria_192_cfb128_info },
2363 { MBEDTLS_CIPHER_ARIA_256_CFB128, &aria_256_cfb128_info },
2364 #endif
2365 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2366 { MBEDTLS_CIPHER_ARIA_128_CTR, &aria_128_ctr_info },
2367 { MBEDTLS_CIPHER_ARIA_192_CTR, &aria_192_ctr_info },
2368 { MBEDTLS_CIPHER_ARIA_256_CTR, &aria_256_ctr_info },
2369 #endif
2370 #if defined(MBEDTLS_GCM_C)
2371 { MBEDTLS_CIPHER_ARIA_128_GCM, &aria_128_gcm_info },
2372 { MBEDTLS_CIPHER_ARIA_192_GCM, &aria_192_gcm_info },
2373 { MBEDTLS_CIPHER_ARIA_256_GCM, &aria_256_gcm_info },
2374 #endif
2375 #if defined(MBEDTLS_CCM_C)
2376 { MBEDTLS_CIPHER_ARIA_128_CCM, &aria_128_ccm_info },
2377 { MBEDTLS_CIPHER_ARIA_192_CCM, &aria_192_ccm_info },
2378 { MBEDTLS_CIPHER_ARIA_256_CCM, &aria_256_ccm_info },
2379 { MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG, &aria_128_ccm_star_no_tag_info },
2380 { MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG, &aria_192_ccm_star_no_tag_info },
2381 { MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG, &aria_256_ccm_star_no_tag_info },
2382 #endif
2383 #endif /* MBEDTLS_ARIA_C */
2384
2385 #if defined(MBEDTLS_DES_C)
2386 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
2387 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
2388 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
2389 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2390 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
2391 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
2392 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
2393 #endif
2394 #endif /* MBEDTLS_DES_C */
2395
2396 #if defined(MBEDTLS_CHACHA20_C)
2397 { MBEDTLS_CIPHER_CHACHA20, &chacha20_info },
2398 #endif
2399
2400 #if defined(MBEDTLS_CHACHAPOLY_C)
2401 { MBEDTLS_CIPHER_CHACHA20_POLY1305, &chachapoly_info },
2402 #endif
2403
2404 #if defined(MBEDTLS_NIST_KW_C)
2405 { MBEDTLS_CIPHER_AES_128_KW, &aes_128_nist_kw_info },
2406 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2407 { MBEDTLS_CIPHER_AES_192_KW, &aes_192_nist_kw_info },
2408 { MBEDTLS_CIPHER_AES_256_KW, &aes_256_nist_kw_info },
2409 #endif
2410 { MBEDTLS_CIPHER_AES_128_KWP, &aes_128_nist_kwp_info },
2411 #if !defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
2412 { MBEDTLS_CIPHER_AES_192_KWP, &aes_192_nist_kwp_info },
2413 { MBEDTLS_CIPHER_AES_256_KWP, &aes_256_nist_kwp_info },
2414 #endif
2415 #endif
2416
2417 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2418 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
2419 #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
2420
2421 { MBEDTLS_CIPHER_NONE, NULL }
2422 };
2423
2424 #define NUM_CIPHERS (sizeof(mbedtls_cipher_definitions) / \
2425 sizeof(mbedtls_cipher_definitions[0]))
2426 int mbedtls_cipher_supported[NUM_CIPHERS];
2427
2428 const mbedtls_cipher_base_t *mbedtls_cipher_base_lookup_table[] = {
2429 #if defined(MBEDTLS_AES_C)
2430 [MBEDTLS_CIPHER_BASE_INDEX_AES] = &aes_info,
2431 #endif
2432 #if defined(MBEDTLS_ARIA_C)
2433 [MBEDTLS_CIPHER_BASE_INDEX_ARIA] = &aria_info,
2434 #endif
2435 #if defined(MBEDTLS_CAMELLIA_C)
2436 [MBEDTLS_CIPHER_BASE_INDEX_CAMELLIA] = &camellia_info,
2437 #endif
2438 #if defined(MBEDTLS_CIPHER_HAVE_CCM_AES_VIA_LEGACY_OR_USE_PSA)
2439 [MBEDTLS_CIPHER_BASE_INDEX_CCM_AES] = &ccm_aes_info,
2440 #endif
2441 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_ARIA_C)
2442 [MBEDTLS_CIPHER_BASE_INDEX_CCM_ARIA] = &ccm_aria_info,
2443 #endif
2444 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_CAMELLIA_C)
2445 [MBEDTLS_CIPHER_BASE_INDEX_CCM_CAMELLIA] = &ccm_camellia_info,
2446 #endif
2447 #if defined(MBEDTLS_CHACHA20_C)
2448 [MBEDTLS_CIPHER_BASE_INDEX_CHACHA20_BASE] = &chacha20_base_info,
2449 #endif
2450 #if defined(MBEDTLS_CHACHAPOLY_C)
2451 [MBEDTLS_CIPHER_BASE_INDEX_CHACHAPOLY_BASE] = &chachapoly_base_info,
2452 #endif
2453 #if defined(MBEDTLS_DES_C)
2454 [MBEDTLS_CIPHER_BASE_INDEX_DES_EDE3] = &des_ede3_info,
2455 #endif
2456 #if defined(MBEDTLS_DES_C)
2457 [MBEDTLS_CIPHER_BASE_INDEX_DES_EDE] = &des_ede_info,
2458 #endif
2459 #if defined(MBEDTLS_DES_C)
2460 [MBEDTLS_CIPHER_BASE_INDEX_DES] = &des_info,
2461 #endif
2462 #if defined(MBEDTLS_CIPHER_HAVE_GCM_AES_VIA_LEGACY_OR_USE_PSA)
2463 [MBEDTLS_CIPHER_BASE_INDEX_GCM_AES] = &gcm_aes_info,
2464 #endif
2465 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_ARIA_C)
2466 [MBEDTLS_CIPHER_BASE_INDEX_GCM_ARIA] = &gcm_aria_info,
2467 #endif
2468 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_CAMELLIA_C)
2469 [MBEDTLS_CIPHER_BASE_INDEX_GCM_CAMELLIA] = &gcm_camellia_info,
2470 #endif
2471 #if defined(MBEDTLS_NIST_KW_C)
2472 [MBEDTLS_CIPHER_BASE_INDEX_KW_AES] = &kw_aes_info,
2473 #endif
2474 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2475 [MBEDTLS_CIPHER_BASE_INDEX_NULL_BASE] = &null_base_info,
2476 #endif
2477 #if defined(MBEDTLS_CIPHER_MODE_XTS) && defined(MBEDTLS_AES_C)
2478 [MBEDTLS_CIPHER_BASE_INDEX_XTS_AES] = &xts_aes_info
2479 #endif
2480 };
2481
2482 #endif /* MBEDTLS_CIPHER_C */
2483