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