Coverage Report

Created: 2025-07-01 06:54

/work/mbedtls-2.28.8/library/cipher_wrap.c
Line
Count
Source (jump to first uncovered line)
1
/**
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
 *
8
 *  Copyright The Mbed TLS Contributors
9
 *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
10
 */
11
12
#include "common.h"
13
14
#if defined(MBEDTLS_CIPHER_C)
15
16
#include "mbedtls/cipher_internal.h"
17
#include "mbedtls/error.h"
18
19
#if defined(MBEDTLS_CHACHAPOLY_C)
20
#include "mbedtls/chachapoly.h"
21
#endif
22
23
#if defined(MBEDTLS_AES_C)
24
#include "mbedtls/aes.h"
25
#endif
26
27
#if defined(MBEDTLS_ARC4_C)
28
#include "mbedtls/arc4.h"
29
#endif
30
31
#if defined(MBEDTLS_CAMELLIA_C)
32
#include "mbedtls/camellia.h"
33
#endif
34
35
#if defined(MBEDTLS_ARIA_C)
36
#include "mbedtls/aria.h"
37
#endif
38
39
#if defined(MBEDTLS_DES_C)
40
#include "mbedtls/des.h"
41
#endif
42
43
#if defined(MBEDTLS_BLOWFISH_C)
44
#include "mbedtls/blowfish.h"
45
#endif
46
47
#if defined(MBEDTLS_CHACHA20_C)
48
#include "mbedtls/chacha20.h"
49
#endif
50
51
#if defined(MBEDTLS_GCM_C)
52
#include "mbedtls/gcm.h"
53
#endif
54
55
#if defined(MBEDTLS_CCM_C)
56
#include "mbedtls/ccm.h"
57
#endif
58
59
#if defined(MBEDTLS_NIST_KW_C)
60
#include "mbedtls/nist_kw.h"
61
#endif
62
63
#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
64
#include <string.h>
65
#endif
66
67
#include "mbedtls/platform.h"
68
69
#if defined(MBEDTLS_GCM_C)
70
/* shared by all GCM ciphers */
71
static void *gcm_ctx_alloc(void)
72
0
{
73
0
    void *ctx = mbedtls_calloc(1, sizeof(mbedtls_gcm_context));
74
75
0
    if (ctx != NULL) {
76
0
        mbedtls_gcm_init((mbedtls_gcm_context *) ctx);
77
0
    }
78
79
0
    return ctx;
80
0
}
81
82
static void gcm_ctx_free(void *ctx)
83
0
{
84
0
    mbedtls_gcm_free(ctx);
85
0
    mbedtls_free(ctx);
86
0
}
87
#endif /* MBEDTLS_GCM_C */
88
89
#if defined(MBEDTLS_CCM_C)
90
/* shared by all CCM ciphers */
91
static void *ccm_ctx_alloc(void)
92
0
{
93
0
    void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ccm_context));
94
95
0
    if (ctx != NULL) {
96
0
        mbedtls_ccm_init((mbedtls_ccm_context *) ctx);
97
0
    }
98
99
0
    return ctx;
100
0
}
101
102
static void ccm_ctx_free(void *ctx)
103
0
{
104
0
    mbedtls_ccm_free(ctx);
105
0
    mbedtls_free(ctx);
106
0
}
107
#endif /* MBEDTLS_CCM_C */
108
109
#if defined(MBEDTLS_AES_C)
110
111
static int aes_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
112
                              const unsigned char *input, unsigned char *output)
113
0
{
114
0
    return mbedtls_aes_crypt_ecb((mbedtls_aes_context *) ctx, operation, input, output);
115
0
}
116
117
#if defined(MBEDTLS_CIPHER_MODE_CBC)
118
static int aes_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
119
                              unsigned char *iv, const unsigned char *input, unsigned char *output)
120
0
{
121
0
    return mbedtls_aes_crypt_cbc((mbedtls_aes_context *) ctx, operation, length, iv, input,
122
0
                                 output);
123
0
}
124
#endif /* MBEDTLS_CIPHER_MODE_CBC */
125
126
#if defined(MBEDTLS_CIPHER_MODE_CFB)
127
static int aes_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
128
                                 size_t length, size_t *iv_off, unsigned char *iv,
129
                                 const unsigned char *input, unsigned char *output)
130
0
{
131
0
    return mbedtls_aes_crypt_cfb128((mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
132
0
                                    input, output);
133
0
}
134
#endif /* MBEDTLS_CIPHER_MODE_CFB */
135
136
#if defined(MBEDTLS_CIPHER_MODE_OFB)
137
static int aes_crypt_ofb_wrap(void *ctx, size_t length, size_t *iv_off,
138
                              unsigned char *iv, const unsigned char *input, unsigned char *output)
139
0
{
140
0
    return mbedtls_aes_crypt_ofb((mbedtls_aes_context *) ctx, length, iv_off,
141
0
                                 iv, input, output);
142
0
}
143
#endif /* MBEDTLS_CIPHER_MODE_OFB */
144
145
#if defined(MBEDTLS_CIPHER_MODE_CTR)
146
static int aes_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
147
                              unsigned char *nonce_counter, unsigned char *stream_block,
148
                              const unsigned char *input, unsigned char *output)
149
0
{
150
0
    return mbedtls_aes_crypt_ctr((mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
151
0
                                 stream_block, input, output);
152
0
}
153
#endif /* MBEDTLS_CIPHER_MODE_CTR */
154
155
#if defined(MBEDTLS_CIPHER_MODE_XTS)
156
static int aes_crypt_xts_wrap(void *ctx, mbedtls_operation_t operation,
157
                              size_t length,
158
                              const unsigned char data_unit[16],
159
                              const unsigned char *input,
160
                              unsigned char *output)
161
0
{
162
0
    mbedtls_aes_xts_context *xts_ctx = ctx;
163
0
    int mode;
164
165
0
    switch (operation) {
166
0
        case MBEDTLS_ENCRYPT:
167
0
            mode = MBEDTLS_AES_ENCRYPT;
168
0
            break;
169
0
        case MBEDTLS_DECRYPT:
170
0
            mode = MBEDTLS_AES_DECRYPT;
171
0
            break;
172
0
        default:
173
0
            return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
174
0
    }
175
176
0
    return mbedtls_aes_crypt_xts(xts_ctx, mode, length,
177
0
                                 data_unit, input, output);
178
0
}
179
#endif /* MBEDTLS_CIPHER_MODE_XTS */
180
181
static int aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
182
                               unsigned int key_bitlen)
183
0
{
184
0
    return mbedtls_aes_setkey_dec((mbedtls_aes_context *) ctx, key, key_bitlen);
185
0
}
186
187
static int aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
188
                               unsigned int key_bitlen)
189
0
{
190
0
    return mbedtls_aes_setkey_enc((mbedtls_aes_context *) ctx, key, key_bitlen);
191
0
}
192
193
static void *aes_ctx_alloc(void)
194
0
{
195
0
    mbedtls_aes_context *aes = mbedtls_calloc(1, sizeof(mbedtls_aes_context));
196
197
0
    if (aes == NULL) {
198
0
        return NULL;
199
0
    }
200
201
0
    mbedtls_aes_init(aes);
202
203
0
    return aes;
204
0
}
205
206
static void aes_ctx_free(void *ctx)
207
0
{
208
0
    mbedtls_aes_free((mbedtls_aes_context *) ctx);
209
0
    mbedtls_free(ctx);
210
0
}
211
212
static const mbedtls_cipher_base_t aes_info = {
213
    MBEDTLS_CIPHER_ID_AES,
214
    aes_crypt_ecb_wrap,
215
#if defined(MBEDTLS_CIPHER_MODE_CBC)
216
    aes_crypt_cbc_wrap,
217
#endif
218
#if defined(MBEDTLS_CIPHER_MODE_CFB)
219
    aes_crypt_cfb128_wrap,
220
#endif
221
#if defined(MBEDTLS_CIPHER_MODE_OFB)
222
    aes_crypt_ofb_wrap,
223
#endif
224
#if defined(MBEDTLS_CIPHER_MODE_CTR)
225
    aes_crypt_ctr_wrap,
226
#endif
227
#if defined(MBEDTLS_CIPHER_MODE_XTS)
228
    NULL,
229
#endif
230
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
231
    NULL,
232
#endif
233
    aes_setkey_enc_wrap,
234
    aes_setkey_dec_wrap,
235
    aes_ctx_alloc,
236
    aes_ctx_free
237
};
238
239
static const mbedtls_cipher_info_t aes_128_ecb_info = {
240
    MBEDTLS_CIPHER_AES_128_ECB,
241
    MBEDTLS_MODE_ECB,
242
    128,
243
    "AES-128-ECB",
244
    0,
245
    0,
246
    16,
247
    &aes_info
248
};
249
250
static const mbedtls_cipher_info_t aes_192_ecb_info = {
251
    MBEDTLS_CIPHER_AES_192_ECB,
252
    MBEDTLS_MODE_ECB,
253
    192,
254
    "AES-192-ECB",
255
    0,
256
    0,
257
    16,
258
    &aes_info
259
};
260
261
static const mbedtls_cipher_info_t aes_256_ecb_info = {
262
    MBEDTLS_CIPHER_AES_256_ECB,
263
    MBEDTLS_MODE_ECB,
264
    256,
265
    "AES-256-ECB",
266
    0,
267
    0,
268
    16,
269
    &aes_info
270
};
271
272
#if defined(MBEDTLS_CIPHER_MODE_CBC)
273
static const mbedtls_cipher_info_t aes_128_cbc_info = {
274
    MBEDTLS_CIPHER_AES_128_CBC,
275
    MBEDTLS_MODE_CBC,
276
    128,
277
    "AES-128-CBC",
278
    16,
279
    0,
280
    16,
281
    &aes_info
282
};
283
284
static const mbedtls_cipher_info_t aes_192_cbc_info = {
285
    MBEDTLS_CIPHER_AES_192_CBC,
286
    MBEDTLS_MODE_CBC,
287
    192,
288
    "AES-192-CBC",
289
    16,
290
    0,
291
    16,
292
    &aes_info
293
};
294
295
static const mbedtls_cipher_info_t aes_256_cbc_info = {
296
    MBEDTLS_CIPHER_AES_256_CBC,
297
    MBEDTLS_MODE_CBC,
298
    256,
299
    "AES-256-CBC",
300
    16,
301
    0,
302
    16,
303
    &aes_info
304
};
305
#endif /* MBEDTLS_CIPHER_MODE_CBC */
306
307
#if defined(MBEDTLS_CIPHER_MODE_CFB)
308
static const mbedtls_cipher_info_t aes_128_cfb128_info = {
309
    MBEDTLS_CIPHER_AES_128_CFB128,
310
    MBEDTLS_MODE_CFB,
311
    128,
312
    "AES-128-CFB128",
313
    16,
314
    0,
315
    16,
316
    &aes_info
317
};
318
319
static const mbedtls_cipher_info_t aes_192_cfb128_info = {
320
    MBEDTLS_CIPHER_AES_192_CFB128,
321
    MBEDTLS_MODE_CFB,
322
    192,
323
    "AES-192-CFB128",
324
    16,
325
    0,
326
    16,
327
    &aes_info
328
};
329
330
static const mbedtls_cipher_info_t aes_256_cfb128_info = {
331
    MBEDTLS_CIPHER_AES_256_CFB128,
332
    MBEDTLS_MODE_CFB,
333
    256,
334
    "AES-256-CFB128",
335
    16,
336
    0,
337
    16,
338
    &aes_info
339
};
340
#endif /* MBEDTLS_CIPHER_MODE_CFB */
341
342
#if defined(MBEDTLS_CIPHER_MODE_OFB)
343
static const mbedtls_cipher_info_t aes_128_ofb_info = {
344
    MBEDTLS_CIPHER_AES_128_OFB,
345
    MBEDTLS_MODE_OFB,
346
    128,
347
    "AES-128-OFB",
348
    16,
349
    0,
350
    16,
351
    &aes_info
352
};
353
354
static const mbedtls_cipher_info_t aes_192_ofb_info = {
355
    MBEDTLS_CIPHER_AES_192_OFB,
356
    MBEDTLS_MODE_OFB,
357
    192,
358
    "AES-192-OFB",
359
    16,
360
    0,
361
    16,
362
    &aes_info
363
};
364
365
static const mbedtls_cipher_info_t aes_256_ofb_info = {
366
    MBEDTLS_CIPHER_AES_256_OFB,
367
    MBEDTLS_MODE_OFB,
368
    256,
369
    "AES-256-OFB",
370
    16,
371
    0,
372
    16,
373
    &aes_info
374
};
375
#endif /* MBEDTLS_CIPHER_MODE_OFB */
376
377
#if defined(MBEDTLS_CIPHER_MODE_CTR)
378
static const mbedtls_cipher_info_t aes_128_ctr_info = {
379
    MBEDTLS_CIPHER_AES_128_CTR,
380
    MBEDTLS_MODE_CTR,
381
    128,
382
    "AES-128-CTR",
383
    16,
384
    0,
385
    16,
386
    &aes_info
387
};
388
389
static const mbedtls_cipher_info_t aes_192_ctr_info = {
390
    MBEDTLS_CIPHER_AES_192_CTR,
391
    MBEDTLS_MODE_CTR,
392
    192,
393
    "AES-192-CTR",
394
    16,
395
    0,
396
    16,
397
    &aes_info
398
};
399
400
static const mbedtls_cipher_info_t aes_256_ctr_info = {
401
    MBEDTLS_CIPHER_AES_256_CTR,
402
    MBEDTLS_MODE_CTR,
403
    256,
404
    "AES-256-CTR",
405
    16,
406
    0,
407
    16,
408
    &aes_info
409
};
410
#endif /* MBEDTLS_CIPHER_MODE_CTR */
411
412
#if defined(MBEDTLS_CIPHER_MODE_XTS)
413
static int xts_aes_setkey_enc_wrap(void *ctx, const unsigned char *key,
414
                                   unsigned int key_bitlen)
