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