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