blob: 5776d5e45d4013bd9ba59202906571faa72f3921 [file] [log] [blame]
Paul Bakker8123e9d2011-01-06 15:37:30 +00001/**
Paul Bakkerfae35f02013-03-13 10:33:51 +01002 * \file cipher_wrap.c
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +00004 * \brief Generic cipher wrapper for mbed TLS
Paul Bakker8123e9d2011-01-06 15:37:30 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02008 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02009 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000022 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker8123e9d2011-01-06 15:37:30 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000027
Chris Jonesdaacb592021-03-09 17:03:29 +000028#include "cipher_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000029#include "mbedtls/error.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000030
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020031#if defined(MBEDTLS_CHACHAPOLY_C)
32#include "mbedtls/chachapoly.h"
Daniel King8fe47012016-05-17 20:33:28 -030033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000037#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_CAMELLIA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000041#endif
42
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +000043#if defined(MBEDTLS_ARIA_C)
44#include "mbedtls/aria.h"
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000049#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000050
Daniel Kingbd920622016-05-15 19:56:20 -030051#if defined(MBEDTLS_CHACHA20_C)
52#include "mbedtls/chacha20.h"
53#endif
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020061#endif
62
Jack Lloydffdf2882019-03-07 17:00:32 -050063#if defined(MBEDTLS_NIST_KW_C)
64#include "mbedtls/nist_kw.h"
65#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard0c851ee2015-02-10 12:47:52 +000068#include <string.h>
69#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020073#else
Rich Evans00ab4702015-02-06 13:43:58 +000074#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020075#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020077#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020080/* shared by all GCM ciphers */
81static void *gcm_ctx_alloc( void )
82{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +020083 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
84
85 if( ctx != NULL )
86 mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
87
88 return( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020089}
90
91static void gcm_ctx_free( void *ctx )
92{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_gcm_free( ctx );
94 mbedtls_free( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +020097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020099/* shared by all CCM ciphers */
100static void *ccm_ctx_alloc( void )
101{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +0200102 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
103
104 if( ctx != NULL )
105 mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
106
107 return( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200108}
109
110static void ccm_ctx_free( void *ctx )
111{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_ccm_free( ctx );
113 mbedtls_free( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200114}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200120 const unsigned char *input, unsigned char *output )
121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200123}
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_CIPHER_MODE_CBC)
126static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000127 unsigned char *iv, const unsigned char *input, unsigned char *output )
128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200130 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_CIPHER_MODE_CFB)
135static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200136 size_t length, size_t *iv_off, unsigned char *iv,
137 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000138{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200140 input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000143
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100144#if defined(MBEDTLS_CIPHER_MODE_OFB)
145static int aes_crypt_ofb_wrap( void *ctx, size_t length, size_t *iv_off,
146 unsigned char *iv, const unsigned char *input, unsigned char *output )
147{
148 return mbedtls_aes_crypt_ofb( (mbedtls_aes_context *) ctx, length, iv_off,
149 iv, input, output );
150}
151#endif /* MBEDTLS_CIPHER_MODE_OFB */
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200154static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
155 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000156 const unsigned char *input, unsigned char *output )
157{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
Paul Bakker343a8702011-06-09 14:27:58 +0000159 stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000162
Jaeden Ameroc6539902018-04-30 17:17:41 +0100163#if defined(MBEDTLS_CIPHER_MODE_XTS)
164static int aes_crypt_xts_wrap( void *ctx, mbedtls_operation_t operation,
165 size_t length,
166 const unsigned char data_unit[16],
167 const unsigned char *input,
168 unsigned char *output )
169{
170 mbedtls_aes_xts_context *xts_ctx = ctx;
171 int mode;
172
173 switch( operation )
174 {
175 case MBEDTLS_ENCRYPT:
176 mode = MBEDTLS_AES_ENCRYPT;
177 break;
178 case MBEDTLS_DECRYPT:
179 mode = MBEDTLS_AES_DECRYPT;
180 break;
181 default:
182 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
183 }
184
185 return mbedtls_aes_crypt_xts( xts_ctx, mode, length,
186 data_unit, input, output );
187}
188#endif /* MBEDTLS_CIPHER_MODE_XTS */
189
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200190static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200191 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000192{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200193 return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000194}
195
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200196static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200197 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000198{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200199 return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000200}
201
202static void * aes_ctx_alloc( void )
203{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200204 mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200205
206 if( aes == NULL )
207 return( NULL );
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_aes_init( aes );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200210
211 return( aes );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000212}
213
214static void aes_ctx_free( void *ctx )
215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_aes_free( (mbedtls_aes_context *) ctx );
217 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218}
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220static const mbedtls_cipher_base_t aes_info = {
221 MBEDTLS_CIPHER_ID_AES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200222 aes_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000224 aes_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000227 aes_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100228#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100229#if defined(MBEDTLS_CIPHER_MODE_OFB)
230 aes_crypt_ofb_wrap,
231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000233 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100234#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100235#if defined(MBEDTLS_CIPHER_MODE_XTS)
236 NULL,
237#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200239 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100240#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000241 aes_setkey_enc_wrap,
242 aes_setkey_dec_wrap,
243 aes_ctx_alloc,
244 aes_ctx_free
245};
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247static const mbedtls_cipher_info_t aes_128_ecb_info = {
248 MBEDTLS_CIPHER_AES_128_ECB,
249 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200250 128,
251 "AES-128-ECB",
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300252 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200253 0,
254 16,
255 &aes_info
256};
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258static const mbedtls_cipher_info_t aes_192_ecb_info = {
259 MBEDTLS_CIPHER_AES_192_ECB,
260 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200261 192,
262 "AES-192-ECB",
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300263 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200264 0,
265 16,
266 &aes_info
267};
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269static const mbedtls_cipher_info_t aes_256_ecb_info = {
270 MBEDTLS_CIPHER_AES_256_ECB,
271 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200272 256,
273 "AES-256-ECB",
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300274 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200275 0,
276 16,
277 &aes_info
278};
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_CIPHER_MODE_CBC)
281static const mbedtls_cipher_info_t aes_128_cbc_info = {
282 MBEDTLS_CIPHER_AES_128_CBC,
283 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000284 128,
285 "AES-128-CBC",
286 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200287 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000288 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000289 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000290};
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292static const mbedtls_cipher_info_t aes_192_cbc_info = {
293 MBEDTLS_CIPHER_AES_192_CBC,
294 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000295 192,
296 "AES-192-CBC",
297 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200298 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000299 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000300 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000301};
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303static const mbedtls_cipher_info_t aes_256_cbc_info = {
304 MBEDTLS_CIPHER_AES_256_CBC,
305 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000306 256,
307 "AES-256-CBC",
308 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200309 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000310 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000311 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000312};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_CIPHER_MODE_CFB)
316static const mbedtls_cipher_info_t aes_128_cfb128_info = {
317 MBEDTLS_CIPHER_AES_128_CFB128,
318 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000319 128,
320 "AES-128-CFB128",
321 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200322 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000323 16,
324 &aes_info
325};
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327static const mbedtls_cipher_info_t aes_192_cfb128_info = {
328 MBEDTLS_CIPHER_AES_192_CFB128,
329 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000330 192,
331 "AES-192-CFB128",
332 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200333 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000334 16,
335 &aes_info
336};
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338static const mbedtls_cipher_info_t aes_256_cfb128_info = {
339 MBEDTLS_CIPHER_AES_256_CFB128,
340 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000341 256,
342 "AES-256-CFB128",
343 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200344 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000345 16,
346 &aes_info
347};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000349
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100350#if defined(MBEDTLS_CIPHER_MODE_OFB)
351static const mbedtls_cipher_info_t aes_128_ofb_info = {
352 MBEDTLS_CIPHER_AES_128_OFB,
353 MBEDTLS_MODE_OFB,
354 128,
355 "AES-128-OFB",
356 16,
357 0,
358 16,
359 &aes_info
360};
361
362static const mbedtls_cipher_info_t aes_192_ofb_info = {
363 MBEDTLS_CIPHER_AES_192_OFB,
364 MBEDTLS_MODE_OFB,
365 192,
366 "AES-192-OFB",
367 16,
368 0,
369 16,
370 &aes_info
371};
372
373static const mbedtls_cipher_info_t aes_256_ofb_info = {
374 MBEDTLS_CIPHER_AES_256_OFB,
375 MBEDTLS_MODE_OFB,
376 256,
377 "AES-256-OFB",
378 16,
379 0,
380 16,
381 &aes_info
382};
383#endif /* MBEDTLS_CIPHER_MODE_OFB */
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_CIPHER_MODE_CTR)
386static const mbedtls_cipher_info_t aes_128_ctr_info = {
387 MBEDTLS_CIPHER_AES_128_CTR,
388 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000389 128,
390 "AES-128-CTR",
391 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200392 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000393 16,
394 &aes_info
395};
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397static const mbedtls_cipher_info_t aes_192_ctr_info = {
398 MBEDTLS_CIPHER_AES_192_CTR,
399 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000400 192,
401 "AES-192-CTR",
402 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200403 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000404 16,
405 &aes_info
406};
407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408static const mbedtls_cipher_info_t aes_256_ctr_info = {
409 MBEDTLS_CIPHER_AES_256_CTR,
410 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000411 256,
412 "AES-256-CTR",
413 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200414 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000415 16,
416 &aes_info
417};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000419
Jaeden Ameroc6539902018-04-30 17:17:41 +0100420#if defined(MBEDTLS_CIPHER_MODE_XTS)
421static int xts_aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
422 unsigned int key_bitlen )
423{
424 mbedtls_aes_xts_context *xts_ctx = ctx;
425 return( mbedtls_aes_xts_setkey_enc( xts_ctx, key, key_bitlen ) );
426}
427
428static int xts_aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
429 unsigned int key_bitlen )
430{
431 mbedtls_aes_xts_context *xts_ctx = ctx;
432 return( mbedtls_aes_xts_setkey_dec( xts_ctx, key, key_bitlen ) );
433}
434
435static void *xts_aes_ctx_alloc( void )
436{
437 mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc( 1, sizeof( *xts_ctx ) );
438
439 if( xts_ctx != NULL )
440 mbedtls_aes_xts_init( xts_ctx );
441
442 return( xts_ctx );
443}
444
445static void xts_aes_ctx_free( void *ctx )
446{
447 mbedtls_aes_xts_context *xts_ctx = ctx;
448
449 if( xts_ctx == NULL )
450 return;
451
452 mbedtls_aes_xts_free( xts_ctx );
453 mbedtls_free( xts_ctx );
454}
455
456static const mbedtls_cipher_base_t xts_aes_info = {
457 MBEDTLS_CIPHER_ID_AES,
458 NULL,
459#if defined(MBEDTLS_CIPHER_MODE_CBC)
460 NULL,
461#endif
462#if defined(MBEDTLS_CIPHER_MODE_CFB)
463 NULL,
464#endif
465#if defined(MBEDTLS_CIPHER_MODE_OFB)
466 NULL,
467#endif
468#if defined(MBEDTLS_CIPHER_MODE_CTR)
469 NULL,
470#endif
471#if defined(MBEDTLS_CIPHER_MODE_XTS)
472 aes_crypt_xts_wrap,
473#endif
474#if defined(MBEDTLS_CIPHER_MODE_STREAM)
475 NULL,
476#endif
477 xts_aes_setkey_enc_wrap,
478 xts_aes_setkey_dec_wrap,
479 xts_aes_ctx_alloc,
480 xts_aes_ctx_free
481};
482
483static const mbedtls_cipher_info_t aes_128_xts_info = {
484 MBEDTLS_CIPHER_AES_128_XTS,
485 MBEDTLS_MODE_XTS,
486 256,
487 "AES-128-XTS",
488 16,
489 0,
490 16,
491 &xts_aes_info
492};
493
494static const mbedtls_cipher_info_t aes_256_xts_info = {
495 MBEDTLS_CIPHER_AES_256_XTS,
496 MBEDTLS_MODE_XTS,
497 512,
498 "AES-256-XTS",
499 16,
500 0,
501 16,
502 &xts_aes_info
503};
504#endif /* MBEDTLS_CIPHER_MODE_XTS */
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200507static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200508 unsigned int key_bitlen )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200509{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200510 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200511 key, key_bitlen );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200512}
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514static const mbedtls_cipher_base_t gcm_aes_info = {
515 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200516 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200518 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100519#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200521 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100522#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100523#if defined(MBEDTLS_CIPHER_MODE_OFB)
524 NULL,
525#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200527 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100528#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100529#if defined(MBEDTLS_CIPHER_MODE_XTS)
530 NULL,
531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200533 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100534#endif
Paul Bakker43aff2a2013-09-09 00:10:27 +0200535 gcm_aes_setkey_wrap,
536 gcm_aes_setkey_wrap,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200537 gcm_ctx_alloc,
538 gcm_ctx_free,
539};
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541static const mbedtls_cipher_info_t aes_128_gcm_info = {
542 MBEDTLS_CIPHER_AES_128_GCM,
543 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100544 128,
545 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200546 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100548 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200549 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100550};
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552static const mbedtls_cipher_info_t aes_192_gcm_info = {
553 MBEDTLS_CIPHER_AES_192_GCM,
554 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200555 192,
556 "AES-192-GCM",
557 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200559 16,
560 &gcm_aes_info
561};
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563static const mbedtls_cipher_info_t aes_256_gcm_info = {
564 MBEDTLS_CIPHER_AES_256_GCM,
565 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100566 256,
567 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200568 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100570 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200571 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100572};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#endif /* MBEDTLS_GCM_C */
Paul Bakker68884e32013-01-07 18:20:04 +0100574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200576static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200577 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200578{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200579 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200580 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200581}
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583static const mbedtls_cipher_base_t ccm_aes_info = {
584 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200585 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200587 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100588#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200590 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100591#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100592#if defined(MBEDTLS_CIPHER_MODE_OFB)
593 NULL,
594#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200596 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100597#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100598#if defined(MBEDTLS_CIPHER_MODE_XTS)
599 NULL,
600#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200602 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100603#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200604 ccm_aes_setkey_wrap,
605 ccm_aes_setkey_wrap,
606 ccm_ctx_alloc,
607 ccm_ctx_free,
608};
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610static const mbedtls_cipher_info_t aes_128_ccm_info = {
611 MBEDTLS_CIPHER_AES_128_CCM,
612 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200613 128,
614 "AES-128-CCM",
615 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200617 16,
618 &ccm_aes_info
619};
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621static const mbedtls_cipher_info_t aes_192_ccm_info = {
622 MBEDTLS_CIPHER_AES_192_CCM,
623 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200624 192,
625 "AES-192-CCM",
626 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200628 16,
629 &ccm_aes_info
630};
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632static const mbedtls_cipher_info_t aes_256_ccm_info = {
633 MBEDTLS_CIPHER_AES_256_CCM,
634 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200635 256,
636 "AES-256-CCM",
637 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200639 16,
640 &ccm_aes_info
641};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_AES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#if defined(MBEDTLS_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200649 const unsigned char *input, unsigned char *output )
650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200652 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200653}
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_CIPHER_MODE_CBC)
656static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200657 size_t length, unsigned char *iv,
658 const unsigned char *input, unsigned char *output )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200661 input, output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_CIPHER_MODE_CFB)
666static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200667 size_t length, size_t *iv_off, unsigned char *iv,
668 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000669{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200671 iv_off, iv, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000672}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200676static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
677 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000678 const unsigned char *input, unsigned char *output )
679{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200681 nonce_counter, stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000684
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200685static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200686 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000687{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200688 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000689}
690
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200691static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200692 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000693{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200694 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000695}
696
697static void * camellia_ctx_alloc( void )
698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_camellia_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200700 ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200701
702 if( ctx == NULL )
703 return( NULL );
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 mbedtls_camellia_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200706
707 return( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000708}
709
710static void camellia_ctx_free( void *ctx )
711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
713 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000714}
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716static const mbedtls_cipher_base_t camellia_info = {
717 MBEDTLS_CIPHER_ID_CAMELLIA,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200718 camellia_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000720 camellia_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000723 camellia_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100724#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100725#if defined(MBEDTLS_CIPHER_MODE_OFB)
726 NULL,
727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000729 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100730#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100731#if defined(MBEDTLS_CIPHER_MODE_XTS)
732 NULL,
733#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200735 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100736#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000737 camellia_setkey_enc_wrap,
738 camellia_setkey_dec_wrap,
739 camellia_ctx_alloc,
740 camellia_ctx_free
741};
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743static const mbedtls_cipher_info_t camellia_128_ecb_info = {
744 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
745 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200746 128,
747 "CAMELLIA-128-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +0100748 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200749 0,
750 16,
751 &camellia_info
752};
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754static const mbedtls_cipher_info_t camellia_192_ecb_info = {
755 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
756 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200757 192,
758 "CAMELLIA-192-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +0100759 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200760 0,
761 16,
762 &camellia_info
763};
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765static const mbedtls_cipher_info_t camellia_256_ecb_info = {
766 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
767 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200768 256,
769 "CAMELLIA-256-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +0100770 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200771 0,
772 16,
773 &camellia_info
774};
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#if defined(MBEDTLS_CIPHER_MODE_CBC)
777static const mbedtls_cipher_info_t camellia_128_cbc_info = {
778 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
779 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000780 128,
781 "CAMELLIA-128-CBC",
782 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200783 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000784 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000785 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000786};
787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788static const mbedtls_cipher_info_t camellia_192_cbc_info = {
789 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
790 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000791 192,
792 "CAMELLIA-192-CBC",
793 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200794 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000795 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000796 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000797};
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799static const mbedtls_cipher_info_t camellia_256_cbc_info = {
800 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
801 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000802 256,
803 "CAMELLIA-256-CBC",
804 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200805 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000806 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000807 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000808};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#if defined(MBEDTLS_CIPHER_MODE_CFB)
812static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
813 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
814 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000815 128,
816 "CAMELLIA-128-CFB128",
817 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200818 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000819 16,
820 &camellia_info
821};
822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
824 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
825 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000826 192,
827 "CAMELLIA-192-CFB128",
828 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200829 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000830 16,
831 &camellia_info
832};
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
835 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
836 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000837 256,
838 "CAMELLIA-256-CFB128",
839 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200840 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000841 16,
842 &camellia_info
843};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#if defined(MBEDTLS_CIPHER_MODE_CTR)
847static const mbedtls_cipher_info_t camellia_128_ctr_info = {
848 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
849 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000850 128,
851 "CAMELLIA-128-CTR",
852 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200853 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000854 16,
855 &camellia_info
856};
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858static const mbedtls_cipher_info_t camellia_192_ctr_info = {
859 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
860 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000861 192,
862 "CAMELLIA-192-CTR",
863 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200864 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000865 16,
866 &camellia_info
867};
868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869static const mbedtls_cipher_info_t camellia_256_ctr_info = {
870 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
871 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000872 256,
873 "CAMELLIA-256-CTR",
874 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200875 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000876 16,
877 &camellia_info
878};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200882static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200883 unsigned int key_bitlen )
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200884{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200885 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200886 key, key_bitlen );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200887}
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889static const mbedtls_cipher_base_t gcm_camellia_info = {
890 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200891 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200893 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100894#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200896 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100897#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100898#if defined(MBEDTLS_CIPHER_MODE_OFB)
899 NULL,
900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200902 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100903#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100904#if defined(MBEDTLS_CIPHER_MODE_XTS)
905 NULL,
906#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200908 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100909#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200910 gcm_camellia_setkey_wrap,
911 gcm_camellia_setkey_wrap,
912 gcm_ctx_alloc,
913 gcm_ctx_free,
914};
915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916static const mbedtls_cipher_info_t camellia_128_gcm_info = {
917 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
918 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200919 128,
920 "CAMELLIA-128-GCM",
921 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200923 16,
924 &gcm_camellia_info
925};
926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927static const mbedtls_cipher_info_t camellia_192_gcm_info = {
928 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
929 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200930 192,
931 "CAMELLIA-192-GCM",
932 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200934 16,
935 &gcm_camellia_info
936};
937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938static const mbedtls_cipher_info_t camellia_256_gcm_info = {
939 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
940 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200941 256,
942 "CAMELLIA-256-GCM",
943 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200945 16,
946 &gcm_camellia_info
947};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200951static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200952 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200953{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200954 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200955 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200956}
957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958static const mbedtls_cipher_base_t ccm_camellia_info = {
959 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200960 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200962 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100963#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200965 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100966#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +0100967#if defined(MBEDTLS_CIPHER_MODE_OFB)
968 NULL,
969#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200971 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100972#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +0100973#if defined(MBEDTLS_CIPHER_MODE_XTS)
974 NULL,
975#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200977 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100978#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200979 ccm_camellia_setkey_wrap,
980 ccm_camellia_setkey_wrap,
981 ccm_ctx_alloc,
982 ccm_ctx_free,
983};
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985static const mbedtls_cipher_info_t camellia_128_ccm_info = {
986 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
987 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200988 128,
989 "CAMELLIA-128-CCM",
990 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200992 16,
993 &ccm_camellia_info
994};
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996static const mbedtls_cipher_info_t camellia_192_ccm_info = {
997 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
998 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200999 192,
1000 "CAMELLIA-192-CCM",
1001 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001003 16,
1004 &ccm_camellia_info
1005};
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007static const mbedtls_cipher_info_t camellia_256_ccm_info = {
1008 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
1009 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001010 256,
1011 "CAMELLIA-256-CCM",
1012 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001014 16,
1015 &ccm_camellia_info
1016};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#endif /* MBEDTLS_CAMELLIA_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001020
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001021#if defined(MBEDTLS_ARIA_C)
1022
1023static int aria_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
1024 const unsigned char *input, unsigned char *output )
1025{
Manuel Pégourié-Gonnard08c337d2018-05-22 13:18:01 +02001026 (void) operation;
1027 return mbedtls_aria_crypt_ecb( (mbedtls_aria_context *) ctx, input,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001028 output );
1029}
1030
1031#if defined(MBEDTLS_CIPHER_MODE_CBC)
1032static int aria_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
1033 size_t length, unsigned char *iv,
1034 const unsigned char *input, unsigned char *output )
1035{
Manuel Pégourié-Gonnard39f25612018-05-24 14:06:02 +02001036 return mbedtls_aria_crypt_cbc( (mbedtls_aria_context *) ctx, operation, length, iv,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001037 input, output );
1038}
1039#endif /* MBEDTLS_CIPHER_MODE_CBC */
1040
1041#if defined(MBEDTLS_CIPHER_MODE_CFB)
1042static int aria_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
1043 size_t length, size_t *iv_off, unsigned char *iv,
1044 const unsigned char *input, unsigned char *output )
1045{
1046 return mbedtls_aria_crypt_cfb128( (mbedtls_aria_context *) ctx, operation, length,
1047 iv_off, iv, input, output );
1048}
1049#endif /* MBEDTLS_CIPHER_MODE_CFB */
1050
1051#if defined(MBEDTLS_CIPHER_MODE_CTR)
1052static int aria_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1053 unsigned char *nonce_counter, unsigned char *stream_block,
1054 const unsigned char *input, unsigned char *output )
1055{
1056 return mbedtls_aria_crypt_ctr( (mbedtls_aria_context *) ctx, length, nc_off,
1057 nonce_counter, stream_block, input, output );
1058}
1059#endif /* MBEDTLS_CIPHER_MODE_CTR */
1060
1061static int aria_setkey_dec_wrap( void *ctx, const unsigned char *key,
1062 unsigned int key_bitlen )
1063{
1064 return mbedtls_aria_setkey_dec( (mbedtls_aria_context *) ctx, key, key_bitlen );
1065}
1066
1067static int aria_setkey_enc_wrap( void *ctx, const unsigned char *key,
1068 unsigned int key_bitlen )
1069{
1070 return mbedtls_aria_setkey_enc( (mbedtls_aria_context *) ctx, key, key_bitlen );
1071}
1072
1073static void * aria_ctx_alloc( void )
1074{
1075 mbedtls_aria_context *ctx;
1076 ctx = mbedtls_calloc( 1, sizeof( mbedtls_aria_context ) );
1077
1078 if( ctx == NULL )
1079 return( NULL );
1080
1081 mbedtls_aria_init( ctx );
1082
1083 return( ctx );
1084}
1085
1086static void aria_ctx_free( void *ctx )
1087{
1088 mbedtls_aria_free( (mbedtls_aria_context *) ctx );
1089 mbedtls_free( ctx );
1090}
1091
1092static const mbedtls_cipher_base_t aria_info = {
1093 MBEDTLS_CIPHER_ID_ARIA,
1094 aria_crypt_ecb_wrap,
1095#if defined(MBEDTLS_CIPHER_MODE_CBC)
1096 aria_crypt_cbc_wrap,
1097#endif
1098#if defined(MBEDTLS_CIPHER_MODE_CFB)
1099 aria_crypt_cfb128_wrap,
1100#endif
Simon Butcher4844bf22018-06-11 15:21:05 +01001101#if defined(MBEDTLS_CIPHER_MODE_OFB)
1102 NULL,
1103#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001104#if defined(MBEDTLS_CIPHER_MODE_CTR)
1105 aria_crypt_ctr_wrap,
1106#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001107#if defined(MBEDTLS_CIPHER_MODE_XTS)
1108 NULL,
1109#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001110#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1111 NULL,
1112#endif
1113 aria_setkey_enc_wrap,
1114 aria_setkey_dec_wrap,
1115 aria_ctx_alloc,
1116 aria_ctx_free
1117};
1118
1119static const mbedtls_cipher_info_t aria_128_ecb_info = {
1120 MBEDTLS_CIPHER_ARIA_128_ECB,
1121 MBEDTLS_MODE_ECB,
1122 128,
1123 "ARIA-128-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001124 0,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001125 0,
1126 16,
1127 &aria_info
1128};
1129
1130static const mbedtls_cipher_info_t aria_192_ecb_info = {
1131 MBEDTLS_CIPHER_ARIA_192_ECB,
1132 MBEDTLS_MODE_ECB,
1133 192,
1134 "ARIA-192-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001135 0,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001136 0,
1137 16,
1138 &aria_info
1139};
1140
1141static const mbedtls_cipher_info_t aria_256_ecb_info = {
1142 MBEDTLS_CIPHER_ARIA_256_ECB,
1143 MBEDTLS_MODE_ECB,
1144 256,
1145 "ARIA-256-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001146 0,
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001147 0,
1148 16,
1149 &aria_info
1150};
1151
1152#if defined(MBEDTLS_CIPHER_MODE_CBC)
1153static const mbedtls_cipher_info_t aria_128_cbc_info = {
1154 MBEDTLS_CIPHER_ARIA_128_CBC,
1155 MBEDTLS_MODE_CBC,
1156 128,
1157 "ARIA-128-CBC",
1158 16,
1159 0,
1160 16,
1161 &aria_info
1162};
1163
1164static const mbedtls_cipher_info_t aria_192_cbc_info = {
1165 MBEDTLS_CIPHER_ARIA_192_CBC,
1166 MBEDTLS_MODE_CBC,
1167 192,
1168 "ARIA-192-CBC",
1169 16,
1170 0,
1171 16,
1172 &aria_info
1173};
1174
1175static const mbedtls_cipher_info_t aria_256_cbc_info = {
1176 MBEDTLS_CIPHER_ARIA_256_CBC,
1177 MBEDTLS_MODE_CBC,
1178 256,
1179 "ARIA-256-CBC",
1180 16,
1181 0,
1182 16,
1183 &aria_info
1184};
1185#endif /* MBEDTLS_CIPHER_MODE_CBC */
1186
1187#if defined(MBEDTLS_CIPHER_MODE_CFB)
1188static const mbedtls_cipher_info_t aria_128_cfb128_info = {
1189 MBEDTLS_CIPHER_ARIA_128_CFB128,
1190 MBEDTLS_MODE_CFB,
1191 128,
1192 "ARIA-128-CFB128",
1193 16,
1194 0,
1195 16,
1196 &aria_info
1197};
1198
1199static const mbedtls_cipher_info_t aria_192_cfb128_info = {
1200 MBEDTLS_CIPHER_ARIA_192_CFB128,
1201 MBEDTLS_MODE_CFB,
1202 192,
1203 "ARIA-192-CFB128",
1204 16,
1205 0,
1206 16,
1207 &aria_info
1208};
1209
1210static const mbedtls_cipher_info_t aria_256_cfb128_info = {
1211 MBEDTLS_CIPHER_ARIA_256_CFB128,
1212 MBEDTLS_MODE_CFB,
1213 256,
1214 "ARIA-256-CFB128",
1215 16,
1216 0,
1217 16,
1218 &aria_info
1219};
1220#endif /* MBEDTLS_CIPHER_MODE_CFB */
1221
1222#if defined(MBEDTLS_CIPHER_MODE_CTR)
1223static const mbedtls_cipher_info_t aria_128_ctr_info = {
1224 MBEDTLS_CIPHER_ARIA_128_CTR,
1225 MBEDTLS_MODE_CTR,
1226 128,
1227 "ARIA-128-CTR",
1228 16,
1229 0,
1230 16,
1231 &aria_info
1232};
1233
1234static const mbedtls_cipher_info_t aria_192_ctr_info = {
1235 MBEDTLS_CIPHER_ARIA_192_CTR,
1236 MBEDTLS_MODE_CTR,
1237 192,
1238 "ARIA-192-CTR",
1239 16,
1240 0,
1241 16,
1242 &aria_info
1243};
1244
1245static const mbedtls_cipher_info_t aria_256_ctr_info = {
1246 MBEDTLS_CIPHER_ARIA_256_CTR,
1247 MBEDTLS_MODE_CTR,
1248 256,
1249 "ARIA-256-CTR",
1250 16,
1251 0,
1252 16,
1253 &aria_info
1254};
1255#endif /* MBEDTLS_CIPHER_MODE_CTR */
1256
1257#if defined(MBEDTLS_GCM_C)
1258static int gcm_aria_setkey_wrap( void *ctx, const unsigned char *key,
1259 unsigned int key_bitlen )
1260{
1261 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1262 key, key_bitlen );
1263}
1264
1265static const mbedtls_cipher_base_t gcm_aria_info = {
1266 MBEDTLS_CIPHER_ID_ARIA,
1267 NULL,
1268#if defined(MBEDTLS_CIPHER_MODE_CBC)
1269 NULL,
1270#endif
1271#if defined(MBEDTLS_CIPHER_MODE_CFB)
1272 NULL,
1273#endif
Simon Butcher4844bf22018-06-11 15:21:05 +01001274#if defined(MBEDTLS_CIPHER_MODE_OFB)
1275 NULL,
1276#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001277#if defined(MBEDTLS_CIPHER_MODE_CTR)
1278 NULL,
1279#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001280#if defined(MBEDTLS_CIPHER_MODE_XTS)
1281 NULL,
1282#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001283#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1284 NULL,
1285#endif
1286 gcm_aria_setkey_wrap,
1287 gcm_aria_setkey_wrap,
1288 gcm_ctx_alloc,
1289 gcm_ctx_free,
1290};
1291
1292static const mbedtls_cipher_info_t aria_128_gcm_info = {
1293 MBEDTLS_CIPHER_ARIA_128_GCM,
1294 MBEDTLS_MODE_GCM,
1295 128,
1296 "ARIA-128-GCM",
1297 12,
1298 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1299 16,
1300 &gcm_aria_info
1301};
1302
1303static const mbedtls_cipher_info_t aria_192_gcm_info = {
1304 MBEDTLS_CIPHER_ARIA_192_GCM,
1305 MBEDTLS_MODE_GCM,
1306 192,
1307 "ARIA-192-GCM",
1308 12,
1309 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1310 16,
1311 &gcm_aria_info
1312};
1313
1314static const mbedtls_cipher_info_t aria_256_gcm_info = {
1315 MBEDTLS_CIPHER_ARIA_256_GCM,
1316 MBEDTLS_MODE_GCM,
1317 256,
1318 "ARIA-256-GCM",
1319 12,
1320 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1321 16,
1322 &gcm_aria_info
1323};
1324#endif /* MBEDTLS_GCM_C */
1325
1326#if defined(MBEDTLS_CCM_C)
1327static int ccm_aria_setkey_wrap( void *ctx, const unsigned char *key,
1328 unsigned int key_bitlen )
1329{
1330 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1331 key, key_bitlen );
1332}
1333
1334static const mbedtls_cipher_base_t ccm_aria_info = {
1335 MBEDTLS_CIPHER_ID_ARIA,
1336 NULL,
1337#if defined(MBEDTLS_CIPHER_MODE_CBC)
1338 NULL,
1339#endif
1340#if defined(MBEDTLS_CIPHER_MODE_CFB)
1341 NULL,
1342#endif
Simon Butcher7487c5b2018-04-29 00:24:51 +01001343#if defined(MBEDTLS_CIPHER_MODE_OFB)
1344 NULL,
1345#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001346#if defined(MBEDTLS_CIPHER_MODE_CTR)
1347 NULL,
1348#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001349#if defined(MBEDTLS_CIPHER_MODE_XTS)
1350 NULL,
1351#endif
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00001352#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1353 NULL,
1354#endif
1355 ccm_aria_setkey_wrap,
1356 ccm_aria_setkey_wrap,
1357 ccm_ctx_alloc,
1358 ccm_ctx_free,
1359};
1360
1361static const mbedtls_cipher_info_t aria_128_ccm_info = {
1362 MBEDTLS_CIPHER_ARIA_128_CCM,
1363 MBEDTLS_MODE_CCM,
1364 128,
1365 "ARIA-128-CCM",
1366 12,
1367 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1368 16,
1369 &ccm_aria_info
1370};
1371
1372static const mbedtls_cipher_info_t aria_192_ccm_info = {
1373 MBEDTLS_CIPHER_ARIA_192_CCM,
1374 MBEDTLS_MODE_CCM,
1375 192,
1376 "ARIA-192-CCM",
1377 12,
1378 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1379 16,
1380 &ccm_aria_info
1381};
1382
1383static const mbedtls_cipher_info_t aria_256_ccm_info = {
1384 MBEDTLS_CIPHER_ARIA_256_CCM,
1385 MBEDTLS_MODE_CCM,
1386 256,
1387 "ARIA-256-CCM",
1388 12,
1389 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1390 16,
1391 &ccm_aria_info
1392};
1393#endif /* MBEDTLS_CCM_C */
1394
1395#endif /* MBEDTLS_ARIA_C */
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +00001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001400 const unsigned char *input, unsigned char *output )
1401{
1402 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001404}
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001407 const unsigned char *input, unsigned char *output )
1408{
1409 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001411}
1412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_CIPHER_MODE_CBC)
1414static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +00001415 unsigned char *iv, const unsigned char *input, unsigned char *output )
1416{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001418 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_CIPHER_MODE_CBC)
1423static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +00001424 unsigned char *iv, const unsigned char *input, unsigned char *output )
1425{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001427 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001430
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001431static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001432 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001433{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001434 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001437}
1438
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001439static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001440 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001441{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001442 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001445}
1446
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001447static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001448 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001449{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001450 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001453}
1454
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001455static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001456 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001457{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001458 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001461}
1462
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001463static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001464 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001465{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001466 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001469}
1470
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001471static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001472 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +00001473{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001474 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +00001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001477}
1478
1479static void * des_ctx_alloc( void )
1480{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001481 mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001482
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001483 if( des == NULL )
1484 return( NULL );
1485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 mbedtls_des_init( des );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001487
1488 return( des );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001489}
1490
1491static void des_ctx_free( void *ctx )
1492{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 mbedtls_des_free( (mbedtls_des_context *) ctx );
1494 mbedtls_free( ctx );
Paul Bakker34617722014-06-13 17:20:13 +02001495}
1496
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001497static void * des3_ctx_alloc( void )
1498{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 mbedtls_des3_context *des3;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001500 des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001501
1502 if( des3 == NULL )
1503 return( NULL );
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 mbedtls_des3_init( des3 );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001506
1507 return( des3 );
1508}
1509
Paul Bakker34617722014-06-13 17:20:13 +02001510static void des3_ctx_free( void *ctx )
1511{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 mbedtls_des3_free( (mbedtls_des3_context *) ctx );
1513 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +00001514}
1515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516static const mbedtls_cipher_base_t des_info = {
1517 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001518 des_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +00001520 des_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001521#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001523 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001524#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001525#if defined(MBEDTLS_CIPHER_MODE_OFB)
1526 NULL,
1527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001529 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001530#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001531#if defined(MBEDTLS_CIPHER_MODE_XTS)
1532 NULL,
1533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001535 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001536#endif
Paul Bakker23986e52011-04-24 08:57:21 +00001537 des_setkey_enc_wrap,
1538 des_setkey_dec_wrap,
1539 des_ctx_alloc,
1540 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +00001541};
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543static const mbedtls_cipher_info_t des_ecb_info = {
1544 MBEDTLS_CIPHER_DES_ECB,
1545 MBEDTLS_MODE_ECB,
1546 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001547 "DES-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001548 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001549 0,
1550 8,
1551 &des_info
1552};
1553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_CIPHER_MODE_CBC)
1555static const mbedtls_cipher_info_t des_cbc_info = {
1556 MBEDTLS_CIPHER_DES_CBC,
1557 MBEDTLS_MODE_CBC,
1558 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker343a8702011-06-09 14:27:58 +00001559 "DES-CBC",
1560 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001561 0,
Paul Bakker343a8702011-06-09 14:27:58 +00001562 8,
1563 &des_info
1564};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567static const mbedtls_cipher_base_t des_ede_info = {
1568 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001569 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +00001571 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001572#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001574 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001575#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001576#if defined(MBEDTLS_CIPHER_MODE_OFB)
1577 NULL,
1578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001580 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001581#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001582#if defined(MBEDTLS_CIPHER_MODE_XTS)
1583 NULL,
1584#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001586 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001587#endif
Paul Bakker23986e52011-04-24 08:57:21 +00001588 des3_set2key_enc_wrap,
1589 des3_set2key_dec_wrap,
1590 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001591 des3_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +00001592};
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594static const mbedtls_cipher_info_t des_ede_ecb_info = {
1595 MBEDTLS_CIPHER_DES_EDE_ECB,
1596 MBEDTLS_MODE_ECB,
1597 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001598 "DES-EDE-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001599 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001600 0,
1601 8,
1602 &des_ede_info
1603};
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_CIPHER_MODE_CBC)
1606static const mbedtls_cipher_info_t des_ede_cbc_info = {
1607 MBEDTLS_CIPHER_DES_EDE_CBC,
1608 MBEDTLS_MODE_CBC,
1609 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker343a8702011-06-09 14:27:58 +00001610 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +02001611 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001612 0,
Paul Bakker0e342352013-06-24 19:33:02 +02001613 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001614 &des_ede_info
1615};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618static const mbedtls_cipher_base_t des_ede3_info = {
Manuel Pégourié-Gonnard9d515832015-06-02 10:00:04 +01001619 MBEDTLS_CIPHER_ID_3DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001620 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +00001622 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001623#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001625 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001626#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01001627#if defined(MBEDTLS_CIPHER_MODE_OFB)
1628 NULL,
1629#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001631 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001632#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001633#if defined(MBEDTLS_CIPHER_MODE_XTS)
1634 NULL,
1635#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001637 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001638#endif
Paul Bakker343a8702011-06-09 14:27:58 +00001639 des3_set3key_enc_wrap,
1640 des3_set3key_dec_wrap,
1641 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001642 des3_ctx_free
Paul Bakker343a8702011-06-09 14:27:58 +00001643};
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1646 MBEDTLS_CIPHER_DES_EDE3_ECB,
1647 MBEDTLS_MODE_ECB,
1648 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001649 "DES-EDE3-ECB",
Bence Szépkútia8e40dd2020-10-29 10:22:35 +01001650 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001651 0,
1652 8,
1653 &des_ede3_info
1654};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655#if defined(MBEDTLS_CIPHER_MODE_CBC)
1656static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1657 MBEDTLS_CIPHER_DES_EDE3_CBC,
1658 MBEDTLS_MODE_CBC,
1659 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker23986e52011-04-24 08:57:21 +00001660 "DES-EDE3-CBC",
1661 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001662 0,
Paul Bakker23986e52011-04-24 08:57:21 +00001663 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001664 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +00001665};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#endif /* MBEDTLS_CIPHER_MODE_CBC */
1667#endif /* MBEDTLS_DES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001668
Daniel Kingbd920622016-05-15 19:56:20 -03001669#if defined(MBEDTLS_CHACHA20_C)
1670
1671static int chacha20_setkey_wrap( void *ctx, const unsigned char *key,
1672 unsigned int key_bitlen )
1673{
1674 if( key_bitlen != 256U )
1675 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1676
1677 if ( 0 != mbedtls_chacha20_setkey( (mbedtls_chacha20_context*)ctx, key ) )
1678 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1679
1680 return( 0 );
1681}
1682
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001683static int chacha20_stream_wrap( void *ctx, size_t length,
1684 const unsigned char *input,
1685 unsigned char *output )
1686{
Janos Follath24eed8d2019-11-22 13:21:35 +00001687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001688
1689 ret = mbedtls_chacha20_update( ctx, length, input, output );
1690 if( ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA )
1691 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1692
1693 return( ret );
1694}
1695
Daniel Kingbd920622016-05-15 19:56:20 -03001696static void * chacha20_ctx_alloc( void )
1697{
1698 mbedtls_chacha20_context *ctx;
1699 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chacha20_context ) );
1700
1701 if( ctx == NULL )
1702 return( NULL );
1703
1704 mbedtls_chacha20_init( ctx );
1705
1706 return( ctx );
1707}
1708
1709static void chacha20_ctx_free( void *ctx )
1710{
1711 mbedtls_chacha20_free( (mbedtls_chacha20_context *) ctx );
1712 mbedtls_free( ctx );
1713}
1714
1715static const mbedtls_cipher_base_t chacha20_base_info = {
1716 MBEDTLS_CIPHER_ID_CHACHA20,
1717 NULL,
1718#if defined(MBEDTLS_CIPHER_MODE_CBC)
1719 NULL,
1720#endif
1721#if defined(MBEDTLS_CIPHER_MODE_CFB)
1722 NULL,
1723#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02001724#if defined(MBEDTLS_CIPHER_MODE_OFB)
1725 NULL,
1726#endif
Daniel Kingbd920622016-05-15 19:56:20 -03001727#if defined(MBEDTLS_CIPHER_MODE_CTR)
1728 NULL,
1729#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02001730#if defined(MBEDTLS_CIPHER_MODE_XTS)
1731 NULL,
1732#endif
Daniel Kingbd920622016-05-15 19:56:20 -03001733#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001734 chacha20_stream_wrap,
Daniel Kingbd920622016-05-15 19:56:20 -03001735#endif
1736 chacha20_setkey_wrap,
1737 chacha20_setkey_wrap,
1738 chacha20_ctx_alloc,
1739 chacha20_ctx_free
1740};
1741static const mbedtls_cipher_info_t chacha20_info = {
1742 MBEDTLS_CIPHER_CHACHA20,
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001743 MBEDTLS_MODE_STREAM,
Daniel Kingbd920622016-05-15 19:56:20 -03001744 256,
1745 "CHACHA20",
1746 12,
1747 0,
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001748 1,
Daniel Kingbd920622016-05-15 19:56:20 -03001749 &chacha20_base_info
1750};
1751#endif /* MBEDTLS_CHACHA20_C */
1752
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001753#if defined(MBEDTLS_CHACHAPOLY_C)
Daniel King8fe47012016-05-17 20:33:28 -03001754
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001755static int chachapoly_setkey_wrap( void *ctx,
1756 const unsigned char *key,
1757 unsigned int key_bitlen )
Daniel King8fe47012016-05-17 20:33:28 -03001758{
1759 if( key_bitlen != 256U )
1760 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1761
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001762 if ( 0 != mbedtls_chachapoly_setkey( (mbedtls_chachapoly_context*)ctx, key ) )
Daniel King8fe47012016-05-17 20:33:28 -03001763 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1764
1765 return( 0 );
1766}
1767
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001768static void * chachapoly_ctx_alloc( void )
Daniel King8fe47012016-05-17 20:33:28 -03001769{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001770 mbedtls_chachapoly_context *ctx;
1771 ctx = mbedtls_calloc( 1, sizeof( mbedtls_chachapoly_context ) );
Daniel King8fe47012016-05-17 20:33:28 -03001772
1773 if( ctx == NULL )
1774 return( NULL );
1775
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001776 mbedtls_chachapoly_init( ctx );
Daniel King8fe47012016-05-17 20:33:28 -03001777
1778 return( ctx );
1779}
1780
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001781static void chachapoly_ctx_free( void *ctx )
Daniel King8fe47012016-05-17 20:33:28 -03001782{
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001783 mbedtls_chachapoly_free( (mbedtls_chachapoly_context *) ctx );
Daniel King8fe47012016-05-17 20:33:28 -03001784 mbedtls_free( ctx );
1785}
1786
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001787static const mbedtls_cipher_base_t chachapoly_base_info = {
Daniel King8fe47012016-05-17 20:33:28 -03001788 MBEDTLS_CIPHER_ID_CHACHA20,
1789 NULL,
1790#if defined(MBEDTLS_CIPHER_MODE_CBC)
1791 NULL,
1792#endif
1793#if defined(MBEDTLS_CIPHER_MODE_CFB)
1794 NULL,
1795#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02001796#if defined(MBEDTLS_CIPHER_MODE_OFB)
1797 NULL,
1798#endif
Daniel King8fe47012016-05-17 20:33:28 -03001799#if defined(MBEDTLS_CIPHER_MODE_CTR)
1800 NULL,
1801#endif
Manuel Pégourié-Gonnarda18034a2018-06-19 11:30:32 +02001802#if defined(MBEDTLS_CIPHER_MODE_XTS)
1803 NULL,
1804#endif
Daniel King8fe47012016-05-17 20:33:28 -03001805#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1806 NULL,
1807#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001808 chachapoly_setkey_wrap,
1809 chachapoly_setkey_wrap,
1810 chachapoly_ctx_alloc,
1811 chachapoly_ctx_free
Daniel King8fe47012016-05-17 20:33:28 -03001812};
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001813static const mbedtls_cipher_info_t chachapoly_info = {
Daniel King8fe47012016-05-17 20:33:28 -03001814 MBEDTLS_CIPHER_CHACHA20_POLY1305,
Manuel Pégourié-Gonnardf57bf8b2018-06-18 11:14:09 +02001815 MBEDTLS_MODE_CHACHAPOLY,
Daniel King8fe47012016-05-17 20:33:28 -03001816 256,
1817 "CHACHA20-POLY1305",
1818 12,
1819 0,
Manuel Pégourié-Gonnard32902e62018-05-10 12:30:19 +02001820 1,
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001821 &chachapoly_base_info
Daniel King8fe47012016-05-17 20:33:28 -03001822};
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02001823#endif /* MBEDTLS_CHACHAPOLY_C */
Daniel King8fe47012016-05-17 20:33:28 -03001824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001826static int null_crypt_stream( void *ctx, size_t length,
1827 const unsigned char *input,
1828 unsigned char *output )
1829{
1830 ((void) ctx);
1831 memmove( output, input, length );
1832 return( 0 );
1833}
1834
1835static int null_setkey( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001836 unsigned int key_bitlen )
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001837{
1838 ((void) ctx);
1839 ((void) key);
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001840 ((void) key_bitlen);
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001841
1842 return( 0 );
1843}
1844
Paul Bakkerfab5c822012-02-06 16:45:10 +00001845static void * null_ctx_alloc( void )
1846{
Manuel Pégourié-Gonnard86bbc7f2014-07-12 02:14:41 +02001847 return( (void *) 1 );
Paul Bakkerfab5c822012-02-06 16:45:10 +00001848}
1849
Paul Bakkerfab5c822012-02-06 16:45:10 +00001850static void null_ctx_free( void *ctx )
1851{
1852 ((void) ctx);
1853}
1854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855static const mbedtls_cipher_base_t null_base_info = {
1856 MBEDTLS_CIPHER_ID_NULL,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001857 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerfab5c822012-02-06 16:45:10 +00001859 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001860#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkerfab5c822012-02-06 16:45:10 +00001862 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001863#endif
Simon Butcher4844bf22018-06-11 15:21:05 +01001864#if defined(MBEDTLS_CIPHER_MODE_OFB)
1865 NULL,
1866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02001868 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001869#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01001870#if defined(MBEDTLS_CIPHER_MODE_XTS)
1871 NULL,
1872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001874 null_crypt_stream,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001875#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001876 null_setkey,
1877 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001878 null_ctx_alloc,
1879 null_ctx_free
1880};
1881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882static const mbedtls_cipher_info_t null_cipher_info = {
1883 MBEDTLS_CIPHER_NULL,
1884 MBEDTLS_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001885 0,
1886 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +01001887 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001888 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001889 1,
1890 &null_base_info
1891};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
Paul Bakkerfab5c822012-02-06 16:45:10 +00001893
Jack Lloydffdf2882019-03-07 17:00:32 -05001894#if defined(MBEDTLS_NIST_KW_C)
1895static void *kw_ctx_alloc( void )
1896{
1897 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_nist_kw_context ) );
1898
1899 if( ctx != NULL )
1900 mbedtls_nist_kw_init( (mbedtls_nist_kw_context *) ctx );
1901
1902 return( ctx );
1903}
1904
1905static void kw_ctx_free( void *ctx )
1906{
1907 mbedtls_nist_kw_free( ctx );
1908 mbedtls_free( ctx );
1909}
1910
1911static int kw_aes_setkey_wrap( void *ctx, const unsigned char *key,
1912 unsigned int key_bitlen )
1913{
Jack Lloyd5f289992019-04-02 10:07:28 -07001914 return mbedtls_nist_kw_setkey( (mbedtls_nist_kw_context *) ctx,
1915 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 1 );
Jack Lloydffdf2882019-03-07 17:00:32 -05001916}
1917
1918static int kw_aes_setkey_unwrap( void *ctx, const unsigned char *key,
1919 unsigned int key_bitlen )
1920{
Jack Lloyd5f289992019-04-02 10:07:28 -07001921 return mbedtls_nist_kw_setkey( (mbedtls_nist_kw_context *) ctx,
1922 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 0 );
Jack Lloydffdf2882019-03-07 17:00:32 -05001923}
1924
1925static const mbedtls_cipher_base_t kw_aes_info = {
1926 MBEDTLS_CIPHER_ID_AES,
1927 NULL,
1928#if defined(MBEDTLS_CIPHER_MODE_CBC)
1929 NULL,
1930#endif
1931#if defined(MBEDTLS_CIPHER_MODE_CFB)
1932 NULL,
1933#endif
1934#if defined(MBEDTLS_CIPHER_MODE_OFB)
1935 NULL,
1936#endif
1937#if defined(MBEDTLS_CIPHER_MODE_CTR)
1938 NULL,
1939#endif
1940#if defined(MBEDTLS_CIPHER_MODE_XTS)
1941 NULL,
1942#endif
1943#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1944 NULL,
1945#endif
1946 kw_aes_setkey_wrap,
1947 kw_aes_setkey_unwrap,
1948 kw_ctx_alloc,
1949 kw_ctx_free,
1950};
1951
1952static const mbedtls_cipher_info_t aes_128_nist_kw_info = {
1953 MBEDTLS_CIPHER_AES_128_KW,
1954 MBEDTLS_MODE_KW,
1955 128,
1956 "AES-128-KW",
1957 0,
1958 0,
1959 16,
1960 &kw_aes_info
1961};
1962
1963static const mbedtls_cipher_info_t aes_192_nist_kw_info = {
1964 MBEDTLS_CIPHER_AES_192_KW,
1965 MBEDTLS_MODE_KW,
1966 192,
1967 "AES-192-KW",
1968 0,
1969 0,
1970 16,
1971 &kw_aes_info
1972};
1973
1974static const mbedtls_cipher_info_t aes_256_nist_kw_info = {
1975 MBEDTLS_CIPHER_AES_256_KW,
1976 MBEDTLS_MODE_KW,
1977 256,
1978 "AES-256-KW",
1979 0,
1980 0,
1981 16,
1982 &kw_aes_info
1983};
1984
1985static const mbedtls_cipher_info_t aes_128_nist_kwp_info = {
1986 MBEDTLS_CIPHER_AES_128_KWP,
1987 MBEDTLS_MODE_KWP,
1988 128,
1989 "AES-128-KWP",
1990 0,
1991 0,
1992 16,
1993 &kw_aes_info
1994};
1995
1996static const mbedtls_cipher_info_t aes_192_nist_kwp_info = {
1997 MBEDTLS_CIPHER_AES_192_KWP,
1998 MBEDTLS_MODE_KWP,
1999 192,
2000 "AES-192-KWP",
2001 0,
2002 0,
2003 16,
2004 &kw_aes_info
2005};
2006
2007static const mbedtls_cipher_info_t aes_256_nist_kwp_info = {
2008 MBEDTLS_CIPHER_AES_256_KWP,
2009 MBEDTLS_MODE_KWP,
2010 256,
2011 "AES-256-KWP",
2012 0,
2013 0,
2014 16,
2015 &kw_aes_info
2016};
2017#endif /* MBEDTLS_NIST_KW_C */
2018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021#if defined(MBEDTLS_AES_C)
2022 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
2023 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
2024 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
2025#if defined(MBEDTLS_CIPHER_MODE_CBC)
2026 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
2027 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
2028 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002029#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030#if defined(MBEDTLS_CIPHER_MODE_CFB)
2031 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
2032 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
2033 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002034#endif
Simon Butcher8c0fd1e2018-04-22 22:58:07 +01002035#if defined(MBEDTLS_CIPHER_MODE_OFB)
2036 { MBEDTLS_CIPHER_AES_128_OFB, &aes_128_ofb_info },
2037 { MBEDTLS_CIPHER_AES_192_OFB, &aes_192_ofb_info },
2038 { MBEDTLS_CIPHER_AES_256_OFB, &aes_256_ofb_info },
2039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040#if defined(MBEDTLS_CIPHER_MODE_CTR)
2041 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
2042 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
2043 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002044#endif
Jaeden Ameroc6539902018-04-30 17:17:41 +01002045#if defined(MBEDTLS_CIPHER_MODE_XTS)
2046 { MBEDTLS_CIPHER_AES_128_XTS, &aes_128_xts_info },
2047 { MBEDTLS_CIPHER_AES_256_XTS, &aes_256_xts_info },
2048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049#if defined(MBEDTLS_GCM_C)
2050 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
2051 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
2052 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054#if defined(MBEDTLS_CCM_C)
2055 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
2056 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
2057 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02002058#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059#endif /* MBEDTLS_AES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061#if defined(MBEDTLS_CAMELLIA_C)
2062 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
2063 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
2064 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
2065#if defined(MBEDTLS_CIPHER_MODE_CBC)
2066 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
2067 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
2068 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070#if defined(MBEDTLS_CIPHER_MODE_CFB)
2071 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
2072 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
2073 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075#if defined(MBEDTLS_CIPHER_MODE_CTR)
2076 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
2077 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
2078 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002079#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080#if defined(MBEDTLS_GCM_C)
2081 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
2082 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
2083 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +02002084#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#if defined(MBEDTLS_CCM_C)
2086 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
2087 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
2088 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02002089#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090#endif /* MBEDTLS_CAMELLIA_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002091
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002092#if defined(MBEDTLS_ARIA_C)
2093 { MBEDTLS_CIPHER_ARIA_128_ECB, &aria_128_ecb_info },
2094 { MBEDTLS_CIPHER_ARIA_192_ECB, &aria_192_ecb_info },
2095 { MBEDTLS_CIPHER_ARIA_256_ECB, &aria_256_ecb_info },
2096#if defined(MBEDTLS_CIPHER_MODE_CBC)
2097 { MBEDTLS_CIPHER_ARIA_128_CBC, &aria_128_cbc_info },
2098 { MBEDTLS_CIPHER_ARIA_192_CBC, &aria_192_cbc_info },
2099 { MBEDTLS_CIPHER_ARIA_256_CBC, &aria_256_cbc_info },
2100#endif
2101#if defined(MBEDTLS_CIPHER_MODE_CFB)
2102 { MBEDTLS_CIPHER_ARIA_128_CFB128, &aria_128_cfb128_info },
2103 { MBEDTLS_CIPHER_ARIA_192_CFB128, &aria_192_cfb128_info },
2104 { MBEDTLS_CIPHER_ARIA_256_CFB128, &aria_256_cfb128_info },
2105#endif
2106#if defined(MBEDTLS_CIPHER_MODE_CTR)
2107 { MBEDTLS_CIPHER_ARIA_128_CTR, &aria_128_ctr_info },
2108 { MBEDTLS_CIPHER_ARIA_192_CTR, &aria_192_ctr_info },
2109 { MBEDTLS_CIPHER_ARIA_256_CTR, &aria_256_ctr_info },
2110#endif
2111#if defined(MBEDTLS_GCM_C)
2112 { MBEDTLS_CIPHER_ARIA_128_GCM, &aria_128_gcm_info },
2113 { MBEDTLS_CIPHER_ARIA_192_GCM, &aria_192_gcm_info },
2114 { MBEDTLS_CIPHER_ARIA_256_GCM, &aria_256_gcm_info },
2115#endif
2116#if defined(MBEDTLS_CCM_C)
2117 { MBEDTLS_CIPHER_ARIA_128_CCM, &aria_128_ccm_info },
2118 { MBEDTLS_CIPHER_ARIA_192_CCM, &aria_192_ccm_info },
2119 { MBEDTLS_CIPHER_ARIA_256_CCM, &aria_256_ccm_info },
2120#endif
2121#endif /* MBEDTLS_ARIA_C */
2122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123#if defined(MBEDTLS_DES_C)
2124 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
2125 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
2126 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
2127#if defined(MBEDTLS_CIPHER_MODE_CBC)
2128 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
2129 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
2130 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002131#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#endif /* MBEDTLS_DES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002133
Daniel Kingbd920622016-05-15 19:56:20 -03002134#if defined(MBEDTLS_CHACHA20_C)
2135 { MBEDTLS_CIPHER_CHACHA20, &chacha20_info },
2136#endif
2137
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +02002138#if defined(MBEDTLS_CHACHAPOLY_C)
2139 { MBEDTLS_CIPHER_CHACHA20_POLY1305, &chachapoly_info },
Daniel King8fe47012016-05-17 20:33:28 -03002140#endif
2141
Jack Lloydffdf2882019-03-07 17:00:32 -05002142#if defined(MBEDTLS_NIST_KW_C)
2143 { MBEDTLS_CIPHER_AES_128_KW, &aes_128_nist_kw_info },
2144 { MBEDTLS_CIPHER_AES_192_KW, &aes_192_nist_kw_info },
2145 { MBEDTLS_CIPHER_AES_256_KW, &aes_256_nist_kw_info },
2146 { MBEDTLS_CIPHER_AES_128_KWP, &aes_128_nist_kwp_info },
2147 { MBEDTLS_CIPHER_AES_192_KWP, &aes_192_nist_kwp_info },
2148 { MBEDTLS_CIPHER_AES_256_KWP, &aes_256_nist_kwp_info },
2149#endif
2150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2152 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
2153#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002155 { MBEDTLS_CIPHER_NONE, NULL }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002156};
2157
Hanno Beckerc3d25b32018-11-08 16:01:22 +00002158#define NUM_CIPHERS ( sizeof(mbedtls_cipher_definitions) / \
2159 sizeof(mbedtls_cipher_definitions[0]) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160int mbedtls_cipher_supported[NUM_CIPHERS];
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02002161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162#endif /* MBEDTLS_CIPHER_C */