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