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