1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay@cryptsoft.com). 6 * The implementation was written so as to conform with Netscapes SSL. 7 * 8 * This library is free for commercial and non-commercial use as long as 9 * the following conditions are aheared to. The following conditions 10 * apply to all code found in this distribution, be it the RC4, RSA, 11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * included with this distribution is covered by the same copyright terms 13 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * 15 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * the code are not to be removed. 17 * If this package is used in a product, Eric Young should be given attribution 18 * as the author of the parts of the library used. 19 * This can be in the form of a textual message at program startup or 20 * in documentation (online or textual) provided with the package. 21 * 22 * Redistribution and use in source and binary forms, with or without 23 * modification, are permitted provided that the following conditions 24 * are met: 25 * 1. Redistributions of source code must retain the copyright 26 * notice, this list of conditions and the following disclaimer. 27 * 2. Redistributions in binary form must reproduce the above copyright 28 * notice, this list of conditions and the following disclaimer in the 29 * documentation and/or other materials provided with the distribution. 30 * 3. All advertising materials mentioning features or use of this software 31 * must display the following acknowledgement: 32 * "This product includes cryptographic software written by 33 * Eric Young (eay@cryptsoft.com)" 34 * The word 'cryptographic' can be left out if the rouines from the library 35 * being used are not cryptographic related :-). 36 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * the apps directory (application code) you must include an acknowledgement: 38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 * 52 * The licence and distribution terms for any publically available version or 53 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * copied and put under another distribution licence 55 * [including the GNU Public Licence.] */ 56 57#include <openssl/dh.h> 58 59#include <openssl/bn.h> 60#include <openssl/err.h> 61 62#include "internal.h" 63 64#define OPENSSL_DH_MAX_MODULUS_BITS 10000 65 66static int generate_parameters(DH *ret, int prime_bits, int generator, BN_GENCB *cb) { 67 /* We generate DH parameters as follows 68 * find a prime q which is prime_bits/2 bits long. 69 * p=(2*q)+1 or (p-1)/2 = q 70 * For this case, g is a generator if 71 * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1. 72 * Since the factors of p-1 are q and 2, we just need to check 73 * g^2 mod p != 1 and g^q mod p != 1. 74 * 75 * Having said all that, 76 * there is another special case method for the generators 2, 3 and 5. 77 * for 2, p mod 24 == 11 78 * for 3, p mod 12 == 5 <<<<< does not work for safe primes. 79 * for 5, p mod 10 == 3 or 7 80 * 81 * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the 82 * special generators and for answering some of my questions. 83 * 84 * I've implemented the second simple method :-). 85 * Since DH should be using a safe prime (both p and q are prime), 86 * this generator function can take a very very long time to run. 87 */ 88 89 /* Actually there is no reason to insist that 'generator' be a generator. 90 * It's just as OK (and in some sense better) to use a generator of the 91 * order-q subgroup. 92 */ 93 94 BIGNUM *t1, *t2; 95 int g, ok = 0; 96 BN_CTX *ctx = NULL; 97 98 ctx = BN_CTX_new(); 99 if (ctx == NULL) { 100 goto err; 101 } 102 BN_CTX_start(ctx); 103 t1 = BN_CTX_get(ctx); 104 t2 = BN_CTX_get(ctx); 105 if (t1 == NULL || t2 == NULL) { 106 goto err; 107 } 108 109 /* Make sure 'ret' has the necessary elements */ 110 if (!ret->p && ((ret->p = BN_new()) == NULL)) { 111 goto err; 112 } 113 if (!ret->g && ((ret->g = BN_new()) == NULL)) { 114 goto err; 115 } 116 117 if (generator <= 1) { 118 OPENSSL_PUT_ERROR(DH, generate_parameters, DH_R_BAD_GENERATOR); 119 goto err; 120 } 121 if (generator == DH_GENERATOR_2) { 122 if (!BN_set_word(t1, 24)) { 123 goto err; 124 } 125 if (!BN_set_word(t2, 11)) { 126 goto err; 127 } 128 g = 2; 129 } else if (generator == DH_GENERATOR_5) { 130 if (!BN_set_word(t1, 10)) { 131 goto err; 132 } 133 if (!BN_set_word(t2, 3)) { 134 goto err; 135 } 136 /* BN_set_word(t3,7); just have to miss 137 * out on these ones :-( */ 138 g = 5; 139 } else { 140 /* in the general case, don't worry if 'generator' is a 141 * generator or not: since we are using safe primes, 142 * it will generate either an order-q or an order-2q group, 143 * which both is OK */ 144 if (!BN_set_word(t1, 2)) { 145 goto err; 146 } 147 if (!BN_set_word(t2, 1)) { 148 goto err; 149 } 150 g = generator; 151 } 152 153 if (!BN_generate_prime_ex(ret->p, prime_bits, 1, t1, t2, cb)) { 154 goto err; 155 } 156 if (!BN_GENCB_call(cb, 3, 0)) { 157 goto err; 158 } 159 if (!BN_set_word(ret->g, g)) { 160 goto err; 161 } 162 ok = 1; 163 164err: 165 if (!ok) { 166 OPENSSL_PUT_ERROR(DH, generate_parameters, ERR_R_BN_LIB); 167 } 168 169 if (ctx != NULL) { 170 BN_CTX_end(ctx); 171 BN_CTX_free(ctx); 172 } 173 return ok; 174} 175 176static int generate_key(DH *dh) { 177 int ok = 0; 178 int generate_new_key = 0; 179 unsigned l; 180 BN_CTX *ctx; 181 BN_MONT_CTX *mont = NULL; 182 BIGNUM *pub_key = NULL, *priv_key = NULL; 183 BIGNUM local_priv; 184 185 ctx = BN_CTX_new(); 186 if (ctx == NULL) { 187 goto err; 188 } 189 190 if (dh->priv_key == NULL) { 191 priv_key = BN_new(); 192 if (priv_key == NULL) { 193 goto err; 194 } 195 generate_new_key = 1; 196 } else { 197 priv_key = dh->priv_key; 198 } 199 200 if (dh->pub_key == NULL) { 201 pub_key = BN_new(); 202 if (pub_key == NULL) { 203 goto err; 204 } 205 } else { 206 pub_key = dh->pub_key; 207 } 208 209 mont = 210 BN_MONT_CTX_set_locked(&dh->method_mont_p, CRYPTO_LOCK_DH, dh->p, ctx); 211 if (!mont) { 212 goto err; 213 } 214 215 if (generate_new_key) { 216 if (dh->q) { 217 do { 218 if (!BN_rand_range(priv_key, dh->q)) { 219 goto err; 220 } 221 } while (BN_is_zero(priv_key) || BN_is_one(priv_key)); 222 } else { 223 /* secret exponent length */ 224 l = dh->priv_length ? dh->priv_length : BN_num_bits(dh->p) - 1; 225 if (!BN_rand(priv_key, l, 0, 0)) { 226 goto err; 227 } 228 } 229 } 230 231 BN_with_flags(&local_priv, priv_key, BN_FLG_CONSTTIME); 232 if (!BN_mod_exp_mont(pub_key, dh->g, &local_priv, dh->p, ctx, mont)) { 233 goto err; 234 } 235 236 dh->pub_key = pub_key; 237 dh->priv_key = priv_key; 238 ok = 1; 239 240err: 241 if (ok != 1) { 242 OPENSSL_PUT_ERROR(DH, generate_key, ERR_R_BN_LIB); 243 } 244 245 if (pub_key != NULL && dh->pub_key == NULL) { 246 BN_free(pub_key); 247 } 248 if (priv_key != NULL && dh->priv_key == NULL) { 249 BN_free(priv_key); 250 } 251 BN_CTX_free(ctx); 252 return ok; 253} 254 255static int compute_key(DH *dh, unsigned char *out, const BIGNUM *pub_key) { 256 BN_CTX *ctx = NULL; 257 BN_MONT_CTX *mont = NULL; 258 BIGNUM *shared_key; 259 int ret = -1; 260 int check_result; 261 BIGNUM local_priv; 262 263 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) { 264 OPENSSL_PUT_ERROR(DH, compute_key, DH_R_MODULUS_TOO_LARGE); 265 goto err; 266 } 267 268 ctx = BN_CTX_new(); 269 if (ctx == NULL) { 270 goto err; 271 } 272 BN_CTX_start(ctx); 273 shared_key = BN_CTX_get(ctx); 274 if (shared_key == NULL) { 275 goto err; 276 } 277 278 if (dh->priv_key == NULL) { 279 OPENSSL_PUT_ERROR(DH, compute_key, DH_R_NO_PRIVATE_VALUE); 280 goto err; 281 } 282 283 mont = 284 BN_MONT_CTX_set_locked(&dh->method_mont_p, CRYPTO_LOCK_DH, dh->p, ctx); 285 if (!mont) { 286 goto err; 287 } 288 289 if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) { 290 OPENSSL_PUT_ERROR(DH, compute_key, DH_R_INVALID_PUBKEY); 291 goto err; 292 } 293 294 BN_with_flags(&local_priv, dh->priv_key, BN_FLG_CONSTTIME); 295 if (!BN_mod_exp_mont(shared_key, pub_key, &local_priv, dh->p, ctx, 296 mont)) { 297 OPENSSL_PUT_ERROR(DH, compute_key, ERR_R_BN_LIB); 298 goto err; 299 } 300 301 ret = BN_bn2bin(shared_key, out); 302 303err: 304 if (ctx != NULL) { 305 BN_CTX_end(ctx); 306 BN_CTX_free(ctx); 307 } 308 309 return ret; 310} 311 312const struct dh_method DH_default_method = { 313 { 314 0 /* references */, 315 1 /* is_static */, 316 }, 317 NULL /* app_data */, 318 NULL /* init */, 319 NULL /* finish */, 320 generate_parameters, 321 generate_key, 322 compute_key, 323}; 324