415
0
{
416
0
    mbedtls_aes_xts_context *xts_ctx = ctx;
417
0
    return mbedtls_aes_xts_setkey_enc(xts_ctx, key, key_bitlen);
418
0
}
419
420
static int xts_aes_setkey_dec_wrap(void *ctx, const unsigned char *key,
421
                                   unsigned int key_bitlen)
422
0
{
423
0
    mbedtls_aes_xts_context *xts_ctx = ctx;
424
0
    return mbedtls_aes_xts_setkey_dec(xts_ctx, key, key_bitlen);
425
0
}
426
427
static void *xts_aes_ctx_alloc(void)
428
0
{
429
0
    mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc(1, sizeof(*xts_ctx));
430
431
0
    if (xts_ctx != NULL) {
432
0
        mbedtls_aes_xts_init(xts_ctx);
433
0
    }
434
435
0
    return xts_ctx;
436
0
}
437
438
static void xts_aes_ctx_free(void *ctx)
439
0
{
440
0
    mbedtls_aes_xts_context *xts_ctx = ctx;
441
442
0
    if (xts_ctx == NULL) {
443
0
        return;
444
0
    }
445
446
0
    mbedtls_aes_xts_free(xts_ctx);
447
0
    mbedtls_free(xts_ctx);
448
0
}
449
450
static const mbedtls_cipher_base_t xts_aes_info = {
451
    MBEDTLS_CIPHER_ID_AES,
452
    NULL,
453
#if defined(MBEDTLS_CIPHER_MODE_CBC)
454
    NULL,
455
#endif
456
#if defined(MBEDTLS_CIPHER_MODE_CFB)
457
    NULL,
458
#endif
459
#if defined(MBEDTLS_CIPHER_MODE_OFB)
460
    NULL,
461
#endif
462
#if defined(MBEDTLS_CIPHER_MODE_CTR)
463
    NULL,
464
#endif
465
#if defined(MBEDTLS_CIPHER_MODE_XTS)
466
    aes_crypt_xts_wrap,
467
#endif
468
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
469
    NULL,
470
#endif
471
    xts_aes_setkey_enc_wrap,
472
    xts_aes_setkey_dec_wrap,
473
    xts_aes_ctx_alloc,
474
    xts_aes_ctx_free
475
};
476
477
static const mbedtls_cipher_info_t aes_128_xts_info = {
478
    MBEDTLS_CIPHER_AES_128_XTS,
479
    MBEDTLS_MODE_XTS,
480
    256,
481
    "AES-128-XTS",
482
    16,
483
    0,
484
    16,
485
    &xts_aes_info
486
};
487
488
static const mbedtls_cipher_info_t aes_256_xts_info = {
489
    MBEDTLS_CIPHER_AES_256_XTS,
490
    MBEDTLS_MODE_XTS,
491
    512,
492
    "AES-256-XTS",
493
    16,
494
    0,
495
    16,
496
    &xts_aes_info
497
};
498
#endif /* MBEDTLS_CIPHER_MODE_XTS */
499
500
#if defined(MBEDTLS_GCM_C)
501
static int gcm_aes_setkey_wrap(void *ctx, const unsigned char *key,
502
                               unsigned int key_bitlen)
503
0
{
504
0
    return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
505
0
                              key, key_bitlen);
506
0
}
507
508
static const mbedtls_cipher_base_t gcm_aes_info = {
509
    MBEDTLS_CIPHER_ID_AES,
510
    NULL,
511
#if defined(MBEDTLS_CIPHER_MODE_CBC)
512
    NULL,
513
#endif
514
#if defined(MBEDTLS_CIPHER_MODE_CFB)
515
    NULL,
516
#endif
517
#if defined(MBEDTLS_CIPHER_MODE_OFB)
518
    NULL,
519
#endif
520
#if defined(MBEDTLS_CIPHER_MODE_CTR)
521
    NULL,
522
#endif
523
#if defined(MBEDTLS_CIPHER_MODE_XTS)
524
    NULL,
525
#endif
526
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
527
    NULL,
528
#endif
529
    gcm_aes_setkey_wrap,
530
    gcm_aes_setkey_wrap,
531
    gcm_ctx_alloc,
532
    gcm_ctx_free,
533
};
534
535
static const mbedtls_cipher_info_t aes_128_gcm_info = {
536
    MBEDTLS_CIPHER_AES_128_GCM,
537
    MBEDTLS_MODE_GCM,
538
    128,
539
    "AES-128-GCM",
540
    12,
541
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
542
    16,
543
    &gcm_aes_info
544
};
545
546
static const mbedtls_cipher_info_t aes_192_gcm_info = {
547
    MBEDTLS_CIPHER_AES_192_GCM,
548
    MBEDTLS_MODE_GCM,
549
    192,
550
    "AES-192-GCM",
551
    12,
552
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
553
    16,
554
    &gcm_aes_info
555
};
556
557
static const mbedtls_cipher_info_t aes_256_gcm_info = {
558
    MBEDTLS_CIPHER_AES_256_GCM,
559
    MBEDTLS_MODE_GCM,
560
    256,
561
    "AES-256-GCM",
562
    12,
563
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
564
    16,
565
    &gcm_aes_info
566
};
567
#endif /* MBEDTLS_GCM_C */
568
569
#if defined(MBEDTLS_CCM_C)
570
static int ccm_aes_setkey_wrap(void *ctx, const unsigned char *key,
571
                               unsigned int key_bitlen)
572
0
{
573
0
    return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
574
0
                              key, key_bitlen);
575
0
}
576
577
static const mbedtls_cipher_base_t ccm_aes_info = {
578
    MBEDTLS_CIPHER_ID_AES,
579
    NULL,
580
#if defined(MBEDTLS_CIPHER_MODE_CBC)
581
    NULL,
582
#endif
583
#if defined(MBEDTLS_CIPHER_MODE_CFB)
584
    NULL,
585
#endif
586
#if defined(MBEDTLS_CIPHER_MODE_OFB)
587
    NULL,
588
#endif
589
#if defined(MBEDTLS_CIPHER_MODE_CTR)
590
    NULL,
591
#endif
592
#if defined(MBEDTLS_CIPHER_MODE_XTS)
593
    NULL,
594
#endif
595
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
596
    NULL,
597
#endif
598
    ccm_aes_setkey_wrap,
599
    ccm_aes_setkey_wrap,
600
    ccm_ctx_alloc,
601
    ccm_ctx_free,
602
};
603
604
static const mbedtls_cipher_info_t aes_128_ccm_info = {
605
    MBEDTLS_CIPHER_AES_128_CCM,
606
    MBEDTLS_MODE_CCM,
607
    128,
608
    "AES-128-CCM",
609
    12,
610
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
611
    16,
612
    &ccm_aes_info
613
};
614
615
static const mbedtls_cipher_info_t aes_192_ccm_info = {
616
    MBEDTLS_CIPHER_AES_192_CCM,
617
    MBEDTLS_MODE_CCM,
618
    192,
619
    "AES-192-CCM",
620
    12,
621
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
622
    16,
623
    &ccm_aes_info
624
};
625
626
static const mbedtls_cipher_info_t aes_256_ccm_info = {
627
    MBEDTLS_CIPHER_AES_256_CCM,
628
    MBEDTLS_MODE_CCM,
629
    256,
630
    "AES-256-CCM",
631
    12,
632
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
633
    16,
634
    &ccm_aes_info
635
};
636
#endif /* MBEDTLS_CCM_C */
637
638
#endif /* MBEDTLS_AES_C */
639
640
#if defined(MBEDTLS_CAMELLIA_C)
641
642
static int camellia_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
643
                                   const unsigned char *input, unsigned char *output)
644
0
{
645
0
    return mbedtls_camellia_crypt_ecb((mbedtls_camellia_context *) ctx, operation, input,
646
0
                                      output);
647
0
}
648
649
#if defined(MBEDTLS_CIPHER_MODE_CBC)
650
static int camellia_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
651
                                   size_t length, unsigned char *iv,
652
                                   const unsigned char *input, unsigned char *output)
653
0
{
654
0
    return mbedtls_camellia_crypt_cbc((mbedtls_camellia_context *) ctx, operation, length, iv,
655
0
                                      input, output);
656
0
}
657
#endif /* MBEDTLS_CIPHER_MODE_CBC */
658
659
#if defined(MBEDTLS_CIPHER_MODE_CFB)
660
static int camellia_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
661
                                      size_t length, size_t *iv_off, unsigned char *iv,
