/
DECTest.dpr
870 lines (797 loc) · 22.4 KB
/
DECTest.dpr
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
program DECTest;
{$IFDEF FPC}
{$MODE Delphi}
{$IFDEF MSWINDOWS}
{$APPTYPE console}
{$ENDIF}
{$ENDIF}
uses
Classes,
{$IFDEF MSWINDOWS}
DECCPU,
windows,
{$ENDIF}
{$IFDEF FPC}
EpikTimer,
{$ENDIF}
SysUtils,
TypInfo,
DECCRC,
DECUtil,
DECFmt,
DECHash,
DECCipher,
DECRandom,
Variants; // Variants required for SetPropValue in FPC (else access violation when converting AnsiString to Variant)
{$R *.res}
procedure RegisterClasses;
begin
RegisterDECClasses([TFormat_HEX, TFormat_HEXL, TFormat_MIME32, TFormat_MIME64,
TFormat_PGP, TFormat_UU, TFormat_XX, TFormat_ESCAPE]);
// preferred hashes
THash_MD2.Register; // 1.5Kb
THash_MD4.Register; // 2.0Kb // for fast checksums
THash_MD5.Register; // 2.5Kb
THash_SHA.Register; // 10Kb for SHA,SHA1,SHA256 // strong
THash_SHA1.Register;
THash_SHA256.Register;
THash_SHA384.Register; // 3.0Kb for SHA384,SHA512
THash_SHA512.Register; // variable digest
THash_Sapphire.Register; // 1.0Kb
THash_Panama.Register; // 2.0Kb
THash_Tiger.Register; // 12.0kb
THash_RipeMD128.Register; // 4.0Kb
THash_RipeMD160.Register; // 8.0Kb
THash_RipeMD256.Register; // 4.5Kb
THash_RipeMD320.Register; // 9.0Kb
THash_Haval128.Register; // 6.0Kb for all Haval's
THash_Haval160.Register;
THash_Haval192.Register;
THash_Haval224.Register;
THash_Haval256.Register;
THash_Whirlpool.Register; // 10.0Kb
THash_Whirlpool1.Register; // 10.0Kb
THash_Square.Register; // 10Kb
THash_Snefru128.Register; // 18Kb
THash_Snefru256.Register; //
// TCipher_Null.Register;
TCipher_Blowfish.Register;
TCipher_Twofish.Register;
TCipher_IDEA.Register;
TCipher_CAST256.Register;
TCipher_Mars.Register;
TCipher_RC4.Register;
TCipher_RC6.Register;
TCipher_Rijndael.Register;
TCipher_Square.Register;
TCipher_SCOP.Register;
TCipher_Sapphire.Register;
TCipher_1DES.Register;
TCipher_2DES.Register;
TCipher_3DES.Register;
TCipher_2DDES.Register;
TCipher_3DDES.Register;
TCipher_3TDES.Register;
TCipher_3Way.Register;
TCipher_Cast128.Register;
TCipher_Gost.Register;
TCipher_Misty.Register;
TCipher_NewDES.Register;
TCipher_Q128.Register;
TCipher_RC2.Register;
TCipher_RC5.Register;
TCipher_SAFER.Register;
TCipher_Shark.Register;
TCipher_Skipjack.Register;
TCipher_TEA.Register;
TCipher_TEAN.Register;
end;
function DoEnumClasses(Data: Pointer; const ClassType: TDECClass): Boolean;
begin
Result := False;
WriteLn(IntToHEX(ClassType.Identity, 8), ' : ', ClassType.ClassName);
end;
procedure PrintRegisteredClasses;
begin
WriteLn('registered classes');
WriteLn;
DECEnumClasses(@DoEnumClasses, nil);
WriteLn;
end;
type
TTestProc = procedure of object;
// process testcases in file DECTest.vec
TTestRunner = class
private
FVectorFile: Text;
FCurChar: PAnsiChar;
FLineNo: Integer;
FInstance: TObject;
FClassType: TDECClass;
// Cipher only special properties
FPassword: Binary;
FIV: Binary;
FIFiller: Byte;
procedure InvalidLine;
function ExtractClassName: PAnsiChar;
procedure ExtractProperty(Instance: TObject);
function ExtractTestResult: Binary;
function ExtractTest(out Data: Binary; out Count: Integer): Boolean;
procedure TestHash;
procedure TestCipher;
procedure TestFormat;
public
constructor Create(const AFileName: string);
destructor Destroy; override;
procedure Run;
end;
procedure TTestRunner.InvalidLine;
begin
raise Exception.CreateFmt('Invalid line format at %d', [FLineNo]);
end;
function TTestRunner.ExtractClassName: PAnsiChar;
begin
while FCurChar^ in [' ', '['] do Inc(FCurChar);
Result := FCurChar;
while FCurChar^ <> #0 do Inc(FCurChar);
while FCurChar^ in [#0, ']', ' ', #13, #10] do Dec(FCurChar);
FCurChar[1] := #0;
end;
procedure TTestRunner.ExtractProperty(Instance: TObject);
// setup property stored in Testvectors
// format is .PropName=PropValue
var
PropName: PAnsiChar;
begin
while FCurChar^ in [' ', '.'] do Inc(FCurChar);
PropName := FCurChar;
while not (FCurChar^ in [#0, '=']) do Inc(FCurChar);
if FCurChar^ <> #0 then
begin
FCurChar^ := #0;
Inc(FCurChar);
while FCurChar^ in ['=', ' '] do Inc(FCurChar);
if Instance is TDECCipher then
if AnsiCompareText(PropName, 'Password') = 0 then
begin
FPassword := TFormat_Escape.Decode(FCurChar^, StrLen(FCurChar));
with TDECCipher(Instance).Context do
if Length(FPassword) > KeySize then SetLength(FPassword, KeySize);
Exit;
end else
if AnsiCompareText(PropName, 'IV') = 0 then
begin
FIV := TFormat_Escape.Decode(FCurChar^, StrLen(FCurChar));
Exit;
end else
if AnsiCompareText(PropName, 'IFiller') = 0 then
begin
FIFiller := StrToInt(FCurChar);
Exit;
end;
try
SetPropValue(Instance, PropName, AnsiString(FCurChar));
except
on E: Exception do
begin
E.Message := E.Message + ' on ' + Instance.ClassName;
raise;
end;
end;
end else InvalidLine;
end;
function TTestRunner.ExtractTestResult: Binary;
// extract valid test result, and convertion from Escaped string
// repositionate to testcases
var
R,P: PAnsiChar;
begin
while FCurChar^ in [' ', '<'] do Inc(FCurChar);
R := FCurChar;
while not (FCurChar^ in [#0, '>']) do Inc(FCurChar);
if FCurChar^ <> '>' then InvalidLine;
P := FCurChar;
while P^ in ['>', ' '] do Inc(P);
if P^ <> '=' then InvalidLine;
FCurChar^ := #0;
while P^ in ['=', ' ', '>'] do Inc(P);
FCurChar := P;
Result := TFormat_Escape.Decode(R^, StrLen(R));
end;
function TTestRunner.ExtractTest(out Data: Binary; out Count: Integer): Boolean;
// extract one testcase and repetition
var
L: Boolean;
T: Binary;
begin
Result := FCurChar^ <> #0;
if Result then
begin
Count := 0;
Data := '';
while FCurChar^ = ' ' do Inc(FCurChar);
while FCurChar^ in ['0'..'9'] do
begin
Count := Count * 10 + Ord(FCurChar^) - Ord('0');
Inc(FCurChar);
end;
L := FCurChar^ = '!';
while not (FCurChar^ in [#0, '<']) do Inc(FCurChar);
if FCurChar^ = '<' then
begin
Inc(FCurChar);
while not (FCurChar^ in [#0, '>']) do
begin
Data := Data + FCurChar^;
Inc(FCurChar);
end;
if FCurChar^ <> '>' then InvalidLine;
end else InvalidLine;
while FCurChar^ in ['>',','] do Inc(FCurChar);
Data := TFormat_Escape.Decode(Data);
if L then
begin
T := '';
repeat
T := T + Data;
Dec(Count);
until Count <= 0;
Count := 1;
Data := T;
end;
end;
end;
procedure TTestRunner.TestHash;
// apply testcases to hash function
var
Digest: Binary;
Data: Binary;
Count: Integer;
Hash: TDECHash;
begin
Hash := TDECHash(FInstance);
Digest := ExtractTestResult;
Hash.Init;
while ExtractTest(Data, Count) do
repeat
Hash.Calc(Data[1], Length(Data));
Dec(Count);
until Count <= 0;
Hash.Done;
Write(FLineNo:5, ': ', Hash.Classname, ' ');
if AnsiCompareText(Hash.DigestStr(TFormat_HEXL), Digest) <> 0 then WriteLn(Digest, ' != ', Hash.DigestStr(TFormat_HEXL))
else WriteLn('test ok.');
end;
procedure TTestRunner.TestCipher;
var
CipherText,PlainText,TestResult,PlainResult: Binary;
Cipher: TDECCipher;
Count: Integer;
begin
Cipher := TDECCipher(FInstance);
CipherText := ExtractTestResult;
Cipher.Init(FPassword, FIV, FIFiller);
TestResult := '';
PlainResult := '';
while ExtractTest(PlainText, Count) do
begin
PlainResult := PlainResult + PlainText;
TestResult := TestResult + Cipher.EncodeBinary(PlainText, TFormat_Copy);
Dec(Count);
end;
Cipher.Done;
TestResult := TFormat_HEXL.Encode(TestResult);
Write(FLineNo:5, ': ', Cipher.Classname, ' ');
if CipherText <> TestResult then
begin
WriteLn(CipherText, ' != ', TestResult);
Exit;
end;
TestResult := Cipher.DecodeBinary(TestResult, TFormat_HEXL);
if TestResult <> PlainResult then
begin
WriteLn('decode error');
Exit;
end;
WriteLn('test ok.');
end;
procedure TTestRunner.TestFormat;
// apply testcases to conversions function
var
Test,Output,Data: Binary;
Count: Integer;
Format: TDECFormatClass;
begin
Format := TDECFormatClass(FClassType);
Test := ExtractTestResult;
ExtractTest(Data, Count);
Output := Format.Encode(Data);
Write(FLineNo:5, ': ', Format.Classname, ' ');
if Test <> Output then
WriteLn(Test, ' != ', Output)
else if Format.Decode(Output) <> Data then
Writeln('Decode FAILED: ', Format.Decode(Output), ' != ', Data)
else
WriteLn('test ok.');
end;
constructor TTestRunner.Create(const AFileName: string);
begin
Assign(FVectorFile, AFileName);
end;
destructor TTestRunner.Destroy;
begin
Close(FVectorFile);
FreeAndNil(FInstance);
end;
procedure TTestRunner.Run;
var
Line: AnsiString;
TestProc: TTestProc;
begin
WriteLn('processing test cases');
WriteLn;
TestProc := nil;
Reset(FVectorFile);
while not EOF(FVectorFile) do
begin
ReadLn(FVectorFile, Line);
FCurChar := PAnsiChar(Line);
while (FCurChar^ <> #0) and (FCurChar^ = ' ') do Inc(FCurChar);
Inc(FLineNo);
case FCurChar^ of
#0: ;
'#': ; // remark
'[': begin // class
FreeAndNil(FInstance);
TestProc := nil;
FClassType := nil;
if FCurChar[1] <> '#' then
try
FClassType := DECClassByName(ExtractClassName, TDECObject);
if FClassType.InheritsFrom(TDECHash) then
begin
FInstance := FClassType.Create;
TestProc := TestHash;
end else
if FClassType.InheritsFrom(TDECFormat) then
begin
TestProc := TestFormat;
end else
if FClassType.InheritsFrom(TDECCipher) then
begin
FPassword := '';
FIV := '';
FIFiller := $FF;
FInstance := FClassType.Create;
TestProc := TestCipher;
end;
except
on E: Exception do
begin
WriteLn(E.Message);
end;
end;
end;
'.': if FInstance <> nil then
ExtractProperty(FInstance);
'<': if Assigned(TestProc) then // testcase
begin
TestProc;
end;
else
if FClassType <> nil then
InvalidLine;
end;
end;
WriteLn;
end;
const
HashBufferSize = 1024 * 16;
{$IFDEF MSWINDOWS}
function DoSpeedHash(Buffer: PByteArray; HashClass: TDECHashClass): Boolean;
var
Start,Stop: Int64;
ThreadPriority: Cardinal;
ProcessPriority: Cardinal;
I: Integer;
begin
Result := False;
ProcessPriority := GetPriorityClass(GetCurrentProcess);
ThreadPriority := GetThreadPriority(GetCurrentThread);
with HashClass.Create do
try
SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL);
Sleep(0);
Start := DECCPU.RDTSC;
for I := 0 to 4 do
begin
Init;
Calc(Buffer[0], HashBufferSize);
Done;
end;
Stop := (DECCPU.RDTSC - Start) div 5;
WriteLn(ClassName, StringOfChar(' ', 20 - Length(ClassName)), ': ',
Stop/HashBufferSize:10:1, ' cycles/byte ',
CPUSpeed/Stop*HashBufferSize:10:2, ' Mb/sec');
finally
Free;
SetThreadPriority(GetCurrentThread, ThreadPriority);
SetPriorityClass(GetCurrentProcess, ProcessPriority);
end;
Sleep(0);
end;
{$ELSE}
function DoSpeedHash(Buffer: PByteArray; HashClass: TDECHashClass): Boolean;
var
Timer: TEpikTimer;
I: Integer;
begin
Result := False;
Timer := TEpikTimer.Create(nil);
with HashClass.Create do
try
Timer.Start;
for I := 0 to 15 do
begin
Init;
Calc(Buffer[0], HashBufferSize);
Done;
end;
Timer.Stop;
WriteLn(ClassName, StringOfChar(' ', 20 - Length(ClassName)), ': ',
(1/Timer.Elapsed/4):10:2, ' Mb/sec');
finally
Free;
Timer.Free;
end;
end;
{$ENDIF}
procedure SpeedTestHashs;
var
Buffer: Binary;
begin
WriteLn('compute hash performances');
WriteLn;
Buffer := '';
SetLength(Buffer, HashBufferSize);
RandomBuffer(Buffer[1], HashBufferSize);
DECEnumClasses(@DoSpeedHash, Pointer(Buffer), TDECHash);
WriteLn;
end;
const
TEST_VECTOR: array[0..39] of Byte = (
$30, $44, $ED, $6E, $45, $A4, $96, $F5,
$F6, $35, $A2, $EB, $3D, $1A, $5D, $D6,
$CB, $1D, $09, $82, $2D, $BD, $F5, $60,
$C2, $B8, $58, $A1, $91, $F9, $81, $B1,
$00, $00, $00, $00, $00, $00, $00, $00
);
function DoTestCipher(Dummy: Pointer; CipherClass: TDECCipherClass): Boolean;
var
Buffer: array[0..31] of Byte;
Key: Binary;
I: Integer;
begin
Result := False;
Key := CipherClass.ClassName;
I := Length(Key);
with CipherClass.Context do
if I > KeySize then I := KeySize;
SetLength(Key, I);
with CipherClass.Create do
try
Mode := cmCTSx;
Init(Key);
Encode(TEST_VECTOR, Buffer, SizeOf(Buffer));
Done;
Decode(Buffer, Buffer, SizeOf(Buffer));
Done;
if not CompareMem(@TEST_VECTOR, @Buffer, SizeOf(Buffer)) then
WriteLn(ClassName + StringOfChar(' ', 18 - Length(ClassName)), 'selftest fails');
finally
Free;
end;
end;
procedure TestCipher;
begin
DECEnumClasses(@DoTestCipher, nil, TDECCipher);
end;
const
CipherBufferSize = 1024 * 16 * 2;
{$IFDEF MSWINDOWS}
function DoSpeedCipher(Buffer: PByteArray; CipherClass: TDECCipherClass): Boolean;
var
Start,Stop: Int64;
ThreadPriority: Cardinal;
ProcessPriority: Cardinal;
I,S: Integer;
begin
Result := False;
ProcessPriority := GetPriorityClass(GetCurrentProcess);
ThreadPriority := GetThreadPriority(GetCurrentThread);
with CipherClass.Create do
try
Mode := cmECBx;
Init(StringOfChar('x', Context.KeySize));
S := CipherBufferSize shr 1;
I := S mod Context.BufferSize;
Dec(S, I);
SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL);
Sleep(0);
Start := DECCPU.RDTSC;
for I := 0 to 2 do
begin
Encode(Buffer[0], Buffer[S], S);
Done;
Decode(Buffer[0], Buffer[S], S);
Done;
end;
Stop := (DECCPU.RDTSC - Start) div 6;
WriteLn(ClassName, StringOfChar(' ', 20 - Length(ClassName)), ': ',
Stop/S:10:1, ' cycles/byte ',
CPUSpeed/Stop*S:10:2, ' Mb/sec');
finally
Free;
SetThreadPriority(GetCurrentThread, ThreadPriority);
SetPriorityClass(GetCurrentProcess, ProcessPriority);
end;
Sleep(0);
end;
{$ELSE}
function DoSpeedCipher(Buffer: PByteArray; CipherClass: TDECCipherClass): Boolean;
var
Timer: TEpikTimer;
I, S: Integer;
begin
Result := False;
Timer := TEpikTimer.Create(nil);
with CipherClass.Create do
try
Mode := cmECBx;
Init(StringOfChar('x', Context.KeySize));
S := CipherBufferSize shr 1;
I := S mod Context.BufferSize;
Dec(S, I);
Timer.Start;
for I := 0 to 7 do
begin
Encode(Buffer[0], Buffer[S], S);
Done;
Decode(Buffer[0], Buffer[S], S);
Done;
end;
Timer.Stop;
WriteLn(ClassName, StringOfChar(' ', 20 - Length(ClassName)), ': ',
(1/Timer.Elapsed/4):10:2, ' Mb/sec');
finally
Free;
Timer.Free;
end;
end;
{$ENDIF}
procedure SpeedTestCiphers;
var
Buffer: Binary;
begin
WriteLn('compute cipher performances');
WriteLn;
Buffer := '';
SetLength(Buffer, CipherBufferSize);
RandomBuffer(Buffer[1], CipherBufferSize);
DECEnumClasses(@DoSpeedCipher, Pointer(Buffer), TDECCipher);
WriteLn;
end;
procedure DemoCipher(Index: Integer);
// demonstrate en/decryption with cipher Blowfish and use of a
// secure Hash based random KDF -> Key Derivation Function
var
Seed, Encoded, Decoded: Binary;
begin
Seed := RandomBinary(16);
with TCipher_Blowfish.Create do
try
Init(THash_SHA1.KDFx('Password here', Seed, Context.KeySize));
Encoded := EncodeBinary('Secret data here', TFormat_MIME64);
finally
Free;
end;
with TCipher_Blowfish.Create do
try
Init(THash_SHA1.KDFx('Password here', Seed, Context.KeySize));
Decoded := DecodeBinary(Encoded, TFormat_MIME64);
finally
Free;
end;
WriteLn(#13#10'Demo Cipher #', Index);
WriteLn('encoded: ', Encoded);
WriteLn('decoded: ', Decoded);
end;
procedure DemoCipherFile;
// demonstrates a "very" secure application of ciphers, hashes, key derivation functions and random seeds.
procedure EncodeFile(const AFileName: String; const APassword: Binary;
ACipher: TDECCipherClass = nil; AMode: TCipherMode = cmCTSx;
AHash: TDECHashClass = nil);
// The source file will be encrypted, then completely overwritten and deleted.
// The file will be encrypted with a session key that was generated with
// a KDF (Key Derivation Function) and a random number.
// The random number == seed has a size of 128 bits and is stored in the encrypted file.
// It ensured that it will be impossible to crack the password and at the same time it
// randomizes the encryption output. A checksum that was generated using CMAC
// (Cipher Message Authentication Code) is stored at the end of the file.
// Furthermore the encrypted file contains a header with information about the used
// Cipher-/Hash algorithm, CipherMode etc. This makes it possible to automatically
// select the correct algorithms for decrypting the file (as long as one has the password).
// If nil is passed for ACipher and/or AHash, then a default cipher/hash will be used.
// The used session key always has random properties, it's practically random data.
// Only those who know the random seed and APassword are able to decrypt the data.
var
Dest: TStream;
procedure Write(const Value; Size: Integer);
begin
Dest.WriteBuffer(Value, Size);
end;
procedure WriteByte(Value: Byte);
begin
Write(Value, SizeOf(Value));
end;
procedure WriteLong(Value: LongWord);
begin
Value := SwapLong(Value);
Write(Value, SizeOf(Value));
end;
procedure WriteBinary(const Value: Binary);
begin
WriteByte(Length(Value));
Write(Value[1], Length(Value));
end;
var
Source: TStream;
Seed: Binary;
begin
ACipher := ValidCipher(ACipher);
AHash := ValidHash(AHash);
Seed := RandomBinary(16);
Source := TFileStream.Create(AFileName, fmOpenReadWrite);
try
Dest := TFileStream.Create(AFileName + '.enc', fmCreate);
try
with ACipher.Create do
try
Mode := AMode;
Init(AHash.KDFx(APassword, Seed, Context.KeySize));
WriteLong(Identity);
WriteByte(Byte(Mode));
WriteLong(AHash.Identity);
WriteBinary(Seed);
WriteLong(Source.Size);
EncodeStream(Source, Dest, Source.Size);
WriteBinary(CalcMAC);
finally
Free;
end;
finally
Dest.Free;
end;
ProtectStream(Source);
finally
Source.Free;
end;
DeleteFile(AFileName);
end;
procedure DecodeFile(const AFileName: String; const APassword: Binary);
// Decrypt a file previously encrypted with EncodeFile().
var
Source: TStream;
procedure Read(var Value; Size: Integer);
begin
Source.ReadBuffer(Value, Size);
end;
function ReadByte: Byte;
begin
Read(Result, SizeOf(Result));
end;
function ReadLong: LongWord;
begin
Read(Result, SizeOf(Result));
Result := SwapLong(Result);
end;
function ReadBinary: Binary;
begin
SetLength(Result, ReadByte);
Read(Result[1], Length(Result));
end;
var
Dest: TStream;
begin
Source := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
try
try
Dest := TFileStream.Create(ChangeFileExt(AFileName, ''), fmCreate);
try
try
with CipherByIdentity(ReadLong).Create do
try
Mode := TCipherMode(ReadByte);
Init(HashByIdentity(ReadLong).KDFx(APassword, ReadBinary, Context.KeySize));
DecodeStream(Source, Dest, ReadLong);
if ReadBinary <> CalcMAC then
raise EDECException.Create('Invalid decryption');
finally
Free;
end;
except
ProtectStream(Dest);
raise;
end;
finally
Dest.Free;
end;
except
DeleteFile(ChangeFileExt(AFileName, ''));
raise;
end;
finally
Source.Free;
end;
end;
var
FileName: String;
begin
WriteLn(#13#10'File En/Decryption test');
FileName := ChangeFileExt(ParamStr(0), '.test');
if not FileExists(FileName) then
begin
Writeln('Skipped (', FileName, ' does not exist)');
Exit;
end;
SetDefaultCipherClass(TCipher_Rijndael);
SetDefaultHashClass(THash_SHA1);
// Set the base identity for the cipher/hash algorithms to an application specific value.
// This ensures that only files that were encrypted with this application can be decrypted.
// The identity of the used cipher/hash is stored in the file by EncodeFile().
// When decrypting with DecodeFile(), the identities will be read and the respective
// DECClasses will be loaded.
IdentityBase := $84485225;
// When using the identity concept, all used ciphers/hashes need to be registered.
RegisterDECClasses([TCipher_Rijndael, THash_SHA1]);
// The lines above should usually be executed during application startup.
EncodeFile(FileName, 'Password');
DecodeFile(FileName + '.enc', 'Password');
end;
begin
RandomSeed; // randomize DEC's own RNG
// Uncomment these two lines if the program output should be written to a text file instead of stdout
//AssignFile(Output, ChangeFileExt(ParamStr(0), '.txt'));
//Rewrite(Output);
try
RegisterClasses;
PrintRegisteredClasses;
with TTestRunner.Create(ExtractFilePath(ParamStr(0)) + 'DECTest.vec') do
begin
try
Run;
finally
Free;
end;
end;
{$IF DEFINED(MSWINDOWS) OR DEFINED(FPC)}
SpeedTestHashs;
{$IFEND}
TestCipher;
{$IF DEFINED(MSWINDOWS) OR DEFINED(FPC)}
SpeedTestCiphers;
{$IFEND}
DemoCipher(0);
DemoCipher(1);
DemoCipher(2);
DemoCipherFile;
except
on E: Exception do
WriteLn(E.Message);
end;
{$IFDEF MSWINDOWS}
Readln;
{$ENDIF}
end.