comparison libtomcrypt/src/ciphers/blowfish.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 /** 9 /**
12 @file blowfish.c 10 @file blowfish.c
13 Implementation of the Blowfish block cipher, Tom St Denis 11 Implementation of the Blowfish block cipher, Tom St Denis
14 */ 12 */
25 &blowfish_ecb_encrypt, 23 &blowfish_ecb_encrypt,
26 &blowfish_ecb_decrypt, 24 &blowfish_ecb_decrypt,
27 &blowfish_test, 25 &blowfish_test,
28 &blowfish_done, 26 &blowfish_done,
29 &blowfish_keysize, 27 &blowfish_keysize,
30 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 28 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
31 }; 29 };
32 30
33 static const ulong32 ORIG_P[16 + 2] = { 31 static const ulong32 ORIG_P[16 + 2] = {
34 0x243F6A88UL, 0x85A308D3UL, 0x13198A2EUL, 0x03707344UL, 32 0x243F6A88UL, 0x85A308D3UL, 0x13198A2EUL, 0x03707344UL,
35 0xA4093822UL, 0x299F31D0UL, 0x082EFA98UL, 0xEC4E6C89UL, 33 0xA4093822UL, 0x299F31D0UL, 0x082EFA98UL, 0xEC4E6C89UL,
320 } 318 }
321 319
322 /* check rounds */ 320 /* check rounds */
323 if (num_rounds != 0 && num_rounds != 16) { 321 if (num_rounds != 0 && num_rounds != 16) {
324 return CRYPT_INVALID_ROUNDS; 322 return CRYPT_INVALID_ROUNDS;
325 } 323 }
326 324
327 /* load in key bytes (Supplied by David Hopwood) */ 325 /* load in key bytes (Supplied by David Hopwood) */
328 for (x = y = 0; x < 18; x++) { 326 for (x = y = 0; x < 18; x++) {
329 A = 0; 327 A = 0;
330 for (z = 0; z < 4; z++) { 328 for (z = 0; z < 4; z++) {
331 A = (A << 8) | ((ulong32)key[y++] & 255); 329 A = (A << 8) | ((ulong32)key[y++] & 255);
332 if (y == (ulong32)keylen) { 330 if (y == (ulong32)keylen) {
333 y = 0; 331 y = 0;
334 } 332 }
335 } 333 }
336 skey->blowfish.K[x] = ORIG_P[x] ^ A; 334 skey->blowfish.K[x] = ORIG_P[x] ^ A;
337 } 335 }
338 336
345 343
346 /* encrypt K array */ 344 /* encrypt K array */
347 for (x = 0; x < 8; x++) { 345 for (x = 0; x < 8; x++) {
348 B[x] = 0; 346 B[x] = 0;
349 } 347 }
350 348
351 for (x = 0; x < 18; x += 2) { 349 for (x = 0; x < 18; x += 2) {
352 /* encrypt it */ 350 /* encrypt it */
353 blowfish_ecb_encrypt(B, B, skey); 351 blowfish_ecb_encrypt(B, B, skey);
354 /* copy it */ 352 /* copy it */
355 LOAD32H(skey->blowfish.K[x], &B[0]); 353 LOAD32H(skey->blowfish.K[x], &B[0]);
444 442
445 /** 443 /**
446 Decrypts a block of text with Blowfish 444 Decrypts a block of text with Blowfish
447 @param ct The input ciphertext (8 bytes) 445 @param ct The input ciphertext (8 bytes)
448 @param pt The output plaintext (8 bytes) 446 @param pt The output plaintext (8 bytes)
449 @param skey The key as scheduled 447 @param skey The key as scheduled
450 @return CRYPT_OK if successful 448 @return CRYPT_OK if successful
451 */ 449 */
452 #ifdef LTC_CLEAN_STACK 450 #ifdef LTC_CLEAN_STACK
453 static int _blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 451 static int _blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
454 #else 452 #else
462 #endif 460 #endif
463 461
464 LTC_ARGCHK(pt != NULL); 462 LTC_ARGCHK(pt != NULL);
465 LTC_ARGCHK(ct != NULL); 463 LTC_ARGCHK(ct != NULL);
466 LTC_ARGCHK(skey != NULL); 464 LTC_ARGCHK(skey != NULL);
467 465
468 #ifndef __GNUC__ 466 #ifndef __GNUC__
469 S1 = skey->blowfish.S[0]; 467 S1 = skey->blowfish.S[0];
470 S2 = skey->blowfish.S[1]; 468 S2 = skey->blowfish.S[1];
471 S3 = skey->blowfish.S[2]; 469 S3 = skey->blowfish.S[2];
472 S4 = skey->blowfish.S[3]; 470 S4 = skey->blowfish.S[3];
510 */ 508 */
511 int blowfish_test(void) 509 int blowfish_test(void)
512 { 510 {
513 #ifndef LTC_TEST 511 #ifndef LTC_TEST
514 return CRYPT_NOP; 512 return CRYPT_NOP;
515 #else 513 #else
516 int err; 514 int err;
517 symmetric_key key; 515 symmetric_key key;
518 static const struct { 516 static const struct {
519 unsigned char key[8], pt[8], ct[8]; 517 unsigned char key[8], pt[8], ct[8];
520 } tests[] = { 518 } tests[] = {
546 /* encrypt and decrypt */ 544 /* encrypt and decrypt */
547 blowfish_ecb_encrypt(tests[x].pt, tmp[0], &key); 545 blowfish_ecb_encrypt(tests[x].pt, tmp[0], &key);
548 blowfish_ecb_decrypt(tmp[0], tmp[1], &key); 546 blowfish_ecb_decrypt(tmp[0], tmp[1], &key);
549 547
550 /* compare */ 548 /* compare */
551 if ((XMEMCMP(tmp[0], tests[x].ct, 8) != 0) || (XMEMCMP(tmp[1], tests[x].pt, 8) != 0)) { 549 if ((compare_testvector(tmp[0], 8, tests[x].ct, 8, "Blowfish Encrypt", x) != 0) ||
550 (compare_testvector(tmp[1], 8, tests[x].pt, 8, "Blowfish Decrypt", x) != 0)) {
552 return CRYPT_FAIL_TESTVECTOR; 551 return CRYPT_FAIL_TESTVECTOR;
553 } 552 }
554 553
555 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 554 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
556 for (y = 0; y < 8; y++) tmp[0][y] = 0; 555 for (y = 0; y < 8; y++) tmp[0][y] = 0;
560 } 559 }
561 return CRYPT_OK; 560 return CRYPT_OK;
562 #endif 561 #endif
563 } 562 }
564 563
565 /** Terminate the context 564 /** Terminate the context
566 @param skey The scheduled key 565 @param skey The scheduled key
567 */ 566 */
568 void blowfish_done(symmetric_key *skey) 567 void blowfish_done(symmetric_key *skey)
569 { 568 {
569 LTC_UNUSED_PARAM(skey);
570 } 570 }
571 571
572 /** 572 /**
573 Gets suitable key size 573 Gets suitable key size
574 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. 574 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable.
587 } 587 }
588 588
589 #endif 589 #endif
590 590
591 591
592 /* $Source$ */ 592 /* ref: $Format:%D$ */
593 /* $Revision$ */ 593 /* git commit: $Format:%H$ */
594 /* $Date$ */ 594 /* commit time: $Format:%ai$ */