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