comparison libtomcrypt/src/ciphers/safer/safer.c @ 1511:5916af64acd4 fuzz

merge from main
author Matt Johnston <matt@ucc.asn.au>
date Sat, 17 Feb 2018 19:29:51 +0800
parents 6dba84798cd5
children
comparison
equal deleted inserted replaced
1457:32f990cc96b1 1511:5916af64acd4
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
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 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 9
12 /******************************************************************************* 10 /*******************************************************************************
13 * 11 *
14 * FILE: safer.c 12 * FILE: safer.c
26 * 24 *
27 * CHANGE HISTORY: 25 * CHANGE HISTORY:
28 * 26 *
29 *******************************************************************************/ 27 *******************************************************************************/
30 28
31 #include <tomcrypt.h> 29 #include "tomcrypt.h"
32 30
33 #ifdef LTC_SAFER 31 #ifdef LTC_SAFER
34 32
35 const struct ltc_cipher_descriptor 33 #define __LTC_SAFER_TAB_C__
36 safer_k64_desc = { 34 #include "safer_tab.c"
37 "safer-k64", 35
36 const struct ltc_cipher_descriptor safer_k64_desc = {
37 "safer-k64",
38 8, 8, 8, 8, LTC_SAFER_K64_DEFAULT_NOF_ROUNDS, 38 8, 8, 8, 8, LTC_SAFER_K64_DEFAULT_NOF_ROUNDS,
39 &safer_k64_setup, 39 &safer_k64_setup,
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, NULL, NULL, NULL, NULL, NULL 45 NULL, NULL, 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, LTC_SAFER_SK64_DEFAULT_NOF_ROUNDS, 50 9, 8, 8, 8, LTC_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, NULL, NULL, NULL, NULL, NULL 57 NULL, NULL, 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, LTC_SAFER_K128_DEFAULT_NOF_ROUNDS, 62 10, 16, 16, 8, LTC_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, NULL, NULL, NULL, NULL, NULL 69 NULL, NULL, 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, LTC_SAFER_SK128_DEFAULT_NOF_ROUNDS, 74 11, 16, 16, 8, LTC_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, NULL, NULL, NULL, NULL, NULL 81 NULL, NULL, 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
93 #define LOG(x) safer_lbox[(x) & 0xFF] 93 #define LOG(x) safer_lbox[(x) & 0xFF]
94 #define PHT(x, y) { y += x; x += y; } 94 #define PHT(x, y) { y += x; x += y; }
95 #define IPHT(x, y) { x -= y; y -= x; } 95 #define IPHT(x, y) { x -= y; y -= x; }
96 96
97 /******************* Types ****************************************************/ 97 /******************* Types ****************************************************/
98 extern const unsigned char safer_ebox[], safer_lbox[];
99 98
100 #ifdef LTC_CLEAN_STACK 99 #ifdef LTC_CLEAN_STACK
101 static void _Safer_Expand_Userkey(const unsigned char *userkey_1, 100 static void _Safer_Expand_Userkey(const unsigned char *userkey_1,
102 const unsigned char *userkey_2, 101 const unsigned char *userkey_2,
103 unsigned int nof_rounds, 102 unsigned int nof_rounds,
156 } else { 155 } else {
157 *key++ = (kb[j] + safer_ebox[(int)safer_ebox[(int)((18 * i + j + 10)&0xFF)]]) & 0xFF; 156 *key++ = (kb[j] + safer_ebox[(int)safer_ebox[(int)((18 * i + j + 10)&0xFF)]]) & 0xFF;
158 } 157 }
159 } 158 }
160 } 159 }
161 160
162 #ifdef LTC_CLEAN_STACK 161 #ifdef LTC_CLEAN_STACK
163 zeromem(ka, sizeof(ka)); 162 zeromem(ka, sizeof(ka));
164 zeromem(kb, sizeof(kb)); 163 zeromem(kb, sizeof(kb));
165 #endif 164 #endif
166 } 165 }
191 } 190 }
192 191
193 Safer_Expand_Userkey(key, key, (unsigned int)(numrounds != 0 ?numrounds:LTC_SAFER_K64_DEFAULT_NOF_ROUNDS), 0, skey->safer.key); 192 Safer_Expand_Userkey(key, key, (unsigned int)(numrounds != 0 ?numrounds:LTC_SAFER_K64_DEFAULT_NOF_ROUNDS), 0, skey->safer.key);
194 return CRYPT_OK; 193 return CRYPT_OK;
195 } 194 }
196 195
197 int safer_sk64_setup(const unsigned char *key, int keylen, int numrounds, symmetric_key *skey) 196 int safer_sk64_setup(const unsigned char *key, int keylen, int numrounds, symmetric_key *skey)
198 { 197 {
199 LTC_ARGCHK(key != NULL); 198 LTC_ARGCHK(key != NULL);
200 LTC_ARGCHK(skey != NULL); 199 LTC_ARGCHK(skey != NULL);
201 200
378 377
379 int safer_k64_test(void) 378 int safer_k64_test(void)
380 { 379 {
381 #ifndef LTC_TEST 380 #ifndef LTC_TEST
382 return CRYPT_NOP; 381 return CRYPT_NOP;
383 #else 382 #else
384 static const unsigned char k64_pt[] = { 1, 2, 3, 4, 5, 6, 7, 8 }, 383 static const unsigned char k64_pt[] = { 1, 2, 3, 4, 5, 6, 7, 8 },
385 k64_key[] = { 8, 7, 6, 5, 4, 3, 2, 1 }, 384 k64_key[] = { 8, 7, 6, 5, 4, 3, 2, 1 },
386 k64_ct[] = { 200, 242, 156, 221, 135, 120, 62, 217 }; 385 k64_ct[] = { 200, 242, 156, 221, 135, 120, 62, 217 };
387 386
388 symmetric_key skey; 387 symmetric_key skey;
394 return err; 393 return err;
395 } 394 }
396 safer_ecb_encrypt(k64_pt, buf[0], &skey); 395 safer_ecb_encrypt(k64_pt, buf[0], &skey);
397 safer_ecb_decrypt(buf[0], buf[1], &skey); 396 safer_ecb_decrypt(buf[0], buf[1], &skey);
398 397
399 if (XMEMCMP(buf[0], k64_ct, 8) != 0 || XMEMCMP(buf[1], k64_pt, 8) != 0) { 398 if (compare_testvector(buf[0], 8, k64_ct, 8, "Safer K64 Encrypt", 0) != 0 ||
399 compare_testvector(buf[1], 8, k64_pt, 8, "Safer K64 Decrypt", 0) != 0) {
400 return CRYPT_FAIL_TESTVECTOR; 400 return CRYPT_FAIL_TESTVECTOR;
401 } 401 }
402 402
403 return CRYPT_OK; 403 return CRYPT_OK;
404 #endif 404 #endif
407 407
408 int safer_sk64_test(void) 408 int safer_sk64_test(void)
409 { 409 {
410 #ifndef LTC_TEST 410 #ifndef LTC_TEST
411 return CRYPT_NOP; 411 return CRYPT_NOP;
412 #else 412 #else
413 static const unsigned char sk64_pt[] = { 1, 2, 3, 4, 5, 6, 7, 8 }, 413 static const unsigned char sk64_pt[] = { 1, 2, 3, 4, 5, 6, 7, 8 },
414 sk64_key[] = { 1, 2, 3, 4, 5, 6, 7, 8 }, 414 sk64_key[] = { 1, 2, 3, 4, 5, 6, 7, 8 },
415 sk64_ct[] = { 95, 206, 155, 162, 5, 132, 56, 199 }; 415 sk64_ct[] = { 95, 206, 155, 162, 5, 132, 56, 199 };
416 416
417 symmetric_key skey; 417 symmetric_key skey;
424 } 424 }
425 425
426 safer_ecb_encrypt(sk64_pt, buf[0], &skey); 426 safer_ecb_encrypt(sk64_pt, buf[0], &skey);
427 safer_ecb_decrypt(buf[0], buf[1], &skey); 427 safer_ecb_decrypt(buf[0], buf[1], &skey);
428 428
429 if (XMEMCMP(buf[0], sk64_ct, 8) != 0 || XMEMCMP(buf[1], sk64_pt, 8) != 0) { 429 if (compare_testvector(buf[0], 8, sk64_ct, 8, "Safer SK64 Encrypt", 0) != 0 ||
430 compare_testvector(buf[1], 8, sk64_pt, 8, "Safer SK64 Decrypt", 0) != 0) {
430 return CRYPT_FAIL_TESTVECTOR; 431 return CRYPT_FAIL_TESTVECTOR;
431 } 432 }
432 433
433 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 434 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
434 for (y = 0; y < 8; y++) buf[0][y] = 0; 435 for (y = 0; y < 8; y++) buf[0][y] = 0;
435 for (y = 0; y < 1000; y++) safer_ecb_encrypt(buf[0], buf[0], &skey); 436 for (y = 0; y < 1000; y++) safer_ecb_encrypt(buf[0], buf[0], &skey);
436 for (y = 0; y < 1000; y++) safer_ecb_decrypt(buf[0], buf[0], &skey); 437 for (y = 0; y < 1000; y++) safer_ecb_decrypt(buf[0], buf[0], &skey);
437 for (y = 0; y < 8; y++) if (buf[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; 438 for (y = 0; y < 8; y++) if (buf[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
438 439
439 return CRYPT_OK; 440 return CRYPT_OK;
440 #endif 441 #endif
441 } 442 }
442 443
443 /** Terminate the context 444 /** Terminate the context
444 @param skey The scheduled key 445 @param skey The scheduled key
445 */ 446 */
446 void safer_done(symmetric_key *skey) 447 void safer_done(symmetric_key *skey)
447 { 448 {
449 LTC_UNUSED_PARAM(skey);
448 } 450 }
449 451
450 int safer_sk128_test(void) 452 int safer_sk128_test(void)
451 { 453 {
452 #ifndef LTC_TEST 454 #ifndef LTC_TEST
453 return CRYPT_NOP; 455 return CRYPT_NOP;
454 #else 456 #else
455 static const unsigned char sk128_pt[] = { 1, 2, 3, 4, 5, 6, 7, 8 }, 457 static const unsigned char sk128_pt[] = { 1, 2, 3, 4, 5, 6, 7, 8 },
456 sk128_key[] = { 1, 2, 3, 4, 5, 6, 7, 8, 458 sk128_key[] = { 1, 2, 3, 4, 5, 6, 7, 8,
457 0, 0, 0, 0, 0, 0, 0, 0 }, 459 0, 0, 0, 0, 0, 0, 0, 0 },
458 sk128_ct[] = { 255, 120, 17, 228, 179, 167, 46, 113 }; 460 sk128_ct[] = { 255, 120, 17, 228, 179, 167, 46, 113 };
459 461
466 return err; 468 return err;
467 } 469 }
468 safer_ecb_encrypt(sk128_pt, buf[0], &skey); 470 safer_ecb_encrypt(sk128_pt, buf[0], &skey);
469 safer_ecb_decrypt(buf[0], buf[1], &skey); 471 safer_ecb_decrypt(buf[0], buf[1], &skey);
470 472
471 if (XMEMCMP(buf[0], sk128_ct, 8) != 0 || XMEMCMP(buf[1], sk128_pt, 8) != 0) { 473 if (compare_testvector(buf[0], 8, sk128_ct, 8, "Safer SK128 Encrypt", 0) != 0 ||
474 compare_testvector(buf[1], 8, sk128_pt, 8, "Safer SK128 Decrypt", 0) != 0) {
472 return CRYPT_FAIL_TESTVECTOR; 475 return CRYPT_FAIL_TESTVECTOR;
473 } 476 }
474 477
475 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ 478 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
476 for (y = 0; y < 8; y++) buf[0][y] = 0; 479 for (y = 0; y < 8; y++) buf[0][y] = 0;
477 for (y = 0; y < 1000; y++) safer_ecb_encrypt(buf[0], buf[0], &skey); 480 for (y = 0; y < 1000; y++) safer_ecb_encrypt(buf[0], buf[0], &skey);
478 for (y = 0; y < 1000; y++) safer_ecb_decrypt(buf[0], buf[0], &skey); 481 for (y = 0; y < 1000; y++) safer_ecb_decrypt(buf[0], buf[0], &skey);
479 for (y = 0; y < 8; y++) if (buf[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; 482 for (y = 0; y < 8; y++) if (buf[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
480 return CRYPT_OK; 483
484 return CRYPT_OK;
481 #endif 485 #endif
482 } 486 }
483 487
484 #endif 488 #endif
485 489
486 490
487 491
488 492
489 /* $Source$ */ 493 /* ref: $Format:%D$ */
490 /* $Revision$ */ 494 /* git commit: $Format:%H$ */
491 /* $Date$ */ 495 /* commit time: $Format:%ai$ */