blob: fb363145c5fe2e3b10fd146ce401376de9a85cdb [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/**
2 * \file cipher_wrap.c
3 *
4 * \brief Generic cipher wrapper for mbed TLS
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
Jerome Forissier79013242021-07-28 10:24:04 +02008 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +020010 *
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.
Jens Wiklander817466c2018-05-22 13:49:31 +020022 */
23
Jerome Forissier79013242021-07-28 10:24:04 +020024#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020025
Edison Ai12484fc2018-12-19 15:36:28 +080026#include <string.h>
27
Jens Wiklander817466c2018-05-22 13:49:31 +020028#if defined(MBEDTLS_CIPHER_C)
29
Jens Wiklander32b31802023-10-06 16:59:46 +020030#include "cipher_wrap.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020031#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020032
Jens Wiklander3d3b0592019-03-20 15:30:29 +010033#if defined(MBEDTLS_CHACHAPOLY_C)
34#include "mbedtls/chachapoly.h"
35#endif
36
Jens Wiklander817466c2018-05-22 13:49:31 +020037#if defined(MBEDTLS_AES_C)
38#include "mbedtls/aes.h"
39#endif
40
Jens Wiklander817466c2018-05-22 13:49:31 +020041#if defined(MBEDTLS_CAMELLIA_C)
42#include "mbedtls/camellia.h"
43#endif
44
Jens Wiklander3d3b0592019-03-20 15:30:29 +010045#if defined(MBEDTLS_ARIA_C)
46#include "mbedtls/aria.h"
47#endif
48
Jens Wiklander817466c2018-05-22 13:49:31 +020049#if defined(MBEDTLS_DES_C)
50#include "mbedtls/des.h"
51#endif
52
Jens Wiklander3d3b0592019-03-20 15:30:29 +010053#if defined(MBEDTLS_CHACHA20_C)
54#include "mbedtls/chacha20.h"
55#endif
56
Jens Wiklander817466c2018-05-22 13:49:31 +020057#if defined(MBEDTLS_GCM_C)
58#include "mbedtls/gcm.h"
59#endif
60
61#if defined(MBEDTLS_CCM_C)
62#include "mbedtls/ccm.h"
63#endif
64
Jerome Forissier11fa71b2020-04-20 17:17:56 +020065#if defined(MBEDTLS_NIST_KW_C)
66#include "mbedtls/nist_kw.h"
67#endif
68
Jens Wiklander817466c2018-05-22 13:49:31 +020069#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
70#include <string.h>
71#endif
72
Jens Wiklander817466c2018-05-22 13:49:31 +020073#include "mbedtls/platform.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020074
75#if defined(MBEDTLS_GCM_C)
76/* shared by all GCM ciphers */
Jens Wiklander32b31802023-10-06 16:59:46 +020077static void *gcm_ctx_alloc(void)
Jens Wiklander817466c2018-05-22 13:49:31 +020078{
Jens Wiklander32b31802023-10-06 16:59:46 +020079 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_gcm_context));
Jens Wiklander817466c2018-05-22 13:49:31 +020080
Jens Wiklander32b31802023-10-06 16:59:46 +020081 if (ctx != NULL) {
82 mbedtls_gcm_init((mbedtls_gcm_context *) ctx);
83 }
Jens Wiklander817466c2018-05-22 13:49:31 +020084
Jens Wiklander32b31802023-10-06 16:59:46 +020085 return ctx;
Jens Wiklander817466c2018-05-22 13:49:31 +020086}
87
Jens Wiklander32b31802023-10-06 16:59:46 +020088static void gcm_ctx_clone(void *dst, const void *src)
Edison Ai12484fc2018-12-19 15:36:28 +080089{
Jens Wiklander32b31802023-10-06 16:59:46 +020090 memcpy(dst, src, sizeof(mbedtls_gcm_context));
Edison Ai12484fc2018-12-19 15:36:28 +080091}
92
Jens Wiklander32b31802023-10-06 16:59:46 +020093static void gcm_ctx_free(void *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +020094{
Jens Wiklander32b31802023-10-06 16:59:46 +020095 mbedtls_gcm_free(ctx);
96 mbedtls_free(ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +020097}
98#endif /* MBEDTLS_GCM_C */
99
100#if defined(MBEDTLS_CCM_C)
101/* shared by all CCM ciphers */
Jens Wiklander32b31802023-10-06 16:59:46 +0200102static void *ccm_ctx_alloc(void)
Jens Wiklander817466c2018-05-22 13:49:31 +0200103{
Jens Wiklander32b31802023-10-06 16:59:46 +0200104 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ccm_context));
Jens Wiklander817466c2018-05-22 13:49:31 +0200105
Jens Wiklander32b31802023-10-06 16:59:46 +0200106 if (ctx != NULL) {
107 mbedtls_ccm_init((mbedtls_ccm_context *) ctx);
108 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200109
Jens Wiklander32b31802023-10-06 16:59:46 +0200110 return ctx;
Jens Wiklander817466c2018-05-22 13:49:31 +0200111}
112
Jens Wiklander32b31802023-10-06 16:59:46 +0200113static void ccm_ctx_clone(void *dst, const void *src)
Edison Ai12484fc2018-12-19 15:36:28 +0800114{
Jens Wiklander32b31802023-10-06 16:59:46 +0200115 memcpy(dst, src, sizeof(mbedtls_ccm_context));
Edison Ai12484fc2018-12-19 15:36:28 +0800116}
117
Jens Wiklander32b31802023-10-06 16:59:46 +0200118static void ccm_ctx_free(void *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200119{
Jens Wiklander32b31802023-10-06 16:59:46 +0200120 mbedtls_ccm_free(ctx);
121 mbedtls_free(ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +0200122}
123#endif /* MBEDTLS_CCM_C */
124
125#if defined(MBEDTLS_AES_C)
126
Jens Wiklander32b31802023-10-06 16:59:46 +0200127static int aes_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
128 const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +0200129{
Jens Wiklander32b31802023-10-06 16:59:46 +0200130 return mbedtls_aes_crypt_ecb((mbedtls_aes_context *) ctx, operation, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200131}
132
133#if defined(MBEDTLS_CIPHER_MODE_CBC)
Jens Wiklander32b31802023-10-06 16:59:46 +0200134static int aes_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
135 unsigned char *iv, const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +0200136{
Jens Wiklander32b31802023-10-06 16:59:46 +0200137 return mbedtls_aes_crypt_cbc((mbedtls_aes_context *) ctx, operation, length, iv, input,
138 output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200139}
140#endif /* MBEDTLS_CIPHER_MODE_CBC */
141
142#if defined(MBEDTLS_CIPHER_MODE_CFB)
Jens Wiklander32b31802023-10-06 16:59:46 +0200143static int aes_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
144 size_t length, size_t *iv_off, unsigned char *iv,
145 const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +0200146{
Jens Wiklander32b31802023-10-06 16:59:46 +0200147 return mbedtls_aes_crypt_cfb128((mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
148 input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200149}
150#endif /* MBEDTLS_CIPHER_MODE_CFB */
151
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100152#if defined(MBEDTLS_CIPHER_MODE_OFB)
Jens Wiklander32b31802023-10-06 16:59:46 +0200153static int aes_crypt_ofb_wrap(void *ctx, size_t length, size_t *iv_off,
154 unsigned char *iv, const unsigned char *input, unsigned char *output)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100155{
Jens Wiklander32b31802023-10-06 16:59:46 +0200156 return mbedtls_aes_crypt_ofb((mbedtls_aes_context *) ctx, length, iv_off,
157 iv, input, output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100158}
159#endif /* MBEDTLS_CIPHER_MODE_OFB */
160
Jens Wiklander817466c2018-05-22 13:49:31 +0200161#if defined(MBEDTLS_CIPHER_MODE_CTR)
Jens Wiklander32b31802023-10-06 16:59:46 +0200162static int aes_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
163 unsigned char *nonce_counter, unsigned char *stream_block,
164 const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +0200165{
Jens Wiklander32b31802023-10-06 16:59:46 +0200166 return mbedtls_aes_crypt_ctr((mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
167 stream_block, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200168}
169#endif /* MBEDTLS_CIPHER_MODE_CTR */
170
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100171#if defined(MBEDTLS_CIPHER_MODE_XTS)
Jens Wiklander32b31802023-10-06 16:59:46 +0200172static int aes_crypt_xts_wrap(void *ctx, mbedtls_operation_t operation,
173 size_t length,
174 const unsigned char data_unit[16],
175 const unsigned char *input,
176 unsigned char *output)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100177{
178 mbedtls_aes_xts_context *xts_ctx = ctx;
179 int mode;
180
Jens Wiklander32b31802023-10-06 16:59:46 +0200181 switch (operation) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100182 case MBEDTLS_ENCRYPT:
183 mode = MBEDTLS_AES_ENCRYPT;
184 break;
185 case MBEDTLS_DECRYPT:
186 mode = MBEDTLS_AES_DECRYPT;
187 break;
188 default:
189 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
190 }
191
Jens Wiklander32b31802023-10-06 16:59:46 +0200192 return mbedtls_aes_crypt_xts(xts_ctx, mode, length,
193 data_unit, input, output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100194}
195#endif /* MBEDTLS_CIPHER_MODE_XTS */
196
Jens Wiklander32b31802023-10-06 16:59:46 +0200197static int aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
198 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200199{
Jens Wiklander32b31802023-10-06 16:59:46 +0200200 return mbedtls_aes_setkey_dec((mbedtls_aes_context *) ctx, key, key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200201}
202
Jens Wiklander32b31802023-10-06 16:59:46 +0200203static int aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
204 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200205{
Jens Wiklander32b31802023-10-06 16:59:46 +0200206 return mbedtls_aes_setkey_enc((mbedtls_aes_context *) ctx, key, key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200207}
208
Jens Wiklander32b31802023-10-06 16:59:46 +0200209static void *aes_ctx_alloc(void)
Jens Wiklander817466c2018-05-22 13:49:31 +0200210{
Jens Wiklander32b31802023-10-06 16:59:46 +0200211 mbedtls_aes_context *aes = mbedtls_calloc(1, sizeof(mbedtls_aes_context));
Jens Wiklander817466c2018-05-22 13:49:31 +0200212
Jens Wiklander32b31802023-10-06 16:59:46 +0200213 if (aes == NULL) {
214 return NULL;
215 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200216
Jens Wiklander32b31802023-10-06 16:59:46 +0200217 mbedtls_aes_init(aes);
Jens Wiklander817466c2018-05-22 13:49:31 +0200218
Jens Wiklander32b31802023-10-06 16:59:46 +0200219 return aes;
Jens Wiklander817466c2018-05-22 13:49:31 +0200220}
221
Jens Wiklander32b31802023-10-06 16:59:46 +0200222static void aes_ctx_clone(void *dst, const void *src)
Edison Ai12484fc2018-12-19 15:36:28 +0800223{
Jens Wiklander32b31802023-10-06 16:59:46 +0200224 memcpy(dst, src, sizeof(mbedtls_aes_context));
Edison Ai12484fc2018-12-19 15:36:28 +0800225}
226
Jens Wiklander32b31802023-10-06 16:59:46 +0200227static void aes_ctx_free(void *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200228{
Jens Wiklander32b31802023-10-06 16:59:46 +0200229 mbedtls_aes_free((mbedtls_aes_context *) ctx);
230 mbedtls_free(ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +0200231}
232
233static const mbedtls_cipher_base_t aes_info = {
234 MBEDTLS_CIPHER_ID_AES,
235 aes_crypt_ecb_wrap,
236#if defined(MBEDTLS_CIPHER_MODE_CBC)
237 aes_crypt_cbc_wrap,
238#endif
239#if defined(MBEDTLS_CIPHER_MODE_CFB)
240 aes_crypt_cfb128_wrap,
241#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100242#if defined(MBEDTLS_CIPHER_MODE_OFB)
243 aes_crypt_ofb_wrap,
244#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200245#if defined(MBEDTLS_CIPHER_MODE_CTR)
246 aes_crypt_ctr_wrap,
247#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100248#if defined(MBEDTLS_CIPHER_MODE_XTS)
249 NULL,
250#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200251#if defined(MBEDTLS_CIPHER_MODE_STREAM)
252 NULL,
253#endif
254 aes_setkey_enc_wrap,
255 aes_setkey_dec_wrap,
256 aes_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +0800257 aes_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +0200258 aes_ctx_free
259};
260
261static const mbedtls_cipher_info_t aes_128_ecb_info = {
262 MBEDTLS_CIPHER_AES_128_ECB,
263 MBEDTLS_MODE_ECB,
264 128,
265 "AES-128-ECB",
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100266 0,
Jens Wiklander817466c2018-05-22 13:49:31 +0200267 0,
268 16,
269 &aes_info
270};
271
272static const mbedtls_cipher_info_t aes_192_ecb_info = {
273 MBEDTLS_CIPHER_AES_192_ECB,
274 MBEDTLS_MODE_ECB,
275 192,
276 "AES-192-ECB",
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100277 0,
Jens Wiklander817466c2018-05-22 13:49:31 +0200278 0,
279 16,
280 &aes_info
281};
282
283static const mbedtls_cipher_info_t aes_256_ecb_info = {
284 MBEDTLS_CIPHER_AES_256_ECB,
285 MBEDTLS_MODE_ECB,
286 256,
287 "AES-256-ECB",
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100288 0,
Jens Wiklander817466c2018-05-22 13:49:31 +0200289 0,
290 16,
291 &aes_info
292};
293
294#if defined(MBEDTLS_CIPHER_MODE_CBC)
295static const mbedtls_cipher_info_t aes_128_cbc_info = {
296 MBEDTLS_CIPHER_AES_128_CBC,
297 MBEDTLS_MODE_CBC,
298 128,
299 "AES-128-CBC",
300 16,
301 0,
302 16,
303 &aes_info
304};
305
306static const mbedtls_cipher_info_t aes_192_cbc_info = {
307 MBEDTLS_CIPHER_AES_192_CBC,
308 MBEDTLS_MODE_CBC,
309 192,
310 "AES-192-CBC",
311 16,
312 0,
313 16,
314 &aes_info
315};
316
317static const mbedtls_cipher_info_t aes_256_cbc_info = {
318 MBEDTLS_CIPHER_AES_256_CBC,
319 MBEDTLS_MODE_CBC,
320 256,
321 "AES-256-CBC",
322 16,
323 0,
324 16,
325 &aes_info
326};
327#endif /* MBEDTLS_CIPHER_MODE_CBC */
328
329#if defined(MBEDTLS_CIPHER_MODE_CFB)
330static const mbedtls_cipher_info_t aes_128_cfb128_info = {
331 MBEDTLS_CIPHER_AES_128_CFB128,
332 MBEDTLS_MODE_CFB,
333 128,
334 "AES-128-CFB128",
335 16,
336 0,
337 16,
338 &aes_info
339};
340
341static const mbedtls_cipher_info_t aes_192_cfb128_info = {
342 MBEDTLS_CIPHER_AES_192_CFB128,
343 MBEDTLS_MODE_CFB,
344 192,
345 "AES-192-CFB128",
346 16,
347 0,
348 16,
349 &aes_info
350};
351
352static const mbedtls_cipher_info_t aes_256_cfb128_info = {
353 MBEDTLS_CIPHER_AES_256_CFB128,
354 MBEDTLS_MODE_CFB,
355 256,
356 "AES-256-CFB128",
357 16,
358 0,
359 16,
360 &aes_info
361};
362#endif /* MBEDTLS_CIPHER_MODE_CFB */
363
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100364#if defined(MBEDTLS_CIPHER_MODE_OFB)
365static const mbedtls_cipher_info_t aes_128_ofb_info = {
366 MBEDTLS_CIPHER_AES_128_OFB,
367 MBEDTLS_MODE_OFB,
368 128,
369 "AES-128-OFB",
370 16,
371 0,
372 16,
373 &aes_info
374};
375
376static const mbedtls_cipher_info_t aes_192_ofb_info = {
377 MBEDTLS_CIPHER_AES_192_OFB,
378 MBEDTLS_MODE_OFB,
379 192,
380 "AES-192-OFB",
381 16,
382 0,
383 16,
384 &aes_info
385};
386
387static const mbedtls_cipher_info_t aes_256_ofb_info = {
388 MBEDTLS_CIPHER_AES_256_OFB,
389 MBEDTLS_MODE_OFB,
390 256,
391 "AES-256-OFB",
392 16,
393 0,
394 16,
395 &aes_info
396};
397#endif /* MBEDTLS_CIPHER_MODE_OFB */
398
Jens Wiklander817466c2018-05-22 13:49:31 +0200399#if defined(MBEDTLS_CIPHER_MODE_CTR)
400static const mbedtls_cipher_info_t aes_128_ctr_info = {
401 MBEDTLS_CIPHER_AES_128_CTR,
402 MBEDTLS_MODE_CTR,
403 128,
404 "AES-128-CTR",
405 16,
406 0,
407 16,
408 &aes_info
409};
410
411static const mbedtls_cipher_info_t aes_192_ctr_info = {
412 MBEDTLS_CIPHER_AES_192_CTR,
413 MBEDTLS_MODE_CTR,
414 192,
415 "AES-192-CTR",
416 16,
417 0,
418 16,
419 &aes_info
420};
421
422static const mbedtls_cipher_info_t aes_256_ctr_info = {
423 MBEDTLS_CIPHER_AES_256_CTR,
424 MBEDTLS_MODE_CTR,
425 256,
426 "AES-256-CTR",
427 16,
428 0,
429 16,
430 &aes_info
431};
432#endif /* MBEDTLS_CIPHER_MODE_CTR */
433
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100434#if defined(MBEDTLS_CIPHER_MODE_XTS)
Jens Wiklander32b31802023-10-06 16:59:46 +0200435static int xts_aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
436 unsigned int key_bitlen)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100437{
438 mbedtls_aes_xts_context *xts_ctx = ctx;
Jens Wiklander32b31802023-10-06 16:59:46 +0200439 return mbedtls_aes_xts_setkey_enc(xts_ctx, key, key_bitlen);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100440}
441
Jens Wiklander32b31802023-10-06 16:59:46 +0200442static int xts_aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
443 unsigned int key_bitlen)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100444{
445 mbedtls_aes_xts_context *xts_ctx = ctx;
Jens Wiklander32b31802023-10-06 16:59:46 +0200446 return mbedtls_aes_xts_setkey_dec(xts_ctx, key, key_bitlen);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100447}
448
Jens Wiklander32b31802023-10-06 16:59:46 +0200449static void *xts_aes_ctx_alloc(void)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100450{
Jens Wiklander32b31802023-10-06 16:59:46 +0200451 mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc(1, sizeof(*xts_ctx));
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100452
Jens Wiklander32b31802023-10-06 16:59:46 +0200453 if (xts_ctx != NULL) {
454 mbedtls_aes_xts_init(xts_ctx);
455 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100456
Jens Wiklander32b31802023-10-06 16:59:46 +0200457 return xts_ctx;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100458}
459
Jens Wiklander32b31802023-10-06 16:59:46 +0200460static void xts_aes_ctx_free(void *ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100461{
462 mbedtls_aes_xts_context *xts_ctx = ctx;
463
Jens Wiklander32b31802023-10-06 16:59:46 +0200464 if (xts_ctx == NULL) {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100465 return;
Jens Wiklander32b31802023-10-06 16:59:46 +0200466 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100467
Jens Wiklander32b31802023-10-06 16:59:46 +0200468 mbedtls_aes_xts_free(xts_ctx);
469 mbedtls_free(xts_ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100470}
471
472static const mbedtls_cipher_base_t xts_aes_info = {
473 MBEDTLS_CIPHER_ID_AES,
474 NULL,
475#if defined(MBEDTLS_CIPHER_MODE_CBC)
476 NULL,
477#endif
478#if defined(MBEDTLS_CIPHER_MODE_CFB)
479 NULL,
480#endif
481#if defined(MBEDTLS_CIPHER_MODE_OFB)
482 NULL,
483#endif
484#if defined(MBEDTLS_CIPHER_MODE_CTR)
485 NULL,
486#endif
487#if defined(MBEDTLS_CIPHER_MODE_XTS)
488 aes_crypt_xts_wrap,
489#endif
490#if defined(MBEDTLS_CIPHER_MODE_STREAM)
491 NULL,
492#endif
493 xts_aes_setkey_enc_wrap,
494 xts_aes_setkey_dec_wrap,
495 xts_aes_ctx_alloc,
496 xts_aes_ctx_free
497};
498
499static const mbedtls_cipher_info_t aes_128_xts_info = {
500 MBEDTLS_CIPHER_AES_128_XTS,
501 MBEDTLS_MODE_XTS,
502 256,
503 "AES-128-XTS",
504 16,
505 0,
506 16,
507 &xts_aes_info
508};
509
510static const mbedtls_cipher_info_t aes_256_xts_info = {
511 MBEDTLS_CIPHER_AES_256_XTS,
512 MBEDTLS_MODE_XTS,
513 512,
514 "AES-256-XTS",
515 16,
516 0,
517 16,
518 &xts_aes_info
519};
520#endif /* MBEDTLS_CIPHER_MODE_XTS */
521
Jens Wiklander817466c2018-05-22 13:49:31 +0200522#if defined(MBEDTLS_GCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200523static int gcm_aes_setkey_wrap(void *ctx, const unsigned char *key,
524 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200525{
Jens Wiklander32b31802023-10-06 16:59:46 +0200526 return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
527 key, key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200528}
529
530static const mbedtls_cipher_base_t gcm_aes_info = {
531 MBEDTLS_CIPHER_ID_AES,
532 NULL,
533#if defined(MBEDTLS_CIPHER_MODE_CBC)
534 NULL,
535#endif
536#if defined(MBEDTLS_CIPHER_MODE_CFB)
537 NULL,
538#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100539#if defined(MBEDTLS_CIPHER_MODE_OFB)
540 NULL,
541#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200542#if defined(MBEDTLS_CIPHER_MODE_CTR)
543 NULL,
544#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100545#if defined(MBEDTLS_CIPHER_MODE_XTS)
546 NULL,
547#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200548#if defined(MBEDTLS_CIPHER_MODE_STREAM)
549 NULL,
550#endif
551 gcm_aes_setkey_wrap,
552 gcm_aes_setkey_wrap,
553 gcm_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +0800554 gcm_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +0200555 gcm_ctx_free,
556};
557
558static const mbedtls_cipher_info_t aes_128_gcm_info = {
559 MBEDTLS_CIPHER_AES_128_GCM,
560 MBEDTLS_MODE_GCM,
561 128,
562 "AES-128-GCM",
563 12,
564 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
565 16,
566 &gcm_aes_info
567};
568
569static const mbedtls_cipher_info_t aes_192_gcm_info = {
570 MBEDTLS_CIPHER_AES_192_GCM,
571 MBEDTLS_MODE_GCM,
572 192,
573 "AES-192-GCM",
574 12,
575 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
576 16,
577 &gcm_aes_info
578};
579
580static const mbedtls_cipher_info_t aes_256_gcm_info = {
581 MBEDTLS_CIPHER_AES_256_GCM,
582 MBEDTLS_MODE_GCM,
583 256,
584 "AES-256-GCM",
585 12,
586 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
587 16,
588 &gcm_aes_info
589};
590#endif /* MBEDTLS_GCM_C */
591
592#if defined(MBEDTLS_CCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200593static int ccm_aes_setkey_wrap(void *ctx, const unsigned char *key,
594 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200595{
Jens Wiklander32b31802023-10-06 16:59:46 +0200596 return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
597 key, key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200598}
599
600static const mbedtls_cipher_base_t ccm_aes_info = {
601 MBEDTLS_CIPHER_ID_AES,
602 NULL,
603#if defined(MBEDTLS_CIPHER_MODE_CBC)
604 NULL,
605#endif
606#if defined(MBEDTLS_CIPHER_MODE_CFB)
607 NULL,
608#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100609#if defined(MBEDTLS_CIPHER_MODE_OFB)
610 NULL,
611#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200612#if defined(MBEDTLS_CIPHER_MODE_CTR)
613 NULL,
614#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100615#if defined(MBEDTLS_CIPHER_MODE_XTS)
616 NULL,
617#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200618#if defined(MBEDTLS_CIPHER_MODE_STREAM)
619 NULL,
620#endif
621 ccm_aes_setkey_wrap,
622 ccm_aes_setkey_wrap,
623 ccm_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +0800624 ccm_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +0200625 ccm_ctx_free,
626};
627
628static const mbedtls_cipher_info_t aes_128_ccm_info = {
629 MBEDTLS_CIPHER_AES_128_CCM,
630 MBEDTLS_MODE_CCM,
631 128,
632 "AES-128-CCM",
633 12,
634 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
635 16,
636 &ccm_aes_info
637};
638
639static const mbedtls_cipher_info_t aes_192_ccm_info = {
640 MBEDTLS_CIPHER_AES_192_CCM,
641 MBEDTLS_MODE_CCM,
642 192,
643 "AES-192-CCM",
644 12,
645 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
646 16,
647 &ccm_aes_info
648};
649
650static const mbedtls_cipher_info_t aes_256_ccm_info = {
651 MBEDTLS_CIPHER_AES_256_CCM,
652 MBEDTLS_MODE_CCM,
653 256,
654 "AES-256-CCM",
655 12,
656 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
657 16,
658 &ccm_aes_info
659};
Jens Wiklander32b31802023-10-06 16:59:46 +0200660
661static const mbedtls_cipher_info_t aes_128_ccm_star_no_tag_info = {
662 MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG,
663 MBEDTLS_MODE_CCM_STAR_NO_TAG,
664 128,
665 "AES-128-CCM*-NO-TAG",
666 12,
667 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
668 16,
669 &ccm_aes_info
670};
671
672static const mbedtls_cipher_info_t aes_192_ccm_star_no_tag_info = {
673 MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG,
674 MBEDTLS_MODE_CCM_STAR_NO_TAG,
675 192,
676 "AES-192-CCM*-NO-TAG",
677 12,
678 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
679 16,
680 &ccm_aes_info
681};
682
683static const mbedtls_cipher_info_t aes_256_ccm_star_no_tag_info = {
684 MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG,
685 MBEDTLS_MODE_CCM_STAR_NO_TAG,
686 256,
687 "AES-256-CCM*-NO-TAG",
688 12,
689 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
690 16,
691 &ccm_aes_info
692};
Jens Wiklander817466c2018-05-22 13:49:31 +0200693#endif /* MBEDTLS_CCM_C */
694
695#endif /* MBEDTLS_AES_C */
696
697#if defined(MBEDTLS_CAMELLIA_C)
698
Jens Wiklander32b31802023-10-06 16:59:46 +0200699static int camellia_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
700 const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +0200701{
Jens Wiklander32b31802023-10-06 16:59:46 +0200702 return mbedtls_camellia_crypt_ecb((mbedtls_camellia_context *) ctx, operation, input,
703 output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200704}
705
706#if defined(MBEDTLS_CIPHER_MODE_CBC)
Jens Wiklander32b31802023-10-06 16:59:46 +0200707static int camellia_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
708 size_t length, unsigned char *iv,
709 const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +0200710{
Jens Wiklander32b31802023-10-06 16:59:46 +0200711 return mbedtls_camellia_crypt_cbc((mbedtls_camellia_context *) ctx, operation, length, iv,
712 input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200713}
714#endif /* MBEDTLS_CIPHER_MODE_CBC */
715
716#if defined(MBEDTLS_CIPHER_MODE_CFB)
Jens Wiklander32b31802023-10-06 16:59:46 +0200717static int camellia_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
718 size_t length, size_t *iv_off, unsigned char *iv,
719 const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +0200720{
Jens Wiklander32b31802023-10-06 16:59:46 +0200721 return mbedtls_camellia_crypt_cfb128((mbedtls_camellia_context *) ctx, operation, length,
722 iv_off, iv, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200723}
724#endif /* MBEDTLS_CIPHER_MODE_CFB */
725
726#if defined(MBEDTLS_CIPHER_MODE_CTR)
Jens Wiklander32b31802023-10-06 16:59:46 +0200727static int camellia_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
728 unsigned char *nonce_counter, unsigned char *stream_block,
729 const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +0200730{
Jens Wiklander32b31802023-10-06 16:59:46 +0200731 return mbedtls_camellia_crypt_ctr((mbedtls_camellia_context *) ctx, length, nc_off,
732 nonce_counter, stream_block, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +0200733}
734#endif /* MBEDTLS_CIPHER_MODE_CTR */
735
Jens Wiklander32b31802023-10-06 16:59:46 +0200736static int camellia_setkey_dec_wrap(void *ctx, const unsigned char *key,
737 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200738{
Jens Wiklander32b31802023-10-06 16:59:46 +0200739 return mbedtls_camellia_setkey_dec((mbedtls_camellia_context *) ctx, key, key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200740}
741
Jens Wiklander32b31802023-10-06 16:59:46 +0200742static int camellia_setkey_enc_wrap(void *ctx, const unsigned char *key,
743 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200744{
Jens Wiklander32b31802023-10-06 16:59:46 +0200745 return mbedtls_camellia_setkey_enc((mbedtls_camellia_context *) ctx, key, key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200746}
747
Jens Wiklander32b31802023-10-06 16:59:46 +0200748static void *camellia_ctx_alloc(void)
Jens Wiklander817466c2018-05-22 13:49:31 +0200749{
750 mbedtls_camellia_context *ctx;
Jens Wiklander32b31802023-10-06 16:59:46 +0200751 ctx = mbedtls_calloc(1, sizeof(mbedtls_camellia_context));
Jens Wiklander817466c2018-05-22 13:49:31 +0200752
Jens Wiklander32b31802023-10-06 16:59:46 +0200753 if (ctx == NULL) {
754 return NULL;
755 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200756
Jens Wiklander32b31802023-10-06 16:59:46 +0200757 mbedtls_camellia_init(ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +0200758
Jens Wiklander32b31802023-10-06 16:59:46 +0200759 return ctx;
Jens Wiklander817466c2018-05-22 13:49:31 +0200760}
761
Jens Wiklander32b31802023-10-06 16:59:46 +0200762static void camellia_ctx_clone(void *dst, const void *src)
Edison Ai12484fc2018-12-19 15:36:28 +0800763{
Jens Wiklander32b31802023-10-06 16:59:46 +0200764 memcpy(dst, src, sizeof(mbedtls_camellia_context));
Edison Ai12484fc2018-12-19 15:36:28 +0800765}
766
Jens Wiklander32b31802023-10-06 16:59:46 +0200767static void camellia_ctx_free(void *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +0200768{
Jens Wiklander32b31802023-10-06 16:59:46 +0200769 mbedtls_camellia_free((mbedtls_camellia_context *) ctx);
770 mbedtls_free(ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +0200771}
772
773static const mbedtls_cipher_base_t camellia_info = {
774 MBEDTLS_CIPHER_ID_CAMELLIA,
775 camellia_crypt_ecb_wrap,
776#if defined(MBEDTLS_CIPHER_MODE_CBC)
777 camellia_crypt_cbc_wrap,
778#endif
779#if defined(MBEDTLS_CIPHER_MODE_CFB)
780 camellia_crypt_cfb128_wrap,
781#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100782#if defined(MBEDTLS_CIPHER_MODE_OFB)
783 NULL,
784#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200785#if defined(MBEDTLS_CIPHER_MODE_CTR)
786 camellia_crypt_ctr_wrap,
787#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100788#if defined(MBEDTLS_CIPHER_MODE_XTS)
789 NULL,
790#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200791#if defined(MBEDTLS_CIPHER_MODE_STREAM)
792 NULL,
793#endif
794 camellia_setkey_enc_wrap,
795 camellia_setkey_dec_wrap,
796 camellia_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +0800797 camellia_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +0200798 camellia_ctx_free
799};
800
801static const mbedtls_cipher_info_t camellia_128_ecb_info = {
802 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
803 MBEDTLS_MODE_ECB,
804 128,
805 "CAMELLIA-128-ECB",
Jerome Forissier79013242021-07-28 10:24:04 +0200806 0,
Jens Wiklander817466c2018-05-22 13:49:31 +0200807 0,
808 16,
809 &camellia_info
810};
811
812static const mbedtls_cipher_info_t camellia_192_ecb_info = {
813 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
814 MBEDTLS_MODE_ECB,
815 192,
816 "CAMELLIA-192-ECB",
Jerome Forissier79013242021-07-28 10:24:04 +0200817 0,
Jens Wiklander817466c2018-05-22 13:49:31 +0200818 0,
819 16,
820 &camellia_info
821};
822
823static const mbedtls_cipher_info_t camellia_256_ecb_info = {
824 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
825 MBEDTLS_MODE_ECB,
826 256,
827 "CAMELLIA-256-ECB",
Jerome Forissier79013242021-07-28 10:24:04 +0200828 0,
Jens Wiklander817466c2018-05-22 13:49:31 +0200829 0,
830 16,
831 &camellia_info
832};
833
834#if defined(MBEDTLS_CIPHER_MODE_CBC)
835static const mbedtls_cipher_info_t camellia_128_cbc_info = {
836 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
837 MBEDTLS_MODE_CBC,
838 128,
839 "CAMELLIA-128-CBC",
840 16,
841 0,
842 16,
843 &camellia_info
844};
845
846static const mbedtls_cipher_info_t camellia_192_cbc_info = {
847 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
848 MBEDTLS_MODE_CBC,
849 192,
850 "CAMELLIA-192-CBC",
851 16,
852 0,
853 16,
854 &camellia_info
855};
856
857static const mbedtls_cipher_info_t camellia_256_cbc_info = {
858 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
859 MBEDTLS_MODE_CBC,
860 256,
861 "CAMELLIA-256-CBC",
862 16,
863 0,
864 16,
865 &camellia_info
866};
867#endif /* MBEDTLS_CIPHER_MODE_CBC */
868
869#if defined(MBEDTLS_CIPHER_MODE_CFB)
870static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
871 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
872 MBEDTLS_MODE_CFB,
873 128,
874 "CAMELLIA-128-CFB128",
875 16,
876 0,
877 16,
878 &camellia_info
879};
880
881static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
882 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
883 MBEDTLS_MODE_CFB,
884 192,
885 "CAMELLIA-192-CFB128",
886 16,
887 0,
888 16,
889 &camellia_info
890};
891
892static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
893 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
894 MBEDTLS_MODE_CFB,
895 256,
896 "CAMELLIA-256-CFB128",
897 16,
898 0,
899 16,
900 &camellia_info
901};
902#endif /* MBEDTLS_CIPHER_MODE_CFB */
903
904#if defined(MBEDTLS_CIPHER_MODE_CTR)
905static const mbedtls_cipher_info_t camellia_128_ctr_info = {
906 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
907 MBEDTLS_MODE_CTR,
908 128,
909 "CAMELLIA-128-CTR",
910 16,
911 0,
912 16,
913 &camellia_info
914};
915
916static const mbedtls_cipher_info_t camellia_192_ctr_info = {
917 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
918 MBEDTLS_MODE_CTR,
919 192,
920 "CAMELLIA-192-CTR",
921 16,
922 0,
923 16,
924 &camellia_info
925};
926
927static const mbedtls_cipher_info_t camellia_256_ctr_info = {
928 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
929 MBEDTLS_MODE_CTR,
930 256,
931 "CAMELLIA-256-CTR",
932 16,
933 0,
934 16,
935 &camellia_info
936};
937#endif /* MBEDTLS_CIPHER_MODE_CTR */
938
939#if defined(MBEDTLS_GCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +0200940static int gcm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
941 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200942{
Jens Wiklander32b31802023-10-06 16:59:46 +0200943 return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
944 key, key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +0200945}
946
947static const mbedtls_cipher_base_t gcm_camellia_info = {
948 MBEDTLS_CIPHER_ID_CAMELLIA,
949 NULL,
950#if defined(MBEDTLS_CIPHER_MODE_CBC)
951 NULL,
952#endif
953#if defined(MBEDTLS_CIPHER_MODE_CFB)
954 NULL,
955#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100956#if defined(MBEDTLS_CIPHER_MODE_OFB)
957 NULL,
958#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200959#if defined(MBEDTLS_CIPHER_MODE_CTR)
960 NULL,
961#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100962#if defined(MBEDTLS_CIPHER_MODE_XTS)
963 NULL,
964#endif
Jens Wiklander817466c2018-05-22 13:49:31 +0200965#if defined(MBEDTLS_CIPHER_MODE_STREAM)
966 NULL,
967#endif
968 gcm_camellia_setkey_wrap,
969 gcm_camellia_setkey_wrap,
970 gcm_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +0800971 gcm_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +0200972 gcm_ctx_free,
973};
974
975static const mbedtls_cipher_info_t camellia_128_gcm_info = {
976 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
977 MBEDTLS_MODE_GCM,
978 128,
979 "CAMELLIA-128-GCM",
980 12,
981 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
982 16,
983 &gcm_camellia_info
984};
985
986static const mbedtls_cipher_info_t camellia_192_gcm_info = {
987 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
988 MBEDTLS_MODE_GCM,
989 192,
990 "CAMELLIA-192-GCM",
991 12,
992 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
993 16,
994 &gcm_camellia_info
995};
996
997static const mbedtls_cipher_info_t camellia_256_gcm_info = {
998 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
999 MBEDTLS_MODE_GCM,
1000 256,
1001 "CAMELLIA-256-GCM",
1002 12,
1003 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1004 16,
1005 &gcm_camellia_info
1006};
1007#endif /* MBEDTLS_GCM_C */
1008
1009#if defined(MBEDTLS_CCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001010static int ccm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
1011 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001012{
Jens Wiklander32b31802023-10-06 16:59:46 +02001013 return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
1014 key, key_bitlen);
Jens Wiklander817466c2018-05-22 13:49:31 +02001015}
1016
1017static const mbedtls_cipher_base_t ccm_camellia_info = {
1018 MBEDTLS_CIPHER_ID_CAMELLIA,
1019 NULL,
1020#if defined(MBEDTLS_CIPHER_MODE_CBC)
1021 NULL,
1022#endif
1023#if defined(MBEDTLS_CIPHER_MODE_CFB)
1024 NULL,
1025#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001026#if defined(MBEDTLS_CIPHER_MODE_OFB)
1027 NULL,
1028#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001029#if defined(MBEDTLS_CIPHER_MODE_CTR)
1030 NULL,
1031#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001032#if defined(MBEDTLS_CIPHER_MODE_XTS)
1033 NULL,
1034#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001035#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1036 NULL,
1037#endif
1038 ccm_camellia_setkey_wrap,
1039 ccm_camellia_setkey_wrap,
1040 ccm_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +08001041 ccm_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +02001042 ccm_ctx_free,
1043};
1044
1045static const mbedtls_cipher_info_t camellia_128_ccm_info = {
1046 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
1047 MBEDTLS_MODE_CCM,
1048 128,
1049 "CAMELLIA-128-CCM",
1050 12,
1051 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1052 16,
1053 &ccm_camellia_info
1054};
1055
1056static const mbedtls_cipher_info_t camellia_192_ccm_info = {
1057 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
1058 MBEDTLS_MODE_CCM,
1059 192,
1060 "CAMELLIA-192-CCM",
1061 12,
1062 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1063 16,
1064 &ccm_camellia_info
1065};
1066
1067static const mbedtls_cipher_info_t camellia_256_ccm_info = {
1068 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
1069 MBEDTLS_MODE_CCM,
1070 256,
1071 "CAMELLIA-256-CCM",
1072 12,
1073 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1074 16,
1075 &ccm_camellia_info
1076};
Jens Wiklander32b31802023-10-06 16:59:46 +02001077
1078static const mbedtls_cipher_info_t camellia_128_ccm_star_no_tag_info = {
1079 MBEDTLS_CIPHER_CAMELLIA_128_CCM_STAR_NO_TAG,
1080 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1081 128,
1082 "CAMELLIA-128-CCM*-NO-TAG",
1083 12,
1084 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1085 16,
1086 &ccm_camellia_info
1087};
1088
1089static const mbedtls_cipher_info_t camellia_192_ccm_star_no_tag_info = {
1090 MBEDTLS_CIPHER_CAMELLIA_192_CCM_STAR_NO_TAG,
1091 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1092 192,
1093 "CAMELLIA-192-CCM*-NO-TAG",
1094 12,
1095 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1096 16,
1097 &ccm_camellia_info
1098};
1099
1100static const mbedtls_cipher_info_t camellia_256_ccm_star_no_tag_info = {
1101 MBEDTLS_CIPHER_CAMELLIA_256_CCM_STAR_NO_TAG,
1102 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1103 256,
1104 "CAMELLIA-256-CCM*-NO-TAG",
1105 12,
1106 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1107 16,
1108 &ccm_camellia_info
1109};
Jens Wiklander817466c2018-05-22 13:49:31 +02001110#endif /* MBEDTLS_CCM_C */
1111
1112#endif /* MBEDTLS_CAMELLIA_C */
1113
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001114#if defined(MBEDTLS_ARIA_C)
1115
Jens Wiklander32b31802023-10-06 16:59:46 +02001116static int aria_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1117 const unsigned char *input, unsigned char *output)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001118{
1119 (void) operation;
Jens Wiklander32b31802023-10-06 16:59:46 +02001120 return mbedtls_aria_crypt_ecb((mbedtls_aria_context *) ctx, input,
1121 output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001122}
1123
1124#if defined(MBEDTLS_CIPHER_MODE_CBC)
Jens Wiklander32b31802023-10-06 16:59:46 +02001125static int aria_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
1126 size_t length, unsigned char *iv,
1127 const unsigned char *input, unsigned char *output)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001128{
Jens Wiklander32b31802023-10-06 16:59:46 +02001129 return mbedtls_aria_crypt_cbc((mbedtls_aria_context *) ctx, operation, length, iv,
1130 input, output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001131}
1132#endif /* MBEDTLS_CIPHER_MODE_CBC */
1133
1134#if defined(MBEDTLS_CIPHER_MODE_CFB)
Jens Wiklander32b31802023-10-06 16:59:46 +02001135static int aria_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
1136 size_t length, size_t *iv_off, unsigned char *iv,
1137 const unsigned char *input, unsigned char *output)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001138{
Jens Wiklander32b31802023-10-06 16:59:46 +02001139 return mbedtls_aria_crypt_cfb128((mbedtls_aria_context *) ctx, operation, length,
1140 iv_off, iv, input, output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001141}
1142#endif /* MBEDTLS_CIPHER_MODE_CFB */
1143
1144#if defined(MBEDTLS_CIPHER_MODE_CTR)
Jens Wiklander32b31802023-10-06 16:59:46 +02001145static int aria_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
1146 unsigned char *nonce_counter, unsigned char *stream_block,
1147 const unsigned char *input, unsigned char *output)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001148{
Jens Wiklander32b31802023-10-06 16:59:46 +02001149 return mbedtls_aria_crypt_ctr((mbedtls_aria_context *) ctx, length, nc_off,
1150 nonce_counter, stream_block, input, output);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001151}
1152#endif /* MBEDTLS_CIPHER_MODE_CTR */
1153
Jens Wiklander32b31802023-10-06 16:59:46 +02001154static int aria_setkey_dec_wrap(void *ctx, const unsigned char *key,
1155 unsigned int key_bitlen)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001156{
Jens Wiklander32b31802023-10-06 16:59:46 +02001157 return mbedtls_aria_setkey_dec((mbedtls_aria_context *) ctx, key, key_bitlen);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001158}
1159
Jens Wiklander32b31802023-10-06 16:59:46 +02001160static int aria_setkey_enc_wrap(void *ctx, const unsigned char *key,
1161 unsigned int key_bitlen)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001162{
Jens Wiklander32b31802023-10-06 16:59:46 +02001163 return mbedtls_aria_setkey_enc((mbedtls_aria_context *) ctx, key, key_bitlen);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001164}
1165
Jens Wiklander32b31802023-10-06 16:59:46 +02001166static void *aria_ctx_alloc(void)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001167{
1168 mbedtls_aria_context *ctx;
Jens Wiklander32b31802023-10-06 16:59:46 +02001169 ctx = mbedtls_calloc(1, sizeof(mbedtls_aria_context));
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001170
Jens Wiklander32b31802023-10-06 16:59:46 +02001171 if (ctx == NULL) {
1172 return NULL;
1173 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001174
Jens Wiklander32b31802023-10-06 16:59:46 +02001175 mbedtls_aria_init(ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001176
Jens Wiklander32b31802023-10-06 16:59:46 +02001177 return ctx;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001178}
1179
Jens Wiklander32b31802023-10-06 16:59:46 +02001180static void aria_ctx_free(void *ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001181{
Jens Wiklander32b31802023-10-06 16:59:46 +02001182 mbedtls_aria_free((mbedtls_aria_context *) ctx);
1183 mbedtls_free(ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001184}
1185
1186static const mbedtls_cipher_base_t aria_info = {
1187 MBEDTLS_CIPHER_ID_ARIA,
1188 aria_crypt_ecb_wrap,
1189#if defined(MBEDTLS_CIPHER_MODE_CBC)
1190 aria_crypt_cbc_wrap,
1191#endif
1192#if defined(MBEDTLS_CIPHER_MODE_CFB)
1193 aria_crypt_cfb128_wrap,
1194#endif
1195#if defined(MBEDTLS_CIPHER_MODE_OFB)
1196 NULL,
1197#endif
1198#if defined(MBEDTLS_CIPHER_MODE_CTR)
1199 aria_crypt_ctr_wrap,
1200#endif
1201#if defined(MBEDTLS_CIPHER_MODE_XTS)
1202 NULL,
1203#endif
1204#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1205 NULL,
1206#endif
1207 aria_setkey_enc_wrap,
1208 aria_setkey_dec_wrap,
1209 aria_ctx_alloc,
1210 aria_ctx_free
1211};
1212
1213static const mbedtls_cipher_info_t aria_128_ecb_info = {
1214 MBEDTLS_CIPHER_ARIA_128_ECB,
1215 MBEDTLS_MODE_ECB,
1216 128,
1217 "ARIA-128-ECB",
Jerome Forissier79013242021-07-28 10:24:04 +02001218 0,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001219 0,
1220 16,
1221 &aria_info
1222};
1223
1224static const mbedtls_cipher_info_t aria_192_ecb_info = {
1225 MBEDTLS_CIPHER_ARIA_192_ECB,
1226 MBEDTLS_MODE_ECB,
1227 192,
1228 "ARIA-192-ECB",
Jerome Forissier79013242021-07-28 10:24:04 +02001229 0,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001230 0,
1231 16,
1232 &aria_info
1233};
1234
1235static const mbedtls_cipher_info_t aria_256_ecb_info = {
1236 MBEDTLS_CIPHER_ARIA_256_ECB,
1237 MBEDTLS_MODE_ECB,
1238 256,
1239 "ARIA-256-ECB",
Jerome Forissier79013242021-07-28 10:24:04 +02001240 0,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001241 0,
1242 16,
1243 &aria_info
1244};
1245
1246#if defined(MBEDTLS_CIPHER_MODE_CBC)
1247static const mbedtls_cipher_info_t aria_128_cbc_info = {
1248 MBEDTLS_CIPHER_ARIA_128_CBC,
1249 MBEDTLS_MODE_CBC,
1250 128,
1251 "ARIA-128-CBC",
1252 16,
1253 0,
1254 16,
1255 &aria_info
1256};
1257
1258static const mbedtls_cipher_info_t aria_192_cbc_info = {
1259 MBEDTLS_CIPHER_ARIA_192_CBC,
1260 MBEDTLS_MODE_CBC,
1261 192,
1262 "ARIA-192-CBC",
1263 16,
1264 0,
1265 16,
1266 &aria_info
1267};
1268
1269static const mbedtls_cipher_info_t aria_256_cbc_info = {
1270 MBEDTLS_CIPHER_ARIA_256_CBC,
1271 MBEDTLS_MODE_CBC,
1272 256,
1273 "ARIA-256-CBC",
1274 16,
1275 0,
1276 16,
1277 &aria_info
1278};
1279#endif /* MBEDTLS_CIPHER_MODE_CBC */
1280
1281#if defined(MBEDTLS_CIPHER_MODE_CFB)
1282static const mbedtls_cipher_info_t aria_128_cfb128_info = {
1283 MBEDTLS_CIPHER_ARIA_128_CFB128,
1284 MBEDTLS_MODE_CFB,
1285 128,
1286 "ARIA-128-CFB128",
1287 16,
1288 0,
1289 16,
1290 &aria_info
1291};
1292
1293static const mbedtls_cipher_info_t aria_192_cfb128_info = {
1294 MBEDTLS_CIPHER_ARIA_192_CFB128,
1295 MBEDTLS_MODE_CFB,
1296 192,
1297 "ARIA-192-CFB128",
1298 16,
1299 0,
1300 16,
1301 &aria_info
1302};
1303
1304static const mbedtls_cipher_info_t aria_256_cfb128_info = {
1305 MBEDTLS_CIPHER_ARIA_256_CFB128,
1306 MBEDTLS_MODE_CFB,
1307 256,
1308 "ARIA-256-CFB128",
1309 16,
1310 0,
1311 16,
1312 &aria_info
1313};
1314#endif /* MBEDTLS_CIPHER_MODE_CFB */
1315
1316#if defined(MBEDTLS_CIPHER_MODE_CTR)
1317static const mbedtls_cipher_info_t aria_128_ctr_info = {
1318 MBEDTLS_CIPHER_ARIA_128_CTR,
1319 MBEDTLS_MODE_CTR,
1320 128,
1321 "ARIA-128-CTR",
1322 16,
1323 0,
1324 16,
1325 &aria_info
1326};
1327
1328static const mbedtls_cipher_info_t aria_192_ctr_info = {
1329 MBEDTLS_CIPHER_ARIA_192_CTR,
1330 MBEDTLS_MODE_CTR,
1331 192,
1332 "ARIA-192-CTR",
1333 16,
1334 0,
1335 16,
1336 &aria_info
1337};
1338
1339static const mbedtls_cipher_info_t aria_256_ctr_info = {
1340 MBEDTLS_CIPHER_ARIA_256_CTR,
1341 MBEDTLS_MODE_CTR,
1342 256,
1343 "ARIA-256-CTR",
1344 16,
1345 0,
1346 16,
1347 &aria_info
1348};
1349#endif /* MBEDTLS_CIPHER_MODE_CTR */
1350
1351#if defined(MBEDTLS_GCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001352static int gcm_aria_setkey_wrap(void *ctx, const unsigned char *key,
1353 unsigned int key_bitlen)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001354{
Jens Wiklander32b31802023-10-06 16:59:46 +02001355 return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1356 key, key_bitlen);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001357}
1358
1359static const mbedtls_cipher_base_t gcm_aria_info = {
1360 MBEDTLS_CIPHER_ID_ARIA,
1361 NULL,
1362#if defined(MBEDTLS_CIPHER_MODE_CBC)
1363 NULL,
1364#endif
1365#if defined(MBEDTLS_CIPHER_MODE_CFB)
1366 NULL,
1367#endif
1368#if defined(MBEDTLS_CIPHER_MODE_OFB)
1369 NULL,
1370#endif
1371#if defined(MBEDTLS_CIPHER_MODE_CTR)
1372 NULL,
1373#endif
1374#if defined(MBEDTLS_CIPHER_MODE_XTS)
1375 NULL,
1376#endif
1377#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1378 NULL,
1379#endif
1380 gcm_aria_setkey_wrap,
1381 gcm_aria_setkey_wrap,
1382 gcm_ctx_alloc,
1383 gcm_ctx_free,
1384};
1385
1386static const mbedtls_cipher_info_t aria_128_gcm_info = {
1387 MBEDTLS_CIPHER_ARIA_128_GCM,
1388 MBEDTLS_MODE_GCM,
1389 128,
1390 "ARIA-128-GCM",
1391 12,
1392 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1393 16,
1394 &gcm_aria_info
1395};
1396
1397static const mbedtls_cipher_info_t aria_192_gcm_info = {
1398 MBEDTLS_CIPHER_ARIA_192_GCM,
1399 MBEDTLS_MODE_GCM,
1400 192,
1401 "ARIA-192-GCM",
1402 12,
1403 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1404 16,
1405 &gcm_aria_info
1406};
1407
1408static const mbedtls_cipher_info_t aria_256_gcm_info = {
1409 MBEDTLS_CIPHER_ARIA_256_GCM,
1410 MBEDTLS_MODE_GCM,
1411 256,
1412 "ARIA-256-GCM",
1413 12,
1414 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1415 16,
1416 &gcm_aria_info
1417};
1418#endif /* MBEDTLS_GCM_C */
1419
1420#if defined(MBEDTLS_CCM_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02001421static int ccm_aria_setkey_wrap(void *ctx, const unsigned char *key,
1422 unsigned int key_bitlen)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001423{
Jens Wiklander32b31802023-10-06 16:59:46 +02001424 return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1425 key, key_bitlen);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001426}
1427
1428static const mbedtls_cipher_base_t ccm_aria_info = {
1429 MBEDTLS_CIPHER_ID_ARIA,
1430 NULL,
1431#if defined(MBEDTLS_CIPHER_MODE_CBC)
1432 NULL,
1433#endif
1434#if defined(MBEDTLS_CIPHER_MODE_CFB)
1435 NULL,
1436#endif
1437#if defined(MBEDTLS_CIPHER_MODE_OFB)
1438 NULL,
1439#endif
1440#if defined(MBEDTLS_CIPHER_MODE_CTR)
1441 NULL,
1442#endif
1443#if defined(MBEDTLS_CIPHER_MODE_XTS)
1444 NULL,
1445#endif
1446#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1447 NULL,
1448#endif
1449 ccm_aria_setkey_wrap,
1450 ccm_aria_setkey_wrap,
1451 ccm_ctx_alloc,
1452 ccm_ctx_free,
1453};
1454
1455static const mbedtls_cipher_info_t aria_128_ccm_info = {
1456 MBEDTLS_CIPHER_ARIA_128_CCM,
1457 MBEDTLS_MODE_CCM,
1458 128,
1459 "ARIA-128-CCM",
1460 12,
1461 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1462 16,
1463 &ccm_aria_info
1464};
1465
1466static const mbedtls_cipher_info_t aria_192_ccm_info = {
1467 MBEDTLS_CIPHER_ARIA_192_CCM,
1468 MBEDTLS_MODE_CCM,
1469 192,
1470 "ARIA-192-CCM",
1471 12,
1472 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1473 16,
1474 &ccm_aria_info
1475};
1476
1477static const mbedtls_cipher_info_t aria_256_ccm_info = {
1478 MBEDTLS_CIPHER_ARIA_256_CCM,
1479 MBEDTLS_MODE_CCM,
1480 256,
1481 "ARIA-256-CCM",
1482 12,
1483 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1484 16,
1485 &ccm_aria_info
1486};
Jens Wiklander32b31802023-10-06 16:59:46 +02001487
1488static const mbedtls_cipher_info_t aria_128_ccm_star_no_tag_info = {
1489 MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG,
1490 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1491 128,
1492 "ARIA-128-CCM*-NO-TAG",
1493 12,
1494 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1495 16,
1496 &ccm_aria_info
1497};
1498
1499static const mbedtls_cipher_info_t aria_192_ccm_star_no_tag_info = {
1500 MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG,
1501 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1502 192,
1503 "ARIA-192-CCM*-NO-TAG",
1504 12,
1505 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1506 16,
1507 &ccm_aria_info
1508};
1509
1510static const mbedtls_cipher_info_t aria_256_ccm_star_no_tag_info = {
1511 MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG,
1512 MBEDTLS_MODE_CCM_STAR_NO_TAG,
1513 256,
1514 "ARIA-256-CCM*-NO-TAG",
1515 12,
1516 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1517 16,
1518 &ccm_aria_info
1519};
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001520#endif /* MBEDTLS_CCM_C */
1521
1522#endif /* MBEDTLS_ARIA_C */
1523
Jens Wiklander817466c2018-05-22 13:49:31 +02001524#if defined(MBEDTLS_DES_C)
1525
Jens Wiklander32b31802023-10-06 16:59:46 +02001526static int des_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1527 const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +02001528{
1529 ((void) operation);
Jens Wiklander32b31802023-10-06 16:59:46 +02001530 return mbedtls_des_crypt_ecb((mbedtls_des_context *) ctx, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +02001531}
1532
Jens Wiklander32b31802023-10-06 16:59:46 +02001533static int des3_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1534 const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +02001535{
1536 ((void) operation);
Jens Wiklander32b31802023-10-06 16:59:46 +02001537 return mbedtls_des3_crypt_ecb((mbedtls_des3_context *) ctx, input, output);
Jens Wiklander817466c2018-05-22 13:49:31 +02001538}
1539
1540#if defined(MBEDTLS_CIPHER_MODE_CBC)
Jens Wiklander32b31802023-10-06 16:59:46 +02001541static int des_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
1542 unsigned char *iv, const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +02001543{
Jens Wiklander32b31802023-10-06 16:59:46 +02001544 return mbedtls_des_crypt_cbc((mbedtls_des_context *) ctx, operation, length, iv, input,
1545 output);
Jens Wiklander817466c2018-05-22 13:49:31 +02001546}
1547#endif /* MBEDTLS_CIPHER_MODE_CBC */
1548
1549#if defined(MBEDTLS_CIPHER_MODE_CBC)
Jens Wiklander32b31802023-10-06 16:59:46 +02001550static int des3_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
1551 unsigned char *iv, const unsigned char *input, unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +02001552{
Jens Wiklander32b31802023-10-06 16:59:46 +02001553 return mbedtls_des3_crypt_cbc((mbedtls_des3_context *) ctx, operation, length, iv, input,
1554 output);
Jens Wiklander817466c2018-05-22 13:49:31 +02001555}
1556#endif /* MBEDTLS_CIPHER_MODE_CBC */
1557
Jens Wiklander32b31802023-10-06 16:59:46 +02001558static int des_setkey_dec_wrap(void *ctx, const unsigned char *key,
1559 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001560{
1561 ((void) key_bitlen);
1562
Jens Wiklander32b31802023-10-06 16:59:46 +02001563 return mbedtls_des_setkey_dec((mbedtls_des_context *) ctx, key);
Jens Wiklander817466c2018-05-22 13:49:31 +02001564}
1565
Jens Wiklander32b31802023-10-06 16:59:46 +02001566static int des_setkey_enc_wrap(void *ctx, const unsigned char *key,
1567 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001568{
1569 ((void) key_bitlen);
1570
Jens Wiklander32b31802023-10-06 16:59:46 +02001571 return mbedtls_des_setkey_enc((mbedtls_des_context *) ctx, key);
Jens Wiklander817466c2018-05-22 13:49:31 +02001572}
1573
Jens Wiklander32b31802023-10-06 16:59:46 +02001574static int des3_set2key_dec_wrap(void *ctx, const unsigned char *key,
1575 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001576{
1577 ((void) key_bitlen);
1578
Jens Wiklander32b31802023-10-06 16:59:46 +02001579 return mbedtls_des3_set2key_dec((mbedtls_des3_context *) ctx, key);
Jens Wiklander817466c2018-05-22 13:49:31 +02001580}
1581
Jens Wiklander32b31802023-10-06 16:59:46 +02001582static int des3_set2key_enc_wrap(void *ctx, const unsigned char *key,
1583 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001584{
1585 ((void) key_bitlen);
1586
Jens Wiklander32b31802023-10-06 16:59:46 +02001587 return mbedtls_des3_set2key_enc((mbedtls_des3_context *) ctx, key);
Jens Wiklander817466c2018-05-22 13:49:31 +02001588}
1589
Jens Wiklander32b31802023-10-06 16:59:46 +02001590static int des3_set3key_dec_wrap(void *ctx, const unsigned char *key,
1591 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001592{
1593 ((void) key_bitlen);
1594
Jens Wiklander32b31802023-10-06 16:59:46 +02001595 return mbedtls_des3_set3key_dec((mbedtls_des3_context *) ctx, key);
Jens Wiklander817466c2018-05-22 13:49:31 +02001596}
1597
Jens Wiklander32b31802023-10-06 16:59:46 +02001598static int des3_set3key_enc_wrap(void *ctx, const unsigned char *key,
1599 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001600{
1601 ((void) key_bitlen);
1602
Jens Wiklander32b31802023-10-06 16:59:46 +02001603 return mbedtls_des3_set3key_enc((mbedtls_des3_context *) ctx, key);
Jens Wiklander817466c2018-05-22 13:49:31 +02001604}
1605
Jens Wiklander32b31802023-10-06 16:59:46 +02001606static void *des_ctx_alloc(void)
Jens Wiklander817466c2018-05-22 13:49:31 +02001607{
Jens Wiklander32b31802023-10-06 16:59:46 +02001608 mbedtls_des_context *des = mbedtls_calloc(1, sizeof(mbedtls_des_context));
Jens Wiklander817466c2018-05-22 13:49:31 +02001609
Jens Wiklander32b31802023-10-06 16:59:46 +02001610 if (des == NULL) {
1611 return NULL;
1612 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001613
Jens Wiklander32b31802023-10-06 16:59:46 +02001614 mbedtls_des_init(des);
Jens Wiklander817466c2018-05-22 13:49:31 +02001615
Jens Wiklander32b31802023-10-06 16:59:46 +02001616 return des;
Jens Wiklander817466c2018-05-22 13:49:31 +02001617}
1618
Jens Wiklander32b31802023-10-06 16:59:46 +02001619static void des_ctx_clone(void *dst, const void *src)
Edison Ai12484fc2018-12-19 15:36:28 +08001620{
Jens Wiklander32b31802023-10-06 16:59:46 +02001621 memcpy(dst, src, sizeof(mbedtls_des_context));
Edison Ai12484fc2018-12-19 15:36:28 +08001622}
1623
Jens Wiklander32b31802023-10-06 16:59:46 +02001624static void des_ctx_free(void *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +02001625{
Jens Wiklander32b31802023-10-06 16:59:46 +02001626 mbedtls_des_free((mbedtls_des_context *) ctx);
1627 mbedtls_free(ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +02001628}
1629
Jens Wiklander32b31802023-10-06 16:59:46 +02001630static void *des3_ctx_alloc(void)
Jens Wiklander817466c2018-05-22 13:49:31 +02001631{
1632 mbedtls_des3_context *des3;
Jens Wiklander32b31802023-10-06 16:59:46 +02001633 des3 = mbedtls_calloc(1, sizeof(mbedtls_des3_context));
Jens Wiklander817466c2018-05-22 13:49:31 +02001634
Jens Wiklander32b31802023-10-06 16:59:46 +02001635 if (des3 == NULL) {
1636 return NULL;
1637 }
Jens Wiklander817466c2018-05-22 13:49:31 +02001638
Jens Wiklander32b31802023-10-06 16:59:46 +02001639 mbedtls_des3_init(des3);
Jens Wiklander817466c2018-05-22 13:49:31 +02001640
Jens Wiklander32b31802023-10-06 16:59:46 +02001641 return des3;
Jens Wiklander817466c2018-05-22 13:49:31 +02001642}
1643
Jens Wiklander32b31802023-10-06 16:59:46 +02001644static void des3_ctx_clone(void *dst, const void *src)
Edison Ai12484fc2018-12-19 15:36:28 +08001645{
Jens Wiklander32b31802023-10-06 16:59:46 +02001646 memcpy(dst, src, sizeof(mbedtls_des3_context));
Edison Ai12484fc2018-12-19 15:36:28 +08001647}
1648
Jens Wiklander32b31802023-10-06 16:59:46 +02001649static void des3_ctx_free(void *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +02001650{
Jens Wiklander32b31802023-10-06 16:59:46 +02001651 mbedtls_des3_free((mbedtls_des3_context *) ctx);
1652 mbedtls_free(ctx);
Jens Wiklander817466c2018-05-22 13:49:31 +02001653}
1654
1655static const mbedtls_cipher_base_t des_info = {
1656 MBEDTLS_CIPHER_ID_DES,
1657 des_crypt_ecb_wrap,
1658#if defined(MBEDTLS_CIPHER_MODE_CBC)
1659 des_crypt_cbc_wrap,
1660#endif
1661#if defined(MBEDTLS_CIPHER_MODE_CFB)
1662 NULL,
1663#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001664#if defined(MBEDTLS_CIPHER_MODE_OFB)
1665 NULL,
1666#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001667#if defined(MBEDTLS_CIPHER_MODE_CTR)
1668 NULL,
1669#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001670#if defined(MBEDTLS_CIPHER_MODE_XTS)
1671 NULL,
1672#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001673#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1674 NULL,
1675#endif
1676 des_setkey_enc_wrap,
1677 des_setkey_dec_wrap,
1678 des_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +08001679 des_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +02001680 des_ctx_free
1681};
1682
1683static const mbedtls_cipher_info_t des_ecb_info = {
1684 MBEDTLS_CIPHER_DES_ECB,
1685 MBEDTLS_MODE_ECB,
1686 MBEDTLS_KEY_LENGTH_DES,
1687 "DES-ECB",
Jerome Forissier79013242021-07-28 10:24:04 +02001688 0,
Jens Wiklander817466c2018-05-22 13:49:31 +02001689 0,
1690 8,
1691 &des_info
1692};
1693
1694#if defined(MBEDTLS_CIPHER_MODE_CBC)
1695static const mbedtls_cipher_info_t des_cbc_info = {
1696 MBEDTLS_CIPHER_DES_CBC,
1697 MBEDTLS_MODE_CBC,
1698 MBEDTLS_KEY_LENGTH_DES,
1699 "DES-CBC",
1700 8,
1701 0,
1702 8,
1703 &des_info
1704};
1705#endif /* MBEDTLS_CIPHER_MODE_CBC */
1706
1707static const mbedtls_cipher_base_t des_ede_info = {
1708 MBEDTLS_CIPHER_ID_DES,
1709 des3_crypt_ecb_wrap,
1710#if defined(MBEDTLS_CIPHER_MODE_CBC)
1711 des3_crypt_cbc_wrap,
1712#endif
1713#if defined(MBEDTLS_CIPHER_MODE_CFB)
1714 NULL,
1715#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001716#if defined(MBEDTLS_CIPHER_MODE_OFB)
1717 NULL,
1718#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001719#if defined(MBEDTLS_CIPHER_MODE_CTR)
1720 NULL,
1721#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001722#if defined(MBEDTLS_CIPHER_MODE_XTS)
1723 NULL,
1724#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001725#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1726 NULL,
1727#endif
1728 des3_set2key_enc_wrap,
1729 des3_set2key_dec_wrap,
1730 des3_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +08001731 des3_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +02001732 des3_ctx_free
1733};
1734
1735static const mbedtls_cipher_info_t des_ede_ecb_info = {
1736 MBEDTLS_CIPHER_DES_EDE_ECB,
1737 MBEDTLS_MODE_ECB,
1738 MBEDTLS_KEY_LENGTH_DES_EDE,
1739 "DES-EDE-ECB",
Jerome Forissier79013242021-07-28 10:24:04 +02001740 0,
Jens Wiklander817466c2018-05-22 13:49:31 +02001741 0,
1742 8,
1743 &des_ede_info
1744};
1745
1746#if defined(MBEDTLS_CIPHER_MODE_CBC)
1747static const mbedtls_cipher_info_t des_ede_cbc_info = {
1748 MBEDTLS_CIPHER_DES_EDE_CBC,
1749 MBEDTLS_MODE_CBC,
1750 MBEDTLS_KEY_LENGTH_DES_EDE,
1751 "DES-EDE-CBC",
1752 8,
1753 0,
1754 8,
1755 &des_ede_info
1756};
1757#endif /* MBEDTLS_CIPHER_MODE_CBC */
1758
1759static const mbedtls_cipher_base_t des_ede3_info = {
1760 MBEDTLS_CIPHER_ID_3DES,
1761 des3_crypt_ecb_wrap,
1762#if defined(MBEDTLS_CIPHER_MODE_CBC)
1763 des3_crypt_cbc_wrap,
1764#endif
1765#if defined(MBEDTLS_CIPHER_MODE_CFB)
1766 NULL,
1767#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001768#if defined(MBEDTLS_CIPHER_MODE_OFB)
1769 NULL,
1770#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001771#if defined(MBEDTLS_CIPHER_MODE_CTR)
1772 NULL,
1773#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001774#if defined(MBEDTLS_CIPHER_MODE_XTS)
1775 NULL,
1776#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02001777#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1778 NULL,
1779#endif
1780 des3_set3key_enc_wrap,
1781 des3_set3key_dec_wrap,
1782 des3_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +08001783 des3_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +02001784 des3_ctx_free
1785};
1786
1787static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1788 MBEDTLS_CIPHER_DES_EDE3_ECB,
1789 MBEDTLS_MODE_ECB,
1790 MBEDTLS_KEY_LENGTH_DES_EDE3,
1791 "DES-EDE3-ECB",
Jerome Forissier79013242021-07-28 10:24:04 +02001792 0,
Jens Wiklander817466c2018-05-22 13:49:31 +02001793 0,
1794 8,
1795 &des_ede3_info
1796};
1797#if defined(MBEDTLS_CIPHER_MODE_CBC)
1798static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1799 MBEDTLS_CIPHER_DES_EDE3_CBC,
1800 MBEDTLS_MODE_CBC,
1801 MBEDTLS_KEY_LENGTH_DES_EDE3,
1802 "DES-EDE3-CBC",
1803 8,
1804 0,
1805 8,
1806 &des_ede3_info
1807};
1808#endif /* MBEDTLS_CIPHER_MODE_CBC */
1809#endif /* MBEDTLS_DES_C */
1810
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001811#if defined(MBEDTLS_CHACHA20_C)
1812
Jens Wiklander32b31802023-10-06 16:59:46 +02001813static int chacha20_setkey_wrap(void *ctx, const unsigned char *key,
1814 unsigned int key_bitlen)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001815{
Jens Wiklander32b31802023-10-06 16:59:46 +02001816 if (key_bitlen != 256U) {
1817 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1818 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001819
Jens Wiklander32b31802023-10-06 16:59:46 +02001820 if (0 != mbedtls_chacha20_setkey((mbedtls_chacha20_context *) ctx, key)) {
1821 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1822 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001823
Jens Wiklander32b31802023-10-06 16:59:46 +02001824 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001825}
1826
Jens Wiklander32b31802023-10-06 16:59:46 +02001827static int chacha20_stream_wrap(void *ctx, size_t length,
1828 const unsigned char *input,
1829 unsigned char *output)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001830{
Jerome Forissier11fa71b2020-04-20 17:17:56 +02001831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001832
Jens Wiklander32b31802023-10-06 16:59:46 +02001833 ret = mbedtls_chacha20_update(ctx, length, input, output);
1834 if (ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA) {
1835 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1836 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001837
Jens Wiklander32b31802023-10-06 16:59:46 +02001838 return ret;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001839}
1840
Jens Wiklander32b31802023-10-06 16:59:46 +02001841static void *chacha20_ctx_alloc(void)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001842{
1843 mbedtls_chacha20_context *ctx;
Jens Wiklander32b31802023-10-06 16:59:46 +02001844 ctx = mbedtls_calloc(1, sizeof(mbedtls_chacha20_context));
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001845
Jens Wiklander32b31802023-10-06 16:59:46 +02001846 if (ctx == NULL) {
1847 return NULL;
1848 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001849
Jens Wiklander32b31802023-10-06 16:59:46 +02001850 mbedtls_chacha20_init(ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001851
Jens Wiklander32b31802023-10-06 16:59:46 +02001852 return ctx;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001853}
1854
Jens Wiklander32b31802023-10-06 16:59:46 +02001855static void chacha20_ctx_clone(void *dst, const void *src)
Simon Ott23ef3872022-10-26 11:38:24 +02001856{
Jens Wiklander32b31802023-10-06 16:59:46 +02001857 memcpy(dst, src, sizeof(mbedtls_chacha20_context));
Simon Ott23ef3872022-10-26 11:38:24 +02001858}
1859
Jens Wiklander32b31802023-10-06 16:59:46 +02001860static void chacha20_ctx_free(void *ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001861{
Jens Wiklander32b31802023-10-06 16:59:46 +02001862 mbedtls_chacha20_free((mbedtls_chacha20_context *) ctx);
1863 mbedtls_free(ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001864}
1865
1866static const mbedtls_cipher_base_t chacha20_base_info = {
1867 MBEDTLS_CIPHER_ID_CHACHA20,
1868 NULL,
1869#if defined(MBEDTLS_CIPHER_MODE_CBC)
1870 NULL,
1871#endif
1872#if defined(MBEDTLS_CIPHER_MODE_CFB)
1873 NULL,
1874#endif
1875#if defined(MBEDTLS_CIPHER_MODE_OFB)
1876 NULL,
1877#endif
1878#if defined(MBEDTLS_CIPHER_MODE_CTR)
1879 NULL,
1880#endif
1881#if defined(MBEDTLS_CIPHER_MODE_XTS)
1882 NULL,
1883#endif
1884#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1885 chacha20_stream_wrap,
1886#endif
1887 chacha20_setkey_wrap,
1888 chacha20_setkey_wrap,
1889 chacha20_ctx_alloc,
Simon Ott23ef3872022-10-26 11:38:24 +02001890 chacha20_ctx_clone,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001891 chacha20_ctx_free
1892};
1893static const mbedtls_cipher_info_t chacha20_info = {
1894 MBEDTLS_CIPHER_CHACHA20,
1895 MBEDTLS_MODE_STREAM,
1896 256,
1897 "CHACHA20",
1898 12,
1899 0,
1900 1,
1901 &chacha20_base_info
1902};
1903#endif /* MBEDTLS_CHACHA20_C */
1904
1905#if defined(MBEDTLS_CHACHAPOLY_C)
1906
Jens Wiklander32b31802023-10-06 16:59:46 +02001907static int chachapoly_setkey_wrap(void *ctx,
1908 const unsigned char *key,
1909 unsigned int key_bitlen)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001910{
Jens Wiklander32b31802023-10-06 16:59:46 +02001911 if (key_bitlen != 256U) {
1912 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1913 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001914
Jens Wiklander32b31802023-10-06 16:59:46 +02001915 if (0 != mbedtls_chachapoly_setkey((mbedtls_chachapoly_context *) ctx, key)) {
1916 return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1917 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001918
Jens Wiklander32b31802023-10-06 16:59:46 +02001919 return 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001920}
1921
Jens Wiklander32b31802023-10-06 16:59:46 +02001922static void *chachapoly_ctx_alloc(void)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001923{
1924 mbedtls_chachapoly_context *ctx;
Jens Wiklander32b31802023-10-06 16:59:46 +02001925 ctx = mbedtls_calloc(1, sizeof(mbedtls_chachapoly_context));
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001926
Jens Wiklander32b31802023-10-06 16:59:46 +02001927 if (ctx == NULL) {
1928 return NULL;
1929 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001930
Jens Wiklander32b31802023-10-06 16:59:46 +02001931 mbedtls_chachapoly_init(ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001932
Jens Wiklander32b31802023-10-06 16:59:46 +02001933 return ctx;
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001934}
1935
Jens Wiklander32b31802023-10-06 16:59:46 +02001936static void chachapoly_ctx_clone(void *dst, const void *src)
Simon Ott23ef3872022-10-26 11:38:24 +02001937{
Jens Wiklander32b31802023-10-06 16:59:46 +02001938 memcpy(dst, src, sizeof(mbedtls_chachapoly_context));
Simon Ott23ef3872022-10-26 11:38:24 +02001939}
1940
Jens Wiklander32b31802023-10-06 16:59:46 +02001941static void chachapoly_ctx_free(void *ctx)
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001942{
Jens Wiklander32b31802023-10-06 16:59:46 +02001943 mbedtls_chachapoly_free((mbedtls_chachapoly_context *) ctx);
1944 mbedtls_free(ctx);
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001945}
1946
1947static const mbedtls_cipher_base_t chachapoly_base_info = {
1948 MBEDTLS_CIPHER_ID_CHACHA20,
1949 NULL,
1950#if defined(MBEDTLS_CIPHER_MODE_CBC)
1951 NULL,
1952#endif
1953#if defined(MBEDTLS_CIPHER_MODE_CFB)
1954 NULL,
1955#endif
1956#if defined(MBEDTLS_CIPHER_MODE_OFB)
1957 NULL,
1958#endif
1959#if defined(MBEDTLS_CIPHER_MODE_CTR)
1960 NULL,
1961#endif
1962#if defined(MBEDTLS_CIPHER_MODE_XTS)
1963 NULL,
1964#endif
1965#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1966 NULL,
1967#endif
1968 chachapoly_setkey_wrap,
1969 chachapoly_setkey_wrap,
1970 chachapoly_ctx_alloc,
Simon Ott23ef3872022-10-26 11:38:24 +02001971 chachapoly_ctx_clone,
Jens Wiklander3d3b0592019-03-20 15:30:29 +01001972 chachapoly_ctx_free
1973};
1974static const mbedtls_cipher_info_t chachapoly_info = {
1975 MBEDTLS_CIPHER_CHACHA20_POLY1305,
1976 MBEDTLS_MODE_CHACHAPOLY,
1977 256,
1978 "CHACHA20-POLY1305",
1979 12,
1980 0,
1981 1,
1982 &chachapoly_base_info
1983};
1984#endif /* MBEDTLS_CHACHAPOLY_C */
1985
Jens Wiklander817466c2018-05-22 13:49:31 +02001986#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
Jens Wiklander32b31802023-10-06 16:59:46 +02001987static int null_crypt_stream(void *ctx, size_t length,
1988 const unsigned char *input,
1989 unsigned char *output)
Jens Wiklander817466c2018-05-22 13:49:31 +02001990{
1991 ((void) ctx);
Jens Wiklander32b31802023-10-06 16:59:46 +02001992 memmove(output, input, length);
1993 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02001994}
1995
Jens Wiklander32b31802023-10-06 16:59:46 +02001996static int null_setkey(void *ctx, const unsigned char *key,
1997 unsigned int key_bitlen)
Jens Wiklander817466c2018-05-22 13:49:31 +02001998{
1999 ((void) ctx);
2000 ((void) key);
2001 ((void) key_bitlen);
2002
Jens Wiklander32b31802023-10-06 16:59:46 +02002003 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +02002004}
2005
Jens Wiklander32b31802023-10-06 16:59:46 +02002006static void *null_ctx_alloc(void)
Jens Wiklander817466c2018-05-22 13:49:31 +02002007{
Jens Wiklander32b31802023-10-06 16:59:46 +02002008 return (void *) 1;
Jens Wiklander817466c2018-05-22 13:49:31 +02002009}
2010
Jens Wiklander32b31802023-10-06 16:59:46 +02002011static void null_ctx_clone(void *dst, const void *src)
Edison Ai12484fc2018-12-19 15:36:28 +08002012{
2013 ((void) dst);
2014 ((void) src);
2015}
2016
Jens Wiklander32b31802023-10-06 16:59:46 +02002017static void null_ctx_free(void *ctx)
Jens Wiklander817466c2018-05-22 13:49:31 +02002018{
2019 ((void) ctx);
2020}
2021
2022static const mbedtls_cipher_base_t null_base_info = {
2023 MBEDTLS_CIPHER_ID_NULL,
2024 NULL,
2025#if defined(MBEDTLS_CIPHER_MODE_CBC)
2026 NULL,
2027#endif
2028#if defined(MBEDTLS_CIPHER_MODE_CFB)
2029 NULL,
2030#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002031#if defined(MBEDTLS_CIPHER_MODE_OFB)
2032 NULL,
2033#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002034#if defined(MBEDTLS_CIPHER_MODE_CTR)
2035 NULL,
2036#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002037#if defined(MBEDTLS_CIPHER_MODE_XTS)
2038 NULL,
2039#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002040#if defined(MBEDTLS_CIPHER_MODE_STREAM)
2041 null_crypt_stream,
2042#endif
2043 null_setkey,
2044 null_setkey,
2045 null_ctx_alloc,
Edison Ai12484fc2018-12-19 15:36:28 +08002046 null_ctx_clone,
Jens Wiklander817466c2018-05-22 13:49:31 +02002047 null_ctx_free
2048};
2049
2050static const mbedtls_cipher_info_t null_cipher_info = {
2051 MBEDTLS_CIPHER_NULL,
2052 MBEDTLS_MODE_STREAM,
2053 0,
2054 "NULL",
2055 0,
2056 0,
2057 1,
2058 &null_base_info
2059};
2060#endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
2061
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002062#if defined(MBEDTLS_NIST_KW_C)
Jens Wiklander32b31802023-10-06 16:59:46 +02002063static void *kw_ctx_alloc(void)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002064{
Jens Wiklander32b31802023-10-06 16:59:46 +02002065 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_nist_kw_context));
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002066
Jens Wiklander32b31802023-10-06 16:59:46 +02002067 if (ctx != NULL) {
2068 mbedtls_nist_kw_init((mbedtls_nist_kw_context *) ctx);
2069 }
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002070
Jens Wiklander32b31802023-10-06 16:59:46 +02002071 return ctx;
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002072}
2073
Jens Wiklander32b31802023-10-06 16:59:46 +02002074static void kw_ctx_clone(void *dst, const void *src)
Sergiy Kibrike5353ad2022-11-14 08:47:11 +00002075{
Jens Wiklander32b31802023-10-06 16:59:46 +02002076 memcpy(dst, src, sizeof(mbedtls_nist_kw_context));
Sergiy Kibrike5353ad2022-11-14 08:47:11 +00002077}
2078
Jens Wiklander32b31802023-10-06 16:59:46 +02002079static void kw_ctx_free(void *ctx)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002080{
Jens Wiklander32b31802023-10-06 16:59:46 +02002081 mbedtls_nist_kw_free(ctx);
2082 mbedtls_free(ctx);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002083}
2084
Jens Wiklander32b31802023-10-06 16:59:46 +02002085static int kw_aes_setkey_wrap(void *ctx, const unsigned char *key,
2086 unsigned int key_bitlen)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002087{
Jens Wiklander32b31802023-10-06 16:59:46 +02002088 return mbedtls_nist_kw_setkey((mbedtls_nist_kw_context *) ctx,
2089 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 1);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002090}
2091
Jens Wiklander32b31802023-10-06 16:59:46 +02002092static int kw_aes_setkey_unwrap(void *ctx, const unsigned char *key,
2093 unsigned int key_bitlen)
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002094{
Jens Wiklander32b31802023-10-06 16:59:46 +02002095 return mbedtls_nist_kw_setkey((mbedtls_nist_kw_context *) ctx,
2096 MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 0);
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002097}
2098
2099static const mbedtls_cipher_base_t kw_aes_info = {
2100 MBEDTLS_CIPHER_ID_AES,
2101 NULL,
2102#if defined(MBEDTLS_CIPHER_MODE_CBC)
2103 NULL,
2104#endif
2105#if defined(MBEDTLS_CIPHER_MODE_CFB)
2106 NULL,
2107#endif
2108#if defined(MBEDTLS_CIPHER_MODE_OFB)
2109 NULL,
2110#endif
2111#if defined(MBEDTLS_CIPHER_MODE_CTR)
2112 NULL,
2113#endif
2114#if defined(MBEDTLS_CIPHER_MODE_XTS)
2115 NULL,
2116#endif
2117#if defined(MBEDTLS_CIPHER_MODE_STREAM)
2118 NULL,
2119#endif
2120 kw_aes_setkey_wrap,
2121 kw_aes_setkey_unwrap,
2122 kw_ctx_alloc,
Sergiy Kibrike5353ad2022-11-14 08:47:11 +00002123 kw_ctx_clone,
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002124 kw_ctx_free,
2125};
2126
2127static const mbedtls_cipher_info_t aes_128_nist_kw_info = {
2128 MBEDTLS_CIPHER_AES_128_KW,
2129 MBEDTLS_MODE_KW,
2130 128,
2131 "AES-128-KW",
2132 0,
2133 0,
2134 16,
2135 &kw_aes_info
2136};
2137
2138static const mbedtls_cipher_info_t aes_192_nist_kw_info = {
2139 MBEDTLS_CIPHER_AES_192_KW,
2140 MBEDTLS_MODE_KW,
2141 192,
2142 "AES-192-KW",
2143 0,
2144 0,
2145 16,
2146 &kw_aes_info
2147};
2148
2149static const mbedtls_cipher_info_t aes_256_nist_kw_info = {
2150 MBEDTLS_CIPHER_AES_256_KW,
2151 MBEDTLS_MODE_KW,
2152 256,
2153 "AES-256-KW",
2154 0,
2155 0,
2156 16,
2157 &kw_aes_info
2158};
2159
2160static const mbedtls_cipher_info_t aes_128_nist_kwp_info = {
2161 MBEDTLS_CIPHER_AES_128_KWP,
2162 MBEDTLS_MODE_KWP,
2163 128,
2164 "AES-128-KWP",
2165 0,
2166 0,
2167 16,
2168 &kw_aes_info
2169};
2170
2171static const mbedtls_cipher_info_t aes_192_nist_kwp_info = {
2172 MBEDTLS_CIPHER_AES_192_KWP,
2173 MBEDTLS_MODE_KWP,
2174 192,
2175 "AES-192-KWP",
2176 0,
2177 0,
2178 16,
2179 &kw_aes_info
2180};
2181
2182static const mbedtls_cipher_info_t aes_256_nist_kwp_info = {
2183 MBEDTLS_CIPHER_AES_256_KWP,
2184 MBEDTLS_MODE_KWP,
2185 256,
2186 "AES-256-KWP",
2187 0,
2188 0,
2189 16,
2190 &kw_aes_info
2191};
2192#endif /* MBEDTLS_NIST_KW_C */
2193
Jens Wiklander817466c2018-05-22 13:49:31 +02002194const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
2195{
2196#if defined(MBEDTLS_AES_C)
2197 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
2198 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
2199 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
2200#if defined(MBEDTLS_CIPHER_MODE_CBC)
2201 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
2202 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
2203 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
2204#endif
2205#if defined(MBEDTLS_CIPHER_MODE_CFB)
2206 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
2207 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
2208 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
2209#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002210#if defined(MBEDTLS_CIPHER_MODE_OFB)
2211 { MBEDTLS_CIPHER_AES_128_OFB, &aes_128_ofb_info },
2212 { MBEDTLS_CIPHER_AES_192_OFB, &aes_192_ofb_info },
2213 { MBEDTLS_CIPHER_AES_256_OFB, &aes_256_ofb_info },
2214#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002215#if defined(MBEDTLS_CIPHER_MODE_CTR)
2216 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
2217 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
2218 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
2219#endif
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002220#if defined(MBEDTLS_CIPHER_MODE_XTS)
2221 { MBEDTLS_CIPHER_AES_128_XTS, &aes_128_xts_info },
2222 { MBEDTLS_CIPHER_AES_256_XTS, &aes_256_xts_info },
2223#endif
Jens Wiklander817466c2018-05-22 13:49:31 +02002224#if defined(MBEDTLS_GCM_C)
2225 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
2226 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
2227 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
2228#endif
2229#if defined(MBEDTLS_CCM_C)
2230 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
2231 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
2232 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
Jens Wiklander32b31802023-10-06 16:59:46 +02002233 { MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG, &aes_128_ccm_star_no_tag_info },
2234 { MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG, &aes_192_ccm_star_no_tag_info },
2235 { MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG, &aes_256_ccm_star_no_tag_info },
Jens Wiklander817466c2018-05-22 13:49:31 +02002236#endif
2237#endif /* MBEDTLS_AES_C */
2238
Jens Wiklander817466c2018-05-22 13:49:31 +02002239#if defined(MBEDTLS_CAMELLIA_C)
2240 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
2241 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
2242 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
2243#if defined(MBEDTLS_CIPHER_MODE_CBC)
2244 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
2245 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
2246 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
2247#endif
2248#if defined(MBEDTLS_CIPHER_MODE_CFB)
2249 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
2250 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
2251 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
2252#endif
2253#if defined(MBEDTLS_CIPHER_MODE_CTR)
2254 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
2255 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
2256 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
2257#endif
2258#if defined(MBEDTLS_GCM_C)
2259 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
2260 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
2261 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
2262#endif
2263#if defined(MBEDTLS_CCM_C)
2264 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
2265 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
2266 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
Jens Wiklander32b31802023-10-06 16:59:46 +02002267 { MBEDTLS_CIPHER_CAMELLIA_128_CCM_STAR_NO_TAG, &camellia_128_ccm_star_no_tag_info },
2268 { MBEDTLS_CIPHER_CAMELLIA_192_CCM_STAR_NO_TAG, &camellia_192_ccm_star_no_tag_info },
2269 { MBEDTLS_CIPHER_CAMELLIA_256_CCM_STAR_NO_TAG, &camellia_256_ccm_star_no_tag_info },
Jens Wiklander817466c2018-05-22 13:49:31 +02002270#endif
2271#endif /* MBEDTLS_CAMELLIA_C */
2272
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002273#if defined(MBEDTLS_ARIA_C)
2274 { MBEDTLS_CIPHER_ARIA_128_ECB, &aria_128_ecb_info },
2275 { MBEDTLS_CIPHER_ARIA_192_ECB, &aria_192_ecb_info },
2276 { MBEDTLS_CIPHER_ARIA_256_ECB, &aria_256_ecb_info },
2277#if defined(MBEDTLS_CIPHER_MODE_CBC)
2278 { MBEDTLS_CIPHER_ARIA_128_CBC, &aria_128_cbc_info },
2279 { MBEDTLS_CIPHER_ARIA_192_CBC, &aria_192_cbc_info },
2280 { MBEDTLS_CIPHER_ARIA_256_CBC, &aria_256_cbc_info },
2281#endif
2282#if defined(MBEDTLS_CIPHER_MODE_CFB)
2283 { MBEDTLS_CIPHER_ARIA_128_CFB128, &aria_128_cfb128_info },
2284 { MBEDTLS_CIPHER_ARIA_192_CFB128, &aria_192_cfb128_info },
2285 { MBEDTLS_CIPHER_ARIA_256_CFB128, &aria_256_cfb128_info },
2286#endif
2287#if defined(MBEDTLS_CIPHER_MODE_CTR)
2288 { MBEDTLS_CIPHER_ARIA_128_CTR, &aria_128_ctr_info },
2289 { MBEDTLS_CIPHER_ARIA_192_CTR, &aria_192_ctr_info },
2290 { MBEDTLS_CIPHER_ARIA_256_CTR, &aria_256_ctr_info },
2291#endif
2292#if defined(MBEDTLS_GCM_C)
2293 { MBEDTLS_CIPHER_ARIA_128_GCM, &aria_128_gcm_info },
2294 { MBEDTLS_CIPHER_ARIA_192_GCM, &aria_192_gcm_info },
2295 { MBEDTLS_CIPHER_ARIA_256_GCM, &aria_256_gcm_info },
2296#endif
2297#if defined(MBEDTLS_CCM_C)
2298 { MBEDTLS_CIPHER_ARIA_128_CCM, &aria_128_ccm_info },
2299 { MBEDTLS_CIPHER_ARIA_192_CCM, &aria_192_ccm_info },
2300 { MBEDTLS_CIPHER_ARIA_256_CCM, &aria_256_ccm_info },
Jens Wiklander32b31802023-10-06 16:59:46 +02002301 { MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG, &aria_128_ccm_star_no_tag_info },
2302 { MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG, &aria_192_ccm_star_no_tag_info },
2303 { MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG, &aria_256_ccm_star_no_tag_info },
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002304#endif
2305#endif /* MBEDTLS_ARIA_C */
2306
Jens Wiklander817466c2018-05-22 13:49:31 +02002307#if defined(MBEDTLS_DES_C)
2308 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
2309 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
2310 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
2311#if defined(MBEDTLS_CIPHER_MODE_CBC)
2312 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
2313 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
2314 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
2315#endif
2316#endif /* MBEDTLS_DES_C */
2317
Jens Wiklander3d3b0592019-03-20 15:30:29 +01002318#if defined(MBEDTLS_CHACHA20_C)
2319 { MBEDTLS_CIPHER_CHACHA20, &chacha20_info },
2320#endif
2321
2322#if defined(MBEDTLS_CHACHAPOLY_C)
2323 { MBEDTLS_CIPHER_CHACHA20_POLY1305, &chachapoly_info },
2324#endif
2325
Jerome Forissier11fa71b2020-04-20 17:17:56 +02002326#if defined(MBEDTLS_NIST_KW_C)
2327 { MBEDTLS_CIPHER_AES_128_KW, &aes_128_nist_kw_info },
2328 { MBEDTLS_CIPHER_AES_192_KW, &aes_192_nist_kw_info },
2329 { MBEDTLS_CIPHER_AES_256_KW, &aes_256_nist_kw_info },
2330 { MBEDTLS_CIPHER_AES_128_KWP, &aes_128_nist_kwp_info },
2331 { MBEDTLS_CIPHER_AES_192_KWP, &aes_192_nist_kwp_info },
2332 { MBEDTLS_CIPHER_AES_256_KWP, &aes_256_nist_kwp_info },
2333#endif
2334
Jens Wiklander817466c2018-05-22 13:49:31 +02002335#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2336 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
2337#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
2338
2339 { MBEDTLS_CIPHER_NONE, NULL }
2340};
2341
Jens Wiklander32b31802023-10-06 16:59:46 +02002342#define NUM_CIPHERS (sizeof(mbedtls_cipher_definitions) / \
2343 sizeof(mbedtls_cipher_definitions[0]))
Jens Wiklander817466c2018-05-22 13:49:31 +02002344int mbedtls_cipher_supported[NUM_CIPHERS];
2345
2346#endif /* MBEDTLS_CIPHER_C */