Const-ify Nb, Nk, Nr
[aes.git] / aes.hpp
1 #ifndef AES_HPP
2 #define AES_HPP
3
4 #include <vector>
5 #include <cstdio>
6 #include <string>
7 #include <iostream>
8 using namespace std;
9
10 // Byte and Byte Array types
11 typedef unsigned char byte;
12 typedef std::vector<byte> byteArray;
13
14 // Word and Word Array types
15 typedef unsigned int word;
16 typedef std::vector<word> wordArray;
17
18 // Exceptions
19 class incorrectKeySizeException { };
20 class incorrectTextSizeException { };
21 class badStateArrayException { };
22
23 class AES
24 {
25         public:
26                 AES (const byteArray& key);
27
28                 byteArray encrypt (const byteArray& plaintext) const;
29                 byteArray decrypt (const byteArray& ciphertext) const;
30
31         private:
32                 /* Block size in words -- Always constant in AES.
33                  *
34                  * We also might as well make this static and share it between
35                  * all instances of AES. */
36                 static const unsigned int Nb = 4;
37
38                 /* Key size in words -- can be 4, 6, or 8.
39                  *
40                  * Once it is set by the constructor, it will never change */
41                 const unsigned int Nk;
42
43                 /* Number of rounds -- depends on key size.
44                  *
45                  * Once it is set by the constructor, it will never change */
46                 const unsigned int Nr;
47
48                 wordArray keySchedule;
49
50                 void KeyExpansion (const byteArray& key, wordArray& w) const;
51                 word SubWord (const word& input) const;
52                 word RotWord (const word& input) const;
53                 wordArray GetRoundKey (const int round) const;
54
55                 void SubBytes (byteArray& state) const;
56                 void ShiftRows (byteArray& state) const;
57                 void MixColumns (byteArray& state) const;
58                 void AddRoundKey (byteArray& state, const wordArray& w) const;
59
60                 void InvSubBytes (byteArray& state) const;
61                 void InvShiftRows (byteArray& state) const;
62                 void InvMixColumns (byteArray& state) const;
63
64 };
65
66 #endif /* AES_HPP */
67
68 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */