Mercurial > dropbear
comparison src/ciphers/des.c @ 380:d5faf4814ddb libtomcrypt-orig libtomcrypt-1.16
Update to LibTomCrypt 1.16
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 11 Jan 2007 02:22:00 +0000 |
parents | 59400faa4b44 |
children | 999a5eb4ed10 |
comparison
equal
deleted
inserted
replaced
280:59400faa4b44 | 380:d5faf4814ddb |
---|---|
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 |
29 &des_ecb_encrypt, | 29 &des_ecb_encrypt, |
30 &des_ecb_decrypt, | 30 &des_ecb_decrypt, |
31 &des_test, | 31 &des_test, |
32 &des_done, | 32 &des_done, |
33 &des_keysize, | 33 &des_keysize, |
34 NULL, NULL, NULL, NULL, NULL, NULL, NULL | 34 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
35 }; | 35 }; |
36 | 36 |
37 const struct ltc_cipher_descriptor des3_desc = | 37 const struct ltc_cipher_descriptor des3_desc = |
38 { | 38 { |
39 "3des", | 39 "3des", |
43 &des3_ecb_encrypt, | 43 &des3_ecb_encrypt, |
44 &des3_ecb_decrypt, | 44 &des3_ecb_decrypt, |
45 &des3_test, | 45 &des3_test, |
46 &des3_done, | 46 &des3_done, |
47 &des3_keysize, | 47 &des3_keysize, |
48 NULL, NULL, NULL, NULL, NULL, NULL, NULL | 48 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
49 }; | 49 }; |
50 | 50 |
51 static const ulong32 bytebit[8] = | 51 static const ulong32 bytebit[8] = |
52 { | 52 { |
53 0200, 0100, 040, 020, 010, 04, 02, 01 | 53 0200, 0100, 040, 020, 010, 04, 02, 01 |
1580 /** | 1580 /** |
1581 Encrypts a block of text with DES | 1581 Encrypts a block of text with DES |
1582 @param pt The input plaintext (8 bytes) | 1582 @param pt The input plaintext (8 bytes) |
1583 @param ct The output ciphertext (8 bytes) | 1583 @param ct The output ciphertext (8 bytes) |
1584 @param skey The key as scheduled | 1584 @param skey The key as scheduled |
1585 @return CRYPT_OK if successful | |
1585 */ | 1586 */ |
1586 void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | 1587 int des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) |
1587 { | 1588 { |
1588 ulong32 work[2]; | 1589 ulong32 work[2]; |
1589 LTC_ARGCHK(pt != NULL); | 1590 LTC_ARGCHK(pt != NULL); |
1590 LTC_ARGCHK(ct != NULL); | 1591 LTC_ARGCHK(ct != NULL); |
1591 LTC_ARGCHK(skey != NULL); | 1592 LTC_ARGCHK(skey != NULL); |
1592 LOAD32H(work[0], pt+0); | 1593 LOAD32H(work[0], pt+0); |
1593 LOAD32H(work[1], pt+4); | 1594 LOAD32H(work[1], pt+4); |
1594 desfunc(work, skey->des.ek); | 1595 desfunc(work, skey->des.ek); |
1595 STORE32H(work[0],ct+0); | 1596 STORE32H(work[0],ct+0); |
1596 STORE32H(work[1],ct+4); | 1597 STORE32H(work[1],ct+4); |
1598 return CRYPT_OK; | |
1597 } | 1599 } |
1598 | 1600 |
1599 /** | 1601 /** |
1600 Decrypts a block of text with DES | 1602 Decrypts a block of text with DES |
1601 @param ct The input ciphertext (8 bytes) | 1603 @param ct The input ciphertext (8 bytes) |
1602 @param pt The output plaintext (8 bytes) | 1604 @param pt The output plaintext (8 bytes) |
1603 @param skey The key as scheduled | 1605 @param skey The key as scheduled |
1606 @return CRYPT_OK if successful | |
1604 */ | 1607 */ |
1605 void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | 1608 int des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) |
1606 { | 1609 { |
1607 ulong32 work[2]; | 1610 ulong32 work[2]; |
1608 LTC_ARGCHK(pt != NULL); | 1611 LTC_ARGCHK(pt != NULL); |
1609 LTC_ARGCHK(ct != NULL); | 1612 LTC_ARGCHK(ct != NULL); |
1610 LTC_ARGCHK(skey != NULL); | 1613 LTC_ARGCHK(skey != NULL); |
1611 LOAD32H(work[0], ct+0); | 1614 LOAD32H(work[0], ct+0); |
1612 LOAD32H(work[1], ct+4); | 1615 LOAD32H(work[1], ct+4); |
1613 desfunc(work, skey->des.dk); | 1616 desfunc(work, skey->des.dk); |
1614 STORE32H(work[0],pt+0); | 1617 STORE32H(work[0],pt+0); |
1615 STORE32H(work[1],pt+4); | 1618 STORE32H(work[1],pt+4); |
1619 return CRYPT_OK; | |
1616 } | 1620 } |
1617 | 1621 |
1618 /** | 1622 /** |
1619 Encrypts a block of text with 3DES-EDE | 1623 Encrypts a block of text with 3DES-EDE |
1620 @param pt The input plaintext (8 bytes) | 1624 @param pt The input plaintext (8 bytes) |
1621 @param ct The output ciphertext (8 bytes) | 1625 @param ct The output ciphertext (8 bytes) |
1622 @param skey The key as scheduled | 1626 @param skey The key as scheduled |
1627 @return CRYPT_OK if successful | |
1623 */ | 1628 */ |
1624 void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | 1629 int des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) |
1625 { | 1630 { |
1626 ulong32 work[2]; | 1631 ulong32 work[2]; |
1627 | 1632 |
1628 LTC_ARGCHK(pt != NULL); | 1633 LTC_ARGCHK(pt != NULL); |
1629 LTC_ARGCHK(ct != NULL); | 1634 LTC_ARGCHK(ct != NULL); |
1633 desfunc(work, skey->des3.ek[0]); | 1638 desfunc(work, skey->des3.ek[0]); |
1634 desfunc(work, skey->des3.ek[1]); | 1639 desfunc(work, skey->des3.ek[1]); |
1635 desfunc(work, skey->des3.ek[2]); | 1640 desfunc(work, skey->des3.ek[2]); |
1636 STORE32H(work[0],ct+0); | 1641 STORE32H(work[0],ct+0); |
1637 STORE32H(work[1],ct+4); | 1642 STORE32H(work[1],ct+4); |
1643 return CRYPT_OK; | |
1638 } | 1644 } |
1639 | 1645 |
1640 /** | 1646 /** |
1641 Decrypts a block of text with 3DES-EDE | 1647 Decrypts a block of text with 3DES-EDE |
1642 @param ct The input ciphertext (8 bytes) | 1648 @param ct The input ciphertext (8 bytes) |
1643 @param pt The output plaintext (8 bytes) | 1649 @param pt The output plaintext (8 bytes) |
1644 @param skey The key as scheduled | 1650 @param skey The key as scheduled |
1651 @return CRYPT_OK if successful | |
1645 */ | 1652 */ |
1646 void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | 1653 int des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) |
1647 { | 1654 { |
1648 ulong32 work[2]; | 1655 ulong32 work[2]; |
1649 LTC_ARGCHK(pt != NULL); | 1656 LTC_ARGCHK(pt != NULL); |
1650 LTC_ARGCHK(ct != NULL); | 1657 LTC_ARGCHK(ct != NULL); |
1651 LTC_ARGCHK(skey != NULL); | 1658 LTC_ARGCHK(skey != NULL); |
1654 desfunc(work, skey->des3.dk[0]); | 1661 desfunc(work, skey->des3.dk[0]); |
1655 desfunc(work, skey->des3.dk[1]); | 1662 desfunc(work, skey->des3.dk[1]); |
1656 desfunc(work, skey->des3.dk[2]); | 1663 desfunc(work, skey->des3.dk[2]); |
1657 STORE32H(work[0],pt+0); | 1664 STORE32H(work[0],pt+0); |
1658 STORE32H(work[1],pt+4); | 1665 STORE32H(work[1],pt+4); |
1666 return CRYPT_OK; | |
1659 } | 1667 } |
1660 | 1668 |
1661 /** | 1669 /** |
1662 Performs a self-test of the DES block cipher | 1670 Performs a self-test of the DES block cipher |
1663 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled | 1671 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled |
1788 des_ecb_encrypt(cases[i].txt, tmp, &des); | 1796 des_ecb_encrypt(cases[i].txt, tmp, &des); |
1789 } else { | 1797 } else { |
1790 des_ecb_decrypt(cases[i].txt, tmp, &des); | 1798 des_ecb_decrypt(cases[i].txt, tmp, &des); |
1791 } | 1799 } |
1792 | 1800 |
1793 if (memcmp(cases[i].out, tmp, sizeof(tmp)) != 0) { | 1801 if (XMEMCMP(cases[i].out, tmp, sizeof(tmp)) != 0) { |
1794 return CRYPT_FAIL_TESTVECTOR; | 1802 return CRYPT_FAIL_TESTVECTOR; |
1795 } | 1803 } |
1796 | 1804 |
1797 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | 1805 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ |
1798 for (y = 0; y < 8; y++) tmp[y] = 0; | 1806 for (y = 0; y < 8; y++) tmp[y] = 0; |
1831 } | 1839 } |
1832 | 1840 |
1833 des3_ecb_encrypt(pt, ct, &skey); | 1841 des3_ecb_encrypt(pt, ct, &skey); |
1834 des3_ecb_decrypt(ct, tmp, &skey); | 1842 des3_ecb_decrypt(ct, tmp, &skey); |
1835 | 1843 |
1836 if (memcmp(pt, tmp, 8) != 0) { | 1844 if (XMEMCMP(pt, tmp, 8) != 0) { |
1837 return CRYPT_FAIL_TESTVECTOR; | 1845 return CRYPT_FAIL_TESTVECTOR; |
1838 } | 1846 } |
1839 | 1847 |
1840 return CRYPT_OK; | 1848 return CRYPT_OK; |
1841 #endif | 1849 #endif |
1888 | 1896 |
1889 #endif | 1897 #endif |
1890 | 1898 |
1891 | 1899 |
1892 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/des.c,v $ */ | 1900 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/des.c,v $ */ |
1893 /* $Revision: 1.8 $ */ | 1901 /* $Revision: 1.13 $ */ |
1894 /* $Date: 2005/05/05 14:35:58 $ */ | 1902 /* $Date: 2006/11/08 23:01:06 $ */ |