-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathClpRsaGenerators.pas
More file actions
230 lines (210 loc) · 7.4 KB
/
Copy pathClpRsaGenerators.pas
File metadata and controls
230 lines (210 loc) · 7.4 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
{ *********************************************************************************** }
{ * 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 ClpRsaGenerators;
{$I ..\..\Include\CryptoLib.inc}
interface
uses
SysUtils,
ClpArrayUtilities,
ClpBigInteger,
ClpBigIntegerUtilities,
ClpBitOperations,
ClpIKeyGenerationParameters,
ClpIRsaParameters,
ClpRsaParameters,
ClpIAsymmetricCipherKeyPair,
ClpAsymmetricCipherKeyPair,
ClpIAsymmetricCipherKeyPairGenerator,
ClpIRsaGenerators,
ClpWNafUtilities,
ClpICipherParameters,
ClpParameterUtilities,
ClpISecureRandom,
ClpCryptoServicesRegistrar,
ClpCryptoLibTypes;
resourcestring
SRsaKeyGenNotInit = 'RSA key pair generator not initialised';
SGeneratorNotInit = 'Generator not initialised';
SPublicKeyRequired = 'Generator requires RSA public key';
SRsaKeyParametersRequired = 'Parameters must support IRsaKeyParameters';
type
TRsaKeyPairGenerator = class(TInterfacedObject,
IAsymmetricCipherKeyPairGenerator, IRsaKeyPairGenerator)
strict private
class var
FDefaultPublicExponent: TBigInteger;
FSpecialEValues: TCryptoLibInt32Array;
FSpecialEHighest: Int32;
FSpecialEBits: Int32;
var
FParam: IRsaKeyGenerationParameters;
class constructor CreateRsaKeyPairGenerator;
function ChooseRandomPrime(ABitLength: Int32;
const AE: TBigInteger): TBigInteger;
public
const DefaultTests = 100;
constructor Create();
procedure Init(const AParameters: IKeyGenerationParameters);
function GenerateKeyPair(): IAsymmetricCipherKeyPair;
end;
TRsaBlindingFactorGenerator = class(TInterfacedObject, IRsaBlindingFactorGenerator)
strict private
FKey: IRsaKeyParameters;
FRandom: ISecureRandom;
public
constructor Create();
procedure Init(const AParam: ICipherParameters);
function GenerateBlindingFactor: TBigInteger;
end;
implementation
{ TRsaKeyPairGenerator }
class constructor TRsaKeyPairGenerator.CreateRsaKeyPairGenerator;
begin
FSpecialEValues := TCryptoLibInt32Array.Create(3, 5, 17, 257, 65537);
FSpecialEHighest := 65537;
FSpecialEBits := TBigInteger.ValueOf(FSpecialEHighest).BitLength;
FDefaultPublicExponent := TBigInteger.ValueOf($10001);
end;
constructor TRsaKeyPairGenerator.Create;
begin
inherited Create();
FParam := nil;
end;
procedure TRsaKeyPairGenerator.Init(const AParameters: IKeyGenerationParameters);
begin
if not Supports(AParameters, IRsaKeyGenerationParameters, FParam) then
FParam := TRsaKeyGenerationParameters.Create(
FDefaultPublicExponent, AParameters.Random, AParameters.Strength, DefaultTests);
end;
function TRsaKeyPairGenerator.ChooseRandomPrime(ABitLength: Int32;
const AE: TBigInteger): TBigInteger;
var
LP, LPSub1: TBigInteger;
LEIsKnownOddPrime: Boolean;
begin
LEIsKnownOddPrime := (AE.BitLength <= FSpecialEBits) and
(TArrayUtilities.Contains(FSpecialEValues, AE.Int32Value));
while True do
begin
LP := TBigIntegerUtilities.CreateRandomPrime(ABitLength, FParam.Certainty, FParam.Random);
if LP.&Mod(AE).Equals(TBigInteger.One) then
Continue;
if not LEIsKnownOddPrime then
begin
LPSub1 := LP.Subtract(TBigInteger.One);
if not AE.Gcd(LPSub1).Equals(TBigInteger.One) then
Continue;
end;
Result := LP;
Exit;
end;
end;
function TRsaKeyPairGenerator.GenerateKeyPair: IAsymmetricCipherKeyPair;
var
LStrength, LPBitLength, LQBitLength, LMinDiffBits, LMinWeight: Int32;
LE, LP, LQ, LPSub1, LQSub1: TBigInteger;
LN, LD, LDP, LDQ, LQInv: TBigInteger;
LDiff, LGcd, LLcm, LTmp: TBigInteger;
LPubKey: IRsaKeyParameters;
LPrivKey: IRsaPrivateCrtKeyParameters;
begin
if FParam = nil then
raise EInvalidOperationCryptoLibException.CreateRes(@SRsaKeyGenNotInit);
while True do
begin
LStrength := FParam.Strength;
LE := FParam.PublicExponent;
LPBitLength := (LStrength + 1) div 2;
LQBitLength := LStrength - LPBitLength;
LMinDiffBits := LStrength div 3;
LMinWeight := TBitOperations.Asr32(LStrength, 2);
LP := ChooseRandomPrime(LPBitLength, LE);
while True do
begin
LQ := ChooseRandomPrime(LQBitLength, LE);
LDiff := LQ.Subtract(LP).Abs();
if LDiff.BitLength < LMinDiffBits then
Continue;
LN := LP.Multiply(LQ);
if LN.BitLength <> LStrength then
begin
LP := LP.Max(LQ);
Continue;
end;
if TWNafUtilities.GetNafWeight(LN) < LMinWeight then
begin
LP := ChooseRandomPrime(LPBitLength, LE);
Continue;
end;
Break;
end;
if LP.CompareTo(LQ) < 0 then
begin
LTmp := LP;
LP := LQ;
LQ := LTmp;
end;
LPSub1 := LP.Subtract(TBigInteger.One);
LQSub1 := LQ.Subtract(TBigInteger.One);
LGcd := LPSub1.Gcd(LQSub1);
LLcm := LPSub1.Divide(LGcd).Multiply(LQSub1);
LD := LE.ModInverse(LLcm);
if LD.BitLength <= LQBitLength then
Continue;
LDP := LD.Remainder(LPSub1);
LDQ := LD.Remainder(LQSub1);
LQInv := TBigIntegerUtilities.ModOddInverse(LP, LQ);
LPubKey := TRsaKeyParameters.Create(False, LN, LE, True);
LPrivKey := TRsaPrivateCrtKeyParameters.Create(LN, LE, LD, LP, LQ, LDP, LDQ, LQInv, True);
Result := TAsymmetricCipherKeyPair.Create(LPubKey, LPrivKey);
Exit;
end;
end;
{ TRsaBlindingFactorGenerator }
constructor TRsaBlindingFactorGenerator.Create;
begin
inherited Create();
FKey := nil;
FRandom := nil;
end;
procedure TRsaBlindingFactorGenerator.Init(const AParam: ICipherParameters);
var
LParameters: ICipherParameters;
LProvidedRandom: ISecureRandom;
begin
LParameters := TParameterUtilities.GetRandom(AParam, LProvidedRandom);
if not Supports(LParameters, IRsaKeyParameters, FKey) then
raise EArgumentCryptoLibException.CreateRes(@SRsaKeyParametersRequired);
FRandom := TCryptoServicesRegistrar.GetSecureRandom(LProvidedRandom);
if FKey.IsPrivate then
raise EArgumentCryptoLibException.CreateRes(@SPublicKeyRequired);
end;
function TRsaBlindingFactorGenerator.GenerateBlindingFactor: TBigInteger;
var
LM: TBigInteger;
LLen: Int32;
LFactor: TBigInteger;
begin
if FKey = nil then
raise EInvalidOperationCryptoLibException.CreateRes(@SGeneratorNotInit);
LM := FKey.Modulus;
LLen := LM.BitLength - 1;
repeat
LFactor := TBigInteger.Create(LLen, FRandom);
until (LFactor.CompareTo(TBigInteger.Two) >= 0) and
TBigIntegerUtilities.ModOddIsCoprimeVar(LM, LFactor);
Result := LFactor;
end;
end.