blob: a813426be24945ea81fd298802dd804387a9feef [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"
Janos Follath24eed8d2019-11-22 13:21:35 +000035#include "mbedtls/error.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000036
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020037#if defined(MBEDTLS_CHACHAPOLY_C)
38#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030039#endif
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000043#endif
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/arc4.h"
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020047#endif
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_CAMELLIA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000051#endif
52
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +000053#if defined(MBEDTLS_ARIA_C)
54#include "mbedtls/aria.h"
55#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000059#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_BLOWFISH_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/blowfish.h"
Paul Bakker6132d0a2012-07-04 17:10:40 +000063#endif
64
Daniel Kingbd920622016-05-15 19:56:20 -030065#if defined(MBEDTLS_CHACHA20_C)
66#include "mbedtls/chacha20.h"
67#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000070#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020071#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020075#endif
76
Jack Lloydffdf2882019-03-07 17:00:32 -050077#if defined(MBEDTLS_NIST_KW_C)
78#include "mbedtls/nist_kw.h"
79#endif
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard0c851ee2015-02-10 12:47:52 +000082#include <string.h>
83#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020087#else
Rich Evans00ab4702015-02-06 13:43:58 +000088#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020089#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020091#endif
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020094/* shared by all GCM ciphers */
95static void *gcm_ctx_alloc( void )
96{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +020097 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
98
99 if( ctx != NULL )
100 mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
101
102 return( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200103}
104
105static void gcm_ctx_free( void *ctx )
106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_gcm_free( ctx );
108 mbedtls_free( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200113/* shared by all CCM ciphers */
114static void *ccm_ctx_alloc( void )
115{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +0200116 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
117
118 if( ctx != NULL )
119 mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
120
121 return( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200122}
123
124static void ccm_ctx_free( void *ctx )
125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_ccm_free( ctx );
127 mbedtls_free( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200134 const unsigned char *input, unsigned char *output )
135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200137}
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_CIPHER_MODE_CBC)
140static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141 unsigned char *iv, const unsigned char *input, unsigned char *output )
142{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200144 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_CIPHER_MODE_CFB)
149static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200150 size_t length, size_t *iv_off, unsigned char *iv,
151 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000152{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200154 input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000155}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000157
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100158#if defined(MBEDTLS_CIPHER_MODE_OFB)
159static int aes_crypt_ofb_wrap( void *ctx, size_t length, size_t *iv_off,
160 unsigned char *iv, const unsigned char *input, unsigned char *output )
161{
162 return mbedtls_aes_crypt_ofb( (mbedtls_aes_context *) ctx, length, iv_off,
163 iv, input, output );
164}
165#endif /* MBEDTLS_CIPHER_MODE_OFB */
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200168static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
169 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000170 const unsigned char *input, unsigned char *output )
171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
Paul Bakker343a8702011-06-09 14:27:58 +0000173 stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000176
Jaeden Ameroc6539902018-04-30 17:17:41 +0100177#if defined(MBEDTLS_CIPHER_MODE_XTS)
178static int aes_crypt_xts_wrap( void *ctx, mbedtls_operation_t operation,
179 size_t length,
180 const unsigned char data_unit[16],
181 const unsigned char *input,
182 unsigned char *output )
183{
184 mbedtls_aes_xts_context *xts_ctx = ctx;
185 int mode;
186
187 switch( operation )
188 {
189 case MBEDTLS_ENCRYPT:
190 mode = MBEDTLS_AES_ENCRYPT;
191 break;
192 case MBEDTLS_DECRYPT:
193 mode = MBEDTLS_AES_DECRYPT;
194 break;
195 default:
196 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
197 }
198
199 return mbedtls_aes_crypt_xts( xts_ctx, mode, length,
200 data_unit, input, output );
201}
202#endif /* MBEDTLS_CIPHER_MODE_XTS */
203
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200204static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200205 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000206{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200207 return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000208}
209
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200210static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200211 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200213 return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000214}
215
216static void * aes_ctx_alloc( void )
217{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200218 mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200219
220 if( aes == NULL )
221 return( NULL );
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 mbedtls_aes_init( aes );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200224
225 return( aes );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000226}
227
228static void aes_ctx_free( void *ctx )
229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_aes_free( (mbedtls_aes_context *) ctx );
231 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000232}
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234static const mbedtls_cipher_base_t aes_info = {
235 MBEDTLS_CIPHER_ID_AES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200236 aes_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000238 aes_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000241 aes_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100242#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100243#if defined(MBEDTLS_CIPHER_MODE_OFB)
244 aes_crypt_ofb_wrap,
245#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000247 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100248#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100249#if defined(MBEDTLS_CIPHER_MODE_XTS)
250 NULL,
251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200253 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100254#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000255 aes_setkey_enc_wrap,
256 aes_setkey_dec_wrap,
257 aes_ctx_alloc,
258 aes_ctx_free
259};
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261static const mbedtls_cipher_info_t aes_128_ecb_info = {
262 MBEDTLS_CIPHER_AES_128_ECB,
263 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200264 128,
265 "AES-128-ECB",
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300266 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200267 0,
268 16,
269 &aes_info
270};
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272static const mbedtls_cipher_info_t aes_192_ecb_info = {
273 MBEDTLS_CIPHER_AES_192_ECB,
274 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200275 192,
276 "AES-192-ECB",
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300277 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200278 0,
279 16,
280 &aes_info
281};
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283static const mbedtls_cipher_info_t aes_256_ecb_info = {
284 MBEDTLS_CIPHER_AES_256_ECB,
285 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200286 256,
287 "AES-256-ECB",
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300288 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200289 0,
290 16,
291 &aes_info
292};
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_CIPHER_MODE_CBC)
295static const mbedtls_cipher_info_t aes_128_cbc_info = {
296 MBEDTLS_CIPHER_AES_128_CBC,
297 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000298 128,
299 "AES-128-CBC",
300 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200301 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000302 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000303 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000304};
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306static const mbedtls_cipher_info_t aes_192_cbc_info = {
307 MBEDTLS_CIPHER_AES_192_CBC,
308 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000309 192,
310 "AES-192-CBC",
311 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200312 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000313 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000314 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000315};
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317static const mbedtls_cipher_info_t aes_256_cbc_info = {
318 MBEDTLS_CIPHER_AES_256_CBC,
319 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000320 256,
321 "AES-256-CBC",
322 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200323 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000324 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000325 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000326};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#if defined(MBEDTLS_CIPHER_MODE_CFB)
330static const mbedtls_cipher_info_t aes_128_cfb128_info = {
331 MBEDTLS_CIPHER_AES_128_CFB128,
332 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000333 128,
334 "AES-128-CFB128",
335 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200336 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000337 16,
338 &aes_info
339};
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341static const mbedtls_cipher_info_t aes_192_cfb128_info = {
342 MBEDTLS_CIPHER_AES_192_CFB128,
343 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000344 192,
345 "AES-192-CFB128",
346 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200347 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000348 16,
349 &aes_info
350};
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352static const mbedtls_cipher_info_t aes_256_cfb128_info = {
353 MBEDTLS_CIPHER_AES_256_CFB128,
354 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000355 256,
356 "AES-256-CFB128",
357 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200358 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000359 16,
360 &aes_info
361};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000363
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100364#if defined(MBEDTLS_CIPHER_MODE_OFB)
365static const mbedtls_cipher_info_t aes_128_ofb_info = {
366 MBEDTLS_CIPHER_AES_128_OFB,
367 MBEDTLS_MODE_OFB,
368 128,
369 "AES-128-OFB",
370 16,
371 0,
372 16,
373 &aes_info
374};
375
376static const mbedtls_cipher_info_t aes_192_ofb_info = {
377 MBEDTLS_CIPHER_AES_192_OFB,
378 MBEDTLS_MODE_OFB,
379 192,
380 "AES-192-OFB",
381 16,
382 0,
383 16,
384 &aes_info
385};
386
387static const mbedtls_cipher_info_t aes_256_ofb_info = {
388 MBEDTLS_CIPHER_AES_256_OFB,
389 MBEDTLS_MODE_OFB,
390 256,
391 "AES-256-OFB",
392 16,
393 0,
394 16,
395 &aes_info
396};
397#endif /* MBEDTLS_CIPHER_MODE_OFB */
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_CIPHER_MODE_CTR)
400static const mbedtls_cipher_info_t aes_128_ctr_info = {
401 MBEDTLS_CIPHER_AES_128_CTR,
402 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000403 128,
404 "AES-128-CTR",
405 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200406 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000407 16,
408 &aes_info
409};
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411static const mbedtls_cipher_info_t aes_192_ctr_info = {
412 MBEDTLS_CIPHER_AES_192_CTR,
413 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000414 192,
415 "AES-192-CTR",
416 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200417 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000418 16,
419 &aes_info
420};
421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422static const mbedtls_cipher_info_t aes_256_ctr_info = {
423 MBEDTLS_CIPHER_AES_256_CTR,
424 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000425 256,
426 "AES-256-CTR",
427 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200428 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000429 16,
430 &aes_info
431};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000433
Jaeden Ameroc6539902018-04-30 17:17:41 +0100434#if defined(MBEDTLS_CIPHER_MODE_XTS)
435static int xts_aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
436 unsigned int key_bitlen )
437{
438 mbedtls_aes_xts_context *xts_ctx = ctx;
439 return( mbedtls_aes_xts_setkey_enc( xts_ctx, key, key_bitlen ) );
440}
441
442static int xts_aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
443 unsigned int key_bitlen )
444{
445 mbedtls_aes_xts_context *xts_ctx = ctx;
446 return( mbedtls_aes_xts_setkey_dec( xts_ctx, key, key_bitlen ) );
447}
448
449static void *xts_aes_ctx_alloc( void )
450{
451 mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc( 1, sizeof( *xts_ctx ) );
452
453 if( xts_ctx != NULL )
454 mbedtls_aes_xts_init( xts_ctx );
455
456 return( xts_ctx );
457}
458
459static void xts_aes_ctx_free( void *ctx )
460{
461 mbedtls_aes_xts_context *xts_ctx = ctx;
462
463 if( xts_ctx == NULL )
464 return;
465
466 mbedtls_aes_xts_free( xts_ctx );
467 mbedtls_free( xts_ctx );
468}
469
470static const mbedtls_cipher_base_t xts_aes_info = {
471 MBEDTLS_CIPHER_ID_AES,
472 NULL,
473#if defined(MBEDTLS_CIPHER_MODE_CBC)
474 NULL,
475#endif
476#if defined(MBEDTLS_CIPHER_MODE_CFB)
477 NULL,
478#endif
479#if defined(MBEDTLS_CIPHER_MODE_OFB)
480 NULL,
481#endif
482#if defined(MBEDTLS_CIPHER_MODE_CTR)
483 NULL,
484#endif
485#if defined(MBEDTLS_CIPHER_MODE_XTS)
486 aes_crypt_xts_wrap,
487#endif
488#if defined(MBEDTLS_CIPHER_MODE_STREAM)
489 NULL,
490#endif
491 xts_aes_setkey_enc_wrap,
492 xts_aes_setkey_dec_wrap,
493 xts_aes_ctx_alloc,
494 xts_aes_ctx_free
495};
496
497static const mbedtls_cipher_info_t aes_128_xts_info = {
498 MBEDTLS_CIPHER_AES_128_XTS,
499 MBEDTLS_MODE_XTS,
500 256,
501 "AES-128-XTS",
502 16,
503 0,
504 16,
505 &xts_aes_info
506};
507
508static const mbedtls_cipher_info_t aes_256_xts_info = {
509 MBEDTLS_CIPHER_AES_256_XTS,
510 MBEDTLS_MODE_XTS,
511 512,
512 "AES-256-XTS",
513 16,
514 0,
515 16,
516 &xts_aes_info
517};
518#endif /* MBEDTLS_CIPHER_MODE_XTS */
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200521static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200522 unsigned int key_bitlen )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200523{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200524 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200525 key, key_bitlen );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200526}
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528static const mbedtls_cipher_base_t gcm_aes_info = {
529 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200530 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200532 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200535 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100536#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100537#if defined(MBEDTLS_CIPHER_MODE_OFB)
538 NULL,
539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200541 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100542#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100543#if defined(MBEDTLS_CIPHER_MODE_XTS)
544 NULL,
545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200547 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100548#endif
Paul Bakker43aff2a2013-09-09 00:10:27 +0200549 gcm_aes_setkey_wrap,
550 gcm_aes_setkey_wrap,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200551 gcm_ctx_alloc,
552 gcm_ctx_free,
553};
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555static const mbedtls_cipher_info_t aes_128_gcm_info = {
556 MBEDTLS_CIPHER_AES_128_GCM,
557 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100558 128,
559 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200560 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100562 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200563 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100564};
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566static const mbedtls_cipher_info_t aes_192_gcm_info = {
567 MBEDTLS_CIPHER_AES_192_GCM,
568 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200569 192,
570 "AES-192-GCM",
571 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200573 16,
574 &gcm_aes_info
575};
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577static const mbedtls_cipher_info_t aes_256_gcm_info = {
578 MBEDTLS_CIPHER_AES_256_GCM,
579 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100580 256,
581 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200582 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100584 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200585 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100586};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_GCM_C */
Paul Bakker68884e32013-01-07 18:20:04 +0100588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200590static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200591 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200592{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200593 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200594 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200595}
596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597static const mbedtls_cipher_base_t ccm_aes_info = {
598 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200599 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200601 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100602#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200604 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100605#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100606#if defined(MBEDTLS_CIPHER_MODE_OFB)
607 NULL,
608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200610 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100611#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100612#if defined(MBEDTLS_CIPHER_MODE_XTS)
613 NULL,
614#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200616 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100617#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200618 ccm_aes_setkey_wrap,
619 ccm_aes_setkey_wrap,
620 ccm_ctx_alloc,
621 ccm_ctx_free,
622};
623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624static const mbedtls_cipher_info_t aes_128_ccm_info = {
625 MBEDTLS_CIPHER_AES_128_CCM,
626 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200627 128,
628 "AES-128-CCM",
629 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200631 16,
632 &ccm_aes_info
633};
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635static const mbedtls_cipher_info_t aes_192_ccm_info = {
636 MBEDTLS_CIPHER_AES_192_CCM,
637 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200638 192,
639 "AES-192-CCM",
640 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200642 16,
643 &ccm_aes_info
644};
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646static const mbedtls_cipher_info_t aes_256_ccm_info = {
647 MBEDTLS_CIPHER_AES_256_CCM,
648 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200649 256,
650 "AES-256-CCM",
651 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200653 16,
654 &ccm_aes_info
655};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_AES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200663 const unsigned char *input, unsigned char *output )
664{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200666 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200667}
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_CIPHER_MODE_CBC)
670static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200671 size_t length, unsigned char *iv,
672 const unsigned char *input, unsigned char *output )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200675 input, output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_CIPHER_MODE_CFB)
680static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200681 size_t length, size_t *iv_off, unsigned char *iv,
682 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000683{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200685 iv_off, iv, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000686}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200690static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
691 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000692 const unsigned char *input, unsigned char *output )
693{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200695 nonce_counter, stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000696}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000698
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200699static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200700 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000701{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200702 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703}
704
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200705static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200706 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000707{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200708 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709}
710
711static void * camellia_ctx_alloc( void )
712{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_camellia_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200714 ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200715
716 if( ctx == NULL )
717 return( NULL );
718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_camellia_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200720
721 return( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000722}
723
724static void camellia_ctx_free( void *ctx )
725{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
727 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000728}
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730static const mbedtls_cipher_base_t camellia_info = {
731 MBEDTLS_CIPHER_ID_CAMELLIA,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200732 camellia_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000734 camellia_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000737 camellia_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100738#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100739#if defined(MBEDTLS_CIPHER_MODE_OFB)
740 NULL,
741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000743 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100744#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100745#if defined(MBEDTLS_CIPHER_MODE_XTS)
746 NULL,
747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200749 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100750#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000751 camellia_setkey_enc_wrap,
752 camellia_setkey_dec_wrap,
753 camellia_ctx_alloc,
754 camellia_ctx_free
755};
756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757static const mbedtls_cipher_info_t camellia_128_ecb_info = {
758 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
759 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200760 128,
761 "CAMELLIA-128-ECB",
762 16,
763 0,
764 16,
765 &camellia_info
766};
767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768static const mbedtls_cipher_info_t camellia_192_ecb_info = {
769 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
770 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200771 192,
772 "CAMELLIA-192-ECB",
773 16,
774 0,
775 16,
776 &camellia_info
777};
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779static const mbedtls_cipher_info_t camellia_256_ecb_info = {
780 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
781 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200782 256,
783 "CAMELLIA-256-ECB",
784 16,
785 0,
786 16,
787 &camellia_info
788};
789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#if defined(MBEDTLS_CIPHER_MODE_CBC)
791static const mbedtls_cipher_info_t camellia_128_cbc_info = {
792 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
793 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000794 128,
795 "CAMELLIA-128-CBC",
796 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200797 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000798 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000799 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000800};
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802static const mbedtls_cipher_info_t camellia_192_cbc_info = {
803 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
804 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000805 192,
806 "CAMELLIA-192-CBC",
807 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200808 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000809 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000810 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000811};
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813static const mbedtls_cipher_info_t camellia_256_cbc_info = {
814 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
815 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000816 256,
817 "CAMELLIA-256-CBC",
818 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200819 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000820 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000821 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000822};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#if defined(MBEDTLS_CIPHER_MODE_CFB)
826static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
827 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
828 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000829 128,
830 "CAMELLIA-128-CFB128",
831 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200832 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000833 16,
834 &camellia_info
835};
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
838 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
839 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000840 192,
841 "CAMELLIA-192-CFB128",
842 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200843 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000844 16,
845 &camellia_info
846};
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
849 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
850 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000851 256,
852 "CAMELLIA-256-CFB128",
853 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200854 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000855 16,
856 &camellia_info
857};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#if defined(MBEDTLS_CIPHER_MODE_CTR)
861static const mbedtls_cipher_info_t camellia_128_ctr_info = {
862 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
863 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000864 128,
865 "CAMELLIA-128-CTR",
866 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200867 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000868 16,
869 &camellia_info
870};
871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872static const mbedtls_cipher_info_t camellia_192_ctr_info = {
873 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
874 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000875 192,
876 "CAMELLIA-192-CTR",
877 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200878 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000879 16,
880 &camellia_info
881};
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883static const mbedtls_cipher_info_t camellia_256_ctr_info = {
884 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
885 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000886 256,
887 "CAMELLIA-256-CTR",
888 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200889 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000890 16,
891 &camellia_info
892};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200896static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200897 unsigned int key_bitlen )
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200898{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200899 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200900 key, key_bitlen );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200901}
902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903static const mbedtls_cipher_base_t gcm_camellia_info = {
904 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200905 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200907 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200910 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100911#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100912#if defined(MBEDTLS_CIPHER_MODE_OFB)
913 NULL,
914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200916 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100917#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100918#if defined(MBEDTLS_CIPHER_MODE_XTS)
919 NULL,
920#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200922 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100923#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200924 gcm_camellia_setkey_wrap,
925 gcm_camellia_setkey_wrap,
926 gcm_ctx_alloc,
927 gcm_ctx_free,
928};
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930static const mbedtls_cipher_info_t camellia_128_gcm_info = {
931 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
932 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200933 128,
934 "CAMELLIA-128-GCM",
935 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200937 16,
938 &gcm_camellia_info
939};
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941static const mbedtls_cipher_info_t camellia_192_gcm_info = {
942 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
943 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200944 192,
945 "CAMELLIA-192-GCM",
946 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200948 16,
949 &gcm_camellia_info
950};
951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952static const mbedtls_cipher_info_t camellia_256_gcm_info = {
953 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
954 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200955 256,
956 "CAMELLIA-256-GCM",
957 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200959 16,
960 &gcm_camellia_info
961};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200965static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200966 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200967{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200968 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200969 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200970}
971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972static const mbedtls_cipher_base_t ccm_camellia_info = {
973 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200974 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200976 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200979 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100980#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100981#if defined(MBEDTLS_CIPHER_MODE_OFB)
982 NULL,
983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200985 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100986#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100987#if defined(MBEDTLS_CIPHER_MODE_XTS)
988 NULL,
989#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200991 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100992#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200993 ccm_camellia_setkey_wrap,
994 ccm_camellia_setkey_wrap,
995 ccm_ctx_alloc,
996 ccm_ctx_free,
997};
998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999static const mbedtls_cipher_info_t camellia_128_ccm_info = {
1000 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
1001 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001002 128,
1003 "CAMELLIA-128-CCM",
1004 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001006 16,
1007 &ccm_camellia_info
1008};
1009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010static const mbedtls_cipher_info_t camellia_192_ccm_info = {
1011 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
1012 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001013 192,
1014 "CAMELLIA-192-CCM",
1015 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001017 16,
1018 &ccm_camellia_info
1019};
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021static const mbedtls_cipher_info_t camellia_256_ccm_info = {
1022 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
1023 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001024 256,
1025 "CAMELLIA-256-CCM",
1026 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001028 16,
1029 &ccm_camellia_info
1030};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#endif /* MBEDTLS_CAMELLIA_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001034
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001035#if defined(MBEDTLS_ARIA_C)
1036
1037static int aria_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
1038 const unsigned char *input, unsigned char *output )
1039{
Manuel Pégourié-Gonnard08c337d2018-05-22 13:18:01 +02001040 (void) operation;
1041 return mbedtls_aria_crypt_ecb( (mbedtls_aria_context *) ctx, input,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001042 output );
1043}
1044
1045#if defined(MBEDTLS_CIPHER_MODE_CBC)
1046static int aria_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
1047 size_t length, unsigned char *iv,
1048 const unsigned char *input, unsigned char *output )
1049{
Manuel Pégourié-Gonnard39f25612018-05-24 14:06:02 +02001050 return mbedtls_aria_crypt_cbc( (mbedtls_aria_context *) ctx, operation, length, iv,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001051 input, output );
1052}
1053#endif /* MBEDTLS_CIPHER_MODE_CBC */
1054
1055#if defined(MBEDTLS_CIPHER_MODE_CFB)
1056static int aria_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
1057 size_t length, size_t *iv_off, unsigned char *iv,
1058 const unsigned char *input, unsigned char *output )
1059{
1060 return mbedtls_aria_crypt_cfb128( (mbedtls_aria_context *) ctx, operation, length,
1061 iv_off, iv, input, output );
1062}
1063#endif /* MBEDTLS_CIPHER_MODE_CFB */
1064
1065#if defined(MBEDTLS_CIPHER_MODE_CTR)
1066static int aria_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1067 unsigned char *nonce_counter, unsigned char *stream_block,
1068 const unsigned char *input, unsigned char *output )
1069{
1070 return mbedtls_aria_crypt_ctr( (mbedtls_aria_context *) ctx, length, nc_off,
1071 nonce_counter, stream_block, input, output );
1072}
1073#endif /* MBEDTLS_CIPHER_MODE_CTR */
1074
1075static int aria_setkey_dec_wrap( void *ctx, const unsigned char *key,
1076 unsigned int key_bitlen )
1077{
1078 return mbedtls_aria_setkey_dec( (mbedtls_aria_context *) ctx, key, key_bitlen );
1079}
1080
1081static int aria_setkey_enc_wrap( void *ctx, const unsigned char *key,
1082 unsigned int key_bitlen )
1083{
1084 return mbedtls_aria_setkey_enc( (mbedtls_aria_context *) ctx, key, key_bitlen );
1085}
1086
1087static void * aria_ctx_alloc( void )
1088{
1089 mbedtls_aria_context *ctx;
1090 ctx = mbedtls_calloc( 1, sizeof( mbedtls_aria_context ) );
1091
1092 if( ctx == NULL )
1093 return( NULL );
1094
1095 mbedtls_aria_init( ctx );
1096
1097 return( ctx );
1098}
1099
1100static void aria_ctx_free( void *ctx )
1101{
1102 mbedtls_aria_free( (mbedtls_aria_context *) ctx );
1103 mbedtls_free( ctx );
1104}
1105
1106static const mbedtls_cipher_base_t aria_info = {
1107 MBEDTLS_CIPHER_ID_ARIA,
1108 aria_crypt_ecb_wrap,
1109#if defined(MBEDTLS_CIPHER_MODE_CBC)
1110 aria_crypt_cbc_wrap,
1111#endif
1112#if defined(MBEDTLS_CIPHER_MODE_CFB)
1113 aria_crypt_cfb128_wrap,
1114#endif
Simon Butcher4844bf22018-06-11 15:21:05 +01001115#if defined(MBEDTLS_CIPHER_MODE_OFB)
1116 NULL,
1117#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001118#if defined(MBEDTLS_CIPHER_MODE_CTR)
1119 aria_crypt_ctr_wrap,
1120#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001121#if defined(MBEDTLS_CIPHER_MODE_XTS)
1122 NULL,
1123#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001124#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1125 NULL,
1126#endif
1127 aria_setkey_enc_wrap,
1128 aria_setkey_dec_wrap,
1129 aria_ctx_alloc,
1130 aria_ctx_free
1131};
1132
1133static const mbedtls_cipher_info_t aria_128_ecb_info = {
1134 MBEDTLS_CIPHER_ARIA_128_ECB,
1135 MBEDTLS_MODE_ECB,
1136 128,
1137 "ARIA-128-ECB",
1138 16,
1139 0,
1140 16,
1141 &aria_info
1142};
1143
1144static const mbedtls_cipher_info_t aria_192_ecb_info = {
1145 MBEDTLS_CIPHER_ARIA_192_ECB,
1146 MBEDTLS_MODE_ECB,
1147 192,
1148 "ARIA-192-ECB",
1149 16,
1150 0,
1151 16,
1152 &aria_info
1153};
1154
1155static const mbedtls_cipher_info_t aria_256_ecb_info = {
1156 MBEDTLS_CIPHER_ARIA_256_ECB,
1157 MBEDTLS_MODE_ECB,
1158 256,
1159 "ARIA-256-ECB",
1160 16,
1161 0,
1162 16,
1163 &aria_info
1164};
1165
1166#if defined(MBEDTLS_CIPHER_MODE_CBC)
1167static const mbedtls_cipher_info_t aria_128_cbc_info = {
1168 MBEDTLS_CIPHER_ARIA_128_CBC,
1169 MBEDTLS_MODE_CBC,
1170 128,
1171 "ARIA-128-CBC",
1172 16,
1173 0,
1174 16,
1175 &aria_info
1176};
1177
1178static const mbedtls_cipher_info_t aria_192_cbc_info = {
1179 MBEDTLS_CIPHER_ARIA_192_CBC,
1180 MBEDTLS_MODE_CBC,
1181 192,
1182 "ARIA-192-CBC",
1183 16,
1184 0,
1185 16,
1186 &aria_info
1187};
1188
1189static const mbedtls_cipher_info_t aria_256_cbc_info = {
1190 MBEDTLS_CIPHER_ARIA_256_CBC,
1191 MBEDTLS_MODE_CBC,
1192 256,
1193 "ARIA-256-CBC",
1194 16,
1195 0,
1196 16,
1197 &aria_info
1198};
1199#endif /* MBEDTLS_CIPHER_MODE_CBC */
1200
1201#if defined(MBEDTLS_CIPHER_MODE_CFB)
1202static const mbedtls_cipher_info_t aria_128_cfb128_info = {
1203 MBEDTLS_CIPHER_ARIA_128_CFB128,
1204 MBEDTLS_MODE_CFB,
1205 128,
1206 "ARIA-128-CFB128",
1207 16,
1208 0,
1209 16,
1210 &aria_info
1211};
1212
1213static const mbedtls_cipher_info_t aria_192_cfb128_info = {
1214 MBEDTLS_CIPHER_ARIA_192_CFB128,
1215 MBEDTLS_MODE_CFB,
1216 192,
1217 "ARIA-192-CFB128",
1218 16,
1219 0,
1220 16,
1221 &aria_info
1222};
1223
1224static const mbedtls_cipher_info_t aria_256_cfb128_info = {
1225 MBEDTLS_CIPHER_ARIA_256_CFB128,
1226 MBEDTLS_MODE_CFB,
1227 256,
1228 "ARIA-256-CFB128",
1229 16,
1230 0,
1231 16,
1232 &aria_info
1233};
1234#endif /* MBEDTLS_CIPHER_MODE_CFB */
1235
1236#if defined(MBEDTLS_CIPHER_MODE_CTR)
1237static const mbedtls_cipher_info_t aria_128_ctr_info = {
1238 MBEDTLS_CIPHER_ARIA_128_CTR,
1239 MBEDTLS_MODE_CTR,
1240 128,
1241 "ARIA-128-CTR",
1242 16,
1243 0,
1244 16,
1245 &aria_info
1246};
1247
1248static const mbedtls_cipher_info_t aria_192_ctr_info = {
1249 MBEDTLS_CIPHER_ARIA_192_CTR,
1250 MBEDTLS_MODE_CTR,
1251 192,
1252 "ARIA-192-CTR",
1253 16,
1254 0,
1255 16,
1256 &aria_info
1257};
1258
1259static const mbedtls_cipher_info_t aria_256_ctr_info = {
1260 MBEDTLS_CIPHER_ARIA_256_CTR,
1261 MBEDTLS_MODE_CTR,
1262 256,
1263 "ARIA-256-CTR",
1264 16,
1265 0,
1266 16,
1267 &aria_info
1268};
1269#endif /* MBEDTLS_CIPHER_MODE_CTR */
1270
1271#if defined(MBEDTLS_GCM_C)
1272static int gcm_aria_setkey_wrap( void *ctx, const unsigned char *key,
1273 unsigned int key_bitlen )
1274{
1275 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1276 key, key_bitlen );
1277}
1278
1279static const mbedtls_cipher_base_t gcm_aria_info = {
1280 MBEDTLS_CIPHER_ID_ARIA,
1281 NULL,
1282#if defined(MBEDTLS_CIPHER_MODE_CBC)
1283 NULL,
1284#endif
1285#if defined(MBEDTLS_CIPHER_MODE_CFB)
1286 NULL,
1287#endif
Simon Butcher4844bf22018-06-11 15:21:05 +01001288#if defined(MBEDTLS_CIPHER_MODE_OFB)
1289 NULL,
1290#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001291#if defined(MBEDTLS_CIPHER_MODE_CTR)
1292 NULL,
1293#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001294#if defined(MBEDTLS_CIPHER_MODE_XTS)
1295 NULL,
1296#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001297#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1298 NULL,
1299#endif
1300 gcm_aria_setkey_wrap,
1301 gcm_aria_setkey_wrap,
1302 gcm_ctx_alloc,
1303 gcm_ctx_free,
1304};
1305
1306static const mbedtls_cipher_info_t aria_128_gcm_info = {
1307 MBEDTLS_CIPHER_ARIA_128_GCM,
1308 MBEDTLS_MODE_GCM,
1309 128,
1310 "ARIA-128-GCM",
1311 12,
1312 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1313 16,
1314 &gcm_aria_info
1315};
1316
1317static const mbedtls_cipher_info_t aria_192_gcm_info = {
1318 MBEDTLS_CIPHER_ARIA_192_GCM,
1319 MBEDTLS_MODE_GCM,
1320 192,
1321 "ARIA-192-GCM",
1322 12,
1323 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1324 16,
1325 &gcm_aria_info
1326};
1327
1328static const mbedtls_cipher_info_t aria_256_gcm_info = {
1329 MBEDTLS_CIPHER_ARIA_256_GCM,
1330 MBEDTLS_MODE_GCM,
1331 256,
1332 "ARIA-256-GCM",
1333 12,
1334 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1335 16,
1336 &gcm_aria_info
1337};
1338#endif /* MBEDTLS_GCM_C */
1339
1340#if defined(MBEDTLS_CCM_C)
1341static int ccm_aria_setkey_wrap( void *ctx, const unsigned char *key,
1342 unsigned int key_bitlen )
1343{
1344 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1345 key, key_bitlen );
1346}
1347
1348static const mbedtls_cipher_base_t ccm_aria_info = {
1349 MBEDTLS_CIPHER_ID_ARIA,
1350 NULL,
1351#if defined(MBEDTLS_CIPHER_MODE_CBC)
1352 NULL,
1353#endif
1354#if defined(MBEDTLS_CIPHER_MODE_CFB)
1355 NULL,
1356#endif
Simon Butcher7487c5b2018-04-29 00:24:51 +01001357#if defined(MBEDTLS_CIPHER_MODE_OFB)
1358 NULL,
1359#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001360#if defined(MBEDTLS_CIPHER_MODE_CTR)
1361 NULL,
1362#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001363#if defined(MBEDTLS_CIPHER_MODE_XTS)
1364 NULL,
1365#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001366#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1367 NULL,
1368#endif
1369 ccm_aria_setkey_wrap,
1370 ccm_aria_setkey_wrap,
1371 ccm_ctx_alloc,
1372 ccm_ctx_free,
1373};
1374
1375static const mbedtls_cipher_info_t aria_128_ccm_info = {
1376 MBEDTLS_CIPHER_ARIA_128_CCM,
1377 MBEDTLS_MODE_CCM,
1378 128,
1379 "ARIA-128-CCM",
1380 12,
1381 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1382 16,
1383 &ccm_aria_info
1384};
1385
1386static const mbedtls_cipher_info_t aria_192_ccm_info = {
1387 MBEDTLS_CIPHER_ARIA_192_CCM,
1388 MBEDTLS_MODE_CCM,
1389 192,
1390 "ARIA-192-CCM",
1391 12,
1392 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1393 16,
1394 &ccm_aria_info
1395};
1396
1397static const mbedtls_cipher_info_t aria_256_ccm_info = {
1398 MBEDTLS_CIPHER_ARIA_256_CCM,
1399 MBEDTLS_MODE_CCM,
1400 256,
1401 "ARIA-256-CCM",
1402 12,
1403 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1404 16,
1405 &ccm_aria_info
1406};
1407#endif /* MBEDTLS_CCM_C */
1408
1409#endif /* MBEDTLS_ARIA_C */
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001414 const unsigned char *input, unsigned char *output )
1415{
1416 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001418}
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001421 const unsigned char *input, unsigned char *output )
1422{
1423 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001425}
1426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_CIPHER_MODE_CBC)
1428static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +00001429 unsigned char *iv, const unsigned char *input, unsigned char *output )
1430{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001432 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_CIPHER_MODE_CBC)
1437static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +00001438 unsigned char *iv, const unsigned char *input, unsigned char *output )
1439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001441 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001444
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001445static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001446 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001447{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001448 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001451}
1452
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001453static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001454 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001455{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001456 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001459}
1460
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001461static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001462 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001463{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001464 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001467}
1468
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001469static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001470 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001471{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001472 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001475}
1476
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001477static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001478 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001479{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001480 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001483}
1484
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001485static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001486 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001487{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001488 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001491}
1492
1493static void * des_ctx_alloc( void )
1494{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001495 mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001496
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001497 if( des == NULL )
1498 return( NULL );
1499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 mbedtls_des_init( des );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001501
1502 return( des );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001503}
1504
1505static void des_ctx_free( void *ctx )
1506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_des_free( (mbedtls_des_context *) ctx );
1508 mbedtls_free( ctx );
Paul Bakker34617722014-06-13 17:20:13 +02001509}
1510
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001511static void * des3_ctx_alloc( void )
1512{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 mbedtls_des3_context *des3;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001514 des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001515
1516 if( des3 == NULL )
1517 return( NULL );
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 mbedtls_des3_init( des3 );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001520
1521 return( des3 );
1522}
1523
Paul Bakker34617722014-06-13 17:20:13 +02001524static void des3_ctx_free( void *ctx )
1525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 mbedtls_des3_free( (mbedtls_des3_context *) ctx );
1527 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001528}
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530static const mbedtls_cipher_base_t des_info = {
1531 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001532 des_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +00001534 des_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001537 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001538#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001539#if defined(MBEDTLS_CIPHER_MODE_OFB)
1540 NULL,
1541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001543 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001544#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001545#if defined(MBEDTLS_CIPHER_MODE_XTS)
1546 NULL,
1547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001549 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001550#endif
Paul Bakker23986e52011-04-24 08:57:21 +00001551 des_setkey_enc_wrap,
1552 des_setkey_dec_wrap,
1553 des_ctx_alloc,
1554 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +00001555};
1556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557static const mbedtls_cipher_info_t des_ecb_info = {
1558 MBEDTLS_CIPHER_DES_ECB,
1559 MBEDTLS_MODE_ECB,
1560 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001561 "DES-ECB",
1562 8,
1563 0,
1564 8,
1565 &des_info
1566};
1567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_CIPHER_MODE_CBC)
1569static const mbedtls_cipher_info_t des_cbc_info = {
1570 MBEDTLS_CIPHER_DES_CBC,
1571 MBEDTLS_MODE_CBC,
1572 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker343a8702011-06-09 14:27:58 +00001573 "DES-CBC",
1574 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001575 0,
Paul Bakker343a8702011-06-09 14:27:58 +00001576 8,
1577 &des_info
1578};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581static const mbedtls_cipher_base_t des_ede_info = {
1582 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001583 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +00001585 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001586#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001588 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001589#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001590#if defined(MBEDTLS_CIPHER_MODE_OFB)
1591 NULL,
1592#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001594 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001595#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001596#if defined(MBEDTLS_CIPHER_MODE_XTS)
1597 NULL,
1598#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001600 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001601#endif
Paul Bakker23986e52011-04-24 08:57:21 +00001602 des3_set2key_enc_wrap,
1603 des3_set2key_dec_wrap,
1604 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001605 des3_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +00001606};
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608static const mbedtls_cipher_info_t des_ede_ecb_info = {
1609 MBEDTLS_CIPHER_DES_EDE_ECB,
1610 MBEDTLS_MODE_ECB,
1611 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001612 "DES-EDE-ECB",
1613 8,
1614 0,
1615 8,
1616 &des_ede_info
1617};
1618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#if defined(MBEDTLS_CIPHER_MODE_CBC)
1620static const mbedtls_cipher_info_t des_ede_cbc_info = {
1621 MBEDTLS_CIPHER_DES_EDE_CBC,
1622 MBEDTLS_MODE_CBC,
1623 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker343a8702011-06-09 14:27:58 +00001624 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +02001625 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001626 0,
Paul Bakker0e342352013-06-24 19:33:02 +02001627 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001628 &des_ede_info
1629};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632static const mbedtls_cipher_base_t des_ede3_info = {
Manuel Pégourié-Gonnard9d515832015-06-02 10:00:04 +01001633 MBEDTLS_CIPHER_ID_3DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001634 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +00001636 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001637#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001639 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001640#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001641#if defined(MBEDTLS_CIPHER_MODE_OFB)
1642 NULL,
1643#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001645 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001646#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001647#if defined(MBEDTLS_CIPHER_MODE_XTS)
1648 NULL,
1649#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001651 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001652#endif
Paul Bakker343a8702011-06-09 14:27:58 +00001653 des3_set3key_enc_wrap,
1654 des3_set3key_dec_wrap,
1655 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001656 des3_ctx_free
Paul Bakker343a8702011-06-09 14:27:58 +00001657};
1658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1660 MBEDTLS_CIPHER_DES_EDE3_ECB,
1661 MBEDTLS_MODE_ECB,
1662 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001663 "DES-EDE3-ECB",
1664 8,
1665 0,
1666 8,
1667 &des_ede3_info
1668};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#if defined(MBEDTLS_CIPHER_MODE_CBC)
1670static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1671 MBEDTLS_CIPHER_DES_EDE3_CBC,
1672 MBEDTLS_MODE_CBC,
1673 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker23986e52011-04-24 08:57:21 +00001674 "DES-EDE3-CBC",
1675 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001676 0,
Paul Bakker23986e52011-04-24 08:57:21 +00001677 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001678 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +00001679};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680#endif /* MBEDTLS_CIPHER_MODE_CBC */
1681#endif /* MBEDTLS_DES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#if defined(MBEDTLS_BLOWFISH_C)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001686 const unsigned char *input, unsigned char *output )
1687{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001689 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001690}
1691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_CIPHER_MODE_CBC)
1693static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001694 size_t length, unsigned char *iv, const unsigned char *input,
1695 unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001696{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697 return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001698 input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#if defined(MBEDTLS_CIPHER_MODE_CFB)
1703static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001704 size_t length, size_t *iv_off, unsigned char *iv,
1705 const unsigned char *input, unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001706{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001708 iv_off, iv, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001713static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1714 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001715 const unsigned char *input, unsigned char *output )
1716{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001718 nonce_counter, stream_block, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001719}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001721
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001722static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001723 unsigned int key_bitlen )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001724{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001725 return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001726}
1727
1728static void * blowfish_ctx_alloc( void )
1729{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 mbedtls_blowfish_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001731 ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001732
1733 if( ctx == NULL )
1734 return( NULL );
1735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 mbedtls_blowfish_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001737
1738 return( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001739}
1740
1741static void blowfish_ctx_free( void *ctx )
1742{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
1744 mbedtls_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001745}
1746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747static const mbedtls_cipher_base_t blowfish_info = {
1748 MBEDTLS_CIPHER_ID_BLOWFISH,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001749 blowfish_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001751 blowfish_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001754 blowfish_crypt_cfb64_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001755#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001756#if defined(MBEDTLS_CIPHER_MODE_OFB)
1757 NULL,
1758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001760 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001761#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001762#if defined(MBEDTLS_CIPHER_MODE_XTS)
1763 NULL,
1764#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001766 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001767#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001768 blowfish_setkey_wrap,
1769 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001770 blowfish_ctx_alloc,
1771 blowfish_ctx_free
1772};
1773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774static const mbedtls_cipher_info_t blowfish_ecb_info = {
1775 MBEDTLS_CIPHER_BLOWFISH_ECB,
1776 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001777 128,
1778 "BLOWFISH-ECB",
1779 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001781 8,
1782 &blowfish_info
1783};
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785#if defined(MBEDTLS_CIPHER_MODE_CBC)
1786static const mbedtls_cipher_info_t blowfish_cbc_info = {
1787 MBEDTLS_CIPHER_BLOWFISH_CBC,
1788 MBEDTLS_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001789 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001790 "BLOWFISH-CBC",
1791 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001793 8,
1794 &blowfish_info
1795};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#if defined(MBEDTLS_CIPHER_MODE_CFB)
1799static const mbedtls_cipher_info_t blowfish_cfb64_info = {
1800 MBEDTLS_CIPHER_BLOWFISH_CFB64,
1801 MBEDTLS_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001802 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001803 "BLOWFISH-CFB64",
1804 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001806 8,
1807 &blowfish_info
1808};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811#if defined(MBEDTLS_CIPHER_MODE_CTR)
1812static const mbedtls_cipher_info_t blowfish_ctr_info = {
1813 MBEDTLS_CIPHER_BLOWFISH_CTR,
1814 MBEDTLS_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001815 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001816 "BLOWFISH-CTR",
1817 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001819 8,
1820 &blowfish_info
1821};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#endif /* MBEDTLS_CIPHER_MODE_CTR */
1823#endif /* MBEDTLS_BLOWFISH_C */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001826static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1827 const unsigned char *input,
1828 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +01001829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001831}
1832
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001833static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001834 unsigned int key_bitlen )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001835{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001836 /* we get key_bitlen in bits, arc4 expects it in bytes */
1837 if( key_bitlen % 8 != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardce411252013-09-04 12:28:37 +02001839
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001840 mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001841 return( 0 );
1842}
1843
1844static void * arc4_ctx_alloc( void )
1845{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 mbedtls_arc4_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001847 ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001848
1849 if( ctx == NULL )
1850 return( NULL );
1851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_arc4_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001853
1854 return( ctx );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001855}
Paul Bakker68884e32013-01-07 18:20:04 +01001856
1857static void arc4_ctx_free( void *ctx )
1858{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
1860 mbedtls_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +01001861}
1862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863static const mbedtls_cipher_base_t arc4_base_info = {
1864 MBEDTLS_CIPHER_ID_ARC4,
Paul Bakker68884e32013-01-07 18:20:04 +01001865 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker68884e32013-01-07 18:20:04 +01001867 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001868#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker68884e32013-01-07 18:20:04 +01001870 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001871#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001872#if defined(MBEDTLS_CIPHER_MODE_OFB)
1873 NULL,
1874#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02001876 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001877#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001878#if defined(MBEDTLS_CIPHER_MODE_XTS)
1879 NULL,
1880#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001882 arc4_crypt_stream_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001883#endif
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001884 arc4_setkey_wrap,
1885 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +01001886 arc4_ctx_alloc,
1887 arc4_ctx_free
1888};
1889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001890static const mbedtls_cipher_info_t arc4_128_info = {
1891 MBEDTLS_CIPHER_ARC4_128,
1892 MBEDTLS_MODE_STREAM,
Paul Bakker68884e32013-01-07 18:20:04 +01001893 128,
1894 "ARC4-128",
1895 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001896 0,
Paul Bakker68884e32013-01-07 18:20:04 +01001897 1,
1898 &arc4_base_info
1899};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#endif /* MBEDTLS_ARC4_C */
Paul Bakker68884e32013-01-07 18:20:04 +01001901
Daniel Kingbd920622016-05-15 19:56:20 -03001902#if defined(MBEDTLS_CHACHA20_C)
1903
1904static int chacha20_setkey_wrap( void *ctx, const unsigned char *key,
1905 unsigned int key_bitlen )
1906{
1907 if( key_bitlen != 256U )
1908 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1909
1910 if ( 0 != mbedtls_chacha20_setkey( (mbedtls_chacha20_context*)ctx, key ) )
1911 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1912
1913 return( 0 );
1914}
1915
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001916static int chacha20_stream_wrap( void *ctx, size_t length,
1917 const unsigned char *input,
1918 unsigned char *output )
1919{
Janos Follath24eed8d2019-11-22 13:21:35 +00001920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001921
1922 ret = mbedtls_chacha20_update( ctx, length, input, output );
1923 if( ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA )
1924 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1925
1926 return( ret );
1927}
1928
Daniel Kingbd920622016-05-15 19:56:20 -03001929static void * chacha20_ctx_alloc( void )
1930{
1931 mbedtls_chacha20_context *ctx;
1932 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chacha20_context ) );
1933
1934 if( ctx == NULL )
1935 return( NULL );
1936
1937 mbedtls_chacha20_init( ctx );
1938
1939 return( ctx );
1940}
1941
1942static void chacha20_ctx_free( void *ctx )
1943{
1944 mbedtls_chacha20_free( (mbedtls_chacha20_context *) ctx );
1945 mbedtls_free( ctx );
1946}
1947
1948static const mbedtls_cipher_base_t chacha20_base_info = {
1949 MBEDTLS_CIPHER_ID_CHACHA20,
1950 NULL,
1951#if defined(MBEDTLS_CIPHER_MODE_CBC)
1952 NULL,
1953#endif
1954#if defined(MBEDTLS_CIPHER_MODE_CFB)
1955 NULL,
1956#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02001957#if defined(MBEDTLS_CIPHER_MODE_OFB)
1958 NULL,
1959#endif
Daniel Kingbd920622016-05-15 19:56:20 -03001960#if defined(MBEDTLS_CIPHER_MODE_CTR)
1961 NULL,
1962#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02001963#if defined(MBEDTLS_CIPHER_MODE_XTS)
1964 NULL,
1965#endif
Daniel Kingbd920622016-05-15 19:56:20 -03001966#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001967 chacha20_stream_wrap,
Daniel Kingbd920622016-05-15 19:56:20 -03001968#endif
1969 chacha20_setkey_wrap,
1970 chacha20_setkey_wrap,
1971 chacha20_ctx_alloc,
1972 chacha20_ctx_free
1973};
1974static const mbedtls_cipher_info_t chacha20_info = {
1975 MBEDTLS_CIPHER_CHACHA20,
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001976 MBEDTLS_MODE_STREAM,
Daniel Kingbd920622016-05-15 19:56:20 -03001977 256,
1978 "CHACHA20",
1979 12,
1980 0,
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001981 1,
Daniel Kingbd920622016-05-15 19:56:20 -03001982 &chacha20_base_info
1983};
1984#endif /* MBEDTLS_CHACHA20_C */
1985
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001986#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001987
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001988static int chachapoly_setkey_wrap( void *ctx,
1989 const unsigned char *key,
1990 unsigned int key_bitlen )
Daniel King8fe47012016-05-17 20:33:28 -03001991{
1992 if( key_bitlen != 256U )
1993 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1994
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001995 if ( 0 != mbedtls_chachapoly_setkey( (mbedtls_chachapoly_context*)ctx, key ) )
Daniel King8fe47012016-05-17 20:33:28 -03001996 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1997
1998 return( 0 );
1999}
2000
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002001static void * chachapoly_ctx_alloc( void )
Daniel King8fe47012016-05-17 20:33:28 -03002002{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002003 mbedtls_chachapoly_context *ctx;
2004 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chachapoly_context ) );
Daniel King8fe47012016-05-17 20:33:28 -03002005
2006 if( ctx == NULL )
2007 return( NULL );
2008
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002009 mbedtls_chachapoly_init( ctx );
Daniel King8fe47012016-05-17 20:33:28 -03002010
2011 return( ctx );
2012}
2013
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002014static void chachapoly_ctx_free( void *ctx )
Daniel King8fe47012016-05-17 20:33:28 -03002015{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002016 mbedtls_chachapoly_free( (mbedtls_chachapoly_context *) ctx );
Daniel King8fe47012016-05-17 20:33:28 -03002017 mbedtls_free( ctx );
2018}
2019
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002020static const mbedtls_cipher_base_t chachapoly_base_info = {
Daniel King8fe47012016-05-17 20:33:28 -03002021 MBEDTLS_CIPHER_ID_CHACHA20,
2022 NULL,
2023#if defined(MBEDTLS_CIPHER_MODE_CBC)
2024 NULL,
2025#endif
2026#if defined(MBEDTLS_CIPHER_MODE_CFB)
2027 NULL,
2028#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02002029#if defined(MBEDTLS_CIPHER_MODE_OFB)
2030 NULL,
2031#endif
Daniel King8fe47012016-05-17 20:33:28 -03002032#if defined(MBEDTLS_CIPHER_MODE_CTR)
2033 NULL,
2034#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02002035#if defined(MBEDTLS_CIPHER_MODE_XTS)
2036 NULL,
2037#endif
Daniel King8fe47012016-05-17 20:33:28 -03002038#if defined(MBEDTLS_CIPHER_MODE_STREAM)
2039 NULL,
2040#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002041 chachapoly_setkey_wrap,
2042 chachapoly_setkey_wrap,
2043 chachapoly_ctx_alloc,
2044 chachapoly_ctx_free
Daniel King8fe47012016-05-17 20:33:28 -03002045};
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002046static const mbedtls_cipher_info_t chachapoly_info = {
Daniel King8fe47012016-05-17 20:33:28 -03002047 MBEDTLS_CIPHER_CHACHA20_POLY1305,
Manuel Pégourié-Gonnardf57bf8b2018-06-18 11:14:09 +02002048 MBEDTLS_MODE_CHACHAPOLY,
Daniel King8fe47012016-05-17 20:33:28 -03002049 256,
2050 "CHACHA20-POLY1305",
2051 12,
2052 0,
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02002053 1,
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002054 &chachapoly_base_info
Daniel King8fe47012016-05-17 20:33:28 -03002055};
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002056#endif /* MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -03002057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002059static int null_crypt_stream( void *ctx, size_t length,
2060 const unsigned char *input,
2061 unsigned char *output )
2062{
2063 ((void) ctx);
2064 memmove( output, input, length );
2065 return( 0 );
2066}
2067
2068static int null_setkey( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02002069 unsigned int key_bitlen )
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002070{
2071 ((void) ctx);
2072 ((void) key);
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02002073 ((void) key_bitlen);
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002074
2075 return( 0 );
2076}
2077
Paul Bakkerfab5c822012-02-06 16:45:10 +00002078static void * null_ctx_alloc( void )
2079{
Manuel Pégourié-Gonnard86bbc7f2014-07-12 02:14:41 +02002080 return( (void *) 1 );
Paul Bakkerfab5c822012-02-06 16:45:10 +00002081}
2082
Paul Bakkerfab5c822012-02-06 16:45:10 +00002083static void null_ctx_free( void *ctx )
2084{
2085 ((void) ctx);
2086}
2087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088static const mbedtls_cipher_base_t null_base_info = {
2089 MBEDTLS_CIPHER_ID_NULL,
Paul Bakkerfab5c822012-02-06 16:45:10 +00002090 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerfab5c822012-02-06 16:45:10 +00002092 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01002093#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002094#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkerfab5c822012-02-06 16:45:10 +00002095 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01002096#endif
Simon Butcher4844bf22018-06-11 15:21:05 +01002097#if defined(MBEDTLS_CIPHER_MODE_OFB)
2098 NULL,
2099#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02002101 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01002102#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01002103#if defined(MBEDTLS_CIPHER_MODE_XTS)
2104 NULL,
2105#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002107 null_crypt_stream,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01002108#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02002109 null_setkey,
2110 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +00002111 null_ctx_alloc,
2112 null_ctx_free
2113};
2114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115static const mbedtls_cipher_info_t null_cipher_info = {
2116 MBEDTLS_CIPHER_NULL,
2117 MBEDTLS_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +00002118 0,
2119 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +01002120 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02002121 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +00002122 1,
2123 &null_base_info
2124};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125#endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
Paul Bakkerfab5c822012-02-06 16:45:10 +00002126
Jack Lloydffdf2882019-03-07 17:00:32 -05002127#if defined(MBEDTLS_NIST_KW_C)
2128static void *kw_ctx_alloc( void )
2129{
2130 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_nist_kw_context ) );
2131
2132 if( ctx != NULL )
2133 mbedtls_nist_kw_init( (mbedtls_nist_kw_context *) ctx );
2134
2135 return( ctx );
2136}
2137
2138static void kw_ctx_free( void *ctx )
2139{
2140 mbedtls_nist_kw_free( ctx );
2141 mbedtls_free( ctx );
2142}
2143
2144static int kw_aes_setkey_wrap( void *ctx, const unsigned char *key,
2145 unsigned int key_bitlen )
2146{
Jack Lloyd5f289992019-04-02 10:07:28 -07002147 return mbedtls_nist_kw_setkey( (mbedtls_nist_kw_context *) ctx,
2148 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 1 );
Jack Lloydffdf2882019-03-07 17:00:32 -05002149}
2150
2151static int kw_aes_setkey_unwrap( void *ctx, const unsigned char *key,
2152 unsigned int key_bitlen )
2153{
Jack Lloyd5f289992019-04-02 10:07:28 -07002154 return mbedtls_nist_kw_setkey( (mbedtls_nist_kw_context *) ctx,
2155 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 0 );
Jack Lloydffdf2882019-03-07 17:00:32 -05002156}
2157
2158static const mbedtls_cipher_base_t kw_aes_info = {
2159 MBEDTLS_CIPHER_ID_AES,
2160 NULL,
2161#if defined(MBEDTLS_CIPHER_MODE_CBC)
2162 NULL,
2163#endif
2164#if defined(MBEDTLS_CIPHER_MODE_CFB)
2165 NULL,
2166#endif
2167#if defined(MBEDTLS_CIPHER_MODE_OFB)
2168 NULL,
2169#endif
2170#if defined(MBEDTLS_CIPHER_MODE_CTR)
2171 NULL,
2172#endif
2173#if defined(MBEDTLS_CIPHER_MODE_XTS)
2174 NULL,
2175#endif
2176#if defined(MBEDTLS_CIPHER_MODE_STREAM)
2177 NULL,
2178#endif
2179 kw_aes_setkey_wrap,
2180 kw_aes_setkey_unwrap,
2181 kw_ctx_alloc,
2182 kw_ctx_free,
2183};
2184
2185static const mbedtls_cipher_info_t aes_128_nist_kw_info = {
2186 MBEDTLS_CIPHER_AES_128_KW,
2187 MBEDTLS_MODE_KW,
2188 128,
2189 "AES-128-KW",
2190 0,
2191 0,
2192 16,
2193 &kw_aes_info
2194};
2195
2196static const mbedtls_cipher_info_t aes_192_nist_kw_info = {
2197 MBEDTLS_CIPHER_AES_192_KW,
2198 MBEDTLS_MODE_KW,
2199 192,
2200 "AES-192-KW",
2201 0,
2202 0,
2203 16,
2204 &kw_aes_info
2205};
2206
2207static const mbedtls_cipher_info_t aes_256_nist_kw_info = {
2208 MBEDTLS_CIPHER_AES_256_KW,
2209 MBEDTLS_MODE_KW,
2210 256,
2211 "AES-256-KW",
2212 0,
2213 0,
2214 16,
2215 &kw_aes_info
2216};
2217
2218static const mbedtls_cipher_info_t aes_128_nist_kwp_info = {
2219 MBEDTLS_CIPHER_AES_128_KWP,
2220 MBEDTLS_MODE_KWP,
2221 128,
2222 "AES-128-KWP",
2223 0,
2224 0,
2225 16,
2226 &kw_aes_info
2227};
2228
2229static const mbedtls_cipher_info_t aes_192_nist_kwp_info = {
2230 MBEDTLS_CIPHER_AES_192_KWP,
2231 MBEDTLS_MODE_KWP,
2232 192,
2233 "AES-192-KWP",
2234 0,
2235 0,
2236 16,
2237 &kw_aes_info
2238};
2239
2240static const mbedtls_cipher_info_t aes_256_nist_kwp_info = {
2241 MBEDTLS_CIPHER_AES_256_KWP,
2242 MBEDTLS_MODE_KWP,
2243 256,
2244 "AES-256-KWP",
2245 0,
2246 0,
2247 16,
2248 &kw_aes_info
2249};
2250#endif /* MBEDTLS_NIST_KW_C */
2251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002252const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002254#if defined(MBEDTLS_AES_C)
2255 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
2256 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
2257 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
2258#if defined(MBEDTLS_CIPHER_MODE_CBC)
2259 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
2260 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
2261 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263#if defined(MBEDTLS_CIPHER_MODE_CFB)
2264 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
2265 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
2266 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002267#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01002268#if defined(MBEDTLS_CIPHER_MODE_OFB)
2269 { MBEDTLS_CIPHER_AES_128_OFB, &aes_128_ofb_info },
2270 { MBEDTLS_CIPHER_AES_192_OFB, &aes_192_ofb_info },
2271 { MBEDTLS_CIPHER_AES_256_OFB, &aes_256_ofb_info },
2272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#if defined(MBEDTLS_CIPHER_MODE_CTR)
2274 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
2275 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
2276 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002277#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01002278#if defined(MBEDTLS_CIPHER_MODE_XTS)
2279 { MBEDTLS_CIPHER_AES_128_XTS, &aes_128_xts_info },
2280 { MBEDTLS_CIPHER_AES_256_XTS, &aes_256_xts_info },
2281#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#if defined(MBEDTLS_GCM_C)
2283 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
2284 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
2285 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287#if defined(MBEDTLS_CCM_C)
2288 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
2289 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
2290 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02002291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292#endif /* MBEDTLS_AES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294#if defined(MBEDTLS_ARC4_C)
2295 { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002296#endif
2297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#if defined(MBEDTLS_BLOWFISH_C)
2299 { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
2300#if defined(MBEDTLS_CIPHER_MODE_CBC)
2301 { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#if defined(MBEDTLS_CIPHER_MODE_CFB)
2304 { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002305#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306#if defined(MBEDTLS_CIPHER_MODE_CTR)
2307 { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002308#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#endif /* MBEDTLS_BLOWFISH_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#if defined(MBEDTLS_CAMELLIA_C)
2312 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
2313 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
2314 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
2315#if defined(MBEDTLS_CIPHER_MODE_CBC)
2316 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
2317 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
2318 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002319#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320#if defined(MBEDTLS_CIPHER_MODE_CFB)
2321 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
2322 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
2323 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#if defined(MBEDTLS_CIPHER_MODE_CTR)
2326 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
2327 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
2328 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002329#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330#if defined(MBEDTLS_GCM_C)
2331 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
2332 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
2333 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +02002334#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002335#if defined(MBEDTLS_CCM_C)
2336 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
2337 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
2338 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02002339#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#endif /* MBEDTLS_CAMELLIA_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002341
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002342#if defined(MBEDTLS_ARIA_C)
2343 { MBEDTLS_CIPHER_ARIA_128_ECB, &aria_128_ecb_info },
2344 { MBEDTLS_CIPHER_ARIA_192_ECB, &aria_192_ecb_info },
2345 { MBEDTLS_CIPHER_ARIA_256_ECB, &aria_256_ecb_info },
2346#if defined(MBEDTLS_CIPHER_MODE_CBC)
2347 { MBEDTLS_CIPHER_ARIA_128_CBC, &aria_128_cbc_info },
2348 { MBEDTLS_CIPHER_ARIA_192_CBC, &aria_192_cbc_info },
2349 { MBEDTLS_CIPHER_ARIA_256_CBC, &aria_256_cbc_info },
2350#endif
2351#if defined(MBEDTLS_CIPHER_MODE_CFB)
2352 { MBEDTLS_CIPHER_ARIA_128_CFB128, &aria_128_cfb128_info },
2353 { MBEDTLS_CIPHER_ARIA_192_CFB128, &aria_192_cfb128_info },
2354 { MBEDTLS_CIPHER_ARIA_256_CFB128, &aria_256_cfb128_info },
2355#endif
2356#if defined(MBEDTLS_CIPHER_MODE_CTR)
2357 { MBEDTLS_CIPHER_ARIA_128_CTR, &aria_128_ctr_info },
2358 { MBEDTLS_CIPHER_ARIA_192_CTR, &aria_192_ctr_info },
2359 { MBEDTLS_CIPHER_ARIA_256_CTR, &aria_256_ctr_info },
2360#endif
2361#if defined(MBEDTLS_GCM_C)
2362 { MBEDTLS_CIPHER_ARIA_128_GCM, &aria_128_gcm_info },
2363 { MBEDTLS_CIPHER_ARIA_192_GCM, &aria_192_gcm_info },
2364 { MBEDTLS_CIPHER_ARIA_256_GCM, &aria_256_gcm_info },
2365#endif
2366#if defined(MBEDTLS_CCM_C)
2367 { MBEDTLS_CIPHER_ARIA_128_CCM, &aria_128_ccm_info },
2368 { MBEDTLS_CIPHER_ARIA_192_CCM, &aria_192_ccm_info },
2369 { MBEDTLS_CIPHER_ARIA_256_CCM, &aria_256_ccm_info },
2370#endif
2371#endif /* MBEDTLS_ARIA_C */
2372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373#if defined(MBEDTLS_DES_C)
2374 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
2375 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
2376 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
2377#if defined(MBEDTLS_CIPHER_MODE_CBC)
2378 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
2379 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
2380 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382#endif /* MBEDTLS_DES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002383
Daniel Kingbd920622016-05-15 19:56:20 -03002384#if defined(MBEDTLS_CHACHA20_C)
2385 { MBEDTLS_CIPHER_CHACHA20, &chacha20_info },
2386#endif
2387
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002388#if defined(MBEDTLS_CHACHAPOLY_C)
2389 { MBEDTLS_CIPHER_CHACHA20_POLY1305, &chachapoly_info },
Daniel King8fe47012016-05-17 20:33:28 -03002390#endif
2391
Jack Lloydffdf2882019-03-07 17:00:32 -05002392#if defined(MBEDTLS_NIST_KW_C)
2393 { MBEDTLS_CIPHER_AES_128_KW, &aes_128_nist_kw_info },
2394 { MBEDTLS_CIPHER_AES_192_KW, &aes_192_nist_kw_info },
2395 { MBEDTLS_CIPHER_AES_256_KW, &aes_256_nist_kw_info },
2396 { MBEDTLS_CIPHER_AES_128_KWP, &aes_128_nist_kwp_info },
2397 { MBEDTLS_CIPHER_AES_192_KWP, &aes_192_nist_kwp_info },
2398 { MBEDTLS_CIPHER_AES_256_KWP, &aes_256_nist_kwp_info },
2399#endif
2400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2402 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
2403#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 { MBEDTLS_CIPHER_NONE, NULL }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002406};
2407
Hanno Beckerc3d25b32018-11-08 16:01:22 +00002408#define NUM_CIPHERS ( sizeof(mbedtls_cipher_definitions) / \
2409 sizeof(mbedtls_cipher_definitions[0]) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410int mbedtls_cipher_supported[NUM_CIPHERS];
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412#endif /* MBEDTLS_CIPHER_C */