Pol  Revision:cb584c9
twofish.h
Go to the documentation of this file.
1 //
3 // crypt/twofish.h
4 //
6 
7 #ifndef __TWOFISH_H__
8 #define __TWOFISH_H__
9 
10 namespace Pol
11 {
12 namespace Crypt
13 {
14 // Structs for TwoFish
15 
16 typedef struct tagkeyInstance
17 {
18  unsigned char direction;
19  int keyLen;
20  int numRounds;
21  char keyMaterial[68];
22  unsigned int keySig;
23  unsigned int key32[8];
24  unsigned int sboxKeys[4];
25  unsigned int subKeys[40];
26 } KeyInstance;
27 
28 typedef struct tagcipherInstance
29 {
30  unsigned char mode;
31  unsigned char IV[16];
32  unsigned int cipherSig;
33  unsigned int iv32[4];
35 
36 class TwoFish
37 {
38  // Constructor / Destructor
39 public:
40  TwoFish();
41  ~TwoFish();
42 
43  // Member Functions
44 
45 public:
46  void Init( unsigned char* gseed );
47  void Decrypt( unsigned char* in, unsigned char* out, int len );
48 
49  unsigned char subData3[256];
50 
51 protected:
52  static unsigned int RS_MDS_Encode( unsigned int k0, unsigned int k1 );
53  static unsigned int F32( unsigned int x, unsigned int* k32, int keyLen );
54  static void ReKey( KeyInstance* key );
55  static void CipherInit( CipherInstance* cipher, unsigned char mode, char* IV );
56  void MakeKey( KeyInstance* key, unsigned char direction, int keyLen, char* keyMaterial );
57  static void BlockEncrypt( CipherInstance* cipher, KeyInstance* key, unsigned char* input,
58  int inputLen, unsigned char* outBuffer );
59 
62  unsigned char tabUsed[256];
63  unsigned int seed;
64  unsigned int dwIndex;
65  int tabEnable;
66  int pos;
67  int numRounds[4];
68 };
69 
70 // TWOFISH Definitions
71 
72 #define p8( N ) P8x8[P_##N]
73 #define RS_rem( x ) \
74  { \
75  unsigned char b = (unsigned char)( x >> 24 ); \
76  unsigned int g2 = ( ( b << 1 ) ^ ( ( b & 0x80 ) ? 0x14D : 0 ) ) & 0xFF; \
77  unsigned int g3 = ( ( b >> 1 ) & 0x7F ) ^ ( ( b & 1 ) ? 0x14D >> 1 : 0 ) ^ g2; \
78  x = ( x << 8 ) ^ ( g3 << 24 ) ^ ( g2 << 16 ) ^ ( g3 << 8 ) ^ b; \
79  }
80 
81 #define LFSR1( x ) ( ( ( x ) >> 1 ) ^ ( ( (x)&0x01 ) ? 0x169 / 2 : 0 ) )
82 #define LFSR2( x ) \
83  ( ( ( x ) >> 2 ) ^ ( ( (x)&0x02 ) ? 0x169 / 2 : 0 ) ^ ( ( (x)&0x01 ) ? 0x169 / 4 : 0 ) )
84 #define Mx_1( x ) ( (unsigned int)( x ) )
85 #define Mx_X( x ) ( (unsigned int)( ( x ) ^ LFSR2( x ) ) )
86 #define Mx_Y( x ) ( (unsigned int)( ( x ) ^ LFSR1( x ) ^ LFSR2( x ) ) )
87 #define M00 Mul_1
88 #define M01 Mul_Y
89 #define M02 Mul_X
90 #define M03 Mul_X
91 #define M10 Mul_X
92 #define M11 Mul_Y
93 #define M12 Mul_Y
94 #define M13 Mul_1
95 #define M20 Mul_Y
96 #define M21 Mul_X
97 #define M22 Mul_1
98 #define M23 Mul_Y
99 #define M30 Mul_Y
100 #define M31 Mul_1
101 #define M32 Mul_Y
102 #define M33 Mul_X
103 #define Mul_1 Mx_1
104 #define Mul_X Mx_X
105 #define Mul_Y Mx_Y
106 #define P_00 1
107 #define P_01 0
108 #define P_02 0
109 #define P_03 ( P_01 ^ 1 )
110 #define P_04 1
111 #define P_10 0
112 #define P_11 0
113 #define P_12 1
114 #define P_13 ( P_11 ^ 1 )
115 #define P_14 0
116 #define P_20 1
117 #define P_21 1
118 #define P_22 0
119 #define P_23 ( P_21 ^ 1 )
120 #define P_24 0
121 #define P_30 0
122 #define P_31 1
123 #define P_32 1
124 #define P_33 ( P_31 ^ 1 )
125 #define P_34 1
126 #define ROL( x, n ) ( ( ( x ) << ( (n)&0x1F ) ) | ( ( x ) >> ( 32 - ( (n)&0x1F ) ) ) )
127 #define ROR( x, n ) ( ( ( x ) >> ( (n)&0x1F ) ) | ( ( x ) << ( 32 - ( (n)&0x1F ) ) ) )
128 #define Bswap( x ) ( x )
129 #define _b( x, N ) ( ( (unsigned char*)&x )[( (N)&3 ) ^ 0] )
130 }
131 }
132 #endif //__TWOFISH_H__
KeyInstance ki
Definition: twofish.h:60
CipherInstance ci
Definition: twofish.h:61
struct Pol::Crypt::tagcipherInstance CipherInstance
struct Pol::Crypt::tagkeyInstance KeyInstance
unsigned int seed
Definition: twofish.h:63
unsigned int keySig
Definition: twofish.h:22
unsigned char direction
Definition: twofish.h:18
unsigned int key32[8]
Definition: twofish.h:23
unsigned int subKeys[40]
Definition: twofish.h:25
unsigned int sboxKeys[4]
Definition: twofish.h:24
Definition: berror.cpp:12
unsigned int dwIndex
Definition: twofish.h:64