662
                                      const unsigned char *input, unsigned char *output)
663
0
{
664
0
    return mbedtls_camellia_crypt_cfb128((mbedtls_camellia_context *) ctx, operation, length,
665
0
                                         iv_off, iv, input, output);
666
0
}
667
#endif /* MBEDTLS_CIPHER_MODE_CFB */
668
669
#if defined(MBEDTLS_CIPHER_MODE_CTR)
670
static int camellia_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
671
                                   unsigned char *nonce_counter, unsigned char *stream_block,
672
                                   const unsigned char *input, unsigned char *output)
673
0
{
674
0
    return mbedtls_camellia_crypt_ctr((mbedtls_camellia_context *) ctx, length, nc_off,
675
0
                                      nonce_counter, stream_block, input, output);
676
0
}
677
#endif /* MBEDTLS_CIPHER_MODE_CTR */
678
679
static int camellia_setkey_dec_wrap(void *ctx, const unsigned char *key,
680
                                    unsigned int key_bitlen)
681
0
{
682
0
    return mbedtls_camellia_setkey_dec((mbedtls_camellia_context *) ctx, key, key_bitlen);
683
0
}
684
685
static int camellia_setkey_enc_wrap(void *ctx, const unsigned char *key,
686
                                    unsigned int key_bitlen)
687
0
{
688
0
    return mbedtls_camellia_setkey_enc((mbedtls_camellia_context *) ctx, key, key_bitlen);
689
0
}
690
691
static void *camellia_ctx_alloc(void)
692
0
{
693
0
    mbedtls_camellia_context *ctx;
694
0
    ctx = mbedtls_calloc(1, sizeof(mbedtls_camellia_context));
695
696
0
    if (ctx == NULL) {
697
0
        return NULL;
698
0
    }
699
700
0
    mbedtls_camellia_init(ctx);
701
702
0
    return ctx;
703
0
}
704
705
static void camellia_ctx_free(void *ctx)
706
0
{
707
0
    mbedtls_camellia_free((mbedtls_camellia_context *) ctx);
708
0
    mbedtls_free(ctx);
709
0
}
710
711
static const mbedtls_cipher_base_t camellia_info = {
712
    MBEDTLS_CIPHER_ID_CAMELLIA,
713
    camellia_crypt_ecb_wrap,
714
#if defined(MBEDTLS_CIPHER_MODE_CBC)
715
    camellia_crypt_cbc_wrap,
716
#endif
717
#if defined(MBEDTLS_CIPHER_MODE_CFB)
718
    camellia_crypt_cfb128_wrap,
719
#endif
720
#if defined(MBEDTLS_CIPHER_MODE_OFB)
721
    NULL,
722
#endif
723
#if defined(MBEDTLS_CIPHER_MODE_CTR)
724
    camellia_crypt_ctr_wrap,
725
#endif
726
#if defined(MBEDTLS_CIPHER_MODE_XTS)
727
    NULL,
728
#endif
729
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
730
    NULL,
731
#endif
732
    camellia_setkey_enc_wrap,
733
    camellia_setkey_dec_wrap,
734
    camellia_ctx_alloc,
735
    camellia_ctx_free
736
};
737
738
static const mbedtls_cipher_info_t camellia_128_ecb_info = {
739
    MBEDTLS_CIPHER_CAMELLIA_128_ECB,
740
    MBEDTLS_MODE_ECB,
741
    128,
742
    "CAMELLIA-128-ECB",
743
    0,
744
    0,
745
    16,
746
    &camellia_info
747
};
748
749
static const mbedtls_cipher_info_t camellia_192_ecb_info = {
750
    MBEDTLS_CIPHER_CAMELLIA_192_ECB,
751
    MBEDTLS_MODE_ECB,
752
    192,
753
    "CAMELLIA-192-ECB",
754
    0,
755
    0,
756
    16,
757
    &camellia_info
758
};
759
760
static const mbedtls_cipher_info_t camellia_256_ecb_info = {
761
    MBEDTLS_CIPHER_CAMELLIA_256_ECB,
762
    MBEDTLS_MODE_ECB,
763
    256,
764
    "CAMELLIA-256-ECB",
765
    0,
766
    0,
767
    16,
768
    &camellia_info
769
};
770
771
#if defined(MBEDTLS_CIPHER_MODE_CBC)
772
static const mbedtls_cipher_info_t camellia_128_cbc_info = {
773
    MBEDTLS_CIPHER_CAMELLIA_128_CBC,
774
    MBEDTLS_MODE_CBC,
775
    128,
776
    "CAMELLIA-128-CBC",
777
    16,
778
    0,
779
    16,
780
    &camellia_info
781
};
782
783
static const mbedtls_cipher_info_t camellia_192_cbc_info = {
784
    MBEDTLS_CIPHER_CAMELLIA_192_CBC,
785
    MBEDTLS_MODE_CBC,
786
    192,
787
    "CAMELLIA-192-CBC",
788
    16,
789
    0,
790
    16,
791
    &camellia_info
792
};
793
794
static const mbedtls_cipher_info_t camellia_256_cbc_info = {
795
    MBEDTLS_CIPHER_CAMELLIA_256_CBC,
796
    MBEDTLS_MODE_CBC,
797
    256,
798
    "CAMELLIA-256-CBC",
799
    16,
800
    0,
801
    16,
802
    &camellia_info
803
};
804
#endif /* MBEDTLS_CIPHER_MODE_CBC */
805
806
#if defined(MBEDTLS_CIPHER_MODE_CFB)
807
static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
808
    MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
809
    MBEDTLS_MODE_CFB,
810
    128,
811
    "CAMELLIA-128-CFB128",
812
    16,
813
    0,
814
    16,
815
    &camellia_info
816
};
817
818
static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
819
    MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
820
    MBEDTLS_MODE_CFB,
821
    192,
822
    "CAMELLIA-192-CFB128",
823
    16,
824
    0,
825
    16,
826
    &camellia_info
827
};
828
829
static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
830
    MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
831
    MBEDTLS_MODE_CFB,
832
    256,
833
    "CAMELLIA-256-CFB128",
834
    16,
835
    0,
836
    16,
837
    &camellia_info
838
};
839
#endif /* MBEDTLS_CIPHER_MODE_CFB */
840
841
#if defined(MBEDTLS_CIPHER_MODE_CTR)
842
static const mbedtls_cipher_info_t camellia_128_ctr_info = {
843
    MBEDTLS_CIPHER_CAMELLIA_128_CTR,
844
    MBEDTLS_MODE_CTR,
845
    128,
846
    "CAMELLIA-128-CTR",
847
    16,
848
    0,
849
    16,
850
    &camellia_info
851
};
852
853
static const mbedtls_cipher_info_t camellia_192_ctr_info = {
854
    MBEDTLS_CIPHER_CAMELLIA_192_CTR,
855
    MBEDTLS_MODE_CTR,
856
    192,
857
    "CAMELLIA-192-CTR",
858
    16,
859
    0,
860
    16,
861
    &camellia_info
862
};
863
864
static const mbedtls_cipher_info_t camellia_256_ctr_info = {
865
    MBEDTLS_CIPHER_CAMELLIA_256_CTR,
866
    MBEDTLS_MODE_CTR,
867
    256,
868
    "CAMELLIA-256-CTR",
869
    16,
870
    0,
871
    16,
872
    &camellia_info
873
};
874
#endif /* MBEDTLS_CIPHER_MODE_CTR */
875
876
#if defined(MBEDTLS_GCM_C)
877
static int gcm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
878
                                    unsigned int key_bitlen)
879
0
{
880
0
    return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
881
0
                              key, key_bitlen);
882
0
}
883
884
static const mbedtls_cipher_base_t gcm_camellia_info = {
885
    MBEDTLS_CIPHER_ID_CAMELLIA,
886
    NULL,
887
#if defined(MBEDTLS_CIPHER_MODE_CBC)
888
    NULL,
889
#endif
890
#if defined(MBEDTLS_CIPHER_MODE_CFB)
891
    NULL,
892
#endif
893
#if defined(MBEDTLS_CIPHER_MODE_OFB)
894
    NULL,
895
#endif
896
#if defined(MBEDTLS_CIPHER_MODE_CTR)
897
    NULL,
898
#endif
899
#if defined(MBEDTLS_CIPHER_MODE_XTS)
900
    NULL,
901
#endif
902
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
903
    NULL,
904
#endif
905
    gcm_camellia_setkey_wrap,
906
    gcm_camellia_setkey_wrap,
907
    gcm_ctx_alloc,
908
    gcm_ctx_free,
909
};
910
911
static const mbedtls_cipher_info_t camellia_128_gcm_info = {
912
    MBEDTLS_CIPHER_CAMELLIA_128_GCM,
913
    MBEDTLS_MODE_GCM,
914
    128,
915
    "CAMELLIA-128-GCM",
916
    12,
917
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
918
    16,
919
    &gcm_camellia_info
920
};
921
922
static const mbedtls_cipher_info_t camellia_192_gcm_info = {
923
    MBEDTLS_CIPHER_CAMELLIA_192_GCM,
924
    MBEDTLS_MODE_GCM,
925
    192,
926
    "CAMELLIA-192-GCM",
927
    12,
928
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
929
    16,
930
    &gcm_camellia_info
931
};
932
933
static const mbedtls_cipher_info_t camellia_256_gcm_info = {
934
    MBEDTLS_CIPHER_CAMELLIA_256_GCM,
935
    MBEDTLS_MODE_GCM,
936
    256,
937
    "CAMELLIA-256-GCM",
938
    12,
939
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
940
    16,
941
    &gcm_camellia_info
942
};
943
#endif /* MBEDTLS_GCM_C */
944
945
#if defined(MBEDTLS_CCM_C)
946
static int ccm_camellia_setkey_wrap(void *ctx, const unsigned char *key,
947
                                    unsigned int key_bitlen)
948
0
{
949
0
    return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
950
0
                              key, key_bitlen);
