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