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: GPL-2.0
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * This file is part of mbed TLS (https://tls.mbed.org)
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "mbedtls/config.h"
31 #include MBEDTLS_CONFIG_FILE
34 #if defined(MBEDTLS_CIPHER_C)
36 #include "mbedtls/cipher_internal.h"
38 #if defined(MBEDTLS_AES_C)
39 #include "mbedtls/aes.h"
42 #if defined(MBEDTLS_ARC4_C)
43 #include "mbedtls/arc4.h"
46 #if defined(MBEDTLS_CAMELLIA_C)
47 #include "mbedtls/camellia.h"
50 #if defined(MBEDTLS_DES_C)
51 #include "mbedtls/des.h"
54 #if defined(MBEDTLS_BLOWFISH_C)
55 #include "mbedtls/blowfish.h"
58 #if defined(MBEDTLS_GCM_C)
59 #include "mbedtls/gcm.h"
62 #if defined(MBEDTLS_CCM_C)
63 #include "mbedtls/ccm.h"
66 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
70 #if defined(MBEDTLS_PLATFORM_C)
71 #include "mbedtls/platform.h"
74 #define mbedtls_calloc calloc
75 #define mbedtls_free free
78 #if defined(MBEDTLS_GCM_C)
79 /* shared by all GCM ciphers */
80 static void *gcm_ctx_alloc( void )
82 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_gcm_context
) );
85 mbedtls_gcm_init( (mbedtls_gcm_context
*) ctx
);
90 static void gcm_ctx_free( void *ctx
)
92 mbedtls_gcm_free( ctx
);
95 #endif /* MBEDTLS_GCM_C */
97 #if defined(MBEDTLS_CCM_C)
98 /* shared by all CCM ciphers */
99 static void *ccm_ctx_alloc( void )
101 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_ccm_context
) );
104 mbedtls_ccm_init( (mbedtls_ccm_context
*) ctx
);
109 static void ccm_ctx_free( void *ctx
)
111 mbedtls_ccm_free( ctx
);
114 #endif /* MBEDTLS_CCM_C */
116 #if defined(MBEDTLS_AES_C)
118 static int aes_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
119 const unsigned char *input
, unsigned char *output
)
121 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context
*) ctx
, operation
, input
, output
);
124 #if defined(MBEDTLS_CIPHER_MODE_CBC)
125 static int aes_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
, size_t length
,
126 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
128 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context
*) ctx
, operation
, length
, iv
, input
,
131 #endif /* MBEDTLS_CIPHER_MODE_CBC */
133 #if defined(MBEDTLS_CIPHER_MODE_CFB)
134 static int aes_crypt_cfb128_wrap( void *ctx
, mbedtls_operation_t operation
,
135 size_t length
, size_t *iv_off
, unsigned char *iv
,
136 const unsigned char *input
, unsigned char *output
)
138 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context
*) ctx
, operation
, length
, iv_off
, iv
,
141 #endif /* MBEDTLS_CIPHER_MODE_CFB */
143 #if defined(MBEDTLS_CIPHER_MODE_CTR)
144 static int aes_crypt_ctr_wrap( void *ctx
, size_t length
, size_t *nc_off
,
145 unsigned char *nonce_counter
, unsigned char *stream_block
,
146 const unsigned char *input
, unsigned char *output
)
148 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context
*) ctx
, length
, nc_off
, nonce_counter
,
149 stream_block
, input
, output
);
151 #endif /* MBEDTLS_CIPHER_MODE_CTR */
153 static int aes_setkey_dec_wrap( void *ctx
, const unsigned char *key
,
154 unsigned int key_bitlen
)
156 return mbedtls_aes_setkey_dec( (mbedtls_aes_context
*) ctx
, key
, key_bitlen
);
159 static int aes_setkey_enc_wrap( void *ctx
, const unsigned char *key
,
160 unsigned int key_bitlen
)
162 return mbedtls_aes_setkey_enc( (mbedtls_aes_context
*) ctx
, key
, key_bitlen
);
165 static void * aes_ctx_alloc( void )
167 mbedtls_aes_context
*aes
= mbedtls_calloc( 1, sizeof( mbedtls_aes_context
) );
172 mbedtls_aes_init( aes
);
177 static void aes_ctx_free( void *ctx
)
179 mbedtls_aes_free( (mbedtls_aes_context
*) ctx
);
183 static const mbedtls_cipher_base_t aes_info
= {
184 MBEDTLS_CIPHER_ID_AES
,
186 #if defined(MBEDTLS_CIPHER_MODE_CBC)
189 #if defined(MBEDTLS_CIPHER_MODE_CFB)
190 aes_crypt_cfb128_wrap
,
192 #if defined(MBEDTLS_CIPHER_MODE_CTR)
195 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
204 static const mbedtls_cipher_info_t aes_128_ecb_info
= {
205 MBEDTLS_CIPHER_AES_128_ECB
,
215 static const mbedtls_cipher_info_t aes_192_ecb_info
= {
216 MBEDTLS_CIPHER_AES_192_ECB
,
226 static const mbedtls_cipher_info_t aes_256_ecb_info
= {
227 MBEDTLS_CIPHER_AES_256_ECB
,
237 #if defined(MBEDTLS_CIPHER_MODE_CBC)
238 static const mbedtls_cipher_info_t aes_128_cbc_info
= {
239 MBEDTLS_CIPHER_AES_128_CBC
,
249 static const mbedtls_cipher_info_t aes_192_cbc_info
= {
250 MBEDTLS_CIPHER_AES_192_CBC
,
260 static const mbedtls_cipher_info_t aes_256_cbc_info
= {
261 MBEDTLS_CIPHER_AES_256_CBC
,
270 #endif /* MBEDTLS_CIPHER_MODE_CBC */
272 #if defined(MBEDTLS_CIPHER_MODE_CFB)
273 static const mbedtls_cipher_info_t aes_128_cfb128_info
= {
274 MBEDTLS_CIPHER_AES_128_CFB128
,
284 static const mbedtls_cipher_info_t aes_192_cfb128_info
= {
285 MBEDTLS_CIPHER_AES_192_CFB128
,
295 static const mbedtls_cipher_info_t aes_256_cfb128_info
= {
296 MBEDTLS_CIPHER_AES_256_CFB128
,
305 #endif /* MBEDTLS_CIPHER_MODE_CFB */
307 #if defined(MBEDTLS_CIPHER_MODE_CTR)
308 static const mbedtls_cipher_info_t aes_128_ctr_info
= {
309 MBEDTLS_CIPHER_AES_128_CTR
,
319 static const mbedtls_cipher_info_t aes_192_ctr_info
= {
320 MBEDTLS_CIPHER_AES_192_CTR
,
330 static const mbedtls_cipher_info_t aes_256_ctr_info
= {
331 MBEDTLS_CIPHER_AES_256_CTR
,
340 #endif /* MBEDTLS_CIPHER_MODE_CTR */
342 #if defined(MBEDTLS_GCM_C)
343 static int gcm_aes_setkey_wrap( void *ctx
, const unsigned char *key
,
344 unsigned int key_bitlen
)
346 return mbedtls_gcm_setkey( (mbedtls_gcm_context
*) ctx
, MBEDTLS_CIPHER_ID_AES
,
350 static const mbedtls_cipher_base_t gcm_aes_info
= {
351 MBEDTLS_CIPHER_ID_AES
,
353 #if defined(MBEDTLS_CIPHER_MODE_CBC)
356 #if defined(MBEDTLS_CIPHER_MODE_CFB)
359 #if defined(MBEDTLS_CIPHER_MODE_CTR)
362 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
371 static const mbedtls_cipher_info_t aes_128_gcm_info
= {
372 MBEDTLS_CIPHER_AES_128_GCM
,
377 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
382 static const mbedtls_cipher_info_t aes_192_gcm_info
= {
383 MBEDTLS_CIPHER_AES_192_GCM
,
388 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
393 static const mbedtls_cipher_info_t aes_256_gcm_info
= {
394 MBEDTLS_CIPHER_AES_256_GCM
,
399 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
403 #endif /* MBEDTLS_GCM_C */
405 #if defined(MBEDTLS_CCM_C)
406 static int ccm_aes_setkey_wrap( void *ctx
, const unsigned char *key
,
407 unsigned int key_bitlen
)
409 return mbedtls_ccm_setkey( (mbedtls_ccm_context
*) ctx
, MBEDTLS_CIPHER_ID_AES
,
413 static const mbedtls_cipher_base_t ccm_aes_info
= {
414 MBEDTLS_CIPHER_ID_AES
,
416 #if defined(MBEDTLS_CIPHER_MODE_CBC)
419 #if defined(MBEDTLS_CIPHER_MODE_CFB)
422 #if defined(MBEDTLS_CIPHER_MODE_CTR)
425 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
434 static const mbedtls_cipher_info_t aes_128_ccm_info
= {
435 MBEDTLS_CIPHER_AES_128_CCM
,
440 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
445 static const mbedtls_cipher_info_t aes_192_ccm_info
= {
446 MBEDTLS_CIPHER_AES_192_CCM
,
451 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
456 static const mbedtls_cipher_info_t aes_256_ccm_info
= {
457 MBEDTLS_CIPHER_AES_256_CCM
,
462 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
466 #endif /* MBEDTLS_CCM_C */
468 #endif /* MBEDTLS_AES_C */
470 #if defined(MBEDTLS_CAMELLIA_C)
472 static int camellia_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
473 const unsigned char *input
, unsigned char *output
)
475 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context
*) ctx
, operation
, input
,
479 #if defined(MBEDTLS_CIPHER_MODE_CBC)
480 static int camellia_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
,
481 size_t length
, unsigned char *iv
,
482 const unsigned char *input
, unsigned char *output
)
484 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context
*) ctx
, operation
, length
, iv
,
487 #endif /* MBEDTLS_CIPHER_MODE_CBC */
489 #if defined(MBEDTLS_CIPHER_MODE_CFB)
490 static int camellia_crypt_cfb128_wrap( void *ctx
, mbedtls_operation_t operation
,
491 size_t length
, size_t *iv_off
, unsigned char *iv
,
492 const unsigned char *input
, unsigned char *output
)
494 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context
*) ctx
, operation
, length
,
495 iv_off
, iv
, input
, output
);
497 #endif /* MBEDTLS_CIPHER_MODE_CFB */
499 #if defined(MBEDTLS_CIPHER_MODE_CTR)
500 static int camellia_crypt_ctr_wrap( void *ctx
, size_t length
, size_t *nc_off
,
501 unsigned char *nonce_counter
, unsigned char *stream_block
,
502 const unsigned char *input
, unsigned char *output
)
504 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context
*) ctx
, length
, nc_off
,
505 nonce_counter
, stream_block
, input
, output
);
507 #endif /* MBEDTLS_CIPHER_MODE_CTR */
509 static int camellia_setkey_dec_wrap( void *ctx
, const unsigned char *key
,
510 unsigned int key_bitlen
)
512 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context
*) ctx
, key
, key_bitlen
);
515 static int camellia_setkey_enc_wrap( void *ctx
, const unsigned char *key
,
516 unsigned int key_bitlen
)
518 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context
*) ctx
, key
, key_bitlen
);
521 static void * camellia_ctx_alloc( void )
523 mbedtls_camellia_context
*ctx
;
524 ctx
= mbedtls_calloc( 1, sizeof( mbedtls_camellia_context
) );
529 mbedtls_camellia_init( ctx
);
534 static void camellia_ctx_free( void *ctx
)
536 mbedtls_camellia_free( (mbedtls_camellia_context
*) ctx
);
540 static const mbedtls_cipher_base_t camellia_info
= {
541 MBEDTLS_CIPHER_ID_CAMELLIA
,
542 camellia_crypt_ecb_wrap
,
543 #if defined(MBEDTLS_CIPHER_MODE_CBC)
544 camellia_crypt_cbc_wrap
,
546 #if defined(MBEDTLS_CIPHER_MODE_CFB)
547 camellia_crypt_cfb128_wrap
,
549 #if defined(MBEDTLS_CIPHER_MODE_CTR)
550 camellia_crypt_ctr_wrap
,
552 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
555 camellia_setkey_enc_wrap
,
556 camellia_setkey_dec_wrap
,
561 static const mbedtls_cipher_info_t camellia_128_ecb_info
= {
562 MBEDTLS_CIPHER_CAMELLIA_128_ECB
,
572 static const mbedtls_cipher_info_t camellia_192_ecb_info
= {
573 MBEDTLS_CIPHER_CAMELLIA_192_ECB
,
583 static const mbedtls_cipher_info_t camellia_256_ecb_info
= {
584 MBEDTLS_CIPHER_CAMELLIA_256_ECB
,
594 #if defined(MBEDTLS_CIPHER_MODE_CBC)
595 static const mbedtls_cipher_info_t camellia_128_cbc_info
= {
596 MBEDTLS_CIPHER_CAMELLIA_128_CBC
,
606 static const mbedtls_cipher_info_t camellia_192_cbc_info
= {
607 MBEDTLS_CIPHER_CAMELLIA_192_CBC
,
617 static const mbedtls_cipher_info_t camellia_256_cbc_info
= {
618 MBEDTLS_CIPHER_CAMELLIA_256_CBC
,
627 #endif /* MBEDTLS_CIPHER_MODE_CBC */
629 #if defined(MBEDTLS_CIPHER_MODE_CFB)
630 static const mbedtls_cipher_info_t camellia_128_cfb128_info
= {
631 MBEDTLS_CIPHER_CAMELLIA_128_CFB128
,
634 "CAMELLIA-128-CFB128",
641 static const mbedtls_cipher_info_t camellia_192_cfb128_info
= {
642 MBEDTLS_CIPHER_CAMELLIA_192_CFB128
,
645 "CAMELLIA-192-CFB128",
652 static const mbedtls_cipher_info_t camellia_256_cfb128_info
= {
653 MBEDTLS_CIPHER_CAMELLIA_256_CFB128
,
656 "CAMELLIA-256-CFB128",
662 #endif /* MBEDTLS_CIPHER_MODE_CFB */
664 #if defined(MBEDTLS_CIPHER_MODE_CTR)
665 static const mbedtls_cipher_info_t camellia_128_ctr_info
= {
666 MBEDTLS_CIPHER_CAMELLIA_128_CTR
,
676 static const mbedtls_cipher_info_t camellia_192_ctr_info
= {
677 MBEDTLS_CIPHER_CAMELLIA_192_CTR
,
687 static const mbedtls_cipher_info_t camellia_256_ctr_info
= {
688 MBEDTLS_CIPHER_CAMELLIA_256_CTR
,
697 #endif /* MBEDTLS_CIPHER_MODE_CTR */
699 #if defined(MBEDTLS_GCM_C)
700 static int gcm_camellia_setkey_wrap( void *ctx
, const unsigned char *key
,
701 unsigned int key_bitlen
)
703 return mbedtls_gcm_setkey( (mbedtls_gcm_context
*) ctx
, MBEDTLS_CIPHER_ID_CAMELLIA
,
707 static const mbedtls_cipher_base_t gcm_camellia_info
= {
708 MBEDTLS_CIPHER_ID_CAMELLIA
,
710 #if defined(MBEDTLS_CIPHER_MODE_CBC)
713 #if defined(MBEDTLS_CIPHER_MODE_CFB)
716 #if defined(MBEDTLS_CIPHER_MODE_CTR)
719 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
722 gcm_camellia_setkey_wrap
,
723 gcm_camellia_setkey_wrap
,
728 static const mbedtls_cipher_info_t camellia_128_gcm_info
= {
729 MBEDTLS_CIPHER_CAMELLIA_128_GCM
,
734 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
739 static const mbedtls_cipher_info_t camellia_192_gcm_info
= {
740 MBEDTLS_CIPHER_CAMELLIA_192_GCM
,
745 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
750 static const mbedtls_cipher_info_t camellia_256_gcm_info
= {
751 MBEDTLS_CIPHER_CAMELLIA_256_GCM
,
756 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
760 #endif /* MBEDTLS_GCM_C */
762 #if defined(MBEDTLS_CCM_C)
763 static int ccm_camellia_setkey_wrap( void *ctx
, const unsigned char *key
,
764 unsigned int key_bitlen
)
766 return mbedtls_ccm_setkey( (mbedtls_ccm_context
*) ctx
, MBEDTLS_CIPHER_ID_CAMELLIA
,
770 static const mbedtls_cipher_base_t ccm_camellia_info
= {
771 MBEDTLS_CIPHER_ID_CAMELLIA
,
773 #if defined(MBEDTLS_CIPHER_MODE_CBC)
776 #if defined(MBEDTLS_CIPHER_MODE_CFB)
779 #if defined(MBEDTLS_CIPHER_MODE_CTR)
782 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
785 ccm_camellia_setkey_wrap
,
786 ccm_camellia_setkey_wrap
,
791 static const mbedtls_cipher_info_t camellia_128_ccm_info
= {
792 MBEDTLS_CIPHER_CAMELLIA_128_CCM
,
797 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
802 static const mbedtls_cipher_info_t camellia_192_ccm_info
= {
803 MBEDTLS_CIPHER_CAMELLIA_192_CCM
,
808 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
813 static const mbedtls_cipher_info_t camellia_256_ccm_info
= {
814 MBEDTLS_CIPHER_CAMELLIA_256_CCM
,
819 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
823 #endif /* MBEDTLS_CCM_C */
825 #endif /* MBEDTLS_CAMELLIA_C */
827 #if defined(MBEDTLS_DES_C)
829 static int des_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
830 const unsigned char *input
, unsigned char *output
)
833 return mbedtls_des_crypt_ecb( (mbedtls_des_context
*) ctx
, input
, output
);
836 static int des3_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
837 const unsigned char *input
, unsigned char *output
)
840 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context
*) ctx
, input
, output
);
843 #if defined(MBEDTLS_CIPHER_MODE_CBC)
844 static int des_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
, size_t length
,
845 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
847 return mbedtls_des_crypt_cbc( (mbedtls_des_context
*) ctx
, operation
, length
, iv
, input
,
850 #endif /* MBEDTLS_CIPHER_MODE_CBC */
852 #if defined(MBEDTLS_CIPHER_MODE_CBC)
853 static int des3_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
, size_t length
,
854 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
856 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context
*) ctx
, operation
, length
, iv
, input
,
859 #endif /* MBEDTLS_CIPHER_MODE_CBC */
861 static int des_setkey_dec_wrap( void *ctx
, const unsigned char *key
,
862 unsigned int key_bitlen
)
866 return mbedtls_des_setkey_dec( (mbedtls_des_context
*) ctx
, key
);
869 static int des_setkey_enc_wrap( void *ctx
, const unsigned char *key
,
870 unsigned int key_bitlen
)
874 return mbedtls_des_setkey_enc( (mbedtls_des_context
*) ctx
, key
);
877 static int des3_set2key_dec_wrap( void *ctx
, const unsigned char *key
,
878 unsigned int key_bitlen
)
882 return mbedtls_des3_set2key_dec( (mbedtls_des3_context
*) ctx
, key
);
885 static int des3_set2key_enc_wrap( void *ctx
, const unsigned char *key
,
886 unsigned int key_bitlen
)
890 return mbedtls_des3_set2key_enc( (mbedtls_des3_context
*) ctx
, key
);
893 static int des3_set3key_dec_wrap( void *ctx
, const unsigned char *key
,
894 unsigned int key_bitlen
)
898 return mbedtls_des3_set3key_dec( (mbedtls_des3_context
*) ctx
, key
);
901 static int des3_set3key_enc_wrap( void *ctx
, const unsigned char *key
,
902 unsigned int key_bitlen
)
906 return mbedtls_des3_set3key_enc( (mbedtls_des3_context
*) ctx
, key
);
909 static void * des_ctx_alloc( void )
911 mbedtls_des_context
*des
= mbedtls_calloc( 1, sizeof( mbedtls_des_context
) );
916 mbedtls_des_init( des
);
921 static void des_ctx_free( void *ctx
)
923 mbedtls_des_free( (mbedtls_des_context
*) ctx
);
927 static void * des3_ctx_alloc( void )
929 mbedtls_des3_context
*des3
;
930 des3
= mbedtls_calloc( 1, sizeof( mbedtls_des3_context
) );
935 mbedtls_des3_init( des3
);
940 static void des3_ctx_free( void *ctx
)
942 mbedtls_des3_free( (mbedtls_des3_context
*) ctx
);
946 static const mbedtls_cipher_base_t des_info
= {
947 MBEDTLS_CIPHER_ID_DES
,
949 #if defined(MBEDTLS_CIPHER_MODE_CBC)
952 #if defined(MBEDTLS_CIPHER_MODE_CFB)
955 #if defined(MBEDTLS_CIPHER_MODE_CTR)
958 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
967 static const mbedtls_cipher_info_t des_ecb_info
= {
968 MBEDTLS_CIPHER_DES_ECB
,
970 MBEDTLS_KEY_LENGTH_DES
,
978 #if defined(MBEDTLS_CIPHER_MODE_CBC)
979 static const mbedtls_cipher_info_t des_cbc_info
= {
980 MBEDTLS_CIPHER_DES_CBC
,
982 MBEDTLS_KEY_LENGTH_DES
,
989 #endif /* MBEDTLS_CIPHER_MODE_CBC */
991 static const mbedtls_cipher_base_t des_ede_info
= {
992 MBEDTLS_CIPHER_ID_DES
,
994 #if defined(MBEDTLS_CIPHER_MODE_CBC)
997 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1000 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1003 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1006 des3_set2key_enc_wrap
,
1007 des3_set2key_dec_wrap
,
1012 static const mbedtls_cipher_info_t des_ede_ecb_info
= {
1013 MBEDTLS_CIPHER_DES_EDE_ECB
,
1015 MBEDTLS_KEY_LENGTH_DES_EDE
,
1023 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1024 static const mbedtls_cipher_info_t des_ede_cbc_info
= {
1025 MBEDTLS_CIPHER_DES_EDE_CBC
,
1027 MBEDTLS_KEY_LENGTH_DES_EDE
,
1034 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1036 static const mbedtls_cipher_base_t des_ede3_info
= {
1037 MBEDTLS_CIPHER_ID_3DES
,
1038 des3_crypt_ecb_wrap
,
1039 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1040 des3_crypt_cbc_wrap
,
1042 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1045 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1048 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1051 des3_set3key_enc_wrap
,
1052 des3_set3key_dec_wrap
,
1057 static const mbedtls_cipher_info_t des_ede3_ecb_info
= {
1058 MBEDTLS_CIPHER_DES_EDE3_ECB
,
1060 MBEDTLS_KEY_LENGTH_DES_EDE3
,
1067 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1068 static const mbedtls_cipher_info_t des_ede3_cbc_info
= {
1069 MBEDTLS_CIPHER_DES_EDE3_CBC
,
1071 MBEDTLS_KEY_LENGTH_DES_EDE3
,
1078 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1079 #endif /* MBEDTLS_DES_C */
1081 #if defined(MBEDTLS_BLOWFISH_C)
1083 static int blowfish_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
1084 const unsigned char *input
, unsigned char *output
)
1086 return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context
*) ctx
, operation
, input
,
1090 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1091 static int blowfish_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
,
1092 size_t length
, unsigned char *iv
, const unsigned char *input
,
1093 unsigned char *output
)
1095 return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context
*) ctx
, operation
, length
, iv
,
1098 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1100 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1101 static int blowfish_crypt_cfb64_wrap( void *ctx
, mbedtls_operation_t operation
,
1102 size_t length
, size_t *iv_off
, unsigned char *iv
,
1103 const unsigned char *input
, unsigned char *output
)
1105 return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context
*) ctx
, operation
, length
,
1106 iv_off
, iv
, input
, output
);
1108 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1110 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1111 static int blowfish_crypt_ctr_wrap( void *ctx
, size_t length
, size_t *nc_off
,
1112 unsigned char *nonce_counter
, unsigned char *stream_block
,
1113 const unsigned char *input
, unsigned char *output
)
1115 return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context
*) ctx
, length
, nc_off
,
1116 nonce_counter
, stream_block
, input
, output
);
1118 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1120 static int blowfish_setkey_wrap( void *ctx
, const unsigned char *key
,
1121 unsigned int key_bitlen
)
1123 return mbedtls_blowfish_setkey( (mbedtls_blowfish_context
*) ctx
, key
, key_bitlen
);
1126 static void * blowfish_ctx_alloc( void )
1128 mbedtls_blowfish_context
*ctx
;
1129 ctx
= mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context
) );
1134 mbedtls_blowfish_init( ctx
);
1139 static void blowfish_ctx_free( void *ctx
)
1141 mbedtls_blowfish_free( (mbedtls_blowfish_context
*) ctx
);
1142 mbedtls_free( ctx
);
1145 static const mbedtls_cipher_base_t blowfish_info
= {
1146 MBEDTLS_CIPHER_ID_BLOWFISH
,
1147 blowfish_crypt_ecb_wrap
,
1148 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1149 blowfish_crypt_cbc_wrap
,
1151 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1152 blowfish_crypt_cfb64_wrap
,
1154 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1155 blowfish_crypt_ctr_wrap
,
1157 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1160 blowfish_setkey_wrap
,
1161 blowfish_setkey_wrap
,
1166 static const mbedtls_cipher_info_t blowfish_ecb_info
= {
1167 MBEDTLS_CIPHER_BLOWFISH_ECB
,
1172 MBEDTLS_CIPHER_VARIABLE_KEY_LEN
,
1177 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1178 static const mbedtls_cipher_info_t blowfish_cbc_info
= {
1179 MBEDTLS_CIPHER_BLOWFISH_CBC
,
1184 MBEDTLS_CIPHER_VARIABLE_KEY_LEN
,
1188 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1190 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1191 static const mbedtls_cipher_info_t blowfish_cfb64_info
= {
1192 MBEDTLS_CIPHER_BLOWFISH_CFB64
,
1197 MBEDTLS_CIPHER_VARIABLE_KEY_LEN
,
1201 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1203 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1204 static const mbedtls_cipher_info_t blowfish_ctr_info
= {
1205 MBEDTLS_CIPHER_BLOWFISH_CTR
,
1210 MBEDTLS_CIPHER_VARIABLE_KEY_LEN
,
1214 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1215 #endif /* MBEDTLS_BLOWFISH_C */
1217 #if defined(MBEDTLS_ARC4_C)
1218 static int arc4_crypt_stream_wrap( void *ctx
, size_t length
,
1219 const unsigned char *input
,
1220 unsigned char *output
)
1222 return( mbedtls_arc4_crypt( (mbedtls_arc4_context
*) ctx
, length
, input
, output
) );
1225 static int arc4_setkey_wrap( void *ctx
, const unsigned char *key
,
1226 unsigned int key_bitlen
)
1228 /* we get key_bitlen in bits, arc4 expects it in bytes */
1229 if( key_bitlen
% 8 != 0 )
1230 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
1232 mbedtls_arc4_setup( (mbedtls_arc4_context
*) ctx
, key
, key_bitlen
/ 8 );
1236 static void * arc4_ctx_alloc( void )
1238 mbedtls_arc4_context
*ctx
;
1239 ctx
= mbedtls_calloc( 1, sizeof( mbedtls_arc4_context
) );
1244 mbedtls_arc4_init( ctx
);
1249 static void arc4_ctx_free( void *ctx
)
1251 mbedtls_arc4_free( (mbedtls_arc4_context
*) ctx
);
1252 mbedtls_free( ctx
);
1255 static const mbedtls_cipher_base_t arc4_base_info
= {
1256 MBEDTLS_CIPHER_ID_ARC4
,
1258 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1261 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1264 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1267 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1268 arc4_crypt_stream_wrap
,
1276 static const mbedtls_cipher_info_t arc4_128_info
= {
1277 MBEDTLS_CIPHER_ARC4_128
,
1278 MBEDTLS_MODE_STREAM
,
1286 #endif /* MBEDTLS_ARC4_C */
1288 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
1289 static int null_crypt_stream( void *ctx
, size_t length
,
1290 const unsigned char *input
,
1291 unsigned char *output
)
1294 memmove( output
, input
, length
);
1298 static int null_setkey( void *ctx
, const unsigned char *key
,
1299 unsigned int key_bitlen
)
1303 ((void) key_bitlen
);
1308 static void * null_ctx_alloc( void )
1310 return( (void *) 1 );
1313 static void null_ctx_free( void *ctx
)
1318 static const mbedtls_cipher_base_t null_base_info
= {
1319 MBEDTLS_CIPHER_ID_NULL
,
1321 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1324 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1327 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1330 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1339 static const mbedtls_cipher_info_t null_cipher_info
= {
1340 MBEDTLS_CIPHER_NULL
,
1341 MBEDTLS_MODE_STREAM
,
1349 #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
1351 const mbedtls_cipher_definition_t mbedtls_cipher_definitions
[] =
1353 #if defined(MBEDTLS_AES_C)
1354 { MBEDTLS_CIPHER_AES_128_ECB
, &aes_128_ecb_info
},
1355 { MBEDTLS_CIPHER_AES_192_ECB
, &aes_192_ecb_info
},
1356 { MBEDTLS_CIPHER_AES_256_ECB
, &aes_256_ecb_info
},
1357 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1358 { MBEDTLS_CIPHER_AES_128_CBC
, &aes_128_cbc_info
},
1359 { MBEDTLS_CIPHER_AES_192_CBC
, &aes_192_cbc_info
},
1360 { MBEDTLS_CIPHER_AES_256_CBC
, &aes_256_cbc_info
},
1362 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1363 { MBEDTLS_CIPHER_AES_128_CFB128
, &aes_128_cfb128_info
},
1364 { MBEDTLS_CIPHER_AES_192_CFB128
, &aes_192_cfb128_info
},
1365 { MBEDTLS_CIPHER_AES_256_CFB128
, &aes_256_cfb128_info
},
1367 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1368 { MBEDTLS_CIPHER_AES_128_CTR
, &aes_128_ctr_info
},
1369 { MBEDTLS_CIPHER_AES_192_CTR
, &aes_192_ctr_info
},
1370 { MBEDTLS_CIPHER_AES_256_CTR
, &aes_256_ctr_info
},
1372 #if defined(MBEDTLS_GCM_C)
1373 { MBEDTLS_CIPHER_AES_128_GCM
, &aes_128_gcm_info
},
1374 { MBEDTLS_CIPHER_AES_192_GCM
, &aes_192_gcm_info
},
1375 { MBEDTLS_CIPHER_AES_256_GCM
, &aes_256_gcm_info
},
1377 #if defined(MBEDTLS_CCM_C)
1378 { MBEDTLS_CIPHER_AES_128_CCM
, &aes_128_ccm_info
},
1379 { MBEDTLS_CIPHER_AES_192_CCM
, &aes_192_ccm_info
},
1380 { MBEDTLS_CIPHER_AES_256_CCM
, &aes_256_ccm_info
},
1382 #endif /* MBEDTLS_AES_C */
1384 #if defined(MBEDTLS_ARC4_C)
1385 { MBEDTLS_CIPHER_ARC4_128
, &arc4_128_info
},
1388 #if defined(MBEDTLS_BLOWFISH_C)
1389 { MBEDTLS_CIPHER_BLOWFISH_ECB
, &blowfish_ecb_info
},
1390 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1391 { MBEDTLS_CIPHER_BLOWFISH_CBC
, &blowfish_cbc_info
},
1393 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1394 { MBEDTLS_CIPHER_BLOWFISH_CFB64
, &blowfish_cfb64_info
},
1396 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1397 { MBEDTLS_CIPHER_BLOWFISH_CTR
, &blowfish_ctr_info
},
1399 #endif /* MBEDTLS_BLOWFISH_C */
1401 #if defined(MBEDTLS_CAMELLIA_C)
1402 { MBEDTLS_CIPHER_CAMELLIA_128_ECB
, &camellia_128_ecb_info
},
1403 { MBEDTLS_CIPHER_CAMELLIA_192_ECB
, &camellia_192_ecb_info
},
1404 { MBEDTLS_CIPHER_CAMELLIA_256_ECB
, &camellia_256_ecb_info
},
1405 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1406 { MBEDTLS_CIPHER_CAMELLIA_128_CBC
, &camellia_128_cbc_info
},
1407 { MBEDTLS_CIPHER_CAMELLIA_192_CBC
, &camellia_192_cbc_info
},
1408 { MBEDTLS_CIPHER_CAMELLIA_256_CBC
, &camellia_256_cbc_info
},
1410 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1411 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128
, &camellia_128_cfb128_info
},
1412 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128
, &camellia_192_cfb128_info
},
1413 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128
, &camellia_256_cfb128_info
},
1415 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1416 { MBEDTLS_CIPHER_CAMELLIA_128_CTR
, &camellia_128_ctr_info
},
1417 { MBEDTLS_CIPHER_CAMELLIA_192_CTR
, &camellia_192_ctr_info
},
1418 { MBEDTLS_CIPHER_CAMELLIA_256_CTR
, &camellia_256_ctr_info
},
1420 #if defined(MBEDTLS_GCM_C)
1421 { MBEDTLS_CIPHER_CAMELLIA_128_GCM
, &camellia_128_gcm_info
},
1422 { MBEDTLS_CIPHER_CAMELLIA_192_GCM
, &camellia_192_gcm_info
},
1423 { MBEDTLS_CIPHER_CAMELLIA_256_GCM
, &camellia_256_gcm_info
},
1425 #if defined(MBEDTLS_CCM_C)
1426 { MBEDTLS_CIPHER_CAMELLIA_128_CCM
, &camellia_128_ccm_info
},
1427 { MBEDTLS_CIPHER_CAMELLIA_192_CCM
, &camellia_192_ccm_info
},
1428 { MBEDTLS_CIPHER_CAMELLIA_256_CCM
, &camellia_256_ccm_info
},
1430 #endif /* MBEDTLS_CAMELLIA_C */
1432 #if defined(MBEDTLS_DES_C)
1433 { MBEDTLS_CIPHER_DES_ECB
, &des_ecb_info
},
1434 { MBEDTLS_CIPHER_DES_EDE_ECB
, &des_ede_ecb_info
},
1435 { MBEDTLS_CIPHER_DES_EDE3_ECB
, &des_ede3_ecb_info
},
1436 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1437 { MBEDTLS_CIPHER_DES_CBC
, &des_cbc_info
},
1438 { MBEDTLS_CIPHER_DES_EDE_CBC
, &des_ede_cbc_info
},
1439 { MBEDTLS_CIPHER_DES_EDE3_CBC
, &des_ede3_cbc_info
},
1441 #endif /* MBEDTLS_DES_C */
1443 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
1444 { MBEDTLS_CIPHER_NULL
, &null_cipher_info
},
1445 #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
1447 { MBEDTLS_CIPHER_NONE
, NULL
}
1450 #define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
1451 int mbedtls_cipher_supported
[NUM_CIPHERS
];
1453 #endif /* MBEDTLS_CIPHER_C */