951
0
}
952
953
static const mbedtls_cipher_base_t ccm_camellia_info = {
954
    MBEDTLS_CIPHER_ID_CAMELLIA,
955
    NULL,
956
#if defined(MBEDTLS_CIPHER_MODE_CBC)
957
    NULL,
958
#endif
959
#if defined(MBEDTLS_CIPHER_MODE_CFB)
960
    NULL,
961
#endif
962
#if defined(MBEDTLS_CIPHER_MODE_OFB)
963
    NULL,
964
#endif
965
#if defined(MBEDTLS_CIPHER_MODE_CTR)
966
    NULL,
967
#endif
968
#if defined(MBEDTLS_CIPHER_MODE_XTS)
969
    NULL,
970
#endif
971
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
972
    NULL,
973
#endif
974
    ccm_camellia_setkey_wrap,
975
    ccm_camellia_setkey_wrap,
976
    ccm_ctx_alloc,
977
    ccm_ctx_free,
978
};
979
980
static const mbedtls_cipher_info_t camellia_128_ccm_info = {
981
    MBEDTLS_CIPHER_CAMELLIA_128_CCM,
982
    MBEDTLS_MODE_CCM,
983
    128,
984
    "CAMELLIA-128-CCM",
985
    12,
986
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
987
    16,
988
    &ccm_camellia_info
989
};
990
991
static const mbedtls_cipher_info_t camellia_192_ccm_info = {
992
    MBEDTLS_CIPHER_CAMELLIA_192_CCM,
993
    MBEDTLS_MODE_CCM,
994
    192,
995
    "CAMELLIA-192-CCM",
996
    12,
997
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
998
    16,
999
    &ccm_camellia_info
1000
};
1001
1002
static const mbedtls_cipher_info_t camellia_256_ccm_info = {
1003
    MBEDTLS_CIPHER_CAMELLIA_256_CCM,
1004
    MBEDTLS_MODE_CCM,
1005
    256,
1006
    "CAMELLIA-256-CCM",
1007
    12,
1008
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1009
    16,
1010
    &ccm_camellia_info
1011
};
1012
#endif /* MBEDTLS_CCM_C */
1013
1014
#endif /* MBEDTLS_CAMELLIA_C */
1015
1016
#if defined(MBEDTLS_ARIA_C)
1017
1018
static int aria_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1019
                               const unsigned char *input, unsigned char *output)
1020
{
1021
    (void) operation;
1022
    return mbedtls_aria_crypt_ecb((mbedtls_aria_context *) ctx, input,
1023
                                  output);
1024
}
1025
1026
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1027
static int aria_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
1028
                               size_t length, unsigned char *iv,
1029
                               const unsigned char *input, unsigned char *output)
1030
{
1031
    return mbedtls_aria_crypt_cbc((mbedtls_aria_context *) ctx, operation, length, iv,
1032
                                  input, output);
1033
}
1034
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1035
1036
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1037
static int aria_crypt_cfb128_wrap(void *ctx, mbedtls_operation_t operation,
1038
                                  size_t length, size_t *iv_off, unsigned char *iv,
1039
                                  const unsigned char *input, unsigned char *output)
1040
{
1041
    return mbedtls_aria_crypt_cfb128((mbedtls_aria_context *) ctx, operation, length,
1042
                                     iv_off, iv, input, output);
1043
}
1044
#endif /* MBEDTLS_CIPHER_MODE_CFB */
1045
1046
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1047
static int aria_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
1048
                               unsigned char *nonce_counter, unsigned char *stream_block,
1049
                               const unsigned char *input, unsigned char *output)
1050
{
1051
    return mbedtls_aria_crypt_ctr((mbedtls_aria_context *) ctx, length, nc_off,
1052
                                  nonce_counter, stream_block, input, output);
1053
}
1054
#endif /* MBEDTLS_CIPHER_MODE_CTR */
1055
1056
static int aria_setkey_dec_wrap(void *ctx, const unsigned char *key,
1057
                                unsigned int key_bitlen)
1058
{
1059
    return mbedtls_aria_setkey_dec((mbedtls_aria_context *) ctx, key, key_bitlen);
1060
}
1061
1062
static int aria_setkey_enc_wrap(void *ctx, const unsigned char *key,
1063
                                unsigned int key_bitlen)
1064
{
1065
    return mbedtls_aria_setkey_enc((mbedtls_aria_context *) ctx, key, key_bitlen);
1066
}
1067
1068
static void *aria_ctx_alloc(void)
1069
{
1070
    mbedtls_aria_context *ctx;
1071
    ctx = mbedtls_calloc(1, sizeof(mbedtls_aria_context));
1072
1073
    if (ctx == NULL) {
1074
        return NULL;
1075
    }
1076
1077
    mbedtls_aria_init(ctx);
1078
1079
    return ctx;
1080
}
1081
1082
static void aria_ctx_free(void *ctx)
1083
{
1084
    mbedtls_aria_free((mbedtls_aria_context *) ctx);
1085
    mbedtls_free(ctx);
1086
}
1087
1088
static const mbedtls_cipher_base_t aria_info = {
1089
    MBEDTLS_CIPHER_ID_ARIA,
1090
    aria_crypt_ecb_wrap,
1091
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1092
    aria_crypt_cbc_wrap,
1093
#endif
1094
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1095
    aria_crypt_cfb128_wrap,
1096
#endif
1097
#if defined(MBEDTLS_CIPHER_MODE_OFB)
1098
    NULL,
1099
#endif
1100
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1101
    aria_crypt_ctr_wrap,
1102
#endif
1103
#if defined(MBEDTLS_CIPHER_MODE_XTS)
1104
    NULL,
1105
#endif
1106
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1107
    NULL,
1108
#endif
1109
    aria_setkey_enc_wrap,
1110
    aria_setkey_dec_wrap,
1111
    aria_ctx_alloc,
1112
    aria_ctx_free
1113
};
1114
1115
static const mbedtls_cipher_info_t aria_128_ecb_info = {
1116
    MBEDTLS_CIPHER_ARIA_128_ECB,
1117
    MBEDTLS_MODE_ECB,
1118
    128,
1119
    "ARIA-128-ECB",
1120
    0,
1121
    0,
1122
    16,
1123
    &aria_info
1124
};
1125
1126
static const mbedtls_cipher_info_t aria_192_ecb_info = {
1127
    MBEDTLS_CIPHER_ARIA_192_ECB,
1128
    MBEDTLS_MODE_ECB,
1129
    192,
1130
    "ARIA-192-ECB",
1131
    0,
1132
    0,
1133
    16,
1134
    &aria_info
1135
};
1136
1137
static const mbedtls_cipher_info_t aria_256_ecb_info = {
1138
    MBEDTLS_CIPHER_ARIA_256_ECB,
1139
    MBEDTLS_MODE_ECB,
1140
    256,
1141
    "ARIA-256-ECB",
1142
    0,
1143
    0,
1144
    16,
1145
    &aria_info
1146
};
1147
1148
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1149
static const mbedtls_cipher_info_t aria_128_cbc_info = {
1150
    MBEDTLS_CIPHER_ARIA_128_CBC,
1151
    MBEDTLS_MODE_CBC,
1152
    128,
1153
    "ARIA-128-CBC",
1154
    16,
1155
    0,
1156
    16,
1157
    &aria_info
1158
};
1159
1160
static const mbedtls_cipher_info_t aria_192_cbc_info = {
1161
    MBEDTLS_CIPHER_ARIA_192_CBC,
1162
    MBEDTLS_MODE_CBC,
1163
    192,
1164
    "ARIA-192-CBC",
1165
    16,
1166
    0,
1167
    16,
1168
    &aria_info
1169
};
1170
1171
static const mbedtls_cipher_info_t aria_256_cbc_info = {
1172
    MBEDTLS_CIPHER_ARIA_256_CBC,
1173
    MBEDTLS_MODE_CBC,
1174
    256,
1175
    "ARIA-256-CBC",
1176
    16,
1177
    0,
1178
    16,
1179
    &aria_info
1180
};
1181
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1182
1183
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1184
static const mbedtls_cipher_info_t aria_128_cfb128_info = {
1185
    MBEDTLS_CIPHER_ARIA_128_CFB128,
1186
    MBEDTLS_MODE_CFB,
1187
    128,
1188
    "ARIA-128-CFB128",
1189
    16,
1190
    0,
1191
    16,
1192
    &aria_info
1193
};
1194
1195
static const mbedtls_cipher_info_t aria_192_cfb128_info = {
1196
    MBEDTLS_CIPHER_ARIA_192_CFB128,
1197
    MBEDTLS_MODE_CFB,
1198
    192,
1199
    "ARIA-192-CFB128",
1200
    16,
1201
    0,
1202
    16,
1203
    &aria_info
1204
};
1205
1206
static const mbedtls_cipher_info_t aria_256_cfb128_info = {
1207
    MBEDTLS_CIPHER_ARIA_256_CFB128,
1208
    MBEDTLS_MODE_CFB,
1209
    256,
1210
    "ARIA-256-CFB128",
1211
    16,
1212
    0,
1213
    16,
1214
    &aria_info
1215
};
1216
#endif /* MBEDTLS_CIPHER_MODE_CFB */
1217
1218
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1219
static const mbedtls_cipher_info_t aria_128_ctr_info = {
1220
    MBEDTLS_CIPHER_ARIA_128_CTR,
1221
    MBEDTLS_MODE_CTR,
1222
    128,
1223
    "ARIA-128-CTR",
1224
    16,
1225
    0,
1226
    16,
1227
    &aria_info
1228
};
1229
1230
static const mbedtls_cipher_info_t aria_192_ctr_info = {
1231
    MBEDTLS_CIPHER_ARIA_192_CTR,
1232
    MBEDTLS_MODE_CTR,
1233
    192,
1234
    "ARIA-192-CTR",
1235
    16,
1236
    0,
1237
    16,
1238
    &aria_info
1239
};
1240
1241
static const mbedtls_cipher_info_t aria_256_ctr_info = {
1242
    MBEDTLS_CIPHER_ARIA_256_CTR,
1243
    MBEDTLS_MODE_CTR,
1244
    256,
1245
    "ARIA-256-CTR",
1246
    16,
1247
    0,
1248
    16,
1249
    &aria_info
1250
};
1251
#endif /* MBEDTLS_CIPHER_MODE_CTR */
1252
1253
#if defined(MBEDTLS_GCM_C)
1254
static int gcm_aria_setkey_wrap(void *ctx, const unsigned char *key,
1255
                                unsigned int key_bitlen)
