-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcrypto.pas
532 lines (484 loc) · 13.8 KB
/
crypto.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
unit Crypto;
{$MODE Delphi}
interface
uses
Classes, Sysutils, DCPcrypt2, DCPBase64;
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;
TBlock64 = array[0..1] of DWORD;
TBlock128 = array[0..3] of DWORD;
TBlock256 = array[0..7] of DWORD;
PBlock64 = ^TBlock64;
PBlock128 = ^TBlock128;
PBlock256 = ^TBlock256;
TKey128 = TBlock128;
TKey256 = TBlock256;
PKey128 = ^TKey128;
PKey256 = ^TKey256;
TCipherAlgorithm = (caNone, caBlowfish, caTwofish, caRC2, {caRC4, }caRC5, caRC6, caDES, ca3DES, caCast128, caCast256, caGOST, caMars, caRijndael, caIDEA, caICE, caThinICE, caICE2, caSerpent, caTEA, caMisty1);
TCipherMode = (cmCBC, cmCFB8Bit, CFBBlock, cmOFB, cmCTR);
THashAlgorithm = (haNone, haMD4, haMD5, haSHA1, haSHA256, haSHA384, haSHA512, haRipeMD128, haRipeMD160, haHaval256, haTiger);
const
CIPHER_ALGORITHM_STR: array[TCipherAlgorithm] of String = ('None','Blowfish','Twofish','RC2',{'RC4',}'RC5','RC6','DES','3DES','CAST-128','CAST-256','GOST','Mars','Rijndael','IDEA','ICE','ThinIce','ICE2','Serpent','TEA','Misty1');
CIPHER_MODE_STR: array[TCipherMode] of String = ('CBC','CFB-8Bit','CFB-Block','OFB','CTR');
HASH_ALGORITHM_STR: array[THashAlgorithm] of String = ('None','MD4','MD5','SHA1','SHA-256','SHA-384','SHA-512','RipeMD-128','RipeMD-160','Haval-256','Tiger');
type
TMAC128 = TBlock128;
TMAC64 = TBlock64;
function GetCipherBlockSize(Kind: TCipherAlgorithm): Integer;
function GetCipherClass(Kind: TCipherAlgorithm): TDCP_BlockCipherClass;
function CreateCipher(Kind: TCipherAlgorithm; AOwner: TComponent = nil): TDCP_BlockCipher;
function CreateHash(Kind: THashAlgorithm; AOwner: TComponent): TDCP_Hash;
procedure EncryptStream(InStream, OutStream: TStream; Password: String); overload;
procedure EncryptStream(InStream, OutStream: TStream; Key: TBlock128); overload;
procedure DecryptStream(InStream, OutStream: TStream; Password: String); overload;
procedure DecryptStream(InStream, OutStream: TStream; Key: TBlock128); overload;
function EncryptString(const InString: String; Password: String): String;
function DecryptString(const InString: String; Password: String): String;
function MD5String(Str: String): TBlock128;
function SHA256String(Str: String): TBlock256;
function Block256ToString(Digest: TBlock256): String;
function Block128ToString(Digest: TBlock128): String;
function Block256ToBase64(Digest: TBlock256): String;
function Block128ToBase64(Digest: TBlock128): String;
function HMAC128(Algorithm: THashAlgorithm; Data: Pointer; DataSize: Integer; Key: Pointer; KeySize: Integer): TMAC128;
function HMAC64(Algorithm: THashAlgorithm; Data: Pointer; DataSize: Integer; Key: Pointer; KeySize: Integer): TMAC64;
procedure XORMem(A,B,R: Pointer; Size: Integer);
procedure XOR64(A,B,R: Pointer);
procedure XOR128(A,B,R: Pointer);
procedure Inc64(V: Pointer);
procedure Inc128(V: Pointer);
function CompareMem(A,B: Pointer; Size: Integer): Integer;
function BufferToHex(Buffer: Pointer; Size: Integer): String;
function LFSR(var N: Cardinal): Cardinal;
function ROTL32(var X: Cardinal; Y: Byte = 1): Cardinal;
function ROTR32(var X: Cardinal; Y: Byte = 1): Cardinal;
function XpowYmodN(X, Y, N: Cardinal): Cardinal;
function GeneratePrime: Cardinal;
function MillerRabin(N: Cardinal; Trials: Byte): Boolean;
implementation
uses
Base32, Base64, Rand,
DCPBlowfish, DCPTwoFish, DCPCast128, DCPCast256, DCPDES, DCPICE, DCPIdea, DCPRC2, DCPRC5, DCPRC6,
DCPRijndael, DCPSerpent, DCPTEA, DCPGOST, DCPMARS, DCPMisty1, DCPRC4,
DCPHaval, DCPMD4, DCPMD5, DCPRipeMD128, DCPRipeMD160,
DCPSHA1, DCPSHA256, DCPSHA512, DCPTiger;
function GetCipherBlockSize(Kind: TCipherAlgorithm): Integer;
begin
Result := GetCipherClass(Kind).GetBlockSize;
end;
function GetCipherClass(Kind: TCipherAlgorithm): TDCP_BlockCipherClass;
begin
case Kind of
caBlowfish : Result := TDCP_Blowfish;
caTwofish : Result := TDCP_Twofish;
caRC2 : Result := TDCP_RC2;
caRC5 : Result := TDCP_RC5;
//caRC4 : Result := TDCP_RC4;
caRC6 : Result := TDCP_RC6;
caDES : Result := TDCP_DES;
ca3DES : Result := TDCP_3DES;
caCast128 : Result := TDCP_Cast128;
caCast256 : Result := TDCP_Cast256;
caRijndael : Result := TDCP_Rijndael;
caIDEA : Result := TDCP_IDEA;
caICE : Result := TDCP_ICE;
caThinICE : Result := TDCP_ThinICE;
caICE2 : Result := TDCP_ICE2;
caSerpent : Result := TDCP_Serpent;
caTEA : Result := TDCP_TEA;
caMisty1 : Result := TDCP_Misty1;
caMars : Result := TDCP_Mars;
caGOST : Result := TDCP_GOST;
else
raise EDCP_BlockCipher.Create('Cipher not found');
end;
end;
function CreateCipher(Kind: TCipherAlgorithm; AOwner: TComponent): TDCP_BlockCipher;
begin
GetCipherClass(Kind).Create(AOwner);
end;
function CreateHash(Kind: THashAlgorithm; AOwner: TComponent): TDCP_Hash;
begin
case Kind of
haNone : Result := nil;
haMD4 : Result := TDCP_MD4.Create(AOwner);
haMD5 : Result := TDCP_MD5.Create(AOwner);
haSHA1 : Result := TDCP_SHA1.Create(AOwner);
haSHA256 : Result := TDCP_SHA256.Create(AOwner);
haSHA384 : Result := TDCP_SHA384.Create(AOwner);
haSHA512 : Result := TDCP_SHA512.Create(AOwner);
haRipeMD128 : Result := TDCP_RipeMD128.Create(AOwner);
haRipeMD160 : Result := TDCP_RipeMD160.Create(AOwner);
haHaval256 : Result := TDCP_Haval.Create(AOwner);
haTiger : Result := TDCP_Tiger.Create(AOwner);
else
raise EDCP_Hash.Create('Hash algorithm not found');
end;
end;
procedure EncryptStream(InStream, OutStream: TStream; Password: String);
var
Cipher: TDCP_RC4;
begin
InStream.Position := 0;
OutStream.Size := 0;
Cipher := TDCP_RC4.Create(nil);
try
Cipher.InitStr(Password,TDCP_SHA512);
Cipher.EncryptStream(InStream,OutStream,InStream.Size);
finally
Cipher.Free;
end;
end;
procedure EncryptStream(InStream, OutStream: TStream; Key: TBlock128);
var
Cipher: TDCP_RC4;
begin
InStream.Position := 0;
OutStream.Size := 0;
Cipher := TDCP_RC4.Create(nil);
try
Cipher.Init(Key,SizeOf(Key),nil);
Cipher.EncryptStream(InStream,OutStream,InStream.Size);
finally
Cipher.Free;
end;
end;
procedure DecryptStream(InStream, OutStream: TStream; Password: String);
var
Cipher: TDCP_RC4;
begin
InStream.Position := 0;
OutStream.Size := 0;
Cipher := TDCP_RC4.Create(nil);
try
Cipher.InitStr(Password,TDCP_SHA512);
Cipher.DecryptStream(InStream,OutStream,InStream.Size);
finally
Cipher.Free;
end;
end;
procedure DecryptStream(InStream, OutStream: TStream; Key: TBlock128);
var
Cipher: TDCP_RC4;
begin
InStream.Position := 0;
OutStream.Size := 0;
Cipher := TDCP_RC4.Create(nil);
try
Cipher.Init(Key,SizeOf(Key),nil);
Cipher.DecryptStream(InStream,OutStream,InStream.Size);
finally
Cipher.Free;
end;
end;
function EncryptString(const InString: String; Password: String): String;
var
Cipher: TDCP_RC4;
begin
Cipher := TDCP_RC4.Create(nil);
try
Cipher.InitStr(Password,TDCP_SHA512);
Result := Cipher.EncryptString(InString);
finally
Cipher.Free;
end;
end;
function DecryptString(const InString: String; Password: String): String;
var
Cipher: TDCP_RC4;
begin
Cipher := TDCP_RC4.Create(nil);
try
Cipher.InitStr(Password,TDCP_SHA512);
Result := Cipher.DecryptString(InString);
finally
Cipher.Free;
end;
end;
procedure Hash(Algorithm: THashAlgorithm; Data: Pointer; DataSize: Integer; out Hash: Pointer; out HashSize: Integer);
var
H: TDCP_Hash;
begin
H := CreateHash(Algorithm,nil);
try
HashSize := H.HashSize div 8;
Hash := GetMem(HashSize);
H.Init;
H.Update(Data^,DataSize);
H.Final(Hash^);
H.Burn;
finally
H.Free;
end;
end;
function MD5String(Str: String): TBlock128;
var
Hash: TDCP_Hash;
Size: Integer;
begin
Hash := CreateHash(haMD5,nil);
try
Size := Hash.HashSize div 8;
Assert(Size = SizeOf(Result));
FillChar(Result,SizeOf(Result),0);
Hash.Init;
Hash.UpdateStr(Str);
Hash.Final(Result);
Hash.Burn;
finally
Hash.Free;
end;
end;
function SHA256String(Str: String): TBlock256;
var
Hash: TDCP_Hash;
Size: Integer;
begin
Hash := CreateHash(haSHA256,nil);
try
Size := Hash.HashSize div 8;
Assert(Size = SizeOf(Result));
FillChar(Result,SizeOf(Result),0);
Hash.Init;
Hash.UpdateStr(Str);
Hash.Final(Result);
Hash.Burn;
finally
Hash.Free;
end;
end;
function Block128ToString(Digest: TBlock128): String;
begin
SetLength(Result,SizeOf(Digest));
Move(Digest,Result,SizeOf(Digest));
end;
function Block256ToString(Digest: TBlock256): String;
begin
SetLength(Result,SizeOf(Digest));
Move(Digest,Result,SizeOf(Digest));
end;
function Block256ToBase64(Digest: TBlock256): String;
begin
SetLength(Result,((SizeOf(Digest)+2) div 3) * 4);
Base64Encode(@Digest[0],@Result[1],SizeOf(Digest));
end;
function Block128ToBase64(Digest: TBlock128): String;
begin
SetLength(Result,((SizeOf(Digest)+2) div 3) * 4);
Base64Encode(@Digest[0],@Result[1],SizeOf(Digest));
end;
function HMAC128(Algorithm: THashAlgorithm; Data: Pointer; DataSize: Integer; Key: Pointer; KeySize: Integer): TMAC128;
var
H: TDCP_Hash;
IKey: Pointer;
IKeySize: Integer;
FreeIKey: Boolean;
IPad: Pointer;
OPad: Pointer;
Digest: Pointer;
begin
// If the key size is over 64 bytes long then use a hash of it instead
if KeySize > 64 then
begin
Hash(Algorithm,Key,KeySize,IKey,IKeySize);
FreeIKey := True;
end
else
begin
IKey := Key;
IKeySize := KeySize;
FreeIKey := False;
end;
try
IPad := GetMem(64);
OPad := GetMem(64);
try
Assert(KeySize <= 64);
FillChar(IPad^,64,$36);
XORMem(IPad,Key,IPad,KeySize);
FillChar(OPad^,64,$5C);
XORMem(OPad,Key,OPad,KeySize);
H := CreateHash(Algorithm,nil);
try
Assert(H.HashSize mod 8 = 0);
Digest := GetMem(H.HashSize div 8);
try
H.Init;
H.Update(IPad^,64);
H.Update(Data^,DataSize);
H.Final(Digest^);
H.Burn;
H.Init;
H.Update(OPad^,64);
H.Update(Digest^,H.HashSize div 8);
H.Final(Digest^);
Move(Digest^,Result,SizeOf(Result));
H.Burn;
finally
FreeMem(Digest);
end;
finally
FreeAndNil(H);
end;
finally
FreeMem(IPad);
FreeMem(OPad);
end;
finally
if FreeIKey then
FreeMem(IKey);
end;
end;
function HMAC64(Algorithm: THashAlgorithm; Data: Pointer; DataSize: Integer; Key: Pointer; KeySize: Integer): TMAC64;
var
MAC128: TMAC128;
begin
MAC128 := HMAC128(Algorithm,Data,DataSize,Key,KeySize);
Result[0] := MAC128[0];
Result[1] := MAC128[1];
end;
procedure XORMem(A,B,R: Pointer; Size: Integer);
var
PA,PB,PR: PByte;
X: Word;
begin
for X := 0 to Size - 1 do
begin
PA := PByte(PtrInt(A)+X);
PB := PByte(PtrInt(B)+X);
PR := PByte(PtrInt(R)+X);
PR^ := PA^ xor PB^;
end;
end;
procedure XOR64(A,B,R: Pointer);
begin
PBlock64(R)[0] := PBlock64(A)[0] xor PBlock64(B)[0];
PBlock64(R)[1] := PBlock64(A)[1] xor PBlock64(B)[1];
end;
procedure XOR128(A,B,R: Pointer);
begin
PBlock128(R)[0] := PBlock128(A)[0] xor PBlock128(B)[0];
PBlock128(R)[1] := PBlock128(A)[1] xor PBlock128(B)[1];
PBlock128(R)[2] := PBlock128(A)[2] xor PBlock128(B)[2];
PBlock128(R)[3] := PBlock128(A)[3] xor PBlock128(B)[3];
end;
procedure Inc64(V: Pointer);
begin
inc(PBlock64(V)[1]);
if PBlock64(V)[1] = 0 then
inc(PBlock64(V)[0]);
end;
procedure Inc128(V: Pointer);
var
i: integer;
begin
Inc(PBlock128(V)[3]);
i:= 3;
while (i> 0) and (PBlock128(V)[i] = 0) do
begin
Inc(PBlock128(V)[i-1]);
Dec(i);
end;
end;
function CompareMem(A, B: Pointer; Size: Integer): Integer;
var
X: Integer;
PA, PB: PByte;
begin
Result := 0;
for X := 0 to Size - 1 do
begin
PA := PByte(PtrUInt(A) + X);
PB := PByte(PtrUInt(B) + X);
if PA^ > PB^ then
begin
Result := 1;
Exit;
end
else
if PB^ > PA^ then
begin
Result := -1;
Exit;
end;
end;
end;
function BufferToHex(Buffer: Pointer; Size: Integer): String;
var { Fails on large strings }
X: Integer;
B: Byte;
S: String;
begin
Result := '';
for X := 0 to Size - 1 do
begin
B := PByte(PtrUInt(Buffer) + X)^;
S := IntToHex(B,2)+' ';
Result := Result + S;
end;
end;
// Linear Feedback Shift Register
function LFSR(var N: Cardinal): Cardinal;
begin
if (N and 1) > 0 then
N := ((N xor $80000055) shr 1) or $80000000
else
N := N shr 1;
Result := N;
end;
function ROTL32(var X: Cardinal; Y: Byte = 1): Cardinal;
begin
X := (X shl Y) or (X shr (32-Y));
end;
function ROTR32(var X: Cardinal; Y: Byte = 1): Cardinal;
begin
X := (X shr Y) or (x shl (32 -Y));
end;
// Raises X to the power Y in modulus N
function XpowYmodN(X, Y, N: Cardinal): Cardinal;
var
A,B: QWORD;
begin
A := 1; B := X;
while Y > 0 do
begin
if Y mod 2 = 1 then
A := (A * B) mod N;
B := (B * B) mod N;
Y := Y div 2;
end;
Result := A mod N;
end;
// Performs the Miller-Rabin primality test on N
function MillerRabin(N: Cardinal; Trials: Byte): Boolean;
var
I: Byte;
A: Cardinal;
begin
Result := False;
for I := 1 to Trials do
begin
A := (RNG.Generate mod (N-3)) + 2; // Get random value between 2 and N-1
if XpowYmodN(A,N-1,N) <> 1 then Exit; // Is it prime?
end;
Result := True;
end;
// Generate a random 32 bit prime number
function GeneratePrime: Cardinal;
begin
Result := RNG.Generate;
if (Result and 1) = 0 then inc(Result);
while not MillerRabin(Result,5) do inc(Result,2);
end;
end.