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