-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
805 lines (755 loc) · 27.9 KB
/
Copy pathmain.cpp
File metadata and controls
805 lines (755 loc) · 27.9 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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/*
* BSD 2-Clause License
*
* Copyright (c) 2020, Piotr Pszczółkowski
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*------- include files:
-------------------------------------------------------------------*/
#include <iostream>
#include <string>
#include <memory>
#include <cassert>
#include <vector>
#include <cstring>
#include "Crypto/Blowfish/Blowfish.h"
#include "Crypto/Gost/Gost.h"
#include "Crypto/Way3/Way3.h"
#include "Crypto/Crypto.h"
/*------- namespaces:
-------------------------------------------------------------------*/
using namespace std;
using namespace beesoft::crypto;
/*------- forward declarations:
-------------------------------------------------------------------*/
void test_way3();
void way3_test_gamma();
void way3_test_mu();
void way3_test_theta();
void way3_test_rho();
void way3_test_block();
void way3_test_ecb();
void way3_test_cbc_without_iv();
void test_gost();
void gost_test_block();
void gost_test_ecb();
void gost_test_cbc_without_iv();
void test_blowfish();
void blowfish_test_block();
void blowfish_test_ecb();
void blowfish_test_cbc_with_iv();
void blowfish_test_cbc_without_iv();
int main() {
test_blowfish();
cout << endl;
test_gost();
cout << endl;
test_way3();
return 0;
}
void test_way3() {
way3_test_gamma();
way3_test_mu();
way3_test_theta();
way3_test_rho();
way3_test_block();
way3_test_ecb();
way3_test_cbc_without_iv();
}
/**
* @brief way_test_cbc_without_iv
*/
void way3_test_cbc_without_iv() {
vector<string> plain = {
string("Beesoft Software, Piotr Pszczółkowski"),
string("Beesoft Software, Piotr Pszczółkowsk"),
string("Beesoft Software, Piotr Pszczółkows"),
string("Beesoft Software, Piotr Pszczółkow"),
string("Beesoft Software, Piotr Pszczółko"),
string("Beesoft Software, Piotr Pszczółk"),
string("Beesoft Software, Piotr Pszczół"),
string("Beesoft Software, Piotr Pszczó"),
string("Beesoft Software, Piotr Pszcz"),
string("Beesoft Software, Piotr Pszc"),
string("Beesoft Software, Piotr Psz"),
string("Beesoft Software, Piotr Ps"),
string("Beesoft Software, Piotr P"),
string("Beesoft Software, Piotr "),
string("Beesoft Software, Piotr"),
string("Beesoft Software, Piot"),
string("Beesoft Software, Pio"),
string("Beesoft Software, Pi"),
string("Beesoft Software, P"),
string("Beesoft Software, "),
string("Beesoft Software,"),
string("Beesoft Software"),
string("Beesoft"),
string("")
};
u8 key[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67};
Way3 w3(key, 12);
for (int i = 0; i < int(plain.size()); i++) {
const auto [cipher, n] = w3.encrypt_cbc(plain[i].data(), plain[i].size());
const auto [decipher, k] = w3.decrypt_cbc(cipher.get(), n);
assert(string(static_cast<char*>(decipher.get()), k) == plain[i]);
}
cout << "way3_test_cbc_without_iv: OK" << endl;
}
void way3_test_ecb() {
struct test {
u8 key[12];
u8 plain[8];
} tests[] =
{
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
{
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
},
{
{0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00},
{0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
},
{
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
},
{
{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67},
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
},
{
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
},
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
},
};
for (int i = 0; i < int(sizeof(tests)/sizeof(test)); i++) {
Way3 w3(tests[i].key, 12);
const auto [cipher, n] = w3.encrypt_ecb(tests[i].plain, sizeof(tests[i].plain));
const auto [plain, k] = w3.decrypt_ecb(cipher.get(), n);
assert(Crypto::compare_bytes(plain.get(), tests[i].plain, k));
}
cout << "way3_test_ecb: OK" << endl;
}
void way3_test_block() {
struct test {
u32 key[3];
u32 plain[3];
u32 cipher[3];
} tests[] = {
{
{0, 0, 0},
{1, 1, 1},
{0x4059c76e, 0x83ae9dc4, 0xad21ecf7}
},
{
{6, 5, 4},
{3, 2, 1},
{0xd2f05b5e, 0xd6144138, 0xcab920cd}
},
{
{0xdef01234, 0x456789ab, 0xbcdef012},
{0x23456789, 0x9abcdef0, 0x01234567},
{0x0aa55dbb, 0x9cdddb6d, 0x7cdb76b2}
},
{
{0xd2f05b5e, 0xd6144138, 0xcab920cd},
{0x4059c76e, 0x83ae9dc4, 0xad21ecf7},
{0x478ea871, 0x6b13f17c, 0x15b155ed},
},
};
u32 buffer[3];
for (int i = 0; i < int(sizeof(tests)/sizeof(test)); i++) {
Way3 w3(tests[i].key, 12);
w3.encrypt_block(tests[i].plain, buffer);
assert(buffer[0] == tests[i].cipher[0]);
assert(buffer[1] == tests[i].cipher[1]);
assert(buffer[2] == tests[i].cipher[2]);
w3.decrypt_block(buffer, buffer);
assert(buffer[0] == tests[i].plain[0]);
assert(buffer[1] == tests[i].plain[1]);
assert(buffer[2] == tests[i].plain[2]);
}
cout << "way3_test_block: OK" << endl;
}
void way3_test_rho() {
struct test {
u32 data[3];
u32 want[3];
} tests[] = {
{
{0x00000000, 0x00000000, 0x00000000},
{0xffffffff, 0xffffffff, 0xffffffff}
},
{
{0x00000001, 0x00000002, 0x00000003},
{0xf77f7ff6, 0x7dbfbcfd, 0xbefeffff}
},
{
{0x00000004, 0x00000005, 0x00000006},
{0xededf06e, 0x7bf9ff3a, 0x7dbdfdfe}
},
{
{0xffffffff, 0xffffffff, 0xffffffff},
{0x00000000, 0x00000000, 0x00000000}
},
{
{0x01010101, 0x02020202, 0x03030303},
{0xfe7efff7, 0xfc3f7ffe, 0xfebfbfff}
},
{
{0x01234567, 0x89abcdef, 0xfedcba98},
{0x842224d3, 0x1a47237a, 0xbb1e62f3}
},
};
Way3 w3;
u32 buffer[3];
for (int i = 0; i < int(sizeof(tests)/sizeof(test)); i++) {
memcpy(buffer, tests[i].data, 3 * sizeof(u32));
w3.rho(buffer);
assert(buffer[0] == tests[i].want[0]);
assert(buffer[1] == tests[i].want[1]);
assert(buffer[2] == tests[i].want[2]);
}
cout << "way3_test_rho: OK" << endl;
}
void way3_test_mu() {
struct test {
u32 data[3];
u32 want[3];
} tests[] = {
{
{0x00000000, 0x00000000, 0x00000000},
{0x00000000, 0x00000000, 0x00000000}
},
{
{0x00000001, 0x00000002, 0x00000003},
{0xc0000000, 0x40000000, 0x80000000}
},
{
{0x00000004, 0x00000005, 0x00000006},
{0x60000000, 0xa0000000, 0x20000000}
},
{
{0xffffffff, 0xffffffff, 0xffffffff},
{0xffffffff, 0xffffffff, 0xffffffff}
},
{
{0x01010101, 0x02020202, 0x03030303},
{0xc0c0c0c0, 0x40404040, 0x80808080}
},
{
{0x01234567, 0x89abcdef, 0xfedcba98},
{0x195d3b7f, 0xf7b3d591, 0xe6a2c480}
}
};
Way3 w3;
u32 buffer[3];
for (int i = 0; i < int(sizeof(tests)/sizeof(test)); i++) {
memcpy(buffer, tests[i].data, 3 * sizeof(u32));
w3.mu(buffer);
assert(buffer[0] == tests[i].want[0]);
assert(buffer[1] == tests[i].want[1]);
assert(buffer[2] == tests[i].want[2]);
}
cout << "way3_test_mu: OK" << endl;
}
void way3_test_gamma() {
struct test {
u32 data[3];
u32 want[3];
} tests[] = {
{
{0x00000000, 0x00000000, 0x00000000},
{0xffffffff, 0xffffffff, 0xffffffff}
},
{
{0x00000001, 0x00000002, 0x00000003},
{0xffffffff, 0xfffffffd, 0xfffffffe}
},
{
{0x00000004, 0x00000005, 0x00000006},
{0xfffffff9, 0xfffffffa, 0xfffffff8}
},
{
{0xffffffff, 0xffffffff, 0xffffffff},
{0x00000000, 0x00000000, 0x00000000}
},
{
{0x01010101, 0x02020202, 0x03030303},
{0xffffffff, 0xfdfdfdfd, 0xfefefefe}
},
{
{0x01234567, 0x89abcdef, 0xfedcba98},
{0x88888888, 0x77777777, 0x89abcdef}
}
};
Way3 w3;
u32 buffer[3];
for (int i = 0; i < int(sizeof(tests)/sizeof(test)); i++) {
memcpy(buffer, tests[i].data, 3 * sizeof(u32));
w3.gamma(buffer);
assert(buffer[0] == tests[i].want[0]);
assert(buffer[1] == tests[i].want[1]);
assert(buffer[2] == tests[i].want[2]);
}
cout << "way3_test_gamma: OK" << endl;
}
void way3_test_theta() {
struct test {
u32 data[3];
u32 want[3];
} tests[] = {
{
{0x00000000, 0x00000000, 0x00000000},
{0x00000000, 0x00000000, 0x00000000}
},
{
{0x00000001, 0x00000002, 0x00000003},
{0x01000201, 0x02000302, 0x03000103}
},
{
{0x00000004, 0x00000005, 0x00000006},
{0x04070204, 0x05070105, 0x06070306}
},
{
{0xffffffff, 0xffffffff, 0xffffffff},
{0xffffffff, 0xffffffff, 0xffffffff}
},
{
{0x01010101, 0x02020202, 0x03030303},
{0x02000003, 0x03000001, 0x01000002}
},
{
{0x01234567, 0x89abcdef, 0xfedcba98},
{0xab3210fe, 0xdc321001, 0x23321089}
}
};
Way3 w3;
u32 buffer[3];
for (int i = 0; i < int(sizeof(tests)/sizeof(test)); i++) {
memcpy(buffer, tests[i].data, 3 * sizeof(u32));
w3.theta(buffer);
assert(buffer[0] == tests[i].want[0]);
assert(buffer[1] == tests[i].want[1]);
assert(buffer[2] == tests[i].want[2]);
}
cout << "way3_test_theta: OK" << endl;
}
/********************************************************************
* *
* G O S T *
* *
********************************************************************/
void test_gost() {
gost_test_block();
gost_test_ecb();
gost_test_cbc_without_iv();
}
void gost_test_block() {
u8 key[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2
};
struct test {
u32 plain[2];
u32 cipher[2];
} tests[] = {
{
{0x0, 0x0},
{0x9b717f65, 0x32b884d0}
},
{
{0x0, 0x1},
{0xe5112916, 0xd5620daf}
},
{
{0x1, 0x0},
{0xd9641556, 0xa0cdcf41}
},
{
{0x1, 0x2},
{0x60591f3d, 0x5797bf40}
},
{
{0x2510, 0x1959},
{0x3967d936, 0x1f7af77b}
},
{
{0xabcdef, 0x123456},
{0x5280fbb5, 0xdd68c520}
},
{
{0xaabbccdd, 0xeeff1122},
{0xc9379503, 0x626e5b08}
},
{
{0xffffffff, 0xffffffff},
{0xef9c8b90, 0x70dbbfbf}
}
};
Gost gt(key, 32);
for (int i = 0; i < int(sizeof(tests)/sizeof(test)); i++) {
u32 result[2] = {};
{
gt.encrypt_block(tests[i].plain, result);
assert(tests[i].cipher[0] == result[0]);
assert(tests[i].cipher[1] == result[1]);
}
{
gt.decrypt_block(result, result);
assert(tests[i].plain[0] == result[0]);
assert(tests[i].plain[1] == result[1]);
}
}
cout << "gost_test_block: OK" << endl;
}
/**
* @brief gost_test_ecb
*/
void gost_test_ecb() {
struct test {
u8 key[32];
u8 plain[8];
u8 cipher[8];
} tests[] =
{
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0xd7, 0x17, 0x2b, 0xe7, 0xc0, 0x22, 0xf1, 0x02}
},
{
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
{0x86, 0x47, 0x64, 0x62, 0xdc, 0xd5, 0x3d, 0x01},
},
{
{0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
{0xb5, 0xb6, 0x98, 0xa0, 0x62, 0x4b, 0x18, 0x57}
},
{
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0x2d, 0x09, 0x35, 0x4c, 0x7e, 0xa3, 0x9e, 0xfa},
},
{
{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0x07, 0x43, 0xde, 0xe7, 0xbd, 0x05, 0x2c, 0x75},
},
{
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
{0xe7, 0x99, 0x31, 0xb9, 0x22, 0x2b, 0x7a, 0xf0},
},
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0xd7, 0x17, 0x2b, 0xe7, 0xc0, 0x22, 0xf1, 0x02},
},
};
for (int i = 0; i < int(sizeof(tests)/sizeof(test)); i++) {
Gost gt(tests[i].key, 32);
const auto [cipher, n] = gt.encrypt_ecb(tests[i].plain, sizeof(tests[i].plain));
assert(Crypto::compare_bytes(cipher.get(), tests[i].cipher, n));
const auto [plain, k] = gt.decrypt_ecb(cipher.get(), 8);
assert(Crypto::compare_bytes(plain.get(), tests[i].plain, k));
}
cout << "gost_test_ecb: OK" << endl;
}
void gost_test_cbc_without_iv() {
vector<string> plain = {
string("Beesoft Software, Piotr Pszczółkowski"),
string("Beesoft Software, Piotr Pszczółkowsk"),
string("Beesoft Software, Piotr Pszczółkows"),
string("Beesoft Software, Piotr Pszczółkow"),
string("Beesoft Software, Piotr Pszczółko"),
string("Beesoft Software, Piotr Pszczółk"),
string("Beesoft Software, Piotr Pszczół"),
string("Beesoft Software, Piotr Pszczó"),
string("Beesoft Software, Piotr Pszcz"),
string("Beesoft Software, Piotr Pszc"),
string("Beesoft Software, Piotr Psz"),
string("Beesoft Software, Piotr Ps"),
string("Beesoft Software, Piotr P"),
string("Beesoft Software, Piotr "),
string("Beesoft Software, Piotr"),
string("Beesoft Software, Piot"),
string("Beesoft Software, Pio"),
string("Beesoft Software, Pi"),
string("Beesoft Software, P"),
string("Beesoft Software, "),
string("Beesoft Software,"),
string("Beesoft Software"),
string("Beesoft"),
string("")
};
// 20 loops with random key value
u8 key[32];
for (int j = 0; j < 20; j++) {
Crypto::random_bytes(key, 32);
Gost gt(key, 32);
for (int i = 0; i < int(plain.size()); i++) {
const auto [cipher, n] = gt.encrypt_cbc(plain[j].data(), plain[j].size());
const auto [decipher, k] = gt.decrypt_cbc(cipher.get(), n);
assert(string(static_cast<char*>(decipher.get()), k) == plain[j]);
}
}
cout << "gosth_test_cbc_without_iv (random keys): OK" << endl;
}
/********************************************************************
* *
* B L O W F I S H *
* *
********************************************************************/
void test_blowfish() {
blowfish_test_block();
blowfish_test_ecb();
blowfish_test_cbc_with_iv();
blowfish_test_cbc_without_iv();
}
/**
* @brief blowfish_test_block
*/
void blowfish_test_block() {
u32 plain[] = {1, 2};
u32 expected[] = {0xdf333fd2, 0x30a71bb4};
u32 buffer[] = {0, 0};
const auto key = string("TESTKEY");
Blowfish bf(key.data(), key.size());
bf.encrypt_block(plain, buffer);
assert(buffer[0] == expected[0]);
assert(buffer[1] == expected[1]);
bf.decrypt_block(buffer, buffer);
assert(buffer[0] == plain[0]);
assert(buffer[1] == plain[1]);
cout << "tblowfish_test_block: OK" << endl;
}
/**
* @brief blowfish_test_ecb
*/
void blowfish_test_ecb() {
struct test {
u8 key[8];
u8 plain[8];
u8 cipher[8];
} tests[] =
{
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x45, 0x97, 0xf9, 0x4e, 0x78, 0xdd, 0x98, 0x61}
},
{
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
{0xd5, 0x6f, 0x86, 0x51, 0x8a, 0xcb, 0x5e, 0xb8},
},
{
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0x87, 0xdd, 0x66, 0x24, 0x9d, 0x3c, 0x96, 0x8b},
},
{
{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
{0x80, 0xc3, 0xf9, 0x61, 0x96, 0xb0, 0x81, 0x22},
},
{
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x45, 0x97, 0xf9, 0x4e, 0x78, 0xdd, 0x98, 0x61},
},
{
{0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x88, 0x46, 0x59, 0x24, 0x9a, 0x36, 0x54, 0x57},
},
{
{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0x9c, 0x5a, 0x5c, 0x6b, 0x5a, 0x0a, 0x9e, 0x5d},
},
};
for (int i = 0; i < int(sizeof(tests)/sizeof(test)); i++) {
Blowfish bf(tests[i].key, 8);
const auto [cipher, n] = bf.encrypt_ecb(tests[i].plain, sizeof(tests[i].plain));
assert(Crypto::compare_bytes(cipher.get(), tests[i].cipher, n));
const auto [plain, k] = bf.decrypt_ecb(cipher.get(), 8);
assert(Crypto::compare_bytes(plain.get(), tests[i].plain, k));
}
cout << "blowfish_test_ecb: OK" << endl;
}
/**
* @brief blowfish_test_cbc_without_iv
*/
void blowfish_test_cbc_without_iv() {
vector<u8> key[] =
{
{ 0x00, 0x01, 0x02, 0x03 },
{ 0x00, 0x01, 0x02, 0x03, 0x04 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8, 0x09 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8, 0x09, 0x0a },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8, 0x09, 0x0a, 0x0b },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8, 0x09, 0x0a, 0x0b, 0x0c },
{ 0xff, 0xfe, 0xfd, 0x0c },
{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb },
{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa },
{ 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9 },
{ 0xdf, 0xde, 0xdd, 0xdc, 0xdb, 0xda, 0xd9, 0xd8 },
{ 0xcf, 0xce, 0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc8, 0xc7 },
{ 0xbf, 0xbe, 0xbd, 0xbc, 0xbb, 0xba, 0xb9, 0xb8, 0xb7, 0xb8 },
{ 0xaf, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9, 0xa8, 0xa7, 0xa8, 0xa6 },
{ 0x9f, 0x9e, 0x9d, 0x9c, 0x9b, 0x9a, 0x99, 0x98, 0x97, 0x98, 0x96, 0x95 },
{ 0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x88, 0x86, 0x85, 0x84 },
};
vector<string> plain = {
string("Beesoft Software, Piotr Pszczółkowski"),
string("Beesoft Software, Piotr Pszczółkowsk"),
string("Beesoft Software, Piotr Pszczółkows"),
string("Beesoft Software, Piotr Pszczółkow"),
string("Beesoft Software, Piotr Pszczółko"),
string("Beesoft Software, Piotr Pszczółk"),
string("Beesoft Software, Piotr Pszczół"),
string("Beesoft Software, Piotr Pszczó"),
string("Beesoft Software, Piotr Pszcz"),
string("Beesoft Software, Piotr Pszc"),
string("Beesoft Software, Piotr Psz"),
string("Beesoft Software, Piotr Ps"),
string("Beesoft Software, Piotr P"),
string("Beesoft Software, Piotr "),
string("Beesoft Software, Piotr"),
string("Beesoft Software, Piot"),
string("Beesoft Software, Pio"),
string("Beesoft Software, Pi"),
string("Beesoft Software, P"),
string("Beesoft Software, "),
string("Beesoft Software,"),
string("Beesoft Software"),
string("Beesoft"),
string("")
};
for (int i = 0; i < 20; i++) {
Blowfish bf(key[i].data(), key[i].size());
for (int j = 0; j < 24; j++) {
const auto [cipher, n] = bf.encrypt_cbc(plain[j].data(), plain[j].size());
const auto [decipher, k] = bf.decrypt_cbc(cipher.get(), n);
assert(string(static_cast<char*>(decipher.get()), k) == plain[j]);
}
}
cout << "blowfish_test_cbc_without_iv: OK" << endl;
}
/**
* @brief blowfish_test_cbc_with_iv
*/
void blowfish_test_cbc_with_iv() {
vector<u8> key[] = {
{ 0x00, 0x01, 0x02, 0x03 },
{ 0x00, 0x01, 0x02, 0x03, 0x04 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8, 0x09 },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8, 0x09, 0x0a },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8, 0x09, 0x0a, 0x0b },
{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x8, 0x09, 0x0a, 0x0b, 0x0c },
{ 0xff, 0xfe, 0xfd, 0x0c },
{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb },
{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa },
{ 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9 },
{ 0xdf, 0xde, 0xdd, 0xdc, 0xdb, 0xda, 0xd9, 0xd8 },
{ 0xcf, 0xce, 0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc8, 0xc7 },
{ 0xbf, 0xbe, 0xbd, 0xbc, 0xbb, 0xba, 0xb9, 0xb8, 0xb7, 0xb8 },
{ 0xaf, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9, 0xa8, 0xa7, 0xa8, 0xa6 },
{ 0x9f, 0x9e, 0x9d, 0x9c, 0x9b, 0x9a, 0x99, 0x98, 0x97, 0x98, 0x96, 0x95 },
{ 0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x88, 0x86, 0x85, 0x84 },
};
vector<string> plain = {
string("Beesoft Software, Piotr Pszczółkowski"),
string("Beesoft Software, Piotr Pszczółkowsk"),
string("Beesoft Software, Piotr Pszczółkows"),
string("Beesoft Software, Piotr Pszczółkow"),
string("Beesoft Software, Piotr Pszczółko"),
string("Beesoft Software, Piotr Pszczółk"),
string("Beesoft Software, Piotr Pszczół"),
string("Beesoft Software, Piotr Pszczó"),
string("Beesoft Software, Piotr Pszcz"),
string("Beesoft Software, Piotr Pszc"),
string("Beesoft Software, Piotr Psz"),
string("Beesoft Software, Piotr Ps"),
string("Beesoft Software, Piotr P"),
string("Beesoft Software, Piotr "),
string("Beesoft Software, Piotr"),
string("Beesoft Software, Piot"),
string("Beesoft Software, Pio"),
string("Beesoft Software, Pi"),
string("Beesoft Software, P"),
string("Beesoft Software, "),
string("Beesoft Software,"),
string("Beesoft Software"),
string("Beesoft"),
string("")
};
vector<u8> iv[] = {
{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 },
{ 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10 },
{ 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x1a },
{ 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x2b },
{ 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x3c },
{ 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x4d },
{ 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x5e },
{ 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xa0 },
{ 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xb0 },
{ 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xc0 },
{ 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xd0 },
{ 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xe0 },
};
for (int i = 0; i < 20; i++) {
Blowfish bf(key[i].data(), key[i].size());
for (int j = 0; j < 24; j++) {
for (int m = 0; m < 12; m++) {
const auto [cipher, n] = bf.encrypt_cbc(plain[j].data(), plain[j].size(), iv[m].data());
const auto [decipher, k] = bf.decrypt_cbc(cipher.get(), n);
assert(string(static_cast<char*>(decipher.get()), k) == plain[j]);
}
}
}
cout << "blowfish_test_cbc_with_iv: OK" << endl;
}