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_CHACHAPOLY_C)
39 #include "mbedtls/chachapoly.h"
42 #if defined(MBEDTLS_AES_C)
43 #include "mbedtls/aes.h"
46 #if defined(MBEDTLS_ARC4_C)
47 #include "mbedtls/arc4.h"
50 #if defined(MBEDTLS_CAMELLIA_C)
51 #include "mbedtls/camellia.h"
54 #if defined(MBEDTLS_ARIA_C)
55 #include "mbedtls/aria.h"
58 #if defined(MBEDTLS_DES_C)
59 #include "mbedtls/des.h"
62 #if defined(MBEDTLS_BLOWFISH_C)
63 #include "mbedtls/blowfish.h"
66 #if defined(MBEDTLS_CHACHA20_C)
67 #include "mbedtls/chacha20.h"
70 #if defined(MBEDTLS_GCM_C)
71 #include "mbedtls/gcm.h"
74 #if defined(MBEDTLS_CCM_C)
75 #include "mbedtls/ccm.h"
78 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
82 #if defined(MBEDTLS_PLATFORM_C)
83 #include "mbedtls/platform.h"
86 #define mbedtls_calloc calloc
87 #define mbedtls_free free
90 #if defined(MBEDTLS_GCM_C)
91 /* shared by all GCM ciphers */
92 static void *gcm_ctx_alloc( void )
94 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_gcm_context
) );
97 mbedtls_gcm_init( (mbedtls_gcm_context
*) ctx
);
102 static void gcm_ctx_free( void *ctx
)
104 mbedtls_gcm_free( ctx
);
107 #endif /* MBEDTLS_GCM_C */
109 #if defined(MBEDTLS_CCM_C)
110 /* shared by all CCM ciphers */
111 static void *ccm_ctx_alloc( void )
113 void *ctx
= mbedtls_calloc( 1, sizeof( mbedtls_ccm_context
) );
116 mbedtls_ccm_init( (mbedtls_ccm_context
*) ctx
);
121 static void ccm_ctx_free( void *ctx
)
123 mbedtls_ccm_free( ctx
);
126 #endif /* MBEDTLS_CCM_C */
128 #if defined(MBEDTLS_AES_C)
130 static int aes_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
131 const unsigned char *input
, unsigned char *output
)
133 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context
*) ctx
, operation
, input
, output
);
136 #if defined(MBEDTLS_CIPHER_MODE_CBC)
137 static int aes_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
, size_t length
,
138 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
140 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context
*) ctx
, operation
, length
, iv
, input
,
143 #endif /* MBEDTLS_CIPHER_MODE_CBC */
145 #if defined(MBEDTLS_CIPHER_MODE_CFB)
146 static int aes_crypt_cfb128_wrap( void *ctx
, mbedtls_operation_t operation
,
147 size_t length
, size_t *iv_off
, unsigned char *iv
,
148 const unsigned char *input
, unsigned char *output
)
150 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context
*) ctx
, operation
, length
, iv_off
, iv
,
153 #endif /* MBEDTLS_CIPHER_MODE_CFB */
155 #if defined(MBEDTLS_CIPHER_MODE_OFB)
156 static int aes_crypt_ofb_wrap( void *ctx
, size_t length
, size_t *iv_off
,
157 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
159 return mbedtls_aes_crypt_ofb( (mbedtls_aes_context
*) ctx
, length
, iv_off
,
162 #endif /* MBEDTLS_CIPHER_MODE_OFB */
164 #if defined(MBEDTLS_CIPHER_MODE_CTR)
165 static int aes_crypt_ctr_wrap( void *ctx
, size_t length
, size_t *nc_off
,
166 unsigned char *nonce_counter
, unsigned char *stream_block
,
167 const unsigned char *input
, unsigned char *output
)
169 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context
*) ctx
, length
, nc_off
, nonce_counter
,
170 stream_block
, input
, output
);
172 #endif /* MBEDTLS_CIPHER_MODE_CTR */
174 #if defined(MBEDTLS_CIPHER_MODE_XTS)
175 static int aes_crypt_xts_wrap( void *ctx
, mbedtls_operation_t operation
,
177 const unsigned char data_unit
[16],
178 const unsigned char *input
,
179 unsigned char *output
)
181 mbedtls_aes_xts_context
*xts_ctx
= ctx
;
186 case MBEDTLS_ENCRYPT
:
187 mode
= MBEDTLS_AES_ENCRYPT
;
189 case MBEDTLS_DECRYPT
:
190 mode
= MBEDTLS_AES_DECRYPT
;
193 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
;
196 return mbedtls_aes_crypt_xts( xts_ctx
, mode
, length
,
197 data_unit
, input
, output
);
199 #endif /* MBEDTLS_CIPHER_MODE_XTS */
201 static int aes_setkey_dec_wrap( void *ctx
, const unsigned char *key
,
202 unsigned int key_bitlen
)
204 return mbedtls_aes_setkey_dec( (mbedtls_aes_context
*) ctx
, key
, key_bitlen
);
207 static int aes_setkey_enc_wrap( void *ctx
, const unsigned char *key
,
208 unsigned int key_bitlen
)
210 return mbedtls_aes_setkey_enc( (mbedtls_aes_context
*) ctx
, key
, key_bitlen
);
213 static void * aes_ctx_alloc( void )
215 mbedtls_aes_context
*aes
= mbedtls_calloc( 1, sizeof( mbedtls_aes_context
) );
220 mbedtls_aes_init( aes
);
225 static void aes_ctx_free( void *ctx
)
227 mbedtls_aes_free( (mbedtls_aes_context
*) ctx
);
231 static const mbedtls_cipher_base_t aes_info
= {
232 MBEDTLS_CIPHER_ID_AES
,
234 #if defined(MBEDTLS_CIPHER_MODE_CBC)
237 #if defined(MBEDTLS_CIPHER_MODE_CFB)
238 aes_crypt_cfb128_wrap
,
240 #if defined(MBEDTLS_CIPHER_MODE_OFB)
243 #if defined(MBEDTLS_CIPHER_MODE_CTR)
246 #if defined(MBEDTLS_CIPHER_MODE_XTS)
249 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
258 static const mbedtls_cipher_info_t aes_128_ecb_info
= {
259 MBEDTLS_CIPHER_AES_128_ECB
,
269 static const mbedtls_cipher_info_t aes_192_ecb_info
= {
270 MBEDTLS_CIPHER_AES_192_ECB
,
280 static const mbedtls_cipher_info_t aes_256_ecb_info
= {
281 MBEDTLS_CIPHER_AES_256_ECB
,
291 #if defined(MBEDTLS_CIPHER_MODE_CBC)
292 static const mbedtls_cipher_info_t aes_128_cbc_info
= {
293 MBEDTLS_CIPHER_AES_128_CBC
,
303 static const mbedtls_cipher_info_t aes_192_cbc_info
= {
304 MBEDTLS_CIPHER_AES_192_CBC
,
314 static const mbedtls_cipher_info_t aes_256_cbc_info
= {
315 MBEDTLS_CIPHER_AES_256_CBC
,
324 #endif /* MBEDTLS_CIPHER_MODE_CBC */
326 #if defined(MBEDTLS_CIPHER_MODE_CFB)
327 static const mbedtls_cipher_info_t aes_128_cfb128_info
= {
328 MBEDTLS_CIPHER_AES_128_CFB128
,
338 static const mbedtls_cipher_info_t aes_192_cfb128_info
= {
339 MBEDTLS_CIPHER_AES_192_CFB128
,
349 static const mbedtls_cipher_info_t aes_256_cfb128_info
= {
350 MBEDTLS_CIPHER_AES_256_CFB128
,
359 #endif /* MBEDTLS_CIPHER_MODE_CFB */
361 #if defined(MBEDTLS_CIPHER_MODE_OFB)
362 static const mbedtls_cipher_info_t aes_128_ofb_info
= {
363 MBEDTLS_CIPHER_AES_128_OFB
,
373 static const mbedtls_cipher_info_t aes_192_ofb_info
= {
374 MBEDTLS_CIPHER_AES_192_OFB
,
384 static const mbedtls_cipher_info_t aes_256_ofb_info
= {
385 MBEDTLS_CIPHER_AES_256_OFB
,
394 #endif /* MBEDTLS_CIPHER_MODE_OFB */
396 #if defined(MBEDTLS_CIPHER_MODE_CTR)
397 static const mbedtls_cipher_info_t aes_128_ctr_info
= {
398 MBEDTLS_CIPHER_AES_128_CTR
,
408 static const mbedtls_cipher_info_t aes_192_ctr_info
= {
409 MBEDTLS_CIPHER_AES_192_CTR
,
419 static const mbedtls_cipher_info_t aes_256_ctr_info
= {
420 MBEDTLS_CIPHER_AES_256_CTR
,
429 #endif /* MBEDTLS_CIPHER_MODE_CTR */
431 #if defined(MBEDTLS_CIPHER_MODE_XTS)
432 static int xts_aes_setkey_enc_wrap( void *ctx
, const unsigned char *key
,
433 unsigned int key_bitlen
)
435 mbedtls_aes_xts_context
*xts_ctx
= ctx
;
436 return( mbedtls_aes_xts_setkey_enc( xts_ctx
, key
, key_bitlen
) );
439 static int xts_aes_setkey_dec_wrap( void *ctx
, const unsigned char *key
,
440 unsigned int key_bitlen
)
442 mbedtls_aes_xts_context
*xts_ctx
= ctx
;
443 return( mbedtls_aes_xts_setkey_dec( xts_ctx
, key
, key_bitlen
) );
446 static void *xts_aes_ctx_alloc( void )
448 mbedtls_aes_xts_context
*xts_ctx
= mbedtls_calloc( 1, sizeof( *xts_ctx
) );
450 if( xts_ctx
!= NULL
)
451 mbedtls_aes_xts_init( xts_ctx
);
456 static void xts_aes_ctx_free( void *ctx
)
458 mbedtls_aes_xts_context
*xts_ctx
= ctx
;
460 if( xts_ctx
== NULL
)
463 mbedtls_aes_xts_free( xts_ctx
);
464 mbedtls_free( xts_ctx
);
467 static const mbedtls_cipher_base_t xts_aes_info
= {
468 MBEDTLS_CIPHER_ID_AES
,
470 #if defined(MBEDTLS_CIPHER_MODE_CBC)
473 #if defined(MBEDTLS_CIPHER_MODE_CFB)
476 #if defined(MBEDTLS_CIPHER_MODE_OFB)
479 #if defined(MBEDTLS_CIPHER_MODE_CTR)
482 #if defined(MBEDTLS_CIPHER_MODE_XTS)
485 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
488 xts_aes_setkey_enc_wrap
,
489 xts_aes_setkey_dec_wrap
,
494 static const mbedtls_cipher_info_t aes_128_xts_info
= {
495 MBEDTLS_CIPHER_AES_128_XTS
,
505 static const mbedtls_cipher_info_t aes_256_xts_info
= {
506 MBEDTLS_CIPHER_AES_256_XTS
,
515 #endif /* MBEDTLS_CIPHER_MODE_XTS */
517 #if defined(MBEDTLS_GCM_C)
518 static int gcm_aes_setkey_wrap( void *ctx
, const unsigned char *key
,
519 unsigned int key_bitlen
)
521 return mbedtls_gcm_setkey( (mbedtls_gcm_context
*) ctx
, MBEDTLS_CIPHER_ID_AES
,
525 static const mbedtls_cipher_base_t gcm_aes_info
= {
526 MBEDTLS_CIPHER_ID_AES
,
528 #if defined(MBEDTLS_CIPHER_MODE_CBC)
531 #if defined(MBEDTLS_CIPHER_MODE_CFB)
534 #if defined(MBEDTLS_CIPHER_MODE_OFB)
537 #if defined(MBEDTLS_CIPHER_MODE_CTR)
540 #if defined(MBEDTLS_CIPHER_MODE_XTS)
543 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
552 static const mbedtls_cipher_info_t aes_128_gcm_info
= {
553 MBEDTLS_CIPHER_AES_128_GCM
,
558 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
563 static const mbedtls_cipher_info_t aes_192_gcm_info
= {
564 MBEDTLS_CIPHER_AES_192_GCM
,
569 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
574 static const mbedtls_cipher_info_t aes_256_gcm_info
= {
575 MBEDTLS_CIPHER_AES_256_GCM
,
580 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
584 #endif /* MBEDTLS_GCM_C */
586 #if defined(MBEDTLS_CCM_C)
587 static int ccm_aes_setkey_wrap( void *ctx
, const unsigned char *key
,
588 unsigned int key_bitlen
)
590 return mbedtls_ccm_setkey( (mbedtls_ccm_context
*) ctx
, MBEDTLS_CIPHER_ID_AES
,
594 static const mbedtls_cipher_base_t ccm_aes_info
= {
595 MBEDTLS_CIPHER_ID_AES
,
597 #if defined(MBEDTLS_CIPHER_MODE_CBC)
600 #if defined(MBEDTLS_CIPHER_MODE_CFB)
603 #if defined(MBEDTLS_CIPHER_MODE_OFB)
606 #if defined(MBEDTLS_CIPHER_MODE_CTR)
609 #if defined(MBEDTLS_CIPHER_MODE_XTS)
612 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
621 static const mbedtls_cipher_info_t aes_128_ccm_info
= {
622 MBEDTLS_CIPHER_AES_128_CCM
,
627 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
632 static const mbedtls_cipher_info_t aes_192_ccm_info
= {
633 MBEDTLS_CIPHER_AES_192_CCM
,
638 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
643 static const mbedtls_cipher_info_t aes_256_ccm_info
= {
644 MBEDTLS_CIPHER_AES_256_CCM
,
649 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
653 #endif /* MBEDTLS_CCM_C */
655 #endif /* MBEDTLS_AES_C */
657 #if defined(MBEDTLS_CAMELLIA_C)
659 static int camellia_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
660 const unsigned char *input
, unsigned char *output
)
662 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context
*) ctx
, operation
, input
,
666 #if defined(MBEDTLS_CIPHER_MODE_CBC)
667 static int camellia_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
,
668 size_t length
, unsigned char *iv
,
669 const unsigned char *input
, unsigned char *output
)
671 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context
*) ctx
, operation
, length
, iv
,
674 #endif /* MBEDTLS_CIPHER_MODE_CBC */
676 #if defined(MBEDTLS_CIPHER_MODE_CFB)
677 static int camellia_crypt_cfb128_wrap( void *ctx
, mbedtls_operation_t operation
,
678 size_t length
, size_t *iv_off
, unsigned char *iv
,
679 const unsigned char *input
, unsigned char *output
)
681 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context
*) ctx
, operation
, length
,
682 iv_off
, iv
, input
, output
);
684 #endif /* MBEDTLS_CIPHER_MODE_CFB */
686 #if defined(MBEDTLS_CIPHER_MODE_CTR)
687 static int camellia_crypt_ctr_wrap( void *ctx
, size_t length
, size_t *nc_off
,
688 unsigned char *nonce_counter
, unsigned char *stream_block
,
689 const unsigned char *input
, unsigned char *output
)
691 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context
*) ctx
, length
, nc_off
,
692 nonce_counter
, stream_block
, input
, output
);
694 #endif /* MBEDTLS_CIPHER_MODE_CTR */
696 static int camellia_setkey_dec_wrap( void *ctx
, const unsigned char *key
,
697 unsigned int key_bitlen
)
699 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context
*) ctx
, key
, key_bitlen
);
702 static int camellia_setkey_enc_wrap( void *ctx
, const unsigned char *key
,
703 unsigned int key_bitlen
)
705 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context
*) ctx
, key
, key_bitlen
);
708 static void * camellia_ctx_alloc( void )
710 mbedtls_camellia_context
*ctx
;
711 ctx
= mbedtls_calloc( 1, sizeof( mbedtls_camellia_context
) );
716 mbedtls_camellia_init( ctx
);
721 static void camellia_ctx_free( void *ctx
)
723 mbedtls_camellia_free( (mbedtls_camellia_context
*) ctx
);
727 static const mbedtls_cipher_base_t camellia_info
= {
728 MBEDTLS_CIPHER_ID_CAMELLIA
,
729 camellia_crypt_ecb_wrap
,
730 #if defined(MBEDTLS_CIPHER_MODE_CBC)
731 camellia_crypt_cbc_wrap
,
733 #if defined(MBEDTLS_CIPHER_MODE_CFB)
734 camellia_crypt_cfb128_wrap
,
736 #if defined(MBEDTLS_CIPHER_MODE_OFB)
739 #if defined(MBEDTLS_CIPHER_MODE_CTR)
740 camellia_crypt_ctr_wrap
,
742 #if defined(MBEDTLS_CIPHER_MODE_XTS)
745 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
748 camellia_setkey_enc_wrap
,
749 camellia_setkey_dec_wrap
,
754 static const mbedtls_cipher_info_t camellia_128_ecb_info
= {
755 MBEDTLS_CIPHER_CAMELLIA_128_ECB
,
765 static const mbedtls_cipher_info_t camellia_192_ecb_info
= {
766 MBEDTLS_CIPHER_CAMELLIA_192_ECB
,
776 static const mbedtls_cipher_info_t camellia_256_ecb_info
= {
777 MBEDTLS_CIPHER_CAMELLIA_256_ECB
,
787 #if defined(MBEDTLS_CIPHER_MODE_CBC)
788 static const mbedtls_cipher_info_t camellia_128_cbc_info
= {
789 MBEDTLS_CIPHER_CAMELLIA_128_CBC
,
799 static const mbedtls_cipher_info_t camellia_192_cbc_info
= {
800 MBEDTLS_CIPHER_CAMELLIA_192_CBC
,
810 static const mbedtls_cipher_info_t camellia_256_cbc_info
= {
811 MBEDTLS_CIPHER_CAMELLIA_256_CBC
,
820 #endif /* MBEDTLS_CIPHER_MODE_CBC */
822 #if defined(MBEDTLS_CIPHER_MODE_CFB)
823 static const mbedtls_cipher_info_t camellia_128_cfb128_info
= {
824 MBEDTLS_CIPHER_CAMELLIA_128_CFB128
,
827 "CAMELLIA-128-CFB128",
834 static const mbedtls_cipher_info_t camellia_192_cfb128_info
= {
835 MBEDTLS_CIPHER_CAMELLIA_192_CFB128
,
838 "CAMELLIA-192-CFB128",
845 static const mbedtls_cipher_info_t camellia_256_cfb128_info
= {
846 MBEDTLS_CIPHER_CAMELLIA_256_CFB128
,
849 "CAMELLIA-256-CFB128",
855 #endif /* MBEDTLS_CIPHER_MODE_CFB */
857 #if defined(MBEDTLS_CIPHER_MODE_CTR)
858 static const mbedtls_cipher_info_t camellia_128_ctr_info
= {
859 MBEDTLS_CIPHER_CAMELLIA_128_CTR
,
869 static const mbedtls_cipher_info_t camellia_192_ctr_info
= {
870 MBEDTLS_CIPHER_CAMELLIA_192_CTR
,
880 static const mbedtls_cipher_info_t camellia_256_ctr_info
= {
881 MBEDTLS_CIPHER_CAMELLIA_256_CTR
,
890 #endif /* MBEDTLS_CIPHER_MODE_CTR */
892 #if defined(MBEDTLS_GCM_C)
893 static int gcm_camellia_setkey_wrap( void *ctx
, const unsigned char *key
,
894 unsigned int key_bitlen
)
896 return mbedtls_gcm_setkey( (mbedtls_gcm_context
*) ctx
, MBEDTLS_CIPHER_ID_CAMELLIA
,
900 static const mbedtls_cipher_base_t gcm_camellia_info
= {
901 MBEDTLS_CIPHER_ID_CAMELLIA
,
903 #if defined(MBEDTLS_CIPHER_MODE_CBC)
906 #if defined(MBEDTLS_CIPHER_MODE_CFB)
909 #if defined(MBEDTLS_CIPHER_MODE_OFB)
912 #if defined(MBEDTLS_CIPHER_MODE_CTR)
915 #if defined(MBEDTLS_CIPHER_MODE_XTS)
918 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
921 gcm_camellia_setkey_wrap
,
922 gcm_camellia_setkey_wrap
,
927 static const mbedtls_cipher_info_t camellia_128_gcm_info
= {
928 MBEDTLS_CIPHER_CAMELLIA_128_GCM
,
933 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
938 static const mbedtls_cipher_info_t camellia_192_gcm_info
= {
939 MBEDTLS_CIPHER_CAMELLIA_192_GCM
,
944 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
949 static const mbedtls_cipher_info_t camellia_256_gcm_info
= {
950 MBEDTLS_CIPHER_CAMELLIA_256_GCM
,
955 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
959 #endif /* MBEDTLS_GCM_C */
961 #if defined(MBEDTLS_CCM_C)
962 static int ccm_camellia_setkey_wrap( void *ctx
, const unsigned char *key
,
963 unsigned int key_bitlen
)
965 return mbedtls_ccm_setkey( (mbedtls_ccm_context
*) ctx
, MBEDTLS_CIPHER_ID_CAMELLIA
,
969 static const mbedtls_cipher_base_t ccm_camellia_info
= {
970 MBEDTLS_CIPHER_ID_CAMELLIA
,
972 #if defined(MBEDTLS_CIPHER_MODE_CBC)
975 #if defined(MBEDTLS_CIPHER_MODE_CFB)
978 #if defined(MBEDTLS_CIPHER_MODE_OFB)
981 #if defined(MBEDTLS_CIPHER_MODE_CTR)
984 #if defined(MBEDTLS_CIPHER_MODE_XTS)
987 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
990 ccm_camellia_setkey_wrap
,
991 ccm_camellia_setkey_wrap
,
996 static const mbedtls_cipher_info_t camellia_128_ccm_info
= {
997 MBEDTLS_CIPHER_CAMELLIA_128_CCM
,
1002 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
1007 static const mbedtls_cipher_info_t camellia_192_ccm_info
= {
1008 MBEDTLS_CIPHER_CAMELLIA_192_CCM
,
1013 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
1018 static const mbedtls_cipher_info_t camellia_256_ccm_info
= {
1019 MBEDTLS_CIPHER_CAMELLIA_256_CCM
,
1024 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
1028 #endif /* MBEDTLS_CCM_C */
1030 #endif /* MBEDTLS_CAMELLIA_C */
1032 #if defined(MBEDTLS_ARIA_C)
1034 static int aria_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
1035 const unsigned char *input
, unsigned char *output
)
1038 return mbedtls_aria_crypt_ecb( (mbedtls_aria_context
*) ctx
, input
,
1042 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1043 static int aria_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
,
1044 size_t length
, unsigned char *iv
,
1045 const unsigned char *input
, unsigned char *output
)
1047 return mbedtls_aria_crypt_cbc( (mbedtls_aria_context
*) ctx
, operation
, length
, iv
,
1050 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1052 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1053 static int aria_crypt_cfb128_wrap( void *ctx
, mbedtls_operation_t operation
,
1054 size_t length
, size_t *iv_off
, unsigned char *iv
,
1055 const unsigned char *input
, unsigned char *output
)
1057 return mbedtls_aria_crypt_cfb128( (mbedtls_aria_context
*) ctx
, operation
, length
,
1058 iv_off
, iv
, input
, output
);
1060 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1062 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1063 static int aria_crypt_ctr_wrap( void *ctx
, size_t length
, size_t *nc_off
,
1064 unsigned char *nonce_counter
, unsigned char *stream_block
,
1065 const unsigned char *input
, unsigned char *output
)
1067 return mbedtls_aria_crypt_ctr( (mbedtls_aria_context
*) ctx
, length
, nc_off
,
1068 nonce_counter
, stream_block
, input
, output
);
1070 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1072 static int aria_setkey_dec_wrap( void *ctx
, const unsigned char *key
,
1073 unsigned int key_bitlen
)
1075 return mbedtls_aria_setkey_dec( (mbedtls_aria_context
*) ctx
, key
, key_bitlen
);
1078 static int aria_setkey_enc_wrap( void *ctx
, const unsigned char *key
,
1079 unsigned int key_bitlen
)
1081 return mbedtls_aria_setkey_enc( (mbedtls_aria_context
*) ctx
, key
, key_bitlen
);
1084 static void * aria_ctx_alloc( void )
1086 mbedtls_aria_context
*ctx
;
1087 ctx
= mbedtls_calloc( 1, sizeof( mbedtls_aria_context
) );
1092 mbedtls_aria_init( ctx
);
1097 static void aria_ctx_free( void *ctx
)
1099 mbedtls_aria_free( (mbedtls_aria_context
*) ctx
);
1100 mbedtls_free( ctx
);
1103 static const mbedtls_cipher_base_t aria_info
= {
1104 MBEDTLS_CIPHER_ID_ARIA
,
1105 aria_crypt_ecb_wrap
,
1106 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1107 aria_crypt_cbc_wrap
,
1109 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1110 aria_crypt_cfb128_wrap
,
1112 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1115 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1116 aria_crypt_ctr_wrap
,
1118 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1121 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1124 aria_setkey_enc_wrap
,
1125 aria_setkey_dec_wrap
,
1130 static const mbedtls_cipher_info_t aria_128_ecb_info
= {
1131 MBEDTLS_CIPHER_ARIA_128_ECB
,
1141 static const mbedtls_cipher_info_t aria_192_ecb_info
= {
1142 MBEDTLS_CIPHER_ARIA_192_ECB
,
1152 static const mbedtls_cipher_info_t aria_256_ecb_info
= {
1153 MBEDTLS_CIPHER_ARIA_256_ECB
,
1163 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1164 static const mbedtls_cipher_info_t aria_128_cbc_info
= {
1165 MBEDTLS_CIPHER_ARIA_128_CBC
,
1175 static const mbedtls_cipher_info_t aria_192_cbc_info
= {
1176 MBEDTLS_CIPHER_ARIA_192_CBC
,
1186 static const mbedtls_cipher_info_t aria_256_cbc_info
= {
1187 MBEDTLS_CIPHER_ARIA_256_CBC
,
1196 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1198 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1199 static const mbedtls_cipher_info_t aria_128_cfb128_info
= {
1200 MBEDTLS_CIPHER_ARIA_128_CFB128
,
1210 static const mbedtls_cipher_info_t aria_192_cfb128_info
= {
1211 MBEDTLS_CIPHER_ARIA_192_CFB128
,
1221 static const mbedtls_cipher_info_t aria_256_cfb128_info
= {
1222 MBEDTLS_CIPHER_ARIA_256_CFB128
,
1231 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1233 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1234 static const mbedtls_cipher_info_t aria_128_ctr_info
= {
1235 MBEDTLS_CIPHER_ARIA_128_CTR
,
1245 static const mbedtls_cipher_info_t aria_192_ctr_info
= {
1246 MBEDTLS_CIPHER_ARIA_192_CTR
,
1256 static const mbedtls_cipher_info_t aria_256_ctr_info
= {
1257 MBEDTLS_CIPHER_ARIA_256_CTR
,
1266 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1268 #if defined(MBEDTLS_GCM_C)
1269 static int gcm_aria_setkey_wrap( void *ctx
, const unsigned char *key
,
1270 unsigned int key_bitlen
)
1272 return mbedtls_gcm_setkey( (mbedtls_gcm_context
*) ctx
, MBEDTLS_CIPHER_ID_ARIA
,
1276 static const mbedtls_cipher_base_t gcm_aria_info
= {
1277 MBEDTLS_CIPHER_ID_ARIA
,
1279 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1282 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1285 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1288 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1291 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1294 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1297 gcm_aria_setkey_wrap
,
1298 gcm_aria_setkey_wrap
,
1303 static const mbedtls_cipher_info_t aria_128_gcm_info
= {
1304 MBEDTLS_CIPHER_ARIA_128_GCM
,
1309 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
1314 static const mbedtls_cipher_info_t aria_192_gcm_info
= {
1315 MBEDTLS_CIPHER_ARIA_192_GCM
,
1320 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
1325 static const mbedtls_cipher_info_t aria_256_gcm_info
= {
1326 MBEDTLS_CIPHER_ARIA_256_GCM
,
1331 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
1335 #endif /* MBEDTLS_GCM_C */
1337 #if defined(MBEDTLS_CCM_C)
1338 static int ccm_aria_setkey_wrap( void *ctx
, const unsigned char *key
,
1339 unsigned int key_bitlen
)
1341 return mbedtls_ccm_setkey( (mbedtls_ccm_context
*) ctx
, MBEDTLS_CIPHER_ID_ARIA
,
1345 static const mbedtls_cipher_base_t ccm_aria_info
= {
1346 MBEDTLS_CIPHER_ID_ARIA
,
1348 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1351 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1354 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1357 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1360 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1363 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1366 ccm_aria_setkey_wrap
,
1367 ccm_aria_setkey_wrap
,
1372 static const mbedtls_cipher_info_t aria_128_ccm_info
= {
1373 MBEDTLS_CIPHER_ARIA_128_CCM
,
1378 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
1383 static const mbedtls_cipher_info_t aria_192_ccm_info
= {
1384 MBEDTLS_CIPHER_ARIA_192_CCM
,
1389 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
1394 static const mbedtls_cipher_info_t aria_256_ccm_info
= {
1395 MBEDTLS_CIPHER_ARIA_256_CCM
,
1400 MBEDTLS_CIPHER_VARIABLE_IV_LEN
,
1404 #endif /* MBEDTLS_CCM_C */
1406 #endif /* MBEDTLS_ARIA_C */
1408 #if defined(MBEDTLS_DES_C)
1410 static int des_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
1411 const unsigned char *input
, unsigned char *output
)
1414 return mbedtls_des_crypt_ecb( (mbedtls_des_context
*) ctx
, input
, output
);
1417 static int des3_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
1418 const unsigned char *input
, unsigned char *output
)
1421 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context
*) ctx
, input
, output
);
1424 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1425 static int des_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
, size_t length
,
1426 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
1428 return mbedtls_des_crypt_cbc( (mbedtls_des_context
*) ctx
, operation
, length
, iv
, input
,
1431 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1433 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1434 static int des3_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
, size_t length
,
1435 unsigned char *iv
, const unsigned char *input
, unsigned char *output
)
1437 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context
*) ctx
, operation
, length
, iv
, input
,
1440 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1442 static int des_setkey_dec_wrap( void *ctx
, const unsigned char *key
,
1443 unsigned int key_bitlen
)
1445 ((void) key_bitlen
);
1447 return mbedtls_des_setkey_dec( (mbedtls_des_context
*) ctx
, key
);
1450 static int des_setkey_enc_wrap( void *ctx
, const unsigned char *key
,
1451 unsigned int key_bitlen
)
1453 ((void) key_bitlen
);
1455 return mbedtls_des_setkey_enc( (mbedtls_des_context
*) ctx
, key
);
1458 static int des3_set2key_dec_wrap( void *ctx
, const unsigned char *key
,
1459 unsigned int key_bitlen
)
1461 ((void) key_bitlen
);
1463 return mbedtls_des3_set2key_dec( (mbedtls_des3_context
*) ctx
, key
);
1466 static int des3_set2key_enc_wrap( void *ctx
, const unsigned char *key
,
1467 unsigned int key_bitlen
)
1469 ((void) key_bitlen
);
1471 return mbedtls_des3_set2key_enc( (mbedtls_des3_context
*) ctx
, key
);
1474 static int des3_set3key_dec_wrap( void *ctx
, const unsigned char *key
,
1475 unsigned int key_bitlen
)
1477 ((void) key_bitlen
);
1479 return mbedtls_des3_set3key_dec( (mbedtls_des3_context
*) ctx
, key
);
1482 static int des3_set3key_enc_wrap( void *ctx
, const unsigned char *key
,
1483 unsigned int key_bitlen
)
1485 ((void) key_bitlen
);
1487 return mbedtls_des3_set3key_enc( (mbedtls_des3_context
*) ctx
, key
);
1490 static void * des_ctx_alloc( void )
1492 mbedtls_des_context
*des
= mbedtls_calloc( 1, sizeof( mbedtls_des_context
) );
1497 mbedtls_des_init( des
);
1502 static void des_ctx_free( void *ctx
)
1504 mbedtls_des_free( (mbedtls_des_context
*) ctx
);
1505 mbedtls_free( ctx
);
1508 static void * des3_ctx_alloc( void )
1510 mbedtls_des3_context
*des3
;
1511 des3
= mbedtls_calloc( 1, sizeof( mbedtls_des3_context
) );
1516 mbedtls_des3_init( des3
);
1521 static void des3_ctx_free( void *ctx
)
1523 mbedtls_des3_free( (mbedtls_des3_context
*) ctx
);
1524 mbedtls_free( ctx
);
1527 static const mbedtls_cipher_base_t des_info
= {
1528 MBEDTLS_CIPHER_ID_DES
,
1530 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1533 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1536 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1539 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1542 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1545 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1548 des_setkey_enc_wrap
,
1549 des_setkey_dec_wrap
,
1554 static const mbedtls_cipher_info_t des_ecb_info
= {
1555 MBEDTLS_CIPHER_DES_ECB
,
1557 MBEDTLS_KEY_LENGTH_DES
,
1565 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1566 static const mbedtls_cipher_info_t des_cbc_info
= {
1567 MBEDTLS_CIPHER_DES_CBC
,
1569 MBEDTLS_KEY_LENGTH_DES
,
1576 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1578 static const mbedtls_cipher_base_t des_ede_info
= {
1579 MBEDTLS_CIPHER_ID_DES
,
1580 des3_crypt_ecb_wrap
,
1581 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1582 des3_crypt_cbc_wrap
,
1584 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1587 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1590 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1593 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1596 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1599 des3_set2key_enc_wrap
,
1600 des3_set2key_dec_wrap
,
1605 static const mbedtls_cipher_info_t des_ede_ecb_info
= {
1606 MBEDTLS_CIPHER_DES_EDE_ECB
,
1608 MBEDTLS_KEY_LENGTH_DES_EDE
,
1616 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1617 static const mbedtls_cipher_info_t des_ede_cbc_info
= {
1618 MBEDTLS_CIPHER_DES_EDE_CBC
,
1620 MBEDTLS_KEY_LENGTH_DES_EDE
,
1627 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1629 static const mbedtls_cipher_base_t des_ede3_info
= {
1630 MBEDTLS_CIPHER_ID_3DES
,
1631 des3_crypt_ecb_wrap
,
1632 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1633 des3_crypt_cbc_wrap
,
1635 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1638 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1641 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1644 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1647 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1650 des3_set3key_enc_wrap
,
1651 des3_set3key_dec_wrap
,
1656 static const mbedtls_cipher_info_t des_ede3_ecb_info
= {
1657 MBEDTLS_CIPHER_DES_EDE3_ECB
,
1659 MBEDTLS_KEY_LENGTH_DES_EDE3
,
1666 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1667 static const mbedtls_cipher_info_t des_ede3_cbc_info
= {
1668 MBEDTLS_CIPHER_DES_EDE3_CBC
,
1670 MBEDTLS_KEY_LENGTH_DES_EDE3
,
1677 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1678 #endif /* MBEDTLS_DES_C */
1680 #if defined(MBEDTLS_BLOWFISH_C)
1682 static int blowfish_crypt_ecb_wrap( void *ctx
, mbedtls_operation_t operation
,
1683 const unsigned char *input
, unsigned char *output
)
1685 return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context
*) ctx
, operation
, input
,
1689 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1690 static int blowfish_crypt_cbc_wrap( void *ctx
, mbedtls_operation_t operation
,
1691 size_t length
, unsigned char *iv
, const unsigned char *input
,
1692 unsigned char *output
)
1694 return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context
*) ctx
, operation
, length
, iv
,
1697 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1699 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1700 static int blowfish_crypt_cfb64_wrap( void *ctx
, mbedtls_operation_t operation
,
1701 size_t length
, size_t *iv_off
, unsigned char *iv
,
1702 const unsigned char *input
, unsigned char *output
)
1704 return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context
*) ctx
, operation
, length
,
1705 iv_off
, iv
, input
, output
);
1707 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1709 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1710 static int blowfish_crypt_ctr_wrap( void *ctx
, size_t length
, size_t *nc_off
,
1711 unsigned char *nonce_counter
, unsigned char *stream_block
,
1712 const unsigned char *input
, unsigned char *output
)
1714 return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context
*) ctx
, length
, nc_off
,
1715 nonce_counter
, stream_block
, input
, output
);
1717 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1719 static int blowfish_setkey_wrap( void *ctx
, const unsigned char *key
,
1720 unsigned int key_bitlen
)
1722 return mbedtls_blowfish_setkey( (mbedtls_blowfish_context
*) ctx
, key
, key_bitlen
);
1725 static void * blowfish_ctx_alloc( void )
1727 mbedtls_blowfish_context
*ctx
;
1728 ctx
= mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context
) );
1733 mbedtls_blowfish_init( ctx
);
1738 static void blowfish_ctx_free( void *ctx
)
1740 mbedtls_blowfish_free( (mbedtls_blowfish_context
*) ctx
);
1741 mbedtls_free( ctx
);
1744 static const mbedtls_cipher_base_t blowfish_info
= {
1745 MBEDTLS_CIPHER_ID_BLOWFISH
,
1746 blowfish_crypt_ecb_wrap
,
1747 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1748 blowfish_crypt_cbc_wrap
,
1750 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1751 blowfish_crypt_cfb64_wrap
,
1753 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1756 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1757 blowfish_crypt_ctr_wrap
,
1759 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1762 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1765 blowfish_setkey_wrap
,
1766 blowfish_setkey_wrap
,
1771 static const mbedtls_cipher_info_t blowfish_ecb_info
= {
1772 MBEDTLS_CIPHER_BLOWFISH_ECB
,
1777 MBEDTLS_CIPHER_VARIABLE_KEY_LEN
,
1782 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1783 static const mbedtls_cipher_info_t blowfish_cbc_info
= {
1784 MBEDTLS_CIPHER_BLOWFISH_CBC
,
1789 MBEDTLS_CIPHER_VARIABLE_KEY_LEN
,
1793 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1795 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1796 static const mbedtls_cipher_info_t blowfish_cfb64_info
= {
1797 MBEDTLS_CIPHER_BLOWFISH_CFB64
,
1802 MBEDTLS_CIPHER_VARIABLE_KEY_LEN
,
1806 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1808 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1809 static const mbedtls_cipher_info_t blowfish_ctr_info
= {
1810 MBEDTLS_CIPHER_BLOWFISH_CTR
,
1815 MBEDTLS_CIPHER_VARIABLE_KEY_LEN
,
1819 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1820 #endif /* MBEDTLS_BLOWFISH_C */
1822 #if defined(MBEDTLS_ARC4_C)
1823 static int arc4_crypt_stream_wrap( void *ctx
, size_t length
,
1824 const unsigned char *input
,
1825 unsigned char *output
)
1827 return( mbedtls_arc4_crypt( (mbedtls_arc4_context
*) ctx
, length
, input
, output
) );
1830 static int arc4_setkey_wrap( void *ctx
, const unsigned char *key
,
1831 unsigned int key_bitlen
)
1833 /* we get key_bitlen in bits, arc4 expects it in bytes */
1834 if( key_bitlen
% 8 != 0 )
1835 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
1837 mbedtls_arc4_setup( (mbedtls_arc4_context
*) ctx
, key
, key_bitlen
/ 8 );
1841 static void * arc4_ctx_alloc( void )
1843 mbedtls_arc4_context
*ctx
;
1844 ctx
= mbedtls_calloc( 1, sizeof( mbedtls_arc4_context
) );
1849 mbedtls_arc4_init( ctx
);
1854 static void arc4_ctx_free( void *ctx
)
1856 mbedtls_arc4_free( (mbedtls_arc4_context
*) ctx
);
1857 mbedtls_free( ctx
);
1860 static const mbedtls_cipher_base_t arc4_base_info
= {
1861 MBEDTLS_CIPHER_ID_ARC4
,
1863 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1866 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1869 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1872 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1875 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1878 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1879 arc4_crypt_stream_wrap
,
1887 static const mbedtls_cipher_info_t arc4_128_info
= {
1888 MBEDTLS_CIPHER_ARC4_128
,
1889 MBEDTLS_MODE_STREAM
,
1897 #endif /* MBEDTLS_ARC4_C */
1899 #if defined(MBEDTLS_CHACHA20_C)
1901 static int chacha20_setkey_wrap( void *ctx
, const unsigned char *key
,
1902 unsigned int key_bitlen
)
1904 if( key_bitlen
!= 256U )
1905 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
1907 if ( 0 != mbedtls_chacha20_setkey( (mbedtls_chacha20_context
*)ctx
, key
) )
1908 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
1913 static int chacha20_stream_wrap( void *ctx
, size_t length
,
1914 const unsigned char *input
,
1915 unsigned char *output
)
1919 ret
= mbedtls_chacha20_update( ctx
, length
, input
, output
);
1920 if( ret
== MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA
)
1921 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
1926 static void * chacha20_ctx_alloc( void )
1928 mbedtls_chacha20_context
*ctx
;
1929 ctx
= mbedtls_calloc( 1, sizeof( mbedtls_chacha20_context
) );
1934 mbedtls_chacha20_init( ctx
);
1939 static void chacha20_ctx_free( void *ctx
)
1941 mbedtls_chacha20_free( (mbedtls_chacha20_context
*) ctx
);
1942 mbedtls_free( ctx
);
1945 static const mbedtls_cipher_base_t chacha20_base_info
= {
1946 MBEDTLS_CIPHER_ID_CHACHA20
,
1948 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1951 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1954 #if defined(MBEDTLS_CIPHER_MODE_OFB)
1957 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1960 #if defined(MBEDTLS_CIPHER_MODE_XTS)
1963 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1964 chacha20_stream_wrap
,
1966 chacha20_setkey_wrap
,
1967 chacha20_setkey_wrap
,
1971 static const mbedtls_cipher_info_t chacha20_info
= {
1972 MBEDTLS_CIPHER_CHACHA20
,
1973 MBEDTLS_MODE_STREAM
,
1981 #endif /* MBEDTLS_CHACHA20_C */
1983 #if defined(MBEDTLS_CHACHAPOLY_C)
1985 static int chachapoly_setkey_wrap( void *ctx
,
1986 const unsigned char *key
,
1987 unsigned int key_bitlen
)
1989 if( key_bitlen
!= 256U )
1990 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
1992 if ( 0 != mbedtls_chachapoly_setkey( (mbedtls_chachapoly_context
*)ctx
, key
) )
1993 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
1998 static void * chachapoly_ctx_alloc( void )
2000 mbedtls_chachapoly_context
*ctx
;
2001 ctx
= mbedtls_calloc( 1, sizeof( mbedtls_chachapoly_context
) );
2006 mbedtls_chachapoly_init( ctx
);
2011 static void chachapoly_ctx_free( void *ctx
)
2013 mbedtls_chachapoly_free( (mbedtls_chachapoly_context
*) ctx
);
2014 mbedtls_free( ctx
);
2017 static const mbedtls_cipher_base_t chachapoly_base_info
= {
2018 MBEDTLS_CIPHER_ID_CHACHA20
,
2020 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2023 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2026 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2029 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2032 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2035 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
2038 chachapoly_setkey_wrap
,
2039 chachapoly_setkey_wrap
,
2040 chachapoly_ctx_alloc
,
2043 static const mbedtls_cipher_info_t chachapoly_info
= {
2044 MBEDTLS_CIPHER_CHACHA20_POLY1305
,
2045 MBEDTLS_MODE_CHACHAPOLY
,
2047 "CHACHA20-POLY1305",
2051 &chachapoly_base_info
2053 #endif /* MBEDTLS_CHACHAPOLY_C */
2055 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2056 static int null_crypt_stream( void *ctx
, size_t length
,
2057 const unsigned char *input
,
2058 unsigned char *output
)
2061 memmove( output
, input
, length
);
2065 static int null_setkey( void *ctx
, const unsigned char *key
,
2066 unsigned int key_bitlen
)
2070 ((void) key_bitlen
);
2075 static void * null_ctx_alloc( void )
2077 return( (void *) 1 );
2080 static void null_ctx_free( void *ctx
)
2085 static const mbedtls_cipher_base_t null_base_info
= {
2086 MBEDTLS_CIPHER_ID_NULL
,
2088 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2091 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2094 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2097 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2100 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2103 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
2112 static const mbedtls_cipher_info_t null_cipher_info
= {
2113 MBEDTLS_CIPHER_NULL
,
2114 MBEDTLS_MODE_STREAM
,
2122 #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
2124 const mbedtls_cipher_definition_t mbedtls_cipher_definitions
[] =
2126 #if defined(MBEDTLS_AES_C)
2127 { MBEDTLS_CIPHER_AES_128_ECB
, &aes_128_ecb_info
},
2128 { MBEDTLS_CIPHER_AES_192_ECB
, &aes_192_ecb_info
},
2129 { MBEDTLS_CIPHER_AES_256_ECB
, &aes_256_ecb_info
},
2130 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2131 { MBEDTLS_CIPHER_AES_128_CBC
, &aes_128_cbc_info
},
2132 { MBEDTLS_CIPHER_AES_192_CBC
, &aes_192_cbc_info
},
2133 { MBEDTLS_CIPHER_AES_256_CBC
, &aes_256_cbc_info
},
2135 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2136 { MBEDTLS_CIPHER_AES_128_CFB128
, &aes_128_cfb128_info
},
2137 { MBEDTLS_CIPHER_AES_192_CFB128
, &aes_192_cfb128_info
},
2138 { MBEDTLS_CIPHER_AES_256_CFB128
, &aes_256_cfb128_info
},
2140 #if defined(MBEDTLS_CIPHER_MODE_OFB)
2141 { MBEDTLS_CIPHER_AES_128_OFB
, &aes_128_ofb_info
},
2142 { MBEDTLS_CIPHER_AES_192_OFB
, &aes_192_ofb_info
},
2143 { MBEDTLS_CIPHER_AES_256_OFB
, &aes_256_ofb_info
},
2145 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2146 { MBEDTLS_CIPHER_AES_128_CTR
, &aes_128_ctr_info
},
2147 { MBEDTLS_CIPHER_AES_192_CTR
, &aes_192_ctr_info
},
2148 { MBEDTLS_CIPHER_AES_256_CTR
, &aes_256_ctr_info
},
2150 #if defined(MBEDTLS_CIPHER_MODE_XTS)
2151 { MBEDTLS_CIPHER_AES_128_XTS
, &aes_128_xts_info
},
2152 { MBEDTLS_CIPHER_AES_256_XTS
, &aes_256_xts_info
},
2154 #if defined(MBEDTLS_GCM_C)
2155 { MBEDTLS_CIPHER_AES_128_GCM
, &aes_128_gcm_info
},
2156 { MBEDTLS_CIPHER_AES_192_GCM
, &aes_192_gcm_info
},
2157 { MBEDTLS_CIPHER_AES_256_GCM
, &aes_256_gcm_info
},
2159 #if defined(MBEDTLS_CCM_C)
2160 { MBEDTLS_CIPHER_AES_128_CCM
, &aes_128_ccm_info
},
2161 { MBEDTLS_CIPHER_AES_192_CCM
, &aes_192_ccm_info
},
2162 { MBEDTLS_CIPHER_AES_256_CCM
, &aes_256_ccm_info
},
2164 #endif /* MBEDTLS_AES_C */
2166 #if defined(MBEDTLS_ARC4_C)
2167 { MBEDTLS_CIPHER_ARC4_128
, &arc4_128_info
},
2170 #if defined(MBEDTLS_BLOWFISH_C)
2171 { MBEDTLS_CIPHER_BLOWFISH_ECB
, &blowfish_ecb_info
},
2172 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2173 { MBEDTLS_CIPHER_BLOWFISH_CBC
, &blowfish_cbc_info
},
2175 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2176 { MBEDTLS_CIPHER_BLOWFISH_CFB64
, &blowfish_cfb64_info
},
2178 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2179 { MBEDTLS_CIPHER_BLOWFISH_CTR
, &blowfish_ctr_info
},
2181 #endif /* MBEDTLS_BLOWFISH_C */
2183 #if defined(MBEDTLS_CAMELLIA_C)
2184 { MBEDTLS_CIPHER_CAMELLIA_128_ECB
, &camellia_128_ecb_info
},
2185 { MBEDTLS_CIPHER_CAMELLIA_192_ECB
, &camellia_192_ecb_info
},
2186 { MBEDTLS_CIPHER_CAMELLIA_256_ECB
, &camellia_256_ecb_info
},
2187 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2188 { MBEDTLS_CIPHER_CAMELLIA_128_CBC
, &camellia_128_cbc_info
},
2189 { MBEDTLS_CIPHER_CAMELLIA_192_CBC
, &camellia_192_cbc_info
},
2190 { MBEDTLS_CIPHER_CAMELLIA_256_CBC
, &camellia_256_cbc_info
},
2192 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2193 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128
, &camellia_128_cfb128_info
},
2194 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128
, &camellia_192_cfb128_info
},
2195 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128
, &camellia_256_cfb128_info
},
2197 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2198 { MBEDTLS_CIPHER_CAMELLIA_128_CTR
, &camellia_128_ctr_info
},
2199 { MBEDTLS_CIPHER_CAMELLIA_192_CTR
, &camellia_192_ctr_info
},
2200 { MBEDTLS_CIPHER_CAMELLIA_256_CTR
, &camellia_256_ctr_info
},
2202 #if defined(MBEDTLS_GCM_C)
2203 { MBEDTLS_CIPHER_CAMELLIA_128_GCM
, &camellia_128_gcm_info
},
2204 { MBEDTLS_CIPHER_CAMELLIA_192_GCM
, &camellia_192_gcm_info
},
2205 { MBEDTLS_CIPHER_CAMELLIA_256_GCM
, &camellia_256_gcm_info
},
2207 #if defined(MBEDTLS_CCM_C)
2208 { MBEDTLS_CIPHER_CAMELLIA_128_CCM
, &camellia_128_ccm_info
},
2209 { MBEDTLS_CIPHER_CAMELLIA_192_CCM
, &camellia_192_ccm_info
},
2210 { MBEDTLS_CIPHER_CAMELLIA_256_CCM
, &camellia_256_ccm_info
},
2212 #endif /* MBEDTLS_CAMELLIA_C */
2214 #if defined(MBEDTLS_ARIA_C)
2215 { MBEDTLS_CIPHER_ARIA_128_ECB
, &aria_128_ecb_info
},
2216 { MBEDTLS_CIPHER_ARIA_192_ECB
, &aria_192_ecb_info
},
2217 { MBEDTLS_CIPHER_ARIA_256_ECB
, &aria_256_ecb_info
},
2218 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2219 { MBEDTLS_CIPHER_ARIA_128_CBC
, &aria_128_cbc_info
},
2220 { MBEDTLS_CIPHER_ARIA_192_CBC
, &aria_192_cbc_info
},
2221 { MBEDTLS_CIPHER_ARIA_256_CBC
, &aria_256_cbc_info
},
2223 #if defined(MBEDTLS_CIPHER_MODE_CFB)
2224 { MBEDTLS_CIPHER_ARIA_128_CFB128
, &aria_128_cfb128_info
},
2225 { MBEDTLS_CIPHER_ARIA_192_CFB128
, &aria_192_cfb128_info
},
2226 { MBEDTLS_CIPHER_ARIA_256_CFB128
, &aria_256_cfb128_info
},
2228 #if defined(MBEDTLS_CIPHER_MODE_CTR)
2229 { MBEDTLS_CIPHER_ARIA_128_CTR
, &aria_128_ctr_info
},
2230 { MBEDTLS_CIPHER_ARIA_192_CTR
, &aria_192_ctr_info
},
2231 { MBEDTLS_CIPHER_ARIA_256_CTR
, &aria_256_ctr_info
},
2233 #if defined(MBEDTLS_GCM_C)
2234 { MBEDTLS_CIPHER_ARIA_128_GCM
, &aria_128_gcm_info
},
2235 { MBEDTLS_CIPHER_ARIA_192_GCM
, &aria_192_gcm_info
},
2236 { MBEDTLS_CIPHER_ARIA_256_GCM
, &aria_256_gcm_info
},
2238 #if defined(MBEDTLS_CCM_C)
2239 { MBEDTLS_CIPHER_ARIA_128_CCM
, &aria_128_ccm_info
},
2240 { MBEDTLS_CIPHER_ARIA_192_CCM
, &aria_192_ccm_info
},
2241 { MBEDTLS_CIPHER_ARIA_256_CCM
, &aria_256_ccm_info
},
2243 #endif /* MBEDTLS_ARIA_C */
2245 #if defined(MBEDTLS_DES_C)
2246 { MBEDTLS_CIPHER_DES_ECB
, &des_ecb_info
},
2247 { MBEDTLS_CIPHER_DES_EDE_ECB
, &des_ede_ecb_info
},
2248 { MBEDTLS_CIPHER_DES_EDE3_ECB
, &des_ede3_ecb_info
},
2249 #if defined(MBEDTLS_CIPHER_MODE_CBC)
2250 { MBEDTLS_CIPHER_DES_CBC
, &des_cbc_info
},
2251 { MBEDTLS_CIPHER_DES_EDE_CBC
, &des_ede_cbc_info
},
2252 { MBEDTLS_CIPHER_DES_EDE3_CBC
, &des_ede3_cbc_info
},
2254 #endif /* MBEDTLS_DES_C */
2256 #if defined(MBEDTLS_CHACHA20_C)
2257 { MBEDTLS_CIPHER_CHACHA20
, &chacha20_info
},
2260 #if defined(MBEDTLS_CHACHAPOLY_C)
2261 { MBEDTLS_CIPHER_CHACHA20_POLY1305
, &chachapoly_info
},
2264 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2265 { MBEDTLS_CIPHER_NULL
, &null_cipher_info
},
2266 #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
2268 { MBEDTLS_CIPHER_NONE
, NULL
}
2271 #define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
2272 int mbedtls_cipher_supported
[NUM_CIPHERS
];
2274 #endif /* MBEDTLS_CIPHER_C */