comparison libtomcrypt/src/ciphers/rc5.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 rc5.c 13 @file rc5.c
14 RC5 code by Tom St Denis 14 RC5 code by Tom St Denis
27 &rc5_ecb_encrypt, 27 &rc5_ecb_encrypt,
28 &rc5_ecb_decrypt, 28 &rc5_ecb_decrypt,
29 &rc5_test, 29 &rc5_test,
30 &rc5_done, 30 &rc5_done,
31 &rc5_keysize, 31 &rc5_keysize,
32 NULL, NULL, NULL, NULL, NULL, NULL, NULL 32 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
33 }; 33 };
34 34
35 static const ulong32 stab[50] = { 35 static const ulong32 stab[50] = {
36 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL, 36 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL,
37 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL, 37 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL,
121 /** 121 /**
122 Encrypts a block of text with RC5 122 Encrypts a block of text with RC5
123 @param pt The input plaintext (8 bytes) 123 @param pt The input plaintext (8 bytes)
124 @param ct The output ciphertext (8 bytes) 124 @param ct The output ciphertext (8 bytes)
125 @param skey The key as scheduled 125 @param skey The key as scheduled
126 */ 126 @return CRYPT_OK if successful
127 #ifdef LTC_CLEAN_STACK 127 */
128 static void _rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 128 #ifdef LTC_CLEAN_STACK
129 static int _rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
129 #else 130 #else
130 void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 131 int rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
131 #endif 132 #endif
132 { 133 {
133 ulong32 A, B, *K; 134 ulong32 A, B, *K;
134 int r; 135 int r;
135 LTC_ARGCHK(skey != NULL); 136 LTC_ARGCHK(skey != NULL);
157 K += 2; 158 K += 2;
158 } 159 }
159 } 160 }
160 STORE32L(A, &ct[0]); 161 STORE32L(A, &ct[0]);
161 STORE32L(B, &ct[4]); 162 STORE32L(B, &ct[4]);
162 } 163
163 164 return CRYPT_OK;
164 #ifdef LTC_CLEAN_STACK 165 }
165 void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 166
166 { 167 #ifdef LTC_CLEAN_STACK
167 _rc5_ecb_encrypt(pt, ct, skey); 168 int rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
169 {
170 int err = _rc5_ecb_encrypt(pt, ct, skey);
168 burn_stack(sizeof(ulong32) * 2 + sizeof(int)); 171 burn_stack(sizeof(ulong32) * 2 + sizeof(int));
172 return err;
169 } 173 }
170 #endif 174 #endif
171 175
172 /** 176 /**
173 Decrypts a block of text with RC5 177 Decrypts a block of text with RC5
174 @param ct The input ciphertext (8 bytes) 178 @param ct The input ciphertext (8 bytes)
175 @param pt The output plaintext (8 bytes) 179 @param pt The output plaintext (8 bytes)
176 @param skey The key as scheduled 180 @param skey The key as scheduled
177 */ 181 @return CRYPT_OK if successful
178 #ifdef LTC_CLEAN_STACK 182 */
179 static void _rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 183 #ifdef LTC_CLEAN_STACK
184 static int _rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
180 #else 185 #else
181 void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 186 int rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
182 #endif 187 #endif
183 { 188 {
184 ulong32 A, B, *K; 189 ulong32 A, B, *K;
185 int r; 190 int r;
186 LTC_ARGCHK(skey != NULL); 191 LTC_ARGCHK(skey != NULL);
209 } 214 }
210 A -= skey->rc5.K[0]; 215 A -= skey->rc5.K[0];
211 B -= skey->rc5.K[1]; 216 B -= skey->rc5.K[1];
212 STORE32L(A, &pt[0]); 217 STORE32L(A, &pt[0]);
213 STORE32L(B, &pt[4]); 218 STORE32L(B, &pt[4]);
214 } 219
215 220 return CRYPT_OK;
216 #ifdef LTC_CLEAN_STACK 221 }
217 void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 222
218 { 223 #ifdef LTC_CLEAN_STACK
219 _rc5_ecb_decrypt(ct, pt, skey); 224 int rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
225 {
226 int err = _rc5_ecb_decrypt(ct, pt, skey);
220 burn_stack(sizeof(ulong32) * 2 + sizeof(int)); 227 burn_stack(sizeof(ulong32) * 2 + sizeof(int));
228 return err;
221 } 229 }
222 #endif 230 #endif
223 231
224 /** 232 /**
225 Performs a self-test of the RC5 block cipher 233 Performs a self-test of the RC5 block cipher
265 /* encrypt and decrypt */ 273 /* encrypt and decrypt */
266 rc5_ecb_encrypt(tests[x].pt, tmp[0], &key); 274 rc5_ecb_encrypt(tests[x].pt, tmp[0], &key);
267 rc5_ecb_decrypt(tmp[0], tmp[1], &key); 275 rc5_ecb_decrypt(tmp[0], tmp[1], &key);
268 276
269 /* compare */ 277 /* compare */
270 if (memcmp(tmp[0], tests[x].ct, 8) != 0 || memcmp(tmp[1], tests[x].pt, 8) != 0) { 278 if (XMEMCMP(tmp[0], tests[x].ct, 8) != 0 || XMEMCMP(tmp[1], tests[x].pt, 8) != 0) {
271 return CRYPT_FAIL_TESTVECTOR; 279 return CRYPT_FAIL_TESTVECTOR;
272 } 280 }
273 281
274 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 282 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
275 for (y = 0; y < 8; y++) tmp[0][y] = 0; 283 for (y = 0; y < 8; y++) tmp[0][y] = 0;
308 316
309 317
310 318
311 319
312 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/rc5.c,v $ */ 320 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/rc5.c,v $ */
313 /* $Revision: 1.7 $ */ 321 /* $Revision: 1.12 $ */
314 /* $Date: 2005/05/05 14:35:58 $ */ 322 /* $Date: 2006/11/08 23:01:06 $ */