blob: 0ab49712bf3e269ae7100a825099ad05dd5447c1 [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 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02008 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000023 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8123e9d2011-01-06 15:37:30 +000024 */
25
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000033
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020034#include "mbedtls/cipher_internal.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000035
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +020036#if defined(MBEDTLS_CHACHAPOLY_C)
37#include "mbedtls/chachapoly.h"
Daniel Kingb437a982016-05-17 20:33:28 -030038#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000042#endif
43
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/arc4.h"
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020046#endif
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_CAMELLIA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000050#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000054#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_BLOWFISH_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/blowfish.h"
Paul Bakker6132d0a2012-07-04 17:10:40 +000058#endif
59
Daniel King0fe7b5b2016-05-15 19:56:20 -030060#if defined(MBEDTLS_CHACHA20_C)
61#include "mbedtls/chacha20.h"
62#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020066#endif
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020070#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard0c851ee2015-02-10 12:47:52 +000073#include <string.h>
74#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020078#else
Rich Evans00ab4702015-02-06 13:43:58 +000079#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020080#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020082#endif
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020085/* shared by all GCM ciphers */
86static void *gcm_ctx_alloc( void )
87{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +020088 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
89
90 if( ctx != NULL )
91 mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
92
93 return( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020094}
95
96static void gcm_ctx_free( void *ctx )
97{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_gcm_free( ctx );
99 mbedtls_free( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200100}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200104/* shared by all CCM ciphers */
105static void *ccm_ctx_alloc( void )
106{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +0200107 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
108
109 if( ctx != NULL )
110 mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
111
112 return( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200113}
114
115static void ccm_ctx_free( void *ctx )
116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_ccm_free( ctx );
118 mbedtls_free( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200119}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122#if defined(MBEDTLS_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200125 const unsigned char *input, unsigned char *output )
126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200128}
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_CIPHER_MODE_CBC)
131static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000132 unsigned char *iv, const unsigned char *input, unsigned char *output )
133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200135 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_CIPHER_MODE_CFB)
140static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200141 size_t length, size_t *iv_off, unsigned char *iv,
142 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200145 input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000146}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000148
Simon Butcher91e254c2018-04-22 22:58:07 +0100149#if defined(MBEDTLS_CIPHER_MODE_OFB)
150static int aes_crypt_ofb_wrap( void *ctx, size_t length, size_t *iv_off,
151 unsigned char *iv, const unsigned char *input, unsigned char *output )
152{
153 return mbedtls_aes_crypt_ofb( (mbedtls_aes_context *) ctx, length, iv_off,
154 iv, input, output );
155}
156#endif /* MBEDTLS_CIPHER_MODE_OFB */
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200159static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
160 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000161 const unsigned char *input, unsigned char *output )
162{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
Paul Bakker343a8702011-06-09 14:27:58 +0000164 stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000167
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100168#if defined(MBEDTLS_CIPHER_MODE_XTS)
169static int aes_crypt_xts_wrap( void *ctx, mbedtls_operation_t operation,
170 size_t length,
171 const unsigned char data_unit[16],
172 const unsigned char *input,
173 unsigned char *output )
174{
175 mbedtls_aes_xts_context *xts_ctx = ctx;
176 int mode;
177
178 switch (operation)
179 {
180 case MBEDTLS_ENCRYPT:
181 mode = MBEDTLS_AES_ENCRYPT;
182 break;
183 case MBEDTLS_DECRYPT:
184 mode = MBEDTLS_AES_DECRYPT;
185 break;
186 default:
187 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
188 }
189
190 return mbedtls_aes_crypt_xts( xts_ctx, mode, length,
191 data_unit, input, output );
192}
193#endif /* MBEDTLS_CIPHER_MODE_XTS */
194
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200195static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200196 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000197{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200198 return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000199}
200
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200201static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200202 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000203{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200204 return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000205}
206
207static void * aes_ctx_alloc( void )
208{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200209 mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200210
211 if( aes == NULL )
212 return( NULL );
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_aes_init( aes );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200215
216 return( aes );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217}
218
219static void aes_ctx_free( void *ctx )
220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_aes_free( (mbedtls_aes_context *) ctx );
222 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000223}
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225static const mbedtls_cipher_base_t aes_info = {
226 MBEDTLS_CIPHER_ID_AES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200227 aes_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000229 aes_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000232 aes_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100233#endif
Simon Butcher91e254c2018-04-22 22:58:07 +0100234#if defined(MBEDTLS_CIPHER_MODE_OFB)
235 aes_crypt_ofb_wrap,
236#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000238 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100239#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100240#if defined(MBEDTLS_CIPHER_MODE_XTS)
241 NULL,
242#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200244 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100245#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000246 aes_setkey_enc_wrap,
247 aes_setkey_dec_wrap,
248 aes_ctx_alloc,
249 aes_ctx_free
250};
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252static const mbedtls_cipher_info_t aes_128_ecb_info = {
253 MBEDTLS_CIPHER_AES_128_ECB,
254 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200255 128,
256 "AES-128-ECB",
257 16,
258 0,
259 16,
260 &aes_info
261};
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263static const mbedtls_cipher_info_t aes_192_ecb_info = {
264 MBEDTLS_CIPHER_AES_192_ECB,
265 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200266 192,
267 "AES-192-ECB",
268 16,
269 0,
270 16,
271 &aes_info
272};
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274static const mbedtls_cipher_info_t aes_256_ecb_info = {
275 MBEDTLS_CIPHER_AES_256_ECB,
276 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200277 256,
278 "AES-256-ECB",
279 16,
280 0,
281 16,
282 &aes_info
283};
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285#if defined(MBEDTLS_CIPHER_MODE_CBC)
286static const mbedtls_cipher_info_t aes_128_cbc_info = {
287 MBEDTLS_CIPHER_AES_128_CBC,
288 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000289 128,
290 "AES-128-CBC",
291 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200292 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000293 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000294 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000295};
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297static const mbedtls_cipher_info_t aes_192_cbc_info = {
298 MBEDTLS_CIPHER_AES_192_CBC,
299 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000300 192,
301 "AES-192-CBC",
302 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200303 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000304 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000305 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000306};
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308static const mbedtls_cipher_info_t aes_256_cbc_info = {
309 MBEDTLS_CIPHER_AES_256_CBC,
310 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000311 256,
312 "AES-256-CBC",
313 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200314 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000315 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000316 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000317};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_CIPHER_MODE_CFB)
321static const mbedtls_cipher_info_t aes_128_cfb128_info = {
322 MBEDTLS_CIPHER_AES_128_CFB128,
323 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000324 128,
325 "AES-128-CFB128",
326 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200327 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000328 16,
329 &aes_info
330};
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332static const mbedtls_cipher_info_t aes_192_cfb128_info = {
333 MBEDTLS_CIPHER_AES_192_CFB128,
334 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000335 192,
336 "AES-192-CFB128",
337 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200338 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000339 16,
340 &aes_info
341};
342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343static const mbedtls_cipher_info_t aes_256_cfb128_info = {
344 MBEDTLS_CIPHER_AES_256_CFB128,
345 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000346 256,
347 "AES-256-CFB128",
348 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200349 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000350 16,
351 &aes_info
352};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000354
Simon Butcher91e254c2018-04-22 22:58:07 +0100355#if defined(MBEDTLS_CIPHER_MODE_OFB)
356static const mbedtls_cipher_info_t aes_128_ofb_info = {
357 MBEDTLS_CIPHER_AES_128_OFB,
358 MBEDTLS_MODE_OFB,
359 128,
360 "AES-128-OFB",
361 16,
362 0,
363 16,
364 &aes_info
365};
366
367static const mbedtls_cipher_info_t aes_192_ofb_info = {
368 MBEDTLS_CIPHER_AES_192_OFB,
369 MBEDTLS_MODE_OFB,
370 192,
371 "AES-192-OFB",
372 16,
373 0,
374 16,
375 &aes_info
376};
377
378static const mbedtls_cipher_info_t aes_256_ofb_info = {
379 MBEDTLS_CIPHER_AES_256_OFB,
380 MBEDTLS_MODE_OFB,
381 256,
382 "AES-256-OFB",
383 16,
384 0,
385 16,
386 &aes_info
387};
388#endif /* MBEDTLS_CIPHER_MODE_OFB */
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_CIPHER_MODE_CTR)
391static const mbedtls_cipher_info_t aes_128_ctr_info = {
392 MBEDTLS_CIPHER_AES_128_CTR,
393 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000394 128,
395 "AES-128-CTR",
396 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200397 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000398 16,
399 &aes_info
400};
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402static const mbedtls_cipher_info_t aes_192_ctr_info = {
403 MBEDTLS_CIPHER_AES_192_CTR,
404 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000405 192,
406 "AES-192-CTR",
407 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200408 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000409 16,
410 &aes_info
411};
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413static const mbedtls_cipher_info_t aes_256_ctr_info = {
414 MBEDTLS_CIPHER_AES_256_CTR,
415 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000416 256,
417 "AES-256-CTR",
418 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200419 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000420 16,
421 &aes_info
422};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000424
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100425#if defined(MBEDTLS_CIPHER_MODE_XTS)
426static int xts_aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
427 unsigned int key_bitlen )
428{
429 mbedtls_aes_xts_context *xts_ctx = ctx;
430 return( mbedtls_aes_xts_setkey_enc( xts_ctx, key, key_bitlen ) );
431}
432
433static int xts_aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
434 unsigned int key_bitlen )
435{
436 mbedtls_aes_xts_context *xts_ctx = ctx;
437 return( mbedtls_aes_xts_setkey_dec( xts_ctx, key, key_bitlen ) );
438}
439
440static void *xts_aes_ctx_alloc( void )
441{
442 mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc( 1, sizeof( *xts_ctx ) );
443
444 if( xts_ctx )
445 mbedtls_aes_xts_init( xts_ctx );
446
447 return( xts_ctx );
448}
449
450static void xts_aes_ctx_free( void *ctx )
451{
452 mbedtls_aes_xts_context *xts_ctx = ctx;
453
454 if ( !xts_ctx )
455 return;
456
457 mbedtls_aes_xts_free( xts_ctx );
458 mbedtls_free( xts_ctx );
459}
460
461static const mbedtls_cipher_base_t xts_aes_info = {
462 MBEDTLS_CIPHER_ID_AES,
463 NULL,
464#if defined(MBEDTLS_CIPHER_MODE_CBC)
465 NULL,
466#endif
467#if defined(MBEDTLS_CIPHER_MODE_CFB)
468 NULL,
469#endif
Jaeden Amero7d8f00e2018-05-11 10:56:21 +0100470#if defined(MBEDTLS_CIPHER_MODE_OFB)
471 NULL,
472#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100473#if defined(MBEDTLS_CIPHER_MODE_CTR)
474 NULL,
475#endif
476#if defined(MBEDTLS_CIPHER_MODE_XTS)
477 aes_crypt_xts_wrap,
478#endif
479#if defined(MBEDTLS_CIPHER_MODE_STREAM)
480 NULL,
481#endif
482 xts_aes_setkey_enc_wrap,
483 xts_aes_setkey_dec_wrap,
484 xts_aes_ctx_alloc,
485 xts_aes_ctx_free
486};
487
488static const mbedtls_cipher_info_t aes_128_xts_info = {
489 MBEDTLS_CIPHER_AES_128_XTS,
490 MBEDTLS_MODE_XTS,
491 256,
492 "AES-128-XTS",
493 16,
494 0,
495 16,
496 &xts_aes_info
497};
498
499static const mbedtls_cipher_info_t aes_256_xts_info = {
500 MBEDTLS_CIPHER_AES_256_XTS,
501 MBEDTLS_MODE_XTS,
502 512,
503 "AES-256-XTS",
504 16,
505 0,
506 16,
507 &xts_aes_info
508};
509#endif /* MBEDTLS_CIPHER_MODE_XTS */
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200512static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200513 unsigned int key_bitlen )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200514{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200515 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200516 key, key_bitlen );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200517}
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519static const mbedtls_cipher_base_t gcm_aes_info = {
520 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200521 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200523 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100524#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200526 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100527#endif
Simon Butcher91e254c2018-04-22 22:58:07 +0100528#if defined(MBEDTLS_CIPHER_MODE_OFB)
529 NULL,
530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200532 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100533#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100534#if defined(MBEDTLS_CIPHER_MODE_XTS)
535 NULL,
536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200538 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100539#endif
Paul Bakker43aff2a2013-09-09 00:10:27 +0200540 gcm_aes_setkey_wrap,
541 gcm_aes_setkey_wrap,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200542 gcm_ctx_alloc,
543 gcm_ctx_free,
544};
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546static const mbedtls_cipher_info_t aes_128_gcm_info = {
547 MBEDTLS_CIPHER_AES_128_GCM,
548 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100549 128,
550 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200551 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100553 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200554 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100555};
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557static const mbedtls_cipher_info_t aes_192_gcm_info = {
558 MBEDTLS_CIPHER_AES_192_GCM,
559 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200560 192,
561 "AES-192-GCM",
562 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200564 16,
565 &gcm_aes_info
566};
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568static const mbedtls_cipher_info_t aes_256_gcm_info = {
569 MBEDTLS_CIPHER_AES_256_GCM,
570 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100571 256,
572 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200573 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100575 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200576 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100577};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578#endif /* MBEDTLS_GCM_C */
Paul Bakker68884e32013-01-07 18:20:04 +0100579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200581static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200582 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200583{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200584 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200585 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200586}
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588static const mbedtls_cipher_base_t ccm_aes_info = {
589 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200590 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200592 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200595 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100596#endif
Simon Butcher91e254c2018-04-22 22:58:07 +0100597#if defined(MBEDTLS_CIPHER_MODE_OFB)
598 NULL,
599#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200601 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100602#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100603#if defined(MBEDTLS_CIPHER_MODE_XTS)
604 NULL,
605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200607 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100608#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200609 ccm_aes_setkey_wrap,
610 ccm_aes_setkey_wrap,
611 ccm_ctx_alloc,
612 ccm_ctx_free,
613};
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615static const mbedtls_cipher_info_t aes_128_ccm_info = {
616 MBEDTLS_CIPHER_AES_128_CCM,
617 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200618 128,
619 "AES-128-CCM",
620 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200622 16,
623 &ccm_aes_info
624};
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626static const mbedtls_cipher_info_t aes_192_ccm_info = {
627 MBEDTLS_CIPHER_AES_192_CCM,
628 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200629 192,
630 "AES-192-CCM",
631 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200633 16,
634 &ccm_aes_info
635};
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637static const mbedtls_cipher_info_t aes_256_ccm_info = {
638 MBEDTLS_CIPHER_AES_256_CCM,
639 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200640 256,
641 "AES-256-CCM",
642 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200644 16,
645 &ccm_aes_info
646};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_AES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200654 const unsigned char *input, unsigned char *output )
655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200657 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200658}
659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_CIPHER_MODE_CBC)
661static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200662 size_t length, unsigned char *iv,
663 const unsigned char *input, unsigned char *output )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200666 input, output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000667}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670#if defined(MBEDTLS_CIPHER_MODE_CFB)
671static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200672 size_t length, size_t *iv_off, unsigned char *iv,
673 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000674{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200676 iv_off, iv, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000677}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200681static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
682 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000683 const unsigned char *input, unsigned char *output )
684{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200686 nonce_counter, stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000689
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200690static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200691 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000692{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200693 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000694}
695
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200696static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200697 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000698{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200699 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000700}
701
702static void * camellia_ctx_alloc( void )
703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_camellia_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200705 ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200706
707 if( ctx == NULL )
708 return( NULL );
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_camellia_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200711
712 return( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000713}
714
715static void camellia_ctx_free( void *ctx )
716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
718 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000719}
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721static const mbedtls_cipher_base_t camellia_info = {
722 MBEDTLS_CIPHER_ID_CAMELLIA,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200723 camellia_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000725 camellia_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100726#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000728 camellia_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100729#endif
Simon Butcher91e254c2018-04-22 22:58:07 +0100730#if defined(MBEDTLS_CIPHER_MODE_OFB)
731 NULL,
732#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000734 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100735#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100736#if defined(MBEDTLS_CIPHER_MODE_XTS)
737 NULL,
738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200740 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100741#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000742 camellia_setkey_enc_wrap,
743 camellia_setkey_dec_wrap,
744 camellia_ctx_alloc,
745 camellia_ctx_free
746};
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748static const mbedtls_cipher_info_t camellia_128_ecb_info = {
749 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
750 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200751 128,
752 "CAMELLIA-128-ECB",
753 16,
754 0,
755 16,
756 &camellia_info
757};
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759static const mbedtls_cipher_info_t camellia_192_ecb_info = {
760 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
761 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200762 192,
763 "CAMELLIA-192-ECB",
764 16,
765 0,
766 16,
767 &camellia_info
768};
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770static const mbedtls_cipher_info_t camellia_256_ecb_info = {
771 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
772 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200773 256,
774 "CAMELLIA-256-ECB",
775 16,
776 0,
777 16,
778 &camellia_info
779};
780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_CIPHER_MODE_CBC)
782static const mbedtls_cipher_info_t camellia_128_cbc_info = {
783 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
784 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000785 128,
786 "CAMELLIA-128-CBC",
787 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200788 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000789 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000790 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000791};
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793static const mbedtls_cipher_info_t camellia_192_cbc_info = {
794 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
795 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000796 192,
797 "CAMELLIA-192-CBC",
798 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200799 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000800 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000801 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000802};
803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804static const mbedtls_cipher_info_t camellia_256_cbc_info = {
805 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
806 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000807 256,
808 "CAMELLIA-256-CBC",
809 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200810 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000811 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000812 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000813};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#if defined(MBEDTLS_CIPHER_MODE_CFB)
817static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
818 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
819 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000820 128,
821 "CAMELLIA-128-CFB128",
822 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200823 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000824 16,
825 &camellia_info
826};
827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
829 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
830 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000831 192,
832 "CAMELLIA-192-CFB128",
833 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200834 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000835 16,
836 &camellia_info
837};
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
840 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
841 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000842 256,
843 "CAMELLIA-256-CFB128",
844 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200845 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000846 16,
847 &camellia_info
848};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_CIPHER_MODE_CTR)
852static const mbedtls_cipher_info_t camellia_128_ctr_info = {
853 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
854 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000855 128,
856 "CAMELLIA-128-CTR",
857 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200858 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000859 16,
860 &camellia_info
861};
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863static const mbedtls_cipher_info_t camellia_192_ctr_info = {
864 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
865 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000866 192,
867 "CAMELLIA-192-CTR",
868 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200869 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000870 16,
871 &camellia_info
872};
873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874static const mbedtls_cipher_info_t camellia_256_ctr_info = {
875 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
876 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000877 256,
878 "CAMELLIA-256-CTR",
879 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200880 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000881 16,
882 &camellia_info
883};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200887static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200888 unsigned int key_bitlen )
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200889{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200890 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200891 key, key_bitlen );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200892}
893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894static const mbedtls_cipher_base_t gcm_camellia_info = {
895 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200896 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200898 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200901 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100902#endif
Simon Butcher91e254c2018-04-22 22:58:07 +0100903#if defined(MBEDTLS_CIPHER_MODE_OFB)
904 NULL,
905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200907 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100908#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100909#if defined(MBEDTLS_CIPHER_MODE_XTS)
910 NULL,
911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200913 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100914#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200915 gcm_camellia_setkey_wrap,
916 gcm_camellia_setkey_wrap,
917 gcm_ctx_alloc,
918 gcm_ctx_free,
919};
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921static const mbedtls_cipher_info_t camellia_128_gcm_info = {
922 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
923 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200924 128,
925 "CAMELLIA-128-GCM",
926 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200928 16,
929 &gcm_camellia_info
930};
931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932static const mbedtls_cipher_info_t camellia_192_gcm_info = {
933 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
934 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200935 192,
936 "CAMELLIA-192-GCM",
937 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200939 16,
940 &gcm_camellia_info
941};
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943static const mbedtls_cipher_info_t camellia_256_gcm_info = {
944 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
945 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200946 256,
947 "CAMELLIA-256-GCM",
948 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200950 16,
951 &gcm_camellia_info
952};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200956static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200957 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200958{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200959 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200960 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200961}
962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963static const mbedtls_cipher_base_t ccm_camellia_info = {
964 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200965 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200967 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100968#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200970 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100971#endif
Simon Butcher91e254c2018-04-22 22:58:07 +0100972#if defined(MBEDTLS_CIPHER_MODE_OFB)
973 NULL,
974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200976 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100977#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +0100978#if defined(MBEDTLS_CIPHER_MODE_XTS)
979 NULL,
980#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200982 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100983#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200984 ccm_camellia_setkey_wrap,
985 ccm_camellia_setkey_wrap,
986 ccm_ctx_alloc,
987 ccm_ctx_free,
988};
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990static const mbedtls_cipher_info_t camellia_128_ccm_info = {
991 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
992 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200993 128,
994 "CAMELLIA-128-CCM",
995 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200997 16,
998 &ccm_camellia_info
999};
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001static const mbedtls_cipher_info_t camellia_192_ccm_info = {
1002 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
1003 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001004 192,
1005 "CAMELLIA-192-CCM",
1006 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001008 16,
1009 &ccm_camellia_info
1010};
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012static const mbedtls_cipher_info_t camellia_256_ccm_info = {
1013 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
1014 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001015 256,
1016 "CAMELLIA-256-CCM",
1017 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001019 16,
1020 &ccm_camellia_info
1021};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#endif /* MBEDTLS_CAMELLIA_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#if defined(MBEDTLS_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001029 const unsigned char *input, unsigned char *output )
1030{
1031 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001033}
1034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001036 const unsigned char *input, unsigned char *output )
1037{
1038 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001040}
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_CIPHER_MODE_CBC)
1043static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +00001044 unsigned char *iv, const unsigned char *input, unsigned char *output )
1045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001047 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001048}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_CIPHER_MODE_CBC)
1052static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +00001053 unsigned char *iv, const unsigned char *input, unsigned char *output )
1054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001056 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001057}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001059
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001060static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001061 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001062{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001063 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001066}
1067
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001068static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001069 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001070{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001071 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001074}
1075
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001076static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001077 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001078{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001079 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001082}
1083
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001084static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001085 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001086{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001087 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001090}
1091
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001092static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001093 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001094{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001095 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001098}
1099
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001100static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001101 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001102{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001103 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001106}
1107
1108static void * des_ctx_alloc( void )
1109{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001110 mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001111
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001112 if( des == NULL )
1113 return( NULL );
1114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 mbedtls_des_init( des );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001116
1117 return( des );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001118}
1119
1120static void des_ctx_free( void *ctx )
1121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_des_free( (mbedtls_des_context *) ctx );
1123 mbedtls_free( ctx );
Paul Bakker34617722014-06-13 17:20:13 +02001124}
1125
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001126static void * des3_ctx_alloc( void )
1127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_des3_context *des3;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001129 des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001130
1131 if( des3 == NULL )
1132 return( NULL );
1133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 mbedtls_des3_init( des3 );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001135
1136 return( des3 );
1137}
1138
Paul Bakker34617722014-06-13 17:20:13 +02001139static void des3_ctx_free( void *ctx )
1140{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_des3_free( (mbedtls_des3_context *) ctx );
1142 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001143}
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145static const mbedtls_cipher_base_t des_info = {
1146 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001147 des_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +00001149 des_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001150#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001152 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001153#endif
Simon Butcher91e254c2018-04-22 22:58:07 +01001154#if defined(MBEDTLS_CIPHER_MODE_OFB)
1155 NULL,
1156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001158 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001159#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +01001160#if defined(MBEDTLS_CIPHER_MODE_XTS)
1161 NULL,
1162#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001164 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001165#endif
Paul Bakker23986e52011-04-24 08:57:21 +00001166 des_setkey_enc_wrap,
1167 des_setkey_dec_wrap,
1168 des_ctx_alloc,
1169 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +00001170};
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172static const mbedtls_cipher_info_t des_ecb_info = {
1173 MBEDTLS_CIPHER_DES_ECB,
1174 MBEDTLS_MODE_ECB,
1175 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001176 "DES-ECB",
1177 8,
1178 0,
1179 8,
1180 &des_info
1181};
1182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#if defined(MBEDTLS_CIPHER_MODE_CBC)
1184static const mbedtls_cipher_info_t des_cbc_info = {
1185 MBEDTLS_CIPHER_DES_CBC,
1186 MBEDTLS_MODE_CBC,
1187 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker343a8702011-06-09 14:27:58 +00001188 "DES-CBC",
1189 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001190 0,
Paul Bakker343a8702011-06-09 14:27:58 +00001191 8,
1192 &des_info
1193};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196static const mbedtls_cipher_base_t des_ede_info = {
1197 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001198 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +00001200 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001203 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001204#endif
Simon Butcher91e254c2018-04-22 22:58:07 +01001205#if defined(MBEDTLS_CIPHER_MODE_OFB)
1206 NULL,
1207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001209 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001210#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +01001211#if defined(MBEDTLS_CIPHER_MODE_XTS)
1212 NULL,
1213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001215 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001216#endif
Paul Bakker23986e52011-04-24 08:57:21 +00001217 des3_set2key_enc_wrap,
1218 des3_set2key_dec_wrap,
1219 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001220 des3_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +00001221};
1222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223static const mbedtls_cipher_info_t des_ede_ecb_info = {
1224 MBEDTLS_CIPHER_DES_EDE_ECB,
1225 MBEDTLS_MODE_ECB,
1226 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001227 "DES-EDE-ECB",
1228 8,
1229 0,
1230 8,
1231 &des_ede_info
1232};
1233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_CIPHER_MODE_CBC)
1235static const mbedtls_cipher_info_t des_ede_cbc_info = {
1236 MBEDTLS_CIPHER_DES_EDE_CBC,
1237 MBEDTLS_MODE_CBC,
1238 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker343a8702011-06-09 14:27:58 +00001239 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +02001240 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001241 0,
Paul Bakker0e342352013-06-24 19:33:02 +02001242 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001243 &des_ede_info
1244};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247static const mbedtls_cipher_base_t des_ede3_info = {
Manuel Pégourié-Gonnard9d515832015-06-02 10:00:04 +01001248 MBEDTLS_CIPHER_ID_3DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001249 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +00001251 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001252#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001254 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001255#endif
Simon Butcher91e254c2018-04-22 22:58:07 +01001256#if defined(MBEDTLS_CIPHER_MODE_OFB)
1257 NULL,
1258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001260 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001261#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +01001262#if defined(MBEDTLS_CIPHER_MODE_XTS)
1263 NULL,
1264#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001266 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001267#endif
Paul Bakker343a8702011-06-09 14:27:58 +00001268 des3_set3key_enc_wrap,
1269 des3_set3key_dec_wrap,
1270 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001271 des3_ctx_free
Paul Bakker343a8702011-06-09 14:27:58 +00001272};
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1275 MBEDTLS_CIPHER_DES_EDE3_ECB,
1276 MBEDTLS_MODE_ECB,
1277 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001278 "DES-EDE3-ECB",
1279 8,
1280 0,
1281 8,
1282 &des_ede3_info
1283};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#if defined(MBEDTLS_CIPHER_MODE_CBC)
1285static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1286 MBEDTLS_CIPHER_DES_EDE3_CBC,
1287 MBEDTLS_MODE_CBC,
1288 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker23986e52011-04-24 08:57:21 +00001289 "DES-EDE3-CBC",
1290 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001291 0,
Paul Bakker23986e52011-04-24 08:57:21 +00001292 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001293 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +00001294};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#endif /* MBEDTLS_CIPHER_MODE_CBC */
1296#endif /* MBEDTLS_DES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_BLOWFISH_C)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001301 const unsigned char *input, unsigned char *output )
1302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001304 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001305}
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_CIPHER_MODE_CBC)
1308static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001309 size_t length, unsigned char *iv, const unsigned char *input,
1310 unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001313 input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001314}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#if defined(MBEDTLS_CIPHER_MODE_CFB)
1318static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001319 size_t length, size_t *iv_off, unsigned char *iv,
1320 const unsigned char *input, unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001321{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001323 iv_off, iv, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001328static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1329 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001330 const unsigned char *input, unsigned char *output )
1331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001333 nonce_counter, stream_block, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001336
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001337static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001338 unsigned int key_bitlen )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001339{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001340 return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001341}
1342
1343static void * blowfish_ctx_alloc( void )
1344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_blowfish_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001346 ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001347
1348 if( ctx == NULL )
1349 return( NULL );
1350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_blowfish_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001352
1353 return( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001354}
1355
1356static void blowfish_ctx_free( void *ctx )
1357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
1359 mbedtls_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001360}
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362static const mbedtls_cipher_base_t blowfish_info = {
1363 MBEDTLS_CIPHER_ID_BLOWFISH,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001364 blowfish_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001366 blowfish_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001367#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001369 blowfish_crypt_cfb64_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001370#endif
Simon Butcher91e254c2018-04-22 22:58:07 +01001371#if defined(MBEDTLS_CIPHER_MODE_OFB)
1372 NULL,
1373#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001375 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001376#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +01001377#if defined(MBEDTLS_CIPHER_MODE_XTS)
1378 NULL,
1379#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001381 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001382#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001383 blowfish_setkey_wrap,
1384 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001385 blowfish_ctx_alloc,
1386 blowfish_ctx_free
1387};
1388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389static const mbedtls_cipher_info_t blowfish_ecb_info = {
1390 MBEDTLS_CIPHER_BLOWFISH_ECB,
1391 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001392 128,
1393 "BLOWFISH-ECB",
1394 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001396 8,
1397 &blowfish_info
1398};
1399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_CIPHER_MODE_CBC)
1401static const mbedtls_cipher_info_t blowfish_cbc_info = {
1402 MBEDTLS_CIPHER_BLOWFISH_CBC,
1403 MBEDTLS_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001404 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001405 "BLOWFISH-CBC",
1406 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001408 8,
1409 &blowfish_info
1410};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_CIPHER_MODE_CFB)
1414static const mbedtls_cipher_info_t blowfish_cfb64_info = {
1415 MBEDTLS_CIPHER_BLOWFISH_CFB64,
1416 MBEDTLS_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001417 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001418 "BLOWFISH-CFB64",
1419 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001421 8,
1422 &blowfish_info
1423};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_CIPHER_MODE_CTR)
1427static const mbedtls_cipher_info_t blowfish_ctr_info = {
1428 MBEDTLS_CIPHER_BLOWFISH_CTR,
1429 MBEDTLS_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001430 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001431 "BLOWFISH-CTR",
1432 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001434 8,
1435 &blowfish_info
1436};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#endif /* MBEDTLS_CIPHER_MODE_CTR */
1438#endif /* MBEDTLS_BLOWFISH_C */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001441static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1442 const unsigned char *input,
1443 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +01001444{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001446}
1447
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001448static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001449 unsigned int key_bitlen )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001450{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001451 /* we get key_bitlen in bits, arc4 expects it in bytes */
1452 if( key_bitlen % 8 != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardce411252013-09-04 12:28:37 +02001454
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001455 mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001456 return( 0 );
1457}
1458
1459static void * arc4_ctx_alloc( void )
1460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 mbedtls_arc4_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001462 ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001463
1464 if( ctx == NULL )
1465 return( NULL );
1466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_arc4_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001468
1469 return( ctx );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001470}
Paul Bakker68884e32013-01-07 18:20:04 +01001471
1472static void arc4_ctx_free( void *ctx )
1473{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
1475 mbedtls_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +01001476}
1477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478static const mbedtls_cipher_base_t arc4_base_info = {
1479 MBEDTLS_CIPHER_ID_ARC4,
Paul Bakker68884e32013-01-07 18:20:04 +01001480 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker68884e32013-01-07 18:20:04 +01001482 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001483#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker68884e32013-01-07 18:20:04 +01001485 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001486#endif
Simon Butcher91e254c2018-04-22 22:58:07 +01001487#if defined(MBEDTLS_CIPHER_MODE_OFB)
1488 NULL,
1489#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02001491 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001492#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +01001493#if defined(MBEDTLS_CIPHER_MODE_XTS)
1494 NULL,
1495#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001497 arc4_crypt_stream_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001498#endif
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001499 arc4_setkey_wrap,
1500 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +01001501 arc4_ctx_alloc,
1502 arc4_ctx_free
1503};
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505static const mbedtls_cipher_info_t arc4_128_info = {
1506 MBEDTLS_CIPHER_ARC4_128,
1507 MBEDTLS_MODE_STREAM,
Paul Bakker68884e32013-01-07 18:20:04 +01001508 128,
1509 "ARC4-128",
1510 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001511 0,
Paul Bakker68884e32013-01-07 18:20:04 +01001512 1,
1513 &arc4_base_info
1514};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#endif /* MBEDTLS_ARC4_C */
Paul Bakker68884e32013-01-07 18:20:04 +01001516
Daniel King0fe7b5b2016-05-15 19:56:20 -03001517#if defined(MBEDTLS_CHACHA20_C)
1518
1519static int chacha20_setkey_wrap( void *ctx, const unsigned char *key,
1520 unsigned int key_bitlen )
1521{
1522 if( key_bitlen != 256U )
1523 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1524
1525 if ( 0 != mbedtls_chacha20_setkey( (mbedtls_chacha20_context*)ctx, key ) )
1526 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1527
1528 return( 0 );
1529}
1530
Manuel Pégourié-Gonnarde363ac72018-05-10 12:30:19 +02001531static int chacha20_stream_wrap( void *ctx, size_t length,
1532 const unsigned char *input,
1533 unsigned char *output )
1534{
1535 int ret;
1536
1537 ret = mbedtls_chacha20_update( ctx, length, input, output );
1538 if( ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA )
1539 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1540
1541 return( ret );
1542}
1543
Daniel King0fe7b5b2016-05-15 19:56:20 -03001544static void * chacha20_ctx_alloc( void )
1545{
1546 mbedtls_chacha20_context *ctx;
1547 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chacha20_context ) );
1548
1549 if( ctx == NULL )
1550 return( NULL );
1551
1552 mbedtls_chacha20_init( ctx );
1553
1554 return( ctx );
1555}
1556
1557static void chacha20_ctx_free( void *ctx )
1558{
1559 mbedtls_chacha20_free( (mbedtls_chacha20_context *) ctx );
1560 mbedtls_free( ctx );
1561}
1562
1563static const mbedtls_cipher_base_t chacha20_base_info = {
1564 MBEDTLS_CIPHER_ID_CHACHA20,
1565 NULL,
1566#if defined(MBEDTLS_CIPHER_MODE_CBC)
1567 NULL,
1568#endif
1569#if defined(MBEDTLS_CIPHER_MODE_CFB)
1570 NULL,
1571#endif
Jaeden Amerob1e4fc62018-05-11 11:06:48 +01001572#if defined(MBEDTLS_CIPHER_MODE_OFB)
1573 NULL,
1574#endif
Daniel King0fe7b5b2016-05-15 19:56:20 -03001575#if defined(MBEDTLS_CIPHER_MODE_CTR)
1576 NULL,
1577#endif
Jaeden Amerob1e4fc62018-05-11 11:06:48 +01001578#if defined(MBEDTLS_CIPHER_MODE_XTS)
1579 NULL,
1580#endif
Daniel King0fe7b5b2016-05-15 19:56:20 -03001581#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnarde363ac72018-05-10 12:30:19 +02001582 chacha20_stream_wrap,
Daniel King0fe7b5b2016-05-15 19:56:20 -03001583#endif
1584 chacha20_setkey_wrap,
1585 chacha20_setkey_wrap,
1586 chacha20_ctx_alloc,
1587 chacha20_ctx_free
1588};
1589static const mbedtls_cipher_info_t chacha20_info = {
1590 MBEDTLS_CIPHER_CHACHA20,
Manuel Pégourié-Gonnarde363ac72018-05-10 12:30:19 +02001591 MBEDTLS_MODE_STREAM,
Daniel King0fe7b5b2016-05-15 19:56:20 -03001592 256,
1593 "CHACHA20",
1594 12,
1595 0,
Manuel Pégourié-Gonnarde363ac72018-05-10 12:30:19 +02001596 1,
Daniel King0fe7b5b2016-05-15 19:56:20 -03001597 &chacha20_base_info
1598};
1599#endif /* MBEDTLS_CHACHA20_C */
1600
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001601#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel Kingb437a982016-05-17 20:33:28 -03001602
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001603static int chachapoly_setkey_wrap( void *ctx,
1604 const unsigned char *key,
1605 unsigned int key_bitlen )
Daniel Kingb437a982016-05-17 20:33:28 -03001606{
1607 if( key_bitlen != 256U )
1608 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1609
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001610 if ( 0 != mbedtls_chachapoly_setkey( (mbedtls_chachapoly_context*)ctx, key ) )
Daniel Kingb437a982016-05-17 20:33:28 -03001611 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1612
1613 return( 0 );
1614}
1615
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001616static void * chachapoly_ctx_alloc( void )
Daniel Kingb437a982016-05-17 20:33:28 -03001617{
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001618 mbedtls_chachapoly_context *ctx;
1619 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chachapoly_context ) );
Daniel Kingb437a982016-05-17 20:33:28 -03001620
1621 if( ctx == NULL )
1622 return( NULL );
1623
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001624 mbedtls_chachapoly_init( ctx );
Daniel Kingb437a982016-05-17 20:33:28 -03001625
1626 return( ctx );
1627}
1628
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001629static void chachapoly_ctx_free( void *ctx )
Daniel Kingb437a982016-05-17 20:33:28 -03001630{
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001631 mbedtls_chachapoly_free( (mbedtls_chachapoly_context *) ctx );
Daniel Kingb437a982016-05-17 20:33:28 -03001632 mbedtls_free( ctx );
1633}
1634
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001635static const mbedtls_cipher_base_t chachapoly_base_info = {
Daniel Kingb437a982016-05-17 20:33:28 -03001636 MBEDTLS_CIPHER_ID_CHACHA20,
1637 NULL,
1638#if defined(MBEDTLS_CIPHER_MODE_CBC)
1639 NULL,
1640#endif
1641#if defined(MBEDTLS_CIPHER_MODE_CFB)
1642 NULL,
1643#endif
Jaeden Amerob1e4fc62018-05-11 11:06:48 +01001644#if defined(MBEDTLS_CIPHER_MODE_OFB)
1645 NULL,
1646#endif
Daniel Kingb437a982016-05-17 20:33:28 -03001647#if defined(MBEDTLS_CIPHER_MODE_CTR)
1648 NULL,
1649#endif
Jaeden Amerob1e4fc62018-05-11 11:06:48 +01001650#if defined(MBEDTLS_CIPHER_MODE_XTS)
1651 NULL,
1652#endif
Daniel Kingb437a982016-05-17 20:33:28 -03001653#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1654 NULL,
1655#endif
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001656 chachapoly_setkey_wrap,
1657 chachapoly_setkey_wrap,
1658 chachapoly_ctx_alloc,
1659 chachapoly_ctx_free
Daniel Kingb437a982016-05-17 20:33:28 -03001660};
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001661static const mbedtls_cipher_info_t chachapoly_info = {
Daniel Kingb437a982016-05-17 20:33:28 -03001662 MBEDTLS_CIPHER_CHACHA20_POLY1305,
1663 MBEDTLS_MODE_NONE,
1664 256,
1665 "CHACHA20-POLY1305",
1666 12,
1667 0,
Manuel Pégourié-Gonnarde363ac72018-05-10 12:30:19 +02001668 1,
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001669 &chachapoly_base_info
Daniel Kingb437a982016-05-17 20:33:28 -03001670};
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001671#endif /* MBEDTLS_CHACHAPOLY_C */
Daniel Kingb437a982016-05-17 20:33:28 -03001672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001674static int null_crypt_stream( void *ctx, size_t length,
1675 const unsigned char *input,
1676 unsigned char *output )
1677{
1678 ((void) ctx);
1679 memmove( output, input, length );
1680 return( 0 );
1681}
1682
1683static int null_setkey( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001684 unsigned int key_bitlen )
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001685{
1686 ((void) ctx);
1687 ((void) key);
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001688 ((void) key_bitlen);
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001689
1690 return( 0 );
1691}
1692
Paul Bakkerfab5c822012-02-06 16:45:10 +00001693static void * null_ctx_alloc( void )
1694{
Manuel Pégourié-Gonnard86bbc7f2014-07-12 02:14:41 +02001695 return( (void *) 1 );
Paul Bakkerfab5c822012-02-06 16:45:10 +00001696}
1697
Paul Bakkerfab5c822012-02-06 16:45:10 +00001698static void null_ctx_free( void *ctx )
1699{
1700 ((void) ctx);
1701}
1702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703static const mbedtls_cipher_base_t null_base_info = {
1704 MBEDTLS_CIPHER_ID_NULL,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001705 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerfab5c822012-02-06 16:45:10 +00001707 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001708#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkerfab5c822012-02-06 16:45:10 +00001710 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001711#endif
Simon Butchera3f3f162018-04-29 00:24:51 +01001712#if defined(MBEDTLS_CIPHER_MODE_OFB)
1713 NULL,
1714#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02001716 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001717#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +01001718#if defined(MBEDTLS_CIPHER_MODE_XTS)
1719 NULL,
1720#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001722 null_crypt_stream,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001723#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001724 null_setkey,
1725 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001726 null_ctx_alloc,
1727 null_ctx_free
1728};
1729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730static const mbedtls_cipher_info_t null_cipher_info = {
1731 MBEDTLS_CIPHER_NULL,
1732 MBEDTLS_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001733 0,
1734 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +01001735 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001736 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001737 1,
1738 &null_base_info
1739};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740#endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
Paul Bakkerfab5c822012-02-06 16:45:10 +00001741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001743{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#if defined(MBEDTLS_AES_C)
1745 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1746 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1747 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1748#if defined(MBEDTLS_CIPHER_MODE_CBC)
1749 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1750 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1751 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#if defined(MBEDTLS_CIPHER_MODE_CFB)
1754 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1755 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1756 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001757#endif
Simon Butcher91e254c2018-04-22 22:58:07 +01001758#if defined(MBEDTLS_CIPHER_MODE_OFB)
1759 { MBEDTLS_CIPHER_AES_128_OFB, &aes_128_ofb_info },
1760 { MBEDTLS_CIPHER_AES_192_OFB, &aes_192_ofb_info },
1761 { MBEDTLS_CIPHER_AES_256_OFB, &aes_256_ofb_info },
1762#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763#if defined(MBEDTLS_CIPHER_MODE_CTR)
1764 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1765 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1766 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001767#endif
Jaeden Ameroe4daf772018-04-30 17:17:41 +01001768#if defined(MBEDTLS_CIPHER_MODE_XTS)
1769 { MBEDTLS_CIPHER_AES_128_XTS, &aes_128_xts_info },
1770 { MBEDTLS_CIPHER_AES_256_XTS, &aes_256_xts_info },
1771#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#if defined(MBEDTLS_GCM_C)
1773 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1774 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1775 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001776#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#if defined(MBEDTLS_CCM_C)
1778 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
1779 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
1780 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782#endif /* MBEDTLS_AES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784#if defined(MBEDTLS_ARC4_C)
1785 { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001786#endif
1787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#if defined(MBEDTLS_BLOWFISH_C)
1789 { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1790#if defined(MBEDTLS_CIPHER_MODE_CBC)
1791 { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001792#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#if defined(MBEDTLS_CIPHER_MODE_CFB)
1794 { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001795#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_CIPHER_MODE_CTR)
1797 { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001798#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799#endif /* MBEDTLS_BLOWFISH_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#if defined(MBEDTLS_CAMELLIA_C)
1802 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1803 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
1804 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
1805#if defined(MBEDTLS_CIPHER_MODE_CBC)
1806 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1807 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1808 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001809#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#if defined(MBEDTLS_CIPHER_MODE_CFB)
1811 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1812 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1813 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001814#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_CIPHER_MODE_CTR)
1816 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1817 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1818 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001819#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#if defined(MBEDTLS_GCM_C)
1821 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1822 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1823 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +02001824#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#if defined(MBEDTLS_CCM_C)
1826 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
1827 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
1828 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001829#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_CAMELLIA_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#if defined(MBEDTLS_DES_C)
1833 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
1834 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1835 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1836#if defined(MBEDTLS_CIPHER_MODE_CBC)
1837 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
1838 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1839 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841#endif /* MBEDTLS_DES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001842
Daniel King0fe7b5b2016-05-15 19:56:20 -03001843#if defined(MBEDTLS_CHACHA20_C)
1844 { MBEDTLS_CIPHER_CHACHA20, &chacha20_info },
1845#endif
1846
Manuel Pégourié-Gonnardd4bd8562018-05-07 10:43:27 +02001847#if defined(MBEDTLS_CHACHAPOLY_C)
1848 { MBEDTLS_CIPHER_CHACHA20_POLY1305, &chachapoly_info },
Daniel Kingb437a982016-05-17 20:33:28 -03001849#endif
1850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
1852 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
1853#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 { MBEDTLS_CIPHER_NONE, NULL }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001856};
1857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
1859int mbedtls_cipher_supported[NUM_CIPHERS];
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861#endif /* MBEDTLS_CIPHER_C */