Mercurial > dropbear
comparison libtomcrypt/src/ciphers/skipjack.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 @file skipjack.c | 11 @file skipjack.c |
14 Skipjack Implementation by Tom St Denis | 12 Skipjack Implementation by Tom St Denis |
26 &skipjack_ecb_encrypt, | 24 &skipjack_ecb_encrypt, |
27 &skipjack_ecb_decrypt, | 25 &skipjack_ecb_decrypt, |
28 &skipjack_test, | 26 &skipjack_test, |
29 &skipjack_done, | 27 &skipjack_done, |
30 &skipjack_keysize, | 28 &skipjack_keysize, |
31 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | 29 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL |
32 }; | 30 }; |
33 | 31 |
34 static const unsigned char sbox[256] = { | 32 static const unsigned char sbox[256] = { |
35 0xa3,0xd7,0x09,0x83,0xf8,0x48,0xf6,0xf4,0xb3,0x21,0x15,0x78,0x99,0xb1,0xaf,0xf9, | 33 0xa3,0xd7,0x09,0x83,0xf8,0x48,0xf6,0xf4,0xb3,0x21,0x15,0x78,0x99,0xb1,0xaf,0xf9, |
36 0xe7,0x2d,0x4d,0x8a,0xce,0x4c,0xca,0x2e,0x52,0x95,0xd9,0x1e,0x4e,0x38,0x44,0x28, | 34 0xe7,0x2d,0x4d,0x8a,0xce,0x4c,0xca,0x2e,0x52,0x95,0xd9,0x1e,0x4e,0x38,0x44,0x28, |
73 | 71 |
74 if (keylen != 10) { | 72 if (keylen != 10) { |
75 return CRYPT_INVALID_KEYSIZE; | 73 return CRYPT_INVALID_KEYSIZE; |
76 } | 74 } |
77 | 75 |
78 if (num_rounds != 32 && num_rounds != 0) { | 76 if (num_rounds != 32 && num_rounds != 0) { |
79 return CRYPT_INVALID_ROUNDS; | 77 return CRYPT_INVALID_ROUNDS; |
80 } | 78 } |
81 | 79 |
82 /* make sure the key is in range for platforms where CHAR_BIT != 8 */ | 80 /* make sure the key is in range for platforms where CHAR_BIT != 8 */ |
83 for (x = 0; x < 10; x++) { | 81 for (x = 0; x < 10; x++) { |
199 | 197 |
200 /** | 198 /** |
201 Decrypts a block of text with Skipjack | 199 Decrypts a block of text with Skipjack |
202 @param ct The input ciphertext (8 bytes) | 200 @param ct The input ciphertext (8 bytes) |
203 @param pt The output plaintext (8 bytes) | 201 @param pt The output plaintext (8 bytes) |
204 @param skey The key as scheduled | 202 @param skey The key as scheduled |
205 @return CRYPT_OK if successful | 203 @return CRYPT_OK if successful |
206 */ | 204 */ |
207 #ifdef LTC_CLEAN_STACK | 205 #ifdef LTC_CLEAN_STACK |
208 static int _skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | 206 static int _skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) |
209 #else | 207 #else |
221 w1 = ((unsigned)ct[0]<<8)|ct[1]; | 219 w1 = ((unsigned)ct[0]<<8)|ct[1]; |
222 w2 = ((unsigned)ct[2]<<8)|ct[3]; | 220 w2 = ((unsigned)ct[2]<<8)|ct[3]; |
223 w3 = ((unsigned)ct[4]<<8)|ct[5]; | 221 w3 = ((unsigned)ct[4]<<8)|ct[5]; |
224 w4 = ((unsigned)ct[6]<<8)|ct[7]; | 222 w4 = ((unsigned)ct[6]<<8)|ct[7]; |
225 | 223 |
226 /* 8 rounds of RULE B^-1 | 224 /* 8 rounds of RULE B^-1 |
227 | 225 |
228 Note the value "kp = 8" comes from "kp = (32 * 4) mod 10" where 32*4 is 128 which mod 10 is 8 | 226 Note the value "kp = 8" comes from "kp = (32 * 4) mod 10" where 32*4 is 128 which mod 10 is 8 |
229 */ | 227 */ |
230 for (x = 32, kp = 8; x > 24; x--) { | 228 for (x = 32, kp = 8; x > 24; x--) { |
231 RULE_B1; | 229 RULE_B1; |
271 */ | 269 */ |
272 int skipjack_test(void) | 270 int skipjack_test(void) |
273 { | 271 { |
274 #ifndef LTC_TEST | 272 #ifndef LTC_TEST |
275 return CRYPT_NOP; | 273 return CRYPT_NOP; |
276 #else | 274 #else |
277 static const struct { | 275 static const struct { |
278 unsigned char key[10], pt[8], ct[8]; | 276 unsigned char key[10], pt[8], ct[8]; |
279 } tests[] = { | 277 } tests[] = { |
280 { | 278 { |
281 { 0x00, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, | 279 { 0x00, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 }, |
296 /* encrypt and decrypt */ | 294 /* encrypt and decrypt */ |
297 skipjack_ecb_encrypt(tests[x].pt, buf[0], &key); | 295 skipjack_ecb_encrypt(tests[x].pt, buf[0], &key); |
298 skipjack_ecb_decrypt(buf[0], buf[1], &key); | 296 skipjack_ecb_decrypt(buf[0], buf[1], &key); |
299 | 297 |
300 /* compare */ | 298 /* compare */ |
301 if (XMEMCMP(buf[0], tests[x].ct, 8) != 0 || XMEMCMP(buf[1], tests[x].pt, 8) != 0) { | 299 if (compare_testvector(buf[0], 8, tests[x].ct, 8, "Skipjack Encrypt", x) != 0 || |
300 compare_testvector(buf[1], 8, tests[x].pt, 8, "Skipjack Decrypt", x) != 0) { | |
302 return CRYPT_FAIL_TESTVECTOR; | 301 return CRYPT_FAIL_TESTVECTOR; |
303 } | 302 } |
304 | 303 |
305 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | 304 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ |
306 for (y = 0; y < 8; y++) buf[0][y] = 0; | 305 for (y = 0; y < 8; y++) buf[0][y] = 0; |
311 | 310 |
312 return CRYPT_OK; | 311 return CRYPT_OK; |
313 #endif | 312 #endif |
314 } | 313 } |
315 | 314 |
316 /** Terminate the context | 315 /** Terminate the context |
317 @param skey The scheduled key | 316 @param skey The scheduled key |
318 */ | 317 */ |
319 void skipjack_done(symmetric_key *skey) | 318 void skipjack_done(symmetric_key *skey) |
320 { | 319 { |
320 LTC_UNUSED_PARAM(skey); | |
321 } | 321 } |
322 | 322 |
323 /** | 323 /** |
324 Gets suitable key size | 324 Gets suitable key size |
325 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. | 325 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. |
336 return CRYPT_OK; | 336 return CRYPT_OK; |
337 } | 337 } |
338 | 338 |
339 #endif | 339 #endif |
340 | 340 |
341 /* $Source$ */ | 341 /* ref: $Format:%D$ */ |
342 /* $Revision$ */ | 342 /* git commit: $Format:%H$ */ |
343 /* $Date$ */ | 343 /* commit time: $Format:%ai$ */ |