1256
{
1257
    return mbedtls_gcm_setkey((mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1258
                              key, key_bitlen);
1259
}
1260
1261
static const mbedtls_cipher_base_t gcm_aria_info = {
1262
    MBEDTLS_CIPHER_ID_ARIA,
1263
    NULL,
1264
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1265
    NULL,
1266
#endif
1267
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1268
    NULL,
1269
#endif
1270
#if defined(MBEDTLS_CIPHER_MODE_OFB)
1271
    NULL,
1272
#endif
1273
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1274
    NULL,
1275
#endif
1276
#if defined(MBEDTLS_CIPHER_MODE_XTS)
1277
    NULL,
1278
#endif
1279
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1280
    NULL,
1281
#endif
1282
    gcm_aria_setkey_wrap,
1283
    gcm_aria_setkey_wrap,
1284
    gcm_ctx_alloc,
1285
    gcm_ctx_free,
1286
};
1287
1288
static const mbedtls_cipher_info_t aria_128_gcm_info = {
1289
    MBEDTLS_CIPHER_ARIA_128_GCM,
1290
    MBEDTLS_MODE_GCM,
1291
    128,
1292
    "ARIA-128-GCM",
1293
    12,
1294
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1295
    16,
1296
    &gcm_aria_info
1297
};
1298
1299
static const mbedtls_cipher_info_t aria_192_gcm_info = {
1300
    MBEDTLS_CIPHER_ARIA_192_GCM,
1301
    MBEDTLS_MODE_GCM,
1302
    192,
1303
    "ARIA-192-GCM",
1304
    12,
1305
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1306
    16,
1307
    &gcm_aria_info
1308
};
1309
1310
static const mbedtls_cipher_info_t aria_256_gcm_info = {
1311
    MBEDTLS_CIPHER_ARIA_256_GCM,
1312
    MBEDTLS_MODE_GCM,
1313
    256,
1314
    "ARIA-256-GCM",
1315
    12,
1316
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1317
    16,
1318
    &gcm_aria_info
1319
};
1320
#endif /* MBEDTLS_GCM_C */
1321
1322
#if defined(MBEDTLS_CCM_C)
1323
static int ccm_aria_setkey_wrap(void *ctx, const unsigned char *key,
1324
                                unsigned int key_bitlen)
1325
{
1326
    return mbedtls_ccm_setkey((mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
1327
                              key, key_bitlen);
1328
}
1329
1330
static const mbedtls_cipher_base_t ccm_aria_info = {
1331
    MBEDTLS_CIPHER_ID_ARIA,
1332
    NULL,
1333
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1334
    NULL,
1335
#endif
1336
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1337
    NULL,
1338
#endif
1339
#if defined(MBEDTLS_CIPHER_MODE_OFB)
1340
    NULL,
1341
#endif
1342
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1343
    NULL,
1344
#endif
1345
#if defined(MBEDTLS_CIPHER_MODE_XTS)
1346
    NULL,
1347
#endif
1348
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1349
    NULL,
1350
#endif
1351
    ccm_aria_setkey_wrap,
1352
    ccm_aria_setkey_wrap,
1353
    ccm_ctx_alloc,
1354
    ccm_ctx_free,
1355
};
1356
1357
static const mbedtls_cipher_info_t aria_128_ccm_info = {
1358
    MBEDTLS_CIPHER_ARIA_128_CCM,
1359
    MBEDTLS_MODE_CCM,
1360
    128,
1361
    "ARIA-128-CCM",
1362
    12,
1363
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1364
    16,
1365
    &ccm_aria_info
1366
};
1367
1368
static const mbedtls_cipher_info_t aria_192_ccm_info = {
1369
    MBEDTLS_CIPHER_ARIA_192_CCM,
1370
    MBEDTLS_MODE_CCM,
1371
    192,
1372
    "ARIA-192-CCM",
1373
    12,
1374
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1375
    16,
1376
    &ccm_aria_info
1377
};
1378
1379
static const mbedtls_cipher_info_t aria_256_ccm_info = {
1380
    MBEDTLS_CIPHER_ARIA_256_CCM,
1381
    MBEDTLS_MODE_CCM,
1382
    256,
1383
    "ARIA-256-CCM",
1384
    12,
1385
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
1386
    16,
1387
    &ccm_aria_info
1388
};
1389
#endif /* MBEDTLS_CCM_C */
1390
1391
#endif /* MBEDTLS_ARIA_C */
1392
1393
#if defined(MBEDTLS_DES_C)
1394
1395
static int des_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1396
                              const unsigned char *input, unsigned char *output)
1397
0
{
1398
0
    ((void) operation);
1399
0
    return mbedtls_des_crypt_ecb((mbedtls_des_context *) ctx, input, output);
1400
0
}
1401
1402
static int des3_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1403
                               const unsigned char *input, unsigned char *output)
1404
0
{
1405
0
    ((void) operation);
1406
0
    return mbedtls_des3_crypt_ecb((mbedtls_des3_context *) ctx, input, output);
1407
0
}
1408
1409
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1410
static int des_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
1411
                              unsigned char *iv, const unsigned char *input, unsigned char *output)
1412
0
{
1413
0
    return mbedtls_des_crypt_cbc((mbedtls_des_context *) ctx, operation, length, iv, input,
1414
0
                                 output);
1415
0
}
1416
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1417
1418
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1419
static int des3_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation, size_t length,
1420
                               unsigned char *iv, const unsigned char *input, unsigned char *output)
1421
0
{
1422
0
    return mbedtls_des3_crypt_cbc((mbedtls_des3_context *) ctx, operation, length, iv, input,
1423
0
                                  output);
1424
0
}
1425
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1426
1427
static int des_setkey_dec_wrap(void *ctx, const unsigned char *key,
1428
                               unsigned int key_bitlen)
1429
0
{
1430
0
    ((void) key_bitlen);
1431
1432
0
    return mbedtls_des_setkey_dec((mbedtls_des_context *) ctx, key);
1433
0
}
1434
1435
static int des_setkey_enc_wrap(void *ctx, const unsigned char *key,
1436
                               unsigned int key_bitlen)
1437
0
{
1438
0
    ((void) key_bitlen);
1439
1440
0
    return mbedtls_des_setkey_enc((mbedtls_des_context *) ctx, key);
1441
0
}
1442
1443
static int des3_set2key_dec_wrap(void *ctx, const unsigned char *key,
1444
                                 unsigned int key_bitlen)
1445
0
{
1446
0
    ((void) key_bitlen);
1447
1448
0
    return mbedtls_des3_set2key_dec((mbedtls_des3_context *) ctx, key);
1449
0
}
1450
1451
static int des3_set2key_enc_wrap(void *ctx, const unsigned char *key,
1452
                                 unsigned int key_bitlen)
1453
0
{
1454
0
    ((void) key_bitlen);
1455
1456
0
    return mbedtls_des3_set2key_enc((mbedtls_des3_context *) ctx, key);
1457
0
}
1458
1459
static int des3_set3key_dec_wrap(void *ctx, const unsigned char *key,
1460
                                 unsigned int key_bitlen)
1461
0
{
1462
0
    ((void) key_bitlen);
1463
1464
0
    return mbedtls_des3_set3key_dec((mbedtls_des3_context *) ctx, key);
1465
0
}
1466
1467
static int des3_set3key_enc_wrap(void *ctx, const unsigned char *key,
1468
                                 unsigned int key_bitlen)
1469
0
{
1470
0
    ((void) key_bitlen);
1471
1472
0
    return mbedtls_des3_set3key_enc((mbedtls_des3_context *) ctx, key);
1473
0
}
1474
1475
static void *des_ctx_alloc(void)
1476
0
{
1477
0
    mbedtls_des_context *des = mbedtls_calloc(1, sizeof(mbedtls_des_context));
1478
1479
0
    if (des == NULL) {
1480
0
        return NULL;
1481
0
    }
1482
1483
0
    mbedtls_des_init(des);
1484
1485
0
    return des;
1486
0
}
1487
1488
static void des_ctx_free(void *ctx)
1489
0
{
1490
0
    mbedtls_des_free((mbedtls_des_context *) ctx);
1491
0
    mbedtls_free(ctx);
1492
0
}
1493
1494
static void *des3_ctx_alloc(void)
1495
0
{
1496
0
    mbedtls_des3_context *des3;
1497
0
    des3 = mbedtls_calloc(1, sizeof(mbedtls_des3_context));
1498
1499
0
    if (des3 == NULL) {
1500
0
        return NULL;
1501
0
    }
1502
1503
0
    mbedtls_des3_init(des3);
1504
1505
0
    return des3;
1506
0
}
1507
1508
static void des3_ctx_free(void *ctx)
1509
0
{
1510
0
    mbedtls_des3_free((mbedtls_des3_context *) ctx);
1511
0
    mbedtls_free(ctx);
1512
0
}
1513
1514
static const mbedtls_cipher_base_t des_info = {
1515
    MBEDTLS_CIPHER_ID_DES,
1516
    des_crypt_ecb_wrap,
1517
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1518
    des_crypt_cbc_wrap,
1519
#endif
1520
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1521
    NULL,
1522
#endif
1523
#if defined(MBEDTLS_CIPHER_MODE_OFB)
1524
    NULL,
1525
#endif
1526
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1527
    NULL,
1528
#endif
1529
#if defined(MBEDTLS_CIPHER_MODE_XTS)
1530
    NULL,
1531
#endif
1532
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1533
    NULL,
1534
#endif
1535
    des_setkey_enc_wrap,
1536
    des_setkey_dec_wrap,
1537
    des_ctx_alloc,
1538
    des_ctx_free
1539
};
1540
1541
static const mbedtls_cipher_info_t des_ecb_info = {
1542
    MBEDTLS_CIPHER_DES_ECB,
1543
    MBEDTLS_MODE_ECB,
1544
    MBEDTLS_KEY_LENGTH_DES,
1545
    "DES-ECB",
1546
    0,
1547
    0,
1548
    8,
1549
    &des_info
1550
};
1551
1552
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1553
static const mbedtls_cipher_info_t des_cbc_info = {
1554
    MBEDTLS_CIPHER_DES_CBC,
1555
    MBEDTLS_MODE_CBC,
1556
    MBEDTLS_KEY_LENGTH_DES,
1557
    "DES-CBC",
1558
    8,
1559
    0,
1560
    8,
1561
    &des_info
1562
};
1563
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1564
1565
static const mbedtls_cipher_base_t des_ede_info = {
1566
    MBEDTLS_CIPHER_ID_DES,
1567
    des3_crypt_ecb_wrap,
1568
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1569
    des3_crypt_cbc_wrap,
1570
#endif
1571
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1572
    NULL,
1573
#endif
1574
#if defined(MBEDTLS_CIPHER_MODE_OFB)
1575
    NULL,
1576
#endif
1577
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1578
    NULL,
1579
#endif
1580
#if defined(MBEDTLS_CIPHER_MODE_XTS)
1581
    NULL,
1582
#endif
1583
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1584
    NULL,
1585
#endif
1586
    des3_set2key_enc_wrap,
1587
    des3_set2key_dec_wrap,
1588
    des3_ctx_alloc,
1589
    des3_ctx_free
1590
};
1591
1592
static const mbedtls_cipher_info_t des_ede_ecb_info = {
1593
    MBEDTLS_CIPHER_DES_EDE_ECB,
1594
    MBEDTLS_MODE_ECB,
1595
    MBEDTLS_KEY_LENGTH_DES_EDE,
1596
    "DES-EDE-ECB",
1597
    0,
1598
    0,
1599
    8,
1600
    &des_ede_info
1601
};
1602
1603
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1604
static const mbedtls_cipher_info_t des_ede_cbc_info = {
1605
    MBEDTLS_CIPHER_DES_EDE_CBC,
1606
    MBEDTLS_MODE_CBC,
1607
    MBEDTLS_KEY_LENGTH_DES_EDE,
1608
    "DES-EDE-CBC",
1609
    8,
1610
    0,
1611
    8,
1612
    &des_ede_info
1613
};
1614
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1615
1616
static const mbedtls_cipher_base_t des_ede3_info = {
1617
    MBEDTLS_CIPHER_ID_3DES,
1618
    des3_crypt_ecb_wrap,
1619
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1620
    des3_crypt_cbc_wrap,
1621
#endif
1622
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1623
    NULL,
1624
#endif
1625
#if defined(MBEDTLS_CIPHER_MODE_OFB)
1626
    NULL,
1627
#endif
1628
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1629
    NULL,
1630
#endif
1631
#if defined(MBEDTLS_CIPHER_MODE_XTS)
1632
    NULL,
1633
#endif
1634
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1635
    NULL,
1636
#endif
1637
    des3_set3key_enc_wrap,
1638
    des3_set3key_dec_wrap,
1639
    des3_ctx_alloc,
1640
    des3_ctx_free
1641
};
1642
1643
static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1644
    MBEDTLS_CIPHER_DES_EDE3_ECB,
