-
Notifications
You must be signed in to change notification settings - Fork 2
/
diffiehellman.pas
406 lines (351 loc) · 10.4 KB
/
diffiehellman.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
unit DiffieHellman;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, CryptoUtils;
type
TDiffieHellmanRequest = record
Generator : DWORD;
Modulus : DWORD;
Interim : DWORD;
end;
TDiffieHellmanResponse = record
Interim : DWORD;
end;
{ TDiffieHellman }
TDiffieHellman = class(TObject)
private
FModulus : DWORD;
FGenerator : DWORD;
FPrivateA : DWORD;
FPrivateB : DWORD;
FInterimA : DWORD;
FInterimB : DWORD;
FKey : DWORD;
//
procedure Clean;
procedure CreateKeys(out Generator, Modulus: DWORD);
function CreateSenderInterKey: DWORD;
function CreateRecipientInterKey(Generator, Modulus: DWORD): DWORD;
procedure CreateSenderEncryptionKey(RecipientInterKey: DWORD);
procedure CreateRecipientEncryptionKey(SenderInterKey: DWORD);
public
constructor Create;
destructor Destroy; override;
//
procedure Clear;
function GenerateRequest: TDiffieHellmanRequest;
function ProcessRequest(Request: TDiffieHellmanRequest): TDiffieHellmanResponse;
procedure ReceiveResponse(Response: TDiffieHellmanResponse);
//
property Key: DWORD read FKey;
end;
TDiffieHellman128Request = array[0..3] of TDiffieHellmanRequest;
PDiffieHellman128Request = ^TDiffieHellman128Request;
TDiffieHellman128Response = array[0..3] of TDiffieHellmanResponse;
PDiffieHellman128Response = ^TDiffieHellman128Response;
{ TDiffieHellman128 }
TDiffieHellman128 = class(TObject)
private
A: array[0..3] of TDiffieHellman;
function GetKey: TKey128;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function GenerateRequest: TDiffieHellman128Request;
function ProcessRequest(Request: TDiffieHellman128Request): TDiffieHellman128Response;
procedure ReceiveResponse(Response: TDiffieHellman128Response);
function KeysMatch(DH: TDiffieHellman128): Boolean;
property Key: TKey128 read GetKey;
end;
TDiffieHellman256Request = array[0..7] of TDiffieHellmanRequest;
PDiffieHellman256Request = ^TDiffieHellman256Request;
TDiffieHellman256Response = array[0..7] of TDiffieHellmanResponse;
PDiffieHellman256Response = ^TDiffieHellman256Response;
{ TDiffieHellman256 }
TDiffieHellman256 = class(TObject)
private
A: array[0..7] of TDiffieHellman;
function GetKey: TKey256;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function GenerateRequest: TDiffieHellman256Request;
function ProcessRequest(Request: TDiffieHellman256Request): TDiffieHellman256Response;
procedure ReceiveResponse(Response: TDiffieHellman256Response);
function KeysMatch(DH: TDiffieHellman256): Boolean;
property Key: TKey256 read GetKey;
end;
function DHRequestToStr(Request: TDiffieHellmanRequest): String;
function DHResponseToStr(Response: TDiffieHellmanResponse): String;
function DH128RequestToStr(Request: TDiffieHellman128Request): String;
function DH128ResponseToStr(Response: TDiffieHellman128Response): String;
function DH256RequestToStr(Request: TDiffieHellman256Request): String;
function DH256ResponseToStr(Response: TDiffieHellman256Response): String;
implementation
uses
Rand;
function DHRequestToStr(Request: TDiffieHellmanRequest): String;
begin
Result := Format('Generator=%s Interim=%s Modulus=%s',[
BufferToHex(@Request.Generator,SizeOf(Request.Generator)),
BufferToHex(@Request.Interim,SizeOf(Request.Interim)),
BufferToHex(@Request.Modulus,SizeOf(Request.Modulus))
]);
end;
function DHResponseToStr(Response: TDiffieHellmanResponse): String;
begin
Result := Format('Interim=%s',[BufferToHex(@Response.Interim,SizeOf(Response.Interim))]);
end;
function DH128RequestToStr(Request: TDiffieHellman128Request): String;
begin
Result := '[0] '+DHRequestToStr(Request[0])+#13#10+
'[1] '+DHRequestToStr(Request[1])+#13#10+
'[2] '+DHRequestToStr(Request[2])+#13#10+
'[3] '+DHRequestToStr(Request[3])+#13#10;
end;
function DH128ResponseToStr(Response: TDiffieHellman128Response): String;
begin
Result := '[0] '+DHResponseToStr(Response[0])+#13#10+
'[1] '+DHResponseToStr(Response[1])+#13#10+
'[2] '+DHResponseToStr(Response[2])+#13#10+
'[3] '+DHResponseToStr(Response[3])+#13#10;
end;
function DH256RequestToStr(Request: TDiffieHellman256Request): String;
begin
Result := '[0] '+DHRequestToStr(Request[0])+#13#10+
'[1] '+DHRequestToStr(Request[1])+#13#10+
'[2] '+DHRequestToStr(Request[2])+#13#10+
'[3] '+DHRequestToStr(Request[3])+#13#10+
'[4] '+DHRequestToStr(Request[4])+#13#10+
'[5] '+DHRequestToStr(Request[5])+#13#10+
'[6] '+DHRequestToStr(Request[6])+#13#10+
'[7] '+DHRequestToStr(Request[7])+#13#10;
end;
function DH256ResponseToStr(Response: TDiffieHellman256Response): String;
begin
Result := '[0] '+DHResponseToStr(Response[0])+#13#10+
'[1] '+DHResponseToStr(Response[1])+#13#10+
'[2] '+DHResponseToStr(Response[2])+#13#10+
'[3] '+DHResponseToStr(Response[3])+#13#10+
'[4] '+DHResponseToStr(Response[4])+#13#10+
'[5] '+DHResponseToStr(Response[5])+#13#10+
'[6] '+DHResponseToStr(Response[6])+#13#10+
'[7] '+DHResponseToStr(Response[7])+#13#10;
end;
{ TDiffieHellman }
constructor TDiffieHellman.Create;
begin
Clear;
end;
destructor TDiffieHellman.Destroy;
begin
Clear;
inherited Destroy;
end;
// Zeros everything except key
procedure TDiffieHellman.Clean;
begin
FGenerator := 0; FModulus := 0;
FPrivateA := 0; FPrivateB := 0;
FInterimA := 0; FInterimB := 0;
end;
// Zeros everything
procedure TDiffieHellman.Clear;
begin
Clean;
FKey := 0;
end;
function TDiffieHellman.GenerateRequest: TDiffieHellmanRequest;
begin
CreateKeys(Result.Generator,Result.Modulus);
Result.Interim := CreateSenderInterKey;
end;
function TDiffieHellman.ProcessRequest(Request: TDiffieHellmanRequest): TDiffieHellmanResponse;
begin
Result.Interim := CreateRecipientInterKey(Request.Generator,Request.Modulus);
CreateRecipientEncryptionKey(Request.Interim);
Clean;
end;
procedure TDiffieHellman.ReceiveResponse(Response: TDiffieHellmanResponse);
begin
CreateSenderEncryptionKey(Response.Interim);
Clean;
end;
procedure TDiffieHellman.CreateKeys(out Generator, Modulus: DWORD);
var
Swap: DWORD;
begin
FGenerator := GeneratePrime;
FModulus := GeneratePrime;
if FGenerator > FModulus then
begin
Swap := FGenerator;
FGenerator := FModulus;
FModulus := Swap;
end;
Generator := FGenerator;
Modulus := FModulus;
end;
function TDiffieHellman.CreateSenderInterKey: DWORD;
begin
FPrivateA := RNG.Generate;
FInterimA := XpowYmodN(FGenerator,FPrivateA,FModulus);
Result := FInterimA;
end;
function TDiffieHellman.CreateRecipientInterKey(Generator, Modulus: DWORD): DWORD;
begin
FPrivateB := RNG.Generate;
FGenerator := Generator;
FModulus := Modulus;
FInterimB := XpowYmodN(FGenerator,FPrivateB,FModulus);
Result := FInterimB;
end;
procedure TDiffieHellman.CreateSenderEncryptionKey(RecipientInterKey: DWORD);
begin
FInterimB := RecipientInterKey;
FKey := XpowYmodN(FInterimB,FPrivateA,FModulus);
end;
procedure TDiffieHellman.CreateRecipientEncryptionKey(SenderInterKey: DWORD);
begin
FInterimA := SenderInterKey;
FKey := XpowYmodN(FInterimA,FPrivateB,FModulus);
end;
{ TDiffieHellman128 }
constructor TDiffieHellman128.Create;
var
I: Byte;
begin
for I := 0 to 3 do A[I] := TDiffieHellman.Create;
end;
destructor TDiffieHellman128.Destroy;
var
I: Byte;
begin
for I := 0 to 3 do A[I].Destroy;
inherited Destroy;
end;
procedure TDiffieHellman128.Clear;
var
I: Byte;
begin
for I := 0 to 3 do A[I].Clear;
end;
function TDiffieHellman128.GetKey: TKey128;
var
X: Byte;
D: DWORD;
begin
for X := 0 to 3 do
begin
D := A[X].Key;
Result[X*4+0] := (D and $FF000000) shr 24;
Result[X*4+1] := (D and $FF0000 ) shr 16;
Result[X*4+2] := (D and $FF00 ) shr 8;
Result[X*4+3] := (D and $FF );
end;
end;
function TDiffieHellman128.GenerateRequest: TDiffieHellman128Request;
var
I: Byte;
begin
for I := 0 to 3 do Result[I] := A[I].GenerateRequest;
end;
function TDiffieHellman128.ProcessRequest(Request: TDiffieHellman128Request): TDiffieHellman128Response;
var
I: Byte;
begin
for I := 0 to 3 do Result[I] := A[I].ProcessRequest(Request[I]);
end;
procedure TDiffieHellman128.ReceiveResponse(Response: TDiffieHellman128Response);
var
I: Byte;
begin
for I := 0 to 3 do A[I].ReceiveResponse(Response[I]);
end;
function TDiffieHellman128.KeysMatch(DH: TDiffieHellman128): Boolean;
var
I: Integer;
Key1, Key2: TKey128;
begin
Key1 := Key;
Key2 := DH.Key;
for I := 0 to 15 do
if Key1[I] <> Key2[I] then
begin
Result := False;
Exit;
end;
Result := True;
end;
{ TDiffieHellman256 }
constructor TDiffieHellman256.Create;
var
I: Byte;
begin
for I := 0 to 7 do A[I] := TDiffieHellman.Create;
end;
destructor TDiffieHellman256.Destroy;
var
I: Byte;
begin
for I := 0 to 7 do A[I].Destroy;
inherited Destroy;
end;
procedure TDiffieHellman256.Clear;
var
I: Byte;
begin
for I := 0 to 7 do A[I].Clear;
end;
function TDiffieHellman256.GetKey: TKey256;
var
X: Byte;
D: DWORD;
begin
for X := 0 to 7 do
begin
D := A[X].Key;
Result[X] := D;
{ Result[X*4+0] := (D and $FF000000) shr 24;
Result[X*4+1] := (D and $FF0000 ) shr 16;
Result[X*4+2] := (D and $FF00 ) shr 8;
Result[X*4+3] := (D and $FF ); }
end;
end;
function TDiffieHellman256.GenerateRequest: TDiffieHellman256Request;
var
I: Byte;
begin
for I := 0 to 7 do Result[I] := A[I].GenerateRequest;
end;
function TDiffieHellman256.ProcessRequest(Request: TDiffieHellman256Request): TDiffieHellman256Response;
var
I: Byte;
begin
for I := 0 to 7 do Result[I] := A[I].ProcessRequest(Request[I]);
end;
procedure TDiffieHellman256.ReceiveResponse(Response: TDiffieHellman256Response);
var
I: Byte;
begin
for I := 0 to 7 do A[I].ReceiveResponse(Response[I]);
end;
function TDiffieHellman256.KeysMatch(DH: TDiffieHellman256): Boolean;
var
I: Integer;
Key1, Key2: TKey256;
begin
Key1 := Key;
Key2 := DH.Key;
for I := 0 to 31 do
if Key1[I] <> Key2[I] then
begin
Result := False;
Exit;
end;
Result := True;
end;
end.