forked from JackTrapper/scrypt-for-delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCryptTests.pas
1710 lines (1420 loc) · 49.5 KB
/
SCryptTests.pas
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit SCryptTests;
interface
uses
TestFramework, SysUtils, Scrypt;
type
TScryptTests = class(TTestCase)
protected
FScrypt: TScrypt;
FFreq: Int64;
procedure SetUp; override;
procedure TearDown; override;
function GetTimestamp: Int64;
procedure Tester_HMAC_SHA1(HMACsha1: IHmacAlgorithm);
procedure Tester_HMAC_SHA256(HMACsha256: IHmacAlgorithm);
procedure Tester_PBKDF2_SHA1(Pbkdf: IPBKDF2Algorithm);
procedure Tester_PBKDF2_SHA256(Pbkdf2sha256: IPBKDF2Algorithm);
procedure Test_Scrypt_PasswordFormatting; //todo
public
published
//Even though we don't use SHA-1, we implemented it because PBKDF2_SHA1 is the only one with published test vectors
procedure Test_SHA1;
procedure Test_SHA1_PurePascal;
procedure Benchmark_SHA1_PurePascal; //because native code should be fast
procedure Test_SHA1_Csp;
procedure Test_SHA1_Cng;
//Scrypt uses PBKDF2_SHA256
procedure Test_SHA256;
procedure Test_SHA256_PurePascal;
procedure Benchmark_SHA256_PurePascal;
procedure Test_SHA256_Csp;
procedure Test_SHA256_Cng;
procedure BenchmarkHashes;
procedure Test_HMAC_SHA1;
procedure Test_HMAC_SHA1_PurePascal;
procedure Test_HMAC_SHA1_Cng;
procedure Test_HMAC_SHA256;
procedure Test_HMAC_SHA256_PurePascal;
procedure Test_HMAC_SHA256_Cng;
procedure Benchmark_HMACs;
procedure Test_PBKDF2_SHA1;
procedure Test_PBKDF2_SHA1_PurePascal;
procedure Test_PBKDF2_SHA1_Cng;
procedure Test_PBKDF2_SHA256;
procedure Test_PBKDF2_SHA256_PurePascal;
procedure Test_PBKDF2_SHA256_Cng;
procedure Benchmark_PBKDF2s;
procedure Test_Salsa208Core;
procedure Test_BlockMix;
procedure Test_ROMix;
procedure Test_Scrypt;
procedure Test_PasswordHashing;
procedure Test_PasswordHashPerformance;
procedure ScryptBenchmarks;
end;
TSHA1Tester = class(TObject)
protected
FSha1: IHashAlgorithm;
procedure SelfTest_Sizes; //block and digest sizes are right
procedure SelfTestA; //FIPS-180 AppendixA Test
procedure SelfTestB; //FIPS-180 AppendixB Test
procedure SelfTestC; //FIPS-180 AppendixC Test
procedure SelfTestD; //string i found that crashes the algoritm
public
constructor Create(SHA1: IHashAlgorithm);
destructor Destroy; override;
class procedure Test(SHA1: IHashAlgorithm);
end;
TSHA256Tester = class(TObject)
private
FSHA256: IHashAlgorithm;
function HexStringToBytes(s: string): TBytes;
function StringOfString(const s: string; Count: Integer): string;
procedure t(const s: string; expectedHash: string);
procedure tb(const Buffer; BufferLength: Integer; ExpectedHash: string);
protected
procedure TestSizes;
procedure OfficialVectors;
procedure OfficialVectors_HugeBuffers; //hashing of 100MB, or even 1.6 GB buffers - slow and out of memory
procedure UnofficialVectors;
public
constructor Create(SHA256: IHashAlgorithm);
destructor Destroy; override;
class procedure Test(SHA256: IHashAlgorithm);
end;
TScryptCracker = class(TScrypt)
public
end;
implementation
uses
Windows;
function HexToBytes(s: string): TBytes;
var
i, j: Integer;
n: Integer;
begin
for i := Length(s) downto 1 do
begin
if s[i] = ' ' then
Delete(s, i, 1);
end;
SetLength(Result, Length(s) div 2);
i := 1;
j := 0;
while (i < Length(s)) do
begin
n := StrToInt('0x'+s[i]+s[i+1]);
Result[j] := n;
Inc(i, 2);
Inc(j, 1);
end;
end;
{ TSHA1Tests }
constructor TSHA1Tester.Create(SHA1: IHashAlgorithm);
begin
inherited Create;
FSha1 := SHA1;
end;
destructor TSHA1Tester.Destroy;
begin
FSHA1 := nil;
inherited;
end;
procedure TSHA1Tester.SelfTestA;
const
Answer: array[0..19] of Byte =
($A9, $99, $3E, $36,
$47, $06, $81, $6A,
$BA, $3E, $25, $71,
$78, $50, $C2, $6C,
$9C, $D0, $D8, $9D);
var
Digest: TBytes;
TestData: TBytes;
szInData: Utf8String;
begin
{This is the test data from FIPS-180 Appendix A}
szInData := 'abc';
testData := BytesOf(szInData);
FSha1.HashData(testData[0], Length(testData));
Digest := FSha1.Finalize;
if not CompareMem(@digest[0], @Answer[0], Length(Answer)) then
raise EScryptException.Create('SHA-1 self-test A failed');
end;
procedure TSHA1Tester.SelfTestB;
const
Answer: array[0..19] of Byte =
($84, $98, $3E, $44,
$1C, $3B, $D2, $6E,
$BA, $AE, $4A, $A1,
$F9, $51, $29, $E5,
$E5, $46, $70, $F1);
var
digest: TBytes;
testData: TBytes;
szInData: Utf8String;
begin
{This is the test data from FIPS-180 Appendix B}
szInData := 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq';
testData := BytesOf(szInData);
FSha1.HashData(TestData[0], Length(TestData));
digest := FSha1.Finalize;
if not CompareMem(@digest[0], @Answer[0], Sizeof(Answer)) then
raise EScryptException.Create('SHA-1 self-test B failed');
end;
procedure TSHA1Tester.SelfTestC;
const
Answer: array[0..19] of Byte =
($34, $AA, $97, $3C,
$D4, $C4, $DA, $A4,
$F6, $1E, $EB, $2B,
$DB, $AD, $27, $31,
$65, $34, $01, $6F);
var
digest: TBytes;
testData: TBytes;
testValue: Byte;
begin
{Build a string which consists of 1,000,000 repetitions of "a"}
testValue := Ord('a');
SetLength(TestData, 1000000);
FillChar(TestData[0], 1000000, TestValue);
FSha1.HashData(testData[0], Length(testData));
digest := FSha1.Finalize;
if not CompareMem(@Digest[0], @Answer[0], Length(Answer)) then
raise EScryptException.Create('SHA-1 self-test C failed');
end;
procedure TSHA1Tester.SelfTestD;
const
Answer: array[0..19] of Byte =
($85, $B6, $95, $33,
$89, $5F, $9C, $08,
$18, $4F, $1F, $16,
$3C, $91, $51, $FD,
$47, $B1, $E4, $9E);
var
digest: TBytes;
TestData: TBytes;
TestValue: Byte;
begin
{Build a string which consists of 202 repetitions of "a"}
TestValue := Ord('a');
SetLength(TestData, 202);
FillChar(TestData[0], 202, TestValue);
FSha1.HashData(TestData[0], Length(testData));
digest := FSha1.Finalize;
if not CompareMem(@digest[0], @Answer[0], Sizeof(Answer)) then
raise EScryptException.Create('SHA-1 self-test A failed');
end;
procedure TSHA1Tester.SelfTest_Sizes;
begin
if FSha1.BlockSize <> 64 then
raise EScryptException.CreateFmt('SHA1 block size (%d) was not 64 bytes', [FSha1.BlockSize]);
if FSha1.DigestSize <> 20 then
raise EScryptException.CreateFmt('SHA1 digest size (%d) was not 20 bytes', [FSha1.DigestSize]);
end;
class procedure TSHA1Tester.Test(SHA1: IHashAlgorithm);
var
tester: TSHA1Tester;
begin
tester := TSHA1Tester.Create(SHA1);
try
tester.SelfTest_Sizes;
tester.SelfTestA;
tester.SelfTestB;
tester.SelfTestC;
tester.SelfTestD;
finally
tester.Free;
end;
end;
{ TScryptTests }
procedure TScryptTests.Test_SHA256;
var
sha256: IHashAlgorithm;
begin
sha256 := TScryptCracker.CreateObject('SHA256') as IHashAlgorithm;
TSHA256Tester.Test(sha256);
end;
procedure TScryptTests.Test_SHA256_Cng;
var
sha256: IHashAlgorithm;
begin
sha256 := TScryptCracker.CreateObject('SHA256.Cng') as IHashAlgorithm;
TSHA256Tester.Test(sha256);
end;
procedure TScryptTests.Test_SHA256_Csp;
var
sha256: IHashAlgorithm;
begin
sha256 := TScryptCracker.CreateObject('SHA256.Csp') as IHashAlgorithm;
TSHA256Tester.Test(sha256);
end;
procedure TScryptTests.Test_SHA256_PurePascal;
var
sha256: IHashAlgorithm;
begin
sha256 := TScryptCracker.CreateObject('SHA256.PurePascal') as IHashAlgorithm;
TSHA256Tester.Test(sha256);
end;
procedure TScryptTests.SetUp;
begin
inherited;
FScrypt := TScrypt.Create;
if not QueryPerformanceFrequency(FFreq) then //it's documented to never fail, but it can (see SO).
FFreq := -1;
end;
procedure TScryptTests.TearDown;
begin
FreeAndNil(FScrypt);
inherited;
end;
{ TSHA256Tester }
function TSHA256Tester.StringOfString(const s: string; Count: Integer): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Count do
Result := Result+s;
end;
constructor TSHA256Tester.Create(SHA256: IHashAlgorithm);
begin
inherited Create;
FSHA256 := SHA256;
end;
destructor TSHA256Tester.Destroy;
begin
FSHA256 := nil;
inherited;
end;
function TSHA256Tester.HexStringToBytes(s: string): TBytes;
var
i, j: Integer;
n: Integer;
begin
for i := Length(s) downto 1 do
begin
if s[i] = ' ' then
Delete(s, i, 1);
end;
SetLength(Result, Length(s) div 2);
i := 1;
j := 0;
while (i < Length(s)) do
begin
n := StrToInt('0x'+s[i]+s[i+1]);
Result[j] := n;
Inc(i, 2);
Inc(j, 1);
end;
end;
procedure TSHA256Tester.tb(const Buffer; BufferLength: Integer; ExpectedHash: string);
var
digest: TBytes;
expectedBytes: TBytes;
begin
FSHA256.HashData(Buffer, BufferLength);
digest := FSHA256.Finalize;
expectedBytes := HexStringToBytes(ExpectedHash);
if not CompareMem(@digest[0], @expectedBytes[0], Length(expectedBytes)) then
raise EScryptException.Create('SHA2-256 hash failed');
end;
procedure TSHA256Tester.t(const s: string; expectedHash: string);
var
utf8Data: TBytes;
digest: TBytes;
expectedBytes: TBytes;
begin
utf8Data := TEncoding.UTF8.GetBytes(s);
expectedBytes := HexStringToBytes(expectedHash);
FSHA256.HashData(utf8Data[0], Length(utf8Data));
digest := FSHA256.Finalize;
if not CompareMem(@digest[0], @expectedBytes[0], Length(expectedBytes)) then
raise EScryptException.Create('SHA2-256 hash failed');
end;
procedure TSHA256Tester.OfficialVectors;
var
buff: TBytes;
begin
{
http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
}
t('abc', 'BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD');
t('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '248D6A61 D20638B8 E5C02693 0C3E6039 A33CE459 64FF2167 F6ECEDD4 19DB06C1');
SetLength(buff, 3);
buff[0] := $61; //'a'
buff[1] := $62; //'b'
buff[2] := $63; //'c'
tb(buff[0], Length(buff), 'BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD');
{
http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA2_Additional.pdf
}
//#1) 1 byte 0xbd
SetLength(buff, 1);
buff[0] := $bd;
tb(buff[0], Length(buff), '68325720 aabd7c82 f30f554b 313d0570 c95accbb 7dc4b5aa e11204c0 8ffe732b');
//#2) 4 bytes 0xc98c8e55
SetLength(buff, 4);
PDWORD(@buff[0])^ := $558e8cc9;
tb(buff[0], Length(buff), '7abc22c0 ae5af26c e93dbb94 433a0e0b 2e119d01 4f8e7f65 bd56c61c cccd9504');
//#3) 55 bytes of zeros
SetLength(buff, 55);
FillChar(buff[0], Length(buff), 0);
tb(buff[0], Length(buff), '02779466 cdec1638 11d07881 5c633f21 90141308 1449002f 24aa3e80 f0b88ef7');
//#4) 56 bytes of zeros
SetLength(buff, 56);
FillChar(buff[0], Length(buff), 0);
tb(buff[0], Length(buff), 'd4817aa5 497628e7 c77e6b60 6107042b bba31308 88c5f47a 375e6179 be789fbb');
//#5) 57 bytes of zeros
SetLength(buff, 57);
FillChar(buff[0], Length(buff), 0);
tb(buff[0], Length(buff), '65a16cb7 861335d5 ace3c607 18b5052e 44660726 da4cd13b b745381b 235a1785');
//#6) 64 bytes of zeros
SetLength(buff, 64);
FillChar(buff[0], Length(buff), 0);
tb(buff[0], Length(buff), 'f5a5fd42 d16a2030 2798ef6e d309979b 43003d23 20d9f0e8 ea9831a9 2759fb4b');
//#7) 1000 bytes of zeros
SetLength(buff, 1000);
FillChar(buff[0], Length(buff) , 0);
tb(buff[0], Length(buff), '541b3e9d aa09b20b f85fa273 e5cbd3e8 0185aa4e c298e765 db87742b 70138a53');
//#8) 1000 bytes of 0x41 A
SetLength(buff, 1000);
FillChar(buff[0], Length(buff), $41);
tb(buff[0], Length(buff), 'c2e68682 3489ced2 017f6059 b8b23931 8b6364f6 dcd835d0 a519105a 1eadd6e4');
//#9) 1005 bytes of 0x55 U
SetLength(buff, 1005);
FillChar(buff[0], Length(buff), $55);
tb(buff[0], Length(buff), 'f4d62dde c0f3dd90 ea1380fa 16a5ff8d c4c54b21 740650f2 4afc4120 903552b0');
end;
procedure TSHA256Tester.OfficialVectors_HugeBuffers;
var
data: PByte;
begin
{
http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
}
//#10) 1,000,000 bytes of zeros
GetMem(data, 1000000);
try
FillChar(data^, 1000000, 0);
tb(data^, 1000000, 'd29751f2 649b32ff 572b5e0a 9f541ea6 60a50f94 ff0beedf b0b692b9 24cc8025');
finally
FreeMem(data);
end;
//#11) 0x20000000 (536,870,912) bytes of 0x5a Z
GetMem(data, $20000000);
try
FillChar(data^, $20000000, $5a);
tb(data^, $20000000, '15a1868c 12cc5395 1e182344 277447cd 0979536b adcc512a d24c67e9 b2d4f3dd');
finally
FreeMem(data);
end;
//#12) 0x41000000 (1,090,519,040) bytes of zeros
GetMem(data, $41000000);
try
FillChar(data^, $41000000, 0);
tb(data^, $41000000, '461c19a9 3bd4344f 9215f5ec 64357090 342bc66b 15a14831 7d276e31 cbc20b53');
finally
FreeMem(data);
end;
//#13) 0x6000003e (1,610,612,798) bytes of 0x42 B
GetMem(data, $6000003e);
try
FillChar(data^, $6000003e, $420);
tb(data^, $6000003e, 'c23ce8a7 895f4b21 ec0daf37 920ac0a2 62a22004 5a03eb2d fed48ef9 b05aabea');
finally
FreeMem(data);
end;
end;
class procedure TSHA256Tester.Test(SHA256: IHashAlgorithm);
var
t: TSHA256Tester;
begin
t := TSHA256Tester.Create(SHA256);
try
t.TestSizes;
t.OfficialVectors;
t.UnofficialVectors;
finally
t.Free;
end;
end;
procedure TSHA256Tester.TestSizes;
begin
if FSha256.BlockSize <> 64 then
raise EScryptException.CreateFmt('SHA256 block size (%d) was not 64 bytes', [FSha256.BlockSize]);
if FSha256.DigestSize <> 32 then
raise EScryptException.CreateFmt('SHA256 digest size (%d) was not 32 bytes', [FSha256.DigestSize]);
end;
procedure TSHA256Tester.UnofficialVectors;
begin
{
https://www.cosic.esat.kuleuven.be/nessie/testvectors/hash/sha/Sha-2-256.unverified.test-vectors
}
t('abcdefghijklmnopqrstuvwxyz', '71C480DF93D6AE2F1EFAD1447C66C9525E316218CF51FC8D9ED832F2DAF18B73');
t('', 'E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855');
t('a', 'CA978112CA1BBDCAFAC231B39A23DC4DA786EFF8147C4E72B9807785AFEE48BB');
t('abc', 'BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD');
t('message digest', 'F7846F55CF23E14EEBEAB5B4E1550CAD5B509E3348FBC4EFA3A1413D393CB650');
t('abcdefghijklmnopqrstuvwxyz', '71C480DF93D6AE2F1EFAD1447C66C9525E316218CF51FC8D9ED832F2DAF18B73');
t('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq', '248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1');
t('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 'DB4BFCBD4DA0CD85A60C3C37D3FBD8805C77F15FC6B1FDFE614EE0A7C8FDB4C0');
t(StringOfString('1234567890', 8), 'F371BC4A311F2B009EEF952DD83CA80E2B60026C8E935592D0F9C308453C813E');
t(StringOfChar('a', 1000000), 'CDC76E5C9914FB9281A1C7E284D73E67F1809A48A497200E046D39CCC7112CD0');
t('The quick brown fox jumps over the lazy dog', 'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592');
end;
procedure TScryptTests.BenchmarkHashes;
var
data: TBytes;
procedure Test(HashAlgorithmName: string);
var
hash: IHashAlgorithm;
t1, t2: Int64;
bestTime: Int64;
i: Integer;
begin
hash := TScryptCracker.CreateObject(HashAlgorithmName) as IHashAlgorithm;
bestTime := 0;
//Fastest time of 30 runs
for i := 1 to 30 do
begin
t1 := GetTimestamp;
hash.HashData(data[0], Length(data));
t2 := GetTimestamp;
t2 := t2-t1;
if (bestTime = 0) or (t2 < bestTime) then
bestTime := t2;
end;
Status(Format('%s %.3f MB/s', [HashAlgorithmName, (Length(data)/1024/1024) / (bestTime/FFreq)]));
end;
begin
data := TScrypt.GetBytes('hash test', 'Scrypt for Delphi', 1, 1, 1, 1*1024*1024); //1 MB
Status(Format('%s %s', ['Algorithm', 'Speed (MB/s)']));
Test('SHA1.PurePascal');
Test('SHA1.Csp');
Test('SHA1.Cng');
Test('SHA256.PurePascal');
Test('SHA256.Csp');
Test('SHA256.Cng');
end;
procedure TScryptTests.Benchmark_SHA1_PurePascal;
var
hash: IHashAlgorithm;
t1, t2: Int64;
data: TBytes;
best: Int64;
i: Integer;
begin
//Generate 1 MB of test data to hash
data := TScrypt.GetBytes('hash test', 'Scrypt for Delphi', 1, 1, 1, 1*1024*1024); //1 MB
//Get our pure pascal SHA-1 implementation
hash := TScryptCracker.CreateObject('SHA1.PurePascal') as IHashAlgorithm;
best := 0;
OutputDebugString('SAMPLING ON');
for i := 1 to 60 do
begin
t1 := GetTimestamp;
hash.HashData(data[0], Length(data));
t2 := GetTimestamp;
if (((t2-t1) < best) or (best = 0)) then
best := (t2-t1);
end;
OutputDebugString('SAMPLING OFF');
Status(Format('%s: %.3f MB/s', ['TSHA1', (Length(data)/1024/1024) / (best/FFreq)]));
end;
procedure TScryptTests.Benchmark_SHA256_PurePascal;
var
hash: IHashAlgorithm;
t1, t2: Int64;
data: TBytes;
best: Int64;
i: Integer;
begin
//Generate 1 MB of test data to hash
data := TScrypt.GetBytes('hash test', 'Scrypt for Delphi', 1, 1, 1, 1*1024*1024); //1 MB
//Benchmark SHA256PurePascal with the test data
best := 0;
hash := TScryptCracker.CreateObject('SHA256.PurePascal') as IHashAlgorithm;
OutputDebugString('SAMPLING ON');
for i := 1 to 60 do
begin
t1 := GetTimestamp;
hash.HashData(data[0], Length(data));
t2 := GetTimestamp;
if (((t2-t1) < best) or (best = 0)) then
best := (t2-t1);
end;
OutputDebugString('SAMPLING OFF');
Status(Format('%s: %.3f MB/s', ['SHA256', (Length(data)/1024/1024) / (best/FFreq)]));
end;
function TScryptTests.GetTimestamp: Int64;
begin
if not QueryPerformanceCounter(Result) then //it's documented to never fail; but it can. See SO
Result := 0;
end;
procedure TScryptTests.ScryptBenchmarks;
var
freq, t1, t2: Int64;
N, r: Integer;
s: string;
RowLeader, ColumnSeparator, RowTrailer: string;
function ElapsedTicks(StartTicks: Int64): Int64;
var
endTicks: Int64;
begin
if not QueryPerformanceCounter(endTicks) then endTicks := 0;
Result := (endTicks - StartTicks);
end;
const
UseTsv: Boolean = True;
N_max = 20; //20 trips up on VM and address space limits
r_max = 16; //8 ==> 1 KB block. 16 ==> 2 KB block
//Tab separated values (copy-paste to Excel)
TsvRowLeader = ''; //'|';
TsvColumnSeparator = ' '; //'|';
TsvRowTrailer = ''; //'|';
//Markdown
MRowLeader = '| ';
MColumnSeparator = ' | ';
MRowTrailer = ' |';
const
STestPassword = 'correct horse battery staple';
begin
QueryPerformanceFrequency(freq);
if UseTsv then
begin
RowLeader := TsvRowLeader;
ColumnSeparator := TsvColumnSeparator;
RowTrailer := TsvRowTrailer;
end
else
begin
RowLeader := MRowLeader;
ColumnSeparator := MColumnSeparator;
RowTrailer := MRowTrailer;
end;
s := RowLeader+'N';
for r := 1 to r_max do
s := s+ColumnSeparator+'r='+IntToStr(r);
s := s+RowTrailer;
Status(s);
if not UseTsv then
begin
s := '|---';
for r := 1 to r_max do
s := s+'|----';
s := s+'|';
Status(s);
end;
for N := 1 to N_max do
begin
s := RowLeader+IntToStr(N);
for r := 1 to r_max do
begin
if (N < 16*r) and ((1 shl N)*r*Int(128) < $7FFFFFFF) then
begin
try
QueryPerformanceCounter(t1);
TScrypt.HashPassword(STestPassword, N, r, 1);
t2 := ElapsedTicks(t1);
s := s+Format(ColumnSeparator+'%.1f', [t2/freq*1000]);
except
on E:EOutOfMemory do
begin
s := s+Format(ColumnSeparator+'%s', ['#mem']);
end;
end;
end
else
begin
//invalid cost
s := s+ColumnSeparator+'#N/A';
end;
end;
s := s+RowTrailer;
Status(s);
end;
end;
procedure TScryptTests.Tester_HMAC_SHA1(HMACsha1: IHmacAlgorithm);
procedure t(const Key: AnsiString; const Data: AnsiString; const ExpectedDigest: array of Byte);
var
digest: TBytes;
begin
digest := HMACsha1.HashData(Key[1], Length(Key), Data[1], Length(Data));
if (Length(ExpectedDigest) <> Length(digest)) then
raise EScryptException.CreateFmt('Scrypt self-test failed: Length failed with Key "%s" and Data "%s"',
[Key, Data]);
if not CompareMem(@ExpectedDigest, @digest[0], 20) then
raise EScryptException.CreateFmt('Scrypt self-test failed: Compare failed with Key "%s" and Data "%s"',
[Key, Data]);
end;
//var
// key: TBytes;
begin
//From RFC 2022: Test Cases for HMAC-MD5 and HMAC-SHA-1
{
test_case = 1
key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
key_len = 20
data = "Hi There"
data_len = 8
digest = 0xb617318655057264e28bc0b6fb378c8ef146be00
}
t(StringOfChar(AnsiChar($0b), 20), 'Hi There', [$b6, $17, $31, $86, $55, $05, $72, $64, $e2, $8b, $c0, $b6, $fb, $37, $8c, $8e, $f1, $46, $be, $00]);
{
test_case = 2
key = "Jefe"
key_len = 4
data = "what do ya want for nothing?"
data_len = 28
digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
}
t('Jefe', 'what do ya want for nothing?', [$ef, $fc, $df, $6a, $e5, $eb, $2f, $a2, $d2, $74, $16, $d5, $f1, $84, $df, $9c, $25, $9a, $7c, $79]);
{
test_case = 3
key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
key_len = 20
data = 0xdd repeated 50 times
data_len = 50
digest = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
}
t(StringOfChar(AnsiChar($aa), 20), StringOfChar(AnsiChar($dd), 50), [$12, $5d, $73, $42, $b9, $ac, $11, $cd, $91, $a3, $9a, $f4, $8a, $a1, $7b, $4f, $63, $f1, $75, $d3]);
{
test_case = 4
key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819
key_len = 25
data = 0xcd repeated 50 times
data_len = 50
digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
}
t(#$01#$02#$03#$04#$05#$06#$07#$08#$09#$0a#$0b#$0c#$0d#$0e#$0f#$10#$11#$12#$13#$14#$15#$16#$17#$18#$19,
StringOfChar(AnsiChar($cd), 50),
[$4c,$90,$07,$f4,$02,$62,$50,$c6,$bc,$84,$14,$f9,$bf,$50,$c8,$6c,$2d,$72,$35,$da]);
{
test_case = 5
key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
key_len = 20
data = "Test With Truncation"
data_len = 20
digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
digest-96 = 0x4c1a03424b55e07fe7f27be1
}
t(StringOfChar(AnsiChar($0c), 20), 'Test With Truncation',
[$4c,$1a,$03,$42,$4b,$55,$e0,$7f,$e7,$f2,$7b,$e1,$d5,$8b,$b9,$32,$4a,$9a,$5a,$04]);
{
test_case = 6
key = 0xaa repeated 80 times
key_len = 80
data = "Test Using Larger Than Block-Size Key - Hash Key First"
data_len = 54
digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
}
t(StringOfChar(AnsiChar($aa), 80), 'Test Using Larger Than Block-Size Key - Hash Key First',
[$aa,$4a,$e5,$e1,$52,$72,$d0,$0e,$95,$70,$56,$37,$ce,$8a,$3b,$55,$ed,$40,$21,$12]);
{
test_case = 7
key = 0xaa repeated 80 times
key_len = 80
data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
data_len = 73
digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
}
t(StringOfChar(AnsiChar($aa), 80),
'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data',
[$e8,$e9,$9d,$0f,$45,$23,$7d,$78,$6d,$6b,$ba,$a7,$96,$5c,$78,$08,$bb,$ff,$1a,$91]);
end;
procedure TScryptTests.Test_HMAC_SHA1;
var
hash: IHmacAlgorithm;
begin
hash := TScryptCracker.CreateObject('HMAC.SHA1') as IHmacAlgorithm;
Tester_HMAC_SHA1(hash);
hash := nil;
end;
procedure TScryptTests.Test_HMAC_SHA1_Cng;
var
hash: IHmacAlgorithm;
begin
hash := TScryptCracker.CreateObject('HMAC.SHA1.Cng') as IHmacAlgorithm;
Tester_HMAC_SHA1(hash);
hash := nil;
end;
procedure TScryptTests.Test_HMAC_SHA1_PurePascal;
var
hash: IHmacAlgorithm;
begin
hash := TScryptCracker.CreateObject('HMAC.SHA1.PurePascal') as IHmacAlgorithm;
Tester_HMAC_SHA1(hash);
hash := nil;
end;
procedure TScryptTests.Test_HMAC_SHA256;
var
hmac: IHmacAlgorithm;
begin
hmac := TScryptCracker.CreateObject('HMAC.SHA256') as IHmacAlgorithm;
Tester_HMAC_SHA256(hmac);
end;
procedure TScryptTests.Test_HMAC_SHA256_Cng;
var
hmac: IHmacAlgorithm;
begin
hmac := TScryptCracker.CreateObject('HMAC.SHA256.Cng') as IHmacAlgorithm;
Tester_HMAC_SHA256(hmac);
end;
procedure TScryptTests.Test_HMAC_SHA256_PurePascal;
var
hmac: IHmacAlgorithm;
begin
hmac := TScryptCracker.CreateObject('HMAC.SHA256.PurePascal') as IHmacAlgorithm;
Tester_HMAC_SHA256(hmac);
end;
procedure TScryptTests.Tester_HMAC_SHA256(HMACsha256: IHmacAlgorithm);
procedure Test(const KeyHexString: string; const DataHexString: string; const ExpectedDigestHexString: string; TruncateToBytes: Integer=0);
var
key: TBytes;
data: TBytes;
expected: TBytes;
actual: TBytes;
begin
//Deserialize the test data
key := HexToBytes(KeyHexString);
data := HexToBytes(DataHexString);
expected := HexToBytes(ExpectedDigestHexString);
actual := HMACsha256.HashData(key[0], Length(key), data[0], Length(data));
if TruncateToBytes > 0 then
actual := Copy(actual, 0, TruncateToBytes);
if (Length(expected) <> Length(actual)) then
raise EScryptException.CreateFmt('Scrypt self-test failed: Length failed with Key "%s" and Data "%s"',
[Key, Data]);
if not CompareMem(@expected[0], @actual[0], Length(expected)) then
raise EScryptException.CreateFmt('Scrypt self-test failed: Compare failed with Key "%s" and Data "%s"',
[Key, Data]);
end;
begin
{
From RFC4231
Identifiers and Test Vectors for HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512
https://www.ietf.org/rfc/rfc4231.txt
}
//Test Case 1
Test(
{Key= } '0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', // (20 bytes)
{Data= } '4869205468657265', // ("Hi There")
{HMAC-SHA-256} 'b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7');
//Test Case 2
//Test with a key shorter than the length of the HMAC output.
Test(
{Key =} '4a656665', // ("Jefe")
{Data =} '7768617420646f2079612077616e7420666f72206e6f7468696e673f', // ("what do ya want for nothing?")
{HMAC-SHA-256} '5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843');
//Test Case 3
//Test with a combined length of key and data that is larger than 64 bytes (= block-size of SHA-224 and SHA-256).
Test(
{Key} 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', // (20 bytes)
{Data} 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd', // (50 bytes)
{HMAC-SHA-256} '773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe');
//Test Case 4
//Test with a combined length of key and data that is larger than 64 bytes (= block-size of SHA-224 and SHA-256).
Test(
{Key} '0102030405060708090a0b0c0d0e0f10111213141516171819', // (25 bytes)
{Data} 'cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd', // (50 bytes)
{HMAC-SHA-256} '82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b');
//Test Case 5
//Test with a truncation of output to 128 bits.
Test(
{Key} '0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', // (20 bytes)
{Data} '546573742057697468205472756e636174696f6e', // ("Test With Truncation")
{HMAC-SHA-256} 'a3b6167473100ee06e0c796c2955552b', 16);
//Test Case 6
//Test with a key larger than 128 bytes (= block-size of SHA-384 and SHA-512).
Test(
{Key} 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', // (131 bytes)
{Data} '54657374205573696e67204c6172676572205468616e20426c6f636b2d53697a'+