forked from Xor-el/CryptoLib4Pascal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleBase.pas
More file actions
281 lines (254 loc) · 8.65 KB
/
Copy pathExampleBase.pas
File metadata and controls
281 lines (254 loc) · 8.65 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
{ *********************************************************************************** }
{ * 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 ExampleBase;
interface
{$IFDEF FPC}
{$MODE DELPHI}
{$HINTS OFF}
{$WARNINGS OFF}
{$ENDIF FPC}
uses
SysUtils,
Classes,
Rtti,
ClpValueHelper,
ClpIAsymmetricCipherKeyPair,
ClpIAsymmetricKeyParameter,
ClpICipherParameters,
ExampleLogger,
ClpGeneratorUtilities,
ClpSecureRandom,
ClpBigInteger,
ClpRsaParameters,
ClpIRsaParameters,
ClpECParameters,
ClpIECParameters,
ClpEd25519Parameters,
ClpIEd25519Parameters,
ClpIAsymmetricCipherKeyPairGenerator,
ClpISecureRandom,
ClpECGenerators,
ClpIECGenerators,
ClpIKeyParameter,
ClpIOpenSslPemWriter,
ClpOpenSslPemWriter,
ClpIOpenSslPemReader,
ClpOpenSslPemReader,
ClpEncoders;
type
TExampleLogger = class
private
class var FDefaultLogger: ILogger;
public
class procedure SetDefaultLogger(const ALogger: ILogger);
class function GetDefaultLogger: ILogger;
end;
IExample = interface(IInterface)
['{30D86CA2-0F19-4AA6-B106-0A13241BC5AA}']
procedure Run;
end;
TExampleBase = class(TInterfacedObject, IExample)
protected
function ExportToPem(const AValue: TValue): string;
function ImportFromPem(const APem: string): TValue;
function ImportKeyPairFromPem(const APem: string;
out AKeyPair: IAsymmetricCipherKeyPair): Boolean;
function ImportKeyFromPem(const APem: string;
out AKey: IAsymmetricKeyParameter): Boolean;
function GenerateRsaKeyPair(AKeySize: Int32 = 2048): IAsymmetricCipherKeyPair;
function GenerateEcKeyPair(const ADomain: IECDomainParameters): IAsymmetricCipherKeyPair;
function GenerateEd25519KeyPair: IAsymmetricCipherKeyPair;
procedure VerifyPemRoundtrip(const AKeyPair: IAsymmetricCipherKeyPair;
const AKeyType: string);
procedure LogDerivedKey(const ALabel: string;
const AParams: ICipherParameters);
procedure LogWithLineBreak(const AMessage: string);
public
function Logger: ILogger;
procedure Run; virtual; abstract;
end;
implementation
{ TExampleLogger }
class procedure TExampleLogger.SetDefaultLogger(const ALogger: ILogger);
begin
FDefaultLogger := ALogger;
end;
class function TExampleLogger.GetDefaultLogger: ILogger;
begin
Result := FDefaultLogger;
end;
function TExampleBase.Logger: ILogger;
begin
Result := TExampleLogger.GetDefaultLogger;
end;
function TExampleBase.ExportToPem(const AValue: TValue): string;
var
LStream: TStringStream;
LWriter: IOpenSslPemWriter;
begin
LStream := TStringStream.Create('', TEncoding.ASCII);
try
LWriter := TOpenSslPemWriter.Create(LStream);
LWriter.WriteObject(AValue);
Result := LStream.DataString;
finally
LStream.Free;
end;
end;
function TExampleBase.ImportFromPem(const APem: string): TValue;
var
LStream: TStringStream;
LReader: IOpenSslPemReader;
begin
Result := TValue.Empty;
if (APem = '') then
Exit;
LStream := TStringStream.Create(APem, TEncoding.ASCII);
try
LReader := TOpenSslPemReader.Create(LStream);
Result := LReader.ReadObject();
finally
LStream.Free;
end;
end;
function TExampleBase.ImportKeyPairFromPem(const APem: string;
out AKeyPair: IAsymmetricCipherKeyPair): Boolean;
var
LStream: TStringStream;
LReader: IOpenSslPemReader;
LReadVal: TValue;
begin
Result := False;
LStream := TStringStream.Create(APem, TEncoding.ASCII);
try
LReader := TOpenSslPemReader.Create(LStream);
LReadVal := LReader.ReadObject();
if LReadVal.IsEmpty then
Exit;
Result := LReadVal.TryGetAsType<IAsymmetricCipherKeyPair>(AKeyPair);
finally
LStream.Free;
end;
end;
function TExampleBase.ImportKeyFromPem(const APem: string;
out AKey: IAsymmetricKeyParameter): Boolean;
var
LStream: TStringStream;
LReader: IOpenSslPemReader;
LReadVal: TValue;
begin
Result := False;
LStream := TStringStream.Create(APem, TEncoding.ASCII);
try
LReader := TOpenSslPemReader.Create(LStream);
LReadVal := LReader.ReadObject();
if LReadVal.IsEmpty then
Exit;
Result := LReadVal.TryGetAsType<IAsymmetricKeyParameter>(AKey);
finally
LStream.Free;
end;
end;
function TExampleBase.GenerateRsaKeyPair(AKeySize: Int32): IAsymmetricCipherKeyPair;
var
LKpg: IAsymmetricCipherKeyPairGenerator;
begin
LKpg := TGeneratorUtilities.GetKeyPairGenerator('RSA');
LKpg.Init(TRsaKeyGenerationParameters.Create(TBigInteger.ValueOf(65537),
TSecureRandom.Create() as ISecureRandom, AKeySize, 25) as IRsaKeyGenerationParameters);
Result := LKpg.GenerateKeyPair();
end;
function TExampleBase.GenerateEcKeyPair(
const ADomain: IECDomainParameters): IAsymmetricCipherKeyPair;
var
LKpg: IAsymmetricCipherKeyPairGenerator;
begin
LKpg := TGeneratorUtilities.GetKeyPairGenerator('ECDSA');
LKpg.Init(TECKeyGenerationParameters.Create(ADomain,
TSecureRandom.Create() as ISecureRandom) as IECKeyGenerationParameters);
Result := LKpg.GenerateKeyPair();
end;
function TExampleBase.GenerateEd25519KeyPair: IAsymmetricCipherKeyPair;
var
LKpg: IAsymmetricCipherKeyPairGenerator;
begin
LKpg := TGeneratorUtilities.GetKeyPairGenerator('Ed25519');
LKpg.Init(TEd25519KeyGenerationParameters.Create(
TSecureRandom.Create() as ISecureRandom) as IEd25519KeyGenerationParameters);
Result := LKpg.GenerateKeyPair();
end;
procedure TExampleBase.VerifyPemRoundtrip(const AKeyPair: IAsymmetricCipherKeyPair;
const AKeyType: string);
var
LPrivPem, LPubPem: string;
LReadPair: IAsymmetricCipherKeyPair;
LReadPriv, LReadPub: IAsymmetricKeyParameter;
begin
LPrivPem := ExportToPem(TValue.From<IAsymmetricKeyParameter>(AKeyPair.Private));
Logger.LogInformation('{0} Private Key PEM:{1}{2}', [AKeyType, sLineBreak, LPrivPem]);
LPubPem := ExportToPem(TValue.From<IAsymmetricKeyParameter>(AKeyPair.Public));
Logger.LogInformation('{0} Public Key PEM:{1}{2}', [AKeyType, sLineBreak, LPubPem]);
if ImportKeyPairFromPem(LPrivPem, LReadPair) then
begin
if LReadPair.Private.Equals(AKeyPair.Private) then
Logger.LogInformation('Private key roundtrip: match.', [])
else
Logger.LogWarning('Private key roundtrip: mismatch.', []);
if LReadPair.Public.Equals(AKeyPair.Public) then
Logger.LogInformation('Public key (from private PEM) roundtrip: match.', [])
else
Logger.LogWarning('Public key (from private PEM) roundtrip: mismatch.', []);
end
else if ImportKeyFromPem(LPrivPem, LReadPriv) then
begin
if LReadPriv.Equals(AKeyPair.Private) then
Logger.LogInformation('Private key roundtrip: match.', [])
else
Logger.LogWarning('Private key roundtrip: mismatch.', []);
end
else
begin
Logger.LogError('Failed to read back {0} private key from PEM.', [AKeyType]);
Exit;
end;
if not ImportKeyFromPem(LPubPem, LReadPub) then
begin
Logger.LogError('Failed to read back {0} public key from PEM.', [AKeyType]);
Exit;
end;
if LReadPub.Equals(AKeyPair.Public) then
Logger.LogInformation('Public key roundtrip: match.', [])
else
Logger.LogWarning('Public key roundtrip: mismatch.', []);
end;
procedure TExampleBase.LogDerivedKey(const ALabel: string;
const AParams: ICipherParameters);
var
LKey: IKeyParameter;
LDerived: TBytes;
begin
if Supports(AParams, IKeyParameter, LKey) then
begin
LDerived := LKey.GetKey();
Logger.LogInformation('{0} derived {1} bytes:{2}{3}', [ALabel, IntToStr(System.Length(LDerived)), sLineBreak, THexEncoder.Encode(LDerived, False)]);
end
else
Logger.LogWarning('{0}: could not get key parameter.', [ALabel]);
end;
procedure TExampleBase.LogWithLineBreak(const AMessage: string);
begin
Logger.LogInformation('{0}{1}', [AMessage, sLineBreak])
end;
end.