-
Notifications
You must be signed in to change notification settings - Fork 5
/
aes.c
498 lines (434 loc) · 15.7 KB
/
aes.c
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* SPDX-License-Identifier: MIT
*/
#if defined(HAVE_CONFIG_H)
#include <config.h>
#endif
#include "aes.h"
#include "fields.h"
#include "compat.h"
#include "utils.h"
#if defined(HAVE_OPENSSL)
#include <openssl/evp.h>
#endif
#include <string.h>
#define ROUNDS_128 10
#define ROUNDS_192 12
#define ROUNDS_256 14
#define KEY_WORDS_128 4
#define KEY_WORDS_192 6
#define KEY_WORDS_256 8
#define AES_BLOCK_WORDS 4
#define RIJNDAEL_BLOCK_WORDS_192 6
#define RIJNDAEL_BLOCK_WORDS_256 8
static const bf8_t round_constants[30] = {
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91,
};
static int contains_zero(const bf8_t* block) {
return !block[0] | !block[1] | !block[2] | !block[3];
}
static bf8_t compute_sbox(bf8_t in) {
bf8_t t = bf8_inv(in);
// get_bit(t, 0) ^ get_bit(t, 4) ^ get_bit(t, 5) ^ get_bit(t, 6) ^ get_bit(t, 7)
bf8_t t0 = set_bit(parity8(t & (1 | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7))), 0);
// get_bit(t, 0) ^ get_bit(t, 1) ^ get_bit(t, 5) ^ get_bit(t, 6) ^ get_bit(t,
t0 ^= set_bit(parity8(t & (1 | (1 << 1) | (1 << 5) | (1 << 6) | (1 << 7))), 1);
// get_bit(t, 0) ^ get_bit(t, 1) ^ get_bit(t, 2) ^ get_bit(t, 6) ^ get_bit(t, 7)
t0 ^= set_bit(parity8(t & (1 | (1 << 1) | (1 << 2) | (1 << 6) | (1 << 7))), 2);
// get_bit(t, 0) ^ get_bit(t, 1) ^ get_bit(t, 2) ^ get_bit(t, 3) ^ get_bit(t, 7)
t0 ^= set_bit(parity8(t & (1 | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 7))), 3);
// get_bit(t, 0) ^ get_bit(t, 1) ^ get_bit(t, 2) ^ get_bit(t, 3) ^ get_bit(t, 4)
t0 ^= set_bit(parity8(t & (1 | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4))), 4);
// get_bit(t, 1) ^ get_bit(t, 2) ^ get_bit(t, 3) ^ get_bit(t, 4) ^ get_bit(t, 5)
t0 ^= set_bit(parity8(t & ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5))), 5);
// get_bit(t, 2) ^ get_bit(t, 3) ^ get_bit(t, 4) ^ get_bit(t, 5) ^ get_bit(t, 6)
t0 ^= set_bit(parity8(t & ((1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6))), 6);
// get_bit(t, 3) ^ get_bit(t, 4) ^ get_bit(t, 5) ^ get_bit(t, 6) ^ get_bit(t, 7)
t0 ^= set_bit(parity8(t & ((1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7))), 7);
return t0 ^ (1 | (1 << 1) | (1 << 5) | (1 << 6));
}
void aes_increment_iv(uint8_t* iv) {
for (unsigned int i = 16; i > 0; i--) {
if (iv[i - 1] == 0xff) {
iv[i - 1] = 0x00;
continue;
}
iv[i - 1] += 0x01;
break;
}
}
// ## AES ##
// Round Functions
static void add_round_key(unsigned int round, aes_block_t state, const aes_round_keys_t* round_key,
unsigned int block_words) {
for (unsigned int c = 0; c < block_words; c++) {
xor_u8_array(&state[c][0], &round_key->round_keys[round][c][0], &state[c][0], AES_NR);
}
}
static int sub_bytes(aes_block_t state, unsigned int block_words) {
int ret = 0;
for (unsigned int c = 0; c < block_words; c++) {
ret |= contains_zero(&state[c][0]);
for (unsigned int r = 0; r < AES_NR; r++) {
state[c][r] = compute_sbox(state[c][r]);
}
}
return ret;
}
static void shift_row(aes_block_t state, unsigned int block_words) {
aes_block_t new_state;
switch (block_words) {
case 4:
case 6:
for (unsigned int i = 0; i < block_words; ++i) {
new_state[i][0] = state[i][0];
new_state[i][1] = state[(i + 1) % block_words][1];
new_state[i][2] = state[(i + 2) % block_words][2];
new_state[i][3] = state[(i + 3) % block_words][3];
}
break;
case 8:
for (unsigned int i = 0; i < block_words; i++) {
new_state[i][0] = state[i][0];
new_state[i][1] = state[(i + 1) % 8][1];
new_state[i][2] = state[(i + 3) % 8][2];
new_state[i][3] = state[(i + 4) % 8][3];
}
break;
}
for (unsigned int i = 0; i < block_words; ++i) {
memcpy(&state[i][0], &new_state[i][0], AES_NR);
}
}
static void mix_column(aes_block_t state, unsigned int block_words) {
for (unsigned int c = 0; c < block_words; c++) {
bf8_t tmp = bf8_mul(state[c][0], 0x02) ^ bf8_mul(state[c][1], 0x03) ^ state[c][2] ^ state[c][3];
bf8_t tmp_1 =
state[c][0] ^ bf8_mul(state[c][1], 0x02) ^ bf8_mul(state[c][2], 0x03) ^ state[c][3];
bf8_t tmp_2 =
state[c][0] ^ state[c][1] ^ bf8_mul(state[c][2], 0x02) ^ bf8_mul(state[c][3], 0x03);
bf8_t tmp_3 =
bf8_mul(state[c][0], 0x03) ^ state[c][1] ^ state[c][2] ^ bf8_mul(state[c][3], 0x02);
state[c][0] = tmp;
state[c][1] = tmp_1;
state[c][2] = tmp_2;
state[c][3] = tmp_3;
}
}
// Key Expansion functions
static void sub_words(bf8_t* words) {
words[0] = compute_sbox(words[0]);
words[1] = compute_sbox(words[1]);
words[2] = compute_sbox(words[2]);
words[3] = compute_sbox(words[3]);
}
static void rot_word(bf8_t* words) {
bf8_t tmp = words[0];
words[0] = words[1];
words[1] = words[2];
words[2] = words[3];
words[3] = tmp;
}
int expand_key(aes_round_keys_t* round_keys, const uint8_t* key, unsigned int key_words,
unsigned int block_words, unsigned int num_rounds) {
int ret = 0;
for (unsigned int k = 0; k < key_words; k++) {
round_keys->round_keys[k / block_words][k % block_words][0] = bf8_load(&key[4 * k]);
round_keys->round_keys[k / block_words][k % block_words][1] = bf8_load(&key[(4 * k) + 1]);
round_keys->round_keys[k / block_words][k % block_words][2] = bf8_load(&key[(4 * k) + 2]);
round_keys->round_keys[k / block_words][k % block_words][3] = bf8_load(&key[(4 * k) + 3]);
}
for (unsigned int k = key_words; k < block_words * (num_rounds + 1); ++k) {
bf8_t tmp[AES_NR];
memcpy(tmp, round_keys->round_keys[(k - 1) / block_words][(k - 1) % block_words], sizeof(tmp));
if (k % key_words == 0) {
rot_word(tmp);
ret |= contains_zero(tmp);
sub_words(tmp);
tmp[0] ^= round_constants[(k / key_words) - 1];
}
if (key_words > 6 && (k % key_words) == 4) {
ret |= contains_zero(tmp);
sub_words(tmp);
}
unsigned int m = k - key_words;
round_keys->round_keys[k / block_words][k % block_words][0] =
round_keys->round_keys[m / block_words][m % block_words][0] ^ tmp[0];
round_keys->round_keys[k / block_words][k % block_words][1] =
round_keys->round_keys[m / block_words][m % block_words][1] ^ tmp[1];
round_keys->round_keys[k / block_words][k % block_words][2] =
round_keys->round_keys[m / block_words][m % block_words][2] ^ tmp[2];
round_keys->round_keys[k / block_words][k % block_words][3] =
round_keys->round_keys[m / block_words][m % block_words][3] ^ tmp[3];
}
return ret;
}
// Calling Functions
int aes128_init_round_keys(aes_round_keys_t* round_key, const uint8_t* key) {
return expand_key(round_key, key, KEY_WORDS_128, AES_BLOCK_WORDS, ROUNDS_128);
}
int aes192_init_round_keys(aes_round_keys_t* round_key, const uint8_t* key) {
return expand_key(round_key, key, KEY_WORDS_192, AES_BLOCK_WORDS, ROUNDS_192);
}
int aes256_init_round_keys(aes_round_keys_t* round_key, const uint8_t* key) {
return expand_key(round_key, key, KEY_WORDS_256, AES_BLOCK_WORDS, ROUNDS_256);
}
int rijndael192_init_round_keys(aes_round_keys_t* round_key, const uint8_t* key) {
return expand_key(round_key, key, KEY_WORDS_192, RIJNDAEL_BLOCK_WORDS_192, ROUNDS_192);
}
int rijndael256_init_round_keys(aes_round_keys_t* round_key, const uint8_t* key) {
return expand_key(round_key, key, KEY_WORDS_256, RIJNDAEL_BLOCK_WORDS_256, ROUNDS_256);
}
static void load_state(aes_block_t state, const uint8_t* src, unsigned int block_words) {
for (unsigned int i = 0; i != block_words * 4; ++i) {
state[i / 4][i % 4] = bf8_load(&src[i]);
}
}
static void store_state(uint8_t* dst, aes_block_t state, unsigned int block_words) {
for (unsigned int i = 0; i != block_words * 4; ++i) {
bf8_store(&dst[i], state[i / 4][i % 4]);
}
}
static int aes_encrypt(const aes_round_keys_t* keys, aes_block_t state, unsigned int block_words,
unsigned int num_rounds) {
int ret = 0;
// first round
add_round_key(0, state, keys, block_words);
for (unsigned int round = 1; round < num_rounds; ++round) {
ret |= sub_bytes(state, block_words);
shift_row(state, block_words);
mix_column(state, block_words);
add_round_key(round, state, keys, block_words);
}
// last round
ret |= sub_bytes(state, block_words);
shift_row(state, block_words);
add_round_key(num_rounds, state, keys, block_words);
return ret;
}
int aes128_encrypt_block(const aes_round_keys_t* key, const uint8_t* plaintext,
uint8_t* ciphertext) {
aes_block_t state;
load_state(state, plaintext, AES_BLOCK_WORDS);
const int ret = aes_encrypt(key, state, AES_BLOCK_WORDS, ROUNDS_128);
store_state(ciphertext, state, AES_BLOCK_WORDS);
return ret;
}
int aes192_encrypt_block(const aes_round_keys_t* key, const uint8_t* plaintext,
uint8_t* ciphertext) {
aes_block_t state;
load_state(state, plaintext, AES_BLOCK_WORDS);
const int ret = aes_encrypt(key, state, AES_BLOCK_WORDS, ROUNDS_192);
store_state(ciphertext, state, AES_BLOCK_WORDS);
return ret;
}
int aes256_encrypt_block(const aes_round_keys_t* key, const uint8_t* plaintext,
uint8_t* ciphertext) {
aes_block_t state;
load_state(state, plaintext, AES_BLOCK_WORDS);
const int ret = aes_encrypt(key, state, AES_BLOCK_WORDS, ROUNDS_256);
store_state(ciphertext, state, AES_BLOCK_WORDS);
return ret;
}
int rijndael192_encrypt_block(const aes_round_keys_t* key, const uint8_t* plaintext,
uint8_t* ciphertext) {
aes_block_t state;
load_state(state, plaintext, RIJNDAEL_BLOCK_WORDS_192);
const int ret = aes_encrypt(key, state, RIJNDAEL_BLOCK_WORDS_192, ROUNDS_192);
store_state(ciphertext, state, RIJNDAEL_BLOCK_WORDS_192);
return ret;
}
int rijndael256_encrypt_block(const aes_round_keys_t* key, const uint8_t* plaintext,
uint8_t* ciphertext) {
aes_block_t state;
load_state(state, plaintext, RIJNDAEL_BLOCK_WORDS_256);
const int ret = aes_encrypt(key, state, RIJNDAEL_BLOCK_WORDS_256, ROUNDS_256);
store_state(ciphertext, state, RIJNDAEL_BLOCK_WORDS_256);
return ret;
}
void prg(const uint8_t* key, const uint8_t* iv, uint8_t* out, unsigned int seclvl, size_t outlen) {
#if !defined(HAVE_OPENSSL)
uint8_t internal_iv[16];
memcpy(internal_iv, iv, sizeof(internal_iv));
aes_round_keys_t round_key;
switch (seclvl) {
case 256:
aes256_init_round_keys(&round_key, key);
for (; outlen >= 16; outlen -= 16, out += 16) {
aes_block_t state;
load_state(state, internal_iv, AES_BLOCK_WORDS);
aes_encrypt(&round_key, state, AES_BLOCK_WORDS, ROUNDS_256);
store_state(out, state, AES_BLOCK_WORDS);
aes_increment_iv(internal_iv);
}
if (outlen) {
aes_block_t state;
load_state(state, internal_iv, AES_BLOCK_WORDS);
aes_encrypt(&round_key, state, AES_BLOCK_WORDS, ROUNDS_256);
uint8_t tmp[16];
store_state(tmp, state, AES_BLOCK_WORDS);
memcpy(out, tmp, outlen);
}
return;
case 192:
aes192_init_round_keys(&round_key, key);
for (; outlen >= 16; outlen -= 16, out += 16) {
aes_block_t state;
load_state(state, internal_iv, AES_BLOCK_WORDS);
aes_encrypt(&round_key, state, AES_BLOCK_WORDS, ROUNDS_192);
store_state(out, state, AES_BLOCK_WORDS);
aes_increment_iv(internal_iv);
}
if (outlen) {
aes_block_t state;
load_state(state, internal_iv, AES_BLOCK_WORDS);
aes_encrypt(&round_key, state, AES_BLOCK_WORDS, ROUNDS_192);
uint8_t tmp[16];
store_state(tmp, state, AES_BLOCK_WORDS);
memcpy(out, tmp, outlen);
}
return;
default:
aes128_init_round_keys(&round_key, key);
for (; outlen >= 16; outlen -= 16, out += 16) {
aes_block_t state;
load_state(state, internal_iv, AES_BLOCK_WORDS);
aes_encrypt(&round_key, state, AES_BLOCK_WORDS, ROUNDS_128);
store_state(out, state, AES_BLOCK_WORDS);
aes_increment_iv(internal_iv);
}
if (outlen) {
aes_block_t state;
load_state(state, internal_iv, AES_BLOCK_WORDS);
aes_encrypt(&round_key, state, AES_BLOCK_WORDS, ROUNDS_128);
uint8_t tmp[16];
store_state(tmp, state, AES_BLOCK_WORDS);
memcpy(out, tmp, outlen);
}
return;
}
#else
const EVP_CIPHER* cipher;
switch (seclvl) {
case 256:
cipher = EVP_aes_256_ctr();
break;
case 192:
cipher = EVP_aes_192_ctr();
break;
default:
cipher = EVP_aes_128_ctr();
break;
}
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
assert(ctx);
EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv);
static const uint8_t plaintext[16] = {0};
int len = 0;
for (size_t idx = 0; idx < outlen / 16; idx += 1, out += 16) {
EVP_EncryptUpdate(ctx, out, &len, plaintext, sizeof(plaintext));
}
if (outlen % 16) {
EVP_EncryptUpdate(ctx, out, &len, plaintext, outlen % 16);
}
EVP_EncryptFinal_ex(ctx, out, &len);
EVP_CIPHER_CTX_free(ctx);
#endif
}
uint8_t* aes_extend_witness(const uint8_t* key, const uint8_t* in, const faest_paramset_t* params) {
const unsigned int lambda = params->faest_param.lambda;
const unsigned int l = params->faest_param.l;
const unsigned int L_ke = params->faest_param.Lke;
const unsigned int S_ke = params->faest_param.Ske;
const unsigned int num_rounds = params->faest_param.R;
uint8_t* w = malloc((l + 7) / 8);
uint8_t* const w_out = w;
unsigned int block_words = AES_BLOCK_WORDS;
unsigned int beta = 1;
switch (params->faest_paramid) {
case FAEST_192F:
case FAEST_192S:
case FAEST_256F:
case FAEST_256S:
beta = 2;
break;
case FAEST_EM_192F:
case FAEST_EM_192S:
block_words = RIJNDAEL_BLOCK_WORDS_192;
break;
case FAEST_EM_256F:
case FAEST_EM_256S:
block_words = RIJNDAEL_BLOCK_WORDS_256;
break;
default:
break;
}
if (!L_ke) {
// switch input and key for EM
const uint8_t* tmp = key;
key = in;
in = tmp;
}
// Step 3
aes_round_keys_t round_keys;
switch (lambda) {
case 256:
if (block_words == RIJNDAEL_BLOCK_WORDS_256) {
rijndael256_init_round_keys(&round_keys, key);
} else {
aes256_init_round_keys(&round_keys, key);
}
break;
case 192:
if (block_words == RIJNDAEL_BLOCK_WORDS_192) {
rijndael192_init_round_keys(&round_keys, key);
} else {
aes192_init_round_keys(&round_keys, key);
}
break;
default:
aes128_init_round_keys(&round_keys, key);
break;
}
// Step 4
if (L_ke > 0) {
// Key schedule constraints only needed for normal AES, not EM variant.
for (unsigned int i = 0; i != params->faest_param.Nwd; ++i) {
memcpy(w, round_keys.round_keys[i / 4][i % 4], sizeof(aes_word_t));
w += sizeof(aes_word_t);
}
for (unsigned int j = 0, ik = params->faest_param.Nwd; j < S_ke / 4; ++j) {
memcpy(w, round_keys.round_keys[ik / 4][ik % 4], sizeof(aes_word_t));
w += sizeof(aes_word_t);
ik += lambda == 192 ? 6 : 4;
}
} else {
// saving the OWF key to the extended witness
memcpy(w, in, lambda / 8);
w += lambda / 8;
}
// Step 10
for (unsigned b = 0; b < beta; ++b, in += sizeof(aes_word_t) * block_words) {
// Step 12
aes_block_t state;
load_state(state, in, block_words);
// Step 13
add_round_key(0, state, &round_keys, block_words);
for (unsigned int round = 1; round < num_rounds; ++round) {
// Step 15
sub_bytes(state, block_words);
// Step 16
shift_row(state, block_words);
// Step 17
store_state(w, state, block_words);
w += sizeof(aes_word_t) * block_words;
// Step 18
mix_column(state, block_words);
// Step 19
add_round_key(round, state, &round_keys, block_words);
}
// last round is not commited to, so not computed
}
return w_out;
}