Mercurial > dropbear
comparison libtomcrypt/src/ciphers/safer/saferp.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 @file saferp.c | 13 @file saferp.c |
14 SAFER+ Implementation by Tom St Denis | 14 SAFER+ Implementation by Tom St Denis |
26 &saferp_ecb_encrypt, | 26 &saferp_ecb_encrypt, |
27 &saferp_ecb_decrypt, | 27 &saferp_ecb_decrypt, |
28 &saferp_test, | 28 &saferp_test, |
29 &saferp_done, | 29 &saferp_done, |
30 &saferp_keysize, | 30 &saferp_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 /* ROUND(b,i) | 34 /* ROUND(b,i) |
35 * | 35 * |
36 * This is one forward key application. Note the basic form is | 36 * This is one forward key application. Note the basic form is |
327 /** | 327 /** |
328 Encrypts a block of text with SAFER+ | 328 Encrypts a block of text with SAFER+ |
329 @param pt The input plaintext (16 bytes) | 329 @param pt The input plaintext (16 bytes) |
330 @param ct The output ciphertext (16 bytes) | 330 @param ct The output ciphertext (16 bytes) |
331 @param skey The key as scheduled | 331 @param skey The key as scheduled |
332 */ | 332 @return CRYPT_OK if successful |
333 void saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | 333 */ |
334 int saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | |
334 { | 335 { |
335 unsigned char b[16]; | 336 unsigned char b[16]; |
336 int x; | 337 int x; |
337 | 338 |
338 LTC_ARGCHK(pt != NULL); | 339 LTC_ARGCHK(pt != NULL); |
382 ct[14] = (b[14] + skey->saferp.K[skey->saferp.rounds*2][14]) & 255; | 383 ct[14] = (b[14] + skey->saferp.K[skey->saferp.rounds*2][14]) & 255; |
383 ct[15] = b[15] ^ skey->saferp.K[skey->saferp.rounds*2][15]; | 384 ct[15] = b[15] ^ skey->saferp.K[skey->saferp.rounds*2][15]; |
384 #ifdef LTC_CLEAN_STACK | 385 #ifdef LTC_CLEAN_STACK |
385 zeromem(b, sizeof(b)); | 386 zeromem(b, sizeof(b)); |
386 #endif | 387 #endif |
388 return CRYPT_OK; | |
387 } | 389 } |
388 | 390 |
389 /** | 391 /** |
390 Decrypts a block of text with SAFER+ | 392 Decrypts a block of text with SAFER+ |
391 @param ct The input ciphertext (16 bytes) | 393 @param ct The input ciphertext (16 bytes) |
392 @param pt The output plaintext (16 bytes) | 394 @param pt The output plaintext (16 bytes) |
393 @param skey The key as scheduled | 395 @param skey The key as scheduled |
394 */ | 396 @return CRYPT_OK if successful |
395 void saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | 397 */ |
398 int saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | |
396 { | 399 { |
397 unsigned char b[16]; | 400 unsigned char b[16]; |
398 int x; | 401 int x; |
399 | 402 |
400 LTC_ARGCHK(pt != NULL); | 403 LTC_ARGCHK(pt != NULL); |
444 pt[x] = b[x]; | 447 pt[x] = b[x]; |
445 } | 448 } |
446 #ifdef LTC_CLEAN_STACK | 449 #ifdef LTC_CLEAN_STACK |
447 zeromem(b, sizeof(b)); | 450 zeromem(b, sizeof(b)); |
448 #endif | 451 #endif |
452 return CRYPT_OK; | |
449 } | 453 } |
450 | 454 |
451 /** | 455 /** |
452 Performs a self-test of the SAFER+ block cipher | 456 Performs a self-test of the SAFER+ block cipher |
453 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled | 457 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled |
501 } | 505 } |
502 saferp_ecb_encrypt(tests[i].pt, tmp[0], &skey); | 506 saferp_ecb_encrypt(tests[i].pt, tmp[0], &skey); |
503 saferp_ecb_decrypt(tmp[0], tmp[1], &skey); | 507 saferp_ecb_decrypt(tmp[0], tmp[1], &skey); |
504 | 508 |
505 /* compare */ | 509 /* compare */ |
506 if (memcmp(tmp[0], tests[i].ct, 16) || memcmp(tmp[1], tests[i].pt, 16)) { | 510 if (XMEMCMP(tmp[0], tests[i].ct, 16) || XMEMCMP(tmp[1], tests[i].pt, 16)) { |
507 return CRYPT_FAIL_TESTVECTOR; | 511 return CRYPT_FAIL_TESTVECTOR; |
508 } | 512 } |
509 | 513 |
510 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | 514 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ |
511 for (y = 0; y < 16; y++) tmp[0][y] = 0; | 515 for (y = 0; y < 16; y++) tmp[0][y] = 0; |
549 #endif | 553 #endif |
550 | 554 |
551 | 555 |
552 | 556 |
553 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/safer/saferp.c,v $ */ | 557 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/safer/saferp.c,v $ */ |
554 /* $Revision: 1.7 $ */ | 558 /* $Revision: 1.12 $ */ |
555 /* $Date: 2005/05/05 14:35:58 $ */ | 559 /* $Date: 2006/11/08 23:01:06 $ */ |