comparison libtomcrypt/src/ciphers/cast5.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 /** 12 /**
13 @file cast5.c 13 @file cast5.c
14 Implementation of CAST5 (RFC 2144) by Tom St Denis 14 Implementation of CAST5 (RFC 2144) by Tom St Denis
25 &cast5_ecb_encrypt, 25 &cast5_ecb_encrypt,
26 &cast5_ecb_decrypt, 26 &cast5_ecb_decrypt,
27 &cast5_test, 27 &cast5_test,
28 &cast5_done, 28 &cast5_done,
29 &cast5_keysize, 29 &cast5_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 S1[256] = { 33 static const ulong32 S1[256] = {
34 0x30fb40d4UL, 0x9fa0ff0bUL, 0x6beccd2fUL, 0x3f258c7aUL, 0x1e213f2fUL, 0x9c004dd3UL, 34 0x30fb40d4UL, 0x9fa0ff0bUL, 0x6beccd2fUL, 0x3f258c7aUL, 0x1e213f2fUL, 0x9c004dd3UL,
35 0x6003e540UL, 0xcf9fc949UL, 0xbfd4af27UL, 0x88bbbdb5UL, 0xe2034090UL, 0x98d09675UL, 35 0x6003e540UL, 0xcf9fc949UL, 0xbfd4af27UL, 0x88bbbdb5UL, 0xe2034090UL, 0x98d09675UL,
534 @param pt The input plaintext (8 bytes) 534 @param pt The input plaintext (8 bytes)
535 @param ct The output ciphertext (8 bytes) 535 @param ct The output ciphertext (8 bytes)
536 @param skey The key as scheduled 536 @param skey The key as scheduled
537 */ 537 */
538 #ifdef LTC_CLEAN_STACK 538 #ifdef LTC_CLEAN_STACK
539 static void _cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 539 static int _cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
540 #else 540 #else
541 void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 541 int cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
542 #endif 542 #endif
543 { 543 {
544 ulong32 R, L; 544 ulong32 R, L;
545 545
546 LTC_ARGCHK(pt != NULL); 546 LTC_ARGCHK(pt != NULL);
567 L ^= FIII(R, skey->cast5.K[14], skey->cast5.K[30]); 567 L ^= FIII(R, skey->cast5.K[14], skey->cast5.K[30]);
568 R ^= FI(L, skey->cast5.K[15], skey->cast5.K[31]); 568 R ^= FI(L, skey->cast5.K[15], skey->cast5.K[31]);
569 } 569 }
570 STORE32H(R,&ct[0]); 570 STORE32H(R,&ct[0]);
571 STORE32H(L,&ct[4]); 571 STORE32H(L,&ct[4]);
572 return CRYPT_OK;
572 } 573 }
573 574
574 575
575 #ifdef LTC_CLEAN_STACK 576 #ifdef LTC_CLEAN_STACK
576 void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 577 int cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
577 { 578 {
578 _cast5_ecb_encrypt(pt,ct,skey); 579 int err =_cast5_ecb_encrypt(pt,ct,skey);
579 burn_stack(sizeof(ulong32)*3); 580 burn_stack(sizeof(ulong32)*3);
581 return err;
580 } 582 }
581 #endif 583 #endif
582 584
583 /** 585 /**
584 Decrypts a block of text with CAST5 586 Decrypts a block of text with CAST5
585 @param ct The input ciphertext (8 bytes) 587 @param ct The input ciphertext (8 bytes)
586 @param pt The output plaintext (8 bytes) 588 @param pt The output plaintext (8 bytes)
587 @param skey The key as scheduled 589 @param skey The key as scheduled
588 */ 590 */
589 #ifdef LTC_CLEAN_STACK 591 #ifdef LTC_CLEAN_STACK
590 static void _cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 592 static int _cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
591 #else 593 #else
592 void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 594 int cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
593 #endif 595 #endif
594 { 596 {
595 ulong32 R, L; 597 ulong32 R, L;
596 598
597 LTC_ARGCHK(pt != NULL); 599 LTC_ARGCHK(pt != NULL);
618 L ^= FIII(R, skey->cast5.K[2], skey->cast5.K[18]); 620 L ^= FIII(R, skey->cast5.K[2], skey->cast5.K[18]);
619 R ^= FII(L, skey->cast5.K[1], skey->cast5.K[17]); 621 R ^= FII(L, skey->cast5.K[1], skey->cast5.K[17]);
620 L ^= FI(R, skey->cast5.K[0], skey->cast5.K[16]); 622 L ^= FI(R, skey->cast5.K[0], skey->cast5.K[16]);
621 STORE32H(L,&pt[0]); 623 STORE32H(L,&pt[0]);
622 STORE32H(R,&pt[4]); 624 STORE32H(R,&pt[4]);
625
626 return CRYPT_OK;
623 } 627 }
624 628
625 #ifdef LTC_CLEAN_STACK 629 #ifdef LTC_CLEAN_STACK
626 void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 630 int cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
627 { 631 {
628 _cast5_ecb_decrypt(ct,pt,skey); 632 int err = _cast5_ecb_decrypt(ct,pt,skey);
629 burn_stack(sizeof(ulong32)*3); 633 burn_stack(sizeof(ulong32)*3);
634 return err;
630 } 635 }
631 #endif 636 #endif
632 637
633 /** 638 /**
634 Performs a self-test of the CAST5 block cipher 639 Performs a self-test of the CAST5 block cipher
669 if ((err = cast5_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) { 674 if ((err = cast5_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
670 return err; 675 return err;
671 } 676 }
672 cast5_ecb_encrypt(tests[i].pt, tmp[0], &key); 677 cast5_ecb_encrypt(tests[i].pt, tmp[0], &key);
673 cast5_ecb_decrypt(tmp[0], tmp[1], &key); 678 cast5_ecb_decrypt(tmp[0], tmp[1], &key);
674 if ((memcmp(tmp[0], tests[i].ct, 8) != 0) || (memcmp(tmp[1], tests[i].pt, 8) != 0)) { 679 if ((XMEMCMP(tmp[0], tests[i].ct, 8) != 0) || (XMEMCMP(tmp[1], tests[i].pt, 8) != 0)) {
675 return CRYPT_FAIL_TESTVECTOR; 680 return CRYPT_FAIL_TESTVECTOR;
676 } 681 }
677 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 682 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
678 for (y = 0; y < 8; y++) tmp[0][y] = 0; 683 for (y = 0; y < 8; y++) tmp[0][y] = 0;
679 for (y = 0; y < 1000; y++) cast5_ecb_encrypt(tmp[0], tmp[0], &key); 684 for (y = 0; y < 1000; y++) cast5_ecb_encrypt(tmp[0], tmp[0], &key);
709 } 714 }
710 715
711 #endif 716 #endif
712 717
713 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/cast5.c,v $ */ 718 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/cast5.c,v $ */
714 /* $Revision: 1.7 $ */ 719 /* $Revision: 1.12 $ */
715 /* $Date: 2005/05/05 14:35:58 $ */ 720 /* $Date: 2006/11/08 23:01:06 $ */