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