comparison libtomcrypt/src/ciphers/xtea.c @ 435:337c45621e81

merge of 'a9b0496634cdd25647b65e585cc3240f3fa699ee' and 'c22be8b8f570b48e9662dac32c7b3e7148a42206'
author Matt Johnston <matt@ucc.asn.au>
date Thu, 22 Feb 2007 14:53:49 +0000
parents 0cbe8f6dbf9e
children f849a5ca2efc
comparison
equal deleted inserted replaced
434:0aaaf68e97dc 435:337c45621e81
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 xtea.c 13 @file xtea.c
14 Implementation of XTEA, Tom St Denis 14 Implementation of XTEA, Tom St Denis
26 &xtea_ecb_encrypt, 26 &xtea_ecb_encrypt,
27 &xtea_ecb_decrypt, 27 &xtea_ecb_decrypt,
28 &xtea_test, 28 &xtea_test,
29 &xtea_done, 29 &xtea_done,
30 &xtea_keysize, 30 &xtea_keysize,
31 NULL, NULL, NULL, NULL, NULL, NULL, NULL 31 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
32 }; 32 };
33 33
34 int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) 34 int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
35 { 35 {
36 unsigned long x, sum, K[4]; 36 unsigned long x, sum, K[4];
69 /** 69 /**
70 Encrypts a block of text with XTEA 70 Encrypts a block of text with XTEA
71 @param pt The input plaintext (8 bytes) 71 @param pt The input plaintext (8 bytes)
72 @param ct The output ciphertext (8 bytes) 72 @param ct The output ciphertext (8 bytes)
73 @param skey The key as scheduled 73 @param skey The key as scheduled
74 */ 74 @return CRYPT_OK if successful
75 void xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 75 */
76 int xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
76 { 77 {
77 unsigned long y, z; 78 unsigned long y, z;
78 int r; 79 int r;
79 80
80 LTC_ARGCHK(pt != NULL); 81 LTC_ARGCHK(pt != NULL);
96 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+3])) & 0xFFFFFFFFUL; 97 y = (y + ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r+3])) & 0xFFFFFFFFUL;
97 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+3])) & 0xFFFFFFFFUL; 98 z = (z + ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r+3])) & 0xFFFFFFFFUL;
98 } 99 }
99 STORE32L(y, &ct[0]); 100 STORE32L(y, &ct[0]);
100 STORE32L(z, &ct[4]); 101 STORE32L(z, &ct[4]);
102 return CRYPT_OK;
101 } 103 }
102 104
103 /** 105 /**
104 Decrypts a block of text with XTEA 106 Decrypts a block of text with XTEA
105 @param ct The input ciphertext (8 bytes) 107 @param ct The input ciphertext (8 bytes)
106 @param pt The output plaintext (8 bytes) 108 @param pt The output plaintext (8 bytes)
107 @param skey The key as scheduled 109 @param skey The key as scheduled
108 */ 110 @return CRYPT_OK if successful
109 void xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 111 */
112 int xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
110 { 113 {
111 unsigned long y, z; 114 unsigned long y, z;
112 int r; 115 int r;
113 116
114 LTC_ARGCHK(pt != NULL); 117 LTC_ARGCHK(pt != NULL);
130 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-3])) & 0xFFFFFFFFUL; 133 z = (z - ((((y<<4)^(y>>5)) + y) ^ skey->xtea.B[r-3])) & 0xFFFFFFFFUL;
131 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-3])) & 0xFFFFFFFFUL; 134 y = (y - ((((z<<4)^(z>>5)) + z) ^ skey->xtea.A[r-3])) & 0xFFFFFFFFUL;
132 } 135 }
133 STORE32L(y, &pt[0]); 136 STORE32L(y, &pt[0]);
134 STORE32L(z, &pt[4]); 137 STORE32L(z, &pt[4]);
138 return CRYPT_OK;
135 } 139 }
136 140
137 /** 141 /**
138 Performs a self-test of the XTEA block cipher 142 Performs a self-test of the XTEA block cipher
139 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled 143 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
158 return err; 162 return err;
159 } 163 }
160 xtea_ecb_encrypt(pt, tmp[0], &skey); 164 xtea_ecb_encrypt(pt, tmp[0], &skey);
161 xtea_ecb_decrypt(tmp[0], tmp[1], &skey); 165 xtea_ecb_decrypt(tmp[0], tmp[1], &skey);
162 166
163 if (memcmp(tmp[0], ct, 8) != 0 || memcmp(tmp[1], pt, 8) != 0) { 167 if (XMEMCMP(tmp[0], ct, 8) != 0 || XMEMCMP(tmp[1], pt, 8) != 0) {
164 return CRYPT_FAIL_TESTVECTOR; 168 return CRYPT_FAIL_TESTVECTOR;
165 } 169 }
166 170
167 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 171 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
168 for (y = 0; y < 8; y++) tmp[0][y] = 0; 172 for (y = 0; y < 8; y++) tmp[0][y] = 0;
201 205
202 206
203 207
204 208
205 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/xtea.c,v $ */ 209 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/xtea.c,v $ */
206 /* $Revision: 1.7 $ */ 210 /* $Revision: 1.12 $ */
207 /* $Date: 2005/05/05 14:35:58 $ */ 211 /* $Date: 2006/11/08 23:01:06 $ */