4 * \brief Generic cipher wrapper for mbed TLS
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
9 * SPDX-License-Identifier: Apache-2.0
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * This file is part of mbed TLS (https://tls.mbed.org)
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "mbedtls/config.h"
29 #include MBEDTLS_CONFIG_FILE
32 #if defined(MBEDTLS_CIPHER_C)
34 #include "mbedtls/cipher_internal.h"
36 #if defined(MBEDTLS_CHACHAPOLY_C)
37 #include "mbedtls/chachapoly.h"
40 #if defined(MBEDTLS_AES_C)
41 #include "mbedtls/aes.h"
44 #if defined(MBEDTLS_ARC4_C)
45 #include "mbedtls/arc4.h"
48 #if defined(MBEDTLS_CAMELLIA_C)
49 #include "mbedtls/camellia.h"
52 #if defined(MBEDTLS_ARIA_C)
53 #include "mbedtls/aria.h"
56 #if defined(MBEDTLS_DES_C)
57 #include "mbedtls/des.h"
60 #if defined(MBEDTLS_BLOWFISH_C)
61 #include "mbedtls/blowfish.h"
64 #if defined(MBEDTLS_CHACHA20_C)
65 #include "mbedtls/chacha20.h"
68 #if defined(MBEDTLS_GCM_C)
69 #include "mbedtls/gcm.h"
72 #if defined(MBEDTLS_CCM_C)
73 #include "mbedtls/ccm.h"
76 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
80 #if defined(MBEDTLS_PLATFORM_C)
81 #include "mbedtls/platform.h"
84 #define mbedtls_calloc calloc
85 #define mbedtls_free free
88 #if defined(MBEDTLS_GCM_C)
89 /* shared by all GCM ciphers */
90 static void *gcm_ctx_alloc( void )
92 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
95 mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
100 static void gcm_ctx_free( void *ctx )
102 mbedtls_gcm_free( ctx );
105 #endif /* MBEDTLS_GCM_C */
107 #if defined(MBEDTLS_CCM_C)
108 /* shared by all CCM ciphers */
109 static void *ccm_ctx_alloc( void )
111 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
114 mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
119 static void ccm_ctx_free( void *ctx )
121 mbedtls_ccm_free( ctx );
124 #endif /* MBEDTLS_CCM_C */
126 #if defined(MBEDTLS_AES_C)
128 static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
129 const unsigned char *input, unsigned char *output )
131 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
134 #if defined(MBEDTLS_CIPHER_MODE_CBC)
135 static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
136 unsigned char *iv, const unsigned char *input, unsigned char *output )
138 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
141 #endif /* MBEDTLS_CIPHER_MODE_CBC */
143 #if defined(MBEDTLS_CIPHER_MODE_CFB)
144 static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
145 size_t length, size_t *iv_off, unsigned char *iv,
146 const unsigned char *input, unsigned char *output )
148 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
151 #endif /* MBEDTLS_CIPHER_MODE_CFB */
153 #if defined(MBEDTLS_CIPHER_MODE_OFB)
154 static int aes_crypt_ofb_wrap( void *ctx, size_t length, size_t *iv_off,
155 unsigned char *iv, const unsigned char *input, unsigned char *output )
157 return mbedtls_aes_crypt_ofb( (mbedtls_aes_context *) ctx, length, iv_off,
160 #endif /* MBEDTLS_CIPHER_MODE_OFB */
162 #if defined(MBEDTLS_CIPHER_MODE_CTR)
163 static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
164 unsigned char *nonce_counter, unsigned char *stream_block,
165 const unsigned char *input, unsigned char *output )
167 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
168 stream_block, input, output );
170 #endif /* MBEDTLS_CIPHER_MODE_CTR */
172 #if defined(MBEDTLS_CIPHER_MODE_XTS)
173 static int aes_crypt_xts_wrap( void *ctx, mbedtls_operation_t operation,
175 const unsigned char data_unit[16],
176 const unsigned char *input,
177 unsigned char *output )
179 mbedtls_aes_xts_context *xts_ctx = ctx;
184 case MBEDTLS_ENCRYPT:
185 mode = MBEDTLS_AES_ENCRYPT;
187 case MBEDTLS_DECRYPT:
188 mode = MBEDTLS_AES_DECRYPT;
191 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
194 return mbedtls_aes_crypt_xts( xts_ctx, mode, length,
195 data_unit, input, output );
197 #endif /* MBEDTLS_CIPHER_MODE_XTS */
199 static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
200 unsigned int key_bitlen )
202 return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
205 static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
206 unsigned int key_bitlen )
208 return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
211 static void * aes_ctx_alloc( void )
213 mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
218 mbedtls_aes_init( aes );
223 static void aes_ctx_free( void *ctx )
225 mbedtls_aes_free( (mbedtls_aes_context *) ctx );
229 static const mbedtls_cipher_base_t aes_info = {
230 MBEDTLS_CIPHER_ID_AES,
232 #if defined(MBEDTLS_CIPHER_MODE_CBC)
235 #if defined(MBEDTLS_CIPHER_MODE_CFB)
236 aes_crypt_cfb128_wrap,
238 #if defined(MBEDTLS_CIPHER_MODE_OFB)
241 #if defined(MBEDTLS_CIPHER_MODE_CTR)
244 #if defined(MBEDTLS_CIPHER_MODE_XTS)
247 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
256 static const mbedtls_cipher_info_t aes_128_ecb_info = {
257 MBEDTLS_CIPHER_AES_128_ECB,
267 static const mbedtls_cipher_info_t aes_192_ecb_info = {
268 MBEDTLS_CIPHER_AES_192_ECB,
278 static const mbedtls_cipher_info_t aes_256_ecb_info = {
279 MBEDTLS_CIPHER_AES_256_ECB,
289 #if defined(MBEDTLS_CIPHER_MODE_CBC)
290 static const mbedtls_cipher_info_t aes_128_cbc_info = {
291 MBEDTLS_CIPHER_AES_128_CBC,
301 static const mbedtls_cipher_info_t aes_192_cbc_info = {
302 MBEDTLS_CIPHER_AES_192_CBC,
312 static const mbedtls_cipher_info_t aes_256_cbc_info = {
313 MBEDTLS_CIPHER_AES_256_CBC,
322 #endif /* MBEDTLS_CIPHER_MODE_CBC */
324 #if defined(MBEDTLS_CIPHER_MODE_CFB)
325 static const mbedtls_cipher_info_t aes_128_cfb128_info = {
326 MBEDTLS_CIPHER_AES_128_CFB128,
336 static const mbedtls_cipher_info_t aes_192_cfb128_info = {
337 MBEDTLS_CIPHER_AES_192_CFB128,
347 static const mbedtls_cipher_info_t aes_256_cfb128_info = {
348 MBEDTLS_CIPHER_AES_256_CFB128,
357 #endif /* MBEDTLS_CIPHER_MODE_CFB */
359 #if defined(MBEDTLS_CIPHER_MODE_OFB)
360 static const mbedtls_cipher_info_t aes_128_ofb_info = {
361 MBEDTLS_CIPHER_AES_128_OFB,
371 static const mbedtls_cipher_info_t aes_192_ofb_info = {
372 MBEDTLS_CIPHER_AES_192_OFB,
382 static const mbedtls_cipher_info_t aes_256_ofb_info = {
383 MBEDTLS_CIPHER_AES_256_OFB,
392 #endif /* MBEDTLS_CIPHER_MODE_OFB */
394 #if defined(MBEDTLS_CIPHER_MODE_CTR)
395 static const mbedtls_cipher_info_t aes_128_ctr_info = {
396 MBEDTLS_CIPHER_AES_128_CTR,
406 static const mbedtls_cipher_info_t aes_192_ctr_info = {
407 MBEDTLS_CIPHER_AES_192_CTR,
417 static const mbedtls_cipher_info_t aes_256_ctr_info = {
418 MBEDTLS_CIPHER_AES_256_CTR,
427 #endif /* MBEDTLS_CIPHER_MODE_CTR */
429 #if defined(MBEDTLS_CIPHER_MODE_XTS)
430 static int xts_aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
431 unsigned int key_bitlen )
433 mbedtls_aes_xts_context *xts_ctx = ctx;
434 return( mbedtls_aes_xts_setkey_enc( xts_ctx, key, key_bitlen ) );
437 static int xts_aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
438 unsigned int key_bitlen )
440 mbedtls_aes_xts_context *xts_ctx = ctx;
441 return( mbedtls_aes_xts_setkey_dec( xts_ctx, key, key_bitlen ) );
444 static void *xts_aes_ctx_alloc( void )
446 mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc( 1, sizeof( *xts_ctx ) );
448 if( xts_ctx != NULL )
449 mbedtls_aes_xts_init( xts_ctx );
454 static void xts_aes_ctx_free( void *ctx )
456 mbedtls_aes_xts_context *xts_ctx = ctx;
458 if( xts_ctx == NULL )
461 mbedtls_aes_xts_free( xts_ctx );
462 mbedtls_free( xts_ctx );
465 static const mbedtls_cipher_base_t xts_aes_info = {
466 MBEDTLS_CIPHER_ID_AES,
468 #if defined(MBEDTLS_CIPHER_MODE_CBC)
471 #if defined(MBEDTLS_CIPHER_MODE_CFB)
474 #if defined(MBEDTLS_CIPHER_MODE_OFB)
477 #if defined(MBEDTLS_CIPHER_MODE_CTR)
480 #if defined(MBEDTLS_CIPHER_MODE_XTS)
483 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
486 xts_aes_setkey_enc_wrap,
487 xts_aes_setkey_dec_wrap,
492 static const mbedtls_cipher_info_t aes_128_xts_info = {
493 MBEDTLS_CIPHER_AES_128_XTS,
503 static const mbedtls_cipher_info_t aes_256_xts_info = {
504 MBEDTLS_CIPHER_AES_256_XTS,
513 #endif /* MBEDTLS_CIPHER_MODE_XTS */
515 #if defined(MBEDTLS_GCM_C)
516 static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
517 unsigned int key_bitlen )
519 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
523 static const mbedtls_cipher_base_t gcm_aes_info = {
524 MBEDTLS_CIPHER_ID_AES,
526 #if defined(MBEDTLS_CIPHER_MODE_CBC)
529 #if defined(MBEDTLS_CIPHER_MODE_CFB)
532 #if defined(MBEDTLS_CIPHER_MODE_OFB)
535 #if defined(MBEDTLS_CIPHER_MODE_CTR)
538 #if defined(MBEDTLS_CIPHER_MODE_XTS)
541 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
550 static const mbedtls_cipher_info_t aes_128_gcm_info = {
551 MBEDTLS_CIPHER_AES_128_GCM,
556 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
561 static const mbedtls_cipher_info_t aes_192_gcm_info = {
562 MBEDTLS_CIPHER_AES_192_GCM,
567 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
572 static const mbedtls_cipher_info_t aes_256_gcm_info = {
573 MBEDTLS_CIPHER_AES_256_GCM,
578 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
582 #endif /* MBEDTLS_GCM_C */
584 #if defined(MBEDTLS_CCM_C)
585 static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
586 unsigned int key_bitlen )
588 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
592 static const mbedtls_cipher_base_t ccm_aes_info = {
593 MBEDTLS_CIPHER_ID_AES,
595 #if defined(MBEDTLS_CIPHER_MODE_CBC)
598 #if defined(MBEDTLS_CIPHER_MODE_CFB)
601 #if defined(MBEDTLS_CIPHER_MODE_OFB)
604 #if defined(MBEDTLS_CIPHER_MODE_CTR)
607 #if defined(MBEDTLS_CIPHER_MODE_XTS)
610 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
619 static const mbedtls_cipher_info_t aes_128_ccm_info = {
620 MBEDTLS_CIPHER_AES_128_CCM,
625 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
630 static const mbedtls_cipher_info_t aes_192_ccm_info = {
631 MBEDTLS_CIPHER_AES_192_CCM,
636 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
641 static const mbedtls_cipher_info_t aes_256_ccm_info = {
642 MBEDTLS_CIPHER_AES_256_CCM,
647 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
651 #endif /* MBEDTLS_CCM_C */
653 #endif /* MBEDTLS_AES_C */
655 #if defined(MBEDTLS_CAMELLIA_C)
657 static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
658 const unsigned char *input, unsigned char *output )
660 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
664 #if defined(MBEDTLS_CIPHER_MODE_CBC)
665 static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
666 size_t length, unsigned char *iv,
667 const unsigned char *input, unsigned char *output )
669 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
672 #endif /* MBEDTLS_CIPHER_MODE_CBC */
674 #if defined(MBEDTLS_CIPHER_MODE_CFB)
675 static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
676 size_t length, size_t *iv_off, unsigned char *iv,
677 const unsigned char *input, unsigned char *output )
679 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
680 iv_off, iv, input, output );
682 #endif /* MBEDTLS_CIPHER_MODE_CFB */
684 #if defined(MBEDTLS_CIPHER_MODE_CTR)
685 static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
686 unsigned char *nonce_counter, unsigned char *stream_block,
687 const unsigned char *input, unsigned char *output )
689 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
690 nonce_counter, stream_block, input, output );
692 #endif /* MBEDTLS_CIPHER_MODE_CTR */
694 static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
695 unsigned int key_bitlen )
697 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
700 static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
701 unsigned int key_bitlen )
703 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
706 static void * camellia_ctx_alloc( void )
708 mbedtls_camellia_context *ctx;
709 ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
714 mbedtls_camellia_init( ctx );
719 static void camellia_ctx_free( void *ctx )
721 mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
725 static const mbedtls_cipher_base_t camellia_info = {
726 MBEDTLS_CIPHER_ID_CAMELLIA,
727 camellia_crypt_ecb_wrap,
728 #if defined(MBEDTLS_CIPHER_MODE_CBC)
729 camellia_crypt_cbc_wrap,
731 #if defined(MBEDTLS_CIPHER_MODE_CFB)
732 camellia_crypt_cfb128_wrap,
734 #if defined(MBEDTLS_CIPHER_MODE_OFB)
737 #if defined(MBEDTLS_CIPHER_MODE_CTR)
738 camellia_crypt_ctr_wrap,
740 #if defined(MBEDTLS_CIPHER_MODE_XTS)
743 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
746 camellia_setkey_enc_wrap,
747 camellia_setkey_dec_wrap,
752 static const mbedtls_cipher_info_t camellia_128_ecb_info = {
753 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
763 static const mbedtls_cipher_info_t camellia_192_ecb_info = {
764 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
774 static const mbedtls_cipher_info_t camellia_256_ecb_info = {
775 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
785 #if defined(MBEDTLS_CIPHER_MODE_CBC)
786 static const mbedtls_cipher_info_t camellia_128_cbc_info = {
787 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
797 static const mbedtls_cipher_info_t camellia_192_cbc_info = {
798 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
808 static const mbedtls_cipher_info_t camellia_256_cbc_info = {
809 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
818 #endif /* MBEDTLS_CIPHER_MODE_CBC */
820 #if defined(MBEDTLS_CIPHER_MODE_CFB)
821 static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
822 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
825 "CAMELLIA-128-CFB128",
832 static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
833 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
836 "CAMELLIA-192-CFB128",
843 static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
844 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
847 "CAMELLIA-256-CFB128",
853 #endif /* MBEDTLS_CIPHER_MODE_CFB */
855 #if defined(MBEDTLS_CIPHER_MODE_CTR)
856 static const mbedtls_cipher_info_t camellia_128_ctr_info = {
857 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
867 static const mbedtls_cipher_info_t camellia_192_ctr_info = {
868 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
878 static const mbedtls_cipher_info_t camellia_256_ctr_info = {
879 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
888 #endif /* MBEDTLS_CIPHER_MODE_CTR */
890 #if defined(MBEDTLS_GCM_C)
891 static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
892 unsigned int key_bitlen )
894 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
898 static const mbedtls_cipher_base_t gcm_camellia_info = {
899 MBEDTLS_CIPHER_ID_CAMELLIA,
901 #if defined(MBEDTLS_CIPHER_MODE_CBC)
904 #if defined(MBEDTLS_CIPHER_MODE_CFB)
907 #if defined(MBEDTLS_CIPHER_MODE_OFB)
910 #if defined(MBEDTLS_CIPHER_MODE_CTR)
913 #if defined(MBEDTLS_CIPHER_MODE_XTS)
916 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
919 gcm_camellia_setkey_wrap,
920 gcm_camellia_setkey_wrap,
925 static const mbedtls_cipher_info_t camellia_128_gcm_info = {
926 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
931 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
936 static const mbedtls_cipher_info_t camellia_192_gcm_info = {
937 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
942 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
947 static const mbedtls_cipher_info_t camellia_256_gcm_info = {
948 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
953 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
957 #endif /* MBEDTLS_GCM_C */
959 #if defined(MBEDTLS_CCM_C)
960 static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
961 unsigned int key_bitlen )
963 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
967 static const mbedtls_cipher_base_t ccm_camellia_info = {
968 MBEDTLS_CIPHER_ID_CAMELLIA,
970 #if defined(MBEDTLS_CIPHER_MODE_CBC)
973 #if defined(MBEDTLS_CIPHER_MODE_CFB)
976 #if defined(MBEDTLS_CIPHER_MODE_OFB)
979 #if defined(MBEDTLS_CIPHER_MODE_CTR)
982 #if defined(MBEDTLS_CIPHER_MODE_XTS)
985 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
988 ccm_camellia_setkey_wrap,
989 ccm_camellia_setkey_wrap,
994 static const mbedtls_cipher_info_t camellia_128_ccm_info = {
995 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
1000 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1005 static const mbedtls_cipher_info_t camellia_192_ccm_info = {
1006 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
1011 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1016 static const mbedtls_cipher_info_t camellia_256_ccm_info = {
1017 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
1022 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1026 #endif /* MBEDTLS_CCM_C */
1028 #endif /* MBEDTLS_CAMELLIA_C */
1030 #if defined(MBEDTLS_ARIA_C)
1032 static int aria_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
1033 const unsigned char *input, unsigned char *output )
1036 return mbedtls_aria_crypt_ecb( (mbedtls_aria_context *) ctx, input,
1040 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1041 static int aria_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
1042 size_t length, unsigned char *iv,
1043 const unsigned char *input, unsigned char *output )
1045 return mbedtls_aria_crypt_cbc( (mbedtls_aria_context *) ctx, operation, length, iv,
1048 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1050 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1051 static int aria_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
1052 size_t length, size_t *iv_off, unsigned char *iv,
1053 const unsigned char *input, unsigned char *output )
1055 return mbedtls_aria_crypt_cfb128( (mbedtls_aria_context *) ctx, operation, length,
1056 iv_off, iv, input, output );
1058 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1060 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1061 static int aria_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1062 unsigned char *nonce_counter, unsigned char *stream_block,
1063 const unsigned char *input, unsigned char *output )
1065 return mbedtls_aria_crypt_ctr( (mbedtls_aria_context *) ctx, length, nc_off,
1066 nonce_counter, stream_block, input, output );
1068 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1070 static int aria_setkey_dec_wrap( void *ctx, const unsigned char *key,
1071 unsigned int key_bitlen )
1073 return mbedtls_aria_setkey_dec( (mbedtls_aria_context *) ctx, key, key_bitlen );
1076 static int aria_setkey_enc_wrap( void *ctx, const unsigned char *key,
1077 unsigned int key_bitlen )
1079 return mbedtls_aria_setkey_enc( (mbedtls_aria_context *) ctx, key, key_bitlen );
1082 static void * aria_ctx_alloc( void )
1084 mbedtls_aria_context *ctx;
1085 ctx = mbedtls_calloc( 1, sizeof( mbedtls_aria_context ) );
1090 mbedtls_aria_init( ctx );
1095 static void aria_ctx_free( void *ctx )
1097 mbedtls_aria_free( (mbedtls_aria_context *) ctx );
1098 mbedtls_free( ctx );
1101 static const mbedtls_cipher_base_t aria_info = {
1102 MBEDTLS_CIPHER_ID_ARIA,
1103 aria_crypt_ecb_wrap,
1104 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1105 aria_crypt_cbc_wrap,
1107 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1108 aria_crypt_cfb128_wrap,
1110 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1113 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1114 aria_crypt_ctr_wrap,
1116 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1119 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1122 aria_setkey_enc_wrap,
1123 aria_setkey_dec_wrap,
1128 static const mbedtls_cipher_info_t aria_128_ecb_info = {
1129 MBEDTLS_CIPHER_ARIA_128_ECB,
1139 static const mbedtls_cipher_info_t aria_192_ecb_info = {
1140 MBEDTLS_CIPHER_ARIA_192_ECB,
1150 static const mbedtls_cipher_info_t aria_256_ecb_info = {
1151 MBEDTLS_CIPHER_ARIA_256_ECB,
1161 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1162 static const mbedtls_cipher_info_t aria_128_cbc_info = {
1163 MBEDTLS_CIPHER_ARIA_128_CBC,
1173 static const mbedtls_cipher_info_t aria_192_cbc_info = {
1174 MBEDTLS_CIPHER_ARIA_192_CBC,
1184 static const mbedtls_cipher_info_t aria_256_cbc_info = {
1185 MBEDTLS_CIPHER_ARIA_256_CBC,
1194 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1196 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1197 static const mbedtls_cipher_info_t aria_128_cfb128_info = {
1198 MBEDTLS_CIPHER_ARIA_128_CFB128,
1208 static const mbedtls_cipher_info_t aria_192_cfb128_info = {
1209 MBEDTLS_CIPHER_ARIA_192_CFB128,
1219 static const mbedtls_cipher_info_t aria_256_cfb128_info = {
1220 MBEDTLS_CIPHER_ARIA_256_CFB128,
1229 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1231 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1232 static const mbedtls_cipher_info_t aria_128_ctr_info = {
1233 MBEDTLS_CIPHER_ARIA_128_CTR,
1243 static const mbedtls_cipher_info_t aria_192_ctr_info = {
1244 MBEDTLS_CIPHER_ARIA_192_CTR,
1254 static const mbedtls_cipher_info_t aria_256_ctr_info = {
1255 MBEDTLS_CIPHER_ARIA_256_CTR,
1264 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1266 #if defined(MBEDTLS_GCM_C)
1267 static int gcm_aria_setkey_wrap( void *ctx, const unsigned char *key,
1268 unsigned int key_bitlen )
1270 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1274 static const mbedtls_cipher_base_t gcm_aria_info = {
1275 MBEDTLS_CIPHER_ID_ARIA,
1277 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1280 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1283 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1286 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1289 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1292 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1295 gcm_aria_setkey_wrap,
1296 gcm_aria_setkey_wrap,
1301 static const mbedtls_cipher_info_t aria_128_gcm_info = {
1302 MBEDTLS_CIPHER_ARIA_128_GCM,
1307 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1312 static const mbedtls_cipher_info_t aria_192_gcm_info = {
1313 MBEDTLS_CIPHER_ARIA_192_GCM,
1318 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1323 static const mbedtls_cipher_info_t aria_256_gcm_info = {
1324 MBEDTLS_CIPHER_ARIA_256_GCM,
1329 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1333 #endif /* MBEDTLS_GCM_C */
1335 #if defined(MBEDTLS_CCM_C)
1336 static int ccm_aria_setkey_wrap( void *ctx, const unsigned char *key,
1337 unsigned int key_bitlen )
1339 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1343 static const mbedtls_cipher_base_t ccm_aria_info = {
1344 MBEDTLS_CIPHER_ID_ARIA,
1346 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1349 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1352 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1355 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1358 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1361 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1364 ccm_aria_setkey_wrap,
1365 ccm_aria_setkey_wrap,
1370 static const mbedtls_cipher_info_t aria_128_ccm_info = {
1371 MBEDTLS_CIPHER_ARIA_128_CCM,
1376 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1381 static const mbedtls_cipher_info_t aria_192_ccm_info = {
1382 MBEDTLS_CIPHER_ARIA_192_CCM,
1387 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1392 static const mbedtls_cipher_info_t aria_256_ccm_info = {
1393 MBEDTLS_CIPHER_ARIA_256_CCM,
1398 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1402 #endif /* MBEDTLS_CCM_C */
1404 #endif /* MBEDTLS_ARIA_C */
1406 #if defined(MBEDTLS_DES_C)
1408 static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
1409 const unsigned char *input, unsigned char *output )
1412 return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
1415 static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
1416 const unsigned char *input, unsigned char *output )
1419 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
1422 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1423 static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
1424 unsigned char *iv, const unsigned char *input, unsigned char *output )
1426 return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
1429 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1431 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1432 static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
1433 unsigned char *iv, const unsigned char *input, unsigned char *output )
1435 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
1438 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1440 static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
1441 unsigned int key_bitlen )
1443 ((void) key_bitlen);
1445 return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
1448 static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
1449 unsigned int key_bitlen )
1451 ((void) key_bitlen);
1453 return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
1456 static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
1457 unsigned int key_bitlen )
1459 ((void) key_bitlen);
1461 return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
1464 static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
1465 unsigned int key_bitlen )
1467 ((void) key_bitlen);
1469 return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
1472 static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
1473 unsigned int key_bitlen )
1475 ((void) key_bitlen);
1477 return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
1480 static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
1481 unsigned int key_bitlen )
1483 ((void) key_bitlen);
1485 return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
1488 static void * des_ctx_alloc( void )
1490 mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
1495 mbedtls_des_init( des );
1500 static void des_ctx_free( void *ctx )
1502 mbedtls_des_free( (mbedtls_des_context *) ctx );
1503 mbedtls_free( ctx );
1506 static void * des3_ctx_alloc( void )
1508 mbedtls_des3_context *des3;
1509 des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
1514 mbedtls_des3_init( des3 );
1519 static void des3_ctx_free( void *ctx )
1521 mbedtls_des3_free( (mbedtls_des3_context *) ctx );
1522 mbedtls_free( ctx );
1525 static const mbedtls_cipher_base_t des_info = {
1526 MBEDTLS_CIPHER_ID_DES,
1528 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1531 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1534 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1537 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1540 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1543 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1546 des_setkey_enc_wrap,
1547 des_setkey_dec_wrap,
1552 static const mbedtls_cipher_info_t des_ecb_info = {
1553 MBEDTLS_CIPHER_DES_ECB,
1555 MBEDTLS_KEY_LENGTH_DES,
1563 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1564 static const mbedtls_cipher_info_t des_cbc_info = {
1565 MBEDTLS_CIPHER_DES_CBC,
1567 MBEDTLS_KEY_LENGTH_DES,
1574 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1576 static const mbedtls_cipher_base_t des_ede_info = {
1577 MBEDTLS_CIPHER_ID_DES,
1578 des3_crypt_ecb_wrap,
1579 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1580 des3_crypt_cbc_wrap,
1582 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1585 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1588 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1591 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1594 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1597 des3_set2key_enc_wrap,
1598 des3_set2key_dec_wrap,
1603 static const mbedtls_cipher_info_t des_ede_ecb_info = {
1604 MBEDTLS_CIPHER_DES_EDE_ECB,
1606 MBEDTLS_KEY_LENGTH_DES_EDE,
1614 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1615 static const mbedtls_cipher_info_t des_ede_cbc_info = {
1616 MBEDTLS_CIPHER_DES_EDE_CBC,
1618 MBEDTLS_KEY_LENGTH_DES_EDE,
1625 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1627 static const mbedtls_cipher_base_t des_ede3_info = {
1628 MBEDTLS_CIPHER_ID_3DES,
1629 des3_crypt_ecb_wrap,
1630 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1631 des3_crypt_cbc_wrap,
1633 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1636 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1639 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1642 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1645 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1648 des3_set3key_enc_wrap,
1649 des3_set3key_dec_wrap,
1654 static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1655 MBEDTLS_CIPHER_DES_EDE3_ECB,
1657 MBEDTLS_KEY_LENGTH_DES_EDE3,
1664 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1665 static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1666 MBEDTLS_CIPHER_DES_EDE3_CBC,
1668 MBEDTLS_KEY_LENGTH_DES_EDE3,
1675 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1676 #endif /* MBEDTLS_DES_C */
1678 #if defined(MBEDTLS_BLOWFISH_C)
1680 static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
1681 const unsigned char *input, unsigned char *output )
1683 return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
1687 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1688 static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
1689 size_t length, unsigned char *iv, const unsigned char *input,
1690 unsigned char *output )
1692 return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
1695 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1697 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1698 static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
1699 size_t length, size_t *iv_off, unsigned char *iv,
1700 const unsigned char *input, unsigned char *output )
1702 return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
1703 iv_off, iv, input, output );
1705 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1707 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1708 static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1709 unsigned char *nonce_counter, unsigned char *stream_block,
1710 const unsigned char *input, unsigned char *output )
1712 return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
1713 nonce_counter, stream_block, input, output );
1715 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1717 static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
1718 unsigned int key_bitlen )
1720 return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
1723 static void * blowfish_ctx_alloc( void )
1725 mbedtls_blowfish_context *ctx;
1726 ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
1731 mbedtls_blowfish_init( ctx );
1736 static void blowfish_ctx_free( void *ctx )
1738 mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
1739 mbedtls_free( ctx );
1742 static const mbedtls_cipher_base_t blowfish_info = {
1743 MBEDTLS_CIPHER_ID_BLOWFISH,
1744 blowfish_crypt_ecb_wrap,
1745 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1746 blowfish_crypt_cbc_wrap,
1748 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1749 blowfish_crypt_cfb64_wrap,
1751 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1754 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1755 blowfish_crypt_ctr_wrap,
1757 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1760 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1763 blowfish_setkey_wrap,
1764 blowfish_setkey_wrap,
1769 static const mbedtls_cipher_info_t blowfish_ecb_info = {
1770 MBEDTLS_CIPHER_BLOWFISH_ECB,
1775 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1780 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1781 static const mbedtls_cipher_info_t blowfish_cbc_info = {
1782 MBEDTLS_CIPHER_BLOWFISH_CBC,
1787 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1791 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1793 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1794 static const mbedtls_cipher_info_t blowfish_cfb64_info = {
1795 MBEDTLS_CIPHER_BLOWFISH_CFB64,
1800 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1804 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1806 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1807 static const mbedtls_cipher_info_t blowfish_ctr_info = {
1808 MBEDTLS_CIPHER_BLOWFISH_CTR,
1813 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1817 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1818 #endif /* MBEDTLS_BLOWFISH_C */
1820 #if defined(MBEDTLS_ARC4_C)
1821 static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1822 const unsigned char *input,
1823 unsigned char *output )
1825 return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
1828 static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
1829 unsigned int key_bitlen )
1831 /* we get key_bitlen in bits, arc4 expects it in bytes */
1832 if( key_bitlen % 8 != 0 )
1833 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1835 mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
1839 static void * arc4_ctx_alloc( void )
1841 mbedtls_arc4_context *ctx;
1842 ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
1847 mbedtls_arc4_init( ctx );
1852 static void arc4_ctx_free( void *ctx )
1854 mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
1855 mbedtls_free( ctx );
1858 static const mbedtls_cipher_base_t arc4_base_info = {
1859 MBEDTLS_CIPHER_ID_ARC4,
1861 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1864 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1867 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1870 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1873 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1876 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1877 arc4_crypt_stream_wrap,
1885 static const mbedtls_cipher_info_t arc4_128_info = {
1886 MBEDTLS_CIPHER_ARC4_128,
1887 MBEDTLS_MODE_STREAM,
1895 #endif /* MBEDTLS_ARC4_C */
1897 #if defined(MBEDTLS_CHACHA20_C)
1899 static int chacha20_setkey_wrap( void *ctx, const unsigned char *key,
1900 unsigned int key_bitlen )
1902 if( key_bitlen != 256U )
1903 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1905 if ( 0 != mbedtls_chacha20_setkey( (mbedtls_chacha20_context*)ctx, key ) )
1906 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1911 static int chacha20_stream_wrap( void *ctx, size_t length,
1912 const unsigned char *input,
1913 unsigned char *output )
1917 ret = mbedtls_chacha20_update( ctx, length, input, output );
1918 if( ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA )
1919 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1924 static void * chacha20_ctx_alloc( void )
1926 mbedtls_chacha20_context *ctx;
1927 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chacha20_context ) );
1932 mbedtls_chacha20_init( ctx );
1937 static void chacha20_ctx_free( void *ctx )
1939 mbedtls_chacha20_free( (mbedtls_chacha20_context *) ctx );
1940 mbedtls_free( ctx );
1943 static const mbedtls_cipher_base_t chacha20_base_info = {
1944 MBEDTLS_CIPHER_ID_CHACHA20,
1946 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1949 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1952 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1955 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1958 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1961 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1962 chacha20_stream_wrap,
1964 chacha20_setkey_wrap,
1965 chacha20_setkey_wrap,
1969 static const mbedtls_cipher_info_t chacha20_info = {
1970 MBEDTLS_CIPHER_CHACHA20,
1971 MBEDTLS_MODE_STREAM,
1979 #endif /* MBEDTLS_CHACHA20_C */
1981 #if defined(MBEDTLS_CHACHAPOLY_C)
1983 static int chachapoly_setkey_wrap( void *ctx,
1984 const unsigned char *key,
1985 unsigned int key_bitlen )
1987 if( key_bitlen != 256U )
1988 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1990 if ( 0 != mbedtls_chachapoly_setkey( (mbedtls_chachapoly_context*)ctx, key ) )
1991 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1996 static void * chachapoly_ctx_alloc( void )
1998 mbedtls_chachapoly_context *ctx;
1999 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chachapoly_context ) );
2004 mbedtls_chachapoly_init( ctx );
2009 static void chachapoly_ctx_free( void *ctx )
2011 mbedtls_chachapoly_free( (mbedtls_chachapoly_context *) ctx );
2012 mbedtls_free( ctx );
2015 static const mbedtls_cipher_base_t chachapoly_base_info = {
2016 MBEDTLS_CIPHER_ID_CHACHA20,
2018 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2021 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2024 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2027 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2030 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2033 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
2036 chachapoly_setkey_wrap,
2037 chachapoly_setkey_wrap,
2038 chachapoly_ctx_alloc,
2041 static const mbedtls_cipher_info_t chachapoly_info = {
2042 MBEDTLS_CIPHER_CHACHA20_POLY1305,
2043 MBEDTLS_MODE_CHACHAPOLY,
2045 "CHACHA20-POLY1305",
2049 &chachapoly_base_info
2051 #endif /* MBEDTLS_CHACHAPOLY_C */
2053 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2054 static int null_crypt_stream( void *ctx, size_t length,
2055 const unsigned char *input,
2056 unsigned char *output )
2059 memmove( output, input, length );
2063 static int null_setkey( void *ctx, const unsigned char *key,
2064 unsigned int key_bitlen )
2068 ((void) key_bitlen);
2073 static void * null_ctx_alloc( void )
2075 return( (void *) 1 );
2078 static void null_ctx_free( void *ctx )
2083 static const mbedtls_cipher_base_t null_base_info = {
2084 MBEDTLS_CIPHER_ID_NULL,
2086 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2089 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2092 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2095 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2098 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2101 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
2110 static const mbedtls_cipher_info_t null_cipher_info = {
2111 MBEDTLS_CIPHER_NULL,
2112 MBEDTLS_MODE_STREAM,
2120 #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
2122 const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
2124 #if defined(MBEDTLS_AES_C)
2125 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
2126 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
2127 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
2128 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2129 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
2130 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
2131 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
2133 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2134 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
2135 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
2136 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
2138 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2139 { MBEDTLS_CIPHER_AES_128_OFB, &aes_128_ofb_info },
2140 { MBEDTLS_CIPHER_AES_192_OFB, &aes_192_ofb_info },
2141 { MBEDTLS_CIPHER_AES_256_OFB, &aes_256_ofb_info },
2143 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2144 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
2145 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
2146 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
2148 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2149 { MBEDTLS_CIPHER_AES_128_XTS, &aes_128_xts_info },
2150 { MBEDTLS_CIPHER_AES_256_XTS, &aes_256_xts_info },
2152 #if defined(MBEDTLS_GCM_C)
2153 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
2154 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
2155 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
2157 #if defined(MBEDTLS_CCM_C)
2158 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
2159 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
2160 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
2162 #endif /* MBEDTLS_AES_C */
2164 #if defined(MBEDTLS_ARC4_C)
2165 { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
2168 #if defined(MBEDTLS_BLOWFISH_C)
2169 { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
2170 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2171 { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
2173 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2174 { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
2176 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2177 { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
2179 #endif /* MBEDTLS_BLOWFISH_C */
2181 #if defined(MBEDTLS_CAMELLIA_C)
2182 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
2183 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
2184 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
2185 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2186 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
2187 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
2188 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
2190 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2191 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
2192 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
2193 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
2195 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2196 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
2197 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
2198 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
2200 #if defined(MBEDTLS_GCM_C)
2201 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
2202 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
2203 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
2205 #if defined(MBEDTLS_CCM_C)
2206 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
2207 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
2208 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
2210 #endif /* MBEDTLS_CAMELLIA_C */
2212 #if defined(MBEDTLS_ARIA_C)
2213 { MBEDTLS_CIPHER_ARIA_128_ECB, &aria_128_ecb_info },
2214 { MBEDTLS_CIPHER_ARIA_192_ECB, &aria_192_ecb_info },
2215 { MBEDTLS_CIPHER_ARIA_256_ECB, &aria_256_ecb_info },
2216 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2217 { MBEDTLS_CIPHER_ARIA_128_CBC, &aria_128_cbc_info },
2218 { MBEDTLS_CIPHER_ARIA_192_CBC, &aria_192_cbc_info },
2219 { MBEDTLS_CIPHER_ARIA_256_CBC, &aria_256_cbc_info },
2221 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2222 { MBEDTLS_CIPHER_ARIA_128_CFB128, &aria_128_cfb128_info },
2223 { MBEDTLS_CIPHER_ARIA_192_CFB128, &aria_192_cfb128_info },
2224 { MBEDTLS_CIPHER_ARIA_256_CFB128, &aria_256_cfb128_info },
2226 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2227 { MBEDTLS_CIPHER_ARIA_128_CTR, &aria_128_ctr_info },
2228 { MBEDTLS_CIPHER_ARIA_192_CTR, &aria_192_ctr_info },
2229 { MBEDTLS_CIPHER_ARIA_256_CTR, &aria_256_ctr_info },
2231 #if defined(MBEDTLS_GCM_C)
2232 { MBEDTLS_CIPHER_ARIA_128_GCM, &aria_128_gcm_info },
2233 { MBEDTLS_CIPHER_ARIA_192_GCM, &aria_192_gcm_info },
2234 { MBEDTLS_CIPHER_ARIA_256_GCM, &aria_256_gcm_info },
2236 #if defined(MBEDTLS_CCM_C)
2237 { MBEDTLS_CIPHER_ARIA_128_CCM, &aria_128_ccm_info },
2238 { MBEDTLS_CIPHER_ARIA_192_CCM, &aria_192_ccm_info },
2239 { MBEDTLS_CIPHER_ARIA_256_CCM, &aria_256_ccm_info },
2241 #endif /* MBEDTLS_ARIA_C */
2243 #if defined(MBEDTLS_DES_C)
2244 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
2245 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
2246 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
2247 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2248 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
2249 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
2250 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
2252 #endif /* MBEDTLS_DES_C */
2254 #if defined(MBEDTLS_CHACHA20_C)
2255 { MBEDTLS_CIPHER_CHACHA20, &chacha20_info },
2258 #if defined(MBEDTLS_CHACHAPOLY_C)
2259 { MBEDTLS_CIPHER_CHACHA20_POLY1305, &chachapoly_info },
2262 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2263 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
2264 #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
2266 { MBEDTLS_CIPHER_NONE, NULL }
2269 #define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
2270 int mbedtls_cipher_supported[NUM_CIPHERS];
2272 #endif /* MBEDTLS_CIPHER_C */