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_AES_C)
62 #include "mbedtls/aes.h"
63 #endif
64
65 #if defined(MBEDTLS_ARC4_C)
66 #include "mbedtls/arc4.h"
67 #endif
68
69 #if defined(MBEDTLS_CAMELLIA_C)
70 #include "mbedtls/camellia.h"
71 #endif
72
73 #if defined(MBEDTLS_DES_C)
74 #include "mbedtls/des.h"
75 #endif
76
77 #if defined(MBEDTLS_BLOWFISH_C)
78 #include "mbedtls/blowfish.h"
79 #endif
80
81 #if defined(MBEDTLS_GCM_C)
82 #include "mbedtls/gcm.h"
83 #endif
84
85 #if defined(MBEDTLS_CCM_C)
86 #include "mbedtls/ccm.h"
87 #endif
88
89 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
90 #include <string.h>
91 #endif
92
93 #if defined(MBEDTLS_PLATFORM_C)
94 #include "mbedtls/platform.h"
95 #else
96 #include <stdlib.h>
97 #define mbedtls_calloc calloc
98 #define mbedtls_free free
99 #endif
100
101 #if defined(MBEDTLS_GCM_C)
102 /* shared by all GCM ciphers */
gcm_ctx_alloc(void)103 static void *gcm_ctx_alloc( void )
104 {
105 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
106
107 if( ctx != NULL )
108 mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
109
110 return( ctx );
111 }
112
gcm_ctx_free(void * ctx)113 static void gcm_ctx_free( void *ctx )
114 {
115 mbedtls_gcm_free( ctx );
116 mbedtls_free( ctx );
117 }
118 #endif /* MBEDTLS_GCM_C */
119
120 #if defined(MBEDTLS_CCM_C)
121 /* shared by all CCM ciphers */
ccm_ctx_alloc(void)122 static void *ccm_ctx_alloc( void )
123 {
124 void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
125
126 if( ctx != NULL )
127 mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
128
129 return( ctx );
130 }
131
ccm_ctx_free(void * ctx)132 static void ccm_ctx_free( void *ctx )
133 {
134 mbedtls_ccm_free( ctx );
135 mbedtls_free( ctx );
136 }
137 #endif /* MBEDTLS_CCM_C */
138
139 #if defined(MBEDTLS_AES_C)
140
aes_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)141 static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
142 const unsigned char *input, unsigned char *output )
143 {
144 return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
145 }
146
147 #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)148 static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
149 unsigned char *iv, const unsigned char *input, unsigned char *output )
150 {
151 return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
152 output );
153 }
154 #endif /* MBEDTLS_CIPHER_MODE_CBC */
155
156 #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)157 static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
158 size_t length, size_t *iv_off, unsigned char *iv,
159 const unsigned char *input, unsigned char *output )
160 {
161 return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
162 input, output );
163 }
164 #endif /* MBEDTLS_CIPHER_MODE_CFB */
165
166 #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)167 static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
168 unsigned char *nonce_counter, unsigned char *stream_block,
169 const unsigned char *input, unsigned char *output )
170 {
171 return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
172 stream_block, input, output );
173 }
174 #endif /* MBEDTLS_CIPHER_MODE_CTR */
175
aes_setkey_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)176 static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
177 unsigned int key_bitlen )
178 {
179 return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
180 }
181
aes_setkey_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)182 static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
183 unsigned int key_bitlen )
184 {
185 return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
186 }
187
aes_ctx_alloc(void)188 static void * aes_ctx_alloc( void )
189 {
190 mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
191
192 if( aes == NULL )
193 return( NULL );
194
195 mbedtls_aes_init( aes );
196
197 return( aes );
198 }
199
aes_ctx_free(void * ctx)200 static void aes_ctx_free( void *ctx )
201 {
202 mbedtls_aes_free( (mbedtls_aes_context *) ctx );
203 mbedtls_free( ctx );
204 }
205
206 static const mbedtls_cipher_base_t aes_info = {
207 MBEDTLS_CIPHER_ID_AES,
208 aes_crypt_ecb_wrap,
209 #if defined(MBEDTLS_CIPHER_MODE_CBC)
210 aes_crypt_cbc_wrap,
211 #endif
212 #if defined(MBEDTLS_CIPHER_MODE_CFB)
213 aes_crypt_cfb128_wrap,
214 #endif
215 #if defined(MBEDTLS_CIPHER_MODE_CTR)
216 aes_crypt_ctr_wrap,
217 #endif
218 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
219 NULL,
220 #endif
221 aes_setkey_enc_wrap,
222 aes_setkey_dec_wrap,
223 aes_ctx_alloc,
224 aes_ctx_free
225 };
226
227 static const mbedtls_cipher_info_t aes_128_ecb_info = {
228 MBEDTLS_CIPHER_AES_128_ECB,
229 MBEDTLS_MODE_ECB,
230 128,
231 "AES-128-ECB",
232 0,
233 0,
234 16,
235 &aes_info
236 };
237
238 static const mbedtls_cipher_info_t aes_192_ecb_info = {
239 MBEDTLS_CIPHER_AES_192_ECB,
240 MBEDTLS_MODE_ECB,
241 192,
242 "AES-192-ECB",
243 0,
244 0,
245 16,
246 &aes_info
247 };
248
249 static const mbedtls_cipher_info_t aes_256_ecb_info = {
250 MBEDTLS_CIPHER_AES_256_ECB,
251 MBEDTLS_MODE_ECB,
252 256,
253 "AES-256-ECB",
254 0,
255 0,
256 16,
257 &aes_info
258 };
259
260 #if defined(MBEDTLS_CIPHER_MODE_CBC)
261 static const mbedtls_cipher_info_t aes_128_cbc_info = {
262 MBEDTLS_CIPHER_AES_128_CBC,
263 MBEDTLS_MODE_CBC,
264 128,
265 "AES-128-CBC",
266 16,
267 0,
268 16,
269 &aes_info
270 };
271
272 static const mbedtls_cipher_info_t aes_192_cbc_info = {
273 MBEDTLS_CIPHER_AES_192_CBC,
274 MBEDTLS_MODE_CBC,
275 192,
276 "AES-192-CBC",
277 16,
278 0,
279 16,
280 &aes_info
281 };
282
283 static const mbedtls_cipher_info_t aes_256_cbc_info = {
284 MBEDTLS_CIPHER_AES_256_CBC,
285 MBEDTLS_MODE_CBC,
286 256,
287 "AES-256-CBC",
288 16,
289 0,
290 16,
291 &aes_info
292 };
293 #endif /* MBEDTLS_CIPHER_MODE_CBC */
294
295 #if defined(MBEDTLS_CIPHER_MODE_CFB)
296 static const mbedtls_cipher_info_t aes_128_cfb128_info = {
297 MBEDTLS_CIPHER_AES_128_CFB128,
298 MBEDTLS_MODE_CFB,
299 128,
300 "AES-128-CFB128",
301 16,
302 0,
303 16,
304 &aes_info
305 };
306
307 static const mbedtls_cipher_info_t aes_192_cfb128_info = {
308 MBEDTLS_CIPHER_AES_192_CFB128,
309 MBEDTLS_MODE_CFB,
310 192,
311 "AES-192-CFB128",
312 16,
313 0,
314 16,
315 &aes_info
316 };
317
318 static const mbedtls_cipher_info_t aes_256_cfb128_info = {
319 MBEDTLS_CIPHER_AES_256_CFB128,
320 MBEDTLS_MODE_CFB,
321 256,
322 "AES-256-CFB128",
323 16,
324 0,
325 16,
326 &aes_info
327 };
328 #endif /* MBEDTLS_CIPHER_MODE_CFB */
329
330 #if defined(MBEDTLS_CIPHER_MODE_CTR)
331 static const mbedtls_cipher_info_t aes_128_ctr_info = {
332 MBEDTLS_CIPHER_AES_128_CTR,
333 MBEDTLS_MODE_CTR,
334 128,
335 "AES-128-CTR",
336 16,
337 0,
338 16,
339 &aes_info
340 };
341
342 static const mbedtls_cipher_info_t aes_192_ctr_info = {
343 MBEDTLS_CIPHER_AES_192_CTR,
344 MBEDTLS_MODE_CTR,
345 192,
346 "AES-192-CTR",
347 16,
348 0,
349 16,
350 &aes_info
351 };
352
353 static const mbedtls_cipher_info_t aes_256_ctr_info = {
354 MBEDTLS_CIPHER_AES_256_CTR,
355 MBEDTLS_MODE_CTR,
356 256,
357 "AES-256-CTR",
358 16,
359 0,
360 16,
361 &aes_info
362 };
363 #endif /* MBEDTLS_CIPHER_MODE_CTR */
364
365 #if defined(MBEDTLS_GCM_C)
gcm_aes_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)366 static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
367 unsigned int key_bitlen )
368 {
369 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
370 key, key_bitlen );
371 }
372
373 static const mbedtls_cipher_base_t gcm_aes_info = {
374 MBEDTLS_CIPHER_ID_AES,
375 NULL,
376 #if defined(MBEDTLS_CIPHER_MODE_CBC)
377 NULL,
378 #endif
379 #if defined(MBEDTLS_CIPHER_MODE_CFB)
380 NULL,
381 #endif
382 #if defined(MBEDTLS_CIPHER_MODE_CTR)
383 NULL,
384 #endif
385 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
386 NULL,
387 #endif
388 gcm_aes_setkey_wrap,
389 gcm_aes_setkey_wrap,
390 gcm_ctx_alloc,
391 gcm_ctx_free,
392 };
393
394 static const mbedtls_cipher_info_t aes_128_gcm_info = {
395 MBEDTLS_CIPHER_AES_128_GCM,
396 MBEDTLS_MODE_GCM,
397 128,
398 "AES-128-GCM",
399 12,
400 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
401 16,
402 &gcm_aes_info
403 };
404
405 static const mbedtls_cipher_info_t aes_192_gcm_info = {
406 MBEDTLS_CIPHER_AES_192_GCM,
407 MBEDTLS_MODE_GCM,
408 192,
409 "AES-192-GCM",
410 12,
411 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
412 16,
413 &gcm_aes_info
414 };
415
416 static const mbedtls_cipher_info_t aes_256_gcm_info = {
417 MBEDTLS_CIPHER_AES_256_GCM,
418 MBEDTLS_MODE_GCM,
419 256,
420 "AES-256-GCM",
421 12,
422 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
423 16,
424 &gcm_aes_info
425 };
426 #endif /* MBEDTLS_GCM_C */
427
428 #if defined(MBEDTLS_CCM_C)
ccm_aes_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)429 static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
430 unsigned int key_bitlen )
431 {
432 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
433 key, key_bitlen );
434 }
435
436 static const mbedtls_cipher_base_t ccm_aes_info = {
437 MBEDTLS_CIPHER_ID_AES,
438 NULL,
439 #if defined(MBEDTLS_CIPHER_MODE_CBC)
440 NULL,
441 #endif
442 #if defined(MBEDTLS_CIPHER_MODE_CFB)
443 NULL,
444 #endif
445 #if defined(MBEDTLS_CIPHER_MODE_CTR)
446 NULL,
447 #endif
448 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
449 NULL,
450 #endif
451 ccm_aes_setkey_wrap,
452 ccm_aes_setkey_wrap,
453 ccm_ctx_alloc,
454 ccm_ctx_free,
455 };
456
457 static const mbedtls_cipher_info_t aes_128_ccm_info = {
458 MBEDTLS_CIPHER_AES_128_CCM,
459 MBEDTLS_MODE_CCM,
460 128,
461 "AES-128-CCM",
462 12,
463 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
464 16,
465 &ccm_aes_info
466 };
467
468 static const mbedtls_cipher_info_t aes_192_ccm_info = {
469 MBEDTLS_CIPHER_AES_192_CCM,
470 MBEDTLS_MODE_CCM,
471 192,
472 "AES-192-CCM",
473 12,
474 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
475 16,
476 &ccm_aes_info
477 };
478
479 static const mbedtls_cipher_info_t aes_256_ccm_info = {
480 MBEDTLS_CIPHER_AES_256_CCM,
481 MBEDTLS_MODE_CCM,
482 256,
483 "AES-256-CCM",
484 12,
485 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
486 16,
487 &ccm_aes_info
488 };
489 #endif /* MBEDTLS_CCM_C */
490
491 #endif /* MBEDTLS_AES_C */
492
493 #if defined(MBEDTLS_CAMELLIA_C)
494
camellia_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)495 static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
496 const unsigned char *input, unsigned char *output )
497 {
498 return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
499 output );
500 }
501
502 #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)503 static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
504 size_t length, unsigned char *iv,
505 const unsigned char *input, unsigned char *output )
506 {
507 return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
508 input, output );
509 }
510 #endif /* MBEDTLS_CIPHER_MODE_CBC */
511
512 #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)513 static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
514 size_t length, size_t *iv_off, unsigned char *iv,
515 const unsigned char *input, unsigned char *output )
516 {
517 return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
518 iv_off, iv, input, output );
519 }
520 #endif /* MBEDTLS_CIPHER_MODE_CFB */
521
522 #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)523 static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
524 unsigned char *nonce_counter, unsigned char *stream_block,
525 const unsigned char *input, unsigned char *output )
526 {
527 return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
528 nonce_counter, stream_block, input, output );
529 }
530 #endif /* MBEDTLS_CIPHER_MODE_CTR */
531
camellia_setkey_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)532 static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
533 unsigned int key_bitlen )
534 {
535 return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
536 }
537
camellia_setkey_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)538 static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
539 unsigned int key_bitlen )
540 {
541 return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
542 }
543
camellia_ctx_alloc(void)544 static void * camellia_ctx_alloc( void )
545 {
546 mbedtls_camellia_context *ctx;
547 ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
548
549 if( ctx == NULL )
550 return( NULL );
551
552 mbedtls_camellia_init( ctx );
553
554 return( ctx );
555 }
556
camellia_ctx_free(void * ctx)557 static void camellia_ctx_free( void *ctx )
558 {
559 mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
560 mbedtls_free( ctx );
561 }
562
563 static const mbedtls_cipher_base_t camellia_info = {
564 MBEDTLS_CIPHER_ID_CAMELLIA,
565 camellia_crypt_ecb_wrap,
566 #if defined(MBEDTLS_CIPHER_MODE_CBC)
567 camellia_crypt_cbc_wrap,
568 #endif
569 #if defined(MBEDTLS_CIPHER_MODE_CFB)
570 camellia_crypt_cfb128_wrap,
571 #endif
572 #if defined(MBEDTLS_CIPHER_MODE_CTR)
573 camellia_crypt_ctr_wrap,
574 #endif
575 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
576 NULL,
577 #endif
578 camellia_setkey_enc_wrap,
579 camellia_setkey_dec_wrap,
580 camellia_ctx_alloc,
581 camellia_ctx_free
582 };
583
584 static const mbedtls_cipher_info_t camellia_128_ecb_info = {
585 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
586 MBEDTLS_MODE_ECB,
587 128,
588 "CAMELLIA-128-ECB",
589 0,
590 0,
591 16,
592 &camellia_info
593 };
594
595 static const mbedtls_cipher_info_t camellia_192_ecb_info = {
596 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
597 MBEDTLS_MODE_ECB,
598 192,
599 "CAMELLIA-192-ECB",
600 0,
601 0,
602 16,
603 &camellia_info
604 };
605
606 static const mbedtls_cipher_info_t camellia_256_ecb_info = {
607 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
608 MBEDTLS_MODE_ECB,
609 256,
610 "CAMELLIA-256-ECB",
611 0,
612 0,
613 16,
614 &camellia_info
615 };
616
617 #if defined(MBEDTLS_CIPHER_MODE_CBC)
618 static const mbedtls_cipher_info_t camellia_128_cbc_info = {
619 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
620 MBEDTLS_MODE_CBC,
621 128,
622 "CAMELLIA-128-CBC",
623 16,
624 0,
625 16,
626 &camellia_info
627 };
628
629 static const mbedtls_cipher_info_t camellia_192_cbc_info = {
630 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
631 MBEDTLS_MODE_CBC,
632 192,
633 "CAMELLIA-192-CBC",
634 16,
635 0,
636 16,
637 &camellia_info
638 };
639
640 static const mbedtls_cipher_info_t camellia_256_cbc_info = {
641 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
642 MBEDTLS_MODE_CBC,
643 256,
644 "CAMELLIA-256-CBC",
645 16,
646 0,
647 16,
648 &camellia_info
649 };
650 #endif /* MBEDTLS_CIPHER_MODE_CBC */
651
652 #if defined(MBEDTLS_CIPHER_MODE_CFB)
653 static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
654 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
655 MBEDTLS_MODE_CFB,
656 128,
657 "CAMELLIA-128-CFB128",
658 16,
659 0,
660 16,
661 &camellia_info
662 };
663
664 static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
665 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
666 MBEDTLS_MODE_CFB,
667 192,
668 "CAMELLIA-192-CFB128",
669 16,
670 0,
671 16,
672 &camellia_info
673 };
674
675 static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
676 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
677 MBEDTLS_MODE_CFB,
678 256,
679 "CAMELLIA-256-CFB128",
680 16,
681 0,
682 16,
683 &camellia_info
684 };
685 #endif /* MBEDTLS_CIPHER_MODE_CFB */
686
687 #if defined(MBEDTLS_CIPHER_MODE_CTR)
688 static const mbedtls_cipher_info_t camellia_128_ctr_info = {
689 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
690 MBEDTLS_MODE_CTR,
691 128,
692 "CAMELLIA-128-CTR",
693 16,
694 0,
695 16,
696 &camellia_info
697 };
698
699 static const mbedtls_cipher_info_t camellia_192_ctr_info = {
700 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
701 MBEDTLS_MODE_CTR,
702 192,
703 "CAMELLIA-192-CTR",
704 16,
705 0,
706 16,
707 &camellia_info
708 };
709
710 static const mbedtls_cipher_info_t camellia_256_ctr_info = {
711 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
712 MBEDTLS_MODE_CTR,
713 256,
714 "CAMELLIA-256-CTR",
715 16,
716 0,
717 16,
718 &camellia_info
719 };
720 #endif /* MBEDTLS_CIPHER_MODE_CTR */
721
722 #if defined(MBEDTLS_GCM_C)
gcm_camellia_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)723 static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
724 unsigned int key_bitlen )
725 {
726 return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
727 key, key_bitlen );
728 }
729
730 static const mbedtls_cipher_base_t gcm_camellia_info = {
731 MBEDTLS_CIPHER_ID_CAMELLIA,
732 NULL,
733 #if defined(MBEDTLS_CIPHER_MODE_CBC)
734 NULL,
735 #endif
736 #if defined(MBEDTLS_CIPHER_MODE_CFB)
737 NULL,
738 #endif
739 #if defined(MBEDTLS_CIPHER_MODE_CTR)
740 NULL,
741 #endif
742 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
743 NULL,
744 #endif
745 gcm_camellia_setkey_wrap,
746 gcm_camellia_setkey_wrap,
747 gcm_ctx_alloc,
748 gcm_ctx_free,
749 };
750
751 static const mbedtls_cipher_info_t camellia_128_gcm_info = {
752 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
753 MBEDTLS_MODE_GCM,
754 128,
755 "CAMELLIA-128-GCM",
756 12,
757 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
758 16,
759 &gcm_camellia_info
760 };
761
762 static const mbedtls_cipher_info_t camellia_192_gcm_info = {
763 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
764 MBEDTLS_MODE_GCM,
765 192,
766 "CAMELLIA-192-GCM",
767 12,
768 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
769 16,
770 &gcm_camellia_info
771 };
772
773 static const mbedtls_cipher_info_t camellia_256_gcm_info = {
774 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
775 MBEDTLS_MODE_GCM,
776 256,
777 "CAMELLIA-256-GCM",
778 12,
779 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
780 16,
781 &gcm_camellia_info
782 };
783 #endif /* MBEDTLS_GCM_C */
784
785 #if defined(MBEDTLS_CCM_C)
ccm_camellia_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)786 static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
787 unsigned int key_bitlen )
788 {
789 return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
790 key, key_bitlen );
791 }
792
793 static const mbedtls_cipher_base_t ccm_camellia_info = {
794 MBEDTLS_CIPHER_ID_CAMELLIA,
795 NULL,
796 #if defined(MBEDTLS_CIPHER_MODE_CBC)
797 NULL,
798 #endif
799 #if defined(MBEDTLS_CIPHER_MODE_CFB)
800 NULL,
801 #endif
802 #if defined(MBEDTLS_CIPHER_MODE_CTR)
803 NULL,
804 #endif
805 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
806 NULL,
807 #endif
808 ccm_camellia_setkey_wrap,
809 ccm_camellia_setkey_wrap,
810 ccm_ctx_alloc,
811 ccm_ctx_free,
812 };
813
814 static const mbedtls_cipher_info_t camellia_128_ccm_info = {
815 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
816 MBEDTLS_MODE_CCM,
817 128,
818 "CAMELLIA-128-CCM",
819 12,
820 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
821 16,
822 &ccm_camellia_info
823 };
824
825 static const mbedtls_cipher_info_t camellia_192_ccm_info = {
826 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
827 MBEDTLS_MODE_CCM,
828 192,
829 "CAMELLIA-192-CCM",
830 12,
831 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
832 16,
833 &ccm_camellia_info
834 };
835
836 static const mbedtls_cipher_info_t camellia_256_ccm_info = {
837 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
838 MBEDTLS_MODE_CCM,
839 256,
840 "CAMELLIA-256-CCM",
841 12,
842 MBEDTLS_CIPHER_VARIABLE_IV_LEN,
843 16,
844 &ccm_camellia_info
845 };
846 #endif /* MBEDTLS_CCM_C */
847
848 #endif /* MBEDTLS_CAMELLIA_C */
849
850 #if defined(MBEDTLS_DES_C)
851
des_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)852 static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
853 const unsigned char *input, unsigned char *output )
854 {
855 ((void) operation);
856 return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
857 }
858
des3_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)859 static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
860 const unsigned char *input, unsigned char *output )
861 {
862 ((void) operation);
863 return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
864 }
865
866 #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)867 static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
868 unsigned char *iv, const unsigned char *input, unsigned char *output )
869 {
870 return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
871 output );
872 }
873 #endif /* MBEDTLS_CIPHER_MODE_CBC */
874
875 #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)876 static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
877 unsigned char *iv, const unsigned char *input, unsigned char *output )
878 {
879 return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
880 output );
881 }
882 #endif /* MBEDTLS_CIPHER_MODE_CBC */
883
des_setkey_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)884 static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
885 unsigned int key_bitlen )
886 {
887 ((void) key_bitlen);
888
889 return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
890 }
891
des_setkey_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)892 static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
893 unsigned int key_bitlen )
894 {
895 ((void) key_bitlen);
896
897 return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
898 }
899
des3_set2key_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)900 static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
901 unsigned int key_bitlen )
902 {
903 ((void) key_bitlen);
904
905 return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
906 }
907
des3_set2key_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)908 static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
909 unsigned int key_bitlen )
910 {
911 ((void) key_bitlen);
912
913 return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
914 }
915
des3_set3key_dec_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)916 static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
917 unsigned int key_bitlen )
918 {
919 ((void) key_bitlen);
920
921 return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
922 }
923
des3_set3key_enc_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)924 static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
925 unsigned int key_bitlen )
926 {
927 ((void) key_bitlen);
928
929 return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
930 }
931
des_ctx_alloc(void)932 static void * des_ctx_alloc( void )
933 {
934 mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
935
936 if( des == NULL )
937 return( NULL );
938
939 mbedtls_des_init( des );
940
941 return( des );
942 }
943
des_ctx_free(void * ctx)944 static void des_ctx_free( void *ctx )
945 {
946 mbedtls_des_free( (mbedtls_des_context *) ctx );
947 mbedtls_free( ctx );
948 }
949
des3_ctx_alloc(void)950 static void * des3_ctx_alloc( void )
951 {
952 mbedtls_des3_context *des3;
953 des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
954
955 if( des3 == NULL )
956 return( NULL );
957
958 mbedtls_des3_init( des3 );
959
960 return( des3 );
961 }
962
des3_ctx_free(void * ctx)963 static void des3_ctx_free( void *ctx )
964 {
965 mbedtls_des3_free( (mbedtls_des3_context *) ctx );
966 mbedtls_free( ctx );
967 }
968
969 static const mbedtls_cipher_base_t des_info = {
970 MBEDTLS_CIPHER_ID_DES,
971 des_crypt_ecb_wrap,
972 #if defined(MBEDTLS_CIPHER_MODE_CBC)
973 des_crypt_cbc_wrap,
974 #endif
975 #if defined(MBEDTLS_CIPHER_MODE_CFB)
976 NULL,
977 #endif
978 #if defined(MBEDTLS_CIPHER_MODE_CTR)
979 NULL,
980 #endif
981 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
982 NULL,
983 #endif
984 des_setkey_enc_wrap,
985 des_setkey_dec_wrap,
986 des_ctx_alloc,
987 des_ctx_free
988 };
989
990 static const mbedtls_cipher_info_t des_ecb_info = {
991 MBEDTLS_CIPHER_DES_ECB,
992 MBEDTLS_MODE_ECB,
993 MBEDTLS_KEY_LENGTH_DES,
994 "DES-ECB",
995 0,
996 0,
997 8,
998 &des_info
999 };
1000
1001 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1002 static const mbedtls_cipher_info_t des_cbc_info = {
1003 MBEDTLS_CIPHER_DES_CBC,
1004 MBEDTLS_MODE_CBC,
1005 MBEDTLS_KEY_LENGTH_DES,
1006 "DES-CBC",
1007 8,
1008 0,
1009 8,
1010 &des_info
1011 };
1012 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1013
1014 static const mbedtls_cipher_base_t des_ede_info = {
1015 MBEDTLS_CIPHER_ID_DES,
1016 des3_crypt_ecb_wrap,
1017 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1018 des3_crypt_cbc_wrap,
1019 #endif
1020 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1021 NULL,
1022 #endif
1023 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1024 NULL,
1025 #endif
1026 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1027 NULL,
1028 #endif
1029 des3_set2key_enc_wrap,
1030 des3_set2key_dec_wrap,
1031 des3_ctx_alloc,
1032 des3_ctx_free
1033 };
1034
1035 static const mbedtls_cipher_info_t des_ede_ecb_info = {
1036 MBEDTLS_CIPHER_DES_EDE_ECB,
1037 MBEDTLS_MODE_ECB,
1038 MBEDTLS_KEY_LENGTH_DES_EDE,
1039 "DES-EDE-ECB",
1040 0,
1041 0,
1042 8,
1043 &des_ede_info
1044 };
1045
1046 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1047 static const mbedtls_cipher_info_t des_ede_cbc_info = {
1048 MBEDTLS_CIPHER_DES_EDE_CBC,
1049 MBEDTLS_MODE_CBC,
1050 MBEDTLS_KEY_LENGTH_DES_EDE,
1051 "DES-EDE-CBC",
1052 8,
1053 0,
1054 8,
1055 &des_ede_info
1056 };
1057 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1058
1059 static const mbedtls_cipher_base_t des_ede3_info = {
1060 MBEDTLS_CIPHER_ID_3DES,
1061 des3_crypt_ecb_wrap,
1062 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1063 des3_crypt_cbc_wrap,
1064 #endif
1065 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1066 NULL,
1067 #endif
1068 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1069 NULL,
1070 #endif
1071 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1072 NULL,
1073 #endif
1074 des3_set3key_enc_wrap,
1075 des3_set3key_dec_wrap,
1076 des3_ctx_alloc,
1077 des3_ctx_free
1078 };
1079
1080 static const mbedtls_cipher_info_t des_ede3_ecb_info = {
1081 MBEDTLS_CIPHER_DES_EDE3_ECB,
1082 MBEDTLS_MODE_ECB,
1083 MBEDTLS_KEY_LENGTH_DES_EDE3,
1084 "DES-EDE3-ECB",
1085 0,
1086 0,
1087 8,
1088 &des_ede3_info
1089 };
1090 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1091 static const mbedtls_cipher_info_t des_ede3_cbc_info = {
1092 MBEDTLS_CIPHER_DES_EDE3_CBC,
1093 MBEDTLS_MODE_CBC,
1094 MBEDTLS_KEY_LENGTH_DES_EDE3,
1095 "DES-EDE3-CBC",
1096 8,
1097 0,
1098 8,
1099 &des_ede3_info
1100 };
1101 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1102 #endif /* MBEDTLS_DES_C */
1103
1104 #if defined(MBEDTLS_BLOWFISH_C)
1105
blowfish_crypt_ecb_wrap(void * ctx,mbedtls_operation_t operation,const unsigned char * input,unsigned char * output)1106 static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
1107 const unsigned char *input, unsigned char *output )
1108 {
1109 return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
1110 output );
1111 }
1112
1113 #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)1114 static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
1115 size_t length, unsigned char *iv, const unsigned char *input,
1116 unsigned char *output )
1117 {
1118 return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
1119 input, output );
1120 }
1121 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1122
1123 #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)1124 static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
1125 size_t length, size_t *iv_off, unsigned char *iv,
1126 const unsigned char *input, unsigned char *output )
1127 {
1128 return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
1129 iv_off, iv, input, output );
1130 }
1131 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1132
1133 #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)1134 static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
1135 unsigned char *nonce_counter, unsigned char *stream_block,
1136 const unsigned char *input, unsigned char *output )
1137 {
1138 return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
1139 nonce_counter, stream_block, input, output );
1140 }
1141 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1142
blowfish_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1143 static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
1144 unsigned int key_bitlen )
1145 {
1146 return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
1147 }
1148
blowfish_ctx_alloc(void)1149 static void * blowfish_ctx_alloc( void )
1150 {
1151 mbedtls_blowfish_context *ctx;
1152 ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
1153
1154 if( ctx == NULL )
1155 return( NULL );
1156
1157 mbedtls_blowfish_init( ctx );
1158
1159 return( ctx );
1160 }
1161
blowfish_ctx_free(void * ctx)1162 static void blowfish_ctx_free( void *ctx )
1163 {
1164 mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
1165 mbedtls_free( ctx );
1166 }
1167
1168 static const mbedtls_cipher_base_t blowfish_info = {
1169 MBEDTLS_CIPHER_ID_BLOWFISH,
1170 blowfish_crypt_ecb_wrap,
1171 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1172 blowfish_crypt_cbc_wrap,
1173 #endif
1174 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1175 blowfish_crypt_cfb64_wrap,
1176 #endif
1177 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1178 blowfish_crypt_ctr_wrap,
1179 #endif
1180 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1181 NULL,
1182 #endif
1183 blowfish_setkey_wrap,
1184 blowfish_setkey_wrap,
1185 blowfish_ctx_alloc,
1186 blowfish_ctx_free
1187 };
1188
1189 static const mbedtls_cipher_info_t blowfish_ecb_info = {
1190 MBEDTLS_CIPHER_BLOWFISH_ECB,
1191 MBEDTLS_MODE_ECB,
1192 128,
1193 "BLOWFISH-ECB",
1194 0,
1195 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1196 8,
1197 &blowfish_info
1198 };
1199
1200 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1201 static const mbedtls_cipher_info_t blowfish_cbc_info = {
1202 MBEDTLS_CIPHER_BLOWFISH_CBC,
1203 MBEDTLS_MODE_CBC,
1204 128,
1205 "BLOWFISH-CBC",
1206 8,
1207 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1208 8,
1209 &blowfish_info
1210 };
1211 #endif /* MBEDTLS_CIPHER_MODE_CBC */
1212
1213 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1214 static const mbedtls_cipher_info_t blowfish_cfb64_info = {
1215 MBEDTLS_CIPHER_BLOWFISH_CFB64,
1216 MBEDTLS_MODE_CFB,
1217 128,
1218 "BLOWFISH-CFB64",
1219 8,
1220 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1221 8,
1222 &blowfish_info
1223 };
1224 #endif /* MBEDTLS_CIPHER_MODE_CFB */
1225
1226 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1227 static const mbedtls_cipher_info_t blowfish_ctr_info = {
1228 MBEDTLS_CIPHER_BLOWFISH_CTR,
1229 MBEDTLS_MODE_CTR,
1230 128,
1231 "BLOWFISH-CTR",
1232 8,
1233 MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
1234 8,
1235 &blowfish_info
1236 };
1237 #endif /* MBEDTLS_CIPHER_MODE_CTR */
1238 #endif /* MBEDTLS_BLOWFISH_C */
1239
1240 #if defined(MBEDTLS_ARC4_C)
arc4_crypt_stream_wrap(void * ctx,size_t length,const unsigned char * input,unsigned char * output)1241 static int arc4_crypt_stream_wrap( void *ctx, size_t length,
1242 const unsigned char *input,
1243 unsigned char *output )
1244 {
1245 return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
1246 }
1247
arc4_setkey_wrap(void * ctx,const unsigned char * key,unsigned int key_bitlen)1248 static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
1249 unsigned int key_bitlen )
1250 {
1251 /* we get key_bitlen in bits, arc4 expects it in bytes */
1252 if( key_bitlen % 8 != 0 )
1253 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
1254
1255 mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
1256 return( 0 );
1257 }
1258
arc4_ctx_alloc(void)1259 static void * arc4_ctx_alloc( void )
1260 {
1261 mbedtls_arc4_context *ctx;
1262 ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
1263
1264 if( ctx == NULL )
1265 return( NULL );
1266
1267 mbedtls_arc4_init( ctx );
1268
1269 return( ctx );
1270 }
1271
arc4_ctx_free(void * ctx)1272 static void arc4_ctx_free( void *ctx )
1273 {
1274 mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
1275 mbedtls_free( ctx );
1276 }
1277
1278 static const mbedtls_cipher_base_t arc4_base_info = {
1279 MBEDTLS_CIPHER_ID_ARC4,
1280 NULL,
1281 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1282 NULL,
1283 #endif
1284 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1285 NULL,
1286 #endif
1287 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1288 NULL,
1289 #endif
1290 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1291 arc4_crypt_stream_wrap,
1292 #endif
1293 arc4_setkey_wrap,
1294 arc4_setkey_wrap,
1295 arc4_ctx_alloc,
1296 arc4_ctx_free
1297 };
1298
1299 static const mbedtls_cipher_info_t arc4_128_info = {
1300 MBEDTLS_CIPHER_ARC4_128,
1301 MBEDTLS_MODE_STREAM,
1302 128,
1303 "ARC4-128",
1304 0,
1305 0,
1306 1,
1307 &arc4_base_info
1308 };
1309 #endif /* MBEDTLS_ARC4_C */
1310
1311 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
null_crypt_stream(void * ctx,size_t length,const unsigned char * input,unsigned char * output)1312 static int null_crypt_stream( void *ctx, size_t length,
1313 const unsigned char *input,
1314 unsigned char *output )
1315 {
1316 ((void) ctx);
1317 memmove( output, input, length );
1318 return( 0 );
1319 }
1320
null_setkey(void * ctx,const unsigned char * key,unsigned int key_bitlen)1321 static int null_setkey( void *ctx, const unsigned char *key,
1322 unsigned int key_bitlen )
1323 {
1324 ((void) ctx);
1325 ((void) key);
1326 ((void) key_bitlen);
1327
1328 return( 0 );
1329 }
1330
null_ctx_alloc(void)1331 static void * null_ctx_alloc( void )
1332 {
1333 return( (void *) 1 );
1334 }
1335
null_ctx_free(void * ctx)1336 static void null_ctx_free( void *ctx )
1337 {
1338 ((void) ctx);
1339 }
1340
1341 static const mbedtls_cipher_base_t null_base_info = {
1342 MBEDTLS_CIPHER_ID_NULL,
1343 NULL,
1344 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1345 NULL,
1346 #endif
1347 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1348 NULL,
1349 #endif
1350 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1351 NULL,
1352 #endif
1353 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
1354 null_crypt_stream,
1355 #endif
1356 null_setkey,
1357 null_setkey,
1358 null_ctx_alloc,
1359 null_ctx_free
1360 };
1361
1362 static const mbedtls_cipher_info_t null_cipher_info = {
1363 MBEDTLS_CIPHER_NULL,
1364 MBEDTLS_MODE_STREAM,
1365 0,
1366 "NULL",
1367 0,
1368 0,
1369 1,
1370 &null_base_info
1371 };
1372 #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
1373
1374 const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
1375 {
1376 #if defined(MBEDTLS_AES_C)
1377 { MBEDTLS_CIPHER_AES_128_ECB, &aes_128_ecb_info },
1378 { MBEDTLS_CIPHER_AES_192_ECB, &aes_192_ecb_info },
1379 { MBEDTLS_CIPHER_AES_256_ECB, &aes_256_ecb_info },
1380 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1381 { MBEDTLS_CIPHER_AES_128_CBC, &aes_128_cbc_info },
1382 { MBEDTLS_CIPHER_AES_192_CBC, &aes_192_cbc_info },
1383 { MBEDTLS_CIPHER_AES_256_CBC, &aes_256_cbc_info },
1384 #endif
1385 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1386 { MBEDTLS_CIPHER_AES_128_CFB128, &aes_128_cfb128_info },
1387 { MBEDTLS_CIPHER_AES_192_CFB128, &aes_192_cfb128_info },
1388 { MBEDTLS_CIPHER_AES_256_CFB128, &aes_256_cfb128_info },
1389 #endif
1390 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1391 { MBEDTLS_CIPHER_AES_128_CTR, &aes_128_ctr_info },
1392 { MBEDTLS_CIPHER_AES_192_CTR, &aes_192_ctr_info },
1393 { MBEDTLS_CIPHER_AES_256_CTR, &aes_256_ctr_info },
1394 #endif
1395 #if defined(MBEDTLS_GCM_C)
1396 { MBEDTLS_CIPHER_AES_128_GCM, &aes_128_gcm_info },
1397 { MBEDTLS_CIPHER_AES_192_GCM, &aes_192_gcm_info },
1398 { MBEDTLS_CIPHER_AES_256_GCM, &aes_256_gcm_info },
1399 #endif
1400 #if defined(MBEDTLS_CCM_C)
1401 { MBEDTLS_CIPHER_AES_128_CCM, &aes_128_ccm_info },
1402 { MBEDTLS_CIPHER_AES_192_CCM, &aes_192_ccm_info },
1403 { MBEDTLS_CIPHER_AES_256_CCM, &aes_256_ccm_info },
1404 #endif
1405 #endif /* MBEDTLS_AES_C */
1406
1407 #if defined(MBEDTLS_ARC4_C)
1408 { MBEDTLS_CIPHER_ARC4_128, &arc4_128_info },
1409 #endif
1410
1411 #if defined(MBEDTLS_BLOWFISH_C)
1412 { MBEDTLS_CIPHER_BLOWFISH_ECB, &blowfish_ecb_info },
1413 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1414 { MBEDTLS_CIPHER_BLOWFISH_CBC, &blowfish_cbc_info },
1415 #endif
1416 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1417 { MBEDTLS_CIPHER_BLOWFISH_CFB64, &blowfish_cfb64_info },
1418 #endif
1419 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1420 { MBEDTLS_CIPHER_BLOWFISH_CTR, &blowfish_ctr_info },
1421 #endif
1422 #endif /* MBEDTLS_BLOWFISH_C */
1423
1424 #if defined(MBEDTLS_CAMELLIA_C)
1425 { MBEDTLS_CIPHER_CAMELLIA_128_ECB, &camellia_128_ecb_info },
1426 { MBEDTLS_CIPHER_CAMELLIA_192_ECB, &camellia_192_ecb_info },
1427 { MBEDTLS_CIPHER_CAMELLIA_256_ECB, &camellia_256_ecb_info },
1428 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1429 { MBEDTLS_CIPHER_CAMELLIA_128_CBC, &camellia_128_cbc_info },
1430 { MBEDTLS_CIPHER_CAMELLIA_192_CBC, &camellia_192_cbc_info },
1431 { MBEDTLS_CIPHER_CAMELLIA_256_CBC, &camellia_256_cbc_info },
1432 #endif
1433 #if defined(MBEDTLS_CIPHER_MODE_CFB)
1434 { MBEDTLS_CIPHER_CAMELLIA_128_CFB128, &camellia_128_cfb128_info },
1435 { MBEDTLS_CIPHER_CAMELLIA_192_CFB128, &camellia_192_cfb128_info },
1436 { MBEDTLS_CIPHER_CAMELLIA_256_CFB128, &camellia_256_cfb128_info },
1437 #endif
1438 #if defined(MBEDTLS_CIPHER_MODE_CTR)
1439 { MBEDTLS_CIPHER_CAMELLIA_128_CTR, &camellia_128_ctr_info },
1440 { MBEDTLS_CIPHER_CAMELLIA_192_CTR, &camellia_192_ctr_info },
1441 { MBEDTLS_CIPHER_CAMELLIA_256_CTR, &camellia_256_ctr_info },
1442 #endif
1443 #if defined(MBEDTLS_GCM_C)
1444 { MBEDTLS_CIPHER_CAMELLIA_128_GCM, &camellia_128_gcm_info },
1445 { MBEDTLS_CIPHER_CAMELLIA_192_GCM, &camellia_192_gcm_info },
1446 { MBEDTLS_CIPHER_CAMELLIA_256_GCM, &camellia_256_gcm_info },
1447 #endif
1448 #if defined(MBEDTLS_CCM_C)
1449 { MBEDTLS_CIPHER_CAMELLIA_128_CCM, &camellia_128_ccm_info },
1450 { MBEDTLS_CIPHER_CAMELLIA_192_CCM, &camellia_192_ccm_info },
1451 { MBEDTLS_CIPHER_CAMELLIA_256_CCM, &camellia_256_ccm_info },
1452 #endif
1453 #endif /* MBEDTLS_CAMELLIA_C */
1454
1455 #if defined(MBEDTLS_DES_C)
1456 { MBEDTLS_CIPHER_DES_ECB, &des_ecb_info },
1457 { MBEDTLS_CIPHER_DES_EDE_ECB, &des_ede_ecb_info },
1458 { MBEDTLS_CIPHER_DES_EDE3_ECB, &des_ede3_ecb_info },
1459 #if defined(MBEDTLS_CIPHER_MODE_CBC)
1460 { MBEDTLS_CIPHER_DES_CBC, &des_cbc_info },
1461 { MBEDTLS_CIPHER_DES_EDE_CBC, &des_ede_cbc_info },
1462 { MBEDTLS_CIPHER_DES_EDE3_CBC, &des_ede3_cbc_info },
1463 #endif
1464 #endif /* MBEDTLS_DES_C */
1465
1466 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
1467 { MBEDTLS_CIPHER_NULL, &null_cipher_info },
1468 #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
1469
1470 { MBEDTLS_CIPHER_NONE, NULL }
1471 };
1472
1473 #define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
1474 int mbedtls_cipher_supported[NUM_CIPHERS];
1475
1476 #endif /* MBEDTLS_CIPHER_C */
1477