052b36d0 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: dh-gen.c,v 1.3 2004/04/08 01:36:15 mdw Exp $ |
052b36d0 |
4 | * |
5 | * Generate Diffie-Hellman parameters |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
052b36d0 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include "dh.h" |
33 | #include "grand.h" |
34 | #include "mp.h" |
35 | #include "mpmont.h" |
36 | #include "mprand.h" |
37 | #include "pfilt.h" |
38 | #include "pgen.h" |
39 | #include "prim.h" |
40 | #include "rabin.h" |
41 | |
42 | /*----- Main code ---------------------------------------------------------*/ |
43 | |
44 | /* --- @dh_gen@ --- * |
45 | * |
46 | * Arguments: @dh_param *dp@ = pointer to output parameter block |
47 | * @unsigned ql@ = length of %$q$% in bits, or zero |
48 | * @unsigned pl@ = length of %$p$% in bits |
49 | * @unsigned steps@ = number of steps to go |
50 | * @grand *r@ = random number source |
51 | * @pgen_proc *event@ = event handler function |
52 | * @void *ectx@ = argument for the event handler |
53 | * |
54 | * Returns: @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't. |
55 | * |
56 | * Use: Generates Diffie-Hellman parameters. |
57 | * |
58 | * The parameters are a prime %$q$%, relatively small, and a |
59 | * large prime %$p = kq + 1$% for some %$k$%, together with a |
60 | * generator %$g$% of the cyclic subgroup of order %$q$%. These |
61 | * are actually the same as the DSA parameter set, but the |
62 | * generation algorithm is different. Also, if @ql@ is zero, |
63 | * this algorithm forces %$k = 2$%, and chooses %$g = 4$%. Make |
64 | * sure you have something interesting to do if you choose this |
65 | * option. |
66 | */ |
67 | |
68 | int dh_gen(dh_param *dp, unsigned ql, unsigned pl, unsigned steps, grand *r, |
69 | pgen_proc *event, void *ectx) |
70 | { |
71 | /* --- If @ql@ is zero, do the time consuming safe-prime thing --- */ |
72 | |
73 | if (!ql) { |
74 | pgen_safetestctx c; |
75 | |
76 | mp *m = mprand(MP_NEW, pl, r, 3); |
77 | dp->p = pgen("p", MP_NEW, m, event, ectx, steps, pgen_safestep, &c.c, |
78 | rabin_iters(pl), pgen_safetest, &c); |
79 | mp_drop(m); |
80 | if (!dp->p) |
81 | return (PGEN_ABORT); |
82 | dp->q = mp_lsr(MP_NEW, dp->p, 1); |
83 | dp->g = MP_FOUR; |
84 | return (PGEN_DONE); |
85 | } |
86 | |
87 | /* --- Otherwise the job is much simpler --- * |
88 | * |
89 | * But doesn't look it... |
90 | */ |
91 | |
92 | else { |
93 | pgen_filterctx c; |
94 | pgen_jumpctx j; |
95 | rabin rb; |
96 | prim_ctx p; |
97 | int i; |
98 | mp *m = MP_NEW; |
99 | mp *x, *y; |
100 | |
101 | /* --- Generate @q@ first --- */ |
102 | |
103 | c.step = 2; |
104 | m = mprand(MP_NEW, ql, r, 1); |
105 | dp->q = pgen("q", MP_NEW, m, event, ectx, steps, pgen_filter, &c, |
106 | rabin_iters(ql), pgen_test, &rb); |
107 | if (!dp->q) |
108 | goto fail_q; |
109 | |
110 | /* --- Now pick a suitable @p@ --- */ |
111 | |
112 | m = mp_lsl(m, dp->q, 1); |
113 | x = mprand(MP_NEW, pl, r, 0); |
114 | y = MP_NEW; mp_div(0, &y, x, m); |
115 | x = mp_sub(x, x, y); |
116 | x = mp_add(x, x, MP_ONE); |
117 | mp_drop(y); |
118 | pfilt_create(&c.f, m); |
119 | j.j = &c.f; |
120 | dp->p = pgen("p", MP_NEW, x, event, ectx, steps, pgen_jump, &j, |
121 | rabin_iters(pl), pgen_test, &rb); |
122 | pfilt_destroy(&c.f); |
123 | mp_drop(x); |
124 | if (!dp->p) |
125 | goto fail_p; |
126 | |
127 | /* --- And finally a suitable @g@ --- */ |
128 | |
129 | mpmont_create(&p.mm, dp->p); |
130 | mp_div(&m, 0, dp->p, dp->q); |
131 | i = 0; |
f621db36 |
132 | p.exp = m; |
052b36d0 |
133 | p.n = 0; |
134 | dp->g = pgen("g", MP_NEW, MP_NEW, event, ectx, 0, prim_step, &i, |
135 | 1, prim_test, &p); |
136 | mpmont_destroy(&p.mm); |
137 | if (!dp->g) |
138 | goto fail_g; |
139 | mp_drop(m); |
140 | return (PGEN_DONE); |
141 | |
142 | /* --- Tidy up --- */ |
143 | |
144 | fail_g: |
145 | mp_drop(dp->q); |
146 | fail_q: |
147 | mp_drop(dp->p); |
148 | fail_p: |
149 | mp_drop(m); |
150 | return (PGEN_ABORT); |
151 | } |
152 | } |
153 | |
154 | /*----- That's all, folks -------------------------------------------------*/ |