comparison libtomcrypt/src/ciphers/blowfish.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents 1b9e69c058d2
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
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 * 8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org 9 * Tom St Denis, [email protected], http://libtomcrypt.com
10 */ 10 */
11 /** 11 /**
12 @file blowfish.c 12 @file blowfish.c
13 Implementation of the Blowfish block cipher, Tom St Denis 13 Implementation of the Blowfish block cipher, Tom St Denis
14 */ 14 */
25 &blowfish_ecb_encrypt, 25 &blowfish_ecb_encrypt,
26 &blowfish_ecb_decrypt, 26 &blowfish_ecb_decrypt,
27 &blowfish_test, 27 &blowfish_test,
28 &blowfish_done, 28 &blowfish_done,
29 &blowfish_keysize, 29 &blowfish_keysize,
30 NULL, NULL, NULL, NULL, NULL, NULL, NULL 30 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
31 }; 31 };
32 32
33 static const ulong32 ORIG_P[16 + 2] = { 33 static const ulong32 ORIG_P[16 + 2] = {
34 0x243F6A88UL, 0x85A308D3UL, 0x13198A2EUL, 0x03707344UL, 34 0x243F6A88UL, 0x85A308D3UL, 0x13198A2EUL, 0x03707344UL,
35 0xA4093822UL, 0x299F31D0UL, 0x082EFA98UL, 0xEC4E6C89UL, 35 0xA4093822UL, 0x299F31D0UL, 0x082EFA98UL, 0xEC4E6C89UL,
383 /** 383 /**
384 Encrypts a block of text with Blowfish 384 Encrypts a block of text with Blowfish
385 @param pt The input plaintext (8 bytes) 385 @param pt The input plaintext (8 bytes)
386 @param ct The output ciphertext (8 bytes) 386 @param ct The output ciphertext (8 bytes)
387 @param skey The key as scheduled 387 @param skey The key as scheduled
388 @return CRYPT_OK if successful
388 */ 389 */
389 #ifdef LTC_CLEAN_STACK 390 #ifdef LTC_CLEAN_STACK
390 static void _blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 391 static int _blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
391 #else 392 #else
392 void blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 393 int blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
393 #endif 394 #endif
394 { 395 {
395 ulong32 L, R; 396 ulong32 L, R;
396 int r; 397 int r;
397 #ifndef __GNUC__ 398 #ifndef __GNUC__
426 L ^= skey->blowfish.K[16]; 427 L ^= skey->blowfish.K[16];
427 428
428 /* store */ 429 /* store */
429 STORE32H(R, &ct[0]); 430 STORE32H(R, &ct[0]);
430 STORE32H(L, &ct[4]); 431 STORE32H(L, &ct[4]);
432
433 return CRYPT_OK;
431 } 434 }
432 435
433 #ifdef LTC_CLEAN_STACK 436 #ifdef LTC_CLEAN_STACK
434 void blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 437 int blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
435 { 438 {
436 _blowfish_ecb_encrypt(pt, ct, skey); 439 int err = _blowfish_ecb_encrypt(pt, ct, skey);
437 burn_stack(sizeof(ulong32) * 2 + sizeof(int)); 440 burn_stack(sizeof(ulong32) * 2 + sizeof(int));
441 return err;
438 } 442 }
439 #endif 443 #endif
440 444
441 /** 445 /**
442 Decrypts a block of text with Blowfish 446 Decrypts a block of text with Blowfish
443 @param ct The input ciphertext (8 bytes) 447 @param ct The input ciphertext (8 bytes)
444 @param pt The output plaintext (8 bytes) 448 @param pt The output plaintext (8 bytes)
445 @param skey The key as scheduled 449 @param skey The key as scheduled
450 @return CRYPT_OK if successful
446 */ 451 */
447 #ifdef LTC_CLEAN_STACK 452 #ifdef LTC_CLEAN_STACK
448 static void _blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 453 static int _blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
449 #else 454 #else
450 void blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 455 int blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
451 #endif 456 #endif
452 { 457 {
453 ulong32 L, R; 458 ulong32 L, R;
454 int r; 459 int r;
455 #ifndef __GNUC__ 460 #ifndef __GNUC__
484 } 489 }
485 490
486 /* store */ 491 /* store */
487 STORE32H(L, &pt[0]); 492 STORE32H(L, &pt[0]);
488 STORE32H(R, &pt[4]); 493 STORE32H(R, &pt[4]);
494 return CRYPT_OK;
489 } 495 }
490 496
491 #ifdef LTC_CLEAN_STACK 497 #ifdef LTC_CLEAN_STACK
492 void blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 498 int blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
493 { 499 {
494 _blowfish_ecb_decrypt(ct, pt, skey); 500 int err = _blowfish_ecb_decrypt(ct, pt, skey);
495 burn_stack(sizeof(ulong32) * 2 + sizeof(int)); 501 burn_stack(sizeof(ulong32) * 2 + sizeof(int));
502 return err;
496 } 503 }
497 #endif 504 #endif
498 505
499 506
500 /** 507 /**
539 /* encrypt and decrypt */ 546 /* encrypt and decrypt */
540 blowfish_ecb_encrypt(tests[x].pt, tmp[0], &key); 547 blowfish_ecb_encrypt(tests[x].pt, tmp[0], &key);
541 blowfish_ecb_decrypt(tmp[0], tmp[1], &key); 548 blowfish_ecb_decrypt(tmp[0], tmp[1], &key);
542 549
543 /* compare */ 550 /* compare */
544 if ((memcmp(tmp[0], tests[x].ct, 8) != 0) || (memcmp(tmp[1], tests[x].pt, 8) != 0)) { 551 if ((XMEMCMP(tmp[0], tests[x].ct, 8) != 0) || (XMEMCMP(tmp[1], tests[x].pt, 8) != 0)) {
545 return CRYPT_FAIL_TESTVECTOR; 552 return CRYPT_FAIL_TESTVECTOR;
546 } 553 }
547 554
548 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 555 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
549 for (y = 0; y < 8; y++) tmp[0][y] = 0; 556 for (y = 0; y < 8; y++) tmp[0][y] = 0;
581 588
582 #endif 589 #endif
583 590
584 591
585 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/blowfish.c,v $ */ 592 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/blowfish.c,v $ */
586 /* $Revision: 1.7 $ */ 593 /* $Revision: 1.12 $ */
587 /* $Date: 2005/05/05 14:35:58 $ */ 594 /* $Date: 2006/11/08 23:01:06 $ */