1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.] */
57 #include <openssl/dh.h>
61 #include <openssl/bn.h>
62 #include <openssl/buf.h>
63 #include <openssl/err.h>
64 #include <openssl/ex_data.h>
65 #include <openssl/mem.h>
66 #include <openssl/thread.h>
68 #include "../internal.h"
71 #define OPENSSL_DH_MAX_MODULUS_BITS 10000
73 static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
76 DH *dh = OPENSSL_malloc(sizeof(DH));
78 OPENSSL_PUT_ERROR(DH, ERR_R_MALLOC_FAILURE);
82 OPENSSL_memset(dh, 0, sizeof(DH));
84 CRYPTO_MUTEX_init(&dh->method_mont_p_lock);
87 CRYPTO_new_ex_data(&dh->ex_data);
92 void DH_free(DH *dh) {
97 if (!CRYPTO_refcount_dec_and_test_zero(&dh->references)) {
101 CRYPTO_free_ex_data(&g_ex_data_class, dh, &dh->ex_data);
103 BN_MONT_CTX_free(dh->method_mont_p);
104 BN_clear_free(dh->p);
105 BN_clear_free(dh->g);
106 BN_clear_free(dh->q);
107 BN_clear_free(dh->j);
108 OPENSSL_free(dh->seed);
109 BN_clear_free(dh->counter);
110 BN_clear_free(dh->pub_key);
111 BN_clear_free(dh->priv_key);
112 CRYPTO_MUTEX_cleanup(&dh->method_mont_p_lock);
117 void DH_get0_key(const DH *dh, const BIGNUM **out_pub_key,
118 const BIGNUM **out_priv_key) {
119 if (out_pub_key != NULL) {
120 *out_pub_key = dh->pub_key;
122 if (out_priv_key != NULL) {
123 *out_priv_key = dh->priv_key;
127 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) {
128 if (pub_key != NULL) {
129 BN_free(dh->pub_key);
130 dh->pub_key = pub_key;
133 if (priv_key != NULL) {
134 BN_free(dh->priv_key);
135 dh->priv_key = priv_key;
141 void DH_get0_pqg(const DH *dh, const BIGNUM **out_p, const BIGNUM **out_q,
142 const BIGNUM **out_g) {
154 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
155 if ((dh->p == NULL && p == NULL) ||
156 (dh->g == NULL && g == NULL)) {
178 int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb) {
179 // We generate DH parameters as follows
180 // find a prime q which is prime_bits/2 bits long.
181 // p=(2*q)+1 or (p-1)/2 = q
182 // For this case, g is a generator if
183 // g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
184 // Since the factors of p-1 are q and 2, we just need to check
185 // g^2 mod p != 1 and g^q mod p != 1.
187 // Having said all that,
188 // there is another special case method for the generators 2, 3 and 5.
189 // for 2, p mod 24 == 11
190 // for 3, p mod 12 == 5 <<<<< does not work for safe primes.
191 // for 5, p mod 10 == 3 or 7
193 // Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
194 // special generators and for answering some of my questions.
196 // I've implemented the second simple method :-).
197 // Since DH should be using a safe prime (both p and q are prime),
198 // this generator function can take a very very long time to run.
200 // Actually there is no reason to insist that 'generator' be a generator.
201 // It's just as OK (and in some sense better) to use a generator of the
213 t1 = BN_CTX_get(ctx);
214 t2 = BN_CTX_get(ctx);
215 if (t1 == NULL || t2 == NULL) {
219 // Make sure |dh| has the necessary elements
233 if (generator <= 1) {
234 OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
237 if (generator == DH_GENERATOR_2) {
238 if (!BN_set_word(t1, 24)) {
241 if (!BN_set_word(t2, 11)) {
245 } else if (generator == DH_GENERATOR_5) {
246 if (!BN_set_word(t1, 10)) {
249 if (!BN_set_word(t2, 3)) {
252 // BN_set_word(t3,7); just have to miss
253 // out on these ones :-(
256 // in the general case, don't worry if 'generator' is a
257 // generator or not: since we are using safe primes,
258 // it will generate either an order-q or an order-2q group,
260 if (!BN_set_word(t1, 2)) {
263 if (!BN_set_word(t2, 1)) {
269 if (!BN_generate_prime_ex(dh->p, prime_bits, 1, t1, t2, cb)) {
272 if (!BN_GENCB_call(cb, 3, 0)) {
275 if (!BN_set_word(dh->g, g)) {
282 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
292 int DH_generate_key(DH *dh) {
294 int generate_new_key = 0;
296 BIGNUM *pub_key = NULL, *priv_key = NULL;
298 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
299 OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
308 if (dh->priv_key == NULL) {
310 if (priv_key == NULL) {
313 generate_new_key = 1;
315 priv_key = dh->priv_key;
318 if (dh->pub_key == NULL) {
320 if (pub_key == NULL) {
324 pub_key = dh->pub_key;
327 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
332 if (generate_new_key) {
334 if (!BN_rand_range_ex(priv_key, 2, dh->q)) {
338 // secret exponent length
339 unsigned priv_bits = dh->priv_length;
340 if (priv_bits == 0) {
341 const unsigned p_bits = BN_num_bits(dh->p);
346 priv_bits = p_bits - 1;
349 if (!BN_rand(priv_key, priv_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) {
355 if (!BN_mod_exp_mont_consttime(pub_key, dh->g, priv_key, dh->p, ctx,
356 dh->method_mont_p)) {
360 dh->pub_key = pub_key;
361 dh->priv_key = priv_key;
366 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
369 if (dh->pub_key == NULL) {
372 if (dh->priv_key == NULL) {
379 int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
385 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
386 OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
395 shared_key = BN_CTX_get(ctx);
396 if (shared_key == NULL) {
400 if (dh->priv_key == NULL) {
401 OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE);
405 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
410 if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
411 OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
415 if (!BN_mod_exp_mont_consttime(shared_key, peers_key, dh->priv_key, dh->p,
416 ctx, dh->method_mont_p)) {
417 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
421 ret = BN_bn2bin(shared_key, out);
432 int DH_size(const DH *dh) { return BN_num_bytes(dh->p); }
434 unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); }
436 int DH_up_ref(DH *dh) {
437 CRYPTO_refcount_inc(&dh->references);
441 static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
456 static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
460 if (!int_dh_bn_cpy(&to->p, from->p) ||
461 !int_dh_bn_cpy(&to->g, from->g)) {
469 if (!int_dh_bn_cpy(&to->q, from->q) ||
470 !int_dh_bn_cpy(&to->j, from->j)) {
474 OPENSSL_free(to->seed);
479 to->seed = BUF_memdup(from->seed, from->seedlen);
483 to->seedlen = from->seedlen;
489 DH *DHparams_dup(const DH *dh) {
495 if (!int_dh_param_copy(ret, dh, -1)) {
503 int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
504 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
506 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
513 int DH_set_ex_data(DH *d, int idx, void *arg) {
514 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
517 void *DH_get_ex_data(DH *d, int idx) {
518 return CRYPTO_get_ex_data(&d->ex_data, idx);