comparison src/ciphers/des.c @ 381:999a5eb4ed10 libtomcrypt-dropbear

propagate from branch 'au.asn.ucc.matt.ltc.orig' (head 52840647ac7f5c707c3bd158d119a15734a7ef28) to branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:39:21 +0000
parents 997e6f7dc01e d5faf4814ddb
children
comparison
equal deleted inserted replaced
281:997e6f7dc01e 381:999a5eb4ed10
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 #include "tomcrypt.h" 11 #include "tomcrypt.h"
12 12
13 /** 13 /**
14 @file des.c 14 @file des.c
30 &des_ecb_encrypt, 30 &des_ecb_encrypt,
31 &des_ecb_decrypt, 31 &des_ecb_decrypt,
32 &des_test, 32 &des_test,
33 &des_done, 33 &des_done,
34 &des_keysize, 34 &des_keysize,
35 NULL, NULL, NULL, NULL, NULL, NULL, NULL 35 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
36 }; 36 };
37 #endif 37 #endif
38 38
39 const struct ltc_cipher_descriptor des3_desc = 39 const struct ltc_cipher_descriptor des3_desc =
40 { 40 {
45 &des3_ecb_encrypt, 45 &des3_ecb_encrypt,
46 &des3_ecb_decrypt, 46 &des3_ecb_decrypt,
47 &des3_test, 47 &des3_test,
48 &des3_done, 48 &des3_done,
49 &des3_keysize, 49 &des3_keysize,
50 NULL, NULL, NULL, NULL, NULL, NULL, NULL 50 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
51 }; 51 };
52 52
53 static const ulong32 bytebit[8] = 53 static const ulong32 bytebit[8] =
54 { 54 {
55 0200, 0100, 040, 020, 010, 04, 02, 01 55 0200, 0100, 040, 020, 010, 04, 02, 01
1585 /** 1585 /**
1586 Encrypts a block of text with DES 1586 Encrypts a block of text with DES
1587 @param pt The input plaintext (8 bytes) 1587 @param pt The input plaintext (8 bytes)
1588 @param ct The output ciphertext (8 bytes) 1588 @param ct The output ciphertext (8 bytes)
1589 @param skey The key as scheduled 1589 @param skey The key as scheduled
1590 @return CRYPT_OK if successful
1590 */ 1591 */
1591 void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 1592 int des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
1592 { 1593 {
1593 ulong32 work[2]; 1594 ulong32 work[2];
1594 LTC_ARGCHK(pt != NULL); 1595 LTC_ARGCHK(pt != NULL);
1595 LTC_ARGCHK(ct != NULL); 1596 LTC_ARGCHK(ct != NULL);
1596 LTC_ARGCHK(skey != NULL); 1597 LTC_ARGCHK(skey != NULL);
1597 LOAD32H(work[0], pt+0); 1598 LOAD32H(work[0], pt+0);
1598 LOAD32H(work[1], pt+4); 1599 LOAD32H(work[1], pt+4);
1599 desfunc(work, skey->des.ek); 1600 desfunc(work, skey->des.ek);
1600 STORE32H(work[0],ct+0); 1601 STORE32H(work[0],ct+0);
1601 STORE32H(work[1],ct+4); 1602 STORE32H(work[1],ct+4);
1603 return CRYPT_OK;
1602 } 1604 }
1603 1605
1604 /** 1606 /**
1605 Decrypts a block of text with DES 1607 Decrypts a block of text with DES
1606 @param ct The input ciphertext (8 bytes) 1608 @param ct The input ciphertext (8 bytes)
1607 @param pt The output plaintext (8 bytes) 1609 @param pt The output plaintext (8 bytes)
1608 @param skey The key as scheduled 1610 @param skey The key as scheduled
1611 @return CRYPT_OK if successful
1609 */ 1612 */
1610 void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 1613 int des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
1611 { 1614 {
1612 ulong32 work[2]; 1615 ulong32 work[2];
1613 LTC_ARGCHK(pt != NULL); 1616 LTC_ARGCHK(pt != NULL);
1614 LTC_ARGCHK(ct != NULL); 1617 LTC_ARGCHK(ct != NULL);
1615 LTC_ARGCHK(skey != NULL); 1618 LTC_ARGCHK(skey != NULL);
1616 LOAD32H(work[0], ct+0); 1619 LOAD32H(work[0], ct+0);
1617 LOAD32H(work[1], ct+4); 1620 LOAD32H(work[1], ct+4);
1618 desfunc(work, skey->des.dk); 1621 desfunc(work, skey->des.dk);
1619 STORE32H(work[0],pt+0); 1622 STORE32H(work[0],pt+0);
1620 STORE32H(work[1],pt+4); 1623 STORE32H(work[1],pt+4);
1624 return CRYPT_OK;
1621 } 1625 }
1622 #endif 1626 #endif
1623 1627
1624 /** 1628 /**
1625 Encrypts a block of text with 3DES-EDE 1629 Encrypts a block of text with 3DES-EDE
1626 @param pt The input plaintext (8 bytes) 1630 @param pt The input plaintext (8 bytes)
1627 @param ct The output ciphertext (8 bytes) 1631 @param ct The output ciphertext (8 bytes)
1628 @param skey The key as scheduled 1632 @param skey The key as scheduled
1633 @return CRYPT_OK if successful
1629 */ 1634 */
1630 void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 1635 int des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
1631 { 1636 {
1632 ulong32 work[2]; 1637 ulong32 work[2];
1633 1638
1634 LTC_ARGCHK(pt != NULL); 1639 LTC_ARGCHK(pt != NULL);
1635 LTC_ARGCHK(ct != NULL); 1640 LTC_ARGCHK(ct != NULL);
1639 desfunc(work, skey->des3.ek[0]); 1644 desfunc(work, skey->des3.ek[0]);
1640 desfunc(work, skey->des3.ek[1]); 1645 desfunc(work, skey->des3.ek[1]);
1641 desfunc(work, skey->des3.ek[2]); 1646 desfunc(work, skey->des3.ek[2]);
1642 STORE32H(work[0],ct+0); 1647 STORE32H(work[0],ct+0);
1643 STORE32H(work[1],ct+4); 1648 STORE32H(work[1],ct+4);
1649 return CRYPT_OK;
1644 } 1650 }
1645 1651
1646 /** 1652 /**
1647 Decrypts a block of text with 3DES-EDE 1653 Decrypts a block of text with 3DES-EDE
1648 @param ct The input ciphertext (8 bytes) 1654 @param ct The input ciphertext (8 bytes)
1649 @param pt The output plaintext (8 bytes) 1655 @param pt The output plaintext (8 bytes)
1650 @param skey The key as scheduled 1656 @param skey The key as scheduled
1657 @return CRYPT_OK if successful
1651 */ 1658 */
1652 void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 1659 int des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
1653 { 1660 {
1654 ulong32 work[2]; 1661 ulong32 work[2];
1655 LTC_ARGCHK(pt != NULL); 1662 LTC_ARGCHK(pt != NULL);
1656 LTC_ARGCHK(ct != NULL); 1663 LTC_ARGCHK(ct != NULL);
1657 LTC_ARGCHK(skey != NULL); 1664 LTC_ARGCHK(skey != NULL);
1660 desfunc(work, skey->des3.dk[0]); 1667 desfunc(work, skey->des3.dk[0]);
1661 desfunc(work, skey->des3.dk[1]); 1668 desfunc(work, skey->des3.dk[1]);
1662 desfunc(work, skey->des3.dk[2]); 1669 desfunc(work, skey->des3.dk[2]);
1663 STORE32H(work[0],pt+0); 1670 STORE32H(work[0],pt+0);
1664 STORE32H(work[1],pt+4); 1671 STORE32H(work[1],pt+4);
1672 return CRYPT_OK;
1665 } 1673 }
1666 1674
1667 #if 0 1675 #if 0
1668 /** 1676 /**
1669 Performs a self-test of the DES block cipher 1677 Performs a self-test of the DES block cipher
1795 des_ecb_encrypt(cases[i].txt, tmp, &des); 1803 des_ecb_encrypt(cases[i].txt, tmp, &des);
1796 } else { 1804 } else {
1797 des_ecb_decrypt(cases[i].txt, tmp, &des); 1805 des_ecb_decrypt(cases[i].txt, tmp, &des);
1798 } 1806 }
1799 1807
1800 if (memcmp(cases[i].out, tmp, sizeof(tmp)) != 0) { 1808 if (XMEMCMP(cases[i].out, tmp, sizeof(tmp)) != 0) {
1801 return CRYPT_FAIL_TESTVECTOR; 1809 return CRYPT_FAIL_TESTVECTOR;
1802 } 1810 }
1803 1811
1804 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 1812 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
1805 for (y = 0; y < 8; y++) tmp[y] = 0; 1813 for (y = 0; y < 8; y++) tmp[y] = 0;
1839 } 1847 }
1840 1848
1841 des3_ecb_encrypt(pt, ct, &skey); 1849 des3_ecb_encrypt(pt, ct, &skey);
1842 des3_ecb_decrypt(ct, tmp, &skey); 1850 des3_ecb_decrypt(ct, tmp, &skey);
1843 1851
1844 if (memcmp(pt, tmp, 8) != 0) { 1852 if (XMEMCMP(pt, tmp, 8) != 0) {
1845 return CRYPT_FAIL_TESTVECTOR; 1853 return CRYPT_FAIL_TESTVECTOR;
1846 } 1854 }
1847 1855
1848 return CRYPT_OK; 1856 return CRYPT_OK;
1849 #endif 1857 #endif
1900 1908
1901 #endif 1909 #endif
1902 1910
1903 1911
1904 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/des.c,v $ */ 1912 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/des.c,v $ */
1905 /* $Revision: 1.8 $ */ 1913 /* $Revision: 1.13 $ */
1906 /* $Date: 2005/05/05 14:35:58 $ */ 1914 /* $Date: 2006/11/08 23:01:06 $ */