forked from NoahBz/Easy-BigInt
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBinaryCipherTest.cpp
More file actions
742 lines (632 loc) · 25.3 KB
/
Copy pathBinaryCipherTest.cpp
File metadata and controls
742 lines (632 loc) · 25.3 KB
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*
MIT License
Copyright (c) 2024-2050 Twilight-Dream & With-Sky
https://github.com/Twilight-Dream-Of-Magic/
https://github.com/With-Sky
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "BinaryCipherTest.hpp"
using BigInteger = TwilightDream::BigInteger::BigInteger;
/*
This byte-substitution box: Strict avalanche criterion is satisfied !
ByteDataSecurityTestData Transparency Order Is: 7.85956
ByteDataSecurityTestData Nonlinearity Is: 112
ByteDataSecurityTestData Propagation Characteristics Is: 8
ByteDataSecurityTestData Delta Uniformity Is: 4
ByteDataSecurityTestData Robustness Is: 0.984375
ByteDataSecurityTestData Signal To Noise Ratio/Differential Power Analysis Is: 10.3062
ByteDataSecurityTestData Absolute Value Indicatorer Is: 32
ByteDataSecurityTestData Sum Of Square Value Indicator Is: 67584
ByteDataSecurityTestData Algebraic Degree Is: 8
ByteDataSecurityTestData Algebraic Immunity Degree Is: 4
*/
constexpr std::array<std::uint8_t, 256> ByteSubstitutionBox
{
0xE2, 0x4E, 0x54, 0xFC, 0x94, 0xC2, 0x4A, 0xCC, 0x62, 0x0D, 0x6A, 0x46, 0x3C, 0x4D, 0x8B, 0xD1,
0x5E, 0xFA, 0x64, 0xCB, 0xB4, 0x97, 0xBE, 0x2B, 0xBC, 0x77, 0x2E, 0x03, 0xD3, 0x19, 0x59, 0xC1,
0x1D, 0x06, 0x41, 0x6B, 0x55, 0xF0, 0x99, 0x69, 0xEA, 0x9C, 0x18, 0xAE, 0x63, 0xDF, 0xE7, 0xBB,
0x00, 0x73, 0x66, 0xFB, 0x96, 0x4C, 0x85, 0xE4, 0x3A, 0x09, 0x45, 0xAA, 0x0F, 0xEE, 0x10, 0xEB,
0x2D, 0x7F, 0xF4, 0x29, 0xAC, 0xCF, 0xAD, 0x91, 0x8D, 0x78, 0xC8, 0x95, 0xF9, 0x2F, 0xCE, 0xCD,
0x08, 0x7A, 0x88, 0x38, 0x5C, 0x83, 0x2A, 0x28, 0x47, 0xDB, 0xB8, 0xC7, 0x93, 0xA4, 0x12, 0x53,
0xFF, 0x87, 0x0E, 0x31, 0x36, 0x21, 0x58, 0x48, 0x01, 0x8E, 0x37, 0x74, 0x32, 0xCA, 0xE9, 0xB1,
0xB7, 0xAB, 0x0C, 0xD7, 0xC4, 0x56, 0x42, 0x26, 0x07, 0x98, 0x60, 0xD9, 0xB6, 0xB9, 0x11, 0x40,
0xEC, 0x20, 0x8C, 0xBD, 0xA0, 0xC9, 0x84, 0x04, 0x49, 0x23, 0xF1, 0x4F, 0x50, 0x1F, 0x13, 0xDC,
0xD8, 0xC0, 0x9E, 0x57, 0xE3, 0xC3, 0x7B, 0x65, 0x3B, 0x02, 0x8F, 0x3E, 0xE8, 0x25, 0x92, 0xE5,
0x15, 0xDD, 0xFD, 0x17, 0xA9, 0xBF, 0xD4, 0x9A, 0x7E, 0xC5, 0x39, 0x67, 0xFE, 0x76, 0x9D, 0x43,
0xA7, 0xE1, 0xD0, 0xF5, 0x68, 0xF2, 0x1B, 0x34, 0x70, 0x05, 0xA3, 0x8A, 0xD5, 0x79, 0x86, 0xA8,
0x30, 0xC6, 0x51, 0x4B, 0x1E, 0xA6, 0x27, 0xF6, 0x35, 0xD2, 0x6E, 0x24, 0x16, 0x82, 0x5F, 0xDA,
0xE6, 0x75, 0xA2, 0xEF, 0x2C, 0xB2, 0x1C, 0x9F, 0x5D, 0x6F, 0x80, 0x0A, 0x72, 0x44, 0x9B, 0x6C,
0x90, 0x0B, 0x5B, 0x33, 0x7D, 0x5A, 0x52, 0xF3, 0x61, 0xA1, 0xF7, 0xB0, 0xD6, 0x3F, 0x7C, 0x6D,
0xED, 0x14, 0xE0, 0xA5, 0x3D, 0x22, 0xB3, 0xF8, 0x89, 0xDE, 0x71, 0x1A, 0xAF, 0xBA, 0xB5, 0x81
};
void BinaryCipher::InitialWithKey( std::vector<uint8_t> Keys )
{
if ( Keys.empty() )
{
throw std::invalid_argument( "Empty key vector provided." );
}
Random0 = Bitset512SizeZero;
Random1 = Bitset512SizeZero;
Random2 = Bitset512SizeZero;
Random3 = Bitset512SizeZero;
Random4 = Bitset512SizeZero;
Random5 = Bitset512SizeZero;
Random6 = Bitset512SizeZero;
Random7 = Bitset512SizeZero;
uint8_t CounterA = 0;
uint8_t CounterB = 0;
std::string BinaryString;
uint8_t ByteData = 0x00;
constexpr size_t ByteBitCount = std::numeric_limits<uint8_t>::digits;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
Random0.FromString( BinaryString, 2 );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
Random1.FromString( BinaryString, 2 );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
Random2.FromString( BinaryString, 2 );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
Random3.FromString( BinaryString, 2 );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
Random4.FromString( BinaryString, 2 );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
Random5.FromString( BinaryString, 2 );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
Random6.FromString( BinaryString, 2 );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
Random7.FromString( BinaryString, 2 );
BinaryString.clear();
CounterB = 0;
size_t KeyIndex = 0;
auto XORWithKeyBytes = []( BigInteger& Random, const std::vector<uint8_t>& Keys, size_t& KeyIndex ) -> void
{
// Loop over each element of the key vector and perform an arithmetic operation on the corresponding Random member.
for ( size_t i = 0; i < 512; ++i )
{
Random.SetBit( Random.GetBit( i ) ^ ( ( Keys[ KeyIndex ] >> ( i % 8 ) ) & 1 ), i );
// Update the key index to loop through the elements of the key vector
if(((i + 1) & 8) == 0 && KeyIndex < Keys.size())
{
++KeyIndex;
}
}
};
XORWithKeyBytes( Random0, Keys, KeyIndex );
XORWithKeyBytes( Random1, Keys, KeyIndex );
XORWithKeyBytes( Random2, Keys, KeyIndex );
XORWithKeyBytes( Random3, Keys, KeyIndex );
XORWithKeyBytes( Random4, Keys, KeyIndex );
XORWithKeyBytes( Random5, Keys, KeyIndex );
XORWithKeyBytes( Random6, Keys, KeyIndex );
XORWithKeyBytes( Random7, Keys, KeyIndex );
}
//Reference web site: https://bisqwit.iki.fi/story/howto/bitmath/
BigInteger BinaryCipher::AdditionBits512( const BigInteger& a, const BigInteger& b )
{
BigInteger sum(Bitset512SizeZero);
bool carry = false;
for ( int i = 0; i < 512; ++i )
{
bool bit_a = a.GetBit(i);
bool bit_b = b.GetBit(i);
// Perform binary addition of the current bit, taking into account rounding
if (carry)
{
sum.SetBit(bit_a != bit_b, i); // The result of an iso-or of 2 bits as a sum of the current bits
carry = bit_a || bit_b; // Binary addition of the incoming bits as an or-operation of the two current bits
}
else
{
sum.SetBit(bit_a != bit_b, i); // The result of an exclusive-or of 2 bits as a sum of the current bits
carry = bit_a && bit_b; // Binary addition of the incoming bits as an and-operation of the two current bits
}
}
return sum;
}
void BinaryCipher::KeyExpansion( const BigInteger& BitsKey, BigInteger& BitsExpansionKey )
{
//BitsKey is 256 Bit
//BitsExpansionKey is 512 Bit
size_t length_bits = BitsKey.BitLength();
/*Key Padding*/
if(length_bits < 264)
{
std::string binary_string = BitsKey.ToBinaryString(512, false);
binary_string.append("1");
// Calculate the number of zeros needed to be added
const uint32_t k = (448 - length_bits - 1 + 512) % 512;
// Append k zeros to the binary string
binary_string.append(k, '0');
// Append a 64-bit binary representation of the original message length
std::bitset<64> length_bitset(length_bits);
binary_string.append(length_bitset.to_string());
// Ensure the length of binary_string is a multiple of 512
while (binary_string.length() % 512 != 0)
{
binary_string.append("0");
}
// Now binary_string has the padded message
// You need to convert the binary string back to BigInteger and assign it to BitsExpansionKey
BitsExpansionKey.FromString(binary_string, 2);
std::cout << "Padded Key:" << BitsExpansionKey.ToBinaryString(512) << std::endl;
}
BigInteger TemporaryState( BitsExpansionKey );
BigInteger TemporaryState2 = BigInteger( Bitset512SizeZero );
/*PRF*/
BigInteger RandomA( Bitset512SizeZero ), RandomB( Bitset512SizeZero ), RandomC( Bitset512SizeZero ), RandomD( Bitset512SizeZero ), RandomE( Bitset512SizeZero ), RandomF( Bitset512SizeZero ), RandomG( Bitset512SizeZero ), RandomH( Bitset512SizeZero ), RandomI( Bitset512SizeZero );
BigInteger Choose( Bitset512SizeZero ), Majority( Bitset512SizeZero ), Choose2( Bitset512SizeZero ), Majority2( Bitset512SizeZero ), Choose3( Bitset512SizeZero ), Majority3( Bitset512SizeZero );
/*
The 79 and 11 is prime number
FindNextPrime(79) = 83
248 = (79 - 11) * (83 + 11) mod 512
FindNextPrime(248) = 251
FindNextPrime(11) = 13
88 = (248 - 13) * (251 + 13) mod 512
FindNextPrime(88) = 89
FindNextPrime(13) = 17
358 = (88 - 17) * (89 + 17) mod 512
FindNextPrime(358) = 359
FindNextPrime(17) = 19
142 = (358 - 19) * (359 + 19) mod 512
FindNextPrime(142) = 149
FindNextPrime(19) = 23
500 = (142 - 23) * (149 + 23) mod 512
FindNextPrime(500) = 503
FindNextPrime(23) = 29
204 = (500 - 29) * (503 + 29) mod 512
FindNextPrime(204) = 211
FindNextPrime(29) = 31
394 = (204 - 31) * (211 + 31) mod 512
FindNextPrime(394) = 397
FindNextPrime(31) = 37
314 = (394 - 37) * (397 + 37) mod 512
*/
for ( int round = 16; round > 0; round-- )
{
Random0 = BigInteger::BitRotateRight(Random0 | ~TemporaryState, 314, 512); //NOT-OR Gate
Random1 = BigInteger::BitRotateLeft(Random1 | ~TemporaryState, 248, 512); //NOT-OR Gate
Random2 = BigInteger::BitRotateLeft(Random2 | ~TemporaryState, 88, 512); //NOT-OR Gate
Random3 = BigInteger::BitRotateRight(Random3 ^ TemporaryState, 358, 512);
Random4 = BigInteger::BitRotateLeft(Random4 ^ TemporaryState, 142, 512);
Random5 = BigInteger::BitRotateRight(~Random5 & TemporaryState, 500, 512); //NOT-AND Gate
Random6 = BigInteger::BitRotateRight(~Random6 & TemporaryState, 204, 512); //NOT-AND Gate
Random7 = BigInteger::BitRotateLeft(~Random7 & TemporaryState, 394, 512); //NOT-AND Gate
RandomA = Random0 ^ Random5;
RandomB = BigInteger::BitRotateRight(( Random1 ^ Random7 ), 7, 512);
RandomC = Random2 ^ Random3 ^ Random4;
RandomD = BigInteger::BitRotateLeft(( Random1 ^ Random4 ), 163, 512);
RandomE = Random2 ^ Random6;
RandomF = BigInteger::BitRotateRight(( Random3 ^ Random4 ^ Random5 ), 439, 512);
RandomG = Random0 ^ Random1 ^ Random3 ^ Random5;
RandomH = BigInteger::BitRotateLeft(( Random0 ^ Random2 ^ Random4 ^ Random6 ), 257, 512);
//ALL XOR
RandomI = Random0 ^ Random1 ^ Random2 ^ Random3 ^ Random4 ^ Random5 ^ Random6 ^ Random7;
Choose = ( RandomA & RandomB ) ^ ( ~RandomA ^ RandomC );
Majority = ( RandomA & RandomB ) ^ ( RandomA & RandomC ) ^ ( RandomB & RandomC );
Choose2 = ( RandomD & RandomE ) ^ ( ~RandomD ^ RandomF );
Majority2 = ( RandomD & RandomE ) ^ ( RandomD & RandomF ) ^ ( RandomE & RandomF );
Choose3 = ( RandomG & RandomH ) ^ ( ~RandomG ^ RandomI );
Majority3 = ( RandomG & RandomH ) ^ ( RandomG & RandomI ) ^ ( RandomH & RandomI );
auto&& NumberA = ( Choose3 ^ Majority2 );
auto&& NumberB = ( Choose | Majority3 ) ^ ( Choose2 & Majority );
#ifdef USE_BIG_INTEGER_ARITHMATIC_VERSION
TemporaryState2 = (NumberA + NumberB) % Number2Power512;
TemporaryState = (TemporaryState + TemporaryState2) % Number2Power512;
#else
TemporaryState2 = AdditionBits512( NumberA, NumberB );
TemporaryState = AdditionBits512( TemporaryState, TemporaryState2 );
#endif
}
#ifdef USE_BIG_INTEGER_ARITHMATIC_VERSION
BitsExpansionKey = (BitsExpansionKey + TemporaryState) % Number2Power512;
#else
BitsExpansionKey = AdditionBits512( BitsExpansionKey, TemporaryState );
#endif
}
int LeadingZeros( uint32_t x )
{
if ( x == 0 )
{
return 32; // 如果输入为0,返回32,因为它的所有位都是零
}
int count = 0;
while ( ( x & 0x80000000 ) == 0 )
{
x <<= 1;
++count;
}
return count;
}
//Reference web site: https://bisqwit.iki.fi/story/howto/bitmath/
std::bitset<512> AdditionBits512( const std::bitset<512>& a, const std::bitset<512>& b )
{
std::bitset<512> sum;
bool carry = false;
for ( int i = 0; i < 512; ++i )
{
bool bit_a = a[i];
bool bit_b = b[i];
// Perform binary addition of the current bit, taking into account rounding
if (carry)
{
sum[i] = bit_a != bit_b; // The result of an iso-or of 2 bits as a sum of the current bits
carry = bit_a || bit_b; // Binary addition of the incoming bits as an or-operation of the two current bits
}
else
{
sum[i] = bit_a != bit_b; // The result of an exclusive-or of 2 bits as a sum of the current bits
carry = bit_a && bit_b; // Binary addition of the incoming bits as an and-operation of the two current bits
}
}
return sum;
}
// Function to rotate left by 'k' bits
template <std::size_t BITSET_SIZE>
std::bitset<BITSET_SIZE> BitRotateLeft( const std::bitset<BITSET_SIZE>& bs, size_t k )
{
k %= BITSET_SIZE; // Ensure k is within [0, 511]
if ( k == 0 )
return bs;
// Perform left rotation: (bs << k) | (bs >> (BITSET_SIZE - k))
return ( bs << k ) | ( bs >> ( BITSET_SIZE - k ) );
}
// Function to rotate right by 'k' bits
template <std::size_t BITSET_SIZE>
std::bitset<BITSET_SIZE> BitRotateRight( const std::bitset<BITSET_SIZE>& bs, size_t k )
{
k %= BITSET_SIZE; // Ensure k is within [0, 511]
if ( k == 0 )
return bs;
// Perform right rotation: (bs >> k) | (bs << (BITSET_SIZE - k))
return ( bs >> k ) | ( bs << ( BITSET_SIZE - k ) );
}
void BinaryCipherNaive::InitialWithKey( std::vector<uint8_t> Keys )
{
if ( Keys.empty() )
{
throw std::invalid_argument( "Empty key vector provided." );
}
RandomBits0.reset();
RandomBits1.reset();
RandomBits2.reset();
RandomBits3.reset();
RandomBits4.reset();
RandomBits5.reset();
RandomBits6.reset();
RandomBits7.reset();
uint8_t CounterA = 0;
uint8_t CounterB = 0;
std::string BinaryString;
uint8_t ByteData = 0x00;
constexpr size_t ByteBitCount = std::numeric_limits<uint8_t>::digits;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
RandomBits0 = std::bitset<512>( BinaryString );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
RandomBits1 = std::bitset<512>( BinaryString );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
RandomBits2 = std::bitset<512>( BinaryString );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
RandomBits3 = std::bitset<512>( BinaryString );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
RandomBits4 = std::bitset<512>( BinaryString );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
RandomBits5 = std::bitset<512>( BinaryString );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
RandomBits6 = std::bitset<512>( BinaryString );
BinaryString.clear();
CounterB = 0;
++CounterA;
ByteData ^= CounterA;
while ( CounterB < 512 / ByteBitCount )
{
ByteData = ByteSubstitutionBox[ ByteSubstitutionBox[ ByteData ] ];
BinaryString.append( std::bitset<ByteBitCount>( ByteData ).to_string() );
++CounterB;
}
RandomBits7 = std::bitset<512>( BinaryString );
BinaryString.clear();
CounterB = 0;
size_t KeyIndex = 0;
auto XORWithKeyBytes = []( std::bitset<512>& Random, const std::vector<uint8_t>& Keys, size_t& KeyIndex ) -> void
{
// Loop over each element of the key vector and perform an arithmetic operation on the corresponding Random member.
for ( size_t i = 0; i < 512; ++i )
{
Random[ i ] = ( Random[ i ] ^ ( ( Keys[ KeyIndex ] >> ( i & 8 ) ) & 1 ) );
// Update the key index to loop through the elements of the key vector
if(((i + 1) & 8) == 0 && KeyIndex < Keys.size())
{
++KeyIndex;
}
}
};
XORWithKeyBytes( RandomBits0, Keys, KeyIndex );
XORWithKeyBytes( RandomBits1, Keys, KeyIndex );
XORWithKeyBytes( RandomBits2, Keys, KeyIndex );
XORWithKeyBytes( RandomBits3, Keys, KeyIndex );
XORWithKeyBytes( RandomBits4, Keys, KeyIndex );
XORWithKeyBytes( RandomBits5, Keys, KeyIndex );
XORWithKeyBytes( RandomBits6, Keys, KeyIndex );
XORWithKeyBytes( RandomBits7, Keys, KeyIndex );
}
void BinaryCipherNaive::KeyExpansion( const std::bitset<256>& BitsKey, std::bitset<512>& BitsExpansionKey )
{
using BigInteger = TwilightDream::BigInteger::BigInteger;
/* Key Padding */
std::string binary_string = BitsKey.to_string();
std::size_t length_bits = binary_string.size();
if(length_bits < 264)
{
// Append "1" to the binary string
binary_string.push_back('1');
// Calculate the number of zeros needed to be added
const uint32_t k = (448 - length_bits - 1 + 512) % 512;
// Append k zeros to the binary string
binary_string.append(k, '0');
// Append a 64-bit binary representation of the original message length
std::bitset<64> length_bits(BitsKey.size());
binary_string.append(length_bits.to_string());
// Ensure the length of binary_string is a multiple of 512
while (binary_string.length() % 512 != 0)
{
binary_string.push_back('0');
}
// Now binary_string has the padded message
// Copy the result to BitsExpansionKey
BitsExpansionKey = std::bitset<512>(binary_string);
std::cout << "Padded Key:" << BitsExpansionKey.to_string() << std::endl;
}
/*PRF*/
std::bitset<512> RandomBitsA, RandomBitsB, RandomBitsC, RandomBitsD, RandomBitsE, RandomBitsF, RandomBitsG, RandomBitsH, RandomBitsI;
std::bitset<512> Choose, Majority, Choose2, Majority2, Choose3, Majority3;
std::bitset<512> TemporaryState = BitsExpansionKey;
std::bitset<512> TemporaryState2;
/*
The 79 and 11 is prime number
FindNextPrime(79) = 83
248 = (79 - 11) * (83 + 11) mod 512
FindNextPrime(248) = 251
FindNextPrime(11) = 13
88 = (248 - 13) * (251 + 13) mod 512
FindNextPrime(88) = 89
FindNextPrime(13) = 17
358 = (88 - 17) * (89 + 17) mod 512
FindNextPrime(358) = 359
FindNextPrime(17) = 19
142 = (358 - 19) * (359 + 19) mod 512
FindNextPrime(142) = 149
FindNextPrime(19) = 23
500 = (142 - 23) * (149 + 23) mod 512
FindNextPrime(500) = 503
FindNextPrime(23) = 29
204 = (500 - 29) * (503 + 29) mod 512
FindNextPrime(204) = 211
FindNextPrime(29) = 31
394 = (204 - 31) * (211 + 31) mod 512
FindNextPrime(394) = 397
FindNextPrime(31) = 37
314 = (394 - 37) * (397 + 37) mod 512
*/
//#define DEBUG
#if defined( DEBUG )
std::cout << "BitsExpansionKeyCopy: " << TemporaryState << '\n';
std::cout << std::endl;
#endif
for ( int round = 16; round > 0; round-- )
{
#if defined( DEBUG )
std::cout << "Round: " << round << std::endl;
#endif
RandomBits0 = BitRotateRight<512>(RandomBits0 | ~TemporaryState, 314); //NOT-OR Gate
RandomBits1 = BitRotateLeft<512>(RandomBits1 | ~TemporaryState, 248); //NOT-OR Gate
RandomBits2 = BitRotateLeft<512>(RandomBits2 | ~TemporaryState, 88); //NOT-OR Gate
RandomBits3 = BitRotateRight<512>(RandomBits3 ^ TemporaryState, 358);
RandomBits4 = BitRotateLeft<512>(RandomBits4 ^ TemporaryState, 142);
RandomBits5 = BitRotateRight<512>(~RandomBits5 & TemporaryState, 500); //NOT-AND Gate
RandomBits6 = BitRotateRight<512>(~RandomBits6 & TemporaryState, 204); //NOT-AND Gate
RandomBits7 = BitRotateLeft<512>(~RandomBits7 & TemporaryState, 394); //NOT-AND Gate
#if defined( DEBUG )
std::cout << "Initial Random Bits:" << '\n';
std::cout << "RandomBits0: " << RandomBits0 << '\n';
std::cout << "RandomBits1: " << RandomBits1 << '\n';
std::cout << "RandomBits2: " << RandomBitsC << '\n';
std::cout << "RandomBits3: " << RandomBitsD << '\n';
std::cout << "RandomBits4: " << RandomBitsE << '\n';
std::cout << "RandomBits5: " << RandomBitsF << '\n';
std::cout << "RandomBits6: " << RandomBitsG << '\n';
std::cout << "RandomBits7: " << RandomBitsH << std::endl;
#endif
RandomBitsA = RandomBits0 ^ RandomBits5;
RandomBitsB = ( RandomBits1 ^ RandomBits7 ) >> 7 | ( RandomBits1 ^ RandomBits7 ) << 512 - 7;
RandomBitsC = RandomBits2 ^ RandomBits3 ^ RandomBits4;
RandomBitsD = ( RandomBits1 ^ RandomBits4 ) << 163 | ( RandomBits1 ^ RandomBits4 ) >> 512 - 163;
RandomBitsE = RandomBits2 ^ RandomBits6;
RandomBitsF = ( RandomBits3 ^ RandomBits4 ^ RandomBits5 ) >> 439 | ( RandomBits3 ^ RandomBits4 ^ RandomBits5 ) << 512 - 439;
RandomBitsG = RandomBits0 ^ RandomBits1 ^ RandomBits3 ^ RandomBits5;
RandomBitsH = ( RandomBits0 ^ RandomBits2 ^ RandomBits4 ^ RandomBits6 ) << 257 | ( RandomBits0 ^ RandomBits2 ^ RandomBits4 ^ RandomBits6 ) >> 512 - 257;
//ALL XOR
RandomBitsI = RandomBits0 ^ RandomBits1 ^ RandomBits2 ^ RandomBits3 ^ RandomBits4 ^ RandomBits5 ^ RandomBits6 ^ RandomBits7;
#if defined( DEBUG )
std::cout << "After XOR operations:" << '\n';
std::cout << "RandomBitsA: " << RandomBitsA << '\n';
std::cout << "RandomBitsB: " << RandomBitsB << '\n';
std::cout << "RandomBitsC: " << RandomBitsC << '\n';
std::cout << "RandomBitsD: " << RandomBitsD << '\n';
std::cout << "RandomBitsE: " << RandomBitsE << '\n';
std::cout << "RandomBitsF: " << RandomBitsF << '\n';
std::cout << "RandomBitsG: " << RandomBitsG << '\n';
std::cout << "RandomBitsH: " << RandomBitsH << '\n';
std::cout << "RandomBitsI: " << RandomBitsI << std::endl;
#endif
Choose = ( RandomBitsA & RandomBitsB ) ^ ( ~RandomBitsA ^ RandomBitsC );
Majority = ( RandomBitsA & RandomBitsB ) ^ ( RandomBitsA & RandomBitsC ) ^ ( RandomBitsB & RandomBitsC );
Choose2 = ( RandomBitsD & RandomBitsE ) ^ ( ~RandomBitsD ^ RandomBitsF );
Majority2 = ( RandomBitsD & RandomBitsE ) ^ ( RandomBitsD & RandomBitsF ) ^ ( RandomBitsE & RandomBitsF );
Choose3 = ( RandomBitsG & RandomBitsH ) ^ ( ~RandomBitsG ^ RandomBitsI );
Majority3 = ( RandomBitsG & RandomBitsH ) ^ ( RandomBitsG & RandomBitsI ) ^ ( RandomBitsH & RandomBitsI );
#if defined( DEBUG )
std::cout << "After Choose and Maj operations:" << '\n';
std::cout << "Choose: " << Choose << '\n';
std::cout << "Majority: " << Majority << '\n';
std::cout << "Choose2: " << Choose2 << '\n';
std::cout << "Majority2: " << Majority2 << '\n';
std::cout << "Choose3: " << Choose << '\n';
std::cout << "Majority3: " << Majority << std::endl;
#endif
auto&& NumberA = ( Choose3 ^ Majority2 );
auto&& NumberB = ( Choose | Majority3 ) ^ ( Choose2 & Majority );
TemporaryState2 = AdditionBits512( NumberA, NumberB );
TemporaryState = AdditionBits512( TemporaryState, TemporaryState2 );
#if defined( DEBUG )
std::cout << "After final operation:" << '\n';
std::cout << "BitsExpansionKeyCopy: " << TemporaryState << '\n';
std::cout << '\n';
std::cout << "#############################################################" << '\n';
std::cout << std::endl;
#endif
}
BitsExpansionKey = AdditionBits512( BitsExpansionKey, TemporaryState );
}
#ifdef USE_BIG_INTEGER_ARITHMATIC_VERSION
#undef USE_BIG_INTEGER_ARITHMATIC_VERSION
#endif // USE_BIG_INTEGER_ARITHMATIC_VERSION