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 <string.h>
60
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>
67
68 #include "../internal.h"
69
70
71 #define OPENSSL_DH_MAX_MODULUS_BITS 10000
72
73 static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
74
DH_new(void)75 DH *DH_new(void) {
76 DH *dh = OPENSSL_malloc(sizeof(DH));
77 if (dh == NULL) {
78 OPENSSL_PUT_ERROR(DH, ERR_R_MALLOC_FAILURE);
79 return NULL;
80 }
81
82 OPENSSL_memset(dh, 0, sizeof(DH));
83
84 CRYPTO_MUTEX_init(&dh->method_mont_p_lock);
85
86 dh->references = 1;
87 CRYPTO_new_ex_data(&dh->ex_data);
88
89 return dh;
90 }
91
DH_free(DH * dh)92 void DH_free(DH *dh) {
93 if (dh == NULL) {
94 return;
95 }
96
97 if (!CRYPTO_refcount_dec_and_test_zero(&dh->references)) {
98 return;
99 }
100
101 CRYPTO_free_ex_data(&g_ex_data_class, dh, &dh->ex_data);
102
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);
113
114 OPENSSL_free(dh);
115 }
116
DH_get0_key(const DH * dh,const BIGNUM ** out_pub_key,const BIGNUM ** out_priv_key)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;
121 }
122 if (out_priv_key != NULL) {
123 *out_priv_key = dh->priv_key;
124 }
125 }
126
DH_get0_pqg(const DH * dh,const BIGNUM ** out_p,const BIGNUM ** out_q,const BIGNUM ** out_g)127 void DH_get0_pqg(const DH *dh, const BIGNUM **out_p, const BIGNUM **out_q,
128 const BIGNUM **out_g) {
129 if (out_p != NULL) {
130 *out_p = dh->p;
131 }
132 if (out_q != NULL) {
133 *out_q = dh->q;
134 }
135 if (out_g != NULL) {
136 *out_g = dh->g;
137 }
138 }
139
DH_generate_parameters_ex(DH * dh,int prime_bits,int generator,BN_GENCB * cb)140 int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb) {
141 /* We generate DH parameters as follows
142 * find a prime q which is prime_bits/2 bits long.
143 * p=(2*q)+1 or (p-1)/2 = q
144 * For this case, g is a generator if
145 * g^((p-1)/q) mod p != 1 for values of q which are the factors of p-1.
146 * Since the factors of p-1 are q and 2, we just need to check
147 * g^2 mod p != 1 and g^q mod p != 1.
148 *
149 * Having said all that,
150 * there is another special case method for the generators 2, 3 and 5.
151 * for 2, p mod 24 == 11
152 * for 3, p mod 12 == 5 <<<<< does not work for safe primes.
153 * for 5, p mod 10 == 3 or 7
154 *
155 * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
156 * special generators and for answering some of my questions.
157 *
158 * I've implemented the second simple method :-).
159 * Since DH should be using a safe prime (both p and q are prime),
160 * this generator function can take a very very long time to run.
161 */
162
163 /* Actually there is no reason to insist that 'generator' be a generator.
164 * It's just as OK (and in some sense better) to use a generator of the
165 * order-q subgroup.
166 */
167
168 BIGNUM *t1, *t2;
169 int g, ok = 0;
170 BN_CTX *ctx = NULL;
171
172 ctx = BN_CTX_new();
173 if (ctx == NULL) {
174 goto err;
175 }
176 BN_CTX_start(ctx);
177 t1 = BN_CTX_get(ctx);
178 t2 = BN_CTX_get(ctx);
179 if (t1 == NULL || t2 == NULL) {
180 goto err;
181 }
182
183 /* Make sure |dh| has the necessary elements */
184 if (dh->p == NULL) {
185 dh->p = BN_new();
186 if (dh->p == NULL) {
187 goto err;
188 }
189 }
190 if (dh->g == NULL) {
191 dh->g = BN_new();
192 if (dh->g == NULL) {
193 goto err;
194 }
195 }
196
197 if (generator <= 1) {
198 OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
199 goto err;
200 }
201 if (generator == DH_GENERATOR_2) {
202 if (!BN_set_word(t1, 24)) {
203 goto err;
204 }
205 if (!BN_set_word(t2, 11)) {
206 goto err;
207 }
208 g = 2;
209 } else if (generator == DH_GENERATOR_5) {
210 if (!BN_set_word(t1, 10)) {
211 goto err;
212 }
213 if (!BN_set_word(t2, 3)) {
214 goto err;
215 }
216 /* BN_set_word(t3,7); just have to miss
217 * out on these ones :-( */
218 g = 5;
219 } else {
220 /* in the general case, don't worry if 'generator' is a
221 * generator or not: since we are using safe primes,
222 * it will generate either an order-q or an order-2q group,
223 * which both is OK */
224 if (!BN_set_word(t1, 2)) {
225 goto err;
226 }
227 if (!BN_set_word(t2, 1)) {
228 goto err;
229 }
230 g = generator;
231 }
232
233 if (!BN_generate_prime_ex(dh->p, prime_bits, 1, t1, t2, cb)) {
234 goto err;
235 }
236 if (!BN_GENCB_call(cb, 3, 0)) {
237 goto err;
238 }
239 if (!BN_set_word(dh->g, g)) {
240 goto err;
241 }
242 ok = 1;
243
244 err:
245 if (!ok) {
246 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
247 }
248
249 if (ctx != NULL) {
250 BN_CTX_end(ctx);
251 BN_CTX_free(ctx);
252 }
253 return ok;
254 }
255
DH_generate_key(DH * dh)256 int DH_generate_key(DH *dh) {
257 int ok = 0;
258 int generate_new_key = 0;
259 BN_CTX *ctx = NULL;
260 BIGNUM *pub_key = NULL, *priv_key = NULL;
261
262 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
263 OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
264 goto err;
265 }
266
267 ctx = BN_CTX_new();
268 if (ctx == NULL) {
269 goto err;
270 }
271
272 if (dh->priv_key == NULL) {
273 priv_key = BN_new();
274 if (priv_key == NULL) {
275 goto err;
276 }
277 generate_new_key = 1;
278 } else {
279 priv_key = dh->priv_key;
280 }
281
282 if (dh->pub_key == NULL) {
283 pub_key = BN_new();
284 if (pub_key == NULL) {
285 goto err;
286 }
287 } else {
288 pub_key = dh->pub_key;
289 }
290
291 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
292 dh->p, ctx)) {
293 goto err;
294 }
295
296 if (generate_new_key) {
297 if (dh->q) {
298 if (!BN_rand_range_ex(priv_key, 2, dh->q)) {
299 goto err;
300 }
301 } else {
302 /* secret exponent length */
303 unsigned priv_bits = dh->priv_length;
304 if (priv_bits == 0) {
305 const unsigned p_bits = BN_num_bits(dh->p);
306 if (p_bits == 0) {
307 goto err;
308 }
309
310 priv_bits = p_bits - 1;
311 }
312
313 if (!BN_rand(priv_key, priv_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) {
314 goto err;
315 }
316 }
317 }
318
319 if (!BN_mod_exp_mont_consttime(pub_key, dh->g, priv_key, dh->p, ctx,
320 dh->method_mont_p)) {
321 goto err;
322 }
323
324 dh->pub_key = pub_key;
325 dh->priv_key = priv_key;
326 ok = 1;
327
328 err:
329 if (ok != 1) {
330 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
331 }
332
333 if (dh->pub_key == NULL) {
334 BN_free(pub_key);
335 }
336 if (dh->priv_key == NULL) {
337 BN_free(priv_key);
338 }
339 BN_CTX_free(ctx);
340 return ok;
341 }
342
DH_compute_key(unsigned char * out,const BIGNUM * peers_key,DH * dh)343 int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
344 BN_CTX *ctx = NULL;
345 BIGNUM *shared_key;
346 int ret = -1;
347 int check_result;
348
349 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
350 OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
351 goto err;
352 }
353
354 ctx = BN_CTX_new();
355 if (ctx == NULL) {
356 goto err;
357 }
358 BN_CTX_start(ctx);
359 shared_key = BN_CTX_get(ctx);
360 if (shared_key == NULL) {
361 goto err;
362 }
363
364 if (dh->priv_key == NULL) {
365 OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE);
366 goto err;
367 }
368
369 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
370 dh->p, ctx)) {
371 goto err;
372 }
373
374 if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
375 OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
376 goto err;
377 }
378
379 if (!BN_mod_exp_mont_consttime(shared_key, peers_key, dh->priv_key, dh->p,
380 ctx, dh->method_mont_p)) {
381 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
382 goto err;
383 }
384
385 ret = BN_bn2bin(shared_key, out);
386
387 err:
388 if (ctx != NULL) {
389 BN_CTX_end(ctx);
390 BN_CTX_free(ctx);
391 }
392
393 return ret;
394 }
395
DH_size(const DH * dh)396 int DH_size(const DH *dh) { return BN_num_bytes(dh->p); }
397
DH_num_bits(const DH * dh)398 unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); }
399
DH_up_ref(DH * dh)400 int DH_up_ref(DH *dh) {
401 CRYPTO_refcount_inc(&dh->references);
402 return 1;
403 }
404
int_dh_bn_cpy(BIGNUM ** dst,const BIGNUM * src)405 static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
406 BIGNUM *a = NULL;
407
408 if (src) {
409 a = BN_dup(src);
410 if (!a) {
411 return 0;
412 }
413 }
414
415 BN_free(*dst);
416 *dst = a;
417 return 1;
418 }
419
int_dh_param_copy(DH * to,const DH * from,int is_x942)420 static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
421 if (is_x942 == -1) {
422 is_x942 = !!from->q;
423 }
424 if (!int_dh_bn_cpy(&to->p, from->p) ||
425 !int_dh_bn_cpy(&to->g, from->g)) {
426 return 0;
427 }
428
429 if (!is_x942) {
430 return 1;
431 }
432
433 if (!int_dh_bn_cpy(&to->q, from->q) ||
434 !int_dh_bn_cpy(&to->j, from->j)) {
435 return 0;
436 }
437
438 OPENSSL_free(to->seed);
439 to->seed = NULL;
440 to->seedlen = 0;
441
442 if (from->seed) {
443 to->seed = BUF_memdup(from->seed, from->seedlen);
444 if (!to->seed) {
445 return 0;
446 }
447 to->seedlen = from->seedlen;
448 }
449
450 return 1;
451 }
452
DHparams_dup(const DH * dh)453 DH *DHparams_dup(const DH *dh) {
454 DH *ret = DH_new();
455 if (!ret) {
456 return NULL;
457 }
458
459 if (!int_dh_param_copy(ret, dh, -1)) {
460 DH_free(ret);
461 return NULL;
462 }
463
464 return ret;
465 }
466
DH_get_ex_new_index(long argl,void * argp,CRYPTO_EX_unused * unused,CRYPTO_EX_dup * dup_unused,CRYPTO_EX_free * free_func)467 int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
468 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
469 int index;
470 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
471 free_func)) {
472 return -1;
473 }
474 return index;
475 }
476
DH_set_ex_data(DH * d,int idx,void * arg)477 int DH_set_ex_data(DH *d, int idx, void *arg) {
478 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
479 }
480
DH_get_ex_data(DH * d,int idx)481 void *DH_get_ex_data(DH *d, int idx) {
482 return CRYPTO_get_ex_data(&d->ex_data, idx);
483 }
484