1645
    MBEDTLS_MODE_ECB,
1646
    MBEDTLS_KEY_LENGTH_DES_EDE3,
1647
    "DES-EDE3-ECB",
1648
    0,
1649
    0,
1650
    8,
1651
    &des_ede3_info
1652
};
1653
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1654
static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1655
    MBEDTLS_CIPHER_DES_EDE3_CBC,
1656
    MBEDTLS_MODE_CBC,
1657
    MBEDTLS_KEY_LENGTH_DES_EDE3,
1658
    "DES-EDE3-CBC",
1659
    8,
1660
    0,
1661
    8,
1662
    &des_ede3_info
1663
};
1664
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1665
#endif /* MBEDTLS_DES_C */
1666
1667
#if defined(MBEDTLS_BLOWFISH_C)
1668
1669
static int blowfish_crypt_ecb_wrap(void *ctx, mbedtls_operation_t operation,
1670
                                   const unsigned char *input, unsigned char *output)
1671
0
{
1672
0
    return mbedtls_blowfish_crypt_ecb((mbedtls_blowfish_context *) ctx, operation, input,
1673
0
                                      output);
1674
0
}
1675
1676
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1677
static int blowfish_crypt_cbc_wrap(void *ctx, mbedtls_operation_t operation,
1678
                                   size_t length, unsigned char *iv, const unsigned char *input,
1679
                                   unsigned char *output)
1680
0
{
1681
0
    return mbedtls_blowfish_crypt_cbc((mbedtls_blowfish_context *) ctx, operation, length, iv,
1682
0
                                      input, output);
1683
0
}
1684
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1685
1686
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1687
static int blowfish_crypt_cfb64_wrap(void *ctx, mbedtls_operation_t operation,
1688
                                     size_t length, size_t *iv_off, unsigned char *iv,
1689
                                     const unsigned char *input, unsigned char *output)
1690
0
{
1691
0
    return mbedtls_blowfish_crypt_cfb64((mbedtls_blowfish_context *) ctx, operation, length,
1692
0
                                        iv_off, iv, input, output);
1693
0
}
1694
#endif /* MBEDTLS_CIPHER_MODE_CFB */
1695
1696
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1697
static int blowfish_crypt_ctr_wrap(void *ctx, size_t length, size_t *nc_off,
1698
                                   unsigned char *nonce_counter, unsigned char *stream_block,
1699
                                   const unsigned char *input, unsigned char *output)
1700
0
{
1701
0
    return mbedtls_blowfish_crypt_ctr((mbedtls_blowfish_context *) ctx, length, nc_off,
1702
0
                                      nonce_counter, stream_block, input, output);
1703
0
}
1704
#endif /* MBEDTLS_CIPHER_MODE_CTR */
1705
1706
static int blowfish_setkey_wrap(void *ctx, const unsigned char *key,
1707
                                unsigned int key_bitlen)
1708
0
{
1709
0
    return mbedtls_blowfish_setkey((mbedtls_blowfish_context *) ctx, key, key_bitlen);
1710
0
}
1711
1712
static void *blowfish_ctx_alloc(void)
1713
0
{
1714
0
    mbedtls_blowfish_context *ctx;
1715
0
    ctx = mbedtls_calloc(1, sizeof(mbedtls_blowfish_context));
1716
1717
0
    if (ctx == NULL) {
1718
0
        return NULL;
1719
0
    }
1720
1721
0
    mbedtls_blowfish_init(ctx);
1722
1723
0
    return ctx;
1724
0
}
1725
1726
static void blowfish_ctx_free(void *ctx)
1727
0
{
1728
0
    mbedtls_blowfish_free((mbedtls_blowfish_context *) ctx);
1729
0
    mbedtls_free(ctx);
1730
0
}
1731
1732
static const mbedtls_cipher_base_t blowfish_info = {
1733
    MBEDTLS_CIPHER_ID_BLOWFISH,
1734
    blowfish_crypt_ecb_wrap,
1735
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1736
    blowfish_crypt_cbc_wrap,
1737
#endif
1738
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1739
    blowfish_crypt_cfb64_wrap,
1740
#endif
1741
#if defined(MBEDTLS_CIPHER_MODE_OFB)
1742
    NULL,
1743
#endif
1744
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1745
    blowfish_crypt_ctr_wrap,
1746
#endif
1747
#if defined(MBEDTLS_CIPHER_MODE_XTS)
1748
    NULL,
1749
#endif
1750
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1751
    NULL,
1752
#endif
1753
    blowfish_setkey_wrap,
1754
    blowfish_setkey_wrap,
1755
    blowfish_ctx_alloc,
1756
    blowfish_ctx_free
1757
};
1758
1759
static const mbedtls_cipher_info_t blowfish_ecb_info = {
1760
    MBEDTLS_CIPHER_BLOWFISH_ECB,
1761
    MBEDTLS_MODE_ECB,
1762
    128,
1763
    "BLOWFISH-ECB",
1764
    0,
1765
    MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1766
    8,
1767
    &blowfish_info
1768
};
1769
1770
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1771
static const mbedtls_cipher_info_t blowfish_cbc_info = {
1772
    MBEDTLS_CIPHER_BLOWFISH_CBC,
1773
    MBEDTLS_MODE_CBC,
1774
    128,
1775
    "BLOWFISH-CBC",
1776
    8,
1777
    MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1778
    8,
1779
    &blowfish_info
1780
};
1781
#endif /* MBEDTLS_CIPHER_MODE_CBC */
1782
1783
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1784
static const mbedtls_cipher_info_t blowfish_cfb64_info = {
1785
    MBEDTLS_CIPHER_BLOWFISH_CFB64,
1786
    MBEDTLS_MODE_CFB,
1787
    128,
1788
    "BLOWFISH-CFB64",
1789
    8,
1790
    MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1791
    8,
1792
    &blowfish_info
1793
};
1794
#endif /* MBEDTLS_CIPHER_MODE_CFB */
1795
1796
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1797
static const mbedtls_cipher_info_t blowfish_ctr_info = {
1798
    MBEDTLS_CIPHER_BLOWFISH_CTR,
1799
    MBEDTLS_MODE_CTR,
1800
    128,
1801
    "BLOWFISH-CTR",
1802
    8,
1803
    MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1804
    8,
1805
    &blowfish_info
1806
};
1807
#endif /* MBEDTLS_CIPHER_MODE_CTR */
1808
#endif /* MBEDTLS_BLOWFISH_C */
1809
1810
#if defined(MBEDTLS_ARC4_C)
1811
static int arc4_crypt_stream_wrap(void *ctx, size_t length,
1812
                                  const unsigned char *input,
1813
                                  unsigned char *output)
1814
0
{
1815
0
    return mbedtls_arc4_crypt((mbedtls_arc4_context *) ctx, length, input, output);
1816
0
}
1817
1818
static int arc4_setkey_wrap(void *ctx, const unsigned char *key,
1819
                            unsigned int key_bitlen)
1820
0
{
1821
    /* we get key_bitlen in bits, arc4 expects it in bytes */
1822
0
    if (key_bitlen % 8 != 0) {
1823
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1824
0
    }
1825
1826
0
    mbedtls_arc4_setup((mbedtls_arc4_context *) ctx, key, key_bitlen / 8);
1827
0
    return 0;
1828
0
}
1829
1830
static void *arc4_ctx_alloc(void)
1831
0
{
1832
0
    mbedtls_arc4_context *ctx;
1833
0
    ctx = mbedtls_calloc(1, sizeof(mbedtls_arc4_context));
1834
1835
0
    if (ctx == NULL) {
1836
0
        return NULL;
1837
0
    }
1838
1839
0
    mbedtls_arc4_init(ctx);
1840
1841
0
    return ctx;
1842
0
}
1843
1844
static void arc4_ctx_free(void *ctx)
1845
0
{
1846
0
    mbedtls_arc4_free((mbedtls_arc4_context *) ctx);
1847
0
    mbedtls_free(ctx);
1848
0
}
1849
1850
static const mbedtls_cipher_base_t arc4_base_info = {
1851
    MBEDTLS_CIPHER_ID_ARC4,
1852
    NULL,
1853
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1854
    NULL,
1855
#endif
1856
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1857
    NULL,
1858
#endif
1859
#if defined(MBEDTLS_CIPHER_MODE_OFB)
1860
    NULL,
1861
#endif
1862
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1863
    NULL,
1864
#endif
1865
#if defined(MBEDTLS_CIPHER_MODE_XTS)
1866
    NULL,
1867
#endif
1868
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1869
    arc4_crypt_stream_wrap,
1870
#endif
1871
    arc4_setkey_wrap,
1872
    arc4_setkey_wrap,
1873
    arc4_ctx_alloc,
1874
    arc4_ctx_free
1875
};
1876
1877
static const mbedtls_cipher_info_t arc4_128_info = {
1878
    MBEDTLS_CIPHER_ARC4_128,
1879
    MBEDTLS_MODE_STREAM,
1880
    128,
1881
    "ARC4-128",
1882
    0,
1883
    0,
1884
    1,
1885
    &arc4_base_info
1886
};
1887
#endif /* MBEDTLS_ARC4_C */
1888
1889
#if defined(MBEDTLS_CHACHA20_C)
1890
1891
static int chacha20_setkey_wrap(void *ctx, const unsigned char *key,
1892
                                unsigned int key_bitlen)
1893
0
{
1894
0
    if (key_bitlen != 256U) {
1895
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1896
0
    }
1897
1898
0
    if (0 != mbedtls_chacha20_setkey((mbedtls_chacha20_context *) ctx, key)) {
1899
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1900
0
    }
1901
1902
0
    return 0;
1903
0
}
1904
1905
static int chacha20_stream_wrap(void *ctx,  size_t length,
1906
                                const unsigned char *input,
1907
                                unsigned char *output)
