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