| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Diffie-Hellman and related public-key systems |
| 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 | |
| 30 | #ifndef CATACOMB_DH_H |
| 31 | #define CATACOMB_DH_H |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | /*----- Header files ------------------------------------------------------*/ |
| 38 | |
| 39 | #ifndef CATACOMB_GROUP_H |
| 40 | # include "group.h" |
| 41 | #endif |
| 42 | |
| 43 | #ifndef CATACOMB_GRAND_H |
| 44 | # include "grand.h" |
| 45 | #endif |
| 46 | |
| 47 | #ifndef CATACOMB_KEY_H |
| 48 | # include "key.h" |
| 49 | #endif |
| 50 | |
| 51 | #ifndef CATACOMB_KEYCHECK_H |
| 52 | # include "keycheck.h" |
| 53 | #endif |
| 54 | |
| 55 | #ifndef CATACOMB_PGEN_H |
| 56 | # include "pgen.h" |
| 57 | #endif |
| 58 | |
| 59 | #ifndef CATACOMB_QDPARSE_H |
| 60 | # include "qdparse.h" |
| 61 | #endif |
| 62 | |
| 63 | /*----- Data structures ---------------------------------------------------*/ |
| 64 | |
| 65 | typedef gprime_param dh_param; /* Group parameters */ |
| 66 | |
| 67 | typedef struct dh_pub { |
| 68 | dh_param dp; /* Shared parameters */ |
| 69 | mp *y; /* Public key */ |
| 70 | } dh_pub; |
| 71 | |
| 72 | typedef struct dh_priv { |
| 73 | dh_param dp; /* Shared parameters */ |
| 74 | mp *x; /* Private key */ |
| 75 | mp *y; /* %$y \equiv g^x \pmod{p}$% */ |
| 76 | } dh_priv; |
| 77 | |
| 78 | /*----- Key fetching ------------------------------------------------------*/ |
| 79 | |
| 80 | extern const key_fetchdef dh_paramfetch[]; |
| 81 | #define DH_PARAMFETCHSZ 5 |
| 82 | |
| 83 | extern const key_fetchdef dh_pubfetch[]; |
| 84 | #define DH_PUBFETCHSZ 6 |
| 85 | |
| 86 | extern const key_fetchdef dh_privfetch[]; |
| 87 | #define DH_PRIVFETCHSZ 9 |
| 88 | |
| 89 | /* --- @dh_paramfree@, @dh_pubfree@, @dh_privfree@ --- * |
| 90 | * |
| 91 | * Arguments: @dh_param *dp@, @dh_pub *dp@, @dh_priv *dp@ = pointer to |
| 92 | * key block to free |
| 93 | * |
| 94 | * Returns: --- |
| 95 | * |
| 96 | * Use: Frees a Diffie-Hellman key block. |
| 97 | */ |
| 98 | |
| 99 | extern void dh_paramfree(dh_param */*dp*/); |
| 100 | extern void dh_pubfree(dh_pub */*dp*/); |
| 101 | extern void dh_privfree(dh_priv */*dp*/); |
| 102 | |
| 103 | /*----- Functions provided ------------------------------------------------*/ |
| 104 | |
| 105 | /* --- @dh_gen@ --- * |
| 106 | * |
| 107 | * Arguments: @dh_param *dp@ = pointer to output parameter block |
| 108 | * @unsigned ql@ = length of %$q$% in bits, or zero |
| 109 | * @unsigned pl@ = length of %$p$% in bits |
| 110 | * @unsigned steps@ = number of steps to go |
| 111 | * @grand *r@ = random number source |
| 112 | * @pgen_proc *event@ = event handler function |
| 113 | * @void *ectx@ = argument for the event handler |
| 114 | * |
| 115 | * Returns: @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't. |
| 116 | * |
| 117 | * Use: Generates Diffie-Hellman parameters. |
| 118 | * |
| 119 | * The parameters are a prime %$q$%, relatively small, and a |
| 120 | * large prime %$p = kq + 1$% for some %$k$%, together with a |
| 121 | * generator %$g$% of the cyclic subgroup of order %$q$%. These |
| 122 | * are actually the same as the DSA parameter set, but the |
| 123 | * generation algorithm is different. Also, if @ql@ is zero, |
| 124 | * this algorithm forces %$k = 2$%, and chooses %$g = 4$%. Make |
| 125 | * sure you have something interesting to do if you choose this |
| 126 | * option. |
| 127 | */ |
| 128 | |
| 129 | extern int dh_gen(dh_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/, |
| 130 | unsigned /*steps*/, grand */*r*/, pgen_proc */*event*/, |
| 131 | void */*ectx*/); |
| 132 | |
| 133 | /* --- @dh_limlee@ --- * |
| 134 | * |
| 135 | * Arguments: @dh_param *dp@ = pointer to output parameter block |
| 136 | * @unsigned ql@ = length of smallest factor of %$(p - 1)/2$% |
| 137 | * @unsigned pl@ = length of %$p$% in bits |
| 138 | * @unsigned flags@ = other generation flags |
| 139 | * @unsigned steps@ = number of steps to go |
| 140 | * @grand *r@ = random number source |
| 141 | * @pgen_proc *oev@ = outer event handler function |
| 142 | * @void *oec@ = argument for the outer event handler |
| 143 | * @pgen_proc *iev@ = inner event handler function |
| 144 | * @void *iec@ = argument for the inner event handler |
| 145 | * @size_t *nf@, @mp ***f@ = output array for factors |
| 146 | * |
| 147 | * Returns: @PGEN_DONE@ if it worked, @PGEN_ABORT@ if it didn't. |
| 148 | * |
| 149 | * Use: Generates Diffie-Hellman parameters based on a Lim-Lee prime. |
| 150 | * |
| 151 | * The modulus is a large prime %$p = 2 \prod q_i + 1$%, @pl@ |
| 152 | * bits long, where the %$q_i$% are smaller primes each at least |
| 153 | * @ql@ bits long. It is safe to set @nf@ and @f@ to zero if |
| 154 | * you're not interested in the factor values. |
| 155 | * |
| 156 | * The returned %$g$% generates a subgroup of order %$q_0$% (the |
| 157 | * first factor, returned as @f[0]@), if the flag @DH_SUBGROUP@ |
| 158 | * is set on entry; otherwise %$g$% will have order |
| 159 | * %$(p - 1)/2$%. |
| 160 | */ |
| 161 | |
| 162 | #define DH_SUBGROUP 1u |
| 163 | |
| 164 | extern int dh_limlee(dh_param */*dp*/, unsigned /*ql*/, unsigned /*pl*/, |
| 165 | unsigned /*flags*/, unsigned /*steps*/, grand */*r*/, |
| 166 | pgen_proc */*oev*/, void */*oec*/, pgen_proc */*iev*/, |
| 167 | void */*iec*/, size_t */*nf*/, mp ***/*f*/); |
| 168 | |
| 169 | /* --- @dh_checkparam@ --- * |
| 170 | * |
| 171 | * Arguments: @keycheck *kc@ = keycheck state |
| 172 | * @const dh_param *dp@ = pointer to the parameter set |
| 173 | * @mp **v@ = optional vector of factors |
| 174 | * @size_t n@ = size of vector |
| 175 | * |
| 176 | * Returns: Zero if all OK, or return status from function. |
| 177 | * |
| 178 | * Use: Checks a set of Diffie-Hellman parameters for consistency and |
| 179 | * security. |
| 180 | */ |
| 181 | |
| 182 | extern int dh_checkparam(keycheck */*kc*/, const dh_param */*dp*/, |
| 183 | mp **/*v*/, size_t /*n*/); |
| 184 | |
| 185 | /* ---- @dh_infofromdata@ --- * |
| 186 | * |
| 187 | * Arguments: @dh_param *dp@ = parameters to fill in |
| 188 | * @pdata *pd@ = packed data structure |
| 189 | * |
| 190 | * Returns: --- |
| 191 | * |
| 192 | * Use: Fills in a parameters structure from a packed data block. |
| 193 | */ |
| 194 | |
| 195 | struct pdata; |
| 196 | extern void dh_infofromdata(dh_param */*dp*/, struct pdata */*pd*/); |
| 197 | |
| 198 | /* --- @dh_parse@, @dhbin_parse@ --- * |
| 199 | * |
| 200 | * Arguments: @qd_parse *qd@ = parser context |
| 201 | * @dh_param *dp@ = parameters to fill in |
| 202 | * |
| 203 | * Returns: Zero if OK, nonzero on error. |
| 204 | * |
| 205 | * Use: Parses a prime/binary group string. This is either one of |
| 206 | * the standard group strings, or a %$p$%, %$q$%, %$g$% triple |
| 207 | * separated by commas. |
| 208 | */ |
| 209 | |
| 210 | extern int dh_parse(qd_parse */*qd*/, dh_param */*dp*/); |
| 211 | extern int dhbin_parse(qd_parse */*qd*/, gbin_param */*gb*/); |
| 212 | |
| 213 | /*----- That's all, folks -------------------------------------------------*/ |
| 214 | |
| 215 | #ifdef __cplusplus |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | #endif |