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_AES_C)
37 #include "mbedtls/aes.h"
40 #if defined(MBEDTLS_ARC4_C)
41 #include "mbedtls/arc4.h"
44 #if defined(MBEDTLS_CAMELLIA_C)
45 #include "mbedtls/camellia.h"
48 #if defined(MBEDTLS_DES_C)
49 #include "mbedtls/des.h"
52 #if defined(MBEDTLS_BLOWFISH_C)
53 #include "mbedtls/blowfish.h"
56 #if defined(MBEDTLS_GCM_C)
57 #include "mbedtls/gcm.h"
60 #if defined(MBEDTLS_CCM_C)
61 #include "mbedtls/ccm.h"
64 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
68 #if defined(MBEDTLS_PLATFORM_C)
69 #include "mbedtls/platform.h"
72 #define mbedtls_calloc calloc
73 #define mbedtls_free free
76 #if defined(MBEDTLS_GCM_C)
77 /* shared by all GCM ciphers */
78 static void *gcm_ctx_alloc( void )
80 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
83 mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
88 static void gcm_ctx_free( void *ctx )
90 mbedtls_gcm_free( ctx );
93 #endif /* MBEDTLS_GCM_C */
95 #if defined(MBEDTLS_CCM_C)
96 /* shared by all CCM ciphers */
97 static void *ccm_ctx_alloc( void )
99 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
102 mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
107 static void ccm_ctx_free( void *ctx )
109 mbedtls_ccm_free( ctx );
112 #endif /* MBEDTLS_CCM_C */
114 #if defined(MBEDTLS_AES_C)
116 static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
117 const unsigned char *input, unsigned char *output )
119 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
122 #if defined(MBEDTLS_CIPHER_MODE_CBC)
123 static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
124 unsigned char *iv, const unsigned char *input, unsigned char *output )
126 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
129 #endif /* MBEDTLS_CIPHER_MODE_CBC */
131 #if defined(MBEDTLS_CIPHER_MODE_CFB)
132 static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
133 size_t length, size_t *iv_off, unsigned char *iv,
134 const unsigned char *input, unsigned char *output )
136 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
139 #endif /* MBEDTLS_CIPHER_MODE_CFB */
141 #if defined(MBEDTLS_CIPHER_MODE_CTR)
142 static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
143 unsigned char *nonce_counter, unsigned char *stream_block,
144 const unsigned char *input, unsigned char *output )
146 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
147 stream_block, input, output );
149 #endif /* MBEDTLS_CIPHER_MODE_CTR */
151 static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
152 unsigned int key_bitlen )
154 return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
157 static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
158 unsigned int key_bitlen )
160 return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
163 static void * aes_ctx_alloc( void )
165 mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
170 mbedtls_aes_init( aes );
175 static void aes_ctx_free( void *ctx )
177 mbedtls_aes_free( (mbedtls_aes_context *) ctx );
181 static const mbedtls_cipher_base_t aes_info = {
182 MBEDTLS_CIPHER_ID_AES,
184 #if defined(MBEDTLS_CIPHER_MODE_CBC)
187 #if defined(MBEDTLS_CIPHER_MODE_CFB)
188 aes_crypt_cfb128_wrap,
190 #if defined(MBEDTLS_CIPHER_MODE_CTR)
193 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
202 static const mbedtls_cipher_info_t aes_128_ecb_info = {
203 MBEDTLS_CIPHER_AES_128_ECB,
213 static const mbedtls_cipher_info_t aes_192_ecb_info = {
214 MBEDTLS_CIPHER_AES_192_ECB,
224 static const mbedtls_cipher_info_t aes_256_ecb_info = {
225 MBEDTLS_CIPHER_AES_256_ECB,
235 #if defined(MBEDTLS_CIPHER_MODE_CBC)
236 static const mbedtls_cipher_info_t aes_128_cbc_info = {
237 MBEDTLS_CIPHER_AES_128_CBC,
247 static const mbedtls_cipher_info_t aes_192_cbc_info = {
248 MBEDTLS_CIPHER_AES_192_CBC,
258 static const mbedtls_cipher_info_t aes_256_cbc_info = {
259 MBEDTLS_CIPHER_AES_256_CBC,
268 #endif /* MBEDTLS_CIPHER_MODE_CBC */
270 #if defined(MBEDTLS_CIPHER_MODE_CFB)
271 static const mbedtls_cipher_info_t aes_128_cfb128_info = {
272 MBEDTLS_CIPHER_AES_128_CFB128,
282 static const mbedtls_cipher_info_t aes_192_cfb128_info = {
283 MBEDTLS_CIPHER_AES_192_CFB128,
293 static const mbedtls_cipher_info_t aes_256_cfb128_info = {
294 MBEDTLS_CIPHER_AES_256_CFB128,
303 #endif /* MBEDTLS_CIPHER_MODE_CFB */
305 #if defined(MBEDTLS_CIPHER_MODE_CTR)
306 static const mbedtls_cipher_info_t aes_128_ctr_info = {
307 MBEDTLS_CIPHER_AES_128_CTR,
317 static const mbedtls_cipher_info_t aes_192_ctr_info = {
318 MBEDTLS_CIPHER_AES_192_CTR,
328 static const mbedtls_cipher_info_t aes_256_ctr_info = {
329 MBEDTLS_CIPHER_AES_256_CTR,
338 #endif /* MBEDTLS_CIPHER_MODE_CTR */
340 #if defined(MBEDTLS_GCM_C)
341 static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
342 unsigned int key_bitlen )
344 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
348 static const mbedtls_cipher_base_t gcm_aes_info = {
349 MBEDTLS_CIPHER_ID_AES,
351 #if defined(MBEDTLS_CIPHER_MODE_CBC)
354 #if defined(MBEDTLS_CIPHER_MODE_CFB)
357 #if defined(MBEDTLS_CIPHER_MODE_CTR)
360 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
369 static const mbedtls_cipher_info_t aes_128_gcm_info = {
370 MBEDTLS_CIPHER_AES_128_GCM,
375 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
380 static const mbedtls_cipher_info_t aes_192_gcm_info = {
381 MBEDTLS_CIPHER_AES_192_GCM,
386 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
391 static const mbedtls_cipher_info_t aes_256_gcm_info = {
392 MBEDTLS_CIPHER_AES_256_GCM,
397 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
401 #endif /* MBEDTLS_GCM_C */
403 #if defined(MBEDTLS_CCM_C)
404 static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
405 unsigned int key_bitlen )
407 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
411 static const mbedtls_cipher_base_t ccm_aes_info = {
412 MBEDTLS_CIPHER_ID_AES,
414 #if defined(MBEDTLS_CIPHER_MODE_CBC)
417 #if defined(MBEDTLS_CIPHER_MODE_CFB)
420 #if defined(MBEDTLS_CIPHER_MODE_CTR)
423 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
432 static const mbedtls_cipher_info_t aes_128_ccm_info = {
433 MBEDTLS_CIPHER_AES_128_CCM,
438 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
443 static const mbedtls_cipher_info_t aes_192_ccm_info = {
444 MBEDTLS_CIPHER_AES_192_CCM,
449 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
454 static const mbedtls_cipher_info_t aes_256_ccm_info = {
455 MBEDTLS_CIPHER_AES_256_CCM,
460 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
464 #endif /* MBEDTLS_CCM_C */
466 #endif /* MBEDTLS_AES_C */
468 #if defined(MBEDTLS_CAMELLIA_C)
470 static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
471 const unsigned char *input, unsigned char *output )
473 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
477 #if defined(MBEDTLS_CIPHER_MODE_CBC)
478 static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
479 size_t length, unsigned char *iv,
480 const unsigned char *input, unsigned char *output )
482 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
485 #endif /* MBEDTLS_CIPHER_MODE_CBC */
487 #if defined(MBEDTLS_CIPHER_MODE_CFB)
488 static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
489 size_t length, size_t *iv_off, unsigned char *iv,
490 const unsigned char *input, unsigned char *output )
492 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
493 iv_off, iv, input, output );
495 #endif /* MBEDTLS_CIPHER_MODE_CFB */
497 #if defined(MBEDTLS_CIPHER_MODE_CTR)
498 static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
499 unsigned char *nonce_counter, unsigned char *stream_block,
500 const unsigned char *input, unsigned char *output )
502 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
503 nonce_counter, stream_block, input, output );
505 #endif /* MBEDTLS_CIPHER_MODE_CTR */
507 static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
508 unsigned int key_bitlen )
510 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
513 static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
514 unsigned int key_bitlen )
516 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
519 static void * camellia_ctx_alloc( void )
521 mbedtls_camellia_context *ctx;
522 ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
527 mbedtls_camellia_init( ctx );
532 static void camellia_ctx_free( void *ctx )
534 mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
538 static const mbedtls_cipher_base_t camellia_info = {
539 MBEDTLS_CIPHER_ID_CAMELLIA,
540 camellia_crypt_ecb_wrap,
541 #if defined(MBEDTLS_CIPHER_MODE_CBC)
542 camellia_crypt_cbc_wrap,
544 #if defined(MBEDTLS_CIPHER_MODE_CFB)
545 camellia_crypt_cfb128_wrap,
547 #if defined(MBEDTLS_CIPHER_MODE_CTR)
548 camellia_crypt_ctr_wrap,
550 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
553 camellia_setkey_enc_wrap,
554 camellia_setkey_dec_wrap,
559 static const mbedtls_cipher_info_t camellia_128_ecb_info = {
560 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
570 static const mbedtls_cipher_info_t camellia_192_ecb_info = {
571 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
581 static const mbedtls_cipher_info_t camellia_256_ecb_info = {
582 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
592 #if defined(MBEDTLS_CIPHER_MODE_CBC)
593 static const mbedtls_cipher_info_t camellia_128_cbc_info = {
594 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
604 static const mbedtls_cipher_info_t camellia_192_cbc_info = {
605 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
615 static const mbedtls_cipher_info_t camellia_256_cbc_info = {
616 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
625 #endif /* MBEDTLS_CIPHER_MODE_CBC */
627 #if defined(MBEDTLS_CIPHER_MODE_CFB)
628 static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
629 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
632 "CAMELLIA-128-CFB128",
639 static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
640 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
643 "CAMELLIA-192-CFB128",
650 static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
651 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
654 "CAMELLIA-256-CFB128",
660 #endif /* MBEDTLS_CIPHER_MODE_CFB */
662 #if defined(MBEDTLS_CIPHER_MODE_CTR)
663 static const mbedtls_cipher_info_t camellia_128_ctr_info = {
664 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
674 static const mbedtls_cipher_info_t camellia_192_ctr_info = {
675 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
685 static const mbedtls_cipher_info_t camellia_256_ctr_info = {
686 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
695 #endif /* MBEDTLS_CIPHER_MODE_CTR */
697 #if defined(MBEDTLS_GCM_C)
698 static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
699 unsigned int key_bitlen )
701 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
705 static const mbedtls_cipher_base_t gcm_camellia_info = {
706 MBEDTLS_CIPHER_ID_CAMELLIA,
708 #if defined(MBEDTLS_CIPHER_MODE_CBC)
711 #if defined(MBEDTLS_CIPHER_MODE_CFB)
714 #if defined(MBEDTLS_CIPHER_MODE_CTR)
717 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
720 gcm_camellia_setkey_wrap,
721 gcm_camellia_setkey_wrap,
726 static const mbedtls_cipher_info_t camellia_128_gcm_info = {
727 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
732 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
737 static const mbedtls_cipher_info_t camellia_192_gcm_info = {
738 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
743 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
748 static const mbedtls_cipher_info_t camellia_256_gcm_info = {
749 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
754 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
758 #endif /* MBEDTLS_GCM_C */
760 #if defined(MBEDTLS_CCM_C)
761 static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
762 unsigned int key_bitlen )
764 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
768 static const mbedtls_cipher_base_t ccm_camellia_info = {
769 MBEDTLS_CIPHER_ID_CAMELLIA,
771 #if defined(MBEDTLS_CIPHER_MODE_CBC)
774 #if defined(MBEDTLS_CIPHER_MODE_CFB)
777 #if defined(MBEDTLS_CIPHER_MODE_CTR)
780 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
783 ccm_camellia_setkey_wrap,
784 ccm_camellia_setkey_wrap,
789 static const mbedtls_cipher_info_t camellia_128_ccm_info = {
790 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
795 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
800 static const mbedtls_cipher_info_t camellia_192_ccm_info = {
801 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
806 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
811 static const mbedtls_cipher_info_t camellia_256_ccm_info = {
812 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
817 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
821 #endif /* MBEDTLS_CCM_C */
823 #endif /* MBEDTLS_CAMELLIA_C */
825 #if defined(MBEDTLS_DES_C)
827 static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
828 const unsigned char *input, unsigned char *output )
831 return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
834 static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
835 const unsigned char *input, unsigned char *output )
838 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
841 #if defined(MBEDTLS_CIPHER_MODE_CBC)
842 static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
843 unsigned char *iv, const unsigned char *input, unsigned char *output )
845 return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
848 #endif /* MBEDTLS_CIPHER_MODE_CBC */
850 #if defined(MBEDTLS_CIPHER_MODE_CBC)
851 static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
852 unsigned char *iv, const unsigned char *input, unsigned char *output )
854 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
857 #endif /* MBEDTLS_CIPHER_MODE_CBC */
859 static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
860 unsigned int key_bitlen )
864 return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
867 static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
868 unsigned int key_bitlen )
872 return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
875 static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
876 unsigned int key_bitlen )
880 return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
883 static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
884 unsigned int key_bitlen )
888 return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
891 static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
892 unsigned int key_bitlen )
896 return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
899 static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
900 unsigned int key_bitlen )
904 return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
907 static void * des_ctx_alloc( void )
909 mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
914 mbedtls_des_init( des );
919 static void des_ctx_free( void *ctx )
921 mbedtls_des_free( (mbedtls_des_context *) ctx );
925 static void * des3_ctx_alloc( void )
927 mbedtls_des3_context *des3;
928 des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
933 mbedtls_des3_init( des3 );
938 static void des3_ctx_free( void *ctx )
940 mbedtls_des3_free( (mbedtls_des3_context *) ctx );
944 static const mbedtls_cipher_base_t des_info = {
945 MBEDTLS_CIPHER_ID_DES,
947 #if defined(MBEDTLS_CIPHER_MODE_CBC)
950 #if defined(MBEDTLS_CIPHER_MODE_CFB)
953 #if defined(MBEDTLS_CIPHER_MODE_CTR)
956 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
965 static const mbedtls_cipher_info_t des_ecb_info = {
966 MBEDTLS_CIPHER_DES_ECB,
968 MBEDTLS_KEY_LENGTH_DES,
976 #if defined(MBEDTLS_CIPHER_MODE_CBC)
977 static const mbedtls_cipher_info_t des_cbc_info = {
978 MBEDTLS_CIPHER_DES_CBC,
980 MBEDTLS_KEY_LENGTH_DES,
987 #endif /* MBEDTLS_CIPHER_MODE_CBC */
989 static const mbedtls_cipher_base_t des_ede_info = {
990 MBEDTLS_CIPHER_ID_DES,
992 #if defined(MBEDTLS_CIPHER_MODE_CBC)
995 #if defined(MBEDTLS_CIPHER_MODE_CFB)
998 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1001 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1004 des3_set2key_enc_wrap,
1005 des3_set2key_dec_wrap,
1010 static const mbedtls_cipher_info_t des_ede_ecb_info = {
1011 MBEDTLS_CIPHER_DES_EDE_ECB,
1013 MBEDTLS_KEY_LENGTH_DES_EDE,
1021 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1022 static const mbedtls_cipher_info_t des_ede_cbc_info = {
1023 MBEDTLS_CIPHER_DES_EDE_CBC,
1025 MBEDTLS_KEY_LENGTH_DES_EDE,
1032 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1034 static const mbedtls_cipher_base_t des_ede3_info = {
1035 MBEDTLS_CIPHER_ID_3DES,
1036 des3_crypt_ecb_wrap,
1037 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1038 des3_crypt_cbc_wrap,
1040 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1043 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1046 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1049 des3_set3key_enc_wrap,
1050 des3_set3key_dec_wrap,
1055 static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1056 MBEDTLS_CIPHER_DES_EDE3_ECB,
1058 MBEDTLS_KEY_LENGTH_DES_EDE3,
1065 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1066 static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1067 MBEDTLS_CIPHER_DES_EDE3_CBC,
1069 MBEDTLS_KEY_LENGTH_DES_EDE3,
1076 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1077 #endif /* MBEDTLS_DES_C */
1079 #if defined(MBEDTLS_BLOWFISH_C)
1081 static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
1082 const unsigned char *input, unsigned char *output )
1084 return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
1088 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1089 static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
1090 size_t length, unsigned char *iv, const unsigned char *input,
1091 unsigned char *output )
1093 return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
1096 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1098 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1099 static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
1100 size_t length, size_t *iv_off, unsigned char *iv,
1101 const unsigned char *input, unsigned char *output )
1103 return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
1104 iv_off, iv, input, output );
1106 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1108 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1109 static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1110 unsigned char *nonce_counter, unsigned char *stream_block,
1111 const unsigned char *input, unsigned char *output )
1113 return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
1114 nonce_counter, stream_block, input, output );
1116 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1118 static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
1119 unsigned int key_bitlen )
1121 return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
1124 static void * blowfish_ctx_alloc( void )
1126 mbedtls_blowfish_context *ctx;
1127 ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
1132 mbedtls_blowfish_init( ctx );
1137 static void blowfish_ctx_free( void *ctx )
1139 mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
1140 mbedtls_free( ctx );
1143 static const mbedtls_cipher_base_t blowfish_info = {
1144 MBEDTLS_CIPHER_ID_BLOWFISH,
1145 blowfish_crypt_ecb_wrap,
1146 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1147 blowfish_crypt_cbc_wrap,
1149 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1150 blowfish_crypt_cfb64_wrap,
1152 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1153 blowfish_crypt_ctr_wrap,
1155 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1158 blowfish_setkey_wrap,
1159 blowfish_setkey_wrap,
1164 static const mbedtls_cipher_info_t blowfish_ecb_info = {
1165 MBEDTLS_CIPHER_BLOWFISH_ECB,
1170 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1175 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1176 static const mbedtls_cipher_info_t blowfish_cbc_info = {
1177 MBEDTLS_CIPHER_BLOWFISH_CBC,
1182 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1186 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1188 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1189 static const mbedtls_cipher_info_t blowfish_cfb64_info = {
1190 MBEDTLS_CIPHER_BLOWFISH_CFB64,
1195 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1199 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1201 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1202 static const mbedtls_cipher_info_t blowfish_ctr_info = {
1203 MBEDTLS_CIPHER_BLOWFISH_CTR,
1208 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1212 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1213 #endif /* MBEDTLS_BLOWFISH_C */
1215 #if defined(MBEDTLS_ARC4_C)
1216 static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1217 const unsigned char *input,
1218 unsigned char *output )
1220 return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
1223 static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
1224 unsigned int key_bitlen )
1226 /* we get key_bitlen in bits, arc4 expects it in bytes */
1227 if( key_bitlen % 8 != 0 )
1228 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1230 mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
1234 static void * arc4_ctx_alloc( void )
1236 mbedtls_arc4_context *ctx;
1237 ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
1242 mbedtls_arc4_init( ctx );
1247 static void arc4_ctx_free( void *ctx )
1249 mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
1250 mbedtls_free( ctx );
1253 static const mbedtls_cipher_base_t arc4_base_info = {
1254 MBEDTLS_CIPHER_ID_ARC4,
1256 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1259 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1262 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1265 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1266 arc4_crypt_stream_wrap,
1274 static const mbedtls_cipher_info_t arc4_128_info = {
1275 MBEDTLS_CIPHER_ARC4_128,
1276 MBEDTLS_MODE_STREAM,
1284 #endif /* MBEDTLS_ARC4_C */
1286 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
1287 static int null_crypt_stream( void *ctx, size_t length,
1288 const unsigned char *input,
1289 unsigned char *output )
1292 memmove( output, input, length );
1296 static int null_setkey( void *ctx, const unsigned char *key,
1297 unsigned int key_bitlen )
1301 ((void) key_bitlen);
1306 static void * null_ctx_alloc( void )
1308 return( (void *) 1 );
1311 static void null_ctx_free( void *ctx )
1316 static const mbedtls_cipher_base_t null_base_info = {
1317 MBEDTLS_CIPHER_ID_NULL,
1319 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1322 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1325 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1328 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1337 static const mbedtls_cipher_info_t null_cipher_info = {
1338 MBEDTLS_CIPHER_NULL,
1339 MBEDTLS_MODE_STREAM,
1347 #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
1349 const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
1351 #if defined(MBEDTLS_AES_C)
1352 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1353 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1354 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1355 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1356 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1357 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1358 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
1360 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1361 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1362 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1363 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
1365 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1366 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1367 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1368 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
1370 #if defined(MBEDTLS_GCM_C)
1371 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1372 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1373 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
1375 #if defined(MBEDTLS_CCM_C)
1376 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
1377 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
1378 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
1380 #endif /* MBEDTLS_AES_C */
1382 #if defined(MBEDTLS_ARC4_C)
1383 { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
1386 #if defined(MBEDTLS_BLOWFISH_C)
1387 { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1388 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1389 { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
1391 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1392 { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
1394 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1395 { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
1397 #endif /* MBEDTLS_BLOWFISH_C */
1399 #if defined(MBEDTLS_CAMELLIA_C)
1400 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1401 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
1402 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
1403 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1404 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1405 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1406 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
1408 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1409 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1410 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1411 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
1413 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1414 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1415 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1416 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
1418 #if defined(MBEDTLS_GCM_C)
1419 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1420 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1421 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
1423 #if defined(MBEDTLS_CCM_C)
1424 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
1425 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
1426 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
1428 #endif /* MBEDTLS_CAMELLIA_C */
1430 #if defined(MBEDTLS_DES_C)
1431 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
1432 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1433 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1434 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1435 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
1436 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1437 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
1439 #endif /* MBEDTLS_DES_C */
1441 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
1442 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
1443 #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
1445 { MBEDTLS_CIPHER_NONE, NULL }
1448 #define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
1449 int mbedtls_cipher_supported[NUM_CIPHERS];
1451 #endif /* MBEDTLS_CIPHER_C */