]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/aes.h
EventHandler class, an abstraction for raw i/o
[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 #include "inspircd_config.h"\r
6 #include "base.h"\r
7 \r
8 using namespace std;\r
9 \r
10 /** The AES class is a utility class for use in modules and the core for encryption of data.\r
11  */\r
12 class AES : public classbase\r
13 {\r
14 public:\r
15         enum { ECB=0, CBC=1, CFB=2 };\r
16 \r
17 private:\r
18         enum { DEFAULT_BLOCK_SIZE=16 };\r
19         enum { MAX_BLOCK_SIZE=32, MAX_ROUNDS=14, MAX_KC=8, MAX_BC=8 };\r
20 \r
21         static int Mul(int a, int b)\r
22         {\r
23                 return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0;\r
24         }\r
25 \r
26         /** Convenience method used in generating Transposition Boxes\r
27          */\r
28         static int Mul4(int a, char b[])\r
29         {\r
30                 if(a == 0)\r
31                         return 0;\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
38         }\r
39 \r
40 public:\r
41         AES();\r
42 \r
43         virtual ~AES();\r
44 \r
45         /** Expand a user-supplied key material into a session key.\r
46          * \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
51          */\r
52         void MakeKey(char const* key, char const* chain, int keylength=DEFAULT_BLOCK_SIZE, int blockSize=DEFAULT_BLOCK_SIZE);\r
53 \r
54 private:\r
55         /** Auxiliary Function\r
56          */\r
57         void Xor(char* buff, char const* chain)\r
58         {\r
59                 if(false==m_bKeyInit)\r
60                         return;\r
61                 for(int i=0; i<m_blockSize; i++)\r
62                         *(buff++) ^= *(chain++);        \r
63         }\r
64 \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
68          */\r
69         void DefEncryptBlock(char const* in, char* result);\r
70 \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
74          */\r
75         void DefDecryptBlock(char const* in, char* result);\r
76 \r
77 public:\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
81          */\r
82         void EncryptBlock(char const* in, char* result);\r
83         \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
87          */\r
88         void DecryptBlock(char const* in, char* result);\r
89 \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
95          */\r
96         void Encrypt(char const* in, char* result, size_t n, int iMode=ECB);\r
97         \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
103          */\r
104         void Decrypt(char const* in, char* result, size_t n, int iMode=ECB);\r
105 \r
106         /** Get Key Length\r
107          */\r
108         int GetKeyLength()\r
109         {\r
110                 if(false==m_bKeyInit)\r
111                         return 0;\r
112                 return m_keylength;\r
113         }\r
114 \r
115         /** Get Block Size\r
116          */\r
117         int GetBlockSize()\r
118         {\r
119                 if(false==m_bKeyInit)\r
120                         return 0;\r
121                 return m_blockSize;\r
122         }\r
123         \r
124         /** Get Number of Rounds\r
125          */\r
126         int GetRounds()\r
127         {\r
128                 if(false==m_bKeyInit)\r
129                         return 0;\r
130                 return m_iROUNDS;\r
131         }\r
132 \r
133         /** Reset the chain\r
134          */\r
135         void ResetChain()\r
136         {\r
137                 memcpy(m_chain, m_chain0, m_blockSize);\r
138         }\r
139 \r
140 public:\r
141         /** Null chain\r
142          */\r
143         static char const* sm_chain0;\r
144 \r
145 private:\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
165          */\r
166         bool m_bKeyInit;\r
167         /** Encryption (m_Ke) round key\r
168          */\r
169         int m_Ke[MAX_ROUNDS+1][MAX_BC];\r
170         /** Decryption (m_Kd) round key\r
171          */\r
172         int m_Kd[MAX_ROUNDS+1][MAX_BC];\r
173         /** Key Length\r
174          */\r
175         int m_keylength;\r
176         /** Block Size\r
177          */\r
178         int     m_blockSize;\r
179         /** Number of Rounds\r
180          */\r
181         int m_iROUNDS;\r
182         /**Chain Block\r
183          */\r
184         char m_chain0[MAX_BLOCK_SIZE];\r
185         char m_chain[MAX_BLOCK_SIZE];\r
186         /** Auxiliary private use buffers\r
187          */\r
188         int tk[MAX_KC];\r
189         int a[MAX_BC];\r
190         int t[MAX_BC];\r
191 };\r
192 \r
193 #endif\r
194 \r
195 /** Convert from binary to base64\r
196  * @param out Output\r
197  * @param in Input\r
198  * @param inlen Number of bytes in input buffer\r
199  */\r
200 \r
201 void to64frombits(unsigned char *out, const unsigned char *in, int inlen);\r
202 /** Convert from base64 to binary\r
203  * @out Output\r
204  * @in Input\r
205  * @maxlen Size of output buffer\r
206  * @return Number of bytes actually converted\r
207  */\r
208 int from64tobits(char *out, const char *in, int maxlen);\r
209 \r