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