]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/aes.h
Alter SetModeParam to take const char* to save on casts, notice a load of modules...
[user/henk/code/inspircd.git] / include / aes.h
1 #ifndef __AES_H__\r
2 #define __AES_H__\r
3 \r
4 #include <cstring>\r
5 \r
6 using namespace std;\r
7 \r
8 /** The AES class is a utility class for use in modules and the core for encryption of data.\r
9  */\r
10 class AES\r
11 {\r
12 public:\r
13         enum { ECB=0, CBC=1, CFB=2 };\r
14 \r
15 private:\r
16         enum { DEFAULT_BLOCK_SIZE=16 };\r
17         enum { MAX_BLOCK_SIZE=32, MAX_ROUNDS=14, MAX_KC=8, MAX_BC=8 };\r
18 \r
19         static int Mul(int a, int b)\r
20         {\r
21                 return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0;\r
22         }\r
23 \r
24         /** Convenience method used in generating Transposition Boxes\r
25          */\r
26         static int Mul4(int a, char b[])\r
27         {\r
28                 if(a == 0)\r
29                         return 0;\r
30                 a = sm_log[a & 0xFF];\r
31                 int a0 = (b[0] != 0) ? sm_alog[(a + sm_log[b[0] & 0xFF]) % 255] & 0xFF : 0;\r
32                 int a1 = (b[1] != 0) ? sm_alog[(a + sm_log[b[1] & 0xFF]) % 255] & 0xFF : 0;\r
33                 int a2 = (b[2] != 0) ? sm_alog[(a + sm_log[b[2] & 0xFF]) % 255] & 0xFF : 0;\r
34                 int a3 = (b[3] != 0) ? sm_alog[(a + sm_log[b[3] & 0xFF]) % 255] & 0xFF : 0;\r
35                 return a0 << 24 | a1 << 16 | a2 << 8 | a3;\r
36         }\r
37 \r
38 public:\r
39         AES();\r
40 \r
41         virtual ~AES();\r
42 \r
43         /** Expand a user-supplied key material into a session key.\r
44          * \r
45          * @param key The 128/192/256-bit user-key to use.\r
46          * @param chain Initial chain block for CBC and CFB modes.\r
47          * @param keylength 16, 24 or 32 bytes\r
48          * @param blockSize The block size in bytes of this Rijndael (16, 24 or 32 bytes).\r
49          */\r
50         void MakeKey(char const* key, char const* chain, int keylength=DEFAULT_BLOCK_SIZE, int blockSize=DEFAULT_BLOCK_SIZE);\r
51 \r
52 private:\r
53         /** Auxiliary Function\r
54          */\r
55         void Xor(char* buff, char const* chain)\r
56         {\r
57                 if(false==m_bKeyInit)\r
58                         return;\r
59                 for(int i=0; i<m_blockSize; i++)\r
60                         *(buff++) ^= *(chain++);        \r
61         }\r
62 \r
63         /** Convenience method to encrypt exactly one block of plaintext, assuming Rijndael's default block size (128-bit).\r
64          * @param in The plaintext\r
65          * @param result The ciphertext generated from a plaintext using the key\r
66          */\r
67         void DefEncryptBlock(char const* in, char* result);\r
68 \r
69         /** Convenience method to decrypt exactly one block of plaintext, assuming Rijndael's default block size (128-bit).\r
70          * @param in The ciphertext.\r
71          * @param result The plaintext generated from a ciphertext using the session key.\r
72          */\r
73         void DefDecryptBlock(char const* in, char* result);\r
74 \r
75 public:\r
76         /** Encrypt exactly one block of plaintext.\r
77          * @param in The plaintext.\r
78          * @param result The ciphertext generated from a plaintext using the key.\r
79          */\r
80         void EncryptBlock(char const* in, char* result);\r
81         \r
82         /** Decrypt exactly one block of ciphertext.\r
83          * @param in The ciphertext.\r
84          * @param result The plaintext generated from a ciphertext using the session key.\r
85          */\r
86         void DecryptBlock(char const* in, char* result);\r
87 \r
88         /** Encrypt multiple blocks of plaintext.\r
89          * @param n Number of bytes to encrypt, must be a multiple of the keysize\r
90          * @param in The plaintext to encrypt\r
91          * @param result The output ciphertext\r
92          * @param iMode Mode to use\r
93          */\r
94         void Encrypt(char const* in, char* result, size_t n, int iMode=ECB);\r
95         \r
96         /** Decrypt multiple blocks of ciphertext.\r
97          * @param n Number of bytes to decrypt, must be a multiple of the keysize\r
98          * @param in The ciphertext to decrypt\r
99          * @param result The output plaintext\r
100          * @param iMode Mode to use\r
101          */\r
102         void Decrypt(char const* in, char* result, size_t n, int iMode=ECB);\r
103 \r
104         /** Get Key Length\r
105          */\r
106         int GetKeyLength()\r
107         {\r
108                 if(false==m_bKeyInit)\r
109                         return 0;\r
110                 return m_keylength;\r
111         }\r
112 \r
113         /** Get Block Size\r
114          */\r
115         int GetBlockSize()\r
116         {\r
117                 if(false==m_bKeyInit)\r
118                         return 0;\r
119                 return m_blockSize;\r
120         }\r
121         \r
122         /** Get Number of Rounds\r
123          */\r
124         int GetRounds()\r
125         {\r
126                 if(false==m_bKeyInit)\r
127                         return 0;\r
128                 return m_iROUNDS;\r
129         }\r
130 \r
131         /** Reset the chain\r
132          */\r
133         void ResetChain()\r
134         {\r
135                 memcpy(m_chain, m_chain0, m_blockSize);\r
136         }\r
137 \r
138 public:\r
139         /** Null chain\r
140          */\r
141         static char const* sm_chain0;\r
142 \r
143 private:\r
144         static const int sm_alog[256];\r
145         static const int sm_log[256];\r
146         static const char sm_S[256];\r
147         static const char sm_Si[256];\r
148         static const int sm_T1[256];\r
149         static const int sm_T2[256];\r
150         static const int sm_T3[256];\r
151         static const int sm_T4[256];\r
152         static const int sm_T5[256];\r
153         static const int sm_T6[256];\r
154         static const int sm_T7[256];\r
155         static const int sm_T8[256];\r
156         static const int sm_U1[256];\r
157         static const int sm_U2[256];\r
158         static const int sm_U3[256];\r
159         static const int sm_U4[256];\r
160         static const char sm_rcon[30];\r
161         static const int sm_shifts[3][4][2];\r
162         /** Key Initialization Flag\r
163          */\r
164         bool m_bKeyInit;\r
165         /** Encryption (m_Ke) round key\r
166          */\r
167         int m_Ke[MAX_ROUNDS+1][MAX_BC];\r
168         /** Decryption (m_Kd) round key\r
169          */\r
170         int m_Kd[MAX_ROUNDS+1][MAX_BC];\r
171         /** Key Length\r
172          */\r
173         int m_keylength;\r
174         /** Block Size\r
175          */\r
176         int     m_blockSize;\r
177         /** Number of Rounds\r
178          */\r
179         int m_iROUNDS;\r
180         /**Chain Block\r
181          */\r
182         char m_chain0[MAX_BLOCK_SIZE];\r
183         char m_chain[MAX_BLOCK_SIZE];\r
184         /** Auxiliary private use buffers\r
185          */\r
186         int tk[MAX_KC];\r
187         int a[MAX_BC];\r
188         int t[MAX_BC];\r
189 };\r
190 \r
191 #endif\r
192 \r
193 /** Convert from binary to base64\r
194  * @param out Output\r
195  * @param in Input\r
196  * @param inlen Number of bytes in input buffer\r
197  */\r
198 \r
199 void to64frombits(unsigned char *out, const unsigned char *in, int inlen);\r
200 /** Convert from base64 to binary\r
201  * @out Output\r
202  * @in Input\r
203  * @maxlen Size of output buffer\r
204  * @return Number of bytes actually converted\r
205  */\r
206 int from64tobits(char *out, const char *in, int maxlen);\r
207 \r