forked from PascalCoinDev/PascalCoin
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathUPCCryptoLib4Pascal.pas
More file actions
571 lines (500 loc) · 19.6 KB
/
Copy pathUPCCryptoLib4Pascal.pas
File metadata and controls
571 lines (500 loc) · 19.6 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
unit UPCCryptoLib4Pascal;
{ Copyright (c) 2019 by Albert Molina
Distributed under the MIT software license, see the accompanying file LICENSE
or visit http://www.opensource.org/licenses/mit-license.php.
This unit is a part of the PascalCoin Project, an infinitely scalable
cryptocurrency. Find us here:
Web: https://www.pascalcoin.org
Source: https://github.com/PascalCoin/PascalCoin
If you like it, consider a donation using Bitcoin:
16K3HCZRhFUtM8GdWRcfKeaa6KsuyxZaYk
This unit contains code made by Ugochukwu Mmaduekwe (aka Xor-el at GitHub)
https://github.com/PascalCoin/PascalCoinTools/tree/master/Tools/PascalCoinKeyTool
Available under MIT License
THIS LICENSE HEADER MUST NOT BE REMOVED.
}
{
This unit is a bridge between PascalCoin and CryptoLib4Pascal in order to
do not use OpenSSL library to do some cryptographic functions.
Specially will define:
- Use of BigIntegers
- ECDSA keys generation
- ECDSA Sign
- ECDSA Verify
- PascalCoin ECDSA encryption
- PascalCoin AES encryption
}
interface
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
Uses SysUtils, UBaseTypes, UPCDataTypes,
ClpBigInteger,
ClpSecureRandom,
ClpISecureRandom,
ClpIECDomainParameters,
ClpECDomainParameters,
ClpIX9ECParameters,
ClpIIESEngine,
ClpIBaseKdfBytesGenerator,
ClpIIESParameterSpec,
ClpIPascalCoinECIESKdfBytesGenerator,
ClpIPascalCoinIESEngine
;
Type
TPCCryptoLib4Pascal = Class
strict private
const
PKCS5_SALT_LEN = Int32(8);
SALT_MAGIC_LEN = Int32(8);
SALT_SIZE = Int32(8);
SALT_MAGIC: string = 'Salted__';
strict private
class var FRandom: ISecureRandom;
class var FDomain_SECP256K1 : IECDomainParameters;
class var FDomain_SECP384R1 : IECDomainParameters;
class var FDomain_SECT283K1 : IECDomainParameters;
class var FDomain_SECP521R1 : IECDomainParameters;
class var FCurve_SECP256K1 : IX9ECParameters;
class var FCurve_SECP384R1 : IX9ECParameters;
class var FCurve_SECT283K1 : IX9ECParameters;
class var FCurve_SECP521R1 : IX9ECParameters;
class var FPascalCoinIESEngine : IPascalCoinIESEngine;
class var FIESParameterSpec : IIESParameterSpec;
class constructor TPCCryptoLib4Pascal();
class function GetCurveAndDomainParameters(const AEC_OpenSSL_NID : Word; var OCurve : IX9ECParameters; var ODomain : IECDomainParameters; ARaiseIfNotForPascal : Boolean = True ) : Boolean;
class function GetDomainParameters(const AEC_OpenSSL_NID : Word) : IECDomainParameters;
class function EVP_GetSalt(): TBytes; static; inline;
class function EVP_GetKeyIV(const APasswordBytes, ASaltBytes: TBytes; out AKeyBytes, AIVBytes: TBytes): boolean; static;
protected
public
class function DoECDSASign(const AEC_OpenSSL_NID : Word; const APrivateKey, AMessage: TBytes; var ASignature : TECDSA_SIG) : Boolean;
class function DoECDSAVerify(const APublicKey : TECDSA_Public; const AMessage: TBytes; const ASignature : TECDSA_SIG) : Boolean;
class function DoPascalCoinECIESEncrypt(const APublicKey : TECDSA_Public; const AMessage : TBytes; var AEncryptedMessage : TBytes) : Boolean;
class function DoPascalCoinECIESDecrypt(const AEC_OpenSSL_NID : Word; const APrivateKey, AEncryptedMessage : TBytes; var ADecryptedMessage : TBytes) : Boolean;
class function DoPascalCoinAESEncrypt(const AMessage, APassword: TBytes): TBytes; static;
class function DoPascalCoinAESDecrypt(const AEncryptedMessage, APassword: TBytes; out ADecryptedMessage: TBytes): boolean; static;
class function DoGetRandomPrivateKey(const AEC_OpenSSL_NID : Word) : TBytes;
class function DoGetPublicKey(const AEC_OpenSSL_NID : Word; const APrivateKey: TBytes) : TECDSA_Public;
class procedure DoSHA256(const AInput : TBytes; var AOutput : TBytes);
class procedure DoRIPEMD160(const AInput : TBytes; var AOutput : TBytes);
End;
implementation
Uses
ClpCustomNamedCurves,
ClpIECPrivateKeyParameters,
ClpECPrivateKeyParameters,
ClpIParametersWithRandom,
ClpParametersWithRandom,
ClpIECDsaSigner,
ClpECDsaSigner,
ClpCryptoLibTypes,
//
ClpIECPublicKeyParameters,
ClpECPublicKeyParameters,
ClpIECC,
//
ClpIIESCipher,
ClpIESCipher,
ClpIBufferedBlockCipher,
ClpIAesEngine,
ClpIBlockCipherModes,
ClpIBasicAgreement,
ClpIESParameterSpec,
ClpIECDHBasicAgreement,
ClpIMac,
ClpECDHBasicAgreement,
ClpPascalCoinECIESKdfBytesGenerator,
ClpDigestUtilities,
ClpMacUtilities,
ClpAesEngine,
ClpBlockCipherModes,
ClpPaddedBufferedBlockCipher,
ClpPaddingModes,
ClpIPaddingModes,
ClpPascalCoinIESEngine,
//
ClpIParametersWithIV,
ClpIBufferedCipher,
ClpArrayUtils,
ClpCipherUtilities,
ClpParametersWithIV,
ClpParameterUtilities,
ClpIDigest,
//
ClpIAsymmetricCipherKeyPairGenerator,
ClpGeneratorUtilities,
ClpECKeyGenerationParameters,
ClpIECKeyGenerationParameters,
ClpIAsymmetricCipherKeyPair,
ClpECKeyPairGenerator,
//
HlpSHA2_256,
HlpRIPEMD160,
//
UAccounts,
UConst,
ULog;
{ TPCCryptoLib4Pascal }
class function TPCCryptoLib4Pascal.DoECDSASign(const AEC_OpenSSL_NID : Word;
Const APrivateKey, AMessage: TBytes; var ASignature: TECDSA_SIG): Boolean;
var
LDomain: IECDomainParameters;
LPrivD: TBigInteger;
LPrivKeyParams : IECPrivateKeyParameters;
LParam: IParametersWithRandom;
LSigner : IECDsaSigner;
LSignerResult : TCryptoLibGenericArray<TBigInteger>;
begin
LDomain := GetDomainParameters(AEC_OpenSSL_NID);
LPrivD := TBigInteger.Create(1, APrivateKey);
LPrivKeyParams := TECPrivateKeyParameters.Create('ECDSA',LPrivD,LDomain);
//
LParam := TParametersWithRandom.Create(LPrivKeyParams, FRandom);
LSigner := TECDsaSigner.Create();
LSigner.Init(True, LParam);
LSignerResult := LSigner.GenerateSignature(AMessage);
ASignature.r := LSignerResult[0].ToByteArray();
ASignature.s := LSignerResult[1].ToByteArray();
Result := True;
end;
class function TPCCryptoLib4Pascal.DoECDSAVerify(const APublicKey: TECDSA_Public; const AMessage: TBytes; const ASignature: TECDSA_SIG): Boolean;
var
LDomain: IECDomainParameters;
LCurve: IX9ECParameters;
LSigner: IECDsaSigner;
LPubKeyParams : IECPublicKeyParameters;
LPoint: IECPoint;
LBigXCoord, LBigYCoord,
LSigR, LSigS: TBigInteger;
begin
GetCurveAndDomainParameters(APublicKey.EC_OpenSSL_NID,LCurve,LDomain);
LBigXCoord := TBigInteger.Create(1, APublicKey.x);
LBigYCoord := TBigInteger.Create(1, APublicKey.y);
LPoint := LCurve.Curve.CreatePoint(LBigXCoord, LBigYCoord);
LPubKeyParams := TECPublicKeyParameters.Create('ECDSA', LPoint, LDomain);
LSigR := TBigInteger.Create(1, ASignature.r);
LSigS := TBigInteger.Create(1, ASignature.s);
LSigner := TECDsaSigner.Create();
LSigner.Init(False, LPubKeyParams);
Result := LSigner.VerifySignature(AMessage, LSigR, LSigS);
end;
class function TPCCryptoLib4Pascal.DoGetPublicKey(const AEC_OpenSSL_NID: Word; const APrivateKey: TBytes): TECDSA_Public;
var
LDomain: IECDomainParameters;
LPrivD : TBigInteger;
LECPrivateKeyParameters : IECPrivateKeyParameters;
LPublicKey: IECPublicKeyParameters;
begin
LDomain := GetDomainParameters(AEC_OpenSSL_NID);
LPrivD := TBigInteger.Create(1,APrivateKey); // Obtain a big num based on private key
LECPrivateKeyParameters := TECPrivateKeyParameters.Create('ECDSA',LPrivD,LDomain);
LPublicKey := TECKeyPairGenerator.GetCorrespondingPublicKey(LECPrivateKeyParameters);
Result.EC_OpenSSL_NID := AEC_OpenSSL_NID;
Result.X := LPublicKey.Q.AffineXCoord.ToBigInteger().ToByteArrayUnsigned();
Result.Y := LPublicKey.Q.AffineYCoord.ToBigInteger().ToByteArrayUnsigned();
end;
class function TPCCryptoLib4Pascal.DoGetRandomPrivateKey(const AEC_OpenSSL_NID: Word): TBytes;
var
LDomain: IECDomainParameters;
LCurve: IX9ECParameters;
KeyPairGeneratorInstance: IAsymmetricCipherKeyPairGenerator;
LKeyPair: IAsymmetricCipherKeyPair;
LPrivateKey: IECPrivateKeyParameters;
begin
GetCurveAndDomainParameters(AEC_OpenSSL_NID,LCurve,LDomain);
KeyPairGeneratorInstance := TGeneratorUtilities.GetKeyPairGenerator('ECDSA');
KeyPairGeneratorInstance.Init(TECKeyGenerationParameters.Create(LDomain, FRandom) as IECKeyGenerationParameters);
LKeyPair := KeyPairGeneratorInstance.GenerateKeyPair();
LPrivateKey := LKeyPair.&Private as IECPrivateKeyParameters;
Result := LPrivateKey.D.ToByteArray();
end;
class function TPCCryptoLib4Pascal.DoPascalCoinAESDecrypt(
const AEncryptedMessage, APassword: TBytes; out ADecryptedMessage: TBytes): boolean;
var
SaltBytes, KeyBytes, IVBytes, Buf, Chopped: TRawBytes;
KeyParametersWithIV: IParametersWithIV;
cipher: IBufferedCipher;
LBufStart, LSrcStart, LCount: Int32;
begin
try
System.SetLength(SaltBytes, SALT_SIZE);
// First read the magic text and the salt - if any
Chopped := System.Copy(AEncryptedMessage, 0, SALT_MAGIC_LEN);
if (System.Length(AEncryptedMessage) >= SALT_MAGIC_LEN) and
(Chopped.ToString = SALT_MAGIC) then
begin
System.Move(AEncryptedMessage[SALT_MAGIC_LEN], SaltBytes[0], SALT_SIZE);
if not EVP_GetKeyIV(APassword, SaltBytes, KeyBytes, IVBytes) then
begin
Result := False;
Exit;
end;
LSrcStart := SALT_MAGIC_LEN + SALT_SIZE;
end
else
begin
if not EVP_GetKeyIV(APassword, nil, KeyBytes, IVBytes) then
begin
Result := False;
Exit;
end;
LSrcStart := 0;
end;
cipher := TCipherUtilities.GetCipher('AES/CBC/PKCS7PADDING');
KeyParametersWithIV := TParametersWithIV.Create(TParameterUtilities.CreateKeyParameter('AES', KeyBytes), IVBytes);
cipher.Init(False, KeyParametersWithIV); // init decryption cipher
System.SetLength(Buf, System.Length(AEncryptedMessage));
LBufStart := 0;
LCount := cipher.ProcessBytes(AEncryptedMessage, LSrcStart, System.Length(AEncryptedMessage) - LSrcStart, Buf, LBufStart);
System.Inc(LBufStart, LCount);
LCount := cipher.DoFinal(Buf, LBufStart);
System.Inc(LBufStart, LCount);
System.SetLength(Buf, LBufStart);
ADecryptedMessage := System.Copy(Buf);
Result := True;
except
Result := False;
end;
end;
class function TPCCryptoLib4Pascal.DoPascalCoinAESEncrypt(const AMessage, APassword: TBytes): TBytes;
var
SaltBytes, KeyBytes, IVBytes, Buf, LAuxBuf: TRawBytes;
KeyParametersWithIV: IParametersWithIV;
cipher: IBufferedCipher;
LBlockSize, LBufStart, Count: Int32;
begin
SaltBytes := EVP_GetSalt();
EVP_GetKeyIV(APassword, SaltBytes, KeyBytes, IVBytes);
cipher := TCipherUtilities.GetCipher('AES/CBC/PKCS7PADDING');
KeyParametersWithIV := TParametersWithIV.Create
(TParameterUtilities.CreateKeyParameter('AES', KeyBytes), IVBytes);
cipher.Init(True, KeyParametersWithIV); // init encryption cipher
LBlockSize := cipher.GetBlockSize;
System.SetLength(Buf, System.Length(AMessage) + LBlockSize + SALT_MAGIC_LEN + PKCS5_SALT_LEN);
LBufStart := 0;
LAuxBuf.FromString(SALT_MAGIC);
System.Move(LAuxBuf[0], Buf[LBufStart], SALT_MAGIC_LEN * System.SizeOf(byte));
System.Inc(LBufStart, SALT_MAGIC_LEN);
System.Move(SaltBytes[0], Buf[LBufStart], PKCS5_SALT_LEN * System.SizeOf(byte));
System.Inc(LBufStart, PKCS5_SALT_LEN);
Count := cipher.ProcessBytes(AMessage, 0, System.Length(AMessage), Buf, LBufStart);
System.Inc(LBufStart, Count);
Count := cipher.DoFinal(Buf, LBufStart);
System.Inc(LBufStart, Count);
System.SetLength(Buf, LBufStart);
Result := Buf;
end;
class function TPCCryptoLib4Pascal.DoPascalCoinECIESDecrypt(
const AEC_OpenSSL_NID: Word; const APrivateKey, AEncryptedMessage: TBytes;
var ADecryptedMessage: TBytes): Boolean;
var
LDomain: IECDomainParameters;
LPrivD: TBigInteger;
LPrivKeyParams : IECPrivateKeyParameters;
LCurve: IX9ECParameters;
LBigXCoord, LBigYCoord : TBigInteger;
LPoint: IECPoint;
LPubKeyParams : IECPublicKeyParameters;
//
LCipherDecrypt: IIESCipher;
begin
try
LDomain := GetDomainParameters(AEC_OpenSSL_NID);
LPrivD := TBigInteger.Create(1, APrivateKey);
LPrivKeyParams := TECPrivateKeyParameters.Create('ECDSA',LPrivD,LDomain);
// Decryption
LCipherDecrypt := TIESCipher.Create(FPascalCoinIESEngine);
LCipherDecrypt.Init(False, LPrivKeyParams, FIESParameterSpec, FRandom);
ADecryptedMessage := System.Copy(LCipherDecrypt.DoFinal(AEncryptedMessage));
Result := True;
except
Result := False;
end;
end;
class function TPCCryptoLib4Pascal.DoPascalCoinECIESEncrypt(
const APublicKey: TECDSA_Public; const AMessage: TBytes;
var AEncryptedMessage: TBytes): Boolean;
var
LDomain: IECDomainParameters;
LCurve: IX9ECParameters;
LBigXCoord, LBigYCoord : TBigInteger;
LPoint: IECPoint;
LPubKeyParams : IECPublicKeyParameters;
//
LCipherEncrypt: IIESCipher;
begin
GetCurveAndDomainParameters(APublicKey.EC_OpenSSL_NID,LCurve,LDomain);
LBigXCoord := TBigInteger.Create(1, APublicKey.x);
LBigYCoord := TBigInteger.Create(1, APublicKey.y);
LPoint := LCurve.Curve.CreatePoint(LBigXCoord, LBigYCoord);
LPubKeyParams := TECPublicKeyParameters.Create('ECDSA', LPoint, LDomain);
// Encryption
LCipherEncrypt := TIESCipher.Create(FPascalCoinIESEngine);
LCipherEncrypt.Init(True, LPubKeyParams, FIESParameterSpec, FRandom);
AEncryptedMessage := LCipherEncrypt.DoFinal(AMessage);
Result := True;
end;
class procedure TPCCryptoLib4Pascal.DoRIPEMD160(const AInput: TBytes; var AOutput: TBytes);
Var Lrmd160 : TRIPEMD160;
begin
Lrmd160 := TRIPEMD160.Create;
try
AOutput := Lrmd160.ComputeBytes(AInput).GetBytes;
finally
Lrmd160.Free;
end;
end;
class procedure TPCCryptoLib4Pascal.DoSHA256(const AInput: TBytes; var AOutput: TBytes);
var Lsha : TSHA2_256;
begin
Lsha := TSHA2_256.Create;
try
AOutput := Lsha.ComputeBytes(AInput).GetBytes;
finally
Lsha.Free;
end;
end;
class function TPCCryptoLib4Pascal.EVP_GetKeyIV(const APasswordBytes,
ASaltBytes: TBytes; out AKeyBytes, AIVBytes: TBytes): boolean;
var
LKey, LIV: Int32;
LDigest: IDigest;
begin
LKey := 32; // AES256 CBC Key Length
LIV := 16; // AES256 CBC IV Length
System.SetLength(AKeyBytes, LKey);
System.SetLength(AIVBytes, LKey);
// Max size to start then reduce it at the end
LDigest := TDigestUtilities.GetDigest('SHA-256'); // SHA2_256
System.Assert(LDigest.GetDigestSize >= LKey);
System.Assert(LDigest.GetDigestSize >= LIV);
// Derive Key First
LDigest.BlockUpdate(APasswordBytes, 0, System.Length(APasswordBytes));
if ASaltBytes <> Nil then
begin
LDigest.BlockUpdate(ASaltBytes, 0, System.Length(ASaltBytes));
end;
LDigest.DoFinal(AKeyBytes, 0);
// Derive IV Next
LDigest.Reset();
LDigest.BlockUpdate(AKeyBytes, 0, System.Length(AKeyBytes));
LDigest.BlockUpdate(APasswordBytes, 0, System.Length(APasswordBytes));
if ASaltBytes <> Nil then
begin
LDigest.BlockUpdate(ASaltBytes, 0, System.Length(ASaltBytes));
end;
LDigest.DoFinal(AIVBytes, 0);
System.SetLength(AIVBytes, LIV);
Result := True;
end;
class function TPCCryptoLib4Pascal.EVP_GetSalt: TBytes;
begin
System.SetLength(Result, PKCS5_SALT_LEN);
FRandom.NextBytes(Result);
end;
class function TPCCryptoLib4Pascal.GetCurveAndDomainParameters(
const AEC_OpenSSL_NID: Word; var OCurve: IX9ECParameters;
var ODomain: IECDomainParameters; ARaiseIfNotForPascal: Boolean): Boolean;
begin
Result := True;
case AEC_OpenSSL_NID of
CT_NID_secp256k1 : begin
OCurve := FCurve_SECP256K1;
ODomain := FDomain_SECP256K1;
end;
CT_NID_secp384r1 : begin
OCurve := FCurve_SECP384R1;
ODomain := FDomain_SECP384R1;
end;
CT_NID_secp521r1 : begin
OCurve := FCurve_SECP521R1;
ODomain := FDomain_SECP521R1;
end;
CT_NID_sect283k1 : begin
OCurve := FCurve_SECT283K1;
ODomain := FDomain_SECT283K1;
end;
else
if ARaiseIfNotForPascal then raise Exception.Create(Format('Invalid Curve type:%d',[AEC_OpenSSL_NID]))
else Result := False;
end;
end;
class function TPCCryptoLib4Pascal.GetDomainParameters(const AEC_OpenSSL_NID: Word): IECDomainParameters;
begin
case AEC_OpenSSL_NID of
CT_NID_secp256k1 : Result := FDomain_SECP256K1;
CT_NID_secp384r1 : Result := FDomain_SECP384R1;
CT_NID_secp521r1 : Result := FDomain_SECP521R1;
CT_NID_sect283k1 : Result := FDomain_SECT283K1;
else raise Exception.Create(Format('Invalid Curve type:%d',[AEC_OpenSSL_NID]));
end;
end;
class constructor TPCCryptoLib4Pascal.TPCCryptoLib4Pascal;
function GetIESParameterSpec: IIESParameterSpec;
var
Derivation, Encoding, IVBytes: TBytes;
MacKeySizeInBits, CipherKeySizeInBits: Int32;
UsePointCompression: boolean;
begin
// Set up IES Cipher Parameters For Compatibility With PascalCoin Current Implementation
// The derivation and encoding vectors are used when initialising the KDF and MAC.
// They're optional but if used then they need to be known by the other user so that
// they can decrypt the ciphertext and verify the MAC correctly. The security is based
// on the shared secret coming from the (static-ephemeral) ECDH key agreement.
Derivation := nil;
Encoding := nil;
System.SetLength(IVBytes, 16); // using Zero Initialized IV for compatibility
MacKeySizeInBits := 32 * 8;
// Since we are using AES256_CBC for compatibility
CipherKeySizeInBits := 32 * 8;
// whether to use point compression when deriving the octets string
// from a point or not in the EphemeralKeyPairGenerator
UsePointCompression := True; // for compatibility
Result := TIESParameterSpec.Create(Derivation, Encoding,
MacKeySizeInBits, CipherKeySizeInBits, IVBytes, UsePointCompression);
end;
function GetECIESPascalCoinCompatibilityEngine(): IPascalCoinIESEngine;
var
cipher: IBufferedBlockCipher;
AesEngine: IAesEngine;
blockCipher: ICbcBlockCipher;
ECDHBasicAgreementInstance: IECDHBasicAgreement;
KDFInstance: IPascalCoinECIESKdfBytesGenerator;
DigestMACInstance: IMac;
begin
// Set up IES Cipher Engine For Compatibility With PascalCoin
ECDHBasicAgreementInstance := TECDHBasicAgreement.Create();
KDFInstance := TPascalCoinECIESKdfBytesGenerator.Create
(TDigestUtilities.GetDigest('SHA-512'));
DigestMACInstance := TMacUtilities.GetMac('HMAC-MD5');
// Set Up Block Cipher
AesEngine := TAesEngine.Create(); // AES Engine
blockCipher := TCbcBlockCipher.Create(AesEngine); // CBC
cipher := TPaddedBufferedBlockCipher.Create(blockCipher,
TZeroBytePadding.Create() as IZeroBytePadding); // ZeroBytePadding
Result := TPascalCoinIESEngine.Create(ECDHBasicAgreementInstance, KDFInstance,
DigestMACInstance, cipher);
end;
var LCipher: IBufferedBlockCipher;
LAesEngine: IAesEngine;
blockCipher: ICbcBlockCipher;
ECDHBasicAgreementInstance: IECDHBasicAgreement;
KDFInstance: IPascalCoinECIESKdfBytesGenerator;
DigestMACInstance: IMac;
begin
FRandom := TSecureRandom.Create();
// Init Curves and Domains for quick usage
FCurve_SECP256K1 := TCustomNamedCurves.GetByName('SECP256K1');
FDomain_SECP256K1 := TECDomainParameters.Create(FCurve_SECP256K1.Curve, FCurve_SECP256K1.G, FCurve_SECP256K1.N, FCurve_SECP256K1.H, FCurve_SECP256K1.GetSeed);
FCurve_SECP384R1 := TCustomNamedCurves.GetByName('SECP384R1');
FDomain_SECP384R1 := TECDomainParameters.Create(FCurve_SECP384R1.Curve, FCurve_SECP384R1.G, FCurve_SECP384R1.N, FCurve_SECP384R1.H, FCurve_SECP384R1.GetSeed);
FCurve_SECT283K1 := TCustomNamedCurves.GetByName('SECT283K1');
FDomain_SECT283K1 := TECDomainParameters.Create(FCurve_SECT283K1.Curve, FCurve_SECT283K1.G, FCurve_SECT283K1.N, FCurve_SECT283K1.H, FCurve_SECT283K1.GetSeed);
FCurve_SECP521R1 := TCustomNamedCurves.GetByName('SECP521R1');
FDomain_SECP521R1 := TECDomainParameters.Create(FCurve_SECP521R1.Curve, FCurve_SECP521R1.G, FCurve_SECP521R1.N, FCurve_SECP521R1.H, FCurve_SECP521R1.GetSeed);
// Init ECIES
FPascalCoinIESEngine := GetECIESPascalCoinCompatibilityEngine;
FIESParameterSpec := GetIESParameterSpec;
end;
end.