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