Mercurial > dropbear
comparison libtomcrypt/src/ciphers/safer/safer.c @ 398:59c7938af2bd
merge of '1250b8af44b62d8f4fe0f8d9fc7e7a1cc34e7e1c'
and '7f8670ac3bb975f40967f3979d09d2199b7e90c8'
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 03 Feb 2007 08:20:30 +0000 |
parents | 0cbe8f6dbf9e |
children | f849a5ca2efc |
comparison
equal
deleted
inserted
replaced
396:e7c1a77d2921 | 398:59c7938af2bd |
---|---|
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 * | 13 * |
14 * FILE: safer.c | 14 * FILE: safer.c |
40 &safer_ecb_encrypt, | 40 &safer_ecb_encrypt, |
41 &safer_ecb_decrypt, | 41 &safer_ecb_decrypt, |
42 &safer_k64_test, | 42 &safer_k64_test, |
43 &safer_done, | 43 &safer_done, |
44 &safer_64_keysize, | 44 &safer_64_keysize, |
45 NULL, NULL, NULL, NULL, NULL, NULL, NULL | 45 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
46 }, | 46 }, |
47 | 47 |
48 safer_sk64_desc = { | 48 safer_sk64_desc = { |
49 "safer-sk64", | 49 "safer-sk64", |
50 9, 8, 8, 8, SAFER_SK64_DEFAULT_NOF_ROUNDS, | 50 9, 8, 8, 8, SAFER_SK64_DEFAULT_NOF_ROUNDS, |
52 &safer_ecb_encrypt, | 52 &safer_ecb_encrypt, |
53 &safer_ecb_decrypt, | 53 &safer_ecb_decrypt, |
54 &safer_sk64_test, | 54 &safer_sk64_test, |
55 &safer_done, | 55 &safer_done, |
56 &safer_64_keysize, | 56 &safer_64_keysize, |
57 NULL, NULL, NULL, NULL, NULL, NULL, NULL | 57 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
58 }, | 58 }, |
59 | 59 |
60 safer_k128_desc = { | 60 safer_k128_desc = { |
61 "safer-k128", | 61 "safer-k128", |
62 10, 16, 16, 8, SAFER_K128_DEFAULT_NOF_ROUNDS, | 62 10, 16, 16, 8, SAFER_K128_DEFAULT_NOF_ROUNDS, |
64 &safer_ecb_encrypt, | 64 &safer_ecb_encrypt, |
65 &safer_ecb_decrypt, | 65 &safer_ecb_decrypt, |
66 &safer_sk128_test, | 66 &safer_sk128_test, |
67 &safer_done, | 67 &safer_done, |
68 &safer_128_keysize, | 68 &safer_128_keysize, |
69 NULL, NULL, NULL, NULL, NULL, NULL, NULL | 69 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
70 }, | 70 }, |
71 | 71 |
72 safer_sk128_desc = { | 72 safer_sk128_desc = { |
73 "safer-sk128", | 73 "safer-sk128", |
74 11, 16, 16, 8, SAFER_SK128_DEFAULT_NOF_ROUNDS, | 74 11, 16, 16, 8, SAFER_SK128_DEFAULT_NOF_ROUNDS, |
76 &safer_ecb_encrypt, | 76 &safer_ecb_encrypt, |
77 &safer_ecb_decrypt, | 77 &safer_ecb_decrypt, |
78 &safer_sk128_test, | 78 &safer_sk128_test, |
79 &safer_done, | 79 &safer_done, |
80 &safer_128_keysize, | 80 &safer_128_keysize, |
81 NULL, NULL, NULL, NULL, NULL, NULL, NULL | 81 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
82 }; | 82 }; |
83 | 83 |
84 /******************* Constants ************************************************/ | 84 /******************* Constants ************************************************/ |
85 /* #define TAB_LEN 256 */ | 85 /* #define TAB_LEN 256 */ |
86 | 86 |
244 Safer_Expand_Userkey(key, key+8, (unsigned int)(numrounds != 0?numrounds:SAFER_SK128_DEFAULT_NOF_ROUNDS), 1, skey->safer.key); | 244 Safer_Expand_Userkey(key, key+8, (unsigned int)(numrounds != 0?numrounds:SAFER_SK128_DEFAULT_NOF_ROUNDS), 1, skey->safer.key); |
245 return CRYPT_OK; | 245 return CRYPT_OK; |
246 } | 246 } |
247 | 247 |
248 #ifdef LTC_CLEAN_STACK | 248 #ifdef LTC_CLEAN_STACK |
249 static void _safer_ecb_encrypt(const unsigned char *block_in, | 249 static int _safer_ecb_encrypt(const unsigned char *block_in, |
250 unsigned char *block_out, | 250 unsigned char *block_out, |
251 symmetric_key *skey) | 251 symmetric_key *skey) |
252 #else | 252 #else |
253 void safer_ecb_encrypt(const unsigned char *block_in, | 253 int safer_ecb_encrypt(const unsigned char *block_in, |
254 unsigned char *block_out, | 254 unsigned char *block_out, |
255 symmetric_key *skey) | 255 symmetric_key *skey) |
256 #endif | 256 #endif |
257 { unsigned char a, b, c, d, e, f, g, h, t; | 257 { unsigned char a, b, c, d, e, f, g, h, t; |
258 unsigned int round; | 258 unsigned int round; |
283 e ^= *++key; f += *++key; g += *++key; h ^= *++key; | 283 e ^= *++key; f += *++key; g += *++key; h ^= *++key; |
284 block_out[0] = a & 0xFF; block_out[1] = b & 0xFF; | 284 block_out[0] = a & 0xFF; block_out[1] = b & 0xFF; |
285 block_out[2] = c & 0xFF; block_out[3] = d & 0xFF; | 285 block_out[2] = c & 0xFF; block_out[3] = d & 0xFF; |
286 block_out[4] = e & 0xFF; block_out[5] = f & 0xFF; | 286 block_out[4] = e & 0xFF; block_out[5] = f & 0xFF; |
287 block_out[6] = g & 0xFF; block_out[7] = h & 0xFF; | 287 block_out[6] = g & 0xFF; block_out[7] = h & 0xFF; |
288 } | 288 return CRYPT_OK; |
289 | 289 } |
290 #ifdef LTC_CLEAN_STACK | 290 |
291 void safer_ecb_encrypt(const unsigned char *block_in, | 291 #ifdef LTC_CLEAN_STACK |
292 unsigned char *block_out, | 292 int safer_ecb_encrypt(const unsigned char *block_in, |
293 symmetric_key *skey) | 293 unsigned char *block_out, |
294 { | 294 symmetric_key *skey) |
295 _safer_ecb_encrypt(block_in, block_out, skey); | 295 { |
296 int err = _safer_ecb_encrypt(block_in, block_out, skey); | |
296 burn_stack(sizeof(unsigned char) * 9 + sizeof(unsigned int) + sizeof(unsigned char *)); | 297 burn_stack(sizeof(unsigned char) * 9 + sizeof(unsigned int) + sizeof(unsigned char *)); |
297 } | 298 return err; |
298 #endif | 299 } |
299 | 300 #endif |
300 #ifdef LTC_CLEAN_STACK | 301 |
301 static void _safer_ecb_decrypt(const unsigned char *block_in, | 302 #ifdef LTC_CLEAN_STACK |
303 static int _safer_ecb_decrypt(const unsigned char *block_in, | |
302 unsigned char *block_out, | 304 unsigned char *block_out, |
303 symmetric_key *skey) | 305 symmetric_key *skey) |
304 #else | 306 #else |
305 void safer_ecb_decrypt(const unsigned char *block_in, | 307 int safer_ecb_decrypt(const unsigned char *block_in, |
306 unsigned char *block_out, | 308 unsigned char *block_out, |
307 symmetric_key *skey) | 309 symmetric_key *skey) |
308 #endif | 310 #endif |
309 { unsigned char a, b, c, d, e, f, g, h, t; | 311 { unsigned char a, b, c, d, e, f, g, h, t; |
310 unsigned int round; | 312 unsigned int round; |
336 } | 338 } |
337 block_out[0] = a & 0xFF; block_out[1] = b & 0xFF; | 339 block_out[0] = a & 0xFF; block_out[1] = b & 0xFF; |
338 block_out[2] = c & 0xFF; block_out[3] = d & 0xFF; | 340 block_out[2] = c & 0xFF; block_out[3] = d & 0xFF; |
339 block_out[4] = e & 0xFF; block_out[5] = f & 0xFF; | 341 block_out[4] = e & 0xFF; block_out[5] = f & 0xFF; |
340 block_out[6] = g & 0xFF; block_out[7] = h & 0xFF; | 342 block_out[6] = g & 0xFF; block_out[7] = h & 0xFF; |
341 } | 343 return CRYPT_OK; |
342 | 344 } |
343 #ifdef LTC_CLEAN_STACK | 345 |
344 void safer_ecb_decrypt(const unsigned char *block_in, | 346 #ifdef LTC_CLEAN_STACK |
345 unsigned char *block_out, | 347 int safer_ecb_decrypt(const unsigned char *block_in, |
346 symmetric_key *skey) | 348 unsigned char *block_out, |
347 { | 349 symmetric_key *skey) |
348 _safer_ecb_decrypt(block_in, block_out, skey); | 350 { |
351 int err = _safer_ecb_decrypt(block_in, block_out, skey); | |
349 burn_stack(sizeof(unsigned char) * 9 + sizeof(unsigned int) + sizeof(unsigned char *)); | 352 burn_stack(sizeof(unsigned char) * 9 + sizeof(unsigned int) + sizeof(unsigned char *)); |
353 return err; | |
350 } | 354 } |
351 #endif | 355 #endif |
352 | 356 |
353 int safer_64_keysize(int *keysize) | 357 int safer_64_keysize(int *keysize) |
354 { | 358 { |
390 return err; | 394 return err; |
391 } | 395 } |
392 safer_ecb_encrypt(k64_pt, buf[0], &skey); | 396 safer_ecb_encrypt(k64_pt, buf[0], &skey); |
393 safer_ecb_decrypt(buf[0], buf[1], &skey); | 397 safer_ecb_decrypt(buf[0], buf[1], &skey); |
394 | 398 |
395 if (memcmp(buf[0], k64_ct, 8) != 0 || memcmp(buf[1], k64_pt, 8) != 0) { | 399 if (XMEMCMP(buf[0], k64_ct, 8) != 0 || XMEMCMP(buf[1], k64_pt, 8) != 0) { |
396 return CRYPT_FAIL_TESTVECTOR; | 400 return CRYPT_FAIL_TESTVECTOR; |
397 } | 401 } |
398 | 402 |
399 return CRYPT_OK; | 403 return CRYPT_OK; |
400 #endif | 404 #endif |
420 } | 424 } |
421 | 425 |
422 safer_ecb_encrypt(sk64_pt, buf[0], &skey); | 426 safer_ecb_encrypt(sk64_pt, buf[0], &skey); |
423 safer_ecb_decrypt(buf[0], buf[1], &skey); | 427 safer_ecb_decrypt(buf[0], buf[1], &skey); |
424 | 428 |
425 if (memcmp(buf[0], sk64_ct, 8) != 0 || memcmp(buf[1], sk64_pt, 8) != 0) { | 429 if (XMEMCMP(buf[0], sk64_ct, 8) != 0 || XMEMCMP(buf[1], sk64_pt, 8) != 0) { |
426 return CRYPT_FAIL_TESTVECTOR; | 430 return CRYPT_FAIL_TESTVECTOR; |
427 } | 431 } |
428 | 432 |
429 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | 433 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ |
430 for (y = 0; y < 8; y++) buf[0][y] = 0; | 434 for (y = 0; y < 8; y++) buf[0][y] = 0; |
462 return err; | 466 return err; |
463 } | 467 } |
464 safer_ecb_encrypt(sk128_pt, buf[0], &skey); | 468 safer_ecb_encrypt(sk128_pt, buf[0], &skey); |
465 safer_ecb_decrypt(buf[0], buf[1], &skey); | 469 safer_ecb_decrypt(buf[0], buf[1], &skey); |
466 | 470 |
467 if (memcmp(buf[0], sk128_ct, 8) != 0 || memcmp(buf[1], sk128_pt, 8) != 0) { | 471 if (XMEMCMP(buf[0], sk128_ct, 8) != 0 || XMEMCMP(buf[1], sk128_pt, 8) != 0) { |
468 return CRYPT_FAIL_TESTVECTOR; | 472 return CRYPT_FAIL_TESTVECTOR; |
469 } | 473 } |
470 | 474 |
471 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | 475 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ |
472 for (y = 0; y < 8; y++) buf[0][y] = 0; | 476 for (y = 0; y < 8; y++) buf[0][y] = 0; |
481 | 485 |
482 | 486 |
483 | 487 |
484 | 488 |
485 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/safer/safer.c,v $ */ | 489 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/safer/safer.c,v $ */ |
486 /* $Revision: 1.8 $ */ | 490 /* $Revision: 1.13 $ */ |
487 /* $Date: 2005/05/05 14:35:58 $ */ | 491 /* $Date: 2006/11/08 23:01:06 $ */ |