3 * $Id: dh-check.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
5 * Checks Diffie-Hellman group parameters
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/dstr.h>
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @dh_checkparam@ --- *
44 * Arguments: @keycheck *kc@ = keycheck state
45 * @const dh_param *dp@ = pointer to the parameter set
46 * @mp **v@ = optional vector of factors
47 * @size_t n@ = size of vector
49 * Returns: Zero if all OK, or return status from function.
51 * Use: Checks a set of Diffie-Hellman parameters for consistency and
55 int dh_checkparam(keycheck *kc, const dh_param *dp, mp **v, size_t n)
65 /* --- Check that the numbers which are supposed to be prime are --- */
67 if ((!v && keycheck_prime(kc, KCSEV_WARN, dp->q, "q")) ||
68 keycheck_prime(kc, KCSEV_ERR, dp->p, "p"))
71 /* --- Ensure that %$q$% is a sensible choice of number --- */
73 pm1 = mp_sub(pm1, dp->p, MP_ONE);
74 mp_div(0, &q, pm1, dp->q);
75 if (!mp_eq(q, MP_ZERO) &&
76 keycheck_report(kc, KCSEV_ERR, "q not a factor of p - 1"))
79 /* --- Check that %$g$% is actually right --- *
81 * This isn't perfect. If %$q$% is composite and we don't have the factors
82 * of %$p - 1$% then the order of %$g$% may be some factor of %$q$% which
83 * we can't find. (If we do have the factors, we check them all lower
84 * down.) We do strip out powers of two from %$q$% before testing, though.
87 if ((mp_eq(dp->g, MP_ONE) || mp_eq(dp->g, pm1)) &&
88 keycheck_report(kc, KCSEV_ERR, "g is degenerate (+/-1 mod p)"))
90 q = mp_odd(q, dp->q, &i);
91 mpmont_create(&mm, dp->p);
92 x = mpmont_mul(&mm, MP_NEW, dp->g, mm.r2);
93 q = mpmont_expr(&mm, q, x, q);
96 if (mp_eq(q, mm.r) != !i) {
97 if (keycheck_report(kc, KCSEV_ERR, "order of g != q")) {
105 q = mpmont_reduce(&mm, q, q);
109 /* --- Check Lim-Lee primes more carefully --- *
111 * In this case, we really can be sure whether the order of %$g$% is
112 * actually %$q$% as advertised. Also ensure that the individual primes
113 * are really prime, and that their product is correct.
123 for (i = 0; i < n; i++) {
125 dstr_putf(&d, "factor f_%lu of p", (unsigned long)i);
126 if ((rc = keycheck_prime(kc, KCSEV_ERR, v[i], d.buf)) != 0)
128 mp_div(&q, &r, dp->q, v[i]);
129 if (mp_eq(r, MP_ZERO) && !mp_eq(q, MP_ONE)) {
130 q = mpmont_exp(&mm, q, dp->g, q);
131 if (mp_eq(q, MP_ONE) &&
132 (rc = keycheck_report(kc, KCSEV_ERR,
133 "order of g is proper divisor of q")) != 0)
136 mpmul_add(&mu, v[i]);
146 if (!mp_eq(q, pm1) &&
147 keycheck_report(kc, KCSEV_ERR, "product of f_i != (p - 1)/2"))
151 /* --- Finally, check the key sizes --- */
153 if ((mp_bits(dp->p) < 1024 &&
154 keycheck_report(kc, KCSEV_WARN,
155 "p too small to resist index calculus attacks")) ||
156 (mp_bits(dp->q) < 160 &&
157 keycheck_report(kc, KCSEV_WARN,
158 "q too small to resist collision-finding attacks")))
172 /*----- That's all, folks -------------------------------------------------*/