Optimize ShiftRows() and InvShiftRows()
[aes.git] / aes_test.cpp
1 #include "aes.hpp"
2 #include <iostream>
3 #include <fstream>
4
5 static void print_by_16s (const byteArray b, std::string name)
6 {
7         int i, j;
8
9         std::cout << "\nPrinting: " << name << std::endl;
10
11         for (i=0; i<b.size()/16; ++i)
12         {
13                 for (j=0; j<16; ++j)
14                         std::printf ("%.2x ", b.at(i*16+j));
15
16                 std::printf ("\n");
17         }
18 }
19
20
21 int main (int argc, char *argv[])
22 {
23
24 #define DEBUG 1
25 //#define MY_TEST2 1
26 //#define PROBLEM2 1
27 //#define PROBLEM3 1
28 //#define AES_128_EXAMPLE 1
29 #define AES_128_EXAMPLE2 1
30
31 #if PROBLEM2
32         int i;
33
34         /* Read the Key */
35         byteArray key;
36
37         for (i=0; i<16; ++i)
38                 key.push_back (getchar());
39
40         /* Create the AES object */
41         AES aes (key);
42
43         /* Read all characters of input */
44         byteArray input;
45
46         for (char c=getchar(); c != EOF; c=getchar())
47         {
48                 input.push_back (c);
49
50                 if (input.size() == 16)
51                 {
52                         byteArray ciphertext = aes.encrypt (input);
53                         byteArray plaintext  = aes.decrypt (ciphertext);
54
55                         /* Print it */
56                         for (i=0; i<plaintext.size(); ++i)
57                                 std::printf ("%c", plaintext.at(i));
58
59                         input.clear();
60                 }
61
62         }
63 #endif // PROBLEM2
64
65 #if PROBLEM3
66         std::ifstream fin ("Problem3.out", ios::binary);
67         int i;
68
69         byteArray key;
70
71         for (i=0; i<24; ++i)
72                 key.push_back (fin.get());
73
74         /* Create the AES Object */
75         AES aes (key);
76
77         byteArray input;
78
79         /* Read the input and decrypt */
80         for (char c=fin.get(); !fin.eof(); c=fin.get())
81         {
82                 input.push_back (c);
83
84                 if (input.size() == 16)
85                 {
86                         byteArray plaintext = aes.decrypt (input);
87
88                         /* Print it */
89                         for (i=0; i<plaintext.size(); ++i)
90                                 std::printf ("%c", plaintext.at(i));
91
92                 input.clear();
93                 }
94         }
95 #endif //PROBLEM3
96
97 #if MY_TEST
98         const int inputSize = 4096;
99
100         byteArray key (16, 2);
101         byteArray input (inputSize, 0);
102         int i, j;
103
104         for (i=0; i<inputSize; ++i)
105                 input.at (i) = i;
106
107         AES aes (key);
108
109         print_by_16s (input, "Input");
110
111         byteArray ciphertext = aes.encrypt (input);
112
113         print_by_16s (ciphertext, "Ciphertext");
114
115         byteArray plaintext  = aes.decrypt (ciphertext);
116
117         print_by_16s (plaintext, "Plaintext");
118 #endif
119
120 #if MY_TEST2
121         int i;
122
123         byteArray key (16, 0);
124
125         for (i=0; i<16; ++i)
126                 key.at (i) = i;
127
128         print_by_16s (key, "Key");
129
130         byteArray plaintext (16, 0);
131
132         for (i=0; i<16; ++i)
133                 plaintext.at (i) = (i << 4) | i;
134
135         print_by_16s (plaintext, "Plaintext");
136
137         AES aes (key);
138
139         byteArray ciphertext = aes.encrypt (plaintext);
140
141         print_by_16s (ciphertext, "Ciphertext");
142 #endif // MY_TEST2
143
144 #if AES_128_EXAMPLE
145         int i;
146         byteArray key (16, 0);
147         byteArray plaintext (16, 0);
148
149         for (i=0; i<16; ++i)
150                 key.at (i) = i;
151
152         for (i=0; i<16; ++i)
153                 plaintext.at (i) = (i) | (i<<4);
154
155         AES aes (key);
156         byteArray ciphertext = aes.encrypt (plaintext);
157
158         print_by_16s (key, "Key");
159         print_by_16s (plaintext, "Plaintext");
160         print_by_16s (ciphertext, "Ciphertext");
161 #endif // AES_128_EXAMPLE
162
163 #if AES_128_EXAMPLE2
164         int i;
165         byteArray key (16, 0);
166         byteArray plaintext (16, 0);
167
168         key[0] = 0x2b;
169         key[1] = 0x7e;
170         key[2] = 0x15;
171         key[3] = 0x16;
172         key[4] = 0x28;
173         key[5] = 0xae;
174         key[6] = 0xd2;
175         key[7] = 0xa6;
176         key[8] = 0xab;
177         key[9] = 0xf7;
178         key[10] = 0x15;
179         key[11] = 0x88;
180         key[12] = 0x09;
181         key[13] = 0xcf;
182         key[14] = 0x4f;
183         key[15] = 0x3c;
184
185         plaintext[0] = 0x32;
186         plaintext[1] = 0x43;
187         plaintext[2] = 0xf6;
188         plaintext[3] = 0xa8;
189         plaintext[4] = 0x88;
190         plaintext[5] = 0x5a;
191         plaintext[6] = 0x30;
192         plaintext[7] = 0x8d;
193         plaintext[8] = 0x31;
194         plaintext[9] = 0x31;
195         plaintext[10] = 0x98;
196         plaintext[11] = 0xa2;
197         plaintext[12] = 0xe0;
198         plaintext[13] = 0x37;
199         plaintext[14] = 0x07;
200         plaintext[15] = 0x34;
201
202         AES aes (key);
203         byteArray ciphertext = aes.encrypt (plaintext);
204
205         print_by_16s (key, "Key");
206         print_by_16s (plaintext, "Plaintext");
207         print_by_16s (ciphertext, "Ciphertext");
208 #endif // AES_128_EXAMPLE
209         return 0;
210
211 }
212
213 /* vim: set ts=4 sts=4 sw=4 noet tw=120 nowrap: */