Optimize ShiftRows() and InvShiftRows()
[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                 unsigned int Nb;
33                 unsigned int Nk;
34                 unsigned int Nr;
35
36                 wordArray keySchedule;
37
38                 void KeyExpansion (const byteArray& key, wordArray& w) const;
39                 word SubWord (const word& input) const;
40                 word RotWord (const word& input) const;
41                 wordArray GetRoundKey (const int round) const;
42
43                 void SubBytes (byteArray& state) const;
44                 void ShiftRows (byteArray& state) const;
45                 void MixColumns (byteArray& state) const;
46                 void AddRoundKey (byteArray& state, const wordArray& w) const;
47
48                 void InvSubBytes (byteArray& state) const;
49                 void InvShiftRows (byteArray& state) const;
50                 void InvMixColumns (byteArray& state) const;
51
52 };
53
54 #endif /* AES_HPP */
55
56 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */