From 58b919244afecc02ea141993a9d22e9e3ff4d5db Mon Sep 17 00:00:00 2001 From: brain Date: Mon, 19 Dec 2005 13:38:14 +0000 Subject: Added AES stuff git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@2567 e03df62e-2008-0410-955e-edbf42e46eb7 --- include/aes.h | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 include/aes.h (limited to 'include/aes.h') diff --git a/include/aes.h b/include/aes.h new file mode 100644 index 000000000..66e68cac9 --- /dev/null +++ b/include/aes.h @@ -0,0 +1,163 @@ +#ifndef __AES_H__ +#define __AES_H__ + +#include + +using namespace std; + +class AES +{ +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: + //CONSTRUCTOR + AES(); + + //DESTRUCTOR + virtual ~AES(); + + //Expand a user-supplied key material into a session key. + // key - The 128/192/256-bit user-key to use. + // chain - initial chain block for CBC and CFB modes. + // keylength - 16, 24 or 32 bytes + // 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