blob: 57eb3cb67fb8fa08942d56f45573b05bc1ed6b03 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
Paul Bakkerfae35f02013-03-13 10:33:51 +01002 * \file cipher_wrap.c
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020028#include "mbedtls/cipher_internal.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000029#include "mbedtls/error.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000030
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020031#if defined(MBEDTLS_CHACHAPOLY_C)
32#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000037#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/arc4.h"
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020041#endif
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_CAMELLIA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000045#endif
46
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +000047#if defined(MBEDTLS_ARIA_C)
48#include "mbedtls/aria.h"
49#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000053#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_BLOWFISH_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/blowfish.h"
Paul Bakker6132d0a2012-07-04 17:10:40 +000057#endif
58
Daniel Kingbd920622016-05-15 19:56:20 -030059#if defined(MBEDTLS_CHACHA20_C)
60#include "mbedtls/chacha20.h"
61#endif
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020065#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020069#endif
70
Jack Lloydffdf2882019-03-07 17:00:32 -050071#if defined(MBEDTLS_NIST_KW_C)
72#include "mbedtls/nist_kw.h"
73#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard0c851ee2015-02-10 12:47:52 +000076#include <string.h>
77#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020081#else
Rich Evans00ab4702015-02-06 13:43:58 +000082#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020083#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020085#endif
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020088/* shared by all GCM ciphers */
89static void *gcm_ctx_alloc( void )
90{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +020091 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
92
93 if( ctx != NULL )
94 mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
95
96 return( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020097}
98
99static void gcm_ctx_free( void *ctx )
100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_gcm_free( ctx );
102 mbedtls_free( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200107/* shared by all CCM ciphers */
108static void *ccm_ctx_alloc( void )
109{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +0200110 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
111
112 if( ctx != NULL )
113 mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
114
115 return( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200116}
117
118static void ccm_ctx_free( void *ctx )
119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_ccm_free( ctx );
121 mbedtls_free( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200128 const unsigned char *input, unsigned char *output )
129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200131}
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_CIPHER_MODE_CBC)
134static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000135 unsigned char *iv, const unsigned char *input, unsigned char *output )
136{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200138 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_CIPHER_MODE_CFB)
143static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200144 size_t length, size_t *iv_off, unsigned char *iv,
145 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000146{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200148 input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000151
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100152#if defined(MBEDTLS_CIPHER_MODE_OFB)
153static int aes_crypt_ofb_wrap( void *ctx, size_t length, size_t *iv_off,
154 unsigned char *iv, const unsigned char *input, unsigned char *output )
155{
156 return mbedtls_aes_crypt_ofb( (mbedtls_aes_context *) ctx, length, iv_off,
157 iv, input, output );
158}
159#endif /* MBEDTLS_CIPHER_MODE_OFB */
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200162static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
163 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000164 const unsigned char *input, unsigned char *output )
165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
Paul Bakker343a8702011-06-09 14:27:58 +0000167 stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000170
Jaeden Ameroc6539902018-04-30 17:17:41 +0100171#if defined(MBEDTLS_CIPHER_MODE_XTS)
172static int aes_crypt_xts_wrap( void *ctx, mbedtls_operation_t operation,
173 size_t length,
174 const unsigned char data_unit[16],
175 const unsigned char *input,
176 unsigned char *output )
177{
178 mbedtls_aes_xts_context *xts_ctx = ctx;
179 int mode;
180
181 switch( operation )
182 {
183 case MBEDTLS_ENCRYPT:
184 mode = MBEDTLS_AES_ENCRYPT;
185 break;
186 case MBEDTLS_DECRYPT:
187 mode = MBEDTLS_AES_DECRYPT;
188 break;
189 default:
190 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
191 }
192
193 return mbedtls_aes_crypt_xts( xts_ctx, mode, length,
194 data_unit, input, output );
195}
196#endif /* MBEDTLS_CIPHER_MODE_XTS */
197
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200198static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200199 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200201 return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000202}
203
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200204static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200205 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200207 return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208}
209
210static void * aes_ctx_alloc( void )
211{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200212 mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200213
214 if( aes == NULL )
215 return( NULL );
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_aes_init( aes );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200218
219 return( aes );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000220}
221
222static void aes_ctx_free( void *ctx )
223{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_aes_free( (mbedtls_aes_context *) ctx );
225 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226}
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228static const mbedtls_cipher_base_t aes_info = {
229 MBEDTLS_CIPHER_ID_AES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200230 aes_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000232 aes_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100233#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000235 aes_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100236#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100237#if defined(MBEDTLS_CIPHER_MODE_OFB)
238 aes_crypt_ofb_wrap,
239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000241 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100242#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100243#if defined(MBEDTLS_CIPHER_MODE_XTS)
244 NULL,
245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200247 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100248#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000249 aes_setkey_enc_wrap,
250 aes_setkey_dec_wrap,
251 aes_ctx_alloc,
252 aes_ctx_free
253};
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255static const mbedtls_cipher_info_t aes_128_ecb_info = {
256 MBEDTLS_CIPHER_AES_128_ECB,
257 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200258 128,
259 "AES-128-ECB",
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300260 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200261 0,
262 16,
263 &aes_info
264};
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266static const mbedtls_cipher_info_t aes_192_ecb_info = {
267 MBEDTLS_CIPHER_AES_192_ECB,
268 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200269 192,
270 "AES-192-ECB",
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300271 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200272 0,
273 16,
274 &aes_info
275};
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277static const mbedtls_cipher_info_t aes_256_ecb_info = {
278 MBEDTLS_CIPHER_AES_256_ECB,
279 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200280 256,
281 "AES-256-ECB",
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300282 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200283 0,
284 16,
285 &aes_info
286};
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288#if defined(MBEDTLS_CIPHER_MODE_CBC)
289static const mbedtls_cipher_info_t aes_128_cbc_info = {
290 MBEDTLS_CIPHER_AES_128_CBC,
291 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000292 128,
293 "AES-128-CBC",
294 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200295 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000296 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000297 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298};
299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300static const mbedtls_cipher_info_t aes_192_cbc_info = {
301 MBEDTLS_CIPHER_AES_192_CBC,
302 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000303 192,
304 "AES-192-CBC",
305 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200306 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000307 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000308 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000309};
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311static const mbedtls_cipher_info_t aes_256_cbc_info = {
312 MBEDTLS_CIPHER_AES_256_CBC,
313 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000314 256,
315 "AES-256-CBC",
316 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200317 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000318 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000319 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_CIPHER_MODE_CFB)
324static const mbedtls_cipher_info_t aes_128_cfb128_info = {
325 MBEDTLS_CIPHER_AES_128_CFB128,
326 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000327 128,
328 "AES-128-CFB128",
329 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200330 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000331 16,
332 &aes_info
333};
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335static const mbedtls_cipher_info_t aes_192_cfb128_info = {
336 MBEDTLS_CIPHER_AES_192_CFB128,
337 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000338 192,
339 "AES-192-CFB128",
340 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200341 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000342 16,
343 &aes_info
344};
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346static const mbedtls_cipher_info_t aes_256_cfb128_info = {
347 MBEDTLS_CIPHER_AES_256_CFB128,
348 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000349 256,
350 "AES-256-CFB128",
351 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200352 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000353 16,
354 &aes_info
355};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000357
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100358#if defined(MBEDTLS_CIPHER_MODE_OFB)
359static const mbedtls_cipher_info_t aes_128_ofb_info = {
360 MBEDTLS_CIPHER_AES_128_OFB,
361 MBEDTLS_MODE_OFB,
362 128,
363 "AES-128-OFB",
364 16,
365 0,
366 16,
367 &aes_info
368};
369
370static const mbedtls_cipher_info_t aes_192_ofb_info = {
371 MBEDTLS_CIPHER_AES_192_OFB,
372 MBEDTLS_MODE_OFB,
373 192,
374 "AES-192-OFB",
375 16,
376 0,
377 16,
378 &aes_info
379};
380
381static const mbedtls_cipher_info_t aes_256_ofb_info = {
382 MBEDTLS_CIPHER_AES_256_OFB,
383 MBEDTLS_MODE_OFB,
384 256,
385 "AES-256-OFB",
386 16,
387 0,
388 16,
389 &aes_info
390};
391#endif /* MBEDTLS_CIPHER_MODE_OFB */
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_CIPHER_MODE_CTR)
394static const mbedtls_cipher_info_t aes_128_ctr_info = {
395 MBEDTLS_CIPHER_AES_128_CTR,
396 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000397 128,
398 "AES-128-CTR",
399 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200400 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000401 16,
402 &aes_info
403};
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405static const mbedtls_cipher_info_t aes_192_ctr_info = {
406 MBEDTLS_CIPHER_AES_192_CTR,
407 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000408 192,
409 "AES-192-CTR",
410 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200411 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000412 16,
413 &aes_info
414};
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416static const mbedtls_cipher_info_t aes_256_ctr_info = {
417 MBEDTLS_CIPHER_AES_256_CTR,
418 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000419 256,
420 "AES-256-CTR",
421 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200422 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000423 16,
424 &aes_info
425};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000427
Jaeden Ameroc6539902018-04-30 17:17:41 +0100428#if defined(MBEDTLS_CIPHER_MODE_XTS)
429static int xts_aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
430 unsigned int key_bitlen )
431{
432 mbedtls_aes_xts_context *xts_ctx = ctx;
433 return( mbedtls_aes_xts_setkey_enc( xts_ctx, key, key_bitlen ) );
434}
435
436static int xts_aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
437 unsigned int key_bitlen )
438{
439 mbedtls_aes_xts_context *xts_ctx = ctx;
440 return( mbedtls_aes_xts_setkey_dec( xts_ctx, key, key_bitlen ) );
441}
442
443static void *xts_aes_ctx_alloc( void )
444{
445 mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc( 1, sizeof( *xts_ctx ) );
446
447 if( xts_ctx != NULL )
448 mbedtls_aes_xts_init( xts_ctx );
449
450 return( xts_ctx );
451}
452
453static void xts_aes_ctx_free( void *ctx )
454{
455 mbedtls_aes_xts_context *xts_ctx = ctx;
456
457 if( xts_ctx == NULL )
458 return;
459
460 mbedtls_aes_xts_free( xts_ctx );
461 mbedtls_free( xts_ctx );
462}
463
464static const mbedtls_cipher_base_t xts_aes_info = {
465 MBEDTLS_CIPHER_ID_AES,
466 NULL,
467#if defined(MBEDTLS_CIPHER_MODE_CBC)
468 NULL,
469#endif
470#if defined(MBEDTLS_CIPHER_MODE_CFB)
471 NULL,
472#endif
473#if defined(MBEDTLS_CIPHER_MODE_OFB)
474 NULL,
475#endif
476#if defined(MBEDTLS_CIPHER_MODE_CTR)
477 NULL,
478#endif
479#if defined(MBEDTLS_CIPHER_MODE_XTS)
480 aes_crypt_xts_wrap,
481#endif
482#if defined(MBEDTLS_CIPHER_MODE_STREAM)
483 NULL,
484#endif
485 xts_aes_setkey_enc_wrap,
486 xts_aes_setkey_dec_wrap,
487 xts_aes_ctx_alloc,
488 xts_aes_ctx_free
489};
490
491static const mbedtls_cipher_info_t aes_128_xts_info = {
492 MBEDTLS_CIPHER_AES_128_XTS,
493 MBEDTLS_MODE_XTS,
494 256,
495 "AES-128-XTS",
496 16,
497 0,
498 16,
499 &xts_aes_info
500};
501
502static const mbedtls_cipher_info_t aes_256_xts_info = {
503 MBEDTLS_CIPHER_AES_256_XTS,
504 MBEDTLS_MODE_XTS,
505 512,
506 "AES-256-XTS",
507 16,
508 0,
509 16,
510 &xts_aes_info
511};
512#endif /* MBEDTLS_CIPHER_MODE_XTS */
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200515static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200516 unsigned int key_bitlen )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200517{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200518 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200519 key, key_bitlen );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200520}
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522static const mbedtls_cipher_base_t gcm_aes_info = {
523 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200524 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200526 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200529 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100530#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100531#if defined(MBEDTLS_CIPHER_MODE_OFB)
532 NULL,
533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200535 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100536#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100537#if defined(MBEDTLS_CIPHER_MODE_XTS)
538 NULL,
539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200541 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100542#endif
Paul Bakker43aff2a2013-09-09 00:10:27 +0200543 gcm_aes_setkey_wrap,
544 gcm_aes_setkey_wrap,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200545 gcm_ctx_alloc,
546 gcm_ctx_free,
547};
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549static const mbedtls_cipher_info_t aes_128_gcm_info = {
550 MBEDTLS_CIPHER_AES_128_GCM,
551 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100552 128,
553 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200554 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100556 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200557 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100558};
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560static const mbedtls_cipher_info_t aes_192_gcm_info = {
561 MBEDTLS_CIPHER_AES_192_GCM,
562 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200563 192,
564 "AES-192-GCM",
565 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200567 16,
568 &gcm_aes_info
569};
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571static const mbedtls_cipher_info_t aes_256_gcm_info = {
572 MBEDTLS_CIPHER_AES_256_GCM,
573 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100574 256,
575 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200576 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100578 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200579 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100580};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#endif /* MBEDTLS_GCM_C */
Paul Bakker68884e32013-01-07 18:20:04 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200584static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200585 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200586{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200587 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200588 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200589}
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591static const mbedtls_cipher_base_t ccm_aes_info = {
592 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200593 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200595 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200598 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100599#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100600#if defined(MBEDTLS_CIPHER_MODE_OFB)
601 NULL,
602#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200604 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100605#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100606#if defined(MBEDTLS_CIPHER_MODE_XTS)
607 NULL,
608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200610 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100611#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200612 ccm_aes_setkey_wrap,
613 ccm_aes_setkey_wrap,
614 ccm_ctx_alloc,
615 ccm_ctx_free,
616};
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618static const mbedtls_cipher_info_t aes_128_ccm_info = {
619 MBEDTLS_CIPHER_AES_128_CCM,
620 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200621 128,
622 "AES-128-CCM",
623 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200625 16,
626 &ccm_aes_info
627};
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629static const mbedtls_cipher_info_t aes_192_ccm_info = {
630 MBEDTLS_CIPHER_AES_192_CCM,
631 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200632 192,
633 "AES-192-CCM",
634 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200636 16,
637 &ccm_aes_info
638};
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640static const mbedtls_cipher_info_t aes_256_ccm_info = {
641 MBEDTLS_CIPHER_AES_256_CCM,
642 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200643 256,
644 "AES-256-CCM",
645 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200647 16,
648 &ccm_aes_info
649};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_AES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200657 const unsigned char *input, unsigned char *output )
658{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200660 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200661}
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_CIPHER_MODE_CBC)
664static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200665 size_t length, unsigned char *iv,
666 const unsigned char *input, unsigned char *output )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200669 input, output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_CIPHER_MODE_CFB)
674static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200675 size_t length, size_t *iv_off, unsigned char *iv,
676 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000677{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200679 iv_off, iv, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200684static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
685 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000686 const unsigned char *input, unsigned char *output )
687{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200689 nonce_counter, stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000692
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200693static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200694 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000695{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200696 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000697}
698
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200699static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200700 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000701{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200702 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703}
704
705static void * camellia_ctx_alloc( void )
706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_camellia_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200708 ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200709
710 if( ctx == NULL )
711 return( NULL );
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_camellia_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200714
715 return( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716}
717
718static void camellia_ctx_free( void *ctx )
719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
721 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000722}
723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724static const mbedtls_cipher_base_t camellia_info = {
725 MBEDTLS_CIPHER_ID_CAMELLIA,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200726 camellia_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000728 camellia_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100729#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000731 camellia_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100732#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100733#if defined(MBEDTLS_CIPHER_MODE_OFB)
734 NULL,
735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000737 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100738#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100739#if defined(MBEDTLS_CIPHER_MODE_XTS)
740 NULL,
741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200743 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100744#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000745 camellia_setkey_enc_wrap,
746 camellia_setkey_dec_wrap,
747 camellia_ctx_alloc,
748 camellia_ctx_free
749};
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751static const mbedtls_cipher_info_t camellia_128_ecb_info = {
752 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
753 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200754 128,
755 "CAMELLIA-128-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +0100756 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200757 0,
758 16,
759 &camellia_info
760};
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762static const mbedtls_cipher_info_t camellia_192_ecb_info = {
763 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
764 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200765 192,
766 "CAMELLIA-192-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +0100767 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200768 0,
769 16,
770 &camellia_info
771};
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773static const mbedtls_cipher_info_t camellia_256_ecb_info = {
774 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
775 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200776 256,
777 "CAMELLIA-256-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +0100778 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200779 0,
780 16,
781 &camellia_info
782};
783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_CIPHER_MODE_CBC)
785static const mbedtls_cipher_info_t camellia_128_cbc_info = {
786 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
787 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000788 128,
789 "CAMELLIA-128-CBC",
790 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200791 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000792 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000793 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000794};
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796static const mbedtls_cipher_info_t camellia_192_cbc_info = {
797 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
798 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000799 192,
800 "CAMELLIA-192-CBC",
801 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200802 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000803 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000804 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000805};
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807static const mbedtls_cipher_info_t camellia_256_cbc_info = {
808 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
809 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000810 256,
811 "CAMELLIA-256-CBC",
812 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200813 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000814 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000815 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000816};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#if defined(MBEDTLS_CIPHER_MODE_CFB)
820static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
821 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
822 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000823 128,
824 "CAMELLIA-128-CFB128",
825 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200826 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000827 16,
828 &camellia_info
829};
830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
832 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
833 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000834 192,
835 "CAMELLIA-192-CFB128",
836 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200837 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000838 16,
839 &camellia_info
840};
841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
843 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
844 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000845 256,
846 "CAMELLIA-256-CFB128",
847 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200848 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000849 16,
850 &camellia_info
851};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854#if defined(MBEDTLS_CIPHER_MODE_CTR)
855static const mbedtls_cipher_info_t camellia_128_ctr_info = {
856 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
857 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000858 128,
859 "CAMELLIA-128-CTR",
860 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200861 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000862 16,
863 &camellia_info
864};
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866static const mbedtls_cipher_info_t camellia_192_ctr_info = {
867 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
868 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000869 192,
870 "CAMELLIA-192-CTR",
871 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200872 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000873 16,
874 &camellia_info
875};
876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877static const mbedtls_cipher_info_t camellia_256_ctr_info = {
878 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
879 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000880 256,
881 "CAMELLIA-256-CTR",
882 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200883 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000884 16,
885 &camellia_info
886};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200890static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200891 unsigned int key_bitlen )
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200892{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200893 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200894 key, key_bitlen );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200895}
896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897static const mbedtls_cipher_base_t gcm_camellia_info = {
898 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200899 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200901 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200904 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100905#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100906#if defined(MBEDTLS_CIPHER_MODE_OFB)
907 NULL,
908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200910 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100911#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100912#if defined(MBEDTLS_CIPHER_MODE_XTS)
913 NULL,
914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200916 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100917#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200918 gcm_camellia_setkey_wrap,
919 gcm_camellia_setkey_wrap,
920 gcm_ctx_alloc,
921 gcm_ctx_free,
922};
923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924static const mbedtls_cipher_info_t camellia_128_gcm_info = {
925 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
926 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200927 128,
928 "CAMELLIA-128-GCM",
929 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200931 16,
932 &gcm_camellia_info
933};
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935static const mbedtls_cipher_info_t camellia_192_gcm_info = {
936 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
937 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200938 192,
939 "CAMELLIA-192-GCM",
940 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200942 16,
943 &gcm_camellia_info
944};
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946static const mbedtls_cipher_info_t camellia_256_gcm_info = {
947 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
948 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200949 256,
950 "CAMELLIA-256-GCM",
951 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200953 16,
954 &gcm_camellia_info
955};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200959static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200960 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200961{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200962 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200963 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200964}
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966static const mbedtls_cipher_base_t ccm_camellia_info = {
967 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200968 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200970 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100971#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200973 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100974#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100975#if defined(MBEDTLS_CIPHER_MODE_OFB)
976 NULL,
977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200979 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100980#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100981#if defined(MBEDTLS_CIPHER_MODE_XTS)
982 NULL,
983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200985 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100986#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200987 ccm_camellia_setkey_wrap,
988 ccm_camellia_setkey_wrap,
989 ccm_ctx_alloc,
990 ccm_ctx_free,
991};
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993static const mbedtls_cipher_info_t camellia_128_ccm_info = {
994 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
995 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200996 128,
997 "CAMELLIA-128-CCM",
998 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001000 16,
1001 &ccm_camellia_info
1002};
1003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004static const mbedtls_cipher_info_t camellia_192_ccm_info = {
1005 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
1006 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001007 192,
1008 "CAMELLIA-192-CCM",
1009 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001011 16,
1012 &ccm_camellia_info
1013};
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015static const mbedtls_cipher_info_t camellia_256_ccm_info = {
1016 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
1017 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001018 256,
1019 "CAMELLIA-256-CCM",
1020 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001022 16,
1023 &ccm_camellia_info
1024};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#endif /* MBEDTLS_CAMELLIA_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001028
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001029#if defined(MBEDTLS_ARIA_C)
1030
1031static int aria_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
1032 const unsigned char *input, unsigned char *output )
1033{
Manuel Pégourié-Gonnard08c337d2018-05-22 13:18:01 +02001034 (void) operation;
1035 return mbedtls_aria_crypt_ecb( (mbedtls_aria_context *) ctx, input,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001036 output );
1037}
1038
1039#if defined(MBEDTLS_CIPHER_MODE_CBC)
1040static int aria_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
1041 size_t length, unsigned char *iv,
1042 const unsigned char *input, unsigned char *output )
1043{
Manuel Pégourié-Gonnard39f25612018-05-24 14:06:02 +02001044 return mbedtls_aria_crypt_cbc( (mbedtls_aria_context *) ctx, operation, length, iv,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001045 input, output );
1046}
1047#endif /* MBEDTLS_CIPHER_MODE_CBC */
1048
1049#if defined(MBEDTLS_CIPHER_MODE_CFB)
1050static int aria_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
1051 size_t length, size_t *iv_off, unsigned char *iv,
1052 const unsigned char *input, unsigned char *output )
1053{
1054 return mbedtls_aria_crypt_cfb128( (mbedtls_aria_context *) ctx, operation, length,
1055 iv_off, iv, input, output );
1056}
1057#endif /* MBEDTLS_CIPHER_MODE_CFB */
1058
1059#if defined(MBEDTLS_CIPHER_MODE_CTR)
1060static int aria_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1061 unsigned char *nonce_counter, unsigned char *stream_block,
1062 const unsigned char *input, unsigned char *output )
1063{
1064 return mbedtls_aria_crypt_ctr( (mbedtls_aria_context *) ctx, length, nc_off,
1065 nonce_counter, stream_block, input, output );
1066}
1067#endif /* MBEDTLS_CIPHER_MODE_CTR */
1068
1069static int aria_setkey_dec_wrap( void *ctx, const unsigned char *key,
1070 unsigned int key_bitlen )
1071{
1072 return mbedtls_aria_setkey_dec( (mbedtls_aria_context *) ctx, key, key_bitlen );
1073}
1074
1075static int aria_setkey_enc_wrap( void *ctx, const unsigned char *key,
1076 unsigned int key_bitlen )
1077{
1078 return mbedtls_aria_setkey_enc( (mbedtls_aria_context *) ctx, key, key_bitlen );
1079}
1080
1081static void * aria_ctx_alloc( void )
1082{
1083 mbedtls_aria_context *ctx;
1084 ctx = mbedtls_calloc( 1, sizeof( mbedtls_aria_context ) );
1085
1086 if( ctx == NULL )
1087 return( NULL );
1088
1089 mbedtls_aria_init( ctx );
1090
1091 return( ctx );
1092}
1093
1094static void aria_ctx_free( void *ctx )
1095{
1096 mbedtls_aria_free( (mbedtls_aria_context *) ctx );
1097 mbedtls_free( ctx );
1098}
1099
1100static const mbedtls_cipher_base_t aria_info = {
1101 MBEDTLS_CIPHER_ID_ARIA,
1102 aria_crypt_ecb_wrap,
1103#if defined(MBEDTLS_CIPHER_MODE_CBC)
1104 aria_crypt_cbc_wrap,
1105#endif
1106#if defined(MBEDTLS_CIPHER_MODE_CFB)
1107 aria_crypt_cfb128_wrap,
1108#endif
Simon Butcher4844bf22018-06-11 15:21:05 +01001109#if defined(MBEDTLS_CIPHER_MODE_OFB)
1110 NULL,
1111#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001112#if defined(MBEDTLS_CIPHER_MODE_CTR)
1113 aria_crypt_ctr_wrap,
1114#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001115#if defined(MBEDTLS_CIPHER_MODE_XTS)
1116 NULL,
1117#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001118#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1119 NULL,
1120#endif
1121 aria_setkey_enc_wrap,
1122 aria_setkey_dec_wrap,
1123 aria_ctx_alloc,
1124 aria_ctx_free
1125};
1126
1127static const mbedtls_cipher_info_t aria_128_ecb_info = {
1128 MBEDTLS_CIPHER_ARIA_128_ECB,
1129 MBEDTLS_MODE_ECB,
1130 128,
1131 "ARIA-128-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001132 0,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001133 0,
1134 16,
1135 &aria_info
1136};
1137
1138static const mbedtls_cipher_info_t aria_192_ecb_info = {
1139 MBEDTLS_CIPHER_ARIA_192_ECB,
1140 MBEDTLS_MODE_ECB,
1141 192,
1142 "ARIA-192-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001143 0,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001144 0,
1145 16,
1146 &aria_info
1147};
1148
1149static const mbedtls_cipher_info_t aria_256_ecb_info = {
1150 MBEDTLS_CIPHER_ARIA_256_ECB,
1151 MBEDTLS_MODE_ECB,
1152 256,
1153 "ARIA-256-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001154 0,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001155 0,
1156 16,
1157 &aria_info
1158};
1159
1160#if defined(MBEDTLS_CIPHER_MODE_CBC)
1161static const mbedtls_cipher_info_t aria_128_cbc_info = {
1162 MBEDTLS_CIPHER_ARIA_128_CBC,
1163 MBEDTLS_MODE_CBC,
1164 128,
1165 "ARIA-128-CBC",
1166 16,
1167 0,
1168 16,
1169 &aria_info
1170};
1171
1172static const mbedtls_cipher_info_t aria_192_cbc_info = {
1173 MBEDTLS_CIPHER_ARIA_192_CBC,
1174 MBEDTLS_MODE_CBC,
1175 192,
1176 "ARIA-192-CBC",
1177 16,
1178 0,
1179 16,
1180 &aria_info
1181};
1182
1183static const mbedtls_cipher_info_t aria_256_cbc_info = {
1184 MBEDTLS_CIPHER_ARIA_256_CBC,
1185 MBEDTLS_MODE_CBC,
1186 256,
1187 "ARIA-256-CBC",
1188 16,
1189 0,
1190 16,
1191 &aria_info
1192};
1193#endif /* MBEDTLS_CIPHER_MODE_CBC */
1194
1195#if defined(MBEDTLS_CIPHER_MODE_CFB)
1196static const mbedtls_cipher_info_t aria_128_cfb128_info = {
1197 MBEDTLS_CIPHER_ARIA_128_CFB128,
1198 MBEDTLS_MODE_CFB,
1199 128,
1200 "ARIA-128-CFB128",
1201 16,
1202 0,
1203 16,
1204 &aria_info
1205};
1206
1207static const mbedtls_cipher_info_t aria_192_cfb128_info = {
1208 MBEDTLS_CIPHER_ARIA_192_CFB128,
1209 MBEDTLS_MODE_CFB,
1210 192,
1211 "ARIA-192-CFB128",
1212 16,
1213 0,
1214 16,
1215 &aria_info
1216};
1217
1218static const mbedtls_cipher_info_t aria_256_cfb128_info = {
1219 MBEDTLS_CIPHER_ARIA_256_CFB128,
1220 MBEDTLS_MODE_CFB,
1221 256,
1222 "ARIA-256-CFB128",
1223 16,
1224 0,
1225 16,
1226 &aria_info
1227};
1228#endif /* MBEDTLS_CIPHER_MODE_CFB */
1229
1230#if defined(MBEDTLS_CIPHER_MODE_CTR)
1231static const mbedtls_cipher_info_t aria_128_ctr_info = {
1232 MBEDTLS_CIPHER_ARIA_128_CTR,
1233 MBEDTLS_MODE_CTR,
1234 128,
1235 "ARIA-128-CTR",
1236 16,
1237 0,
1238 16,
1239 &aria_info
1240};
1241
1242static const mbedtls_cipher_info_t aria_192_ctr_info = {
1243 MBEDTLS_CIPHER_ARIA_192_CTR,
1244 MBEDTLS_MODE_CTR,
1245 192,
1246 "ARIA-192-CTR",
1247 16,
1248 0,
1249 16,
1250 &aria_info
1251};
1252
1253static const mbedtls_cipher_info_t aria_256_ctr_info = {
1254 MBEDTLS_CIPHER_ARIA_256_CTR,
1255 MBEDTLS_MODE_CTR,
1256 256,
1257 "ARIA-256-CTR",
1258 16,
1259 0,
1260 16,
1261 &aria_info
1262};
1263#endif /* MBEDTLS_CIPHER_MODE_CTR */
1264
1265#if defined(MBEDTLS_GCM_C)
1266static int gcm_aria_setkey_wrap( void *ctx, const unsigned char *key,
1267 unsigned int key_bitlen )
1268{
1269 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1270 key, key_bitlen );
1271}
1272
1273static const mbedtls_cipher_base_t gcm_aria_info = {
1274 MBEDTLS_CIPHER_ID_ARIA,
1275 NULL,
1276#if defined(MBEDTLS_CIPHER_MODE_CBC)
1277 NULL,
1278#endif
1279#if defined(MBEDTLS_CIPHER_MODE_CFB)
1280 NULL,
1281#endif
Simon Butcher4844bf22018-06-11 15:21:05 +01001282#if defined(MBEDTLS_CIPHER_MODE_OFB)
1283 NULL,
1284#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001285#if defined(MBEDTLS_CIPHER_MODE_CTR)
1286 NULL,
1287#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001288#if defined(MBEDTLS_CIPHER_MODE_XTS)
1289 NULL,
1290#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001291#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1292 NULL,
1293#endif
1294 gcm_aria_setkey_wrap,
1295 gcm_aria_setkey_wrap,
1296 gcm_ctx_alloc,
1297 gcm_ctx_free,
1298};
1299
1300static const mbedtls_cipher_info_t aria_128_gcm_info = {
1301 MBEDTLS_CIPHER_ARIA_128_GCM,
1302 MBEDTLS_MODE_GCM,
1303 128,
1304 "ARIA-128-GCM",
1305 12,
1306 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1307 16,
1308 &gcm_aria_info
1309};
1310
1311static const mbedtls_cipher_info_t aria_192_gcm_info = {
1312 MBEDTLS_CIPHER_ARIA_192_GCM,
1313 MBEDTLS_MODE_GCM,
1314 192,
1315 "ARIA-192-GCM",
1316 12,
1317 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1318 16,
1319 &gcm_aria_info
1320};
1321
1322static const mbedtls_cipher_info_t aria_256_gcm_info = {
1323 MBEDTLS_CIPHER_ARIA_256_GCM,
1324 MBEDTLS_MODE_GCM,
1325 256,
1326 "ARIA-256-GCM",
1327 12,
1328 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1329 16,
1330 &gcm_aria_info
1331};
1332#endif /* MBEDTLS_GCM_C */
1333
1334#if defined(MBEDTLS_CCM_C)
1335static int ccm_aria_setkey_wrap( void *ctx, const unsigned char *key,
1336 unsigned int key_bitlen )
1337{
1338 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1339 key, key_bitlen );
1340}
1341
1342static const mbedtls_cipher_base_t ccm_aria_info = {
1343 MBEDTLS_CIPHER_ID_ARIA,
1344 NULL,
1345#if defined(MBEDTLS_CIPHER_MODE_CBC)
1346 NULL,
1347#endif
1348#if defined(MBEDTLS_CIPHER_MODE_CFB)
1349 NULL,
1350#endif
Simon Butcher7487c5b2018-04-29 00:24:51 +01001351#if defined(MBEDTLS_CIPHER_MODE_OFB)
1352 NULL,
1353#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001354#if defined(MBEDTLS_CIPHER_MODE_CTR)
1355 NULL,
1356#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001357#if defined(MBEDTLS_CIPHER_MODE_XTS)
1358 NULL,
1359#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001360#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1361 NULL,
1362#endif
1363 ccm_aria_setkey_wrap,
1364 ccm_aria_setkey_wrap,
1365 ccm_ctx_alloc,
1366 ccm_ctx_free,
1367};
1368
1369static const mbedtls_cipher_info_t aria_128_ccm_info = {
1370 MBEDTLS_CIPHER_ARIA_128_CCM,
1371 MBEDTLS_MODE_CCM,
1372 128,
1373 "ARIA-128-CCM",
1374 12,
1375 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1376 16,
1377 &ccm_aria_info
1378};
1379
1380static const mbedtls_cipher_info_t aria_192_ccm_info = {
1381 MBEDTLS_CIPHER_ARIA_192_CCM,
1382 MBEDTLS_MODE_CCM,
1383 192,
1384 "ARIA-192-CCM",
1385 12,
1386 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1387 16,
1388 &ccm_aria_info
1389};
1390
1391static const mbedtls_cipher_info_t aria_256_ccm_info = {
1392 MBEDTLS_CIPHER_ARIA_256_CCM,
1393 MBEDTLS_MODE_CCM,
1394 256,
1395 "ARIA-256-CCM",
1396 12,
1397 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1398 16,
1399 &ccm_aria_info
1400};
1401#endif /* MBEDTLS_CCM_C */
1402
1403#endif /* MBEDTLS_ARIA_C */
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#if defined(MBEDTLS_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001408 const unsigned char *input, unsigned char *output )
1409{
1410 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001412}
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001415 const unsigned char *input, unsigned char *output )
1416{
1417 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001419}
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#if defined(MBEDTLS_CIPHER_MODE_CBC)
1422static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +00001423 unsigned char *iv, const unsigned char *input, unsigned char *output )
1424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001426 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_CIPHER_MODE_CBC)
1431static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +00001432 unsigned char *iv, const unsigned char *input, unsigned char *output )
1433{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001435 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001438
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001439static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001440 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001441{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001442 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001445}
1446
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001447static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001448 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001449{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001450 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001453}
1454
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001455static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001456 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001457{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001458 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001461}
1462
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001463static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001464 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001465{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001466 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001469}
1470
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001471static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001472 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001473{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001474 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001477}
1478
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001479static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001480 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001481{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001482 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001485}
1486
1487static void * des_ctx_alloc( void )
1488{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001489 mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001490
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001491 if( des == NULL )
1492 return( NULL );
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_des_init( des );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001495
1496 return( des );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001497}
1498
1499static void des_ctx_free( void *ctx )
1500{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 mbedtls_des_free( (mbedtls_des_context *) ctx );
1502 mbedtls_free( ctx );
Paul Bakker34617722014-06-13 17:20:13 +02001503}
1504
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001505static void * des3_ctx_alloc( void )
1506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_des3_context *des3;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001508 des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001509
1510 if( des3 == NULL )
1511 return( NULL );
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 mbedtls_des3_init( des3 );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001514
1515 return( des3 );
1516}
1517
Paul Bakker34617722014-06-13 17:20:13 +02001518static void des3_ctx_free( void *ctx )
1519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 mbedtls_des3_free( (mbedtls_des3_context *) ctx );
1521 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001522}
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524static const mbedtls_cipher_base_t des_info = {
1525 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001526 des_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +00001528 des_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001529#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001531 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001532#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001533#if defined(MBEDTLS_CIPHER_MODE_OFB)
1534 NULL,
1535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001537 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001538#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001539#if defined(MBEDTLS_CIPHER_MODE_XTS)
1540 NULL,
1541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001543 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001544#endif
Paul Bakker23986e52011-04-24 08:57:21 +00001545 des_setkey_enc_wrap,
1546 des_setkey_dec_wrap,
1547 des_ctx_alloc,
1548 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +00001549};
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551static const mbedtls_cipher_info_t des_ecb_info = {
1552 MBEDTLS_CIPHER_DES_ECB,
1553 MBEDTLS_MODE_ECB,
1554 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001555 "DES-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001556 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001557 0,
1558 8,
1559 &des_info
1560};
1561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_CIPHER_MODE_CBC)
1563static const mbedtls_cipher_info_t des_cbc_info = {
1564 MBEDTLS_CIPHER_DES_CBC,
1565 MBEDTLS_MODE_CBC,
1566 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker343a8702011-06-09 14:27:58 +00001567 "DES-CBC",
1568 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001569 0,
Paul Bakker343a8702011-06-09 14:27:58 +00001570 8,
1571 &des_info
1572};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575static const mbedtls_cipher_base_t des_ede_info = {
1576 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001577 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +00001579 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001580#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001582 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001583#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001584#if defined(MBEDTLS_CIPHER_MODE_OFB)
1585 NULL,
1586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001588 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001589#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001590#if defined(MBEDTLS_CIPHER_MODE_XTS)
1591 NULL,
1592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001594 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001595#endif
Paul Bakker23986e52011-04-24 08:57:21 +00001596 des3_set2key_enc_wrap,
1597 des3_set2key_dec_wrap,
1598 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001599 des3_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +00001600};
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602static const mbedtls_cipher_info_t des_ede_ecb_info = {
1603 MBEDTLS_CIPHER_DES_EDE_ECB,
1604 MBEDTLS_MODE_ECB,
1605 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001606 "DES-EDE-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001607 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001608 0,
1609 8,
1610 &des_ede_info
1611};
1612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#if defined(MBEDTLS_CIPHER_MODE_CBC)
1614static const mbedtls_cipher_info_t des_ede_cbc_info = {
1615 MBEDTLS_CIPHER_DES_EDE_CBC,
1616 MBEDTLS_MODE_CBC,
1617 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker343a8702011-06-09 14:27:58 +00001618 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +02001619 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001620 0,
Paul Bakker0e342352013-06-24 19:33:02 +02001621 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001622 &des_ede_info
1623};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626static const mbedtls_cipher_base_t des_ede3_info = {
Manuel Pégourié-Gonnard9d515832015-06-02 10:00:04 +01001627 MBEDTLS_CIPHER_ID_3DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001628 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +00001630 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001631#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001633 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001634#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001635#if defined(MBEDTLS_CIPHER_MODE_OFB)
1636 NULL,
1637#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001639 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001640#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001641#if defined(MBEDTLS_CIPHER_MODE_XTS)
1642 NULL,
1643#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001645 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001646#endif
Paul Bakker343a8702011-06-09 14:27:58 +00001647 des3_set3key_enc_wrap,
1648 des3_set3key_dec_wrap,
1649 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001650 des3_ctx_free
Paul Bakker343a8702011-06-09 14:27:58 +00001651};
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1654 MBEDTLS_CIPHER_DES_EDE3_ECB,
1655 MBEDTLS_MODE_ECB,
1656 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001657 "DES-EDE3-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001658 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001659 0,
1660 8,
1661 &des_ede3_info
1662};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_CIPHER_MODE_CBC)
1664static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1665 MBEDTLS_CIPHER_DES_EDE3_CBC,
1666 MBEDTLS_MODE_CBC,
1667 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker23986e52011-04-24 08:57:21 +00001668 "DES-EDE3-CBC",
1669 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001670 0,
Paul Bakker23986e52011-04-24 08:57:21 +00001671 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001672 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +00001673};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#endif /* MBEDTLS_CIPHER_MODE_CBC */
1675#endif /* MBEDTLS_DES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#if defined(MBEDTLS_BLOWFISH_C)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001680 const unsigned char *input, unsigned char *output )
1681{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001683 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001684}
1685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#if defined(MBEDTLS_CIPHER_MODE_CBC)
1687static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001688 size_t length, unsigned char *iv, const unsigned char *input,
1689 unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001690{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001692 input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#if defined(MBEDTLS_CIPHER_MODE_CFB)
1697static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001698 size_t length, size_t *iv_off, unsigned char *iv,
1699 const unsigned char *input, unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001700{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001702 iv_off, iv, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001707static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1708 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001709 const unsigned char *input, unsigned char *output )
1710{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001712 nonce_counter, stream_block, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001713}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001715
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001716static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001717 unsigned int key_bitlen )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001718{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001719 return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001720}
1721
1722static void * blowfish_ctx_alloc( void )
1723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 mbedtls_blowfish_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001725 ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001726
1727 if( ctx == NULL )
1728 return( NULL );
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 mbedtls_blowfish_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001731
1732 return( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001733}
1734
1735static void blowfish_ctx_free( void *ctx )
1736{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
1738 mbedtls_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001739}
1740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741static const mbedtls_cipher_base_t blowfish_info = {
1742 MBEDTLS_CIPHER_ID_BLOWFISH,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001743 blowfish_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001745 blowfish_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001746#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001748 blowfish_crypt_cfb64_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001749#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001750#if defined(MBEDTLS_CIPHER_MODE_OFB)
1751 NULL,
1752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001754 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001755#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001756#if defined(MBEDTLS_CIPHER_MODE_XTS)
1757 NULL,
1758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001760 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001761#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001762 blowfish_setkey_wrap,
1763 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001764 blowfish_ctx_alloc,
1765 blowfish_ctx_free
1766};
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768static const mbedtls_cipher_info_t blowfish_ecb_info = {
1769 MBEDTLS_CIPHER_BLOWFISH_ECB,
1770 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001771 128,
1772 "BLOWFISH-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001773 0,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001775 8,
1776 &blowfish_info
1777};
1778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779#if defined(MBEDTLS_CIPHER_MODE_CBC)
1780static const mbedtls_cipher_info_t blowfish_cbc_info = {
1781 MBEDTLS_CIPHER_BLOWFISH_CBC,
1782 MBEDTLS_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001783 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001784 "BLOWFISH-CBC",
1785 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001787 8,
1788 &blowfish_info
1789};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#if defined(MBEDTLS_CIPHER_MODE_CFB)
1793static const mbedtls_cipher_info_t blowfish_cfb64_info = {
1794 MBEDTLS_CIPHER_BLOWFISH_CFB64,
1795 MBEDTLS_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001796 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001797 "BLOWFISH-CFB64",
1798 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001800 8,
1801 &blowfish_info
1802};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805#if defined(MBEDTLS_CIPHER_MODE_CTR)
1806static const mbedtls_cipher_info_t blowfish_ctr_info = {
1807 MBEDTLS_CIPHER_BLOWFISH_CTR,
1808 MBEDTLS_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001809 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001810 "BLOWFISH-CTR",
1811 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001813 8,
1814 &blowfish_info
1815};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_CIPHER_MODE_CTR */
1817#endif /* MBEDTLS_BLOWFISH_C */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001820static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1821 const unsigned char *input,
1822 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +01001823{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001825}
1826
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001827static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001828 unsigned int key_bitlen )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001829{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001830 /* we get key_bitlen in bits, arc4 expects it in bytes */
1831 if( key_bitlen % 8 != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardce411252013-09-04 12:28:37 +02001833
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001834 mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001835 return( 0 );
1836}
1837
1838static void * arc4_ctx_alloc( void )
1839{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 mbedtls_arc4_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001841 ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001842
1843 if( ctx == NULL )
1844 return( NULL );
1845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 mbedtls_arc4_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001847
1848 return( ctx );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001849}
Paul Bakker68884e32013-01-07 18:20:04 +01001850
1851static void arc4_ctx_free( void *ctx )
1852{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
1854 mbedtls_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +01001855}
1856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857static const mbedtls_cipher_base_t arc4_base_info = {
1858 MBEDTLS_CIPHER_ID_ARC4,
Paul Bakker68884e32013-01-07 18:20:04 +01001859 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker68884e32013-01-07 18:20:04 +01001861 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker68884e32013-01-07 18:20:04 +01001864 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001865#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001866#if defined(MBEDTLS_CIPHER_MODE_OFB)
1867 NULL,
1868#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02001870 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001871#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001872#if defined(MBEDTLS_CIPHER_MODE_XTS)
1873 NULL,
1874#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001876 arc4_crypt_stream_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001877#endif
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001878 arc4_setkey_wrap,
1879 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +01001880 arc4_ctx_alloc,
1881 arc4_ctx_free
1882};
1883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884static const mbedtls_cipher_info_t arc4_128_info = {
1885 MBEDTLS_CIPHER_ARC4_128,
1886 MBEDTLS_MODE_STREAM,
Paul Bakker68884e32013-01-07 18:20:04 +01001887 128,
1888 "ARC4-128",
1889 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001890 0,
Paul Bakker68884e32013-01-07 18:20:04 +01001891 1,
1892 &arc4_base_info
1893};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894#endif /* MBEDTLS_ARC4_C */
Paul Bakker68884e32013-01-07 18:20:04 +01001895
Daniel Kingbd920622016-05-15 19:56:20 -03001896#if defined(MBEDTLS_CHACHA20_C)
1897
1898static int chacha20_setkey_wrap( void *ctx, const unsigned char *key,
1899 unsigned int key_bitlen )
1900{
1901 if( key_bitlen != 256U )
1902 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1903
1904 if ( 0 != mbedtls_chacha20_setkey( (mbedtls_chacha20_context*)ctx, key ) )
1905 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1906
1907 return( 0 );
1908}
1909
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001910static int chacha20_stream_wrap( void *ctx, size_t length,
1911 const unsigned char *input,
1912 unsigned char *output )
1913{
Janos Follath24eed8d2019-11-22 13:21:35 +00001914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001915
1916 ret = mbedtls_chacha20_update( ctx, length, input, output );
1917 if( ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA )
1918 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1919
1920 return( ret );
1921}
1922
Daniel Kingbd920622016-05-15 19:56:20 -03001923static void * chacha20_ctx_alloc( void )
1924{
1925 mbedtls_chacha20_context *ctx;
1926 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chacha20_context ) );
1927
1928 if( ctx == NULL )
1929 return( NULL );
1930
1931 mbedtls_chacha20_init( ctx );
1932
1933 return( ctx );
1934}
1935
1936static void chacha20_ctx_free( void *ctx )
1937{
1938 mbedtls_chacha20_free( (mbedtls_chacha20_context *) ctx );
1939 mbedtls_free( ctx );
1940}
1941
1942static const mbedtls_cipher_base_t chacha20_base_info = {
1943 MBEDTLS_CIPHER_ID_CHACHA20,
1944 NULL,
1945#if defined(MBEDTLS_CIPHER_MODE_CBC)
1946 NULL,
1947#endif
1948#if defined(MBEDTLS_CIPHER_MODE_CFB)
1949 NULL,
1950#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02001951#if defined(MBEDTLS_CIPHER_MODE_OFB)
1952 NULL,
1953#endif
Daniel Kingbd920622016-05-15 19:56:20 -03001954#if defined(MBEDTLS_CIPHER_MODE_CTR)
1955 NULL,
1956#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02001957#if defined(MBEDTLS_CIPHER_MODE_XTS)
1958 NULL,
1959#endif
Daniel Kingbd920622016-05-15 19:56:20 -03001960#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001961 chacha20_stream_wrap,
Daniel Kingbd920622016-05-15 19:56:20 -03001962#endif
1963 chacha20_setkey_wrap,
1964 chacha20_setkey_wrap,
1965 chacha20_ctx_alloc,
1966 chacha20_ctx_free
1967};
1968static const mbedtls_cipher_info_t chacha20_info = {
1969 MBEDTLS_CIPHER_CHACHA20,
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001970 MBEDTLS_MODE_STREAM,
Daniel Kingbd920622016-05-15 19:56:20 -03001971 256,
1972 "CHACHA20",
1973 12,
1974 0,
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001975 1,
Daniel Kingbd920622016-05-15 19:56:20 -03001976 &chacha20_base_info
1977};
1978#endif /* MBEDTLS_CHACHA20_C */
1979
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001980#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001981
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001982static int chachapoly_setkey_wrap( void *ctx,
1983 const unsigned char *key,
1984 unsigned int key_bitlen )
Daniel King8fe47012016-05-17 20:33:28 -03001985{
1986 if( key_bitlen != 256U )
1987 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1988
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001989 if ( 0 != mbedtls_chachapoly_setkey( (mbedtls_chachapoly_context*)ctx, key ) )
Daniel King8fe47012016-05-17 20:33:28 -03001990 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1991
1992 return( 0 );
1993}
1994
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001995static void * chachapoly_ctx_alloc( void )
Daniel King8fe47012016-05-17 20:33:28 -03001996{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001997 mbedtls_chachapoly_context *ctx;
1998 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chachapoly_context ) );
Daniel King8fe47012016-05-17 20:33:28 -03001999
2000 if( ctx == NULL )
2001 return( NULL );
2002
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002003 mbedtls_chachapoly_init( ctx );
Daniel King8fe47012016-05-17 20:33:28 -03002004
2005 return( ctx );
2006}
2007
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002008static void chachapoly_ctx_free( void *ctx )
Daniel King8fe47012016-05-17 20:33:28 -03002009{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002010 mbedtls_chachapoly_free( (mbedtls_chachapoly_context *) ctx );
Daniel King8fe47012016-05-17 20:33:28 -03002011 mbedtls_free( ctx );
2012}
2013
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002014static const mbedtls_cipher_base_t chachapoly_base_info = {
Daniel King8fe47012016-05-17 20:33:28 -03002015 MBEDTLS_CIPHER_ID_CHACHA20,
2016 NULL,
2017#if defined(MBEDTLS_CIPHER_MODE_CBC)
2018 NULL,
2019#endif
2020#if defined(MBEDTLS_CIPHER_MODE_CFB)
2021 NULL,
2022#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02002023#if defined(MBEDTLS_CIPHER_MODE_OFB)
2024 NULL,
2025#endif
Daniel King8fe47012016-05-17 20:33:28 -03002026#if defined(MBEDTLS_CIPHER_MODE_CTR)
2027 NULL,
2028#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02002029#if defined(MBEDTLS_CIPHER_MODE_XTS)
2030 NULL,
2031#endif
Daniel King8fe47012016-05-17 20:33:28 -03002032#if defined(MBEDTLS_CIPHER_MODE_STREAM)
2033 NULL,
2034#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002035 chachapoly_setkey_wrap,
2036 chachapoly_setkey_wrap,
2037 chachapoly_ctx_alloc,
2038 chachapoly_ctx_free
Daniel King8fe47012016-05-17 20:33:28 -03002039};
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002040static const mbedtls_cipher_info_t chachapoly_info = {
Daniel King8fe47012016-05-17 20:33:28 -03002041 MBEDTLS_CIPHER_CHACHA20_POLY1305,
Manuel Pégourié-Gonnardf57bf8b2018-06-18 11:14:09 +02002042 MBEDTLS_MODE_CHACHAPOLY,
Daniel King8fe47012016-05-17 20:33:28 -03002043 256,
2044 "CHACHA20-POLY1305",
2045 12,
2046 0,
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02002047 1,
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002048 &chachapoly_base_info
Daniel King8fe47012016-05-17 20:33:28 -03002049};
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002050#endif /* MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -03002051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002053static int null_crypt_stream( void *ctx, size_t length,
2054 const unsigned char *input,
2055 unsigned char *output )
2056{
2057 ((void) ctx);
2058 memmove( output, input, length );
2059 return( 0 );
2060}
2061
2062static int null_setkey( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02002063 unsigned int key_bitlen )
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002064{
2065 ((void) ctx);
2066 ((void) key);
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02002067 ((void) key_bitlen);
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002068
2069 return( 0 );
2070}
2071
Paul Bakkerfab5c822012-02-06 16:45:10 +00002072static void * null_ctx_alloc( void )
2073{
Manuel Pégourié-Gonnard86bbc7f2014-07-12 02:14:41 +02002074 return( (void *) 1 );
Paul Bakkerfab5c822012-02-06 16:45:10 +00002075}
2076
Paul Bakkerfab5c822012-02-06 16:45:10 +00002077static void null_ctx_free( void *ctx )
2078{
2079 ((void) ctx);
2080}
2081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082static const mbedtls_cipher_base_t null_base_info = {
2083 MBEDTLS_CIPHER_ID_NULL,
Paul Bakkerfab5c822012-02-06 16:45:10 +00002084 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerfab5c822012-02-06 16:45:10 +00002086 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01002087#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkerfab5c822012-02-06 16:45:10 +00002089 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01002090#endif
Simon Butcher4844bf22018-06-11 15:21:05 +01002091#if defined(MBEDTLS_CIPHER_MODE_OFB)
2092 NULL,
2093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02002095 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01002096#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01002097#if defined(MBEDTLS_CIPHER_MODE_XTS)
2098 NULL,
2099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002101 null_crypt_stream,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01002102#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002103 null_setkey,
2104 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +00002105 null_ctx_alloc,
2106 null_ctx_free
2107};
2108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109static const mbedtls_cipher_info_t null_cipher_info = {
2110 MBEDTLS_CIPHER_NULL,
2111 MBEDTLS_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +00002112 0,
2113 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +01002114 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02002115 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +00002116 1,
2117 &null_base_info
2118};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119#endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
Paul Bakkerfab5c822012-02-06 16:45:10 +00002120
Jack Lloydffdf2882019-03-07 17:00:32 -05002121#if defined(MBEDTLS_NIST_KW_C)
2122static void *kw_ctx_alloc( void )
2123{
2124 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_nist_kw_context ) );
2125
2126 if( ctx != NULL )
2127 mbedtls_nist_kw_init( (mbedtls_nist_kw_context *) ctx );
2128
2129 return( ctx );
2130}
2131
2132static void kw_ctx_free( void *ctx )
2133{
2134 mbedtls_nist_kw_free( ctx );
2135 mbedtls_free( ctx );
2136}
2137
2138static int kw_aes_setkey_wrap( void *ctx, const unsigned char *key,
2139 unsigned int key_bitlen )
2140{
Jack Lloyd5f289992019-04-02 10:07:28 -07002141 return mbedtls_nist_kw_setkey( (mbedtls_nist_kw_context *) ctx,
2142 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 1 );
Jack Lloydffdf2882019-03-07 17:00:32 -05002143}
2144
2145static int kw_aes_setkey_unwrap( void *ctx, const unsigned char *key,
2146 unsigned int key_bitlen )
2147{
Jack Lloyd5f289992019-04-02 10:07:28 -07002148 return mbedtls_nist_kw_setkey( (mbedtls_nist_kw_context *) ctx,
2149 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 0 );
Jack Lloydffdf2882019-03-07 17:00:32 -05002150}
2151
2152static const mbedtls_cipher_base_t kw_aes_info = {
2153 MBEDTLS_CIPHER_ID_AES,
2154 NULL,
2155#if defined(MBEDTLS_CIPHER_MODE_CBC)
2156 NULL,
2157#endif
2158#if defined(MBEDTLS_CIPHER_MODE_CFB)
2159 NULL,
2160#endif
2161#if defined(MBEDTLS_CIPHER_MODE_OFB)
2162 NULL,
2163#endif
2164#if defined(MBEDTLS_CIPHER_MODE_CTR)
2165 NULL,
2166#endif
2167#if defined(MBEDTLS_CIPHER_MODE_XTS)
2168 NULL,
2169#endif
2170#if defined(MBEDTLS_CIPHER_MODE_STREAM)
2171 NULL,
2172#endif
2173 kw_aes_setkey_wrap,
2174 kw_aes_setkey_unwrap,
2175 kw_ctx_alloc,
2176 kw_ctx_free,
2177};
2178
2179static const mbedtls_cipher_info_t aes_128_nist_kw_info = {
2180 MBEDTLS_CIPHER_AES_128_KW,
2181 MBEDTLS_MODE_KW,
2182 128,
2183 "AES-128-KW",
2184 0,
2185 0,
2186 16,
2187 &kw_aes_info
2188};
2189
2190static const mbedtls_cipher_info_t aes_192_nist_kw_info = {
2191 MBEDTLS_CIPHER_AES_192_KW,
2192 MBEDTLS_MODE_KW,
2193 192,
2194 "AES-192-KW",
2195 0,
2196 0,
2197 16,
2198 &kw_aes_info
2199};
2200
2201static const mbedtls_cipher_info_t aes_256_nist_kw_info = {
2202 MBEDTLS_CIPHER_AES_256_KW,
2203 MBEDTLS_MODE_KW,
2204 256,
2205 "AES-256-KW",
2206 0,
2207 0,
2208 16,
2209 &kw_aes_info
2210};
2211
2212static const mbedtls_cipher_info_t aes_128_nist_kwp_info = {
2213 MBEDTLS_CIPHER_AES_128_KWP,
2214 MBEDTLS_MODE_KWP,
2215 128,
2216 "AES-128-KWP",
2217 0,
2218 0,
2219 16,
2220 &kw_aes_info
2221};
2222
2223static const mbedtls_cipher_info_t aes_192_nist_kwp_info = {
2224 MBEDTLS_CIPHER_AES_192_KWP,
2225 MBEDTLS_MODE_KWP,
2226 192,
2227 "AES-192-KWP",
2228 0,
2229 0,
2230 16,
2231 &kw_aes_info
2232};
2233
2234static const mbedtls_cipher_info_t aes_256_nist_kwp_info = {
2235 MBEDTLS_CIPHER_AES_256_KWP,
2236 MBEDTLS_MODE_KWP,
2237 256,
2238 "AES-256-KWP",
2239 0,
2240 0,
2241 16,
2242 &kw_aes_info
2243};
2244#endif /* MBEDTLS_NIST_KW_C */
2245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002247{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248#if defined(MBEDTLS_AES_C)
2249 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
2250 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
2251 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
2252#if defined(MBEDTLS_CIPHER_MODE_CBC)
2253 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
2254 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
2255 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002256#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_CIPHER_MODE_CFB)
2258 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
2259 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
2260 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002261#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01002262#if defined(MBEDTLS_CIPHER_MODE_OFB)
2263 { MBEDTLS_CIPHER_AES_128_OFB, &aes_128_ofb_info },
2264 { MBEDTLS_CIPHER_AES_192_OFB, &aes_192_ofb_info },
2265 { MBEDTLS_CIPHER_AES_256_OFB, &aes_256_ofb_info },
2266#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002267#if defined(MBEDTLS_CIPHER_MODE_CTR)
2268 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
2269 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
2270 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002271#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01002272#if defined(MBEDTLS_CIPHER_MODE_XTS)
2273 { MBEDTLS_CIPHER_AES_128_XTS, &aes_128_xts_info },
2274 { MBEDTLS_CIPHER_AES_256_XTS, &aes_256_xts_info },
2275#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276#if defined(MBEDTLS_GCM_C)
2277 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
2278 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
2279 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002280#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002281#if defined(MBEDTLS_CCM_C)
2282 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
2283 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
2284 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02002285#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#endif /* MBEDTLS_AES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288#if defined(MBEDTLS_ARC4_C)
2289 { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002290#endif
2291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#if defined(MBEDTLS_BLOWFISH_C)
2293 { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
2294#if defined(MBEDTLS_CIPHER_MODE_CBC)
2295 { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002296#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#if defined(MBEDTLS_CIPHER_MODE_CFB)
2298 { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002299#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_CIPHER_MODE_CTR)
2301 { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#endif /* MBEDTLS_BLOWFISH_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305#if defined(MBEDTLS_CAMELLIA_C)
2306 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
2307 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
2308 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
2309#if defined(MBEDTLS_CIPHER_MODE_CBC)
2310 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
2311 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
2312 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#if defined(MBEDTLS_CIPHER_MODE_CFB)
2315 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
2316 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
2317 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002318#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319#if defined(MBEDTLS_CIPHER_MODE_CTR)
2320 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
2321 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
2322 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002323#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324#if defined(MBEDTLS_GCM_C)
2325 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
2326 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
2327 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +02002328#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002329#if defined(MBEDTLS_CCM_C)
2330 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
2331 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
2332 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02002333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334#endif /* MBEDTLS_CAMELLIA_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002335
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002336#if defined(MBEDTLS_ARIA_C)
2337 { MBEDTLS_CIPHER_ARIA_128_ECB, &aria_128_ecb_info },
2338 { MBEDTLS_CIPHER_ARIA_192_ECB, &aria_192_ecb_info },
2339 { MBEDTLS_CIPHER_ARIA_256_ECB, &aria_256_ecb_info },
2340#if defined(MBEDTLS_CIPHER_MODE_CBC)
2341 { MBEDTLS_CIPHER_ARIA_128_CBC, &aria_128_cbc_info },
2342 { MBEDTLS_CIPHER_ARIA_192_CBC, &aria_192_cbc_info },
2343 { MBEDTLS_CIPHER_ARIA_256_CBC, &aria_256_cbc_info },
2344#endif
2345#if defined(MBEDTLS_CIPHER_MODE_CFB)
2346 { MBEDTLS_CIPHER_ARIA_128_CFB128, &aria_128_cfb128_info },
2347 { MBEDTLS_CIPHER_ARIA_192_CFB128, &aria_192_cfb128_info },
2348 { MBEDTLS_CIPHER_ARIA_256_CFB128, &aria_256_cfb128_info },
2349#endif
2350#if defined(MBEDTLS_CIPHER_MODE_CTR)
2351 { MBEDTLS_CIPHER_ARIA_128_CTR, &aria_128_ctr_info },
2352 { MBEDTLS_CIPHER_ARIA_192_CTR, &aria_192_ctr_info },
2353 { MBEDTLS_CIPHER_ARIA_256_CTR, &aria_256_ctr_info },
2354#endif
2355#if defined(MBEDTLS_GCM_C)
2356 { MBEDTLS_CIPHER_ARIA_128_GCM, &aria_128_gcm_info },
2357 { MBEDTLS_CIPHER_ARIA_192_GCM, &aria_192_gcm_info },
2358 { MBEDTLS_CIPHER_ARIA_256_GCM, &aria_256_gcm_info },
2359#endif
2360#if defined(MBEDTLS_CCM_C)
2361 { MBEDTLS_CIPHER_ARIA_128_CCM, &aria_128_ccm_info },
2362 { MBEDTLS_CIPHER_ARIA_192_CCM, &aria_192_ccm_info },
2363 { MBEDTLS_CIPHER_ARIA_256_CCM, &aria_256_ccm_info },
2364#endif
2365#endif /* MBEDTLS_ARIA_C */
2366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367#if defined(MBEDTLS_DES_C)
2368 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
2369 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
2370 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
2371#if defined(MBEDTLS_CIPHER_MODE_CBC)
2372 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
2373 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
2374 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002375#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#endif /* MBEDTLS_DES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002377
Daniel Kingbd920622016-05-15 19:56:20 -03002378#if defined(MBEDTLS_CHACHA20_C)
2379 { MBEDTLS_CIPHER_CHACHA20, &chacha20_info },
2380#endif
2381
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002382#if defined(MBEDTLS_CHACHAPOLY_C)
2383 { MBEDTLS_CIPHER_CHACHA20_POLY1305, &chachapoly_info },
Daniel King8fe47012016-05-17 20:33:28 -03002384#endif
2385
Jack Lloydffdf2882019-03-07 17:00:32 -05002386#if defined(MBEDTLS_NIST_KW_C)
2387 { MBEDTLS_CIPHER_AES_128_KW, &aes_128_nist_kw_info },
2388 { MBEDTLS_CIPHER_AES_192_KW, &aes_192_nist_kw_info },
2389 { MBEDTLS_CIPHER_AES_256_KW, &aes_256_nist_kw_info },
2390 { MBEDTLS_CIPHER_AES_128_KWP, &aes_128_nist_kwp_info },
2391 { MBEDTLS_CIPHER_AES_192_KWP, &aes_192_nist_kwp_info },
2392 { MBEDTLS_CIPHER_AES_256_KWP, &aes_256_nist_kwp_info },
2393#endif
2394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2396 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
2397#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002399 { MBEDTLS_CIPHER_NONE, NULL }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002400};
2401
Hanno Beckerc3d25b32018-11-08 16:01:22 +00002402#define NUM_CIPHERS ( sizeof(mbedtls_cipher_definitions) / \
2403 sizeof(mbedtls_cipher_definitions[0]) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404int mbedtls_cipher_supported[NUM_CIPHERS];
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406#endif /* MBEDTLS_CIPHER_C */