1908
0
{
1909
0
    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1910
1911
0
    ret = mbedtls_chacha20_update(ctx, length, input, output);
1912
0
    if (ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA) {
1913
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1914
0
    }
1915
1916
0
    return ret;
1917
0
}
1918
1919
static void *chacha20_ctx_alloc(void)
1920
0
{
1921
0
    mbedtls_chacha20_context *ctx;
1922
0
    ctx = mbedtls_calloc(1, sizeof(mbedtls_chacha20_context));
1923
1924
0
    if (ctx == NULL) {
1925
0
        return NULL;
1926
0
    }
1927
1928
0
    mbedtls_chacha20_init(ctx);
1929
1930
0
    return ctx;
1931
0
}
1932
1933
static void chacha20_ctx_free(void *ctx)
1934
0
{
1935
0
    mbedtls_chacha20_free((mbedtls_chacha20_context *) ctx);
1936
0
    mbedtls_free(ctx);
1937
0
}
1938
1939
static const mbedtls_cipher_base_t chacha20_base_info = {
1940
    MBEDTLS_CIPHER_ID_CHACHA20,
1941
    NULL,
1942
#if defined(MBEDTLS_CIPHER_MODE_CBC)
1943
    NULL,
1944
#endif
1945
#if defined(MBEDTLS_CIPHER_MODE_CFB)
1946
    NULL,
1947
#endif
1948
#if defined(MBEDTLS_CIPHER_MODE_OFB)
1949
    NULL,
1950
#endif
1951
#if defined(MBEDTLS_CIPHER_MODE_CTR)
1952
    NULL,
1953
#endif
1954
#if defined(MBEDTLS_CIPHER_MODE_XTS)
1955
    NULL,
1956
#endif
1957
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
1958
    chacha20_stream_wrap,
1959
#endif
1960
    chacha20_setkey_wrap,
1961
    chacha20_setkey_wrap,
1962
    chacha20_ctx_alloc,
1963
    chacha20_ctx_free
1964
};
1965
static const mbedtls_cipher_info_t chacha20_info = {
1966
    MBEDTLS_CIPHER_CHACHA20,
1967
    MBEDTLS_MODE_STREAM,
1968
    256,
1969
    "CHACHA20",
1970
    12,
1971
    0,
1972
    1,
1973
    &chacha20_base_info
1974
};
1975
#endif /* MBEDTLS_CHACHA20_C */
1976
1977
#if defined(MBEDTLS_CHACHAPOLY_C)
1978
1979
static int chachapoly_setkey_wrap(void *ctx,
1980
                                  const unsigned char *key,
1981
                                  unsigned int key_bitlen)
1982
0
{
1983
0
    if (key_bitlen != 256U) {
1984
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1985
0
    }
1986
1987
0
    if (0 != mbedtls_chachapoly_setkey((mbedtls_chachapoly_context *) ctx, key)) {
1988
0
        return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1989
0
    }
1990
1991
0
    return 0;
1992
0
}
1993
1994
static void *chachapoly_ctx_alloc(void)
1995
0
{
1996
0
    mbedtls_chachapoly_context *ctx;
1997
0
    ctx = mbedtls_calloc(1, sizeof(mbedtls_chachapoly_context));
1998
1999
0
    if (ctx == NULL) {
2000
0
        return NULL;
2001
0
    }
2002
2003
0
    mbedtls_chachapoly_init(ctx);
2004
2005
0
    return ctx;
2006
0
}
2007
2008
static void chachapoly_ctx_free(void *ctx)
2009
0
{
2010
0
    mbedtls_chachapoly_free((mbedtls_chachapoly_context *) ctx);
2011
0
    mbedtls_free(ctx);
2012
0
}
2013
2014
static const mbedtls_cipher_base_t chachapoly_base_info = {
2015
    MBEDTLS_CIPHER_ID_CHACHA20,
2016
    NULL,
2017
#if defined(MBEDTLS_CIPHER_MODE_CBC)
2018
    NULL,
2019
#endif
2020
#if defined(MBEDTLS_CIPHER_MODE_CFB)
2021
    NULL,
2022
#endif
2023
#if defined(MBEDTLS_CIPHER_MODE_OFB)
2024
    NULL,
2025
#endif
2026
#if defined(MBEDTLS_CIPHER_MODE_CTR)
2027
    NULL,
2028
#endif
2029
#if defined(MBEDTLS_CIPHER_MODE_XTS)
2030
    NULL,
2031
#endif
2032
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
2033
    NULL,
2034
#endif
2035
    chachapoly_setkey_wrap,
2036
    chachapoly_setkey_wrap,
2037
    chachapoly_ctx_alloc,
2038
    chachapoly_ctx_free
2039
};
2040
static const mbedtls_cipher_info_t chachapoly_info = {
2041
    MBEDTLS_CIPHER_CHACHA20_POLY1305,
2042
    MBEDTLS_MODE_CHACHAPOLY,
2043
    256,
2044
    "CHACHA20-POLY1305",
2045
    12,
2046
    0,
2047
    1,
2048
    &chachapoly_base_info
2049
};
2050
#endif /* MBEDTLS_CHACHAPOLY_C */
2051
2052
#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2053
static int null_crypt_stream(void *ctx, size_t length,
2054
                             const unsigned char *input,
2055
                             unsigned char *output)
2056
{
2057
    ((void) ctx);
2058
    memmove(output, input, length);
2059
    return 0;
2060
}
2061
2062
static int null_setkey(void *ctx, const unsigned char *key,
2063
                       unsigned int key_bitlen)
2064
{
2065
    ((void) ctx);
2066
    ((void) key);
2067
    ((void) key_bitlen);
2068
2069
    return 0;
2070
}
2071
2072
static void *null_ctx_alloc(void)
2073
{
2074
    return (void *) 1;
2075
}
2076
2077
static void null_ctx_free(void *ctx)
2078
{
2079
    ((void) ctx);
2080
}
2081
2082
static const mbedtls_cipher_base_t null_base_info = {
2083
    MBEDTLS_CIPHER_ID_NULL,
2084
    NULL,
2085
#if defined(MBEDTLS_CIPHER_MODE_CBC)
2086
    NULL,
2087
#endif
2088
#if defined(MBEDTLS_CIPHER_MODE_CFB)
2089
    NULL,
2090
#endif
2091
#if defined(MBEDTLS_CIPHER_MODE_OFB)
2092
    NULL,
2093
#endif
2094
#if defined(MBEDTLS_CIPHER_MODE_CTR)
2095
    NULL,
2096
#endif
2097
#if defined(MBEDTLS_CIPHER_MODE_XTS)
2098
    NULL,
2099
#endif
2100
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
2101
    null_crypt_stream,
2102
#endif
2103
    null_setkey,
2104
    null_setkey,
2105
    null_ctx_alloc,
2106
    null_ctx_free
2107
};
2108
2109
static const mbedtls_cipher_info_t null_cipher_info = {
2110
    MBEDTLS_CIPHER_NULL,
2111
    MBEDTLS_MODE_STREAM,
2112
    0,
2113
    "NULL",
2114
    0,
2115
    0,
2116
    1,
2117
    &null_base_info
2118
};
2119
#endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
2120
2121
#if defined(MBEDTLS_NIST_KW_C)
2122
static void *kw_ctx_alloc(void)
2123
{
2124
    void *ctx = mbedtls_calloc(1, sizeof(mbedtls_nist_kw_context));
2125
2126
    if (ctx != NULL) {
2127
        mbedtls_nist_kw_init((mbedtls_nist_kw_context *) ctx);
2128
    }
2129
2130
    return ctx;
2131
}
2132
2133
static void kw_ctx_free(void *ctx)
2134
{
2135
    mbedtls_nist_kw_free(ctx);
2136
    mbedtls_free(ctx);
2137
}
2138
2139
static int kw_aes_setkey_wrap(void *ctx, const unsigned char *key,
2140
                              unsigned int key_bitlen)
2141
{
2142
    return mbedtls_nist_kw_setkey((mbedtls_nist_kw_context *) ctx,
2143
                                  MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 1);
2144
}
2145
2146
static int kw_aes_setkey_unwrap(void *ctx, const unsigned char *key,
2147
                                unsigned int key_bitlen)
