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