| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: dh-check.c,v 1.1 2001/02/03 16:08:24 mdw Exp $ |
| 4 | * |
| 5 | * Checks Diffie-Hellman group parameters |
| 6 | * |
| 7 | * (c) 2001 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 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: dh-check.c,v $ |
| 33 | * Revision 1.1 2001/02/03 16:08:24 mdw |
| 34 | * Add consistency checking for public keys. |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | /*----- Header files ------------------------------------------------------*/ |
| 39 | |
| 40 | #include <mLib/dstr.h> |
| 41 | |
| 42 | #include "dh.h" |
| 43 | #include "keycheck.h" |
| 44 | #include "mp.h" |
| 45 | #include "mpmont.h" |
| 46 | #include "mpmul.h" |
| 47 | |
| 48 | /*----- Main code ---------------------------------------------------------*/ |
| 49 | |
| 50 | /* --- @dh_checkparam@ --- * |
| 51 | * |
| 52 | * Arguments: @keycheck *kc@ = keycheck state |
| 53 | * @const dh_param *dp@ = pointer to the parameter set |
| 54 | * @mp **v@ = optional vector of factors |
| 55 | * @size_t n@ = size of vector |
| 56 | * |
| 57 | * Returns: Zero if all OK, or return status from function. |
| 58 | * |
| 59 | * Use: Checks a set of Diffie-Hellman parameters for consistency and |
| 60 | * security. |
| 61 | */ |
| 62 | |
| 63 | int dh_checkparam(keycheck *kc, const dh_param *dp, mp **v, size_t n) |
| 64 | { |
| 65 | int rc = 0; |
| 66 | mpmont mm; |
| 67 | mp *pm1 = MP_NEW; |
| 68 | mp *q = MP_NEW; |
| 69 | mpmul mu; |
| 70 | size_t i; |
| 71 | |
| 72 | /* --- Check that the numbers which are supposed to be prime are --- */ |
| 73 | |
| 74 | if ((!v && keycheck_prime(kc, KCSEV_WARN, dp->q, "q")) || |
| 75 | keycheck_prime(kc, KCSEV_ERR, dp->p, "p")) |
| 76 | goto fail; |
| 77 | |
| 78 | /* --- Ensure that %$q$% is a sensible choice of number --- */ |
| 79 | |
| 80 | pm1 = mp_sub(pm1, dp->p, MP_ONE); |
| 81 | mp_div(0, &q, pm1, dp->q); |
| 82 | if (!mp_eq(q, MP_ZERO) && |
| 83 | keycheck_report(kc, KCSEV_ERR, "q not a factor of p - 1")) |
| 84 | goto fail; |
| 85 | |
| 86 | /* --- Check that %$g$% is actually right --- * |
| 87 | * |
| 88 | * This isn't perfect. If %$q$% is composite and we don't have the factors |
| 89 | * of %$p - 1$% then the order of %$g$% may be some factor of %$q$% which |
| 90 | * we can't find. (If we do have the factors, we check them all lower |
| 91 | * down.) We do strip out powers of two from %$q$% before testing, though. |
| 92 | */ |
| 93 | |
| 94 | if ((mp_eq(dp->g, MP_ONE) || mp_eq(dp->g, pm1)) && |
| 95 | keycheck_report(kc, KCSEV_ERR, "g is degenerate (+/-1 mod p)")) |
| 96 | goto fail; |
| 97 | q = mp_odd(q, dp->q, &i); |
| 98 | mpmont_create(&mm, dp->p); |
| 99 | q = mpmont_expr(&mm, q, dp->g, q); |
| 100 | do { |
| 101 | if (mp_eq(q, mm.r) != !i) { |
| 102 | if (keycheck_report(kc, KCSEV_ERR, "order of g != q")) { |
| 103 | mpmont_destroy(&mm); |
| 104 | goto fail; |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | if (i) { |
| 109 | q = mp_sqr(q, q); |
| 110 | q = mpmont_reduce(&mm, q, q); |
| 111 | } |
| 112 | } while (i--); |
| 113 | |
| 114 | /* --- Check Lim-Lee primes more carefully --- * |
| 115 | * |
| 116 | * In this case, we really can be sure whether the order of %$g$% is |
| 117 | * actually %$q$% as advertised. Also ensure that the individual primes |
| 118 | * are really prime, and that their product is correct. |
| 119 | */ |
| 120 | |
| 121 | if (!v) |
| 122 | mpmont_destroy(&mm); |
| 123 | else { |
| 124 | dstr d = DSTR_INIT; |
| 125 | mp *r = MP_NEW; |
| 126 | |
| 127 | mpmul_init(&mu); |
| 128 | for (i = 0; i < n; i++) { |
| 129 | DRESET(&d); |
| 130 | dstr_putf(&d, "factor f_%lu of p", (unsigned long)i); |
| 131 | if ((rc = keycheck_prime(kc, KCSEV_ERR, v[i], d.buf)) != 0) |
| 132 | break; |
| 133 | mp_div(&q, &r, dp->q, v[i]); |
| 134 | if (mp_eq(r, MP_ZERO) && !mp_eq(q, MP_ONE)) { |
| 135 | q = mpmont_exp(&mm, q, dp->g, q); |
| 136 | if (mp_eq(q, MP_ONE) && |
| 137 | (rc = keycheck_report(kc, KCSEV_ERR, |
| 138 | "order of g is proper divisor of q")) != 0) |
| 139 | break; |
| 140 | } |
| 141 | mpmul_add(&mu, v[i]); |
| 142 | } |
| 143 | mp_drop(q); |
| 144 | mp_drop(r); |
| 145 | q = mpmul_done(&mu); |
| 146 | mpmont_destroy(&mm); |
| 147 | dstr_destroy(&d); |
| 148 | if (rc) |
| 149 | goto fail; |
| 150 | q = mp_lsl(q, q, 1); |
| 151 | if (!mp_eq(q, pm1) && |
| 152 | keycheck_report(kc, KCSEV_ERR, "product of f_i != (p - 1)/2")) |
| 153 | goto fail; |
| 154 | } |
| 155 | |
| 156 | /* --- Finally, check the key sizes --- */ |
| 157 | |
| 158 | if ((mp_bits(dp->p) < 1024 && |
| 159 | keycheck_report(kc, KCSEV_WARN, |
| 160 | "p too small to resist index calculus attacks")) || |
| 161 | (mp_bits(dp->q) < 160 && |
| 162 | keycheck_report(kc, KCSEV_WARN, |
| 163 | "q too small to resist collision-finding attacks"))) |
| 164 | goto fail; |
| 165 | |
| 166 | /* --- Done --- */ |
| 167 | |
| 168 | tidy: |
| 169 | mp_drop(q); |
| 170 | mp_drop(pm1); |
| 171 | return (rc); |
| 172 | fail: |
| 173 | rc = -1; |
| 174 | goto tidy; |
| 175 | } |
| 176 | |
| 177 | /*----- That's all, folks -------------------------------------------------*/ |