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
|
/** \ingroup DL_m
* \file dldp.c
*
* Discrete Logarithm Domain Parameters, code.
*/
/*
* <conformance statement for IEEE P1363 needed here>
*
* Copyright (c) 2000, 2001 Virtual Unlimited B.V.
*
* Author: Bob Deblier <bob@virtualunlimited.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "system.h"
#include "dldp.h"
#include "mp.h"
#include "mpprime.h"
#include "debug.h"
/**
*/
static int dldp_pgoqGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, /*@out@*/ uint32* wksp)
/*@modifies dp->g, wksp @*/;
/**
*/
static int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, /*@out@*/ uint32* wksp)
/*@modifies dp->g, wksp @*/;
int dldp_pPrivate(const dldp_p* dp, randomGeneratorContext* rgc, mp32number* x)
{
/*
* Note: the private key is randomly selected to be smaller than q
*
* This is the variant of Diffie-Hellman as described in IEEE P1363
*/
mp32bnrnd(&dp->q, rgc, x);
return 0;
}
int dldp_pPublic(const dldp_p* dp, const mp32number* x, mp32number* y)
{
/*
* Public key y is computed as g^x mod p
*/
mp32bnpowmod(&dp->p, &dp->g, x, y);
return 0;
}
int dldp_pPair(const dldp_p* dp, randomGeneratorContext* rgc, mp32number* x, mp32number* y)
{
/*
* Combination of the two previous functions
*/
mp32bnrnd(&dp->q, rgc, x);
mp32bnpowmod(&dp->p, &dp->g, x, y);
return 0;
}
int dldp_pEqual(const dldp_p* a, const dldp_p* b)
{
return mp32eqx(a->p.size, a->p.modl, b->p.size, b->p.modl) &&
mp32eqx(a->q.size, a->q.modl, b->q.size, b->q.modl) &&
mp32eqx(a->g.size, a->g.data, b->g.size, b->g.data);
}
/**
* needs to make workspace of 8*size+2
*/
static int dldp_pValidate(const dldp_p* dp, randomGeneratorContext* rgc)
/*@*/
{
register uint32 size = dp->p.size;
register uint32* temp = (uint32*) malloc((8*size+2) * sizeof(*temp));
if (temp)
{
/* check that p > 2 and p odd, then run miller-rabin test with t 50 */
if (mp32even(dp->p.size, dp->p.modl))
{
free(temp);
return 0;
}
if (mp32pmilrab_w(&dp->p, rgc, 50, temp) == 0)
{
free(temp);
return 0;
}
/* check that q > 2 and q odd, then run miller-rabin test with t 50 */
if (mp32even(dp->q.size, dp->q.modl))
{
free(temp);
return 0;
}
if (mp32pmilrab_w(&dp->q, rgc, 50, temp) == 0)
{
free(temp);
return 0;
}
free(temp);
/* check that 1 < g < p */
if (mp32leone(dp->g.size, dp->g.data))
return 0;
if (mp32gex(dp->g.size, dp->g.data, dp->p.size, dp->p.modl))
return 0;
return 1;
}
return -1;
}
int dldp_pInit(dldp_p* dp)
{
mp32bzero(&dp->p);
mp32bzero(&dp->q);
mp32nzero(&dp->g);
mp32nzero(&dp->r);
mp32bzero(&dp->n);
return 0;
}
int dldp_pFree(dldp_p* dp)
{
/*@-usedef -compdef@*/
mp32bfree(&dp->p);
mp32bfree(&dp->q);
mp32nfree(&dp->g);
mp32nfree(&dp->r);
mp32bfree(&dp->n);
/*@=usedef =compdef@*/
return 0;
}
int dldp_pCopy(dldp_p* dst, const dldp_p* src)
{
mp32bcopy(&dst->p, &src->p);
mp32bcopy(&dst->q, &src->q);
mp32ncopy(&dst->r, &src->r);
mp32ncopy(&dst->g, &src->g);
mp32bcopy(&dst->n, &src->n);
return 0;
}
int dldp_pgoqMake(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize, uint32 qsize, int cofactor)
{
/*
* Generate parameters as described by IEEE P1363, A.16.1
*/
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
if (temp)
{
/* first generate q */
/*@-globs@*/
mp32prnd_w(&dp->q, rgc, qsize, mp32ptrials(qsize << 5), (const mp32number*) 0, temp);
/*@=globs@*/
/* generate p with the appropriate congruences */
/*@-globs@*/
mp32prndconone_w(&dp->p, rgc, psize, mp32ptrials(psize << 5), &dp->q, (const mp32number*) 0, &dp->r, cofactor, temp);
/*@=globs@*/
/* clear n */
mp32bzero(&dp->n);
/* clear g */
mp32nzero(&dp->g);
(void) dldp_pgoqGenerator_w(dp, rgc, temp);
free(temp);
return 0;
}
return -1;
}
/*@-bounds@*/
int dldp_pgoqMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize)
{
/*
* Generate parameters with a safe prime; p = 2q+1 i.e. r=2
*
*/
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
if (temp)
{
/* generate p */
/*@-globs@*/
mp32prndsafe_w(&dp->p, rgc, psize, mp32ptrials(psize << 5), temp);
/*@=globs@*/
/* set q */
mp32copy(psize, temp, dp->p.modl);
mp32divtwo(psize, temp);
mp32bset(&dp->q, psize, temp);
/* set r = 2 */
mp32nsetw(&dp->r, 2);
/* clear n */
mp32bzero(&dp->n);
(void) dldp_pgoqGenerator_w(dp, rgc, temp);
free(temp);
return 0;
}
return -1;
}
/*@=bounds@*/
int dldp_pgoqGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
{
/*
* Randomly determine a generator over the subgroup with order q
*/
register uint32 size = dp->p.size;
mp32nfree(&dp->g);
mp32nsize(&dp->g, size);
while (1)
{
/* get a random value h (stored into g) */
mp32brnd_w(&dp->p, rgc, dp->g.data, wksp);
/* first compute h^r mod p (stored in g) */
mp32bpowmod_w(&dp->p, size, dp->g.data, dp->r.size, dp->r.data, dp->g.data, wksp);
if (mp32isone(size, dp->g.data))
continue;
return 0;
}
return -1;
}
int dldp_pgoqGenerator(dldp_p* dp, randomGeneratorContext* rgc)
{
register uint32 size = dp->p.size;
register uint32* temp = (uint32*) malloc((4*size+2) * sizeof(*temp));
if (temp)
{
(void) dldp_pgoqGenerator_w(dp, rgc, temp);
free(temp);
return 0;
}
return -1;
}
int dldp_pgoqValidate(const dldp_p* dp, randomGeneratorContext* rgc, /*@unused@*/ int cofactor)
{
register int rc = dldp_pValidate(dp, rgc);
if (rc <= 0)
return rc;
/* check that g^q mod p = 1 */
/* if r != 0, then check that qr+1 = p */
/* if cofactor, then check that q does not divide (r) */
return 1;
}
int dldp_pgonMake(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize, uint32 qsize)
{
/*
* Generate parameters with a prime p such that p = qr+1, with q prime, and r = 2s, with s prime
*/
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
if (temp)
{
/* generate q */
/*@-globs@*/
mp32prnd_w(&dp->q, rgc, qsize, mp32ptrials(qsize << 5), (const mp32number*) 0, temp);
/*@=globs@*/
/* generate p with the appropriate congruences */
/*@-globs@*/
mp32prndconone_w(&dp->p, rgc, psize, mp32ptrials(psize << 5), &dp->q, (const mp32number*) 0, &dp->r, 2, temp);
/*@=globs@*/
/* set n */
mp32bsubone(&dp->p, temp);
mp32bset(&dp->n, psize, temp);
(void) dldp_pgonGenerator_w(dp, rgc, temp);
free(temp);
return 0;
}
return -1;
}
int dldp_pgonMakeSafe(dldp_p* dp, randomGeneratorContext* rgc, uint32 psize)
{
/*
* Generate parameters with a safe prime; i.e. p = 2q+1, where q is prime
*/
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
if (temp)
{
/* generate safe p */
/*@-globs@*/
mp32prndsafe_w(&dp->p, rgc, psize, mp32ptrials(psize << 5), temp);
/*@=globs@*/
/* set n */
mp32bsubone(&dp->p, temp);
mp32bset(&dp->n, psize, temp);
/* set q */
mp32copy(psize, temp, dp->p.modl);
mp32divtwo(psize, temp);
mp32bset(&dp->q, psize, temp);
/* set r = 2 */
mp32nsetw(&dp->r, 2);
(void) dldp_pgonGenerator_w(dp, rgc, temp);
free(temp);
return 0;
}
return -1;
}
int dldp_pgonGenerator_w(dldp_p* dp, randomGeneratorContext* rgc, uint32* wksp)
{
register uint32 size = dp->p.size;
mp32nfree(&dp->g);
mp32nsize(&dp->g, size);
while (1)
{
mp32brnd_w(&dp->p, rgc, dp->g.data, wksp);
if (mp32istwo(dp->r.size, dp->r.data))
{
/*
* A little math here: the only element in the group which has order 2 is (p-1);
* the two group elements raised to power two which result in 1 (mod p) are thus (p-1) and 1
*
* mp32brnd_w doesn't return 1 or (p-1), so the test where g^2 mod p = 1 can be safely skipped
*/
/* check g^q mod p*/
mp32bpowmod_w(&dp->p, size, dp->g.data, dp->q.size, dp->q.modl, wksp, wksp+size);
if (mp32isone(size, wksp))
continue;
}
else
{
/* we can either compute g^r, g^2q and g^(qr/2) or
* we first compute s = r/2, and then compute g^2s, g^2q and g^qs
*
* hence we first compute t = g^s
* then compute t^2 mod p, and test if one
* then compute t^q mod p, and test if one
* then compute (g^q mod p)^2 mod p, and test if one
*/
/* compute s = r/2 */
mp32setx(size, wksp, dp->r.size, dp->r.data);
mp32divtwo(size, wksp);
/* compute t = g^s mod p */
mp32bpowmod_w(&dp->p, size, dp->g.data, size, wksp, wksp+size, wksp+2*size);
/* compute t^2 mod p = g^2s mod p = g^r mod p*/
mp32bsqrmod_w(&dp->p, size, wksp+size, wksp+size, wksp+2*size);
if (mp32isone(size, wksp+size))
continue;
/* compute t^q mod p = g^qs mod p */
mp32bpowmod_w(&dp->p, size, wksp, dp->q.size, dp->q.modl, wksp+size, wksp+2*size);
if (mp32isone(size, wksp+size))
continue;
/* compute g^2q mod p */
mp32bpowmod_w(&dp->p, size, dp->g.data, dp->q.size, dp->q.modl, wksp, wksp+size);
mp32bsqrmod_w(&dp->p, size, wksp, wksp+size, wksp+2*size);
if (mp32isone(size, wksp+size))
continue;
}
return 0;
}
return -1;
}
int dldp_pgonGenerator(dldp_p* dp, randomGeneratorContext* rgc)
{
register uint32 psize = dp->p.size;
register uint32* temp = (uint32*) malloc((8*psize+2) * sizeof(*temp));
if (temp)
{
(void) dldp_pgonGenerator_w(dp, rgc, temp);
free(temp);
return 0;
}
return -1;
}
int dldp_pgonValidate(const dldp_p* dp, randomGeneratorContext* rgc)
{
return dldp_pValidate((const dldp_p*) dp, rgc);
}
|