-
Notifications
You must be signed in to change notification settings - Fork 0
/
abc.cpp
441 lines (392 loc) · 16.7 KB
/
abc.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
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
/*
This is the C++ implementation of ABC together with the implementation of the presented
Meet-in-the-middle attack on Byte2. This attack takes 2 min on a standard desktop computer.
(compile with: g++ abc.cpp -std=c++14 -Ofast -march=native -fopenmp -Wall -Wextra -Wpedantic)
=> Easier: make / make mitm
*/
#include <iostream>
#include <omp.h>
#include <cstdint>
#include <chrono>
#include <unistd.h>
#include <array>
#include <vector>
#include <algorithm>
#include <sys/random.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef __uint128_t u128;
const unsigned int N_ROUNDS = 8;
using namespace std::chrono;
using namespace std::chrono_literals;
/////////////////////////////////
// Start of ABC implementation //
/////////////////////////////////
u8 SBOX[16] = {0x4, 0xc, 0x0, 0x8, 0x6, 0xe, 0x1, 0xb, 0x9, 0xd, 0x2, 0x5, 0xA, 0xF, 0x3, 0x7};
u8 DOUBLE_SBOX[256] = {
0x44, 0x4c, 0x40, 0x48, 0x46, 0x4e, 0x41, 0x4b, 0x49, 0x4d, 0x42, 0x45, 0x4a, 0x4f, 0x43, 0x47,
0xc4, 0xcc, 0xc0, 0xc8, 0xc6, 0xce, 0xc1, 0xcb, 0xc9, 0xcd, 0xc2, 0xc5, 0xca, 0xcf, 0xc3, 0xc7,
0x04, 0x0c, 0x00, 0x08, 0x06, 0x0e, 0x01, 0x0b, 0x09, 0x0d, 0x02, 0x05, 0x0a, 0x0f, 0x03, 0x07,
0x84, 0x8c, 0x80, 0x88, 0x86, 0x8e, 0x81, 0x8b, 0x89, 0x8d, 0x82, 0x85, 0x8a, 0x8f, 0x83, 0x87,
0x64, 0x6c, 0x60, 0x68, 0x66, 0x6e, 0x61, 0x6b, 0x69, 0x6d, 0x62, 0x65, 0x6a, 0x6f, 0x63, 0x67,
0xe4, 0xec, 0xe0, 0xe8, 0xe6, 0xee, 0xe1, 0xeb, 0xe9, 0xed, 0xe2, 0xe5, 0xea, 0xef, 0xe3, 0xe7,
0x14, 0x1c, 0x10, 0x18, 0x16, 0x1e, 0x11, 0x1b, 0x19, 0x1d, 0x12, 0x15, 0x1a, 0x1f, 0x13, 0x17,
0xb4, 0xbc, 0xb0, 0xb8, 0xb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< 4; i++){
u8 old_byte = (states >> 8*i) & 0xFF;
u8 new_byte = DOUBLE_SBOX[old_byte];
states ^= ((u64) new_byte << (8*i)) ^ ((u64) old_byte << (8*i));
}
return states;
}
u64 S(u64 half_state){
for(int i = 0; i < 8; i++){
u8 old_byte = (half_state >> 8*i) & 0xFF;
u8 new_byte = DOUBLE_SBOX[old_byte];
half_state ^= ((u64) new_byte << (8*i)) ^ ((u64) old_byte << (8*i));
}
return half_state;
}
u32 S_INV(u32 states){
for(int i = 0; i < 4; i++){
u8 old_byte = (states >> 8*i) & 0xFF;
u8 new_byte = DOUBLE_SBOX_INV[old_byte];
states ^= ((u64) new_byte << (8*i)) ^ ((u64) old_byte << (8*i));
}
return states;
}
u64 S_INV(u64 half_state){
for(int i = 0; i < 8; i++){
u8 old_byte = (half_state >> 8*i) & 0xFF;
u8 new_byte = DOUBLE_SBOX_INV[old_byte];
half_state ^= ((u64) new_byte << (8*i)) ^ ((u64) old_byte << (8*i));
}
return half_state;
}
u64 r(u64 half_state){
// order of 64-bit word in patent: byte0 || byte1 || byte2 || ... || byte7
// r: rotate byte i by i to the right
// where bytei = bit0 || bit1 || ... || bit7 = bit0 + 2*bit1 + ... + 128*bit7
// i.e. least significant bit is on the left -> we must shift to the left here
// example from patent: let w = abcdefgh. then r(w, 1) = habcdefg i.e. msb -> lsb
for(int i = 1; i < 8; i++){
u8 old_byte = (half_state >> (8*(7-i))) & 0xFF;
u8 new_byte = (old_byte << i) | (old_byte >> (8-i));
half_state ^= ((u64) (new_byte ^ old_byte)) << (8*(7-i));
}
return half_state;
}
// self inverse
u64 R(u64 half_state){
// order of 64-bit word in patent: byte0 || byte1 || byte2 || ... || byte7
/*
|0 1| |6 5|
|2 3| |2 7|
|4 5| -> |4 1|
|6 7| |0 3|
|*/
u64 byte2_4 = half_state & 0x0000FF00FF000000;
u64 byte1_5_3_7 = ((half_state << 32) | (half_state >> 32)) & 0x00FF00FF00FF00FF;
u64 byte0 = (half_state >> 48) & 0x000000000000FF00;
u64 byte6 = (half_state << 48) & 0xFF00000000000000;
return byte2_4 ^ byte1_5_3_7 ^ byte0 ^ byte6;
}
u32 BS(u32 states){
for(int i = 0; i < 4; i++){
u8 old_byte = (states >> 8*i) & 0xFF;
u8 new_byte = BS_BOX[old_byte];
states ^= ((u64) (new_byte ^ old_byte)) << (8*i);
}
return states;
}
u64 BS(u64 half_state){
for(int i = 0; i < 8; i++){
u8 old_byte = (half_state >> 8*i) & 0xFF;
u8 new_byte = BS_BOX[old_byte];
half_state ^= ((u64) (new_byte ^ old_byte)) << (8*i);
}
return half_state;
}
u32 BS_inv(u32 states){
for(int i = 0; i < 4; i++){
u8 old_byte = (states >> 8*i) & 0xFF;
u8 new_byte = BS_BOX_INV[old_byte];
states ^= ((u64) (new_byte ^ old_byte)) << (8*i);
}
return states;
}
u64 BS_inv(u64 half_state){
for(int i = 0; i < 8; i++){
u8 old_byte = (half_state >> 8*i) & 0xFF;
u8 new_byte = BS_BOX_INV[old_byte];
half_state ^= ((u64) (new_byte ^ old_byte)) << (8*i);
}
return half_state;
}
u64 RotateXor(u64 half_state, u8 i){
u32 left = (half_state >> 32);
u32 right = (u32) half_state;
u8 a = (13+i) & 0x1F;
u8 b = (29+i) & 0x1F;
left = left ^ ((left << a) | (left >> (32-a)));
right = right ^ ((right << b) | (right >> (32-b)));
return ((u64) left << 32) ^ right;
}
u128 round(u128 state, u64 round_key){
u64 left = state >> 64; u64 right = (u64) state;
left = R(left);
left = left ^ r(S(right ^ round_key));
left = BS(left);
state = ((u128) right << 64) | left;
return state;
}
u128 round_inv(u128 state, u64 round_key){
u64 left = state >> 64; u64 right = (u64) state;
right = BS_inv(right);
right = right ^ r(S(left ^ round_key));
right = R(right);
state = ((u128) right << 64) | left;
return state;
}
u128 key_schedule_round(u128 state, u8 i){
u64 left = state >> 64; u64 right = state & 0xFFFFFFFFFFFFFFFF;
left = left ^ R(RotateXor(S(right), i));
left = BS(left);
state = ((u128) right << 64) | left;
return state;
}
void key_schedule(u64* RK, u128 key){
u128 state = key;
for(u32 i = 0; i < N_ROUNDS; i++){
RK[i] = (u64) state;
state = key_schedule_round(state, i);
}
}
u128 encrypt(u128 plaintext, u128 key){
u64 RK[N_ROUNDS] = {0x0};
key_schedule(RK, key);
u128 state = plaintext;
for(u32 i = 0; i < N_ROUNDS; i++){
state = round(state, RK[i]);
}
return state;
}
u128 decrypt(u128 ciphertext, u128 key){
u64 RK[N_ROUNDS] = {0x0};
key_schedule(RK, key);
u128 state = ciphertext;
for(u32 i = 0; i < N_ROUNDS; i++){
state = round_inv(state, RK[N_ROUNDS-i-1]);
}
return state;
}
///////////////////////////////
// END of ABC implementation //
///////////////////////////////
struct pair_t{
u128 p; // plaintext
u128 c; // ciphertext
};
// meet-in-the-middle-attack on 8-round ABC
u64 byte2_forward(u64 plaintexts, u8 *RK){
// plaintext: 4 16-bit plaintexts corresponding to byte 2 parts
u32 lefts = plaintexts >> 32;
u32 rights = (u32) plaintexts;
for(int i = 0; i < 3; i++){
u32 rk = ((u32) RK[i] << 24) ^ ((u32) RK[i] << 16) ^ ((u32) RK[i] << 8) ^ RK[i];
u32 mid = S(rights ^ rk);
mid = ((mid << 2) & 0xFCFCFCFC) ^ ((mid >> 6) & 0x03030303);
lefts = lefts ^ mid;
lefts = BS(lefts);
std::swap(lefts, rights);
}
// output: L3s and L4s = R3s
return ((u64) lefts << 32) | rights;
}
u64 byte2_backward(u64 ciphertexts, u8 *RK){
u32 lefts = ciphertexts >> 32;
u32 rights = (u32) ciphertexts;
for(int i = 0; i < 4; i++){
u32 rk = ((u32) RK[i] << 24) ^ ((u32) RK[i] << 16) ^ ((u32) RK[i] << 8) ^ RK[i];
rights = BS_inv(rights);
u32 mid = S(lefts ^ rk);
mid = ((mid << 2) & 0xFCFCFCFC) ^ ((mid >> 6) & 0x03030303);
rights = rights ^ mid;
std::swap(lefts, rights);
}
return ((u64) lefts << 32) | rights;
}
bool comp(u64 a, u64 b){
return (a >> 56) < (b >> 56);
}
void mitm(){
u128 k = 0;
int error = getentropy(&k, 16);
std::cout << "key = 0x" << std::hex << ((u64) (k >> 64)) << ((u64) k) << std::endl;
u64 RK[N_ROUNDS] = {0x0};
key_schedule(RK, k);
std::cout << "Correct round key bytes:" << std::endl;
for(u32 i = 0; i < N_ROUNDS; i++){
std::cout << "Round " << std::dec << i << ": 0x" << std::hex << ((RK[i] >> 40) & 0xFF) << std::endl;
}
// generate challenge pairs
pair_t PAIRS[4];
for(int i = 0; i < 4; i++){
u128 p = 0;
error |= getentropy(&p, 16);
u128 c = encrypt(p, k);
PAIRS[i].p = p;
PAIRS[i].c = c;
}
if(error) std::cout << "BAD RNG" << std::endl;
// build table
auto start = steady_clock::now();
std::array<std::vector<u64>, (1 << 24)> *T = new std::array<std::vector<u64>, (1 << 24)>;
u64 plaintexts = 0;
for(int i = 0; i < 4; i++){
plaintexts |= ((PAIRS[i].p >> 104) & 0xFF) << (32 + (8 * i));
plaintexts |= ((PAIRS[i].p >> 40) & 0xFF) << (8 * i);
}
for(u64 keys = 0; keys < 0x1000000; keys++){
u8 rk0 = (keys >> 16) & 0xFF;
u8 rk1 = (keys >> 8) & 0xFF;
u8 rk2 = (keys >> 0) & 0xFF;
u8 RK[3] = {rk0, rk1, rk2};
u64 states_3 = byte2_forward(plaintexts, RK);
u32 L3s = states_3 >> 32;
u32 L4s = (u32) states_3;
(*T)[L4s >> 8].push_back((((u64) L4s & 0xFF) << 56) | (keys << 32) | L3s);
}
for(int j = 0; j < (1<<24); j++){
std::sort((*T)[j].begin(), (*T)[j].end(), comp);
}
auto stop = steady_clock::now();
auto duration = duration_cast<seconds>(stop - start);
std::cout << "Generating table took " << std::dec << duration.count() << "s" << std::endl;
// find key
start = steady_clock::now();
u64 ciphertexts = 0;
for(int i = 0; i < 4; i++){
ciphertexts |= ((PAIRS[i].c >> 104) & 0xFF) << (32 + (8 * i));
ciphertexts |= ((PAIRS[i].c >> 40) & 0xFF) << (8 * i);
}
#pragma omp parallel for
for(u64 keys = 0; keys < 0x100000000; keys++){
if((keys & 0x1FFFFFF) == 0) std::cout << "#" << std::flush;
u8 rk4 = (keys >> 24) & 0xFF;
u8 rk5 = (keys >> 16) & 0xFF;
u8 rk6 = (keys >> 8) & 0xFF;
u8 rk7 = (keys >> 0) & 0xFF;
u8 rk[4] = {rk7, rk6, rk5, rk4};
u64 states_4 = byte2_backward(ciphertexts, rk);
auto it = std::lower_bound((*T)[(states_4 >> 40)].begin(), (*T)[(states_4 >> 40)].end(), (((states_4 >> 32) & 0xFF) << 56), comp);
if(it == (*T)[(states_4 >> 40)].end()) continue;
if (((*it) >> 56) == ((states_4 >> 32) & 0xFF)){
u32 L3s = (u32) *it;
u32 L4s = states_4 >> 32;
u32 R4s = (u32) states_4;
R4s = BS_inv(R4s);
R4s = R4s ^ L3s;
// undo r
R4s = ((R4s >> 2) & 0x3F3F3F3F) ^ ((R4s << 6) & 0xC0C0C0C0);
R4s = S_INV(R4s);
u32 rk3 = R4s ^ L4s;
u8 rk3_0 = (rk3 >> 24) & 0xFF;
u8 rk3_1 = (rk3 >> 16) & 0xFF;
u8 rk3_2 = (rk3 >> 8) & 0xFF;
u8 rk3_3 = rk3 & 0xFF;
if(!((rk3_0 == rk3_1) && (rk3_1 == rk3_2) && (rk3_2 == rk3_3))) continue;
u8 rk0 = ((*it) >> 48) & 0xFF;
u8 rk1 = ((*it) >> 40) & 0xFF;
u8 rk2 = ((*it) >> 32) & 0xFF;
u64 rks = ((u64) rk0 << 56) | ((u64) rk1 << 48) | ((u64) rk2 << 40) | ((u64) rk3_0 << 32) | ((u64) rk4 << 24) | ((u64) rk5 << 16) | ((u64) rk6 << 8) | ((u64) rk7 << 0);
std::cout << "Recovered round key bytes: 0x" << std::hex << rks << std::endl;
}
}
stop = steady_clock::now();
duration = duration_cast<seconds>(stop - start);
std::cout << "Finding key took another " << std::dec << duration.count() << "s" << std::endl;
// we expect one wrong candidate
// about 100s for full keyspaces -> on average less than 1min to find the key
}
int main(){
u128 p = 0;
u128 k = 0;
u128 c = encrypt(p, k);
u64 left = c >> 64;
u64 right = (u64) c;
std::cout << std::hex << left << right << std::endl;
p = decrypt(c, k);
left = p >> 64;
right = (u64) p;
std::cout << std::hex << left << right << std::endl;
c = encrypt(p ^ ((u128) 1 << 104), k);
left = c >> 64;
right = (u64) c;
std::cout << std::hex << left << right << std::endl;
mitm();
return 0;
}