Rearrange the file tree.
[u/mdw/catacomb] / pub / dh-check.c
1 /* -*-c-*-
2 *
3 * Checks Diffie-Hellman group parameters
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/dstr.h>
31
32 #include "dh.h"
33 #include "keycheck.h"
34 #include "mp.h"
35 #include "mpmont.h"
36 #include "mpmul.h"
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 /* --- @dh_checkparam@ --- *
41 *
42 * Arguments: @keycheck *kc@ = keycheck state
43 * @const dh_param *dp@ = pointer to the parameter set
44 * @mp **v@ = optional vector of factors
45 * @size_t n@ = size of vector
46 *
47 * Returns: Zero if all OK, or return status from function.
48 *
49 * Use: Checks a set of Diffie-Hellman parameters for consistency and
50 * security.
51 */
52
53 int dh_checkparam(keycheck *kc, const dh_param *dp, mp **v, size_t n)
54 {
55 int rc = 0;
56 mpmont mm;
57 mp *pm1 = MP_NEW;
58 mp *q = MP_NEW;
59 mp *x;
60 mpmul mu;
61 size_t i;
62
63 /* --- Check that the numbers which are supposed to be prime are --- */
64
65 if ((!v && keycheck_prime(kc, KCSEV_WARN, dp->q, "q")) ||
66 keycheck_prime(kc, KCSEV_ERR, dp->p, "p"))
67 goto fail;
68
69 /* --- Ensure that %$q$% is a sensible choice of number --- */
70
71 pm1 = mp_sub(pm1, dp->p, MP_ONE);
72 mp_div(0, &q, pm1, dp->q);
73 if (!mp_eq(q, MP_ZERO) &&
74 keycheck_report(kc, KCSEV_ERR, "q not a factor of p - 1"))
75 goto fail;
76
77 /* --- Check that %$g$% is actually right --- *
78 *
79 * This isn't perfect. If %$q$% is composite and we don't have the factors
80 * of %$p - 1$% then the order of %$g$% may be some factor of %$q$% which
81 * we can't find. (If we do have the factors, we check them all lower
82 * down.) We do strip out powers of two from %$q$% before testing, though.
83 */
84
85 if ((mp_eq(dp->g, MP_ONE) || mp_eq(dp->g, pm1)) &&
86 keycheck_report(kc, KCSEV_ERR, "g is degenerate (+/-1 mod p)"))
87 goto fail;
88 q = mp_odd(q, dp->q, &i);
89 mpmont_create(&mm, dp->p);
90 x = mpmont_mul(&mm, MP_NEW, dp->g, mm.r2);
91 q = mpmont_expr(&mm, q, x, q);
92 mp_drop(x);
93 do {
94 if (mp_eq(q, mm.r) != !i) {
95 if (keycheck_report(kc, KCSEV_ERR, "order of g != q")) {
96 mpmont_destroy(&mm);
97 goto fail;
98 }
99 break;
100 }
101 if (i) {
102 q = mp_sqr(q, q);
103 q = mpmont_reduce(&mm, q, q);
104 }
105 } while (i--);
106
107 /* --- Check Lim-Lee primes more carefully --- *
108 *
109 * In this case, we really can be sure whether the order of %$g$% is
110 * actually %$q$% as advertised. Also ensure that the individual primes
111 * are really prime, and that their product is correct.
112 */
113
114 if (!v)
115 mpmont_destroy(&mm);
116 else {
117 dstr d = DSTR_INIT;
118 mp *r = MP_NEW;
119
120 mpmul_init(&mu);
121 for (i = 0; i < n; i++) {
122 DRESET(&d);
123 dstr_putf(&d, "factor f_%lu of p", (unsigned long)i);
124 if ((rc = keycheck_prime(kc, KCSEV_ERR, v[i], d.buf)) != 0)
125 break;
126 mp_div(&q, &r, dp->q, v[i]);
127 if (mp_eq(r, MP_ZERO) && !mp_eq(q, MP_ONE)) {
128 q = mpmont_exp(&mm, q, dp->g, q);
129 if (mp_eq(q, MP_ONE) &&
130 (rc = keycheck_report(kc, KCSEV_ERR,
131 "order of g is proper divisor of q")) != 0)
132 break;
133 }
134 mpmul_add(&mu, v[i]);
135 }
136 mp_drop(q);
137 mp_drop(r);
138 q = mpmul_done(&mu);
139 mpmont_destroy(&mm);
140 dstr_destroy(&d);
141 if (rc)
142 goto fail;
143 q = mp_lsl(q, q, 1);
144 if (!mp_eq(q, pm1) &&
145 keycheck_report(kc, KCSEV_ERR, "product of f_i != (p - 1)/2"))
146 goto fail;
147 }
148
149 /* --- Finally, check the key sizes --- */
150
151 if ((mp_bits(dp->p) < 1024 &&
152 keycheck_report(kc, KCSEV_WARN,
153 "p too small to resist index calculus attacks")) ||
154 (mp_bits(dp->q) < 160 &&
155 keycheck_report(kc, KCSEV_WARN,
156 "q too small to resist collision-finding attacks")))
157 goto fail;
158
159 /* --- Done --- */
160
161 tidy:
162 mp_drop(q);
163 mp_drop(pm1);
164 return (rc);
165 fail:
166 rc = -1;
167 goto tidy;
168 }
169
170 /*----- That's all, folks -------------------------------------------------*/