11 enum { ECB=0, CBC=1, CFB=2 };
\r
14 enum { DEFAULT_BLOCK_SIZE=16 };
\r
15 enum { MAX_BLOCK_SIZE=32, MAX_ROUNDS=14, MAX_KC=8, MAX_BC=8 };
\r
17 static int Mul(int a, int b)
\r
19 return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0;
\r
22 //Convenience method used in generating Transposition Boxes
\r
23 static int Mul4(int a, char b[])
\r
27 a = sm_log[a & 0xFF];
\r
28 int a0 = (b[0] != 0) ? sm_alog[(a + sm_log[b[0] & 0xFF]) % 255] & 0xFF : 0;
\r
29 int a1 = (b[1] != 0) ? sm_alog[(a + sm_log[b[1] & 0xFF]) % 255] & 0xFF : 0;
\r
30 int a2 = (b[2] != 0) ? sm_alog[(a + sm_log[b[2] & 0xFF]) % 255] & 0xFF : 0;
\r
31 int a3 = (b[3] != 0) ? sm_alog[(a + sm_log[b[3] & 0xFF]) % 255] & 0xFF : 0;
\r
32 return a0 << 24 | a1 << 16 | a2 << 8 | a3;
\r
42 //Expand a user-supplied key material into a session key.
\r
43 // key - The 128/192/256-bit user-key to use.
\r
44 // chain - initial chain block for CBC and CFB modes.
\r
45 // keylength - 16, 24 or 32 bytes
\r
46 // blockSize - The block size in bytes of this Rijndael (16, 24 or 32 bytes).
\r
47 void MakeKey(char const* key, char const* chain, int keylength=DEFAULT_BLOCK_SIZE, int blockSize=DEFAULT_BLOCK_SIZE);
\r
50 //Auxiliary Function
\r
51 void Xor(char* buff, char const* chain)
\r
53 if(false==m_bKeyInit)
\r
55 for(int i=0; i<m_blockSize; i++)
\r
56 *(buff++) ^= *(chain++);
\r
59 //Convenience method to encrypt exactly one block of plaintext, assuming
\r
60 //Rijndael's default block size (128-bit).
\r
61 // in - The plaintext
\r
62 // result - The ciphertext generated from a plaintext using the key
\r
63 void DefEncryptBlock(char const* in, char* result);
\r
65 //Convenience method to decrypt exactly one block of plaintext, assuming
\r
66 //Rijndael's default block size (128-bit).
\r
67 // in - The ciphertext.
\r
68 // result - The plaintext generated from a ciphertext using the session key.
\r
69 void DefDecryptBlock(char const* in, char* result);
\r
72 //Encrypt exactly one block of plaintext.
\r
73 // in - The plaintext.
\r
74 // result - The ciphertext generated from a plaintext using the key.
\r
75 void EncryptBlock(char const* in, char* result);
\r
77 //Decrypt exactly one block of ciphertext.
\r
78 // in - The ciphertext.
\r
79 // result - The plaintext generated from a ciphertext using the session key.
\r
80 void DecryptBlock(char const* in, char* result);
\r
82 void Encrypt(char const* in, char* result, size_t n, int iMode=ECB);
\r
84 void Decrypt(char const* in, char* result, size_t n, int iMode=ECB);
\r
89 if(false==m_bKeyInit)
\r
97 if(false==m_bKeyInit)
\r
105 if(false==m_bKeyInit)
\r
112 memcpy(m_chain, m_chain0, m_blockSize);
\r
117 static char const* sm_chain0;
\r
120 static const int sm_alog[256];
\r
121 static const int sm_log[256];
\r
122 static const char sm_S[256];
\r
123 static const char sm_Si[256];
\r
124 static const int sm_T1[256];
\r
125 static const int sm_T2[256];
\r
126 static const int sm_T3[256];
\r
127 static const int sm_T4[256];
\r
128 static const int sm_T5[256];
\r
129 static const int sm_T6[256];
\r
130 static const int sm_T7[256];
\r
131 static const int sm_T8[256];
\r
132 static const int sm_U1[256];
\r
133 static const int sm_U2[256];
\r
134 static const int sm_U3[256];
\r
135 static const int sm_U4[256];
\r
136 static const char sm_rcon[30];
\r
137 static const int sm_shifts[3][4][2];
\r
139 static char const* sm_szErrorMsg1;
\r
140 static char const* sm_szErrorMsg2;
\r
141 //Key Initialization Flag
\r
143 //Encryption (m_Ke) round key
\r
144 int m_Ke[MAX_ROUNDS+1][MAX_BC];
\r
145 //Decryption (m_Kd) round key
\r
146 int m_Kd[MAX_ROUNDS+1][MAX_BC];
\r
154 char m_chain0[MAX_BLOCK_SIZE];
\r
155 char m_chain[MAX_BLOCK_SIZE];
\r
156 //Auxiliary private use buffers
\r
162 #endif // __RIJNDAEL_H__
\r
164 void to64frombits(unsigned char *out, const unsigned char *in, int inlen);
\r
165 int from64tobits(char *out, const char *in, int maxlen);
\r