ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Crypt-Twofish2/twofish.c
(Generate patch)

Comparing Crypt-Twofish2/twofish.c (file contents):
Revision 1.1 by root, Sat Sep 6 22:10:54 2003 UTC vs.
Revision 1.4 by root, Sun Aug 1 18:09:47 2021 UTC

5 Bruce Schneier, Counterpane Systems 5 Bruce Schneier, Counterpane Systems
6 Doug Whiting, Hi/fn 6 Doug Whiting, Hi/fn
7 John Kelsey, Counterpane Systems 7 John Kelsey, Counterpane Systems
8 Chris Hall, Counterpane Systems 8 Chris Hall, Counterpane Systems
9 David Wagner, UC Berkeley 9 David Wagner, UC Berkeley
10 10
11 Code Author: Doug Whiting, Hi/fn 11 Code Author: Doug Whiting, Hi/fn
12 12
13 Version 1.00 April 1998 13 Version 1.00 April 1998
14 14
15 Copyright 1998, Hi/fn and Counterpane Systems. All rights reserved. 15 Copyright 1998, Hi/fn and Counterpane Systems. All rights reserved.
16 16
17 Notes: 17 Notes:
18 * Optimized version 18 * Optimized version
19 * Tab size is set to 4 characters in this file 19 * Tab size is set to 4 characters in this file
20 20
21***************************************************************************/ 21***************************************************************************/
52* Constants/Macros/Tables 52* Constants/Macros/Tables
53-****************************************************************************/ 53-****************************************************************************/
54 54
55#define CONST /* help syntax from C++, NOP here */ 55#define CONST /* help syntax from C++, NOP here */
56 56
57CONST fullSbox MDStab; /* not actually const. Initialized ONE time */ 57static CONST fullSbox MDStab; /* not actually const. Initialized ONE time */
58int needToBuildMDS=1; /* is MDStab initialized yet? */ 58static int needToBuildMDS=1; /* is MDStab initialized yet? */
59 59
60#define BIG_TAB 0 60#define BIG_TAB 0
61 61
62#if BIG_TAB 62#if BIG_TAB
63BYTE bigTab[4][256][256]; /* pre-computed S-box */ 63static BYTE bigTab[4][256][256]; /* pre-computed S-box */
64#endif 64#endif
65 65
66/* number of rounds for various key sizes: 128, 192, 256 */ 66/* number of rounds for various key sizes: 128, 192, 256 */
67/* (ignored for now in optimized code!) */ 67/* (ignored for now in optimized code!) */
68CONST int numRounds[4]= {0,ROUNDS_128,ROUNDS_192,ROUNDS_256}; 68static CONST int numRounds[4]= {0,ROUNDS_128,ROUNDS_192,ROUNDS_256};
69 69
70#if REENTRANT 70#if REENTRANT
71#define _sBox_ key->sBox8x32 71#define _sBox_ key->sBox8x32
72#else 72#else
73static fullSbox _sBox_; /* permuted MDStab based on keys */ 73static fullSbox _sBox_; /* permuted MDStab based on keys */
103 MDStab[2][p8(21)[_sBox8_(2)[_b(x,R+2)]] ^ b2(SKEY0)] ^ \ 103 MDStab[2][p8(21)[_sBox8_(2)[_b(x,R+2)]] ^ b2(SKEY0)] ^ \
104 MDStab[3][p8(31)[_sBox8_(3)[_b(x,R+3)]] ^ b3(SKEY0)]) 104 MDStab[3][p8(31)[_sBox8_(3)[_b(x,R+3)]] ^ b3(SKEY0)])
105#define sbSet(N,i,J,v) { _sBox8_(N)[i+J] = v; } 105#define sbSet(N,i,J,v) { _sBox8_(N)[i+J] = v; }
106#define GetSboxKey DWORD SKEY0 = key->sboxKeys[0] /* local copy */ 106#define GetSboxKey DWORD SKEY0 = key->sboxKeys[0] /* local copy */
107/*----------------------------------------------------------------*/ 107/*----------------------------------------------------------------*/
108#elif defined(PART_KEY) 108#elif defined(PART_KEY)
109#define MOD_STRING "(Partial keying)" 109#define MOD_STRING "(Partial keying)"
110#define Fe32_(x,R)(MDStab[0][_sBox8_(0)[_b(x,R )]] ^ \ 110#define Fe32_(x,R)(MDStab[0][_sBox8_(0)[_b(x,R )]] ^ \
111 MDStab[1][_sBox8_(1)[_b(x,R+1)]] ^ \ 111 MDStab[1][_sBox8_(1)[_b(x,R+1)]] ^ \
112 MDStab[2][_sBox8_(2)[_b(x,R+2)]] ^ \ 112 MDStab[2][_sBox8_(2)[_b(x,R+2)]] ^ \
113 MDStab[3][_sBox8_(3)[_b(x,R+3)]]) 113 MDStab[3][_sBox8_(3)[_b(x,R+3)]])
114#define sbSet(N,i,J,v) { _sBox8_(N)[i+J] = v; } 114#define sbSet(N,i,J,v) { _sBox8_(N)[i+J] = v; }
115#define GetSboxKey 115#define GetSboxKey
116/*----------------------------------------------------------------*/ 116/*----------------------------------------------------------------*/
117#else /* default is FULL_KEY */ 117#else /* default is FULL_KEY */
118#ifndef FULL_KEY 118#ifndef FULL_KEY
119#define FULL_KEY 1 119#define FULL_KEY 1
120#endif 120#endif
133 in optimized assembly language. 133 in optimized assembly language.
134*/ 134*/
135#define Fe32_(x,R) (_sBox_[0][2*_b(x,R )] ^ _sBox_[0][2*_b(x,R+1)+1] ^ \ 135#define Fe32_(x,R) (_sBox_[0][2*_b(x,R )] ^ _sBox_[0][2*_b(x,R+1)+1] ^ \
136 _sBox_[2][2*_b(x,R+2)] ^ _sBox_[2][2*_b(x,R+3)+1]) 136 _sBox_[2][2*_b(x,R+2)] ^ _sBox_[2][2*_b(x,R+3)+1])
137 /* set a single S-box value, given the input byte */ 137 /* set a single S-box value, given the input byte */
138#define sbSet(N,i,J,v) { _sBox_[N&2][2*i+(N&1)+2*J]=MDStab[N][v]; } 138//#define sbSet(N,i,J,v) { _sBox_[N&2][2*i+(N&1)+2*J]=MDStab[N][v]; }
139#define sbSet(N,i,J,v) { *((DWORD *)_sBox_ + (N&2)*256 + 2*i + (N&1) + 2*J) = MDStab[N][v]; }
139#define GetSboxKey 140#define GetSboxKey
140#endif 141#endif
141 142
142/* macro(s) for debugging help */ 143/* macro(s) for debugging help */
143#define CHECK_TABLE 0 /* nonzero --> compare against "slow" table */ 144#define CHECK_TABLE 0 /* nonzero --> compare against "slow" table */
144#define VALIDATE_PARMS 0 /* disable for full speed */ 145#define VALIDATE_PARMS 0 /* disable for full speed */
145 146
146/* end of debug macros */ 147/* end of debug macros */
147 148
148#ifdef GetCodeSize 149#ifdef GetCodeSize
149extern DWORD Here(DWORD x); /* return caller's address! */ 150static extern DWORD Here(DWORD x); /* return caller's address! */
150DWORD TwofishCodeStart(void) { return Here(0); } 151static DWORD TwofishCodeStart(void) { return Here(0); }
151#endif 152#endif
152 153
153/* 154/*
154+***************************************************************************** 155+*****************************************************************************
155* 156*
157* 158*
158* Function: Handle table use checking 159* Function: Handle table use checking
159* 160*
160* Arguments: op = what to do (see TAB_* defns in AES.H) 161* Arguments: op = what to do (see TAB_* defns in AES.H)
161* 162*
162* Return: TRUE --> done (for TAB_QUERY) 163* Return: TRUE --> done (for TAB_QUERY)
163* 164*
164* Notes: This routine is for use in generating the tables KAT file. 165* Notes: This routine is for use in generating the tables KAT file.
165* For this optimized version, we don't actually track table usage, 166* For this optimized version, we don't actually track table usage,
166* since it would make the macros incredibly ugly. Instead we just 167* since it would make the macros incredibly ugly. Instead we just
167* run for a fixed number of queries and then say we're done. 168* run for a fixed number of queries and then say we're done.
168* 169*
169-****************************************************************************/ 170-****************************************************************************/
170int TableOp(int op) 171static int TableOp(int op)
171 { 172 {
172 static int queryCnt=0; 173 static int queryCnt=0;
173 174
174 switch (op) 175 switch (op)
175 { 176 {
203* 204*
204* Return: The output of the keyed permutation applied to x. 205* Return: The output of the keyed permutation applied to x.
205* 206*
206* Notes: 207* Notes:
207* This function is a keyed 32-bit permutation. It is the major building 208* This function is a keyed 32-bit permutation. It is the major building
208* block for the Twofish round function, including the four keyed 8x8 209* block for the Twofish round function, including the four keyed 8x8
209* permutations and the 4x4 MDS matrix multiply. This function is used 210* permutations and the 4x4 MDS matrix multiply. This function is used
210* both for generating round subkeys and within the round function on the 211* both for generating round subkeys and within the round function on the
211* block being encrypted. 212* block being encrypted.
212* 213*
213* This version is fairly slow and pedagogical, although a smartcard would 214* This version is fairly slow and pedagogical, although a smartcard would
214* probably perform the operation exactly this way in firmware. For 215* probably perform the operation exactly this way in firmware. For
215* ultimate performance, the entire operation can be completed with four 216* ultimate performance, the entire operation can be completed with four
216* lookups into four 256x32-bit tables, with three dword xors. 217* lookups into four 256x32-bit tables, with three dword xors.
217* 218*
218* The MDS matrix is defined in TABLE.H. To multiply by Mij, just use the 219* The MDS matrix is defined in TABLE.H. To multiply by Mij, just use the
219* macro Mij(x). 220* macro Mij(x).
220* 221*
221-****************************************************************************/ 222-****************************************************************************/
222DWORD f32(DWORD x,CONST DWORD *k32,int keyLen) 223static DWORD f32(DWORD x,CONST DWORD *k32,int keyLen)
223 { 224 {
224 BYTE b[4]; 225 BYTE b[4];
225 226
226 /* Run each byte thru 8x8 S-boxes, xoring with key byte at each stage. */ 227 /* Run each byte thru 8x8 S-boxes, xoring with key byte at each stage. */
227 /* Note that each byte goes through a different combination of S-boxes.*/ 228 /* Note that each byte goes through a different combination of S-boxes.*/
228 229
229 *((DWORD *)b) = Bswap(x); /* make b[0] = LSB, b[3] = MSB */ 230 *((DWORD *)b) = Bswap(x); /* make b[0] = LSB, b[3] = MSB */
230 switch (((keyLen + 63)/64) & 3) 231 switch (((keyLen + 63)/64) & 3)
275* the performance impact of this routine is imperceptible. The RS code 276* the performance impact of this routine is imperceptible. The RS code
276* chosen has "simple" coefficients to allow smartcard/hardware implementation 277* chosen has "simple" coefficients to allow smartcard/hardware implementation
277* without lookup tables. 278* without lookup tables.
278* 279*
279-****************************************************************************/ 280-****************************************************************************/
280DWORD RS_MDS_Encode(DWORD k0,DWORD k1) 281static DWORD RS_MDS_Encode(DWORD k0,DWORD k1)
281 { 282 {
282 int i,j; 283 int i,j;
283 DWORD r; 284 DWORD r;
284 285
285 for (i=r=0;i<2;i++) 286 for (i=r=0;i<2;i++)
286 { 287 {
287 r ^= (i) ? k0 : k1; /* merge in 32 more key bits */ 288 r ^= (i) ? k0 : k1; /* merge in 32 more key bits */
288 for (j=0;j<4;j++) /* shift one byte at a time */ 289 for (j=0;j<4;j++) /* shift one byte at a time */
289 RS_rem(r); 290 RS_rem(r);
290 } 291 }
291 return r; 292 return r;
292 } 293 }
293 294
294 295
306* Notes: 307* Notes:
307* Here we precompute all the fixed MDS table. This only needs to be done 308* Here we precompute all the fixed MDS table. This only needs to be done
308* one time at initialization, after which the table is "CONST". 309* one time at initialization, after which the table is "CONST".
309* 310*
310-****************************************************************************/ 311-****************************************************************************/
311void BuildMDS(void) 312static void BuildMDS(void)
312 { 313 {
313 int i; 314 int i;
314 DWORD d; 315 DWORD d;
315 BYTE m1[2],mX[2],mY[4]; 316 BYTE m1[2],mX[2],mY[4];
316 317
326 327
327#undef Mul_1 /* change what the pre-processor does with Mij */ 328#undef Mul_1 /* change what the pre-processor does with Mij */
328#undef Mul_X 329#undef Mul_X
329#undef Mul_Y 330#undef Mul_Y
330#define Mul_1 m1 /* It will now access m01[], m5B[], and mEF[] */ 331#define Mul_1 m1 /* It will now access m01[], m5B[], and mEF[] */
331#define Mul_X mX 332#define Mul_X mX
332#define Mul_Y mY 333#define Mul_Y mY
333 334
334#define SetMDS(N) \ 335#define SetMDS(N) \
335 b0(d) = M0##N[P_##N##0]; \ 336 b0(d) = M0##N[P_##N##0]; \
336 b1(d) = M1##N[P_##N##0]; \ 337 b1(d) = M1##N[P_##N##0]; \
347#undef Mul_X 348#undef Mul_X
348#undef Mul_Y 349#undef Mul_Y
349#define Mul_1 Mx_1 /* re-enable true multiply */ 350#define Mul_1 Mx_1 /* re-enable true multiply */
350#define Mul_X Mx_X 351#define Mul_X Mx_X
351#define Mul_Y Mx_Y 352#define Mul_Y Mx_Y
352 353
353#if BIG_TAB 354#if BIG_TAB
354 { 355 {
355 int j,k; 356 int j,k;
356 BYTE *q0,*q1; 357 BYTE *q0,*q1;
357 358
390* This optimization allows both blockEncrypt and blockDecrypt to use the same 391* This optimization allows both blockEncrypt and blockDecrypt to use the same
391* "fallthru" switch statement based on the number of rounds. 392* "fallthru" switch statement based on the number of rounds.
392* Note that key->numRounds must be even and >= 2 here. 393* Note that key->numRounds must be even and >= 2 here.
393* 394*
394-****************************************************************************/ 395-****************************************************************************/
395void ReverseRoundSubkeys(keyInstance *key,BYTE newDir) 396static void ReverseRoundSubkeys(keyInstance *key,BYTE newDir)
396 { 397 {
397 DWORD t0,t1; 398 DWORD t0,t1;
398 register DWORD *r0=key->subKeys+ROUND_SUBKEYS; 399 register DWORD *r0=key->subKeys+ROUND_SUBKEYS;
399 register DWORD *r1=r0 + 2*key->numRounds - 2; 400 register DWORD *r1=r0 + 2*key->numRounds - 2;
400 401
431* The penalty is nearly 50%! So we take the code size hit for inlining for 432* The penalty is nearly 50%! So we take the code size hit for inlining for
432* Borland, while Microsoft happily works with a call. 433* Borland, while Microsoft happily works with a call.
433* 434*
434-****************************************************************************/ 435-****************************************************************************/
435#if defined(__BORLANDC__) /* do it inline */ 436#if defined(__BORLANDC__) /* do it inline */
436#define Xor32(dst,src,i) { ((DWORD *)dst)[i] = ((DWORD *)src)[i] ^ tmpX; } 437#define Xor32(dst,src,i) { ((DWORD *)dst)[i] = ((DWORD *)src)[i] ^ tmpX; }
437#define Xor256(dst,src,b) \ 438#define Xor256(dst,src,b) \
438 { \ 439 { \
439 register DWORD tmpX=0x01010101u * b;\ 440 register DWORD tmpX=0x01010101u * b;\
440 for (i=0;i<64;i+=4) \ 441 for (i=0;i<64;i+=4) \
441 { Xor32(dst,src,i ); Xor32(dst,src,i+1); Xor32(dst,src,i+2); Xor32(dst,src,i+3); } \ 442 { Xor32(dst,src,i ); Xor32(dst,src,i+1); Xor32(dst,src,i+2); Xor32(dst,src,i+3); } \
442 } 443 }
443#else /* do it as a function call */ 444#else /* do it as a function call */
444void Xor256(void *dst,void *src,BYTE b) 445static void Xor256(void *dst,void *src,BYTE b)
445 { 446 {
446 register DWORD x=b*0x01010101u; /* replicate byte to all four bytes */ 447 register DWORD x=b*0x01010101u; /* replicate byte to all four bytes */
447 register DWORD *d=(DWORD *)dst; 448 register DWORD *d=(DWORD *)dst;
448 register DWORD *s=(DWORD *)src; 449 register DWORD *s=(DWORD *)src;
449#define X_8(N) { d[N]=s[N] ^ x; d[N+1]=s[N+1] ^ x; } 450#define X_8(N) { d[N]=s[N] ^ x; d[N+1]=s[N+1] ^ x; }
466* 467*
467* Return: TRUE on success 468* Return: TRUE on success
468* 469*
469* Notes: 470* Notes:
470* Here we precompute all the round subkeys, although that is not actually 471* Here we precompute all the round subkeys, although that is not actually
471* required. For example, on a smartcard, the round subkeys can 472* required. For example, on a smartcard, the round subkeys can
472* be generated on-the-fly using f32() 473* be generated on-the-fly using f32()
473* 474*
474-****************************************************************************/ 475-****************************************************************************/
475int reKey(keyInstance *key) 476static int reKey(keyInstance *key)
476 { 477 {
477 int i,j,k64Cnt,keyLen; 478 int i,j,k64Cnt,keyLen;
478 int subkeyCnt; 479 int subkeyCnt;
479 DWORD A=0,B=0,q; 480 DWORD A=0,B=0,q;
480 DWORD sKey[MAX_KEY_BITS/64],k32e[MAX_KEY_BITS/64],k32o[MAX_KEY_BITS/64]; 481 DWORD sKey[MAX_KEY_BITS/64],k32e[MAX_KEY_BITS/64],k32o[MAX_KEY_BITS/64];
545 reKey_86(key); 546 reKey_86(key);
546 } 547 }
547else 548else
548#endif 549#endif
549 { 550 {
550 for (i=q=0;i<subkeyCnt/2;i++,q+=SK_STEP) 551 for (i=q=0;i<subkeyCnt/2;i++,q+=SK_STEP)
551 { /* compute round subkeys for PHT */ 552 { /* compute round subkeys for PHT */
552 F32(A,q ,k32e); /* A uses even key dwords */ 553 F32(A,q ,k32e); /* A uses even key dwords */
553 F32(B,q+SK_BUMP,k32o); /* B uses odd key dwords */ 554 F32(B,q+SK_BUMP,k32o); /* B uses odd key dwords */
554 B = ROL(B,8); 555 B = ROL(B,8);
555 key->subKeys[2*i ] = A+B; /* combine with a PHT */ 556 key->subKeys[2*i ] = A+B; /* combine with a PHT */
639 assert(f32(q,key->sboxKeys,keyLen) == Fe32_(q,0)); 640 assert(f32(q,key->sboxKeys,keyLen) == Fe32_(q,0));
640 #endif 641 #endif
641 } 642 }
642#endif /* CHECK_TABLE */ 643#endif /* CHECK_TABLE */
643 644
644 if (key->direction == DIR_ENCRYPT) 645 if (key->direction == DIR_ENCRYPT)
645 ReverseRoundSubkeys(key,DIR_ENCRYPT); /* reverse the round subkey order */ 646 ReverseRoundSubkeys(key,DIR_ENCRYPT); /* reverse the round subkey order */
646 647
647 return TRUE; 648 return TRUE;
648 } 649 }
649/* 650/*
662* else error code (e.g., BAD_KEY_DIR) 663* else error code (e.g., BAD_KEY_DIR)
663* 664*
664* Notes: This parses the key bits from keyMaterial. Zeroes out unused key bits 665* Notes: This parses the key bits from keyMaterial. Zeroes out unused key bits
665* 666*
666-****************************************************************************/ 667-****************************************************************************/
667int makeKey(keyInstance *key, BYTE direction, int keyLen,CONST char *keyMaterial) 668static int makeKey(keyInstance *key, BYTE direction, int keyLen,CONST char *keyMaterial)
668 { 669 {
669 int i; 670 int i;
670 671
671#if VALIDATE_PARMS /* first, sanity check on parameters */ 672#if VALIDATE_PARMS /* first, sanity check on parameters */
672 if (key == NULL) 673 if (key == NULL)
673 return BAD_KEY_INSTANCE;/* must have a keyInstance to initialize */ 674 return BAD_KEY_INSTANCE;/* must have a keyInstance to initialize */
674 if ((direction != DIR_ENCRYPT) && (direction != DIR_DECRYPT)) 675 if ((direction != DIR_ENCRYPT) && (direction != DIR_DECRYPT))
675 return BAD_KEY_DIR; /* must have valid direction */ 676 return BAD_KEY_DIR; /* must have valid direction */
676 if ((keyLen > MAX_KEY_BITS) || (keyLen < 8) || (keyLen & 0x3F)) 677 if ((keyLen > MAX_KEY_BITS) || (keyLen < 8) || (keyLen & 0x3F))
677 return BAD_KEY_MAT; /* length must be valid */ 678 return BAD_KEY_MAT; /* length must be valid */
687 key->numRounds = numRounds[(keyLen-1)/64]; 688 key->numRounds = numRounds[(keyLen-1)/64];
688 memset(key->key32,0,sizeof(key->key32)); /* zero unused bits */ 689 memset(key->key32,0,sizeof(key->key32)); /* zero unused bits */
689 690
690 if (keyMaterial == NULL) 691 if (keyMaterial == NULL)
691 return TRUE; /* allow a "dummy" call */ 692 return TRUE; /* allow a "dummy" call */
692 693
693 for (i=0;i<keyLen/32;i++) /* make byte-oriented copy for CFB1 */ 694 for (i=0;i<keyLen/32;i++) /* make byte-oriented copy for CFB1 */
694 key->key32[i] = (((unsigned char *)keyMaterial)[i*4+0] << 0) 695 key->key32[i] = (((unsigned char *)keyMaterial)[i*4+0] << 0)
695 | (((unsigned char *)keyMaterial)[i*4+1] << 8) 696 | (((unsigned char *)keyMaterial)[i*4+1] << 8)
696 | (((unsigned char *)keyMaterial)[i*4+2] << 16) 697 | (((unsigned char *)keyMaterial)[i*4+2] << 16)
697 | (((unsigned char *)keyMaterial)[i*4+3] << 24); 698 | (((unsigned char *)keyMaterial)[i*4+3] << 24);
713* 714*
714* Return: TRUE on success 715* Return: TRUE on success
715* else error code (e.g., BAD_CIPHER_MODE) 716* else error code (e.g., BAD_CIPHER_MODE)
716* 717*
717-****************************************************************************/ 718-****************************************************************************/
718int cipherInit(cipherInstance *cipher, BYTE mode,CONST char *IV) 719static int cipherInit(cipherInstance *cipher, BYTE mode,CONST char *IV)
719 { 720 {
720 int i; 721 int i;
721#if VALIDATE_PARMS /* first, sanity check on parameters */ 722#if VALIDATE_PARMS /* first, sanity check on parameters */
722 if (cipher == NULL) 723 if (cipher == NULL)
723 return BAD_PARAMS; /* must have a cipherInstance to initialize */ 724 return BAD_PARAMS; /* must have a cipherInstance to initialize */
724 if ((mode != MODE_ECB) && (mode != MODE_CBC) && (mode != MODE_CFB1)) 725 if ((mode != MODE_ECB) && (mode != MODE_CBC) && (mode != MODE_CFB1))
725 return BAD_CIPHER_MODE; /* must have valid cipher mode */ 726 return BAD_CIPHER_MODE; /* must have valid cipher mode */
726 cipher->cipherSig = VALID_SIG; 727 cipher->cipherSig = VALID_SIG;
727 #if ALIGN32 728 #if ALIGN32
758* Return: # bits ciphered (>= 0) 759* Return: # bits ciphered (>= 0)
759* else error code (e.g., BAD_CIPHER_STATE, BAD_KEY_MATERIAL) 760* else error code (e.g., BAD_CIPHER_STATE, BAD_KEY_MATERIAL)
760* 761*
761* Notes: The only supported block size for ECB/CBC modes is BLOCK_SIZE bits. 762* Notes: The only supported block size for ECB/CBC modes is BLOCK_SIZE bits.
762* If inputLen is not a multiple of BLOCK_SIZE bits in those modes, 763* If inputLen is not a multiple of BLOCK_SIZE bits in those modes,
763* an error BAD_INPUT_LEN is returned. In CFB1 mode, all block 764* an error BAD_INPUT_LEN is returned. In CFB1 mode, all block
764* sizes can be supported. 765* sizes can be supported.
765* 766*
766-****************************************************************************/ 767-****************************************************************************/
767int blockEncrypt(cipherInstance *cipher, keyInstance *key,CONST BYTE *input, 768static int blockEncrypt(cipherInstance *cipher, keyInstance *key,CONST BYTE *input,
768 int inputLen, BYTE *outBuffer) 769 int inputLen, BYTE *outBuffer)
769 { 770 {
770 int i,n; /* loop counters */ 771 int i,n; /* loop counters */
771 DWORD x[BLOCK_SIZE/32]; /* block being encrypted */ 772 DWORD x[BLOCK_SIZE/32]; /* block being encrypted */
772 DWORD t0,t1; /* temp variables */ 773 DWORD t0,t1; /* temp variables */
824#ifdef USE_ASM 825#ifdef USE_ASM
825 if ((useAsm & 1) && (inputLen)) 826 if ((useAsm & 1) && (inputLen))
826 #ifdef COMPILE_KEY 827 #ifdef COMPILE_KEY
827 if (key->keySig == VALID_SIG) 828 if (key->keySig == VALID_SIG)
828 return ((CipherProc *)(key->encryptFuncPtr))(cipher,key,input,inputLen,outBuffer); 829 return ((CipherProc *)(key->encryptFuncPtr))(cipher,key,input,inputLen,outBuffer);
829 #else 830 #else
830 return (*blockEncrypt_86)(cipher,key,input,inputLen,outBuffer); 831 return (*blockEncrypt_86)(cipher,key,input,inputLen,outBuffer);
831 #endif 832 #endif
832#endif 833#endif
833 /* make local copy of subkeys for speed */ 834 /* make local copy of subkeys for speed */
834 memcpy(sk,key->subKeys,sizeof(DWORD)*(ROUND_SUBKEYS+2*rounds)); 835 memcpy(sk,key->subKeys,sizeof(DWORD)*(ROUND_SUBKEYS+2*rounds));
915* Return: # bits ciphered (>= 0) 916* Return: # bits ciphered (>= 0)
916* else error code (e.g., BAD_CIPHER_STATE, BAD_KEY_MATERIAL) 917* else error code (e.g., BAD_CIPHER_STATE, BAD_KEY_MATERIAL)
917* 918*
918* Notes: The only supported block size for ECB/CBC modes is BLOCK_SIZE bits. 919* Notes: The only supported block size for ECB/CBC modes is BLOCK_SIZE bits.
919* If inputLen is not a multiple of BLOCK_SIZE bits in those modes, 920* If inputLen is not a multiple of BLOCK_SIZE bits in those modes,
920* an error BAD_INPUT_LEN is returned. In CFB1 mode, all block 921* an error BAD_INPUT_LEN is returned. In CFB1 mode, all block
921* sizes can be supported. 922* sizes can be supported.
922* 923*
923-****************************************************************************/ 924-****************************************************************************/
924int blockDecrypt(cipherInstance *cipher, keyInstance *key,CONST BYTE *input, 925static int blockDecrypt(cipherInstance *cipher, keyInstance *key,CONST BYTE *input,
925 int inputLen, BYTE *outBuffer) 926 int inputLen, BYTE *outBuffer)
926 { 927 {
927 int i,n; /* loop counters */ 928 int i,n; /* loop counters */
928 DWORD x[BLOCK_SIZE/32]; /* block being encrypted */ 929 DWORD x[BLOCK_SIZE/32]; /* block being encrypted */
929 DWORD t0,t1; /* temp variables */ 930 DWORD t0,t1; /* temp variables */
981#ifdef USE_ASM 982#ifdef USE_ASM
982 if ((useAsm & 2) && (inputLen)) 983 if ((useAsm & 2) && (inputLen))
983 #ifdef COMPILE_KEY 984 #ifdef COMPILE_KEY
984 if (key->keySig == VALID_SIG) 985 if (key->keySig == VALID_SIG)
985 return ((CipherProc *)(key->decryptFuncPtr))(cipher,key,input,inputLen,outBuffer); 986 return ((CipherProc *)(key->decryptFuncPtr))(cipher,key,input,inputLen,outBuffer);
986 #else 987 #else
987 return (*blockDecrypt_86)(cipher,key,input,inputLen,outBuffer); 988 return (*blockDecrypt_86)(cipher,key,input,inputLen,outBuffer);
988 #endif 989 #endif
989#endif 990#endif
990 /* make local copy of subkeys for speed */ 991 /* make local copy of subkeys for speed */
991 memcpy(sk,key->subKeys,sizeof(DWORD)*(ROUND_SUBKEYS+2*rounds)); 992 memcpy(sk,key->subKeys,sizeof(DWORD)*(ROUND_SUBKEYS+2*rounds));
1062 1063
1063 return inputLen; 1064 return inputLen;
1064 } 1065 }
1065 1066
1066#ifdef GetCodeSize 1067#ifdef GetCodeSize
1067DWORD TwofishCodeSize(void) 1068static DWORD TwofishCodeSize(void)
1068 { 1069 {
1069 DWORD x= Here(0); 1070 DWORD x= Here(0);
1070#ifdef USE_ASM 1071#ifdef USE_ASM
1071 if (useAsm & 3) 1072 if (useAsm & 3)
1072 return TwofishAsmCodeSize(); 1073 return TwofishAsmCodeSize();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines