summaryrefslogtreecommitdiff
path: root/src/core/stdcrypt/Rijndael.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/stdcrypt/Rijndael.h')
-rw-r--r--src/core/stdcrypt/Rijndael.h123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/core/stdcrypt/Rijndael.h b/src/core/stdcrypt/Rijndael.h
new file mode 100644
index 0000000000..a904259eb5
--- /dev/null
+++ b/src/core/stdcrypt/Rijndael.h
@@ -0,0 +1,123 @@
+
+//Rijndael.h
+
+#ifndef __RIJNDAEL_H__
+#define __RIJNDAEL_H__
+
+//Rijndael (pronounced Reindaal) is a block cipher, designed by Joan Daemen and Vincent Rijmen as a candidate algorithm for the AES.
+//The cipher has a variable block length and key length. The authors currently specify how to use keys with a length
+//of 128, 192, or 256 bits to encrypt blocks with al length of 128, 192 or 256 bits (all nine combinations of
+//key length and block length are possible). Both block length and key length can be extended very easily to
+// multiples of 32 bits.
+//Rijndael can be implemented very efficiently on a wide range of processors and in hardware.
+//This implementation is based on the Java Implementation used with the Cryptix toolkit found at:
+//http://www.esat.kuleuven.ac.be/~rijmen/rijndael/rijndael.zip
+//Java code authors: Raif S. Naffah, Paulo S. L. M. Barreto
+//This Implementation was tested against KAT test published by the authors of the method and the
+//results were identical.
+class CRijndael
+{
+private:
+ enum { DEFAULT_BLOCK_SIZE=16 };
+ enum { MAX_BLOCK_SIZE=32, MAX_ROUNDS=14, MAX_KC=8, MAX_BC=8 };
+
+public:
+ //CONSTRUCTOR
+ CRijndael();
+
+ //DESTRUCTOR
+ virtual ~CRijndael();
+
+ //Expand a user-supplied key material into a session key.
+ // key - The 128/192/256-bit user-key to use.
+ // chain - initial chain block
+ // keylength - 16, 24 or 32 bytes
+ // blockSize - The block size in bytes of this Rijndael (16, 24 or 32 bytes).
+ int MakeKey(char const* key, char const* chain, int keylength, int blockSize);
+
+private:
+ //Auxiliary Function
+ void Xor(char* buff, char const* chain)
+ {
+ for (int i = 0; i < m_blockSize; i++)
+ *(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
+ 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.
+ 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.
+ 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.
+ void DecryptBlock(char const* in, char* result);
+
+ int Encrypt(char const* in, char* result, size_t n);
+
+ int Decrypt(char const* in, char* result, size_t n);
+
+ //Get Key Length
+ int GetKeyLength()
+ {
+ return m_keylength;
+ }
+
+ //Block Size
+ int GetBlockSize()
+ {
+ return m_blockSize;
+ }
+
+ //Number of Rounds
+ int GetRounds()
+ {
+ return m_iROUNDS;
+ }
+
+ void ResetChain()
+ {
+ memcpy(m_chain, m_chain0, m_blockSize);
+ }
+
+public:
+ //Null chain
+ static char const* sm_chain0;
+
+private:
+ //Key Initialization Flag
+ bool m_bKeyInit;
+ //Encryption (m_Ke) round key
+ int m_Ke[MAX_ROUNDS + 1][MAX_BC];
+ //Decryption (m_Kd) round key
+ int m_Kd[MAX_ROUNDS + 1][MAX_BC];
+ //Key Length
+ int m_keylength;
+ //Block Size
+ int m_blockSize;
+ //Number of Rounds
+ int m_iROUNDS;
+ //Chain Block
+ char m_chain0[MAX_BLOCK_SIZE];
+ char m_chain[MAX_BLOCK_SIZE];
+ //Auxiliary private use buffers
+ int tk[MAX_KC];
+ int a[MAX_BC];
+ int t[MAX_BC];
+};
+
+#endif // __RIJNDAEL_H__
+