blob: 64ce901736582e763189c641eb011bbf7c0b8794 [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úti44bfbe32020-08-19 16:54:51 +02008 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02009 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10 *
11 * This file is provided under the Apache License 2.0, or the
12 * GNU General Public License v2.0 or later.
13 *
14 * **********
15 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020016 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakker8123e9d2011-01-06 15:37:30 +000028 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020029 * **********
30 *
31 * **********
32 * GNU General Public License v2.0 or later:
33 *
34 * This program is free software; you can redistribute it and/or modify
35 * it under the terms of the GNU General Public License as published by
36 * the Free Software Foundation; either version 2 of the License, or
37 * (at your option) any later version.
38 *
39 * This program is distributed in the hope that it will be useful,
40 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 * GNU General Public License for more details.
43 *
44 * You should have received a copy of the GNU General Public License along
45 * with this program; if not, write to the Free Software Foundation, Inc.,
46 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 *
48 * **********
Paul Bakker8123e9d2011-01-06 15:37:30 +000049 */
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020055#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_CIPHER_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +000058
Manuel Pégourié-Gonnard50518f42015-05-26 11:04:15 +020059#include "mbedtls/cipher_internal.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_AES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/aes.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000063#endif
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/arc4.h"
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +020067#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_CAMELLIA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000070#include "mbedtls/camellia.h"
Paul Bakkerf6543712012-03-05 14:01:29 +000071#endif
72
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_DES_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/des.h"
Paul Bakker02f61692012-03-15 10:54:25 +000075#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_BLOWFISH_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/blowfish.h"
Paul Bakker6132d0a2012-07-04 17:10:40 +000079#endif
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +020083#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000086#include "mbedtls/ccm.h"
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +020087#endif
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnard0c851ee2015-02-10 12:47:52 +000090#include <string.h>
91#endif
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000094#include "mbedtls/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020095#else
Rich Evans00ab4702015-02-06 13:43:58 +000096#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020097#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#define mbedtls_free free
Paul Bakker6e339b52013-07-03 13:37:05 +020099#endif
100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200102/* shared by all GCM ciphers */
103static void *gcm_ctx_alloc( void )
104{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +0200105 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
106
107 if( ctx != NULL )
108 mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
109
110 return( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200111}
112
113static void gcm_ctx_free( void *ctx )
114{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_gcm_free( ctx );
116 mbedtls_free( ctx );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200121/* shared by all CCM ciphers */
122static void *ccm_ctx_alloc( void )
123{
Manuel Pégourié-Gonnard96fb6852015-06-23 11:39:01 +0200124 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
125
126 if( ctx != NULL )
127 mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
128
129 return( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200130}
131
132static void ccm_ctx_free( void *ctx )
133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_ccm_free( ctx );
135 mbedtls_free( ctx );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200136}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_AES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200142 const unsigned char *input, unsigned char *output )
143{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200145}
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_CIPHER_MODE_CBC)
148static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000149 unsigned char *iv, const unsigned char *input, unsigned char *output )
150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200152 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_CIPHER_MODE_CFB)
157static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200158 size_t length, size_t *iv_off, unsigned char *iv,
159 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000160{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200162 input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200167static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
168 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000169 const unsigned char *input, unsigned char *output )
170{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
Paul Bakker343a8702011-06-09 14:27:58 +0000172 stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000175
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200176static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200177 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000178{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200179 return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000180}
181
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200182static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200183 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000184{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200185 return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000186}
187
188static void * aes_ctx_alloc( void )
189{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200190 mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200191
192 if( aes == NULL )
193 return( NULL );
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_aes_init( aes );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200196
197 return( aes );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000198}
199
200static void aes_ctx_free( void *ctx )
201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_aes_free( (mbedtls_aes_context *) ctx );
203 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000204}
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206static const mbedtls_cipher_base_t aes_info = {
207 MBEDTLS_CIPHER_ID_AES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200208 aes_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000210 aes_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100211#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000213 aes_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100214#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000216 aes_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100217#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200219 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100220#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000221 aes_setkey_enc_wrap,
222 aes_setkey_dec_wrap,
223 aes_ctx_alloc,
224 aes_ctx_free
225};
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227static const mbedtls_cipher_info_t aes_128_ecb_info = {
228 MBEDTLS_CIPHER_AES_128_ECB,
229 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200230 128,
231 "AES-128-ECB",
Ron Eldorefba4b02017-09-25 18:22:32 +0300232 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200233 0,
234 16,
235 &aes_info
236};
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238static const mbedtls_cipher_info_t aes_192_ecb_info = {
239 MBEDTLS_CIPHER_AES_192_ECB,
240 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200241 192,
242 "AES-192-ECB",
Ron Eldorefba4b02017-09-25 18:22:32 +0300243 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200244 0,
245 16,
246 &aes_info
247};
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249static const mbedtls_cipher_info_t aes_256_ecb_info = {
250 MBEDTLS_CIPHER_AES_256_ECB,
251 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200252 256,
253 "AES-256-ECB",
Ron Eldorefba4b02017-09-25 18:22:32 +0300254 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200255 0,
256 16,
257 &aes_info
258};
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_CIPHER_MODE_CBC)
261static const mbedtls_cipher_info_t aes_128_cbc_info = {
262 MBEDTLS_CIPHER_AES_128_CBC,
263 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000264 128,
265 "AES-128-CBC",
266 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200267 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000268 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000269 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000270};
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272static const mbedtls_cipher_info_t aes_192_cbc_info = {
273 MBEDTLS_CIPHER_AES_192_CBC,
274 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000275 192,
276 "AES-192-CBC",
277 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200278 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000279 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000280 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000281};
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283static const mbedtls_cipher_info_t aes_256_cbc_info = {
284 MBEDTLS_CIPHER_AES_256_CBC,
285 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000286 256,
287 "AES-256-CBC",
288 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200289 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000290 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000291 &aes_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000292};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_CIPHER_MODE_CFB)
296static const mbedtls_cipher_info_t aes_128_cfb128_info = {
297 MBEDTLS_CIPHER_AES_128_CFB128,
298 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000299 128,
300 "AES-128-CFB128",
301 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200302 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000303 16,
304 &aes_info
305};
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307static const mbedtls_cipher_info_t aes_192_cfb128_info = {
308 MBEDTLS_CIPHER_AES_192_CFB128,
309 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000310 192,
311 "AES-192-CFB128",
312 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200313 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000314 16,
315 &aes_info
316};
317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318static const mbedtls_cipher_info_t aes_256_cfb128_info = {
319 MBEDTLS_CIPHER_AES_256_CFB128,
320 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000321 256,
322 "AES-256-CFB128",
323 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200324 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000325 16,
326 &aes_info
327};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_CIPHER_MODE_CTR)
331static const mbedtls_cipher_info_t aes_128_ctr_info = {
332 MBEDTLS_CIPHER_AES_128_CTR,
333 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000334 128,
335 "AES-128-CTR",
336 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200337 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000338 16,
339 &aes_info
340};
341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342static const mbedtls_cipher_info_t aes_192_ctr_info = {
343 MBEDTLS_CIPHER_AES_192_CTR,
344 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000345 192,
346 "AES-192-CTR",
347 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200348 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000349 16,
350 &aes_info
351};
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353static const mbedtls_cipher_info_t aes_256_ctr_info = {
354 MBEDTLS_CIPHER_AES_256_CTR,
355 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000356 256,
357 "AES-256-CTR",
358 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200359 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000360 16,
361 &aes_info
362};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200366static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200367 unsigned int key_bitlen )
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200368{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200369 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200370 key, key_bitlen );
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200371}
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373static const mbedtls_cipher_base_t gcm_aes_info = {
374 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200375 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200377 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100378#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200380 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200383 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Paul Bakker5e0efa72013-09-08 23:04:04 +0200386 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100387#endif
Paul Bakker43aff2a2013-09-09 00:10:27 +0200388 gcm_aes_setkey_wrap,
389 gcm_aes_setkey_wrap,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200390 gcm_ctx_alloc,
391 gcm_ctx_free,
392};
393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394static const mbedtls_cipher_info_t aes_128_gcm_info = {
395 MBEDTLS_CIPHER_AES_128_GCM,
396 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100397 128,
398 "AES-128-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200399 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100401 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200402 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100403};
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405static const mbedtls_cipher_info_t aes_192_gcm_info = {
406 MBEDTLS_CIPHER_AES_192_GCM,
407 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200408 192,
409 "AES-192-GCM",
410 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard83f3fc02013-09-04 12:07:24 +0200412 16,
413 &gcm_aes_info
414};
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416static const mbedtls_cipher_info_t aes_256_gcm_info = {
417 MBEDTLS_CIPHER_AES_256_GCM,
418 MBEDTLS_MODE_GCM,
Paul Bakker68884e32013-01-07 18:20:04 +0100419 256,
420 "AES-256-GCM",
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200421 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Paul Bakker68884e32013-01-07 18:20:04 +0100423 16,
Manuel Pégourié-Gonnard07f8fa52013-08-30 18:34:08 +0200424 &gcm_aes_info
Paul Bakker68884e32013-01-07 18:20:04 +0100425};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#endif /* MBEDTLS_GCM_C */
Paul Bakker68884e32013-01-07 18:20:04 +0100427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200429static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200430 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200431{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200432 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200433 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200434}
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436static const mbedtls_cipher_base_t ccm_aes_info = {
437 MBEDTLS_CIPHER_ID_AES,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200438 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200440 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100441#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200443 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100444#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200446 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100447#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200449 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100450#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200451 ccm_aes_setkey_wrap,
452 ccm_aes_setkey_wrap,
453 ccm_ctx_alloc,
454 ccm_ctx_free,
455};
456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457static const mbedtls_cipher_info_t aes_128_ccm_info = {
458 MBEDTLS_CIPHER_AES_128_CCM,
459 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200460 128,
461 "AES-128-CCM",
462 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200464 16,
465 &ccm_aes_info
466};
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468static const mbedtls_cipher_info_t aes_192_ccm_info = {
469 MBEDTLS_CIPHER_AES_192_CCM,
470 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200471 192,
472 "AES-192-CCM",
473 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200475 16,
476 &ccm_aes_info
477};
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479static const mbedtls_cipher_info_t aes_256_ccm_info = {
480 MBEDTLS_CIPHER_AES_256_CCM,
481 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200482 256,
483 "AES-256-CCM",
484 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200486 16,
487 &ccm_aes_info
488};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#endif /* MBEDTLS_AES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#if defined(MBEDTLS_CAMELLIA_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200496 const unsigned char *input, unsigned char *output )
497{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200499 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200500}
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_CIPHER_MODE_CBC)
503static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200504 size_t length, unsigned char *iv,
505 const unsigned char *input, unsigned char *output )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200508 input, output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_CIPHER_MODE_CFB)
513static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200514 size_t length, size_t *iv_off, unsigned char *iv,
515 const unsigned char *input, unsigned char *output )
Paul Bakker343a8702011-06-09 14:27:58 +0000516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200518 iv_off, iv, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200523static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
524 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker343a8702011-06-09 14:27:58 +0000525 const unsigned char *input, unsigned char *output )
526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200528 nonce_counter, stream_block, input, output );
Paul Bakker343a8702011-06-09 14:27:58 +0000529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000531
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200532static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200533 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200535 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000536}
537
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200538static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200539 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000540{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200541 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000542}
543
544static void * camellia_ctx_alloc( void )
545{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_camellia_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200547 ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200548
549 if( ctx == NULL )
550 return( NULL );
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_camellia_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200553
554 return( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000555}
556
557static void camellia_ctx_free( void *ctx )
558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
560 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000561}
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563static const mbedtls_cipher_base_t camellia_info = {
564 MBEDTLS_CIPHER_ID_CAMELLIA,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200565 camellia_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +0000567 camellia_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100568#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker343a8702011-06-09 14:27:58 +0000570 camellia_crypt_cfb128_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker343a8702011-06-09 14:27:58 +0000573 camellia_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100574#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200576 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100577#endif
Paul Bakker343a8702011-06-09 14:27:58 +0000578 camellia_setkey_enc_wrap,
579 camellia_setkey_dec_wrap,
580 camellia_ctx_alloc,
581 camellia_ctx_free
582};
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584static const mbedtls_cipher_info_t camellia_128_ecb_info = {
585 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
586 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200587 128,
588 "CAMELLIA-128-ECB",
Bence Szépkútib4756c22020-10-29 10:22:35 +0100589 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200590 0,
591 16,
592 &camellia_info
593};
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595static const mbedtls_cipher_info_t camellia_192_ecb_info = {
596 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
597 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200598 192,
599 "CAMELLIA-192-ECB",
Bence Szépkútib4756c22020-10-29 10:22:35 +0100600 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200601 0,
602 16,
603 &camellia_info
604};
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606static const mbedtls_cipher_info_t camellia_256_ecb_info = {
607 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
608 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200609 256,
610 "CAMELLIA-256-ECB",
Bence Szépkútib4756c22020-10-29 10:22:35 +0100611 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200612 0,
613 16,
614 &camellia_info
615};
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_CIPHER_MODE_CBC)
618static const mbedtls_cipher_info_t camellia_128_cbc_info = {
619 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
620 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000621 128,
622 "CAMELLIA-128-CBC",
623 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200624 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000625 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000626 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000627};
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629static const mbedtls_cipher_info_t camellia_192_cbc_info = {
630 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
631 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000632 192,
633 "CAMELLIA-192-CBC",
634 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200635 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000636 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000637 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000638};
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640static const mbedtls_cipher_info_t camellia_256_cbc_info = {
641 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
642 MBEDTLS_MODE_CBC,
Paul Bakker23986e52011-04-24 08:57:21 +0000643 256,
644 "CAMELLIA-256-CBC",
645 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200646 0,
Paul Bakker23986e52011-04-24 08:57:21 +0000647 16,
Paul Bakker343a8702011-06-09 14:27:58 +0000648 &camellia_info
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +0000651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_CIPHER_MODE_CFB)
653static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
654 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
655 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000656 128,
657 "CAMELLIA-128-CFB128",
658 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200659 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000660 16,
661 &camellia_info
662};
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
665 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
666 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000667 192,
668 "CAMELLIA-192-CFB128",
669 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200670 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000671 16,
672 &camellia_info
673};
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
676 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
677 MBEDTLS_MODE_CFB,
Paul Bakker343a8702011-06-09 14:27:58 +0000678 256,
679 "CAMELLIA-256-CFB128",
680 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200681 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000682 16,
683 &camellia_info
684};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker343a8702011-06-09 14:27:58 +0000686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#if defined(MBEDTLS_CIPHER_MODE_CTR)
688static const mbedtls_cipher_info_t camellia_128_ctr_info = {
689 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
690 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000691 128,
692 "CAMELLIA-128-CTR",
693 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200694 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000695 16,
696 &camellia_info
697};
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699static const mbedtls_cipher_info_t camellia_192_ctr_info = {
700 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
701 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000702 192,
703 "CAMELLIA-192-CTR",
704 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200705 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000706 16,
707 &camellia_info
708};
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710static const mbedtls_cipher_info_t camellia_256_ctr_info = {
711 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
712 MBEDTLS_MODE_CTR,
Paul Bakker343a8702011-06-09 14:27:58 +0000713 256,
714 "CAMELLIA-256-CTR",
715 16,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +0200716 0,
Paul Bakker343a8702011-06-09 14:27:58 +0000717 16,
718 &camellia_info
719};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker343a8702011-06-09 14:27:58 +0000721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722#if defined(MBEDTLS_GCM_C)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200723static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200724 unsigned int key_bitlen )
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200725{
Manuel Pégourié-Gonnardc34e8dd2015-04-28 21:42:17 +0200726 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200727 key, key_bitlen );
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200728}
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730static const mbedtls_cipher_base_t gcm_camellia_info = {
731 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200732 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200734 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100735#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200737 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100738#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200740 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100741#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200743 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100744#endif
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200745 gcm_camellia_setkey_wrap,
746 gcm_camellia_setkey_wrap,
747 gcm_ctx_alloc,
748 gcm_ctx_free,
749};
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751static const mbedtls_cipher_info_t camellia_128_gcm_info = {
752 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
753 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200754 128,
755 "CAMELLIA-128-GCM",
756 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200758 16,
759 &gcm_camellia_info
760};
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762static const mbedtls_cipher_info_t camellia_192_gcm_info = {
763 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
764 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200765 192,
766 "CAMELLIA-192-GCM",
767 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200769 16,
770 &gcm_camellia_info
771};
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773static const mbedtls_cipher_info_t camellia_256_gcm_info = {
774 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
775 MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200776 256,
777 "CAMELLIA-256-GCM",
778 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200780 16,
781 &gcm_camellia_info
782};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_GCM_C */
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +0200784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200786static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200787 unsigned int key_bitlen )
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200788{
Manuel Pégourié-Gonnard6963ff02015-04-28 18:02:54 +0200789 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200790 key, key_bitlen );
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200791}
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793static const mbedtls_cipher_base_t ccm_camellia_info = {
794 MBEDTLS_CIPHER_ID_CAMELLIA,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200795 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#if defined(MBEDTLS_CIPHER_MODE_CBC)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200797 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100798#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200800 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100801#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200803 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100804#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200806 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100807#endif
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200808 ccm_camellia_setkey_wrap,
809 ccm_camellia_setkey_wrap,
810 ccm_ctx_alloc,
811 ccm_ctx_free,
812};
813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814static const mbedtls_cipher_info_t camellia_128_ccm_info = {
815 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
816 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200817 128,
818 "CAMELLIA-128-CCM",
819 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200821 16,
822 &ccm_camellia_info
823};
824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825static const mbedtls_cipher_info_t camellia_192_ccm_info = {
826 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
827 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200828 192,
829 "CAMELLIA-192-CCM",
830 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200832 16,
833 &ccm_camellia_info
834};
835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836static const mbedtls_cipher_info_t camellia_256_ccm_info = {
837 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
838 MBEDTLS_MODE_CCM,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200839 256,
840 "CAMELLIA-256-CCM",
841 12,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200843 16,
844 &ccm_camellia_info
845};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#endif /* MBEDTLS_CCM_C */
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +0200847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#endif /* MBEDTLS_CAMELLIA_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#if defined(MBEDTLS_DES_C)
Paul Bakker8123e9d2011-01-06 15:37:30 +0000851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200853 const unsigned char *input, unsigned char *output )
854{
855 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200857}
858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200860 const unsigned char *input, unsigned char *output )
861{
862 ((void) operation);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200864}
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_CIPHER_MODE_CBC)
867static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000868 unsigned char *iv, const unsigned char *input, unsigned char *output )
869{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200871 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_CIPHER_MODE_CBC)
876static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
Paul Bakker8123e9d2011-01-06 15:37:30 +0000877 unsigned char *iv, const unsigned char *input, unsigned char *output )
878{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200880 output );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000881}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000883
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200884static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200885 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000886{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200887 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000890}
891
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200892static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200893 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000894{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200895 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000898}
899
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200900static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200901 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000902{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200903 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000906}
907
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200908static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200909 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000910{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200911 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000914}
915
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200916static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200917 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000918{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200919 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000922}
923
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200924static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200925 unsigned int key_bitlen )
Paul Bakker8123e9d2011-01-06 15:37:30 +0000926{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +0200927 ((void) key_bitlen);
Paul Bakkerd61e7d92011-01-18 16:17:47 +0000928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000930}
931
932static void * des_ctx_alloc( void )
933{
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200934 mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000935
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200936 if( des == NULL )
937 return( NULL );
938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_des_init( des );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200940
941 return( des );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000942}
943
944static void des_ctx_free( void *ctx )
945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_des_free( (mbedtls_des_context *) ctx );
947 mbedtls_free( ctx );
Paul Bakker34617722014-06-13 17:20:13 +0200948}
949
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200950static void * des3_ctx_alloc( void )
951{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 mbedtls_des3_context *des3;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200953 des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200954
955 if( des3 == NULL )
956 return( NULL );
957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_des3_init( des3 );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200959
960 return( des3 );
961}
962
Paul Bakker34617722014-06-13 17:20:13 +0200963static void des3_ctx_free( void *ctx )
964{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_des3_free( (mbedtls_des3_context *) ctx );
966 mbedtls_free( ctx );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000967}
968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969static const mbedtls_cipher_base_t des_info = {
970 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200971 des_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +0000973 des_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +0200976 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100977#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +0200979 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100980#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +0200982 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +0100983#endif
Paul Bakker23986e52011-04-24 08:57:21 +0000984 des_setkey_enc_wrap,
985 des_setkey_dec_wrap,
986 des_ctx_alloc,
987 des_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +0000988};
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990static const mbedtls_cipher_info_t des_ecb_info = {
991 MBEDTLS_CIPHER_DES_ECB,
992 MBEDTLS_MODE_ECB,
993 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200994 "DES-ECB",
Bence Szépkútib4756c22020-10-29 10:22:35 +0100995 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200996 0,
997 8,
998 &des_info
999};
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_CIPHER_MODE_CBC)
1002static const mbedtls_cipher_info_t des_cbc_info = {
1003 MBEDTLS_CIPHER_DES_CBC,
1004 MBEDTLS_MODE_CBC,
1005 MBEDTLS_KEY_LENGTH_DES,
Paul Bakker343a8702011-06-09 14:27:58 +00001006 "DES-CBC",
1007 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001008 0,
Paul Bakker343a8702011-06-09 14:27:58 +00001009 8,
1010 &des_info
1011};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014static const mbedtls_cipher_base_t des_ede_info = {
1015 MBEDTLS_CIPHER_ID_DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001016 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker23986e52011-04-24 08:57:21 +00001018 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001021 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001024 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001025#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001027 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001028#endif
Paul Bakker23986e52011-04-24 08:57:21 +00001029 des3_set2key_enc_wrap,
1030 des3_set2key_dec_wrap,
1031 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001032 des3_ctx_free
Paul Bakker8123e9d2011-01-06 15:37:30 +00001033};
1034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035static const mbedtls_cipher_info_t des_ede_ecb_info = {
1036 MBEDTLS_CIPHER_DES_EDE_ECB,
1037 MBEDTLS_MODE_ECB,
1038 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001039 "DES-EDE-ECB",
Bence Szépkútib4756c22020-10-29 10:22:35 +01001040 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001041 0,
1042 8,
1043 &des_ede_info
1044};
1045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046#if defined(MBEDTLS_CIPHER_MODE_CBC)
1047static const mbedtls_cipher_info_t des_ede_cbc_info = {
1048 MBEDTLS_CIPHER_DES_EDE_CBC,
1049 MBEDTLS_MODE_CBC,
1050 MBEDTLS_KEY_LENGTH_DES_EDE,
Paul Bakker343a8702011-06-09 14:27:58 +00001051 "DES-EDE-CBC",
Paul Bakker0e342352013-06-24 19:33:02 +02001052 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001053 0,
Paul Bakker0e342352013-06-24 19:33:02 +02001054 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001055 &des_ede_info
1056};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker343a8702011-06-09 14:27:58 +00001058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059static const mbedtls_cipher_base_t des_ede3_info = {
Manuel Pégourié-Gonnard9d515832015-06-02 10:00:04 +01001060 MBEDTLS_CIPHER_ID_3DES,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001061 des3_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker343a8702011-06-09 14:27:58 +00001063 des3_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_CIPHER_MODE_CFB)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001066 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_CIPHER_MODE_CTR)
Manuel Pégourié-Gonnardb9126162014-06-13 15:06:59 +02001069 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001072 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001073#endif
Paul Bakker343a8702011-06-09 14:27:58 +00001074 des3_set3key_enc_wrap,
1075 des3_set3key_dec_wrap,
1076 des3_ctx_alloc,
Paul Bakker34617722014-06-13 17:20:13 +02001077 des3_ctx_free
Paul Bakker343a8702011-06-09 14:27:58 +00001078};
1079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1081 MBEDTLS_CIPHER_DES_EDE3_ECB,
1082 MBEDTLS_MODE_ECB,
1083 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001084 "DES-EDE3-ECB",
Bence Szépkútib4756c22020-10-29 10:22:35 +01001085 0,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001086 0,
1087 8,
1088 &des_ede3_info
1089};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_CIPHER_MODE_CBC)
1091static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1092 MBEDTLS_CIPHER_DES_EDE3_CBC,
1093 MBEDTLS_MODE_CBC,
1094 MBEDTLS_KEY_LENGTH_DES_EDE3,
Paul Bakker23986e52011-04-24 08:57:21 +00001095 "DES-EDE3-CBC",
1096 8,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001097 0,
Paul Bakker23986e52011-04-24 08:57:21 +00001098 8,
Paul Bakker343a8702011-06-09 14:27:58 +00001099 &des_ede3_info
Paul Bakker8123e9d2011-01-06 15:37:30 +00001100};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#endif /* MBEDTLS_CIPHER_MODE_CBC */
1102#endif /* MBEDTLS_DES_C */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104#if defined(MBEDTLS_BLOWFISH_C)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001107 const unsigned char *input, unsigned char *output )
1108{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001110 output );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001111}
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#if defined(MBEDTLS_CIPHER_MODE_CBC)
1114static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001115 size_t length, unsigned char *iv, const unsigned char *input,
1116 unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001119 input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_CIPHER_MODE_CFB)
1124static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001125 size_t length, size_t *iv_off, unsigned char *iv,
1126 const unsigned char *input, unsigned char *output )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001129 iv_off, iv, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001134static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1135 unsigned char *nonce_counter, unsigned char *stream_block,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001136 const unsigned char *input, unsigned char *output )
1137{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001139 nonce_counter, stream_block, input, output );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001142
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001143static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001144 unsigned int key_bitlen )
Paul Bakker6132d0a2012-07-04 17:10:40 +00001145{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001146 return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001147}
1148
1149static void * blowfish_ctx_alloc( void )
1150{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 mbedtls_blowfish_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001152 ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001153
1154 if( ctx == NULL )
1155 return( NULL );
1156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_blowfish_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001158
1159 return( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001160}
1161
1162static void blowfish_ctx_free( void *ctx )
1163{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
1165 mbedtls_free( ctx );
Paul Bakker6132d0a2012-07-04 17:10:40 +00001166}
1167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168static const mbedtls_cipher_base_t blowfish_info = {
1169 MBEDTLS_CIPHER_ID_BLOWFISH,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001170 blowfish_crypt_ecb_wrap,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001172 blowfish_crypt_cbc_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001175 blowfish_crypt_cfb64_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker6132d0a2012-07-04 17:10:40 +00001178 blowfish_crypt_ctr_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001181 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001182#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001183 blowfish_setkey_wrap,
1184 blowfish_setkey_wrap,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001185 blowfish_ctx_alloc,
1186 blowfish_ctx_free
1187};
1188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189static const mbedtls_cipher_info_t blowfish_ecb_info = {
1190 MBEDTLS_CIPHER_BLOWFISH_ECB,
1191 MBEDTLS_MODE_ECB,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001192 128,
1193 "BLOWFISH-ECB",
Bence Szépkútib4756c22020-10-29 10:22:35 +01001194 0,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001196 8,
1197 &blowfish_info
1198};
1199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#if defined(MBEDTLS_CIPHER_MODE_CBC)
1201static const mbedtls_cipher_info_t blowfish_cbc_info = {
1202 MBEDTLS_CIPHER_BLOWFISH_CBC,
1203 MBEDTLS_MODE_CBC,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001204 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001205 "BLOWFISH-CBC",
1206 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001208 8,
1209 &blowfish_info
1210};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#if defined(MBEDTLS_CIPHER_MODE_CFB)
1214static const mbedtls_cipher_info_t blowfish_cfb64_info = {
1215 MBEDTLS_CIPHER_BLOWFISH_CFB64,
1216 MBEDTLS_MODE_CFB,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001217 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001218 "BLOWFISH-CFB64",
1219 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001221 8,
1222 &blowfish_info
1223};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#endif /* MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#if defined(MBEDTLS_CIPHER_MODE_CTR)
1227static const mbedtls_cipher_info_t blowfish_ctr_info = {
1228 MBEDTLS_CIPHER_BLOWFISH_CTR,
1229 MBEDTLS_MODE_CTR,
Paul Bakkerbfe671f2013-04-07 22:35:44 +02001230 128,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001231 "BLOWFISH-CTR",
1232 8,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
Paul Bakker6132d0a2012-07-04 17:10:40 +00001234 8,
1235 &blowfish_info
1236};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#endif /* MBEDTLS_CIPHER_MODE_CTR */
1238#endif /* MBEDTLS_BLOWFISH_C */
Paul Bakker6132d0a2012-07-04 17:10:40 +00001239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001241static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1242 const unsigned char *input,
1243 unsigned char *output )
Paul Bakker68884e32013-01-07 18:20:04 +01001244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
Paul Bakker68884e32013-01-07 18:20:04 +01001246}
1247
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001248static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001249 unsigned int key_bitlen )
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001250{
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001251 /* we get key_bitlen in bits, arc4 expects it in bytes */
1252 if( key_bitlen % 8 != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardce411252013-09-04 12:28:37 +02001254
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001255 mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001256 return( 0 );
1257}
1258
1259static void * arc4_ctx_alloc( void )
1260{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 mbedtls_arc4_context *ctx;
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001262 ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001263
1264 if( ctx == NULL )
1265 return( NULL );
1266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 mbedtls_arc4_init( ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +02001268
1269 return( ctx );
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001270}
Paul Bakker68884e32013-01-07 18:20:04 +01001271
1272static void arc4_ctx_free( void *ctx )
1273{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
1275 mbedtls_free( ctx );
Paul Bakker68884e32013-01-07 18:20:04 +01001276}
1277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278static const mbedtls_cipher_base_t arc4_base_info = {
1279 MBEDTLS_CIPHER_ID_ARC4,
Paul Bakker68884e32013-01-07 18:20:04 +01001280 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker68884e32013-01-07 18:20:04 +01001282 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001283#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker68884e32013-01-07 18:20:04 +01001285 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02001288 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001289#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001291 arc4_crypt_stream_wrap,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001292#endif
Manuel Pégourié-Gonnard37e230c2013-08-28 13:50:42 +02001293 arc4_setkey_wrap,
1294 arc4_setkey_wrap,
Paul Bakker68884e32013-01-07 18:20:04 +01001295 arc4_ctx_alloc,
1296 arc4_ctx_free
1297};
1298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299static const mbedtls_cipher_info_t arc4_128_info = {
1300 MBEDTLS_CIPHER_ARC4_128,
1301 MBEDTLS_MODE_STREAM,
Paul Bakker68884e32013-01-07 18:20:04 +01001302 128,
1303 "ARC4-128",
1304 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001305 0,
Paul Bakker68884e32013-01-07 18:20:04 +01001306 1,
1307 &arc4_base_info
1308};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#endif /* MBEDTLS_ARC4_C */
Paul Bakker68884e32013-01-07 18:20:04 +01001310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001312static int null_crypt_stream( void *ctx, size_t length,
1313 const unsigned char *input,
1314 unsigned char *output )
1315{
1316 ((void) ctx);
1317 memmove( output, input, length );
1318 return( 0 );
1319}
1320
1321static int null_setkey( void *ctx, const unsigned char *key,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001322 unsigned int key_bitlen )
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001323{
1324 ((void) ctx);
1325 ((void) key);
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001326 ((void) key_bitlen);
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001327
1328 return( 0 );
1329}
1330
Paul Bakkerfab5c822012-02-06 16:45:10 +00001331static void * null_ctx_alloc( void )
1332{
Manuel Pégourié-Gonnard86bbc7f2014-07-12 02:14:41 +02001333 return( (void *) 1 );
Paul Bakkerfab5c822012-02-06 16:45:10 +00001334}
1335
Paul Bakkerfab5c822012-02-06 16:45:10 +00001336static void null_ctx_free( void *ctx )
1337{
1338 ((void) ctx);
1339}
1340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341static const mbedtls_cipher_base_t null_base_info = {
1342 MBEDTLS_CIPHER_ID_NULL,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001343 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakkerfab5c822012-02-06 16:45:10 +00001345 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001346#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakkerfab5c822012-02-06 16:45:10 +00001348 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001349#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker5e0efa72013-09-08 23:04:04 +02001351 NULL,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001352#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#if defined(MBEDTLS_CIPHER_MODE_STREAM)
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001354 null_crypt_stream,
Manuel Pégourié-Gonnardb8ca7232014-12-02 10:09:10 +01001355#endif
Manuel Pégourié-Gonnardb5e85882013-08-28 16:36:14 +02001356 null_setkey,
1357 null_setkey,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001358 null_ctx_alloc,
1359 null_ctx_free
1360};
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362static const mbedtls_cipher_info_t null_cipher_info = {
1363 MBEDTLS_CIPHER_NULL,
1364 MBEDTLS_MODE_STREAM,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001365 0,
1366 "NULL",
Paul Bakker68884e32013-01-07 18:20:04 +01001367 0,
Manuel Pégourié-Gonnarda235b5b2013-09-03 13:25:52 +02001368 0,
Paul Bakkerfab5c822012-02-06 16:45:10 +00001369 1,
1370 &null_base_info
1371};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
Paul Bakkerfab5c822012-02-06 16:45:10 +00001373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_AES_C)
1377 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1378 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1379 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1380#if defined(MBEDTLS_CIPHER_MODE_CBC)
1381 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1382 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1383 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001384#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_CIPHER_MODE_CFB)
1386 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1387 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1388 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001389#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_CIPHER_MODE_CTR)
1391 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1392 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1393 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001394#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_GCM_C)
1396 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1397 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1398 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_CCM_C)
1401 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
1402 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
1403 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_AES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#if defined(MBEDTLS_ARC4_C)
1408 { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001409#endif
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_BLOWFISH_C)
1412 { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1413#if defined(MBEDTLS_CIPHER_MODE_CBC)
1414 { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001415#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#if defined(MBEDTLS_CIPHER_MODE_CFB)
1417 { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_CIPHER_MODE_CTR)
1420 { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#endif /* MBEDTLS_BLOWFISH_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#if defined(MBEDTLS_CAMELLIA_C)
1425 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1426 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
1427 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
1428#if defined(MBEDTLS_CIPHER_MODE_CBC)
1429 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1430 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1431 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001432#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_CIPHER_MODE_CFB)
1434 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1435 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1436 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001437#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#if defined(MBEDTLS_CIPHER_MODE_CTR)
1439 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1440 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1441 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001442#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_GCM_C)
1444 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1445 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1446 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
Manuel Pégourié-Gonnard87181d12013-10-24 14:02:40 +02001447#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_CCM_C)
1449 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
1450 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
1451 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
Manuel Pégourié-Gonnard41936952014-05-13 13:18:17 +02001452#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_CAMELLIA_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#if defined(MBEDTLS_DES_C)
1456 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
1457 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1458 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1459#if defined(MBEDTLS_CIPHER_MODE_CBC)
1460 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
1461 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1462 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001463#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#endif /* MBEDTLS_DES_C */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
1467 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
1468#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 { MBEDTLS_CIPHER_NONE, NULL }
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001471};
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
1474int mbedtls_cipher_supported[NUM_CIPHERS];
Manuel Pégourié-Gonnarddace82f2013-09-18 15:12:07 +02001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476#endif /* MBEDTLS_CIPHER_C */