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