From 9a6d5c5152dac57c967e6a936b36f640258fa43d Mon Sep 17 00:00:00 2001 From: brain Date: Mon, 19 Dec 2005 17:55:20 +0000 Subject: Added comments git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@2577 e03df62e-2008-0410-955e-edbf42e46eb7 --- include/aes.h | 120 ++++++++++++++++++++++++++++++++++++++-------------------- src/aes.cpp | 18 ++++++++- 2 files changed, 97 insertions(+), 41 deletions(-) diff --git a/include/aes.h b/include/aes.h index 05381954a..114d882fe 100644 --- a/include/aes.h +++ b/include/aes.h @@ -5,6 +5,8 @@ using namespace std; +/** The AES class is a utility class for use in modules and the core for encryption of data. + */ class AES { public: @@ -19,7 +21,8 @@ private: return (a != 0 && b != 0) ? sm_alog[(sm_log[a & 0xFF] + sm_log[b & 0xFF]) % 255] : 0; } - //Convenience method used in generating Transposition Boxes + /** Convenience method used in generating Transposition Boxes + */ static int Mul4(int a, char b[]) { if(a == 0) @@ -33,21 +36,22 @@ private: } 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). + /** 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 + /** Auxiliary Function + */ void Xor(char* buff, char const* chain) { if(false==m_bKeyInit) @@ -56,34 +60,49 @@ private: *(buff++) ^= *(chain++); } - //Convenience method to encrypt exactly one block of plaintext, assuming - //Rijndael's default block size (128-bit). - // in - The plaintext - // result - The ciphertext generated from a plaintext using the key + /** Convenience method to encrypt exactly one block of plaintext, assuming Rijndael's default block size (128-bit). + * @param in The plaintext + * @param result The ciphertext generated from a plaintext using the key + */ void DefEncryptBlock(char const* in, char* result); - //Convenience method to decrypt exactly one block of plaintext, assuming - //Rijndael's default block size (128-bit). - // in - The ciphertext. - // result - The plaintext generated from a ciphertext using the session key. + /** Convenience method to decrypt exactly one block of plaintext, assuming Rijndael's default block size (128-bit). + * @param in The ciphertext. + * @param result The plaintext generated from a ciphertext using the session key. + */ void DefDecryptBlock(char const* in, char* result); public: - //Encrypt exactly one block of plaintext. - // in - The plaintext. - // result - The ciphertext generated from a plaintext using the key. + /** Encrypt exactly one block of plaintext. + * @param in The plaintext. + * @param result The ciphertext generated from a plaintext using the key. + */ void EncryptBlock(char const* in, char* result); - //Decrypt exactly one block of ciphertext. - // in - The ciphertext. - // result - The plaintext generated from a ciphertext using the session key. + /** Decrypt exactly one block of ciphertext. + * @param in The ciphertext. + * @param result The plaintext generated from a ciphertext using the session key. + */ void DecryptBlock(char const* in, char* result); + /** Encrypt multiple blocks of plaintext. + * @param n Number of bytes to encrypt, must be a multiple of the keysize + * @param in The plaintext to encrypt + * @param result The output ciphertext + * @param iMode Mode to use + */ void Encrypt(char const* in, char* result, size_t n, int iMode=ECB); + /** Decrypt multiple blocks of ciphertext. + * @param n Number of bytes to decrypt, must be a multiple of the keysize + * @param in The ciphertext to decrypt + * @param result The output plaintext + * @param iMode Mode to use + */ void Decrypt(char const* in, char* result, size_t n, int iMode=ECB); - //Get Key Length + /** Get Key Length + */ int GetKeyLength() { if(false==m_bKeyInit) @@ -91,7 +110,8 @@ public: return m_keylength; } - //Block Size + /** Get Block Size + */ int GetBlockSize() { if(false==m_bKeyInit) @@ -99,7 +119,8 @@ public: return m_blockSize; } - //Number of Rounds + /** Get Number of Rounds + */ int GetRounds() { if(false==m_bKeyInit) @@ -107,13 +128,16 @@ public: return m_iROUNDS; } + /** Reset the chain + */ void ResetChain() { memcpy(m_chain, m_chain0, m_blockSize); } public: - //Null chain + /** Null chain + */ static char const* sm_chain0; private: @@ -135,33 +159,49 @@ private: static const int sm_U4[256]; static const char sm_rcon[30]; static const int sm_shifts[3][4][2]; - //Error Messages - static char const* sm_szErrorMsg1; - static char const* sm_szErrorMsg2; - //Key Initialization Flag + /** Key Initialization Flag + */ bool m_bKeyInit; - //Encryption (m_Ke) round key + /** Encryption (m_Ke) round key + */ int m_Ke[MAX_ROUNDS+1][MAX_BC]; - //Decryption (m_Kd) round key + /** Decryption (m_Kd) round key + */ int m_Kd[MAX_ROUNDS+1][MAX_BC]; - //Key Length + /** Key Length + */ int m_keylength; - //Block Size + /** Block Size + */ int m_blockSize; - //Number of Rounds + /** Number of Rounds + */ int m_iROUNDS; - //Chain Block + /**Chain Block + */ char m_chain0[MAX_BLOCK_SIZE]; char m_chain[MAX_BLOCK_SIZE]; - //Auxiliary private use buffers + /** Auxiliary private use buffers + */ int tk[MAX_KC]; int a[MAX_BC]; int t[MAX_BC]; }; -#endif // __RIJNDAEL_H__ +#endif + +/** Convert from binary to base64 + * @param out Output + * @param in Input + * @param inlen Number of bytes in input buffer + */ void to64frombits(unsigned char *out, const unsigned char *in, int inlen); +/** Convert from base64 to binary + * @out Output + * @in Input + * @maxlen Size of output buffer + * @return Number of bytes actually converted + */ int from64tobits(char *out, const char *in, int maxlen); - diff --git a/src/aes.cpp b/src/aes.cpp index 25f29d4ba..cf56e7f46 100644 --- a/src/aes.cpp +++ b/src/aes.cpp @@ -1,5 +1,21 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * Inspire is copyright (C) 2002-2005 ChatSpike-Dev. + * E-mail: + * + * + * + * Written by Craig Edwards, Craig McLure, and others. + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ -//Rijndael.cpp +// Based on existing implementations of the industry standard AES algorithms +// in the public domain. #include #include "aes.h" -- cgit v1.2.3