comparison libtomcrypt/src/ciphers/twofish/twofish.c @ 1439:8d24733026c5 coverity

merge
author Matt Johnston <matt@ucc.asn.au>
date Sat, 24 Jun 2017 23:33:16 +0800
parents f849a5ca2efc
children 6dba84798cd5
comparison
equal deleted inserted replaced
1400:238a439670f5 1439:8d24733026c5
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.com 9 * Tom St Denis, [email protected], http://libtom.org
10 */ 10 */
11 11
12 /** 12 /**
13 @file twofish.c 13 @file twofish.c
14 Implementation of Twofish by Tom St Denis 14 Implementation of Twofish by Tom St Denis
15 */ 15 */
16 #include "tomcrypt.h" 16 #include "tomcrypt.h"
17 17
18 #ifdef TWOFISH 18 #ifdef LTC_TWOFISH
19 19
20 /* first TWOFISH_ALL_TABLES must ensure TWOFISH_TABLES is defined */ 20 /* first LTC_TWOFISH_ALL_TABLES must ensure LTC_TWOFISH_TABLES is defined */
21 #ifdef TWOFISH_ALL_TABLES 21 #ifdef LTC_TWOFISH_ALL_TABLES
22 #ifndef TWOFISH_TABLES 22 #ifndef LTC_TWOFISH_TABLES
23 #define TWOFISH_TABLES 23 #define LTC_TWOFISH_TABLES
24 #endif 24 #endif
25 #endif 25 #endif
26 26
27 const struct ltc_cipher_descriptor twofish_desc = 27 const struct ltc_cipher_descriptor twofish_desc =
28 { 28 {
66 { 0, 1, 1, 0, 0 }, 66 { 0, 1, 1, 0, 0 },
67 { 0, 0, 0, 1, 1 }, 67 { 0, 0, 0, 1, 1 },
68 { 1, 0, 1, 1, 0 } 68 { 1, 0, 1, 1, 0 }
69 }; 69 };
70 70
71 #ifdef TWOFISH_TABLES 71 #ifdef LTC_TWOFISH_TABLES
72 72
73 #include "twofish_tab.c" 73 #include "twofish_tab.c"
74 74
75 #define sbox(i, x) ((ulong32)SBOX[i][(x)&255]) 75 #define sbox(i, x) ((ulong32)SBOX[i][(x)&255])
76 76
140 burn_stack(sizeof(unsigned char) * 11); 140 burn_stack(sizeof(unsigned char) * 11);
141 return y; 141 return y;
142 } 142 }
143 #endif /* LTC_CLEAN_STACK */ 143 #endif /* LTC_CLEAN_STACK */
144 144
145 #endif /* TWOFISH_TABLES */ 145 #endif /* LTC_TWOFISH_TABLES */
146 146
147 /* computes ab mod p */ 147 /* computes ab mod p */
148 static ulong32 gf_mult(ulong32 a, ulong32 b, ulong32 p) 148 static ulong32 gf_mult(ulong32 a, ulong32 b, ulong32 p)
149 { 149 {
150 ulong32 result, B[2], P[2]; 150 ulong32 result, B[2], P[2];
165 165
166 return result; 166 return result;
167 } 167 }
168 168
169 /* computes [y0 y1 y2 y3] = MDS . [x0] */ 169 /* computes [y0 y1 y2 y3] = MDS . [x0] */
170 #ifndef TWOFISH_TABLES 170 #ifndef LTC_TWOFISH_TABLES
171 static ulong32 mds_column_mult(unsigned char in, int col) 171 static ulong32 mds_column_mult(unsigned char in, int col)
172 { 172 {
173 ulong32 x01, x5B, xEF; 173 ulong32 x01, x5B, xEF;
174 174
175 x01 = in; 175 x01 = in;
200 } 200 }
201 /* avoid warnings, we'd never get here normally but just to calm compiler warnings... */ 201 /* avoid warnings, we'd never get here normally but just to calm compiler warnings... */
202 return 0; 202 return 0;
203 } 203 }
204 204
205 #else /* !TWOFISH_TABLES */ 205 #else /* !LTC_TWOFISH_TABLES */
206 206
207 #define mds_column_mult(x, i) mds_tab[i][x] 207 #define mds_column_mult(x, i) mds_tab[i][x]
208 208
209 #endif /* TWOFISH_TABLES */ 209 #endif /* LTC_TWOFISH_TABLES */
210 210
211 /* Computes [y0 y1 y2 y3] = MDS . [x0 x1 x2 x3] */ 211 /* Computes [y0 y1 y2 y3] = MDS . [x0 x1 x2 x3] */
212 static void mds_mult(const unsigned char *in, unsigned char *out) 212 static void mds_mult(const unsigned char *in, unsigned char *out)
213 { 213 {
214 int x; 214 int x;
217 tmp ^= mds_column_mult(in[x], x); 217 tmp ^= mds_column_mult(in[x], x);
218 } 218 }
219 STORE32L(tmp, out); 219 STORE32L(tmp, out);
220 } 220 }
221 221
222 #ifdef TWOFISH_ALL_TABLES 222 #ifdef LTC_TWOFISH_ALL_TABLES
223 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */ 223 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
224 static void rs_mult(const unsigned char *in, unsigned char *out) 224 static void rs_mult(const unsigned char *in, unsigned char *out)
225 { 225 {
226 ulong32 tmp; 226 ulong32 tmp;
227 tmp = rs_tab0[in[0]] ^ rs_tab1[in[1]] ^ rs_tab2[in[2]] ^ rs_tab3[in[3]] ^ 227 tmp = rs_tab0[in[0]] ^ rs_tab1[in[1]] ^ rs_tab2[in[2]] ^ rs_tab3[in[3]] ^
228 rs_tab4[in[4]] ^ rs_tab5[in[5]] ^ rs_tab6[in[6]] ^ rs_tab7[in[7]]; 228 rs_tab4[in[4]] ^ rs_tab5[in[5]] ^ rs_tab6[in[6]] ^ rs_tab7[in[7]];
229 STORE32L(tmp, out); 229 STORE32L(tmp, out);
230 } 230 }
231 231
232 #else /* !TWOFISH_ALL_TABLES */ 232 #else /* !LTC_TWOFISH_ALL_TABLES */
233 233
234 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */ 234 /* computes [y0 y1 y2 y3] = RS . [x0 x1 x2 x3 x4 x5 x6 x7] */
235 static void rs_mult(const unsigned char *in, unsigned char *out) 235 static void rs_mult(const unsigned char *in, unsigned char *out)
236 { 236 {
237 int x, y; 237 int x, y;
271 y[3] = (unsigned char)(sbox(0, sbox(1, sbox(1, (ulong32)y[3]) ^ M[4 * (2 + offset) + 3]) ^ M[4 * (0 + offset) + 3])); 271 y[3] = (unsigned char)(sbox(0, sbox(1, sbox(1, (ulong32)y[3]) ^ M[4 * (2 + offset) + 3]) ^ M[4 * (0 + offset) + 3]));
272 } 272 }
273 mds_mult(y, out); 273 mds_mult(y, out);
274 } 274 }
275 275
276 #ifndef TWOFISH_SMALL 276 #ifndef LTC_TWOFISH_SMALL
277 277
278 /* for GCC we don't use pointer aliases */ 278 /* for GCC we don't use pointer aliases */
279 #if defined(__GNUC__) 279 #if defined(__GNUC__)
280 #define S1 skey->twofish.S[0] 280 #define S1 skey->twofish.S[0]
281 #define S2 skey->twofish.S[1] 281 #define S2 skey->twofish.S[1]
330 burn_stack(sizeof(unsigned char) * 4 + sizeof(ulong32)); 330 burn_stack(sizeof(unsigned char) * 4 + sizeof(ulong32));
331 return y; 331 return y;
332 } 332 }
333 #endif /* LTC_CLEAN_STACK */ 333 #endif /* LTC_CLEAN_STACK */
334 334
335 #endif /* TWOFISH_SMALL */ 335 #endif /* LTC_TWOFISH_SMALL */
336 336
337 /** 337 /**
338 Initialize the Twofish block cipher 338 Initialize the Twofish block cipher
339 @param key The symmetric key you wish to pass 339 @param key The symmetric key you wish to pass
340 @param keylen The key length in bytes 340 @param keylen The key length in bytes
346 static int _twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) 346 static int _twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
347 #else 347 #else
348 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) 348 int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
349 #endif 349 #endif
350 { 350 {
351 #ifndef TWOFISH_SMALL 351 #ifndef LTC_TWOFISH_SMALL
352 unsigned char S[4*4], tmpx0, tmpx1; 352 unsigned char S[4*4], tmpx0, tmpx1;
353 #endif 353 #endif
354 int k, x, y; 354 int k, x, y;
355 unsigned char tmp[4], tmp2[4], M[8*4]; 355 unsigned char tmp[4], tmp2[4], M[8*4];
356 ulong32 A, B; 356 ulong32 A, B;
374 for (x = 0; x < keylen; x++) { 374 for (x = 0; x < keylen; x++) {
375 M[x] = key[x] & 255; 375 M[x] = key[x] & 255;
376 } 376 }
377 377
378 /* create the S[..] words */ 378 /* create the S[..] words */
379 #ifndef TWOFISH_SMALL 379 #ifndef LTC_TWOFISH_SMALL
380 for (x = 0; x < k; x++) { 380 for (x = 0; x < k; x++) {
381 rs_mult(M+(x*8), S+(x*4)); 381 rs_mult(M+(x*8), S+(x*4));
382 } 382 }
383 #else 383 #else
384 for (x = 0; x < k; x++) { 384 for (x = 0; x < k; x++) {
408 408
409 /* K[2i+1] = (A + 2B) <<< 9 */ 409 /* K[2i+1] = (A + 2B) <<< 9 */
410 skey->twofish.K[x+x+1] = ROLc(B + B + A, 9); 410 skey->twofish.K[x+x+1] = ROLc(B + B + A, 9);
411 } 411 }
412 412
413 #ifndef TWOFISH_SMALL 413 #ifndef LTC_TWOFISH_SMALL
414 /* make the sboxes (large ram variant) */ 414 /* make the sboxes (large ram variant) */
415 if (k == 2) { 415 if (k == 2) {
416 for (x = 0; x < 256; x++) { 416 for (x = 0; x < 256; x++) {
417 tmpx0 = (unsigned char)sbox(0, x); 417 tmpx0 = (unsigned char)sbox(0, x);
418 tmpx1 = (unsigned char)sbox(1, x); 418 tmpx1 = (unsigned char)sbox(1, x);
475 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) 475 int twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
476 #endif 476 #endif
477 { 477 {
478 ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k; 478 ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
479 int r; 479 int r;
480 #if !defined(TWOFISH_SMALL) && !defined(__GNUC__) 480 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
481 ulong32 *S1, *S2, *S3, *S4; 481 ulong32 *S1, *S2, *S3, *S4;
482 #endif 482 #endif
483 483
484 LTC_ARGCHK(pt != NULL); 484 LTC_ARGCHK(pt != NULL);
485 LTC_ARGCHK(ct != NULL); 485 LTC_ARGCHK(ct != NULL);
486 LTC_ARGCHK(skey != NULL); 486 LTC_ARGCHK(skey != NULL);
487 487
488 #if !defined(TWOFISH_SMALL) && !defined(__GNUC__) 488 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
489 S1 = skey->twofish.S[0]; 489 S1 = skey->twofish.S[0];
490 S2 = skey->twofish.S[1]; 490 S2 = skey->twofish.S[1];
491 S3 = skey->twofish.S[2]; 491 S3 = skey->twofish.S[2];
492 S4 = skey->twofish.S[3]; 492 S4 = skey->twofish.S[3];
493 #endif 493 #endif
548 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) 548 int twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
549 #endif 549 #endif
550 { 550 {
551 ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k; 551 ulong32 a,b,c,d,ta,tb,tc,td,t1,t2, *k;
552 int r; 552 int r;
553 #if !defined(TWOFISH_SMALL) && !defined(__GNUC__) 553 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
554 ulong32 *S1, *S2, *S3, *S4; 554 ulong32 *S1, *S2, *S3, *S4;
555 #endif 555 #endif
556 556
557 LTC_ARGCHK(pt != NULL); 557 LTC_ARGCHK(pt != NULL);
558 LTC_ARGCHK(ct != NULL); 558 LTC_ARGCHK(ct != NULL);
559 LTC_ARGCHK(skey != NULL); 559 LTC_ARGCHK(skey != NULL);
560 560
561 #if !defined(TWOFISH_SMALL) && !defined(__GNUC__) 561 #if !defined(LTC_TWOFISH_SMALL) && !defined(__GNUC__)
562 S1 = skey->twofish.S[0]; 562 S1 = skey->twofish.S[0];
563 S2 = skey->twofish.S[1]; 563 S2 = skey->twofish.S[1];
564 S3 = skey->twofish.S[2]; 564 S3 = skey->twofish.S[2];
565 S4 = skey->twofish.S[3]; 565 S4 = skey->twofish.S[3];
566 #endif 566 #endif
712 #endif 712 #endif
713 713
714 714
715 715
716 716
717 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/twofish/twofish.c,v $ */ 717 /* $Source$ */
718 /* $Revision: 1.14 $ */ 718 /* $Revision$ */
719 /* $Date: 2006/12/04 21:34:03 $ */ 719 /* $Date$ */