#define __AES_H__\r
\r
#include <cstring>\r
+#include "inspircd_config.h"\r
+#include "base.h"\r
\r
using namespace std;\r
\r
-class AES\r
+/** The AES class is a utility class for use in modules and the core for encryption of data.\r
+ */\r
+class AES : public classbase\r
{\r
public:\r
enum { ECB=0, CBC=1, CFB=2 };\r
return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0;\r
}\r
\r
- //Convenience method used in generating Transposition Boxes\r
+ /** Convenience method used in generating Transposition Boxes\r
+ */\r
static int Mul4(int a, char b[])\r
{\r
if(a == 0)\r
}\r
\r
public:\r
- //CONSTRUCTOR\r
AES();\r
\r
- //DESTRUCTOR\r
virtual ~AES();\r
\r
- //Expand a user-supplied key material into a session key.\r
- // key - The 128/192/256-bit user-key to use.\r
- // chain - initial chain block for CBC and CFB modes.\r
- // keylength - 16, 24 or 32 bytes\r
- // blockSize - The block size in bytes of this Rijndael (16, 24 or 32 bytes).\r
+ /** Expand a user-supplied key material into a session key.\r
+ * \r
+ * @param key The 128/192/256-bit user-key to use.\r
+ * @param chain Initial chain block for CBC and CFB modes.\r
+ * @param keylength 16, 24 or 32 bytes\r
+ * @param blockSize The block size in bytes of this Rijndael (16, 24 or 32 bytes).\r
+ */\r
void MakeKey(char const* key, char const* chain, int keylength=DEFAULT_BLOCK_SIZE, int blockSize=DEFAULT_BLOCK_SIZE);\r
\r
private:\r
- //Auxiliary Function\r
+ /** Auxiliary Function\r
+ */\r
void Xor(char* buff, char const* chain)\r
{\r
if(false==m_bKeyInit)\r
*(buff++) ^= *(chain++); \r
}\r
\r
- //Convenience method to encrypt exactly one block of plaintext, assuming\r
- //Rijndael's default block size (128-bit).\r
- // in - The plaintext\r
- // result - The ciphertext generated from a plaintext using the key\r
+ /** Convenience method to encrypt exactly one block of plaintext, assuming Rijndael's default block size (128-bit).\r
+ * @param in The plaintext\r
+ * @param result The ciphertext generated from a plaintext using the key\r
+ */\r
void DefEncryptBlock(char const* in, char* result);\r
\r
- //Convenience method to decrypt exactly one block of plaintext, assuming\r
- //Rijndael's default block size (128-bit).\r
- // in - The ciphertext.\r
- // result - The plaintext generated from a ciphertext using the session key.\r
+ /** Convenience method to decrypt exactly one block of plaintext, assuming Rijndael's default block size (128-bit).\r
+ * @param in The ciphertext.\r
+ * @param result The plaintext generated from a ciphertext using the session key.\r
+ */\r
void DefDecryptBlock(char const* in, char* result);\r
\r
public:\r
- //Encrypt exactly one block of plaintext.\r
- // in - The plaintext.\r
- // result - The ciphertext generated from a plaintext using the key.\r
+ /** Encrypt exactly one block of plaintext.\r
+ * @param in The plaintext.\r
+ * @param result The ciphertext generated from a plaintext using the key.\r
+ */\r
void EncryptBlock(char const* in, char* result);\r
\r
- //Decrypt exactly one block of ciphertext.\r
- // in - The ciphertext.\r
- // result - The plaintext generated from a ciphertext using the session key.\r
+ /** Decrypt exactly one block of ciphertext.\r
+ * @param in The ciphertext.\r
+ * @param result The plaintext generated from a ciphertext using the session key.\r
+ */\r
void DecryptBlock(char const* in, char* result);\r
\r
+ /** Encrypt multiple blocks of plaintext.\r
+ * @param n Number of bytes to encrypt, must be a multiple of the keysize\r
+ * @param in The plaintext to encrypt\r
+ * @param result The output ciphertext\r
+ * @param iMode Mode to use\r
+ */\r
void Encrypt(char const* in, char* result, size_t n, int iMode=ECB);\r
\r
+ /** Decrypt multiple blocks of ciphertext.\r
+ * @param n Number of bytes to decrypt, must be a multiple of the keysize\r
+ * @param in The ciphertext to decrypt\r
+ * @param result The output plaintext\r
+ * @param iMode Mode to use\r
+ */\r
void Decrypt(char const* in, char* result, size_t n, int iMode=ECB);\r
\r
- //Get Key Length\r
+ /** Get Key Length\r
+ */\r
int GetKeyLength()\r
{\r
if(false==m_bKeyInit)\r
return m_keylength;\r
}\r
\r
- //Block Size\r
+ /** Get Block Size\r
+ */\r
int GetBlockSize()\r
{\r
if(false==m_bKeyInit)\r
return m_blockSize;\r
}\r
\r
- //Number of Rounds\r
+ /** Get Number of Rounds\r
+ */\r
int GetRounds()\r
{\r
if(false==m_bKeyInit)\r
return m_iROUNDS;\r
}\r
\r
+ /** Reset the chain\r
+ */\r
void ResetChain()\r
{\r
memcpy(m_chain, m_chain0, m_blockSize);\r
}\r
\r
public:\r
- //Null chain\r
+ /** Null chain\r
+ */\r
static char const* sm_chain0;\r
\r
private:\r
static const int sm_U4[256];\r
static const char sm_rcon[30];\r
static const int sm_shifts[3][4][2];\r
- //Error Messages\r
- static char const* sm_szErrorMsg1;\r
- static char const* sm_szErrorMsg2;\r
- //Key Initialization Flag\r
+ /** Key Initialization Flag\r
+ */\r
bool m_bKeyInit;\r
- //Encryption (m_Ke) round key\r
+ /** Encryption (m_Ke) round key\r
+ */\r
int m_Ke[MAX_ROUNDS+1][MAX_BC];\r
- //Decryption (m_Kd) round key\r
+ /** Decryption (m_Kd) round key\r
+ */\r
int m_Kd[MAX_ROUNDS+1][MAX_BC];\r
- //Key Length\r
+ /** Key Length\r
+ */\r
int m_keylength;\r
- //Block Size\r
+ /** Block Size\r
+ */\r
int m_blockSize;\r
- //Number of Rounds\r
+ /** Number of Rounds\r
+ */\r
int m_iROUNDS;\r
- //Chain Block\r
+ /**Chain Block\r
+ */\r
char m_chain0[MAX_BLOCK_SIZE];\r
char m_chain[MAX_BLOCK_SIZE];\r
- //Auxiliary private use buffers\r
+ /** Auxiliary private use buffers\r
+ */\r
int tk[MAX_KC];\r
int a[MAX_BC];\r
int t[MAX_BC];\r
};\r
\r
-#endif // __RIJNDAEL_H__\r
+#endif\r
+\r
+/** Convert from binary to base64\r
+ * @param out Output\r
+ * @param in Input\r
+ * @param inlen Number of bytes in input buffer\r
+ */\r
\r
void to64frombits(unsigned char *out, const unsigned char *in, int inlen);\r
+/** Convert from base64 to binary\r
+ * @out Output\r
+ * @in Input\r
+ * @maxlen Size of output buffer\r
+ * @return Number of bytes actually converted\r
+ */\r
int from64tobits(char *out, const char *in, int maxlen);\r
\r
-\r