-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes.cpp
415 lines (387 loc) · 10.2 KB
/
aes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "aes.h"
AES::AES(AES_KeyLength len) {
changeType(len);
}
AES::~AES() {}
void AES::changeType(AES_KeyLength len) {
this->keyLen = len;
switch (len) {
case (AES_128):
Nk = 4;
Nr = 10;
break;
case (AES_192):
Nk = 6;
Nr = 12;
break;
case (AES_256):
Nk = 8;
Nr = 14;
break;
}
}
state_t AES::cipher(state_t state, const std::vector<uint32_t> w) {
uint8_t round = 0;
addRoundKey(round, state, w);
round++;
while (round < this->Nr) {
subBytes(state);
shiftRows(state);
mixColumns(state);
addRoundKey(round, state, w);
round++;
}
subBytes(state);
shiftRows(state);
addRoundKey(round, state, w);
return state;
}
state_t AES::cipherWithDebug(state_t state, const std::vector<uint32_t> w) {
uint8_t round = 0;
std::cout<<"#######CIPHERSTART#######"<<std::endl;
std::cout << "------roundstart: " << (int)round << "------" << std::endl;
printState(state);
addRoundKey(round, state, w);
round++;
while (round < this->Nr) {
std::cout << "------roundstart: " << (int)round << "------" << std::endl;
subBytes(state);
printState(state);
shiftRows(state);
printState(state);
mixColumns(state);
printState(state);
addRoundKey(round, state, w);
std::cout << "------roundend: " << (int)round << "------" << std::endl;
printState(state);
round++;
}
subBytes(state);
printState(state);
shiftRows(state);
printState(state);
addRoundKey(round, state, w);
printState(state);
std::cout<<"#######CIPHEREND#######"<<std::endl;
return state;
}
state_t AES::invCipher(state_t state, const std::vector<uint32_t> w) {
uint8_t round = this->Nr;
addRoundKey(round, state, w);
round--;
while (round > 0) {
shiftRows(state, true);
subBytes(state, true);
// addRoundKeyとmixColumnsが、cipherとは逆であることに注意
addRoundKey(round, state, w);
mixColumns(state, true);
round--;
}
shiftRows(state, true);
subBytes(state, true);
addRoundKey(round, state, w);
return state;
}
state_t AES::invCipherWithDebug(state_t state, const std::vector<uint32_t> w) {
std::cout<<"#######INVCIPHERSTART#######"<<std::endl;
uint8_t round = this->Nr;
std::cout << "------roundstart: " << (int)round << "------" << std::endl;
printState(state);
addRoundKey(round, state, w);
round--;
while (round >= 1) {
std::cout << "------roundstart: " << (int)round << "------" << std::endl;
printState(state);
shiftRows(state, true);
printState(state);
subBytes(state, true);
printState(state);
addRoundKey(round, state, w);
printState(state);
mixColumns(state, true);
std::cout << "------roundend: " << (int)round << "------" << std::endl;
printState(state);
round--;
}
shiftRows(state, true);
printState(state);
subBytes(state, true);
printState(state);
addRoundKey(round, state, w);
printState(state);
std::cout<<"#######INVCIPHEREND#######"<<std::endl;
return state;
}
uint8_t AES::SBox(uint8_t byte, bool inverse/* = false*/) {
if (inverse) {
byte = INV_SBOX[(byte & 0xf0) >> 4][byte & 0x0f];
}
else {
byte = SBOX[(byte & 0xf0) >> 4][byte & 0x0f];
}
return byte;
}
void AES::subBytes(state_t& state, bool inverse/* = false*/) {
for (int i = 0; i < STATE_ROWS; i++) {
for (int j = 0; j < STATE_COLS; j++) {
if (inverse) {
state[i][j] = SBox(state[i][j], true);
}
else {
state[i][j] = SBox(state[i][j]);
}
}
}
return ;
}
void AES::shiftRows(state_t& state, bool inverse/* = false*/) {
std::array<uint8_t, STATE_COLS> tmp;
for (int row = 1; row < STATE_ROWS; row++) {
for (int col = 0; col < STATE_COLS; col++) {
if (inverse) {
// shift to the right
tmp[(col + row) % STATE_COLS] = state[row][col];
}
else {
// shift to the left
tmp[col] = state[row][(col + row) % STATE_COLS];
}
}
for (int col = 0; col < STATE_COLS; col++) {
state[row][col] = tmp[col];
}
}
return ;
}
uint8_t AES::xTimes(uint8_t x) {
if (x & 0x80) {
return (x << 1);
} else {
return ((x << 1) ^ irreducible);
}
}
// mul(x, y=3)
// mask = 0b00000001
// product + x
// x <- xtimes(x) <=> x <- x・2
// mask = 0b00000010
// product + x ・ 2
// return product = x・3
uint8_t AES::mul(uint8_t x, uint8_t y) {
uint8_t product = 0;
for (uint8_t mask = 0x01; mask; mask <<= 1) {
if (y & mask) {
product ^= x;
}
x = xTimes(x);
}
return (product);
}
// 2 3 1 1
// 1 2 3 1
// 1 1 2 3
// 3 1 1 2
void AES::mixColumns(state_t& state, bool inverse/* = false()*/) {
std::array<uint8_t, STATE_ROWS> temp;
for (uint8_t col = 0; col < STATE_COLS; col++) {
for (uint8_t row = 0; row < STATE_ROWS; row++) {
if (inverse) {
temp[row] = mul(state[0][col], INV_MIXBOX[row][0])
^ mul(state[1][col], INV_MIXBOX[row][1])
^ mul(state[2][col], INV_MIXBOX[row][2])
^ mul(state[3][col], INV_MIXBOX[row][3]);
}
else {
temp[row] = mul(state[0][col], MIXBOX[row][0])
^ mul(state[1][col], MIXBOX[row][1])
^ mul(state[2][col], MIXBOX[row][2])
^ mul(state[3][col], MIXBOX[row][3]);
}
}
for (uint8_t row = 0; row < STATE_ROWS; row++) {
state[row][col] = temp[row];
}
}
return ;
}
void AES::addRoundKey(uint8_t round, state_t& state, const std::vector<uint32_t> w) {
for (uint8_t col = 0; col < STATE_COLS; col++) {
uint32_t word = w[STATE_COLS * round + col];
//printf("round: %d col: %d roundkey: %08x\n", (int)round, (int)col, word);
state[0][col] ^= (uint8_t)((word & 0xff000000) >> 24);
state[1][col] ^= (uint8_t)((word & 0x00ff0000) >> 16);
state[2][col] ^= (uint8_t)((word & 0x0000ff00) >> 8);
state[3][col] ^= (uint8_t)(word & 0x000000ff);
}
return ;
}
uint32_t AES::rotWord(uint32_t word) {
uint32_t head = ((word & 0xff000000) >> 24);
word <<= 8;
word |= head;
return word;
}
uint32_t AES::subWord(uint32_t word) {
uint32_t result = 0;
for (uint8_t i = 0; i < 4; i++) {
uint8_t byte = (word >> (i * 8)) & 0xff;
byte = SBox(byte);
result |= (byte << (i * 8));
}
return result;
}
std::vector<uint32_t> AES::keyExpansion(std::vector<uint32_t> key) {
std::vector<uint32_t> w(4 * (this->Nr + 1));
// 1Word = 4Bytes = 32bits
// aes128 = 128bits = 16Bytes = 4Word
int i = 0;
while (i <= this->Nk - 1) {
w[i] = key[i];
i++;
}
uint32_t temp;
while (i <= 4 * this->Nr + 3) {
temp = w[i - 1];
// AES256に限った処理
if (this->keyLen == AES_256 && (i + 4) % 8 == 0) {
w[i] = w[i - this->Nk] ^ subWord(w[i - 1]);
i++;
continue;
}
else if (i % this->Nk == 0) {
temp = subWord(rotWord((temp))) ^ RCON[i / this->Nk];
}
else if (this->Nk > 6 && (i % this->Nk) == 4) {
temp = subWord(temp);
}
w[i] = w[i - this->Nk] ^ temp;
i++;
}
return w;
}
void AES::printState(state_t state) {
for (uint8_t i = 0; i < STATE_ROWS; i++) {
for (uint8_t j = 0; j < STATE_COLS; j++) {
printf("%02x ", state[i][j]);
}
std::cout << std::endl;
}
return ;
}
bool AES::keyIsValid(std::vector<uint32_t> key) {
return key.size() == this->Nk;
}
// PKCS
void AES::padding(state_t& state, uint8_t row, uint8_t col) {
uint8_t padding = 0x10 - (row * STATE_COLS + col);
uint8_t cnt = row * STATE_COLS + col;
while (cnt < STATE_ROWS * STATE_COLS) {
state[cnt / STATE_COLS][cnt % STATE_COLS] = padding;
cnt++;
}
return ;
}
std::pair<uint8_t, uint8_t> AES::findPadding(state_t& state) {
std::pair<uint8_t, uint8_t> padding;
uint8_t size = state[STATE_ROWS - 1][STATE_COLS - 1];
uint8_t cnt = size;
for (int row = STATE_ROWS - 1; row >= 0; row--) {
for (int col = STATE_COLS - 1; col >= 0; col--) {
// paddingの値がsizeと一致しない場合、paddingが不正であると判断
if (state[row][col] != size) {
throw std::runtime_error("Error: invalid padding type!");
}
cnt--;
// paddingの値がsizeと一致した場合、paddingの終端と判断
if (cnt == 0) {
padding = std::make_pair(row, col);
return padding;
}
}
}
// unreachable
return padding;
}
// string or vector<uint8_t> to vector<state_t>
template<typename T>
typename std::enable_if<std::is_same<T, std::string>::value || std::is_same<T, std::vector<uint8_t> >::value, std::vector<state_t> >::type
AES::toBlocks(T data, bool paddingEnabled/* = true*/) {
std::vector<state_t> blocks;
state_t block;
size_t len = data.size();
size_t i = 0;
while (i < len) {
for (uint8_t row = 0; row < STATE_ROWS; row++) {
for (uint8_t col = 0; col < STATE_COLS; col++) {
block[row][col] = data[i];
if (i == len - 1 && paddingEnabled) {
// もしpaddingする余地がない場合、0x10で埋めた終端blockを追加
if (row == STATE_ROWS - 1 && col == STATE_COLS - 1) {
blocks.push_back(block);
padding(block, 0, 0);
blocks.push_back(block);
} else {
padding(block, row, col);
blocks.push_back(block);
}
return blocks;
}
i++;
}
}
blocks.push_back(block);
}
// unreachable
return blocks;
}
std::string AES::encryptECB(std::string data, std::vector<uint32_t> key) {
if (!keyIsValid(key)) {
throw std::runtime_error("Error: encryptECB called with invalid key!");
}
std::vector<state_t> blocks = toBlocks(data);
std::vector<uint32_t> w = keyExpansion(key);
std::string result;
state_t encrypted;
for (state_t block : blocks) {
encrypted = cipher(block, w);
for (uint8_t i = 0; i < STATE_ROWS; i++) {
for (uint8_t j = 0; j < STATE_COLS; j++) {
result.push_back(encrypted[i][j]);
}
}
}
return result;
}
std::string AES::decryptECB(std::string data, std::vector<uint32_t> key) {
if (!keyIsValid(key)) {
throw std::runtime_error("Error: decryptECB called with invalid key!");
} else if (data.size() % 16 != 0) {
throw std::runtime_error("Error: decryptECB called with invalid data!");
}
std::vector<state_t> blocks = toBlocks(data, false);
std::vector<uint32_t> w = keyExpansion(key);
std::string result;
state_t decrypted;
std::pair<uint8_t, uint8_t> padding;
uint8_t cnt = 0;
for (state_t block : blocks) {
decrypted = invCipher(block, w);
// もし、最後のblockの場合
if (block == blocks.back()) {
padding = findPadding(decrypted);
while (true) {
if (cnt / STATE_ROWS == padding.first && cnt % STATE_COLS == padding.second) {
return result;
}
result.push_back(decrypted[cnt / STATE_ROWS][cnt % STATE_COLS]);
cnt++;
}
}
for (uint8_t i = 0; i < STATE_ROWS; i++) {
for (uint8_t j = 0; j < STATE_COLS; j++) {
result.push_back(decrypted[i][j]);
}
}
}
return result;
}