2148
{
2149
    return mbedtls_nist_kw_setkey((mbedtls_nist_kw_context *) ctx,
2150
                                  MBEDTLS_CIPHER_ID_AES, key, key_bitlen, 0);
2151
}
2152
2153
static const mbedtls_cipher_base_t kw_aes_info = {
2154
    MBEDTLS_CIPHER_ID_AES,
2155
    NULL,
2156
#if defined(MBEDTLS_CIPHER_MODE_CBC)
2157
    NULL,
2158
#endif
2159
#if defined(MBEDTLS_CIPHER_MODE_CFB)
2160
    NULL,
2161
#endif
2162
#if defined(MBEDTLS_CIPHER_MODE_OFB)
2163
    NULL,
2164
#endif
2165
#if defined(MBEDTLS_CIPHER_MODE_CTR)
2166
    NULL,
2167
#endif
2168
#if defined(MBEDTLS_CIPHER_MODE_XTS)
2169
    NULL,
2170
#endif
2171
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
2172
    NULL,
2173
#endif
2174
    kw_aes_setkey_wrap,
2175
    kw_aes_setkey_unwrap,
2176
    kw_ctx_alloc,
2177
    kw_ctx_free,
2178
};
2179
2180
static const mbedtls_cipher_info_t aes_128_nist_kw_info = {
2181
    MBEDTLS_CIPHER_AES_128_KW,
2182
    MBEDTLS_MODE_KW,
2183
    128,
2184
    "AES-128-KW",
2185
    0,
2186
    0,
2187
    16,
2188
    &kw_aes_info
2189
};
2190
2191
static const mbedtls_cipher_info_t aes_192_nist_kw_info = {
2192
    MBEDTLS_CIPHER_AES_192_KW,
2193
    MBEDTLS_MODE_KW,
2194
    192,
2195
    "AES-192-KW",
2196
    0,
2197
    0,
2198
    16,
2199
    &kw_aes_info
2200
};
2201
2202
static const mbedtls_cipher_info_t aes_256_nist_kw_info = {
2203
    MBEDTLS_CIPHER_AES_256_KW,
2204
    MBEDTLS_MODE_KW,
2205
    256,
2206
    "AES-256-KW",
2207
    0,
2208
    0,
2209
    16,
2210
    &kw_aes_info
2211
};
2212
2213
static const mbedtls_cipher_info_t aes_128_nist_kwp_info = {
2214
    MBEDTLS_CIPHER_AES_128_KWP,
2215
    MBEDTLS_MODE_KWP,
2216
    128,
2217
    "AES-128-KWP",
2218
    0,
2219
    0,
2220
    16,
2221
    &kw_aes_info
2222
};
2223
2224
static const mbedtls_cipher_info_t aes_192_nist_kwp_info = {
2225
    MBEDTLS_CIPHER_AES_192_KWP,
2226
    MBEDTLS_MODE_KWP,
2227
    192,
2228
    "AES-192-KWP",
2229
    0,
2230
    0,
2231
    16,
2232
    &kw_aes_info
2233
};
2234
2235
static const mbedtls_cipher_info_t aes_256_nist_kwp_info = {
2236
    MBEDTLS_CIPHER_AES_256_KWP,
2237
    MBEDTLS_MODE_KWP,
2238
    256,
2239
    "AES-256-KWP",
2240
    0,
2241
    0,
2242
    16,
2243
    &kw_aes_info
2244
};
2245
#endif /* MBEDTLS_NIST_KW_C */
2246
2247
const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
2248
{
2249
#if defined(MBEDTLS_AES_C)
2250
    { MBEDTLS_CIPHER_AES_128_ECB,          &aes_128_ecb_info },
2251
    { MBEDTLS_CIPHER_AES_192_ECB,          &aes_192_ecb_info },
2252
    { MBEDTLS_CIPHER_AES_256_ECB,          &aes_256_ecb_info },
2253
#if defined(MBEDTLS_CIPHER_MODE_CBC)
2254
    { MBEDTLS_CIPHER_AES_128_CBC,          &aes_128_cbc_info },
2255
    { MBEDTLS_CIPHER_AES_192_CBC,          &aes_192_cbc_info },
2256
    { MBEDTLS_CIPHER_AES_256_CBC,          &aes_256_cbc_info },
2257
#endif
2258
#if defined(MBEDTLS_CIPHER_MODE_CFB)
2259
    { MBEDTLS_CIPHER_AES_128_CFB128,       &aes_128_cfb128_info },
2260
    { MBEDTLS_CIPHER_AES_192_CFB128,       &aes_192_cfb128_info },
2261
    { MBEDTLS_CIPHER_AES_256_CFB128,       &aes_256_cfb128_info },
2262
#endif
2263
#if defined(MBEDTLS_CIPHER_MODE_OFB)
2264
    { MBEDTLS_CIPHER_AES_128_OFB,          &aes_128_ofb_info },
2265
    { MBEDTLS_CIPHER_AES_192_OFB,          &aes_192_ofb_info },
2266
    { MBEDTLS_CIPHER_AES_256_OFB,          &aes_256_ofb_info },
2267
#endif
2268
#if defined(MBEDTLS_CIPHER_MODE_CTR)
2269
    { MBEDTLS_CIPHER_AES_128_CTR,          &aes_128_ctr_info },
2270
    { MBEDTLS_CIPHER_AES_192_CTR,          &aes_192_ctr_info },
2271
    { MBEDTLS_CIPHER_AES_256_CTR,          &aes_256_ctr_info },
2272
#endif
2273
#if defined(MBEDTLS_CIPHER_MODE_XTS)
2274
    { MBEDTLS_CIPHER_AES_128_XTS,          &aes_128_xts_info },
2275
    { MBEDTLS_CIPHER_AES_256_XTS,          &aes_256_xts_info },
2276
#endif
2277
#if defined(MBEDTLS_GCM_C)
2278
    { MBEDTLS_CIPHER_AES_128_GCM,          &aes_128_gcm_info },
2279
    { MBEDTLS_CIPHER_AES_192_GCM,          &aes_192_gcm_info },
2280
    { MBEDTLS_CIPHER_AES_256_GCM,          &aes_256_gcm_info },
2281
#endif
2282
#if defined(MBEDTLS_CCM_C)
2283
    { MBEDTLS_CIPHER_AES_128_CCM,          &aes_128_ccm_info },
2284
    { MBEDTLS_CIPHER_AES_192_CCM,          &aes_192_ccm_info },
2285
    { MBEDTLS_CIPHER_AES_256_CCM,          &aes_256_ccm_info },
2286
#endif
2287
#endif /* MBEDTLS_AES_C */
2288
2289
#if defined(MBEDTLS_ARC4_C)
2290
    { MBEDTLS_CIPHER_ARC4_128,             &arc4_128_info },
2291
#endif
2292
2293
#if defined(MBEDTLS_BLOWFISH_C)
2294
    { MBEDTLS_CIPHER_BLOWFISH_ECB,         &blowfish_ecb_info },
2295
#if defined(MBEDTLS_CIPHER_MODE_CBC)
2296
    { MBEDTLS_CIPHER_BLOWFISH_CBC,         &blowfish_cbc_info },
2297
#endif
2298
#if defined(MBEDTLS_CIPHER_MODE_CFB)
2299
    { MBEDTLS_CIPHER_BLOWFISH_CFB64,       &blowfish_cfb64_info },
2300
#endif
2301
#if defined(MBEDTLS_CIPHER_MODE_CTR)
2302
    { MBEDTLS_CIPHER_BLOWFISH_CTR,         &blowfish_ctr_info },
2303
#endif
2304
#endif /* MBEDTLS_BLOWFISH_C */
2305
2306
#if defined(MBEDTLS_CAMELLIA_C)
2307
    { MBEDTLS_CIPHER_CAMELLIA_128_ECB,     &camellia_128_ecb_info },
2308
    { MBEDTLS_CIPHER_CAMELLIA_192_ECB,     &camellia_192_ecb_info },
2309
    { MBEDTLS_CIPHER_CAMELLIA_256_ECB,     &camellia_256_ecb_info },
2310
#if defined(MBEDTLS_CIPHER_MODE_CBC)
2311
    { MBEDTLS_CIPHER_CAMELLIA_128_CBC,     &camellia_128_cbc_info },
2312
    { MBEDTLS_CIPHER_CAMELLIA_192_CBC,     &camellia_192_cbc_info },
2313
    { MBEDTLS_CIPHER_CAMELLIA_256_CBC,     &camellia_256_cbc_info },
2314
#endif
2315
#if defined(MBEDTLS_CIPHER_MODE_CFB)
2316
    { MBEDTLS_CIPHER_CAMELLIA_128_CFB128,  &camellia_128_cfb128_info },
2317
    { MBEDTLS_CIPHER_CAMELLIA_192_CFB128,  &camellia_192_cfb128_info },
2318
    { MBEDTLS_CIPHER_CAMELLIA_256_CFB128,  &camellia_256_cfb128_info },
2319
#endif
2320
#if defined(MBEDTLS_CIPHER_MODE_CTR)
2321
    { MBEDTLS_CIPHER_CAMELLIA_128_CTR,     &camellia_128_ctr_info },
2322
    { MBEDTLS_CIPHER_CAMELLIA_192_CTR,     &camellia_192_ctr_info },
2323
    { MBEDTLS_CIPHER_CAMELLIA_256_CTR,     &camellia_256_ctr_info },
2324
#endif
2325
#if defined(MBEDTLS_GCM_C)
2326
    { MBEDTLS_CIPHER_CAMELLIA_128_GCM,     &camellia_128_gcm_info },
2327
    { MBEDTLS_CIPHER_CAMELLIA_192_GCM,     &camellia_192_gcm_info },
2328
    { MBEDTLS_CIPHER_CAMELLIA_256_GCM,     &camellia_256_gcm_info },
2329
#endif
2330
#if defined(MBEDTLS_CCM_C)
2331
    { MBEDTLS_CIPHER_CAMELLIA_128_CCM,     &camellia_128_ccm_info },
2332
    { MBEDTLS_CIPHER_CAMELLIA_192_CCM,     &camellia_192_ccm_info },
2333
    { MBEDTLS_CIPHER_CAMELLIA_256_CCM,     &camellia_256_ccm_info },
2334
#endif
2335
#endif /* MBEDTLS_CAMELLIA_C */
2336
2337
#if defined(MBEDTLS_ARIA_C)
2338
    { MBEDTLS_CIPHER_ARIA_128_ECB,     &aria_128_ecb_info },
2339
    { MBEDTLS_CIPHER_ARIA_192_ECB,     &aria_192_ecb_info },
2340
    { MBEDTLS_CIPHER_ARIA_256_ECB,     &aria_256_ecb_info },
2341
#if defined(MBEDTLS_CIPHER_MODE_CBC)
2342
    { MBEDTLS_CIPHER_ARIA_128_CBC,     &aria_128_cbc_info },
2343
    { MBEDTLS_CIPHER_ARIA_192_CBC,     &aria_192_cbc_info },
2344
    { MBEDTLS_CIPHER_ARIA_256_CBC,     &aria_256_cbc_info },
2345
#endif
2346
#if defined(MBEDTLS_CIPHER_MODE_CFB)
2347
    { MBEDTLS_CIPHER_ARIA_128_CFB128,  &aria_128_cfb128_info },
2348
    { MBEDTLS_CIPHER_ARIA_192_CFB128,  &aria_192_cfb128_info },
2349
    { MBEDTLS_CIPHER_ARIA_256_CFB128,  &aria_256_cfb128_info },
2350
#endif
2351
#if defined(MBEDTLS_CIPHER_MODE_CTR)
2352
    { MBEDTLS_CIPHER_ARIA_128_CTR,     &aria_128_ctr_info },
2353
    { MBEDTLS_CIPHER_ARIA_192_CTR,     &aria_192_ctr_info },
2354
    { MBEDTLS_CIPHER_ARIA_256_CTR,     &aria_256_ctr_info },
2355
#endif
2356
#if defined(MBEDTLS_GCM_C)
2357
    { MBEDTLS_CIPHER_ARIA_128_GCM,     &aria_128_gcm_info },
2358
    { MBEDTLS_CIPHER_ARIA_192_GCM,     &aria_192_gcm_info },
2359
    { MBEDTLS_CIPHER_ARIA_256_GCM,     &aria_256_gcm_info },
2360
#endif
2361
#if defined(MBEDTLS_CCM_C)
2362
    { MBEDTLS_CIPHER_ARIA_128_CCM,     &aria_128_ccm_info },
2363
    { MBEDTLS_CIPHER_ARIA_192_CCM,     &aria_192_ccm_info },
2364
    { MBEDTLS_CIPHER_ARIA_256_CCM,     &aria_256_ccm_info },
2365
#endif
2366
#endif /* MBEDTLS_ARIA_C */
2367
2368
#if defined(MBEDTLS_DES_C)
2369
    { MBEDTLS_CIPHER_DES_ECB,              &des_ecb_info },
2370
    { MBEDTLS_CIPHER_DES_EDE_ECB,          &des_ede_ecb_info },
2371
    { MBEDTLS_CIPHER_DES_EDE3_ECB,         &des_ede3_ecb_info },
2372
#if defined(MBEDTLS_CIPHER_MODE_CBC)
2373
    { MBEDTLS_CIPHER_DES_CBC,              &des_cbc_info },
2374
    { MBEDTLS_CIPHER_DES_EDE_CBC,          &des_ede_cbc_info },
2375
    { MBEDTLS_CIPHER_DES_EDE3_CBC,         &des_ede3_cbc_info },
2376
#endif
2377
#endif /* MBEDTLS_DES_C */
2378
2379
#if defined(MBEDTLS_CHACHA20_C)
2380
    { MBEDTLS_CIPHER_CHACHA20,             &chacha20_info },
2381
#endif
2382
2383
#if defined(MBEDTLS_CHACHAPOLY_C)
2384
    { MBEDTLS_CIPHER_CHACHA20_POLY1305,    &chachapoly_info },
2385
#endif
2386
2387
#if defined(MBEDTLS_NIST_KW_C)
2388
    { MBEDTLS_CIPHER_AES_128_KW,          &aes_128_nist_kw_info },
2389
    { MBEDTLS_CIPHER_AES_192_KW,          &aes_192_nist_kw_info },
2390
    { MBEDTLS_CIPHER_AES_256_KW,          &aes_256_nist_kw_info },
2391
    { MBEDTLS_CIPHER_AES_128_KWP,         &aes_128_nist_kwp_info },
2392
    { MBEDTLS_CIPHER_AES_192_KWP,         &aes_192_nist_kwp_info },
2393
    { MBEDTLS_CIPHER_AES_256_KWP,         &aes_256_nist_kwp_info },
2394
#endif
2395
2396
#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
2397
    { MBEDTLS_CIPHER_NULL,                 &null_cipher_info },
2398
#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
2399
2400
    { MBEDTLS_CIPHER_NONE, NULL }
2401
};
2402
2403
#define NUM_CIPHERS (sizeof(mbedtls_cipher_definitions) /      \
2404
                     sizeof(mbedtls_cipher_definitions[0]))
2405
int mbedtls_cipher_supported[NUM_CIPHERS];
2406
2407
#endif /* MBEDTLS_CIPHER_C */