-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdcpcrypt2.pas
More file actions
800 lines (677 loc) · 24.7 KB
/
dcpcrypt2.pas
File metadata and controls
800 lines (677 loc) · 24.7 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
{===============================================================================
DCPcrypt v2.0.6 - Main component definitions
SPDX-License-Identifier: MIT
See LICENSE for full license text.
Copyright (c) 1999-2003 David Barton (crypto@cityinthesky.co.uk)
Copyright (c) 2006 Barko (Lazarus port)
Copyright (c) 2009-2010 Graeme Geldenhuys
Copyright (c) 2022 Werner Pamler
Copyright (c) 2026 Nicolas Deoux (NDXDev@gmail.com)
===============================================================================}
unit DCPcrypt2;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPbase64;
//{$DEFINE DCP1COMPAT} { DCPcrypt v1.31 compatibility mode - see documentation }
{ --- Predefined Types ------------------------------------------------------- }
type
{$IFNDEF FPC}
Pbyte= ^byte;
Pword= ^word;
Pdword= ^dword;
Pint64= ^int64;
dword= longword;
Pwordarray= ^Twordarray;
Twordarray= array[0..19383] of word;
{$ENDIF}
Pdwordarray= ^Tdwordarray;
Tdwordarray= array[0..8191] of dword;
{ Callback for progress reporting during stream encryption/decryption.
Progress is an integer percentage (0..100). }
TProgressEvent = procedure(Sender: TObject; Progress: integer) of object;
{ --- TDCP_hash - Base Hash Class -------------------------------------------- }
{ All hash algorithm implementations derive from this class.
Usage: call Init, then Update/UpdateStr/UpdateStream, then Final.
The Burn method clears internal state without producing a digest. }
type
EDCP_hash= class(Exception); { Exception class for hash errors }
TDCP_hash= class(TComponent)
protected
fInitialized: boolean; { Whether or not the algorithm has been initialized }
procedure DeadInt(Value: integer); { Knudge to display vars in the object inspector }
procedure DeadStr(Value: string); { Knudge to display vars in the object inspector }
private
function _GetId: integer;
function _GetAlgorithm: string;
function _GetHashSize: integer;
public
property Initialized: boolean
read fInitialized;
class function GetId: integer; virtual;
{ Get the algorithm id }
class function GetAlgorithm: string; virtual;
{ Get the algorithm name }
class function GetHashSize: integer; virtual;
{ Get the size of the digest produced - in bits }
class function SelfTest: boolean; virtual;
{ Tests the implementation with several test vectors }
procedure Init; virtual;
{ Initialize the hash algorithm }
procedure Final(var Digest); virtual;
{ Create the final digest and clear the stored information.
The size of the Digest var must be at least equal to the hash size }
procedure Burn; virtual;
{ Clear any stored information with out creating the final digest }
procedure Update(const Buffer; Size: longword); virtual;
{ Update the hash buffer with Size bytes of data from Buffer }
procedure UpdateStream(Stream: TStream; Size: QWord);
{ Update the hash buffer with Size bytes of data from the stream }
procedure UpdateStr(const Str: string);
{ Update the hash buffer with the string }
destructor Destroy; override;
published
property Id: integer
read _GetId write DeadInt;
property Algorithm: string
read _GetAlgorithm write DeadStr;
property HashSize: integer
read _GetHashSize write DeadInt;
end;
TDCP_hashclass= class of TDCP_hash;
{ --- TDCP_cipher - Base Cipher Class ---------------------------------------- }
{ Base class for all encryption components.
Stream ciphers derive directly from this class.
Block ciphers derive from TDCP_blockcipher (see below).
Supports key setup via raw bytes (Init) or passphrase (InitStr).
Provides stream and string encryption with progress reporting. }
type
EDCP_cipher= class(Exception); { Exception class for cipher errors }
TDCP_cipher= class(TComponent)
protected
fInitialized: boolean; { Whether or not the key setup has been done yet }
procedure DeadInt(Value: integer); { Knudge to display vars in the object inspector }
procedure DeadStr(Value: string); { Knudge to display vars in the object inspector }
private
FCancelByCallingThread: boolean;
FOnProgressEvent: TProgressEvent;
function _GetId: integer;
function _GetAlgorithm: string;
function _GetMaxKeySize: integer;
public
property Initialized: boolean
read fInitialized;
property OnProgressEvent: TProgressEvent read FOnProgressEvent write FOnProgressEvent;
property CancelByCallingThread: boolean read FCancelByCallingThread write FCancelByCallingThread;
class function GetId: integer; virtual;
{ Get the algorithm id }
class function GetAlgorithm: string; virtual;
{ Get the algorithm name }
class function GetMaxKeySize: integer; virtual;
{ Get the maximum key size (in bits) }
class function SelfTest: boolean; virtual;
{ Tests the implementation with several test vectors }
procedure Init(const Key; Size: longword; InitVector: pointer); virtual;
{ Do key setup based on the data in Key, size is in bits }
procedure InitStr(const Key: string; HashType: TDCP_hashclass);
{ Do key setup based on a hash of the key string }
procedure Burn; virtual;
{ Clear all stored key information }
procedure Reset; virtual;
{ Reset any stored chaining information }
procedure Encrypt(const Indata; var Outdata; Size: longword); virtual;
{ Encrypt size bytes of data and place in Outdata }
procedure Decrypt(const Indata; var Outdata; Size: longword); virtual;
{ Decrypt size bytes of data and place in Outdata }
function EncryptStream(InStream, OutStream: TStream; Size: Int64): longword;
{ Encrypt size bytes of data from InStream and place in OutStream }
function DecryptStream(InStream, OutStream: TStream; Size: Int64): longword;
{ Decrypt size bytes of data from InStream and place in OutStream }
function PartialEncryptStream(AStream: TMemoryStream; Size: longword): longword;
{ Encrypt up to EncryptLimit bytes in place on a TMemoryStream }
function PartialDecryptStream(AStream: TMemoryStream; Size: longword): longword;
{ Decrypt up to EncryptLimit bytes in place on a TMemoryStream }
function EncryptString(const Str: string): string; virtual;
{ Encrypt a string and return Base64 encoded }
function DecryptString(const Str: string): string; virtual;
{ Decrypt a Base64 encoded string }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Id: integer
read _GetId write DeadInt;
property Algorithm: string
read _GetAlgorithm write DeadStr;
property MaxKeySize: integer
read _GetMaxKeySize write DeadInt;
end;
TDCP_cipherclass= class of TDCP_cipher;
{ --- TDCP_blockcipher - Base Block Cipher Class ----------------------------- }
{ Extends TDCP_cipher with block cipher modes (ECB, CBC, CFB, OFB, CTR).
The Encrypt/Decrypt methods dispatch to the mode selected by CipherMode.
Direct mode-specific methods (EncryptCBC, EncryptCTR, etc.) are also available.
EncryptString/DecryptString always use CFB 8-bit mode for variable-length data. }
type
{ Block cipher chaining modes:
cmCBC - Cipher Block Chaining (default, requires padding to block size)
cmCFB8bit - Cipher Feedback 8-bit (byte-level, compatible with DCPcrypt v1.x)
cmCFBblock - Cipher Feedback block-level (faster than CFB 8-bit)
cmOFB - Output Feedback (turns block cipher into stream cipher)
cmCTR - Counter mode (parallelizable, turns block cipher into stream cipher) }
TDCP_ciphermode= (cmCBC, cmCFB8bit, cmCFBblock, cmOFB, cmCTR);
EDCP_blockcipher= class(EDCP_cipher); { Exception class for block cipher errors }
TDCP_blockcipher= class(TDCP_cipher)
protected
fCipherMode: TDCP_ciphermode; { The cipher mode the encrypt method uses }
procedure InitKey(const Key; Size: longword); virtual;
private
function _GetBlockSize: integer;
public
class function GetBlockSize: integer; virtual;
{ Get the block size of the cipher (in bits) }
procedure SetIV(const Value); virtual;
{ Sets the IV to Value and performs a reset }
procedure GetIV(var Value); virtual;
{ Returns the current chaining information, not the actual IV }
procedure Encrypt(const Indata; var Outdata; Size: longword); override;
{ Encrypt size bytes of data and place in Outdata using CipherMode }
procedure Decrypt(const Indata; var Outdata; Size: longword); override;
{ Decrypt size bytes of data and place in Outdata using CipherMode }
function EncryptString(const Str: string): string; override;
{ Encrypt a string and return Base64 encoded }
function DecryptString(const Str: string): string; override;
{ Decrypt a Base64 encoded string }
procedure EncryptECB(const Indata; var Outdata); virtual;
{ Encrypt a block of data using the ECB method of encryption }
procedure DecryptECB(const Indata; var Outdata); virtual;
{ Decrypt a block of data using the ECB method of decryption }
procedure EncryptCBC(const Indata; var Outdata; Size: longword); virtual;
{ Encrypt size bytes of data using the CBC method of encryption }
procedure DecryptCBC(const Indata; var Outdata; Size: longword); virtual;
{ Decrypt size bytes of data using the CBC method of decryption }
procedure EncryptCFB8bit(const Indata; var Outdata; Size: longword); virtual;
{ Encrypt size bytes of data using the CFB (8 bit) method of encryption }
procedure DecryptCFB8bit(const Indata; var Outdata; Size: longword); virtual;
{ Decrypt size bytes of data using the CFB (8 bit) method of decryption }
procedure EncryptCFBblock(const Indata; var Outdata; Size: longword); virtual;
{ Encrypt size bytes of data using the CFB (block) method of encryption }
procedure DecryptCFBblock(const Indata; var Outdata; Size: longword); virtual;
{ Decrypt size bytes of data using the CFB (block) method of decryption }
procedure EncryptOFB(const Indata; var Outdata; Size: longword); virtual;
{ Encrypt size bytes of data using the OFB method of encryption }
procedure DecryptOFB(const Indata; var Outdata; Size: longword); virtual;
{ Decrypt size bytes of data using the OFB method of decryption }
procedure EncryptCTR(const Indata; var Outdata; Size: longword); virtual;
{ Encrypt size bytes of data using the CTR method of encryption }
procedure DecryptCTR(const Indata; var Outdata; Size: longword); virtual;
{ Decrypt size bytes of data using the CTR method of decryption }
constructor Create(AOwner: TComponent); override;
published
property BlockSize: integer
read _GetBlockSize write DeadInt;
property CipherMode: TDCP_ciphermode
read fCipherMode write fCipherMode default cmCBC;
end;
TDCP_blockcipherclass= class of TDCP_blockcipher;
{ --- Helper Functions ------------------------------------------------------- }
{ XOR Size bytes of InData2 into InData1 (byte-by-byte). }
procedure XorBlock(var InData1, InData2; Size: longword);
{ XOR Size bytes of InData2 into InData1 (optimized: 32-bit words, then remaining bytes). }
procedure XorBlockEx(var InData1, InData2; Size: longword);
{ Wrapper around FillChar that suppresses compiler hints for var/out mismatch. }
procedure dcpFillChar(out x; count: SizeInt; Value: Byte); overload;
procedure dcpFillChar(out x; count: SizeInt; Value: Char); overload;
{ Fill a memory block with zeros. }
procedure ZeroMemory(Destination: Pointer; Length: PtrUInt);
implementation
{$Q-}{$R-}
const
EncryptBufSize = 1024 * 1024 * 8; { 8 MB - chunk size for stream encryption/decryption }
EncryptLimit = 16 * 1024; { 16 KB - max bytes for PartialEncrypt/DecryptStream }
{ --- TDCP_hash Implementation ----------------------------------------------- }
procedure TDCP_hash.DeadInt(Value: integer);
begin
end;
procedure TDCP_hash.DeadStr(Value: string);
begin
end;
function TDCP_hash._GetId: integer;
begin
Result:= GetId;
end;
function TDCP_hash._GetAlgorithm: string;
begin
Result:= GetAlgorithm;
end;
function TDCP_hash._GetHashSize: integer;
begin
Result:= GetHashSize;
end;
class function TDCP_hash.GetId: integer;
begin
Result:= -1;
end;
class function TDCP_hash.GetAlgorithm: string;
begin
Result:= '';
end;
class function TDCP_hash.GetHashSize: integer;
begin
Result:= -1;
end;
class function TDCP_hash.SelfTest: boolean;
begin
Result:= false;
end;
procedure TDCP_hash.Init;
begin
end;
procedure TDCP_hash.Final(var Digest);
begin
end;
procedure TDCP_hash.Burn;
begin
end;
procedure TDCP_hash.Update(const Buffer; Size: longword);
begin
end;
procedure TDCP_hash.UpdateStream(Stream: TStream; Size: QWord);
var
Buffer: array[0..8191] of byte;
i, read: integer;
begin
dcpFillChar(Buffer, SizeOf(Buffer), 0);
for i:= 1 to (Size div Sizeof(Buffer)) do
begin
read:= Stream.Read(Buffer,Sizeof(Buffer));
Update(Buffer,read);
end;
if (Size mod Sizeof(Buffer))<> 0 then
begin
read:= Stream.Read(Buffer,Size mod Sizeof(Buffer));
Update(Buffer,read);
end;
end;
procedure TDCP_hash.UpdateStr(const Str: string);
begin
Update(Str[1],Length(Str));
end;
destructor TDCP_hash.Destroy;
begin
if fInitialized then
Burn;
inherited Destroy;
end;
{ --- TDCP_cipher Implementation --------------------------------------------- }
procedure TDCP_cipher.DeadInt(Value: integer);
begin
end;
procedure TDCP_cipher.DeadStr(Value: string);
begin
end;
function TDCP_cipher._GetId: integer;
begin
Result:= GetId;
end;
function TDCP_cipher._GetAlgorithm: string;
begin
Result:= GetAlgorithm;
end;
function TDCP_cipher._GetMaxKeySize: integer;
begin
Result:= GetMaxKeySize;
end;
class function TDCP_cipher.GetId: integer;
begin
Result:= -1;
end;
class function TDCP_cipher.GetAlgorithm: string;
begin
Result:= '';
end;
class function TDCP_cipher.GetMaxKeySize: integer;
begin
Result:= -1;
end;
class function TDCP_cipher.SelfTest: boolean;
begin
Result:= false;
end;
procedure TDCP_cipher.Init(const Key; Size: longword; InitVector: pointer);
begin
if fInitialized then
Burn;
if (Size <= 0) or ((Size and 3)<> 0) or (Size> longword(GetMaxKeySize)) then
raise EDCP_cipher.Create('Invalid key size')
else
fInitialized:= true;
end;
{ Derives an encryption key from a passphrase by hashing it with HashType.
If the hash digest is larger than MaxKeySize, only MaxKeySize bits are used.
The digest buffer is overwritten before being freed (key material cleanup). }
procedure TDCP_cipher.InitStr(const Key: string; HashType: TDCP_hashclass);
var
Hash: TDCP_hash;
Digest: pointer;
begin
if fInitialized then
Burn;
try
GetMem(Digest,HashType.GetHashSize div 8);
Hash:= HashType.Create(Self);
Hash.Init;
Hash.UpdateStr(Key);
Hash.Final(Digest^);
Hash.Free;
if MaxKeySize< HashType.GetHashSize then
begin
Init(Digest^,MaxKeySize,nil);
end
else
begin
Init(Digest^,HashType.GetHashSize,nil);
end;
FillChar(Digest^,HashType.GetHashSize div 8,$FF);
FreeMem(Digest);
except
raise EDCP_cipher.Create('Unable to allocate sufficient memory for hash digest');
end;
end;
procedure TDCP_cipher.Burn;
begin
fInitialized:= false;
end;
procedure TDCP_cipher.Reset;
begin
end;
procedure TDCP_cipher.Encrypt(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_cipher.Decrypt(const Indata; var Outdata; Size: longword);
begin
end;
{ Encrypts Size bytes from InStream to OutStream in chunks of EncryptBufSize.
Returns the total number of bytes processed. Fires OnProgressEvent if assigned.
Set CancelByCallingThread to True from another thread to abort early. }
function TDCP_cipher.EncryptStream(InStream, OutStream: TStream; Size: Int64): longword;
var
Buffer: array of byte;
BufSize: longword;
Read: longword;
Remaining: Int64;
Progress: integer;
begin
if Size <= EncryptBufSize then
BufSize:= Size
else
BufSize:= EncryptBufSize;
SetLength(Buffer, BufSize);
dcpFillChar(Buffer[0], BufSize, 0);
Result:= 0;
FCancelByCallingThread:= false;
Remaining:= Size;
while (Remaining > 0) and (not FCancelByCallingThread) do
begin
if Remaining > BufSize then
Read:= InStream.Read(Buffer[0], BufSize)
else
Read:= InStream.Read(Buffer[0], Remaining);
if Read = 0 then
Break;
Inc(Result, Read);
Encrypt(Buffer[0], Buffer[0], Read);
OutStream.Write(Buffer[0], Read);
Dec(Remaining, Read);
if Assigned(FOnProgressEvent) and (Size > 0) then
begin
Progress:= Round((Size - Remaining) * 100.0 / Size);
FOnProgressEvent(Self, Progress);
end;
end;
SetLength(Buffer, 0);
end;
{ Decrypts Size bytes from InStream to OutStream in chunks of EncryptBufSize.
Returns the total number of bytes processed. Fires OnProgressEvent if assigned.
Set CancelByCallingThread to True from another thread to abort early. }
function TDCP_cipher.DecryptStream(InStream, OutStream: TStream; Size: Int64): longword;
var
Buffer: array of byte;
BufSize: longword;
Read: longword;
Remaining: Int64;
Progress: integer;
begin
if Size <= EncryptBufSize then
BufSize:= Size
else
BufSize:= EncryptBufSize;
SetLength(Buffer, BufSize);
dcpFillChar(Buffer[0], BufSize, 0);
Result:= 0;
FCancelByCallingThread:= false;
Remaining:= Size;
while (Remaining > 0) and (not FCancelByCallingThread) do
begin
if Remaining > BufSize then
Read:= InStream.Read(Buffer[0], BufSize)
else
Read:= InStream.Read(Buffer[0], Remaining);
if Read = 0 then
Break;
Inc(Result, Read);
Decrypt(Buffer[0], Buffer[0], Read);
OutStream.Write(Buffer[0], Read);
Dec(Remaining, Read);
if Assigned(FOnProgressEvent) and (Size > 0) then
begin
Progress:= Round((Size - Remaining) * 100.0 / Size);
FOnProgressEvent(Self, Progress);
end;
end;
SetLength(Buffer, 0);
end;
{ Encrypts data in-place on a TMemoryStream starting at the current position.
At most EncryptLimit (16 KB) bytes are processed regardless of Size.
Returns the actual number of bytes encrypted. }
function TDCP_cipher.PartialEncryptStream(AStream: TMemoryStream; Size: longword): longword;
var
p: PByte;
ActualSize: longword;
begin
if Size > EncryptLimit then
ActualSize:= EncryptLimit
else
ActualSize:= Size;
if ActualSize > longword(AStream.Size - AStream.Position) then
ActualSize:= longword(AStream.Size - AStream.Position);
p:= PByte(AStream.Memory) + AStream.Position;
Encrypt(p^, p^, ActualSize);
Result:= ActualSize;
end;
{ Decrypts data in-place on a TMemoryStream starting at the current position.
At most EncryptLimit (16 KB) bytes are processed regardless of Size.
Returns the actual number of bytes decrypted. }
function TDCP_cipher.PartialDecryptStream(AStream: TMemoryStream; Size: longword): longword;
var
p: PByte;
ActualSize: longword;
begin
if Size > EncryptLimit then
ActualSize:= EncryptLimit
else
ActualSize:= Size;
if ActualSize > longword(AStream.Size - AStream.Position) then
ActualSize:= longword(AStream.Size - AStream.Position);
p:= PByte(AStream.Memory) + AStream.Position;
Decrypt(p^, p^, ActualSize);
Result:= ActualSize;
end;
function TDCP_cipher.EncryptString(const Str: string): string;
begin
SetLength(Result,Length(Str));
Encrypt(Str[1],Result[1],Length(Str));
Result:= Base64EncodeStr(Result);
end;
function TDCP_cipher.DecryptString(const Str: string): string;
begin
Result:= Base64DecodeStr(Str);
Decrypt(Result[1],Result[1],Length(Result));
end;
constructor TDCP_cipher.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Burn;
end;
destructor TDCP_cipher.Destroy;
begin
if fInitialized then
Burn;
inherited Destroy;
end;
{ --- TDCP_blockcipher Implementation ---------------------------------------- }
procedure TDCP_blockcipher.InitKey(const Key; Size: longword);
begin
end;
function TDCP_blockcipher._GetBlockSize: integer;
begin
Result:= GetBlockSize;
end;
class function TDCP_blockcipher.GetBlockSize: integer;
begin
Result:= -1;
end;
procedure TDCP_blockcipher.SetIV(const Value);
begin
end;
procedure TDCP_blockcipher.GetIV(var Value);
begin
end;
{ Dispatches to the mode-specific encryption method based on CipherMode. }
procedure TDCP_blockcipher.Encrypt(const Indata; var Outdata; Size: longword);
begin
case fCipherMode of
cmCBC: EncryptCBC(Indata,Outdata,Size);
cmCFB8bit: EncryptCFB8bit(Indata,Outdata,Size);
cmCFBblock: EncryptCFBblock(Indata,Outdata,Size);
cmOFB: EncryptOFB(Indata,Outdata,Size);
cmCTR: EncryptCTR(Indata,Outdata,Size);
end;
end;
{ Block cipher string encryption always uses CFB 8-bit mode (not CipherMode)
to handle arbitrary-length strings without padding. Result is Base64 encoded. }
function TDCP_blockcipher.EncryptString(const Str: string): string;
begin
SetLength(Result,Length(Str));
EncryptCFB8bit(Str[1],Result[1],Length(Str));
Result:= Base64EncodeStr(Result);
end;
{ Decodes the Base64 string, then decrypts using CFB 8-bit mode. }
function TDCP_blockcipher.DecryptString(const Str: string): string;
begin
Result:= Base64DecodeStr(Str);
DecryptCFB8bit(Result[1],Result[1],Length(Result));
end;
{ Dispatches to the mode-specific decryption method based on CipherMode. }
procedure TDCP_blockcipher.Decrypt(const Indata; var Outdata; Size: longword);
begin
case fCipherMode of
cmCBC: DecryptCBC(Indata,Outdata,Size);
cmCFB8bit: DecryptCFB8bit(Indata,Outdata,Size);
cmCFBblock: DecryptCFBblock(Indata,Outdata,Size);
cmOFB: DecryptOFB(Indata,Outdata,Size);
cmCTR: DecryptCTR(Indata,Outdata,Size);
end;
end;
procedure TDCP_blockcipher.EncryptECB(const Indata; var Outdata);
begin
end;
procedure TDCP_blockcipher.DecryptECB(const Indata; var Outdata);
begin
end;
procedure TDCP_blockcipher.EncryptCBC(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_blockcipher.DecryptCBC(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_blockcipher.EncryptCFB8bit(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_blockcipher.DecryptCFB8bit(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_blockcipher.EncryptCFBblock(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_blockcipher.DecryptCFBblock(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_blockcipher.EncryptOFB(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_blockcipher.DecryptOFB(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_blockcipher.EncryptCTR(const Indata; var Outdata; Size: longword);
begin
end;
procedure TDCP_blockcipher.DecryptCTR(const Indata; var Outdata; Size: longword);
begin
end;
constructor TDCP_blockcipher.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
fCipherMode:= cmCBC;
end;
{ --- Helper Functions ------------------------------------------------------- }
procedure XorBlock(var InData1, InData2; Size: longword);
var
b1: PByteArray;
b2: PByteArray;
i: longword;
begin
b1 := @InData1;
b2 := @InData2;
for i := 0 to size-1 do
b1[i] := b1[i] xor b2[i];
end;
procedure dcpFillChar(out x; count: SizeInt; Value: Byte);
begin
{$HINTS OFF}
FillChar(x, count, value);
{$HINTS ON}
end;
procedure ZeroMemory(Destination: Pointer; Length: PtrUInt);
begin
FillChar(Destination^, Length, 0);
end;
procedure dcpFillChar(out x; count: SizeInt; Value: Char);
begin
{$HINTS OFF}
FillChar(x, count, Value);
{$HINTS ON}
end;
{ XOR using 32-bit words for bulk, then byte-by-byte for remainder. }
procedure XorBlockEx(var InData1, InData2; Size: longword);
var
l1: PIntegerArray;
l2: PIntegerArray;
b1: PByteArray;
b2: PByteArray;
i: integer;
c: integer;
begin
l1 := @inData1;
l2 := @inData2;
for i := 0 to size div sizeof(LongWord)-1 do
l1[i] := l1[i] xor l2[i];
// the rest of the buffer (3 bytes)
c := size mod sizeof(longWord);
if c > 0 then begin
b1 := @InData1;
b2 := @InData2;
for i := (size-c) to size-1 do
b1[i] := b1[i] xor b2[i];
end;
end;
end.