-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathClpDHGenerators.pas
More file actions
136 lines (119 loc) · 4.85 KB
/
ClpDHGenerators.pas
File metadata and controls
136 lines (119 loc) · 4.85 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
{ *********************************************************************************** }
{ * CryptoLib Library * }
{ * Author - Ugochukwu Mmaduekwe * }
{ * Github Repository <https://github.com/Xor-el> * }
{ * * }
{ * Distributed under the MIT software license, see the accompanying file LICENSE * }
{ * or visit http://www.opensource.org/licenses/mit-license.php. * }
{ * * }
{ * Acknowledgements: * }
{ * * }
{ * Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring * }
{ * the development of this library * }
{ * ******************************************************************************* * }
(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
unit ClpDHGenerators;
{$I ..\..\Include\CryptoLib.inc}
interface
uses
SysUtils,
ClpIDHParameters,
ClpIDHGenerators,
ClpDHParameters,
ClpAsymmetricCipherKeyPair,
ClpIAsymmetricCipherKeyPair,
ClpIKeyGenerationParameters,
ClpDHKeyGeneratorHelper,
ClpIAsymmetricCipherKeyPairGenerator,
ClpISecureRandom,
ClpDHParametersHelper,
ClpBigInteger,
ClpCryptoLibTypes;
resourcestring
SParametersCannotBeNil = '"parameters" Cannot Be Nil';
type
TDHKeyPairGenerator = class sealed(TInterfacedObject,
IAsymmetricCipherKeyPairGenerator, IDHKeyPairGenerator)
strict private
FParam: IDHKeyGenerationParameters;
public
procedure Init(const AParameters: IKeyGenerationParameters);
function GenerateKeyPair(): IAsymmetricCipherKeyPair;
end;
TDHBasicKeyPairGenerator = class sealed(TInterfacedObject,
IAsymmetricCipherKeyPairGenerator, IDHBasicKeyPairGenerator)
strict private
FParam: IDHKeyGenerationParameters;
public
procedure Init(const AParameters: IKeyGenerationParameters);
function GenerateKeyPair(): IAsymmetricCipherKeyPair;
end;
TDHParametersGenerator = class(TInterfacedObject, IDHParametersGenerator)
strict private
FCertainty, FSize: Int32;
FRandom: ISecureRandom;
public
procedure Init(ASize, ACertainty: Int32; const ARandom: ISecureRandom);
function GenerateParameters(): IDHParameters; virtual;
end;
implementation
function TDHKeyPairGenerator.GenerateKeyPair: IAsymmetricCipherKeyPair;
var
LDhp: IDHParameters;
LX, LY: TBigInteger;
begin
LDhp := FParam.Parameters;
LX := TDHKeyGeneratorHelper.CalculatePrivate(LDhp, FParam.Random);
LY := TDHKeyGeneratorHelper.CalculatePublic(LDhp, LX);
Result := TAsymmetricCipherKeyPair.Create(TDHPublicKeyParameters.Create(LY,
LDhp) as IDHPublicKeyParameters, TDHPrivateKeyParameters.Create(LX, LDhp)
as IDHPrivateKeyParameters);
end;
procedure TDHKeyPairGenerator.Init(const AParameters: IKeyGenerationParameters);
begin
if AParameters = nil then
raise EArgumentNilCryptoLibException.CreateRes(@SParametersCannotBeNil);
if not Supports(AParameters, IDHKeyGenerationParameters, FParam) then
raise EArgumentNilCryptoLibException.CreateRes(@SParametersCannotBeNil);
end;
function TDHBasicKeyPairGenerator.GenerateKeyPair: IAsymmetricCipherKeyPair;
var
LDhp: IDHParameters;
LX, LY: TBigInteger;
begin
LDhp := FParam.Parameters;
LX := TDHKeyGeneratorHelper.CalculatePrivate(LDhp, FParam.Random);
LY := TDHKeyGeneratorHelper.CalculatePublic(LDhp, LX);
Result := TAsymmetricCipherKeyPair.Create(TDHPublicKeyParameters.Create(LY,
LDhp) as IDHPublicKeyParameters, TDHPrivateKeyParameters.Create(LX, LDhp)
as IDHPrivateKeyParameters);
end;
procedure TDHBasicKeyPairGenerator.Init(const AParameters: IKeyGenerationParameters);
begin
if AParameters = nil then
raise EArgumentNilCryptoLibException.CreateRes(@SParametersCannotBeNil);
if not Supports(AParameters, IDHKeyGenerationParameters, FParam) then
raise EArgumentNilCryptoLibException.CreateRes(@SParametersCannotBeNil);
end;
function TDHParametersGenerator.GenerateParameters: IDHParameters;
var
LSafePrimes: TCryptoLibGenericArray<TBigInteger>;
LP, LQ, LG: TBigInteger;
begin
LSafePrimes := TDHParametersHelper.GenerateSafePrimes(FSize, FCertainty, FRandom, True);
LP := LSafePrimes[0];
LQ := LSafePrimes[1];
{$IFDEF DEBUG}
Assert((LP.Int32ValueExact and 7) = 7);
{$ENDIF DEBUG}
LG := TBigInteger.Two;
Result := TDHParameters.Create(LP, LG, LQ, TBigInteger.Two, nil);
end;
procedure TDHParametersGenerator.Init(ASize, ACertainty: Int32;
const ARandom: ISecureRandom);
begin
FSize := ASize;
FCertainty := ACertainty;
FRandom := ARandom;
end;
end.