-
Notifications
You must be signed in to change notification settings - Fork 112
/
testing.cpp
656 lines (534 loc) · 19.5 KB
/
testing.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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//============================================================================
// Name : MGR_NTL.cpp
// Author : Dusan Klinec (ph4r05)
// Version :
// Copyright : Your copyright notice
// Description : Tests for implemented routines. Tests MixingBijection generator,
// A1A2 relations generator and BGE attack
// * Author: Dusan Klinec (ph4r05)
// *
// * License: GPLv3 [http://www.gnu.org/licenses/gpl-3.0.html]
//============================================================================
#include "testing.h"
// hardcoded elements
// http://stackoverflow.com/questions/2236197/c-easiest-way-to-initialize-an-stl-vector-with-hardcoded-elements
NTL_CLIENT
using namespace std;
using namespace NTL;
using namespace boost;
using namespace wbacr;
using namespace wbacr::laeqv;
using namespace wbacr::attack;
#define GENERIC_AES_DEBUG 1
int main(void) {
// very poor PRNG seeding, but just for now
time_t start, end;
GF2X defaultModulus = GF2XFromLong(0x11B, 9);
GF2E::init(defaultModulus);
GenericAES defAES;
defAES.init(0x11B, 0x03);
defAES.printAll();
WBAESGenerator generator;
WBAES * genAES = new WBAES;
// Test WB AES with test vectors.
// This test also demonstrates usage of external encodings by wrapping AES
generator.testWithVectors(true, genAES);
// Invert test
BGEAttack atk;
// BGE attack
time(&start);
cout << "Starting an attack! Obj: " << endl;
atk.run();
time(&end);
cout << "Computation took: " << (end - start) << " seconds." << endl;
delete genAES;
exit(3);
// Cipher inversion - works only without external encodings,
// more specifically, big diffusion matrix has to be I_{128}
//cout << "Testing cipher inversion;" << endl;
//atk.invertCipherTest();
//exit(3);
//generator.useDualAESIdentity=true;
//generator.useIO04x04Identity=true;
//generator.useIO08x08Identity=true;
//generator.useMB08x08Identity=true;
//generator.useMB32x32Identity=true;
//int errors = generator.testWithVectors(true, genAES);
//cout << "Testing done, errors: " << errors << endl;
//exit(3);
//mat_GF2 m = defAES.makeMultAMatrix(0x44);
//mat_GF2 minv = inv(m);
//mat_GF2 t1 = defAES.makeSquareMatrix(0x1);
//dumpMatrix(t1);
//mat_GF2 t1inv = inv(t1);
//dumpMatrix(t1inv);
//mat_GF2 t2 = defAES.makeSquareMatrix(7);
//dumpMatrix(t2);
//mat_GF2 res = m*t1;
//dumpMatrix(res);
//res = t1inv*minv;
//dumpMatrix(res);
//res = m*t1*t1inv*minv;
//dumpMatrix(res);
exit(4);
//dualAESTest();
//exit(3);
//A1A2relationsGenerator();
//exit(2);
//AESAffineRelationsVerify(true);
//exit(3);
//MBgen();
//exit(3);
return 0;
}
int A1A2relationsGenerator(void){
GF2X defaultModulus = GF2XFromLong(0x11B, 9);
GF2E::init(defaultModulus);
// unordered_map
a1a2map a1store;
a1a2map a2store;
ofstream dump;
ofstream dumpA;
dump.open("/media/share/AES_A1A2dump.txt");
dumpA.open("/media/share/AES_signature.txt");
dump << "id;polynomial;generator;qq;ii;problems;A1;A2" << endl;
dumpA << "polynomial;generator;sbox;sboxinv;mixcol;mixcolinv" << endl;
GenericAES defAES;
defAES.init(0x11B, 0x03);
defAES.printAll();
int AES_gen, AES_poly;
for(AES_poly=0; AES_poly < AES_IRRED_POLYNOMIALS; AES_poly++){
for(AES_gen=0; AES_gen < AES_GENERATORS; AES_gen++){
GenericAES dualAES;
dualAES.initFromIndex(AES_poly, AES_gen);
// write to file
dumpA << CHEX(GenericAES::irreduciblePolynomials[AES_poly]) << ";"
<< CHEX(GenericAES::generators[AES_poly][AES_gen]) << ";";
dumpVector(dumpA, dualAES.sboxAffineGF2E, 256); dumpA << ";";
dumpVector(dumpA, dualAES.sboxAffineInvGF2E, 256); dumpA << ";";
dumpMatrix(dumpA, dualAES.mixColMat); dumpA << ";";
dumpMatrix(dumpA, dualAES.mixColInvMat); dumpA << ";";
dumpA << endl;
dumpA.flush();
cout << "+";
int ii=0,qq=0,probAll=0;
for(qq=0;qq<8; qq++){
cout << ".";
for(ii=1;ii<256;ii++){
unsigned long int id = (AES_poly << 24) | (AES_gen << 16) | (qq << 8) | ii;
int problems=0;
vec_GF2E A1;
vec_GF2E A2;
dualAES.generateA1A2Relations(A1, A2, ii, qq);
problems = dualAES.testA1A2Relations(A1, A2);
// write to file
dump << CHEX(id) << ";"
<< CHEX(GenericAES::irreduciblePolynomials[AES_poly]) << ";"
<< CHEX(GenericAES::generators[AES_poly][AES_gen]) << ";"
<< qq << ";" << ii << ";" << problems << ";";
dumpVector(dump, A1);
dump << ";";
dumpVector(dump, A2);
dump << endl;
if (problems>0){
cout << "!!!Current Dual AES: "
<< CHEX(GenericAES::irreduciblePolynomials[AES_poly]) << ";"
<< CHEX(GenericAES::generators[AES_poly][AES_gen]) << endl;
cout << "!!!Problem with relations ii="<<ii<<"; qq="<<qq<<"; problems=" << problems << endl;
probAll+=1;
}
//
// A1 A2 duplicity check via unordered hashed structure
//
a1a2rec rec;
rec.count=1;
rec.id = id;
rec.vec = NULL;
std::string a1hash = hashLookupTable(A1);
std::string a2hash = hashLookupTable(A2);
// A1 hashing
if (a1store.count(a1hash)==0){
a1store[a1hash] = rec; // new A1 hash
} else {
a1a2rec &trec = a1store.at(a1hash); // already contains some record
trec.count++;
if (trec.vec==NULL) trec.vec = new vector<unsigned long int>();
trec.vec->push_back(id);
if (false)
cout << "A1: Spoiler, duplicate of ["
<< CHEX8(id) << ";"
<< CHEX(GenericAES::irreduciblePolynomials[AES_poly]) << ";"
<< CHEX(GenericAES::generators[AES_poly][AES_gen]) << ";"
<< qq << ";" << ii << ";] collide with ["
<< CHEX8(trec.id) << ";"
<< CHEX(GenericAES::irreduciblePolynomials[(trec.id >> 24) & 0xff]) << ";"
<< CHEX(GenericAES::generators[(trec.id >> 24) & 0xff][(trec.id >> 16) & 0xff]) << ";"
<< ((trec.id >> 16) & 0xff) << ";" << ((trec.id) & 0xff) << ";] count="
<< trec.count << endl;
}
// A2 hashing
if (a2store.count(a2hash)==0){
a2store[a2hash] = rec; // new A2 hash
} else {
a1a2rec &trec = a2store.at(a2hash); // already contains some record
trec.count++;
if (trec.vec==NULL) trec.vec = new vector<unsigned long int>();
trec.vec->push_back(id);
cout << "A2: Spoiler, duplicate of ["
<< CHEX8(id) << ";"
<< CHEX(GenericAES::irreduciblePolynomials[AES_poly]) << ";"
<< CHEX(GenericAES::generators[AES_poly][AES_gen]) << ";"
<< qq << ";" << ii << ";] collide with ["
<< CHEX8(trec.id) << ";"
<< CHEX(GenericAES::irreduciblePolynomials[(trec.id >> 24) & 0xff]) << ";"
<< CHEX(GenericAES::generators[(trec.id >> 24) & 0xff][(trec.id >> 16) & 0xff]) << ";"
<< ((trec.id >> 16) & 0xff) << ";" << ((trec.id) & 0xff) << ";] count="
<< trec.count << endl;
}
}
}
// force write
dump.flush();
}
}
//
// Hashing completed, harvest our results...
//
dumpA << "======================================================" << endl << "CollisionsA1" << endl;
BOOST_FOREACH( a1a2map::value_type & v, a1store ) {
if (v.second.count==1) continue;
a1a2rec &trec = v.second;
dumpA << "colisions=" << trec.count << " ["
<< CHEX8(trec.id) << ";"
<< CHEX(GenericAES::irreduciblePolynomials[(trec.id >> 24) & 0xff]) << ";"
<< CHEX(GenericAES::generators[(trec.id >> 24) & 0xff][(trec.id >> 16) & 0xff]) << ";"
<< ((trec.id >> 16) & 0xff) << ";" << ((trec.id) & 0xff) << "] ";
vector<unsigned long int> * pvec = trec.vec;
for(std::vector<unsigned long int>::iterator it = pvec->begin(); it != pvec->end(); ++it) {
unsigned long int id = *it;
dumpA << "["
<< CHEX8(id) << ";"
<< CHEX(GenericAES::irreduciblePolynomials[(id >> 24) & 0xff]) << ";"
<< CHEX(GenericAES::generators[(id >> 24) & 0xff][(id >> 16) & 0xff]) << ";"
<< ((id >> 16) & 0xff) << ";" << ((id) & 0xff) << "] ";
}
dumpA << endl;
}
dumpA << "======================================================" << endl << "CollisionsA2" << endl;
BOOST_FOREACH( a1a2map::value_type & v, a2store ) {
if (v.second.count==1) continue;
a1a2rec &trec = v.second;
dumpA << "colisions=" << trec.count << " ["
<< CHEX8(trec.id) << ";"
<< CHEX(GenericAES::irreduciblePolynomials[(trec.id >> 24) & 0xff]) << ";"
<< CHEX(GenericAES::generators[(trec.id >> 24) & 0xff][(trec.id >> 16) & 0xff]) << ";"
<< ((trec.id >> 16) & 0xff) << ";" << ((trec.id) & 0xff) << "] ";
vector<unsigned long int> * pvec = trec.vec;
for(std::vector<unsigned long int>::iterator it = pvec->begin(); it != pvec->end(); ++it) {
unsigned long int id = *it;
dumpA << "["
<< CHEX8(id) << ";"
<< CHEX(GenericAES::irreduciblePolynomials[(id >> 24) & 0xff]) << ";"
<< CHEX(GenericAES::generators[(id >> 24) & 0xff][(id >> 16) & 0xff]) << ";"
<< ((id >> 16) & 0xff) << ";" << ((id) & 0xff) << "] ";
}
dumpA << endl;
}
dumpA.close();
dump.close();
return 0;
}
int dualAESTest(void){
GF2X defaultModulus = GF2XFromLong(0x11B, 9);
GF2E::init(defaultModulus);
GenericAES defAES;
defAES.init(0x11B, 0x03);
defAES.printAll();
defAES.testWithVectors();
GenericAES dualAES;
dualAES.init(0x11D, 0x9d);
//dualAES.initFromIndex(15,5);
dualAES.printAll();
dualAES.testWithVectors();
// try round key expansion
vec_GF2E roundKey;
vec_GF2E key;
key.SetLength(128);
dualAES.expandKey(roundKey, key, KEY_SIZE_16);
cout << "Round key for ZERO key for 16B: " << endl;
dumpVector(roundKey);
mat_GF2E state(INIT_SIZE, 4, 4);
cout << "Plaintext: " << endl;
dumpMatrix(state);
cout << "Testing encryption: " << endl;
dualAES.encryptInternal(state, roundKey);
dumpMatrix(state);
dualAES.applyTinv(state);
cout << "Testing encryption AFTER Tinv: " << endl;
dumpMatrix(state);
dualAES.applyT(state);
cout << "Testing backward decryption: " << endl;
dualAES.decryptInternal(state, roundKey);
dumpMatrix(state);
cout << "Multiplication matrix: " << endl;
mat_GF2 multA= dualAES.makeMultAMatrix(2);
dumpMatrix(multA);
cout << "Squaring matrix: " << endl;
mat_GF2 sqr = dualAES.makeSquareMatrix(1);
dumpMatrix(sqr);
cout << "A1 and A2 relations, testing all possible" << endl;
int ii=0,qq=0,probAll=0;
for(qq=0;qq<8; qq++){
for(ii=1;ii<256;ii++){
int problems=0;
vec_GF2E A1;
vec_GF2E A2;
dualAES.generateA1A2Relations(A1, A2, ii, qq);
problems = dualAES.testA1A2Relations(A1, A2, true, false);
if (problems!=0){
cout << "Problem with ENC relations ii="<<ii<<"; qq="<<qq<<"; problems=" << problems << endl;
probAll+=1;
}
problems = dualAES.testA1A2Relations(A2, A1, false, false);
if (problems!=0){
cout << "Problem with DEC relations ii="<<ii<<"; qq="<<qq<<"; problems=" << problems << endl;
probAll+=1;
}
}
}
cout << "All relations tested, problemsAll = " << probAll << endl;
vec_GF2E A1;
vec_GF2E A2;
dualAES.generateA1A2Relations(A1, A2, 1+(phrand() % 0xfe), phrand() % 7);
cout << "Testing relations A1 A2: Problems = " << dualAES.testA1A2Relations(A1, A2) << endl;
cout << "A1: " << endl;
dumpVector(A1);
cout << "A2: " << endl;
dumpVector(A2);
cout << "Generating random bijections: " << endl;
vec_GF2X rndB;
vec_GF2X rndBinv;
generateRandomBijection(rndB, rndBinv, AES_FIELD_SIZE, AES_FIELD_DIM);
dumpVector(rndB);
dumpVector(rndBinv);
return 0;
}
int MBgen(void){
long i,j;
// sample matrix stuff
mat_GF2 A;
A.SetDims(QSIZE, QSIZE);
cout << "I will show you a nice matrix: " << endl << A << endl << endl;
// now generate invertible matrix
i = generateInvertiblePM(A, QSIZE);
if (i>=0){
cout << "found invertible matrix in [" << i << "] iterations: " << endl << A << endl << endl;
} else {
cout << "Invertible matrix was not found" << endl;
}
// Now try bigger matrix - mixing bijection 8x8
i = generateInvertiblePM(A, 8);
if (i>=0){
cout << "found invertible matrix in [" << i << "] iterations: " << endl << A << endl << endl;
} else {
cout << "Invertible matrix was not found" << endl;
}
// now try our Gauss method returning modification matrix P
mat_GF2 B;
B.SetDims(QSIZE, QSIZE);
long const Bdata[][4] = {
{1, 0, 1, 0},
{1, 1, 0, 0},
{0, 0, 1, 1},
{0, 0, 1, 1}
};
initMatrix(B, (long *)Bdata);
cout << "My custom init matrix: " << endl << B << endl << endl;
cout << "Now trying to find inverse: " << endl;
GF2 d;
ref_GF2 dd(d);
mat_GF2 P;
mat_GF2 Q;
i = invP(dd,P,Q,B);
cout << "Determinant returned: " << d << "; Rank = " << i;
cout << "; P matrix: " << endl << P << endl << endl;
cout << "; Q matrix: " << endl << Q << endl << endl;
cout << "; R matrix: " << endl << (P*B*Q) << endl << endl;
// A matrix generation
mat_GF2 Amat;
generateARankMatrix(Amat, 1, 8);
cout << "Generating A matrix, r=1, p=8" << endl << Amat << endl <<endl;
generateARankMatrix(Amat, 2, 8);
cout << "Generating A matrix, r=2, p=8" << endl << Amat << endl <<endl;
generateARankMatrix(Amat, 3, 8);
cout << "Generating A matrix, r=3, p=8" << endl << Amat << endl <<endl;
generateARankMatrix(Amat, 4, 8);
cout << "Generating A matrix, r=4, p=8" << endl << Amat << endl <<endl;
generateARankMatrix(Amat, 5, 8);
cout << "Generating A matrix, r=5, p=8" << endl << Amat << endl <<endl;
mat_GF2 MB;
// simple mixing bijection invertibility test, 100 iterations
for(j=0; j<1000; j++){
generateMixingBijection(MB, 32, 4);
mat_GF2 MBinv;
inv(MBinv, MB);
cout << "## Test passed: " << j << endl;
cout << "MB: " << MB << endl << endl;
}
return EXIT_SUCCESS;
}
//
// Find affine equivalences for Sboxes for default AES
//
int AESAffineRelationsVerify(bool inverseSbox){
int i;
unordered_set<std::string> hashes;
GenericAES defAES;
defAES.init(0x11B, 0x03);
LinearAffineEq eqCheck;
eqCheck.setDimension(8);
eqCheck.verbosity=0;
eqCheck.verbosityAffine=1;
eqCheck.randomizeXGuess=false;
bsetElem S2[256];
bsetElem S2inv[256];
bsetElem S1[256];
bsetElem S1inv[256];
for(i=0; i<256; i++) {
S1[i] = (!inverseSbox) ? defAES.sboxAffine[i] : defAES.sboxAffineInv[i];
S2[i] = (!inverseSbox) ? defAES.sboxAffine[i] : defAES.sboxAffineInv[i];
S1inv[S1[i]] = i;
S2inv[S2[i]] = i;
}
// Print out some usefull information
cout << "#" << endl
<< "# Finding affine equivalences between" << endl;
if (inverseSbox) cout << "# Sinv(x + b) ~~ Sinv(x) + a" << endl;
else cout << "# S(x + a) ~~ S(x) + b" << endl;
cout
<< "#" << endl
<< "# new iteration of a value (a=3):" << endl
<< "# +++++++++++++++++++++++++++++ @@[ 3]" << endl
<< "# new iteration of a value (b=8):" << endl
<< "# ........................... ##[ 8]" << endl
<< "#" << endl
<< "# Idx to matrix correspondence:" << endl
<< "# +-------------------------------------------------------+" << endl
<< "# | 0 | 1 | 2 | 3 | 4 | 5 |" << endl
<< "# | mQM | mMQ | mAQMAinv | mAinvQMA | mAMQAinv | mAinvMQA |" << endl
<< "# +-------------------------------------------------------+" << endl
<< "#" << endl << endl;
//
// Generate ordinary matrix representation & find equivalences, for each matrix, generate hashes
//
AESAffineMap amap;
int j, k, l, m;
mat_GF2 tmpMat1, tmpMat2, tmpMat3, tmpMat4;
// 5. S-BOX with affine mappings. At first obtain default form of affine transformation for normal AES
mat_GF2 tmpSboxAffMatrix(INIT_SIZE, AES_FIELD_DIM, AES_FIELD_DIM); // T * Affine * Tinv
mat_GF2 tmpSboxAffConst(INIT_SIZE, AES_FIELD_DIM, 1); // column vector
mat_GF2 tmpSboxAffMatrixDec(INIT_SIZE, AES_FIELD_DIM, AES_FIELD_DIM); // T * AffineDec * Tinv
mat_GF2 tmpSboxAffConstDec(INIT_SIZE, AES_FIELD_DIM, 1); // column dec vector
tmpSboxAffMatrix = defAES.getDefaultAffineMatrix();
tmpSboxAffConst = colVector(defAES.getDefaultAffineConst());
tmpSboxAffMatrixDec = defAES.getDefaultAffineMatrixDec();
tmpSboxAffConstDec = colVector(defAES.getDefaultAffineConstDec());
// prepare squaring matrices
cout << "Generating square matrices ..." << endl;
mat_GF2 squares[8];
for(j=0; j<8; j++){
squares[j] = defAES.makeSquareMatrix(j);
}
// mult & squares
cout << "Generating multiplication & squared matrices ..." << endl;
for(k=1; k<256; k++){
tmpMat2 = defAES.makeMultAMatrix(k);
cout << '+';
cout.flush();
for(j=0; j<8; j++){
cout << '.';
cout.flush();
smap mQM, mMQ, mAQMAinv, mAinvQMA, mAMQAinv, mAinvMQA;
tmpMat1 = squares[j];
mat_GF2 QM = tmpMat1 * tmpMat2;
mat_GF2 MQ = tmpMat2 * tmpMat1;
eqCheck.buildLookupTableAndCheck(QM, 0, mQM);
eqCheck.buildLookupTableAndCheck(MQ, 0, mMQ);
// Now generate transformation for L1
for(l=0; l<AES_FIELD_SIZE; l++){
GF2E tmpElem = GF2EFromLong(l, AES_FIELD_DIM);
GF2E transformedElem;
mat_GF2 resMatrix;
// mAQMAinv
resMatrix = tmpSboxAffMatrix*(QM * ((tmpSboxAffMatrixDec * colVector(tmpElem, AES_FIELD_DIM)) + tmpSboxAffConstDec)) + tmpSboxAffConst;
colVector(transformedElem, resMatrix, 0);
mAQMAinv[l] = (bsetElem) getLong(transformedElem);
// mAMQAinv
resMatrix = tmpSboxAffMatrix*(MQ * ((tmpSboxAffMatrixDec * colVector(tmpElem, AES_FIELD_DIM)) + tmpSboxAffConstDec)) + tmpSboxAffConst;
colVector(transformedElem, resMatrix, 0);
mAMQAinv[l] = (bsetElem) getLong(transformedElem);
// mAinvQMA
resMatrix = tmpSboxAffMatrixDec*(QM * ((tmpSboxAffMatrix * colVector(tmpElem, AES_FIELD_DIM)) + tmpSboxAffConst)) + tmpSboxAffConstDec;
colVector(transformedElem, resMatrix, 0);
mAinvQMA[l] = (bsetElem) getLong(transformedElem);
// mAinvMQA
resMatrix = tmpSboxAffMatrixDec*(MQ * ((tmpSboxAffMatrix * colVector(tmpElem, AES_FIELD_DIM)) + tmpSboxAffConst)) + tmpSboxAffConstDec;
colVector(transformedElem, resMatrix, 0);
mAinvMQA[l] = (bsetElem) getLong(transformedElem);
}
//mQM, mMQ, mAQMAinv, mAinvQMA, mAMQAinv, mAinvMQA
std::string mhash[6] = {
LinearAffineEq::hashSmap(mQM), LinearAffineEq::hashSmap(mMQ),
LinearAffineEq::hashSmap(mAQMAinv), LinearAffineEq::hashSmap(mAinvQMA),
LinearAffineEq::hashSmap(mAMQAinv), LinearAffineEq::hashSmap(mAinvMQA) };
for (m=0; m<6; m++){
AESAffineElement nEl;
nEl.square = j;
nEl.multi = k;
nEl.type=m;
amap.insert(AESAffineMapElem(mhash[m], nEl));
}
}
}
cout << endl << "Done, starting affine relations finder" << endl;
// Launch main affine equivalences finding algorithm
affineEquivalencesList list;
return eqCheck.findAffineEquivalences(S1, S1inv, S2, S2inv, &list, inverseSbox, &AffCallbackCorrespondence, &amap);
}
//
// Find affine relations correspondence for AES Sboxes
//
int AffCallbackCorrespondence(wbacr::laeqv::affineEquiv_t * el, wbacr::laeqv::affineEquivalencesList * lish, boost::unordered_set<std::string> * hashes, wbacr::laeqv::LinearAffineEq * eqCheck, void * usrData){
std::string hashL1 = LinearAffineEq::hashSmap(el->L1);
std::string hashL2 = LinearAffineEq::hashSmap(el->L2);
std::string totalHash = hashL1;
totalHash.append(";").append(hashL2);
// try to determine form of the matrix
// In inversion case, S2 should be linear, try several options
std::string lhash[2] = {hashL1, hashL2};
std::string L1str = dumpMatrix2str(el->linPart.Ta, false);
std::string L2str = dumpMatrix2str(el->linPart.Tbinv, false);
// convert usrData to map
AESAffineMap * amap = (AESAffineMap * ) usrData;
int n=0;
for(n=0; n<2; n++){
if (amap->count(lhash[n])>0){
auto its = amap->equal_range(lhash[n]);
for (auto it = its.first; it != its.second; ++it) {
cout << "We have match! L"<<(n+1)
<<" ~ idx="<<(it->second.type)
<<" ; [a]="<< setw(2) << (it->second.multi)
<<" ; Q^i="<<(it->second.square)
<<" ; L1 const=" << (el->a)
<<" ; L2 const=" << (el->b) << endl;
if (el->a==0 && el->b==0){
cout << "L1 matrix: " << endl;
dumpMatrix(el->linPart.Ta);
cout << "L2 matrix: " << endl;
dumpMatrix(el->linPart.Tbinv);
}
}
}
}
return 0;
}