#ifndef __AES_H__ #define __AES_H__ #include #include "inspircd_config.h" #include "base.h" using namespace std; /** The AES class is a utility class for use in modules and the core for encryption of data. */ class AES : public classbase { public: enum { ECB=0, CBC=1, CFB=2 }; private: enum { DEFAULT_BLOCK_SIZE=16 }; enum { MAX_BLOCK_SIZE=32, MAX_ROUNDS=14, MAX_KC=8, MAX_BC=8 }; static int Mul(int a, int b) { return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0; } /** Convenience method used in generating Transposition Boxes */ static int Mul4(int a, char b[]) { if(a == 0) return 0; a = sm_log[a & 0xFF]; int a0 = (b[0] != 0) ? sm_alog[(a + sm_log[b[0] & 0xFF]) % 255] & 0xFF : 0; int a1 = (b[1] != 0) ? sm_alog[(a + sm_log[b[1] & 0xFF]) % 255] & 0xFF : 0; int a2 = (b[2] != 0) ? sm_alog[(a + sm_log[b[2] & 0xFF]) % 255] & 0xFF : 0; int a3 = (b[3] != 0) ? sm_alog[(a + sm_log[b[3] & 0xFF]) % 255] & 0xFF : 0; return a0 << 24 | a1 << 16 | a2 << 8 | a3; } public: AES(); virtual ~AES(); /** Expand a user-supplied key material into a session key. * * @param key The 128/192/256-bit user-key to use. * @param chain Initial chain block for CBC and CFB modes. * @param keylength 16, 24 or 32 bytes * @param blockSize The block size in bytes of this Rijndael (16, 24 or 32 bytes). */ void MakeKey(char const* key, char const* chain, int keylength=DEFAULT_BLOCK_SIZE, int blockSize=DEFAULT_BLOCK_SIZE); private: /** Auxiliary Function */ void Xor(char* buff, char const* chain) { if(false==m_bKeyInit) return; for(int i=0; i