comparison src/ciphers/rc2.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
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 /**********************************************************************\ 11 /**********************************************************************\
12 * To commemorate the 1996 RSA Data Security Conference, the following * 12 * To commemorate the 1996 RSA Data Security Conference, the following *
13 * code is released into the public domain by its author. Prost! * 13 * code is released into the public domain by its author. Prost! *
14 * * 14 * *
34 &rc2_ecb_encrypt, 34 &rc2_ecb_encrypt,
35 &rc2_ecb_decrypt, 35 &rc2_ecb_decrypt,
36 &rc2_test, 36 &rc2_test,
37 &rc2_done, 37 &rc2_done,
38 &rc2_keysize, 38 &rc2_keysize,
39 NULL, NULL, NULL, NULL, NULL, NULL, NULL 39 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
40 }; 40 };
41 41
42 /* 256-entry permutation table, probably derived somehow from pi */ 42 /* 256-entry permutation table, probably derived somehow from pi */
43 static const unsigned char permute[256] = { 43 static const unsigned char permute[256] = {
44 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157, 44 217,120,249,196, 25,221,181,237, 40,233,253,121, 74,160,216,157,
123 /** 123 /**
124 Encrypts a block of text with RC2 124 Encrypts a block of text with RC2
125 @param pt The input plaintext (8 bytes) 125 @param pt The input plaintext (8 bytes)
126 @param ct The output ciphertext (8 bytes) 126 @param ct The output ciphertext (8 bytes)
127 @param skey The key as scheduled 127 @param skey The key as scheduled
128 */ 128 @return CRYPT_OK if successful
129 #ifdef LTC_CLEAN_STACK 129 */
130 static void _rc2_ecb_encrypt( const unsigned char *pt, 130 #ifdef LTC_CLEAN_STACK
131 static int _rc2_ecb_encrypt( const unsigned char *pt,
131 unsigned char *ct, 132 unsigned char *ct,
132 symmetric_key *skey) 133 symmetric_key *skey)
133 #else 134 #else
134 void rc2_ecb_encrypt( const unsigned char *pt, 135 int rc2_ecb_encrypt( const unsigned char *pt,
135 unsigned char *ct, 136 unsigned char *ct,
136 symmetric_key *skey) 137 symmetric_key *skey)
137 #endif 138 #endif
138 { 139 {
139 unsigned *xkey; 140 unsigned *xkey;
177 ct[3] = (unsigned char)(x32 >> 8); 178 ct[3] = (unsigned char)(x32 >> 8);
178 ct[4] = (unsigned char)x54; 179 ct[4] = (unsigned char)x54;
179 ct[5] = (unsigned char)(x54 >> 8); 180 ct[5] = (unsigned char)(x54 >> 8);
180 ct[6] = (unsigned char)x76; 181 ct[6] = (unsigned char)x76;
181 ct[7] = (unsigned char)(x76 >> 8); 182 ct[7] = (unsigned char)(x76 >> 8);
182 } 183
183 184 return CRYPT_OK;
184 #ifdef LTC_CLEAN_STACK 185 }
185 void rc2_ecb_encrypt( const unsigned char *pt, 186
187 #ifdef LTC_CLEAN_STACK
188 int rc2_ecb_encrypt( const unsigned char *pt,
186 unsigned char *ct, 189 unsigned char *ct,
187 symmetric_key *skey) 190 symmetric_key *skey)
188 { 191 {
189 _rc2_ecb_encrypt(pt, ct, skey); 192 int err = _rc2_ecb_encrypt(pt, ct, skey);
190 burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 5); 193 burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 5);
194 return err;
191 } 195 }
192 #endif 196 #endif
193 197
194 /**********************************************************************\ 198 /**********************************************************************\
195 * Decrypt an 8-byte block of ciphertext using the given key. * 199 * Decrypt an 8-byte block of ciphertext using the given key. *
197 /** 201 /**
198 Decrypts a block of text with RC2 202 Decrypts a block of text with RC2
199 @param ct The input ciphertext (8 bytes) 203 @param ct The input ciphertext (8 bytes)
200 @param pt The output plaintext (8 bytes) 204 @param pt The output plaintext (8 bytes)
201 @param skey The key as scheduled 205 @param skey The key as scheduled
202 */ 206 @return CRYPT_OK if successful
203 #ifdef LTC_CLEAN_STACK 207 */
204 static void _rc2_ecb_decrypt( const unsigned char *ct, 208 #ifdef LTC_CLEAN_STACK
209 static int _rc2_ecb_decrypt( const unsigned char *ct,
205 unsigned char *pt, 210 unsigned char *pt,
206 symmetric_key *skey) 211 symmetric_key *skey)
207 #else 212 #else
208 void rc2_ecb_decrypt( const unsigned char *ct, 213 int rc2_ecb_decrypt( const unsigned char *ct,
209 unsigned char *pt, 214 unsigned char *pt,
210 symmetric_key *skey) 215 symmetric_key *skey)
211 #endif 216 #endif
212 { 217 {
213 unsigned x76, x54, x32, x10; 218 unsigned x76, x54, x32, x10;
252 pt[3] = (unsigned char)(x32 >> 8); 257 pt[3] = (unsigned char)(x32 >> 8);
253 pt[4] = (unsigned char)x54; 258 pt[4] = (unsigned char)x54;
254 pt[5] = (unsigned char)(x54 >> 8); 259 pt[5] = (unsigned char)(x54 >> 8);
255 pt[6] = (unsigned char)x76; 260 pt[6] = (unsigned char)x76;
256 pt[7] = (unsigned char)(x76 >> 8); 261 pt[7] = (unsigned char)(x76 >> 8);
257 } 262
258 263 return CRYPT_OK;
259 #ifdef LTC_CLEAN_STACK 264 }
260 void rc2_ecb_decrypt( const unsigned char *ct, 265
266 #ifdef LTC_CLEAN_STACK
267 int rc2_ecb_decrypt( const unsigned char *ct,
261 unsigned char *pt, 268 unsigned char *pt,
262 symmetric_key *skey) 269 symmetric_key *skey)
263 { 270 {
264 _rc2_ecb_decrypt(ct, pt, skey); 271 int err = _rc2_ecb_decrypt(ct, pt, skey);
265 burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 4 + sizeof(int)); 272 burn_stack(sizeof(unsigned *) + sizeof(unsigned) * 4 + sizeof(int));
273 return err;
266 } 274 }
267 #endif 275 #endif
268 276
269 /** 277 /**
270 Performs a self-test of the RC2 block cipher 278 Performs a self-test of the RC2 block cipher
305 } 313 }
306 314
307 rc2_ecb_encrypt(tests[x].pt, tmp[0], &skey); 315 rc2_ecb_encrypt(tests[x].pt, tmp[0], &skey);
308 rc2_ecb_decrypt(tmp[0], tmp[1], &skey); 316 rc2_ecb_decrypt(tmp[0], tmp[1], &skey);
309 317
310 if (memcmp(tmp[0], tests[x].ct, 8) != 0 || memcmp(tmp[1], tests[x].pt, 8) != 0) { 318 if (XMEMCMP(tmp[0], tests[x].ct, 8) != 0 || XMEMCMP(tmp[1], tests[x].pt, 8) != 0) {
311 return CRYPT_FAIL_TESTVECTOR; 319 return CRYPT_FAIL_TESTVECTOR;
312 } 320 }
313 321
314 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 322 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
315 for (y = 0; y < 8; y++) tmp[0][y] = 0; 323 for (y = 0; y < 8; y++) tmp[0][y] = 0;
348 356
349 357
350 358
351 359
352 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/rc2.c,v $ */ 360 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/rc2.c,v $ */
353 /* $Revision: 1.7 $ */ 361 /* $Revision: 1.12 $ */
354 /* $Date: 2005/05/05 14:35:58 $ */ 362 /* $Date: 2006/11/08 23:01:06 $ */