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