00001 #ifndef __AES_H__ 00002 #define __AES_H__ 00003 00004 #include <cstring> 00005 00006 using namespace std; 00007 00010 class AES 00011 { 00012 public: 00013 enum { ECB=0, CBC=1, CFB=2 }; 00014 00015 private: 00016 enum { DEFAULT_BLOCK_SIZE=16 }; 00017 enum { MAX_BLOCK_SIZE=32, MAX_ROUNDS=14, MAX_KC=8, MAX_BC=8 }; 00018 00019 static int Mul(int a, int b) 00020 { 00021 return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0; 00022 } 00023 00026 static int Mul4(int a, char b[]) 00027 { 00028 if(a == 0) 00029 return 0; 00030 a = sm_log[a & 0xFF]; 00031 int a0 = (b[0] != 0) ? sm_alog[(a + sm_log[b[0] & 0xFF]) % 255] & 0xFF : 0; 00032 int a1 = (b[1] != 0) ? sm_alog[(a + sm_log[b[1] & 0xFF]) % 255] & 0xFF : 0; 00033 int a2 = (b[2] != 0) ? sm_alog[(a + sm_log[b[2] & 0xFF]) % 255] & 0xFF : 0; 00034 int a3 = (b[3] != 0) ? sm_alog[(a + sm_log[b[3] & 0xFF]) % 255] & 0xFF : 0; 00035 return a0 << 24 | a1 << 16 | a2 << 8 | a3; 00036 } 00037 00038 public: 00039 AES(); 00040 00041 virtual ~AES(); 00042 00050 void MakeKey(char const* key, char const* chain, int keylength=DEFAULT_BLOCK_SIZE, int blockSize=DEFAULT_BLOCK_SIZE); 00051 00052 private: 00055 void Xor(char* buff, char const* chain) 00056 { 00057 if(false==m_bKeyInit) 00058 return; 00059 for(int i=0; i<m_blockSize; i++) 00060 *(buff++) ^= *(chain++); 00061 } 00062 00067 void DefEncryptBlock(char const* in, char* result); 00068 00073 void DefDecryptBlock(char const* in, char* result); 00074 00075 public: 00080 void EncryptBlock(char const* in, char* result); 00081 00086 void DecryptBlock(char const* in, char* result); 00087 00094 void Encrypt(char const* in, char* result, size_t n, int iMode=ECB); 00095 00102 void Decrypt(char const* in, char* result, size_t n, int iMode=ECB); 00103 00106 int GetKeyLength() 00107 { 00108 if(false==m_bKeyInit) 00109 return 0; 00110 return m_keylength; 00111 } 00112 00115 int GetBlockSize() 00116 { 00117 if(false==m_bKeyInit) 00118 return 0; 00119 return m_blockSize; 00120 } 00121 00124 int GetRounds() 00125 { 00126 if(false==m_bKeyInit) 00127 return 0; 00128 return m_iROUNDS; 00129 } 00130 00133 void ResetChain() 00134 { 00135 memcpy(m_chain, m_chain0, m_blockSize); 00136 } 00137 00138 public: 00141 static char const* sm_chain0; 00142 00143 private: 00144 static const int sm_alog[256]; 00145 static const int sm_log[256]; 00146 static const char sm_S[256]; 00147 static const char sm_Si[256]; 00148 static const int sm_T1[256]; 00149 static const int sm_T2[256]; 00150 static const int sm_T3[256]; 00151 static const int sm_T4[256]; 00152 static const int sm_T5[256]; 00153 static const int sm_T6[256]; 00154 static const int sm_T7[256]; 00155 static const int sm_T8[256]; 00156 static const int sm_U1[256]; 00157 static const int sm_U2[256]; 00158 static const int sm_U3[256]; 00159 static const int sm_U4[256]; 00160 static const char sm_rcon[30]; 00161 static const int sm_shifts[3][4][2]; 00164 bool m_bKeyInit; 00167 int m_Ke[MAX_ROUNDS+1][MAX_BC]; 00170 int m_Kd[MAX_ROUNDS+1][MAX_BC]; 00173 int m_keylength; 00176 int m_blockSize; 00179 int m_iROUNDS; 00182 char m_chain0[MAX_BLOCK_SIZE]; 00183 char m_chain[MAX_BLOCK_SIZE]; 00186 int tk[MAX_KC]; 00187 int a[MAX_BC]; 00188 int t[MAX_BC]; 00189 }; 00190 00191 #endif 00192 00199 void to64frombits(unsigned char *out, const unsigned char *in, int inlen); 00206 int from64tobits(char *out, const char *in, int maxlen); 00207