Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

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