5 #include "inspircd_config.h"
\r
10 /** The AES class is a utility class for use in modules and the core for encryption of data.
\r
12 class AES : public classbase
\r
15 enum { ECB=0, CBC=1, CFB=2 };
\r
18 enum { DEFAULT_BLOCK_SIZE=16 };
\r
19 enum { MAX_BLOCK_SIZE=32, MAX_ROUNDS=14, MAX_KC=8, MAX_BC=8 };
\r
21 static int Mul(int a, int b)
\r
23 return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0;
\r
26 /** Convenience method used in generating Transposition Boxes
\r
28 static int Mul4(int a, char b[])
\r
32 a = sm_log[a & 0xFF];
\r
33 int a0 = (b[0] != 0) ? sm_alog[(a + sm_log[b[0] & 0xFF]) % 255] & 0xFF : 0;
\r
34 int a1 = (b[1] != 0) ? sm_alog[(a + sm_log[b[1] & 0xFF]) % 255] & 0xFF : 0;
\r
35 int a2 = (b[2] != 0) ? sm_alog[(a + sm_log[b[2] & 0xFF]) % 255] & 0xFF : 0;
\r
36 int a3 = (b[3] != 0) ? sm_alog[(a + sm_log[b[3] & 0xFF]) % 255] & 0xFF : 0;
\r
37 return a0 << 24 | a1 << 16 | a2 << 8 | a3;
\r
45 /** Expand a user-supplied key material into a session key.
\r
47 * @param key The 128/192/256-bit user-key to use.
\r
48 * @param chain Initial chain block for CBC and CFB modes.
\r
49 * @param keylength 16, 24 or 32 bytes
\r
50 * @param blockSize The block size in bytes of this Rijndael (16, 24 or 32 bytes).
\r
52 void MakeKey(char const* key, char const* chain, int keylength=DEFAULT_BLOCK_SIZE, int blockSize=DEFAULT_BLOCK_SIZE);
\r
55 /** Auxiliary Function
\r
57 void Xor(char* buff, char const* chain)
\r
59 if(false==m_bKeyInit)
\r
61 for(int i=0; i<m_blockSize; i++)
\r
62 *(buff++) ^= *(chain++);
\r
65 /** Convenience method to encrypt exactly one block of plaintext, assuming Rijndael's default block size (128-bit).
\r
66 * @param in The plaintext
\r
67 * @param result The ciphertext generated from a plaintext using the key
\r
69 void DefEncryptBlock(char const* in, char* result);
\r
71 /** Convenience method to decrypt exactly one block of plaintext, assuming Rijndael's default block size (128-bit).
\r
72 * @param in The ciphertext.
\r
73 * @param result The plaintext generated from a ciphertext using the session key.
\r
75 void DefDecryptBlock(char const* in, char* result);
\r
78 /** Encrypt exactly one block of plaintext.
\r
79 * @param in The plaintext.
\r
80 * @param result The ciphertext generated from a plaintext using the key.
\r
82 void EncryptBlock(char const* in, char* result);
\r
84 /** Decrypt exactly one block of ciphertext.
\r
85 * @param in The ciphertext.
\r
86 * @param result The plaintext generated from a ciphertext using the session key.
\r
88 void DecryptBlock(char const* in, char* result);
\r
90 /** Encrypt multiple blocks of plaintext.
\r
91 * @param n Number of bytes to encrypt, must be a multiple of the keysize
\r
92 * @param in The plaintext to encrypt
\r
93 * @param result The output ciphertext
\r
94 * @param iMode Mode to use
\r
96 void Encrypt(char const* in, char* result, size_t n, int iMode=ECB);
\r
98 /** Decrypt multiple blocks of ciphertext.
\r
99 * @param n Number of bytes to decrypt, must be a multiple of the keysize
\r
100 * @param in The ciphertext to decrypt
\r
101 * @param result The output plaintext
\r
102 * @param iMode Mode to use
\r
104 void Decrypt(char const* in, char* result, size_t n, int iMode=ECB);
\r
110 if(false==m_bKeyInit)
\r
112 return m_keylength;
\r
119 if(false==m_bKeyInit)
\r
121 return m_blockSize;
\r
124 /** Get Number of Rounds
\r
128 if(false==m_bKeyInit)
\r
133 /** Reset the chain
\r
137 memcpy(m_chain, m_chain0, m_blockSize);
\r
143 static char const* sm_chain0;
\r
146 static const int sm_alog[256];
\r
147 static const int sm_log[256];
\r
148 static const char sm_S[256];
\r
149 static const char sm_Si[256];
\r
150 static const int sm_T1[256];
\r
151 static const int sm_T2[256];
\r
152 static const int sm_T3[256];
\r
153 static const int sm_T4[256];
\r
154 static const int sm_T5[256];
\r
155 static const int sm_T6[256];
\r
156 static const int sm_T7[256];
\r
157 static const int sm_T8[256];
\r
158 static const int sm_U1[256];
\r
159 static const int sm_U2[256];
\r
160 static const int sm_U3[256];
\r
161 static const int sm_U4[256];
\r
162 static const char sm_rcon[30];
\r
163 static const int sm_shifts[3][4][2];
\r
164 /** Key Initialization Flag
\r
167 /** Encryption (m_Ke) round key
\r
169 int m_Ke[MAX_ROUNDS+1][MAX_BC];
\r
170 /** Decryption (m_Kd) round key
\r
172 int m_Kd[MAX_ROUNDS+1][MAX_BC];
\r
179 /** Number of Rounds
\r
184 char m_chain0[MAX_BLOCK_SIZE];
\r
185 char m_chain[MAX_BLOCK_SIZE];
\r
186 /** Auxiliary private use buffers
\r
195 /** Convert from binary to base64
\r
196 * @param out Output
\r
198 * @param inlen Number of bytes in input buffer
\r
201 void to64frombits(unsigned char *out, const unsigned char *in, int inlen);
\r
202 /** Convert from base64 to binary
\r
205 * @maxlen Size of output buffer
\r
206 * @return Number of bytes actually converted
\r
208 int from64tobits(char *out, const char *in, int maxlen);
\r