comparison libtomcrypt/src/ciphers/rc6.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents 1b9e69c058d2
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
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 rc6.c 13 @file rc6.c
14 RC6 code by Tom St Denis 14 RC6 code by Tom St Denis
26 &rc6_ecb_encrypt, 26 &rc6_ecb_encrypt,
27 &rc6_ecb_decrypt, 27 &rc6_ecb_decrypt,
28 &rc6_test, 28 &rc6_test,
29 &rc6_done, 29 &rc6_done,
30 &rc6_keysize, 30 &rc6_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 static const ulong32 stab[44] = { 34 static const ulong32 stab[44] = {
35 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL, 35 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL,
36 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL, 36 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL,
118 @param pt The input plaintext (16 bytes) 118 @param pt The input plaintext (16 bytes)
119 @param ct The output ciphertext (16 bytes) 119 @param ct The output ciphertext (16 bytes)
120 @param skey The key as scheduled 120 @param skey The key as scheduled
121 */ 121 */
122 #ifdef LTC_CLEAN_STACK 122 #ifdef LTC_CLEAN_STACK
123 static void _rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 123 static int _rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
124 #else 124 #else
125 void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 125 int rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
126 #endif 126 #endif
127 { 127 {
128 ulong32 a,b,c,d,t,u, *K; 128 ulong32 a,b,c,d,t,u, *K;
129 int r; 129 int r;
130 130
153 #undef RND 153 #undef RND
154 154
155 a += skey->rc6.K[42]; 155 a += skey->rc6.K[42];
156 c += skey->rc6.K[43]; 156 c += skey->rc6.K[43];
157 STORE32L(a,&ct[0]);STORE32L(b,&ct[4]);STORE32L(c,&ct[8]);STORE32L(d,&ct[12]); 157 STORE32L(a,&ct[0]);STORE32L(b,&ct[4]);STORE32L(c,&ct[8]);STORE32L(d,&ct[12]);
158 } 158 return CRYPT_OK;
159 159 }
160 #ifdef LTC_CLEAN_STACK 160
161 void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 161 #ifdef LTC_CLEAN_STACK
162 { 162 int rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
163 _rc6_ecb_encrypt(pt, ct, skey); 163 {
164 int err = _rc6_ecb_encrypt(pt, ct, skey);
164 burn_stack(sizeof(ulong32) * 6 + sizeof(int)); 165 burn_stack(sizeof(ulong32) * 6 + sizeof(int));
166 return err;
165 } 167 }
166 #endif 168 #endif
167 169
168 /** 170 /**
169 Decrypts a block of text with RC6 171 Decrypts a block of text with RC6
170 @param ct The input ciphertext (16 bytes) 172 @param ct The input ciphertext (16 bytes)
171 @param pt The output plaintext (16 bytes) 173 @param pt The output plaintext (16 bytes)
172 @param skey The key as scheduled 174 @param skey The key as scheduled
173 */ 175 */
174 #ifdef LTC_CLEAN_STACK 176 #ifdef LTC_CLEAN_STACK
175 static void _rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 177 static int _rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
176 #else 178 #else
177 void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 179 int rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
178 #endif 180 #endif
179 { 181 {
180 ulong32 a,b,c,d,t,u, *K; 182 ulong32 a,b,c,d,t,u, *K;
181 int r; 183 int r;
182 184
206 #undef RND 208 #undef RND
207 209
208 b -= skey->rc6.K[0]; 210 b -= skey->rc6.K[0];
209 d -= skey->rc6.K[1]; 211 d -= skey->rc6.K[1];
210 STORE32L(a,&pt[0]);STORE32L(b,&pt[4]);STORE32L(c,&pt[8]);STORE32L(d,&pt[12]); 212 STORE32L(a,&pt[0]);STORE32L(b,&pt[4]);STORE32L(c,&pt[8]);STORE32L(d,&pt[12]);
211 } 213
212 214 return CRYPT_OK;
213 #ifdef LTC_CLEAN_STACK 215 }
214 void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 216
215 { 217 #ifdef LTC_CLEAN_STACK
216 _rc6_ecb_decrypt(ct, pt, skey); 218 int rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
219 {
220 int err = _rc6_ecb_decrypt(ct, pt, skey);
217 burn_stack(sizeof(ulong32) * 6 + sizeof(int)); 221 burn_stack(sizeof(ulong32) * 6 + sizeof(int));
222 return err;
218 } 223 }
219 #endif 224 #endif
220 225
221 /** 226 /**
222 Performs a self-test of the RC6 block cipher 227 Performs a self-test of the RC6 block cipher
278 /* encrypt and decrypt */ 283 /* encrypt and decrypt */
279 rc6_ecb_encrypt(tests[x].pt, tmp[0], &key); 284 rc6_ecb_encrypt(tests[x].pt, tmp[0], &key);
280 rc6_ecb_decrypt(tmp[0], tmp[1], &key); 285 rc6_ecb_decrypt(tmp[0], tmp[1], &key);
281 286
282 /* compare */ 287 /* compare */
283 if (memcmp(tmp[0], tests[x].ct, 16) || memcmp(tmp[1], tests[x].pt, 16)) { 288 if (XMEMCMP(tmp[0], tests[x].ct, 16) || XMEMCMP(tmp[1], tests[x].pt, 16)) {
284 #if 0 289 #if 0
285 printf("\n\nFailed test %d\n", x); 290 printf("\n\nFailed test %d\n", x);
286 if (memcmp(tmp[0], tests[x].ct, 16)) { 291 if (XMEMCMP(tmp[0], tests[x].ct, 16)) {
287 printf("Ciphertext: "); 292 printf("Ciphertext: ");
288 for (y = 0; y < 16; y++) printf("%02x ", tmp[0][y]); 293 for (y = 0; y < 16; y++) printf("%02x ", tmp[0][y]);
289 printf("\nExpected : "); 294 printf("\nExpected : ");
290 for (y = 0; y < 16; y++) printf("%02x ", tests[x].ct[y]); 295 for (y = 0; y < 16; y++) printf("%02x ", tests[x].ct[y]);
291 printf("\n"); 296 printf("\n");
292 } 297 }
293 if (memcmp(tmp[1], tests[x].pt, 16)) { 298 if (XMEMCMP(tmp[1], tests[x].pt, 16)) {
294 printf("Plaintext: "); 299 printf("Plaintext: ");
295 for (y = 0; y < 16; y++) printf("%02x ", tmp[0][y]); 300 for (y = 0; y < 16; y++) printf("%02x ", tmp[0][y]);
296 printf("\nExpected : "); 301 printf("\nExpected : ");
297 for (y = 0; y < 16; y++) printf("%02x ", tests[x].pt[y]); 302 for (y = 0; y < 16; y++) printf("%02x ", tests[x].pt[y]);
298 printf("\n"); 303 printf("\n");
337 #endif /*RC6*/ 342 #endif /*RC6*/
338 343
339 344
340 345
341 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/rc6.c,v $ */ 346 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/rc6.c,v $ */
342 /* $Revision: 1.7 $ */ 347 /* $Revision: 1.12 $ */
343 /* $Date: 2005/05/05 14:35:58 $ */ 348 /* $Date: 2006/11/08 23:01:06 $ */