1 /** ---- SYMMETRIC KEY STUFF ----- 2 * 3 * We put each of the ciphers scheduled keys in their own structs then we put all of 4 * the key formats in one union. This makes the function prototypes easier to use. 5 */ 6 module tomcrypt.cipher; 7 8 import tomcrypt.custom; 9 import tomcrypt.tomcrypt; 10 import core.stdc.config; 11 12 extern(C) nothrow: 13 14 version(LTC_BLOWFISH) 15 { 16 struct blowfish_key 17 { 18 uint[4][256] S; 19 uint[18] K; 20 } 21 } 22 23 version(LTC_RC5) 24 { 25 struct rc5_key 26 { 27 int rounds; 28 uint[50] K; 29 } 30 } 31 32 version(LTC_RC6) 33 { 34 struct rc6_key 35 { 36 uint[44] K; 37 } 38 } 39 40 version(LTC_SAFERP) 41 { 42 struct saferp_key 43 { 44 ubyte[33][16] K; 45 long rounds; 46 } 47 } 48 49 version(LTC_RIJNDAEL) 50 { 51 struct rijndael_key 52 { 53 uint[60] eK, dK; 54 int Nr; 55 } 56 } 57 58 version(LTC_KSEED) 59 { 60 struct kseed_key 61 { 62 uint[32] K, dK; 63 } 64 } 65 66 version(LTC_KASUMI) 67 { 68 struct kasumi_key 69 { 70 uint[8] KLi1, KLi2, 71 KOi1, KOi2, KOi3, 72 KIi1, KIi2, KIi3; 73 } 74 } 75 76 version(LTC_XTEA) 77 { 78 struct xtea_key 79 { 80 c_ulong[32] A, B; 81 } 82 } 83 84 version(LTC_TWOFISH) 85 { 86 version(LTC_TWOFISH_SMALL) 87 { 88 struct twofish_key 89 { 90 uint[40] K; 91 ubyte[32] S; 92 ubyte start; 93 } 94 } 95 else 96 { 97 struct twofish_key 98 { 99 uint[4][256] S; 100 uint[40] K; 101 } 102 } 103 } 104 105 version(LTC_SAFER) 106 { 107 enum LTC_SAFER_K64_DEFAULT_NOF_ROUNDS = 6; 108 enum LTC_SAFER_K128_DEFAULT_NOF_ROUNDS = 10; 109 enum LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS = 8; 110 enum LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS = 10; 111 enum LTC_SAFER_MAX_NOF_ROUNDS = 13; 112 enum LTC_SAFER_BLOCK_LEN = 8; 113 enum LTC_SAFER_KEY_LEN = (1 + LTC_SAFER_BLOCK_LEN * (1 + 2 * LTC_SAFER_MAX_NOF_ROUNDS)); 114 alias ubyte safer_block_t[LTC_SAFER_BLOCK_LEN]; 115 alias ubyte safer_key_t[LTC_SAFER_KEY_LEN]; 116 struct safer_key { safer_key_t key; }; 117 } 118 119 version(LTC_RC2) 120 { 121 struct rc2_key { uint[64] xkey; }; 122 } 123 124 version(LTC_DES) 125 { 126 struct des_key 127 { 128 uint[32] ek, dk; 129 } 130 131 struct des3_key 132 { 133 uint[3][32] ek, dk; 134 } 135 } 136 137 version(LTC_CAST5) 138 { 139 struct cast5_key 140 { 141 uint[32] K; 142 uint keylen; 143 } 144 } 145 146 version(LTC_NOEKEON) 147 { 148 struct noekeon_key 149 { 150 uint[4] K, dK; 151 } 152 } 153 154 version(LTC_SKIPJACK) 155 { 156 struct skipjack_key 157 { 158 ubyte[10] key; 159 } 160 } 161 162 version(LTC_KHAZAD) 163 { 164 struct khazad_key 165 { 166 ulong[8 + 1] roundKeyEnc; 167 ulong[8 + 1] roundKeyDec; 168 } 169 } 170 171 version(LTC_ANUBIS) 172 { 173 struct anubis_key 174 { 175 int keyBits; 176 int R; 177 uint[18 + 1][4] roundKeyEnc; 178 uint[18 + 1][4] roundKeyDec; 179 } 180 } 181 182 version(LTC_MULTI2) 183 { 184 struct multi2_key 185 { 186 int N; 187 uint[8] uk; 188 } 189 } 190 191 union Symmetric_key 192 { 193 version(LTC_DES) 194 { 195 des_key des; 196 des3_key des3; 197 } 198 199 version(LTC_RC2) 200 { 201 rc2_key rc2; 202 } 203 204 version(LTC_SAFER) 205 { 206 safer_key safer; 207 } 208 209 version(LTC_TWOFISH) 210 { 211 twofish_key twofish; 212 } 213 214 version(LTC_BLOWFISH) 215 { 216 blowfish_key blowfish; 217 } 218 219 version(LTC_RC5) 220 { 221 rc5_key rc5; 222 } 223 224 version(LTC_RC6) 225 { 226 rc6_key rc6; 227 } 228 229 version(LTC_SAFERP) 230 { 231 saferp_key saferp; 232 } 233 234 version(LTC_RIJNDAEL) 235 { 236 rijndael_key rijndael; 237 } 238 239 version(LTC_XTEA) 240 { 241 xtea_key xtea; 242 } 243 244 version(LTC_CAST5) 245 { 246 cast5_key cast5; 247 } 248 249 version(LTC_NOEKEON) 250 { 251 noekeon_key noekeon; 252 } 253 254 version(LTC_SKIPJACK) 255 { 256 skipjack_key skipjack; 257 } 258 259 version(LTC_KHAZAD) 260 { 261 khazad_key khazad; 262 } 263 264 version(LTC_ANUBIS) 265 { 266 anubis_key anubis; 267 } 268 269 version(LTC_KSEED) 270 { 271 kseed_key kseed; 272 } 273 274 version(LTC_KASUMI) 275 { 276 kasumi_key kasumi; 277 } 278 279 version(LTC_MULTI2) 280 { 281 multi2_key multi2; 282 } 283 284 void *data; 285 } 286 alias symmetric_key = Symmetric_key; 287 288 version(LTC_ECB_MODE) 289 { 290 /** A block cipher ECB structure */ 291 struct symmetric_ECB 292 { 293 /** The index of the cipher chosen */ 294 int cipher, 295 /** The block size of the given cipher */ 296 blocklen; 297 /** The scheduled key */ 298 symmetric_key key; 299 } 300 } 301 302 version(LTC_CFB_MODE) 303 { 304 /** A block cipher CFB structure */ 305 struct symmetric_CFB 306 { 307 /** The index of the cipher chosen */ 308 int cipher, 309 /** The block size of the given cipher */ 310 blocklen, 311 /** The padding offset */ 312 padlen; 313 /** The current IV */ 314 ubyte[MAXBLOCKSIZE] IV, 315 /** The pad used to encrypt/decrypt */ 316 pad; 317 /** The scheduled key */ 318 symmetric_key key; 319 } 320 } 321 322 version(LTC_OFB_MODE) 323 { 324 /** A block cipher OFB structure */ 325 struct symmetric_OFB 326 { 327 /** The index of the cipher chosen */ 328 int cipher, 329 /** The block size of the given cipher */ 330 blocklen, 331 /** The padding offset */ 332 padlen; 333 /** The current IV */ 334 ubyte[MAXBLOCKSIZE] IV; 335 /** The scheduled key */ 336 symmetric_key key; 337 } 338 } 339 340 version(LTC_CBC_MODE) 341 { 342 /** A block cipher CBC structure */ 343 struct symmetric_CBC 344 { 345 /** The index of the cipher chosen */ 346 int cipher, 347 /** The block size of the given cipher */ 348 blocklen; 349 /** The current IV */ 350 ubyte[MAXBLOCKSIZE] IV; 351 /** The scheduled key */ 352 symmetric_key key; 353 } 354 } 355 356 357 version(LTC_CTR_MODE) 358 { 359 /** A block cipher CTR structure */ 360 struct symmetric_CTR 361 { 362 /** The index of the cipher chosen */ 363 int cipher, 364 /** The block size of the given cipher */ 365 blocklen, 366 /** The padding offset */ 367 padlen, 368 /** The mode (endianess) of the CTR, 0==little, 1==big */ 369 mode, 370 /** counter width */ 371 ctrlen; 372 373 /** The counter */ 374 ubyte[MAXBLOCKSIZE] ctr, 375 /** The pad used to encrypt/decrypt */ 376 pad; 377 /** The scheduled key */ 378 symmetric_key key; 379 } 380 } 381 382 383 version(LTC_LRW_MODE) 384 { 385 /** A LRW structure */ 386 struct symmetric_LRW 387 { 388 /** The index of the cipher chosen (must be a 128-bit block cipher) */ 389 int cipher; 390 391 /** The current IV */ 392 ubyte[16] IV, 393 394 /** the tweak key */ 395 tweak, 396 397 /** The current pad, it's the product of the first 15 bytes against the tweak key */ 398 pad; 399 400 /** The scheduled symmetric key */ 401 symmetric_key key; 402 403 version(LRW_TABLES) 404 { 405 /** The pre-computed multiplication table */ 406 ubyte[16][256][16] PC; 407 } 408 } 409 } 410 411 version(LTC_F8_MODE) 412 { 413 /** A block cipher F8 structure */ 414 struct symmetric_F8 415 { 416 /** The index of the cipher chosen */ 417 int cipher, 418 /** The block size of the given cipher */ 419 blocklen, 420 /** The padding offset */ 421 padlen; 422 /** The current IV */ 423 ubyte[MAXBLOCKSIZE] IV, 424 MIV; 425 /** Current block count */ 426 uint blockcnt; 427 /** The scheduled key */ 428 symmetric_key key; 429 } 430 } 431 432 433 /** cipher descriptor table, last entry has "name == NULL" to mark the end of table */ 434 struct ltc_cipher_descriptor 435 { 436 /** name of cipher */ 437 char *name; 438 /** internal ID */ 439 ubyte ID; 440 /** min keysize (octets) */ 441 int min_key_length, 442 /** max keysize (octets) */ 443 max_key_length, 444 /** block size (octets) */ 445 block_length, 446 /** default number of rounds */ 447 default_rounds; 448 /** Setup the cipher 449 @param key The input symmetric key 450 @param keylen The length of the input key (octets) 451 @param num_rounds The requested number of rounds (0==default) 452 @param skey [out] The destination of the scheduled key 453 @return CRYPT_OK if successful 454 */ 455 int function(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey) setup; 456 /** Encrypt a block 457 @param pt The plaintext 458 @param ct [out] The ciphertext 459 @param skey The scheduled key 460 @return CRYPT_OK if successful 461 */ 462 int function(const ubyte *pt, ubyte *ct, symmetric_key *skey) ecb_encrypt; 463 /** Decrypt a block 464 @param ct The ciphertext 465 @param pt [out] The plaintext 466 @param skey The scheduled key 467 @return CRYPT_OK if successful 468 */ 469 int function(const ubyte *ct, ubyte *pt, symmetric_key *skey) ecb_decrypt; 470 /** Test the block cipher 471 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled 472 */ 473 int function() test; 474 475 /** Terminate the context 476 @param skey The scheduled key 477 */ 478 void function(symmetric_key *skey) done; 479 480 /** Determine a key size 481 @param keysize [in/out] The size of the key desired and the suggested size 482 @return CRYPT_OK if successful 483 */ 484 int function(int *keysize) keysize; 485 486 /** Accelerators **/ 487 /** Accelerated ECB encryption 488 @param pt Plaintext 489 @param ct Ciphertext 490 @param blocks The number of complete blocks to process 491 @param skey The scheduled key context 492 @return CRYPT_OK if successful 493 */ 494 int function(const ubyte *pt, ubyte *ct, c_ulong blocks, symmetric_key *skey) accel_ecb_encrypt; 495 496 /** Accelerated ECB decryption 497 @param pt Plaintext 498 @param ct Ciphertext 499 @param blocks The number of complete blocks to process 500 @param skey The scheduled key context 501 @return CRYPT_OK if successful 502 */ 503 int function(const ubyte *ct, ubyte *pt, c_ulong blocks, symmetric_key *skey) accel_ecb_decrypt; 504 505 /** Accelerated CBC encryption 506 @param pt Plaintext 507 @param ct Ciphertext 508 @param blocks The number of complete blocks to process 509 @param IV The initial value (input/output) 510 @param skey The scheduled key context 511 @return CRYPT_OK if successful 512 */ 513 int function(const ubyte *pt, ubyte *ct, c_ulong blocks, ubyte *IV, symmetric_key *skey) accel_cbc_encrypt; 514 515 /** Accelerated CBC decryption 516 @param pt Plaintext 517 @param ct Ciphertext 518 @param blocks The number of complete blocks to process 519 @param IV The initial value (input/output) 520 @param skey The scheduled key context 521 @return CRYPT_OK if successful 522 */ 523 int function(const ubyte *ct, ubyte *pt, c_ulong blocks, ubyte *IV, symmetric_key *skey) accel_cbc_decrypt; 524 525 /** Accelerated CTR encryption 526 @param pt Plaintext 527 @param ct Ciphertext 528 @param blocks The number of complete blocks to process 529 @param IV The initial value (input/output) 530 @param mode little or big endian counter (mode=0 or mode=1) 531 @param skey The scheduled key context 532 @return CRYPT_OK if successful 533 */ 534 int function(const ubyte *pt, ubyte *ct, c_ulong blocks, ubyte *IV, int mode, symmetric_key *skey) accel_ctr_encrypt; 535 536 /** Accelerated LRW 537 @param pt Plaintext 538 @param ct Ciphertext 539 @param blocks The number of complete blocks to process 540 @param IV The initial value (input/output) 541 @param tweak The LRW tweak 542 @param skey The scheduled key context 543 @return CRYPT_OK if successful 544 */ 545 int function(const ubyte *pt, ubyte *ct, c_ulong blocks, ubyte *IV, const ubyte *tweak, symmetric_key *skey) accel_lrw_encrypt; 546 547 /** Accelerated LRW 548 @param ct Ciphertext 549 @param pt Plaintext 550 @param blocks The number of complete blocks to process 551 @param IV The initial value (input/output) 552 @param tweak The LRW tweak 553 @param skey The scheduled key context 554 @return CRYPT_OK if successful 555 */ 556 int function(const ubyte *ct, ubyte *pt, c_ulong blocks, ubyte *IV, const ubyte *tweak, symmetric_key *skey) accel_lrw_decrypt; 557 558 /** Accelerated CCM packet (one-shot) 559 @param key The secret key to use 560 @param keylen The length of the secret key (octets) 561 @param uskey A previously scheduled key [optional can be NULL] 562 @param nonce The session nonce [use once] 563 @param noncelen The length of the nonce 564 @param header The header for the session 565 @param headerlen The length of the header (octets) 566 @param pt [out] The plaintext 567 @param ptlen The length of the plaintext (octets) 568 @param ct [out] The ciphertext 569 @param tag [out] The destination tag 570 @param taglen [in/out] The max size and resulting size of the authentication tag 571 @param direction Encrypt or Decrypt direction (0 or 1) 572 @return CRYPT_OK if successful 573 */ 574 int function( 575 const ubyte *key, c_ulong keylen, 576 symmetric_key *uskey, 577 const ubyte *nonce, c_ulong noncelen, 578 const ubyte *header, c_ulong headerlen, 579 ubyte *pt, c_ulong ptlen, 580 ubyte *ct, 581 ubyte *tag, c_ulong *taglen, 582 int direction) accel_ccm_memory; 583 584 /** Accelerated GCM packet (one shot) 585 @param key The secret key 586 @param keylen The length of the secret key 587 @param IV The initial vector 588 @param IVlen The length of the initial vector 589 @param adata The additional authentication data (header) 590 @param adatalen The length of the adata 591 @param pt The plaintext 592 @param ptlen The length of the plaintext (ciphertext length is the same) 593 @param ct The ciphertext 594 @param tag [out] The MAC tag 595 @param taglen [in/out] The MAC tag length 596 @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT) 597 @return CRYPT_OK on success 598 */ 599 int function( 600 const ubyte *key, c_ulong keylen, 601 const ubyte *IV, c_ulong IVlen, 602 const ubyte *adata, c_ulong adatalen, 603 ubyte *pt, c_ulong ptlen, 604 ubyte *ct, 605 ubyte *tag, c_ulong *taglen, 606 int direction) accel_gcm_memory; 607 608 /** Accelerated one shot LTC_OMAC 609 @param key The secret key 610 @param keylen The key length (octets) 611 @param in The message 612 @param inlen Length of message (octets) 613 @param out [out] Destination for tag 614 @param outlen [in/out] Initial and final size of out 615 @return CRYPT_OK on success 616 */ 617 int function( 618 const ubyte *key, c_ulong keylen, 619 const ubyte *_in, c_ulong inlen, 620 ubyte *_out, c_ulong *outlen) omac_memory; 621 622 /** Accelerated one shot XCBC 623 @param key The secret key 624 @param keylen The key length (octets) 625 @param in The message 626 @param inlen Length of message (octets) 627 @param out [out] Destination for tag 628 @param outlen [in/out] Initial and final size of out 629 @return CRYPT_OK on success 630 */ 631 int function( 632 const ubyte *key, c_ulong keylen, 633 const ubyte *_in, c_ulong inlen, 634 ubyte *_out, c_ulong *outlen) xcbc_memory; 635 636 /** Accelerated one shot F9 637 @param key The secret key 638 @param keylen The key length (octets) 639 @param in The message 640 @param inlen Length of message (octets) 641 @param out [out] Destination for tag 642 @param outlen [in/out] Initial and final size of out 643 @return CRYPT_OK on success 644 @remark Requires manual padding 645 */ 646 int function( 647 const ubyte *key, c_ulong keylen, 648 const ubyte *_in, c_ulong inlen, 649 ubyte *_out, c_ulong *outlen) f9_memory; 650 } 651 652 extern __gshared ltc_cipher_descriptor[] cipher_descriptor; 653 654 version(LTC_BLOWFISH) 655 { 656 int blowfish_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 657 int blowfish_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 658 int blowfish_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 659 int blowfish_test(); 660 void blowfish_done(symmetric_key *skey); 661 int blowfish_keysize(int *keysize); 662 extern const __gshared ltc_cipher_descriptor blowfish_desc; 663 } 664 665 version(LTC_RC5) 666 { 667 int rc5_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 668 int rc5_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 669 int rc5_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 670 int rc5_test(); 671 void rc5_done(symmetric_key *skey); 672 int rc5_keysize(int *keysize); 673 extern const __gshared ltc_cipher_descriptor rc5_desc; 674 } 675 676 version(LTC_RC6) 677 { 678 int rc6_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 679 int rc6_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 680 int rc6_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 681 int rc6_test(); 682 void rc6_done(symmetric_key *skey); 683 int rc6_keysize(int *keysize); 684 extern const __gshared ltc_cipher_descriptor rc6_desc; 685 } 686 687 version(LTC_RC2) 688 { 689 int rc2_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 690 int rc2_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 691 int rc2_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 692 int rc2_test(); 693 void rc2_done(symmetric_key *skey); 694 int rc2_keysize(int *keysize); 695 extern const __gshared ltc_cipher_descriptor rc2_desc; 696 } 697 698 version(LTC_SAFERP) 699 { 700 int saferp_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 701 int saferp_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 702 int saferp_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 703 int saferp_test(); 704 void saferp_done(symmetric_key *skey); 705 int saferp_keysize(int *keysize); 706 extern const __gshared ltc_cipher_descriptor saferp_desc; 707 } 708 709 version(LTC_SAFER) 710 { 711 int safer_k64_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 712 int safer_sk64_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 713 int safer_k128_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 714 int safer_sk128_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 715 int safer_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *key); 716 int safer_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *key); 717 int safer_k64_test(); 718 int safer_sk64_test(); 719 int safer_sk128_test(); 720 void safer_done(symmetric_key *skey); 721 int safer_64_keysize(int *keysize); 722 int safer_128_keysize(int *keysize); 723 extern const __gshared ltc_cipher_descriptor safer_k64_desc, safer_k128_desc, safer_sk64_desc, safer_sk128_desc; 724 } 725 726 version(LTC_RIJNDAEL) 727 { 728 729 /* make aes an alias */ 730 alias aes_setup = rijndael_setup; 731 alias aes_ecb_encrypt = rijndael_ecb_encrypt; 732 alias aes_ecb_decrypt = rijndael_ecb_decrypt; 733 alias aes_test = rijndael_test; 734 alias aes_done = rijndael_done; 735 alias aes_keysize = rijndael_keysize; 736 737 alias aes_enc_setup = rijndael_enc_setup; 738 alias aes_enc_ecb_encrypt = rijndael_enc_ecb_encrypt; 739 alias aes_enc_keysize = rijndael_enc_keysize; 740 741 int rijndael_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 742 int rijndael_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 743 int rijndael_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 744 int rijndael_test(); 745 void rijndael_done(symmetric_key *skey); 746 int rijndael_keysize(int *keysize); 747 int rijndael_enc_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 748 int rijndael_enc_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 749 void rijndael_enc_done(symmetric_key *skey); 750 int rijndael_enc_keysize(int *keysize); 751 extern const __gshared ltc_cipher_descriptor rijndael_desc, aes_desc; 752 extern const __gshared ltc_cipher_descriptor rijndael_enc_desc, aes_enc_desc; 753 } 754 755 version(LTC_XTEA) 756 { 757 int xtea_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 758 int xtea_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 759 int xtea_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 760 int xtea_test(); 761 void xtea_done(symmetric_key *skey); 762 int xtea_keysize(int *keysize); 763 extern const __gshared ltc_cipher_descriptor xtea_desc; 764 } 765 766 version(LTC_TWOFISH) 767 { 768 int twofish_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 769 int twofish_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 770 int twofish_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 771 int twofish_test(); 772 void twofish_done(symmetric_key *skey); 773 int twofish_keysize(int *keysize); 774 extern const __gshared ltc_cipher_descriptor twofish_desc; 775 } 776 777 version(LTC_DES) 778 { 779 int des_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 780 int des_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 781 int des_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 782 int des_test(); 783 void des_done(symmetric_key *skey); 784 int des_keysize(int *keysize); 785 int des3_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 786 int des3_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 787 int des3_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 788 int des3_test(); 789 void des3_done(symmetric_key *skey); 790 int des3_keysize(int *keysize); 791 extern const __gshared ltc_cipher_descriptor des_desc, des3_desc; 792 } 793 794 version(LTC_CAST5) 795 { 796 int cast5_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 797 int cast5_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 798 int cast5_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 799 int cast5_test(); 800 void cast5_done(symmetric_key *skey); 801 int cast5_keysize(int *keysize); 802 extern const __gshared ltc_cipher_descriptor cast5_desc; 803 } 804 805 version(LTC_NOEKEON) 806 { 807 int noekeon_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 808 int noekeon_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 809 int noekeon_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 810 int noekeon_test(); 811 void noekeon_done(symmetric_key *skey); 812 int noekeon_keysize(int *keysize); 813 extern const __gshared ltc_cipher_descriptor noekeon_desc; 814 } 815 816 version(LTC_SKIPJACK) 817 { 818 int skipjack_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 819 int skipjack_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 820 int skipjack_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 821 int skipjack_test(); 822 void skipjack_done(symmetric_key *skey); 823 int skipjack_keysize(int *keysize); 824 extern const __gshared ltc_cipher_descriptor skipjack_desc; 825 } 826 827 version(LTC_KHAZAD) 828 { 829 int khazad_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 830 int khazad_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 831 int khazad_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 832 int khazad_test(); 833 void khazad_done(symmetric_key *skey); 834 int khazad_keysize(int *keysize); 835 extern const __gshared ltc_cipher_descriptor khazad_desc; 836 } 837 838 version(LTC_ANUBIS) 839 { 840 int anubis_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 841 int anubis_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 842 int anubis_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 843 int anubis_test(); 844 void anubis_done(symmetric_key *skey); 845 int anubis_keysize(int *keysize); 846 extern const __gshared ltc_cipher_descriptor anubis_desc; 847 } 848 849 version(LTC_KSEED) 850 { 851 int kseed_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 852 int kseed_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 853 int kseed_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 854 int kseed_test(); 855 void kseed_done(symmetric_key *skey); 856 int kseed_keysize(int *keysize); 857 extern const __gshared ltc_cipher_descriptor kseed_desc; 858 } 859 860 version(LTC_KASUMI) 861 { 862 int kasumi_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 863 int kasumi_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 864 int kasumi_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 865 int kasumi_test(); 866 void kasumi_done(symmetric_key *skey); 867 int kasumi_keysize(int *keysize); 868 extern const __gshared ltc_cipher_descriptor kasumi_desc; 869 } 870 871 872 version(LTC_MULTI2) 873 { 874 int multi2_setup(const ubyte *key, int keylen, int num_rounds, symmetric_key *skey); 875 int multi2_ecb_encrypt(const ubyte *pt, ubyte *ct, symmetric_key *skey); 876 int multi2_ecb_decrypt(const ubyte *ct, ubyte *pt, symmetric_key *skey); 877 int multi2_test(); 878 void multi2_done(symmetric_key *skey); 879 int multi2_keysize(int *keysize); 880 extern const __gshared ltc_cipher_descriptor multi2_desc; 881 } 882 883 version(LTC_ECB_MODE) 884 { 885 int ecb_start(int cipher, const ubyte *key, 886 int keylen, int num_rounds, symmetric_ECB *ecb); 887 int ecb_encrypt(const ubyte *pt, ubyte *ct, c_ulong len, symmetric_ECB *ecb); 888 int ecb_decrypt(const ubyte *ct, ubyte *pt, c_ulong len, symmetric_ECB *ecb); 889 int ecb_done(symmetric_ECB *ecb); 890 } 891 892 version(LTC_CFB_MODE) 893 { 894 int cfb_start(int cipher, const ubyte *IV, const ubyte *key, 895 int keylen, int num_rounds, symmetric_CFB *cfb); 896 int cfb_encrypt(const ubyte *pt, ubyte *ct, c_ulong len, symmetric_CFB *cfb); 897 int cfb_decrypt(const ubyte *ct, ubyte *pt, c_ulong len, symmetric_CFB *cfb); 898 int cfb_getiv(ubyte *IV, c_ulong *len, symmetric_CFB *cfb); 899 int cfb_setiv(const ubyte *IV, c_ulong len, symmetric_CFB *cfb); 900 int cfb_done(symmetric_CFB *cfb); 901 } 902 903 version(LTC_OFB_MODE) 904 { 905 int ofb_start(int cipher, const ubyte *IV, const ubyte *key, 906 int keylen, int num_rounds, symmetric_OFB *ofb); 907 int ofb_encrypt(const ubyte *pt, ubyte *ct, c_ulong len, symmetric_OFB *ofb); 908 int ofb_decrypt(const ubyte *ct, ubyte *pt, c_ulong len, symmetric_OFB *ofb); 909 int ofb_getiv(ubyte *IV, c_ulong *len, symmetric_OFB *ofb); 910 int ofb_setiv(const ubyte *IV, c_ulong len, symmetric_OFB *ofb); 911 int ofb_done(symmetric_OFB *ofb); 912 } 913 914 version(LTC_CBC_MODE) 915 { 916 int cbc_start(int cipher, const ubyte *IV, const ubyte *key, 917 int keylen, int num_rounds, symmetric_CBC *cbc); 918 int cbc_encrypt(const ubyte *pt, ubyte *ct, c_ulong len, symmetric_CBC *cbc); 919 int cbc_decrypt(const ubyte *ct, ubyte *pt, c_ulong len, symmetric_CBC *cbc); 920 int cbc_getiv(ubyte *IV, c_ulong *len, symmetric_CBC *cbc); 921 int cbc_setiv(const ubyte *IV, c_ulong len, symmetric_CBC *cbc); 922 int cbc_done(symmetric_CBC *cbc); 923 } 924 925 version(LTC_CTR_MODE) 926 { 927 928 enum CTR_COUNTER_LITTLE_ENDIAN = 0x0000; 929 enum CTR_COUNTER_BIG_ENDIAN = 0x1000; 930 enum LTC_CTR_RFC3686 = 0x2000; 931 932 int ctr_start( int cipher, 933 const ubyte* IV, 934 const ubyte* key, 935 int keylen, 936 int num_rounds, 937 int ctr_mode, 938 symmetric_CTR *ctr); 939 int ctr_encrypt(const ubyte *pt, ubyte *ct, c_ulong len, symmetric_CTR *ctr); 940 int ctr_decrypt(const ubyte *ct, ubyte *pt, c_ulong len, symmetric_CTR *ctr); 941 int ctr_getiv(ubyte *IV, c_ulong *len, symmetric_CTR *ctr); 942 int ctr_setiv(const ubyte *IV, c_ulong len, symmetric_CTR *ctr); 943 int ctr_done(symmetric_CTR *ctr); 944 int ctr_test(); 945 } 946 947 version(LTC_LRW_MODE) 948 { 949 950 enum LRW_ENCRYPT = 0; 951 enum LRW_DECRYPT = 1; 952 953 int lrw_start( int cipher, 954 const ubyte *IV, 955 const ubyte *key, 956 int keylen, 957 const ubyte *tweak, 958 int num_rounds, 959 symmetric_LRW *lrw); 960 int lrw_encrypt(const ubyte *pt, ubyte *ct, c_ulong len, symmetric_LRW *lrw); 961 int lrw_decrypt(const ubyte *ct, ubyte *pt, c_ulong len, symmetric_LRW *lrw); 962 int lrw_getiv(ubyte *IV, c_ulong *len, symmetric_LRW *lrw); 963 int lrw_setiv(const ubyte *IV, c_ulong len, symmetric_LRW *lrw); 964 int lrw_done(symmetric_LRW *lrw); 965 int lrw_test(); 966 967 /* don't call */ 968 int lrw_process(const ubyte *pt, ubyte *ct, c_ulong len, int mode, symmetric_LRW *lrw); 969 } 970 971 version(LTC_F8_MODE) 972 { 973 int f8_start( int cipher, 974 const ubyte *IV, 975 const ubyte *key, int keylen, 976 const ubyte *salt_key, int skeylen, 977 int num_rounds, symmetric_F8 *f8); 978 int f8_encrypt(const ubyte *pt, ubyte *ct, c_ulong len, symmetric_F8 *f8); 979 int f8_decrypt(const ubyte *ct, ubyte *pt, c_ulong len, symmetric_F8 *f8); 980 int f8_getiv(ubyte *IV, c_ulong *len, symmetric_F8 *f8); 981 int f8_setiv(const ubyte *IV, c_ulong len, symmetric_F8 *f8); 982 int f8_done(symmetric_F8 *f8); 983 int f8_test_mode(); 984 } 985 986 version(LTC_XTS_MODE) 987 { 988 struct symmetric_xts 989 { 990 symmetric_key key1, key2; 991 int cipher; 992 } 993 994 int xts_start( int cipher, 995 const ubyte *key1, 996 const ubyte *key2, 997 c_ulong keylen, 998 int num_rounds, 999 symmetric_xts *xts); 1000 1001 int xts_encrypt( 1002 const ubyte *pt, c_ulong ptlen, 1003 ubyte *ct, 1004 const ubyte *tweak, 1005 symmetric_xts *xts); 1006 int xts_decrypt( 1007 const ubyte *ct, c_ulong ptlen, 1008 ubyte *pt, 1009 const ubyte *tweak, 1010 symmetric_xts *xts); 1011 1012 void xts_done(symmetric_xts *xts); 1013 int xts_test(); 1014 void xts_mult_x(ubyte *I); 1015 } 1016 1017 int find_cipher(const char *name); 1018 int find_cipher_any(const char *name, int blocklen, int keylen); 1019 int find_cipher_id(ubyte ID); 1020 int register_cipher(const ltc_cipher_descriptor *cipher); 1021 int unregister_cipher(const ltc_cipher_descriptor *cipher); 1022 int cipher_is_valid(int idx); 1023 1024 mixin(LTC_MUTEX_PROTO("ltc_cipher_mutex")); 1025 1026 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_cipher.h,v $ */ 1027 /* $Revision: 1.54 $ */ 1028 /* $Date: 2007/05/12 14:37:41 $ */