blob: 33c36f31fa557df3e0f39b3e6b8cef86a14b4e82 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
Adam Langleye9ada862015-05-11 17:20:37 -070068#include "../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080069
70
Adam Langleyfad63272015-11-12 12:15:39 -080071#define OPENSSL_DH_MAX_MODULUS_BITS 10000
Adam Langleyd9e397b2015-01-22 14:27:53 -080072
Adam Langleye9ada862015-05-11 17:20:37 -070073static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
74
Adam Langleyfad63272015-11-12 12:15:39 -080075DH *DH_new(void) {
David Benjamin4969cc92016-04-22 15:02:23 -040076 DH *dh = OPENSSL_malloc(sizeof(DH));
Adam Langleyd9e397b2015-01-22 14:27:53 -080077 if (dh == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +000078 OPENSSL_PUT_ERROR(DH, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -080079 return NULL;
80 }
81
Robert Sloan69939df2017-01-09 10:53:07 -080082 OPENSSL_memset(dh, 0, sizeof(DH));
Adam Langleyd9e397b2015-01-22 14:27:53 -080083
Adam Langleye9ada862015-05-11 17:20:37 -070084 CRYPTO_MUTEX_init(&dh->method_mont_p_lock);
85
Adam Langleyd9e397b2015-01-22 14:27:53 -080086 dh->references = 1;
Adam Langley4139edb2016-01-13 15:00:54 -080087 CRYPTO_new_ex_data(&dh->ex_data);
Adam Langleyd9e397b2015-01-22 14:27:53 -080088
Adam Langleyd9e397b2015-01-22 14:27:53 -080089 return dh;
90}
91
92void DH_free(DH *dh) {
93 if (dh == NULL) {
94 return;
95 }
96
Adam Langleyf4e42722015-06-04 17:45:09 -070097 if (!CRYPTO_refcount_dec_and_test_zero(&dh->references)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080098 return;
99 }
100
Adam Langleye9ada862015-05-11 17:20:37 -0700101 CRYPTO_free_ex_data(&g_ex_data_class, dh, &dh->ex_data);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800102
Kenny Roote99801b2015-11-06 15:31:15 -0800103 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);
Adam Langleye9ada862015-05-11 17:20:37 -0700112 CRYPTO_MUTEX_cleanup(&dh->method_mont_p_lock);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800113
114 OPENSSL_free(dh);
115}
116
David Benjaminc895d6b2016-08-11 13:26:41 -0400117void 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
127void 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
Adam Langleyd9e397b2015-01-22 14:27:53 -0800140int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb) {
Adam Langleyfad63272015-11-12 12:15:39 -0800141 /* 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;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175 }
Adam Langleyfad63272015-11-12 12:15:39 -0800176 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
244err:
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;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800254}
255
256int DH_generate_key(DH *dh) {
Adam Langleyfad63272015-11-12 12:15:39 -0800257 int ok = 0;
258 int generate_new_key = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800259 BN_CTX *ctx = NULL;
Adam Langleyfad63272015-11-12 12:15:39 -0800260 BIGNUM *pub_key = NULL, *priv_key = NULL;
Adam Langleyfad63272015-11-12 12:15:39 -0800261
Adam Langley4139edb2016-01-13 15:00:54 -0800262 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
Adam Langleyfad63272015-11-12 12:15:39 -0800267 ctx = BN_CTX_new();
268 if (ctx == NULL) {
269 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800270 }
Adam Langleyfad63272015-11-12 12:15:39 -0800271
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
David Benjamin4969cc92016-04-22 15:02:23 -0400291 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
292 dh->p, ctx)) {
Adam Langleyfad63272015-11-12 12:15:39 -0800293 goto err;
294 }
295
296 if (generate_new_key) {
297 if (dh->q) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400298 if (!BN_rand_range_ex(priv_key, 2, dh->q)) {
299 goto err;
300 }
Adam Langleyfad63272015-11-12 12:15:39 -0800301 } else {
302 /* secret exponent length */
David Benjaminc895d6b2016-08-11 13:26:41 -0400303 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
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400313 if (!BN_rand(priv_key, priv_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) {
Adam Langleyfad63272015-11-12 12:15:39 -0800314 goto err;
315 }
316 }
317 }
318
Steven Valdezb0b45c62017-01-17 16:23:54 -0500319 if (!BN_mod_exp_mont_consttime(pub_key, dh->g, priv_key, dh->p, ctx,
David Benjamind316cba2016-06-02 16:17:39 -0400320 dh->method_mont_p)) {
Adam Langleyfad63272015-11-12 12:15:39 -0800321 goto err;
322 }
323
324 dh->pub_key = pub_key;
325 dh->priv_key = priv_key;
326 ok = 1;
327
328err:
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;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800341}
342
343int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
Adam Langleyfad63272015-11-12 12:15:39 -0800344 BN_CTX *ctx = NULL;
Adam Langleyfad63272015-11-12 12:15:39 -0800345 BIGNUM *shared_key;
346 int ret = -1;
347 int check_result;
Adam Langleyfad63272015-11-12 12:15:39 -0800348
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;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800352 }
Adam Langleyfad63272015-11-12 12:15:39 -0800353
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
David Benjamin4969cc92016-04-22 15:02:23 -0400369 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
370 dh->p, ctx)) {
Adam Langleyfad63272015-11-12 12:15:39 -0800371 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
Steven Valdezb0b45c62017-01-17 16:23:54 -0500379 if (!BN_mod_exp_mont_consttime(shared_key, peers_key, dh->priv_key, dh->p,
380 ctx, dh->method_mont_p)) {
Adam Langleyfad63272015-11-12 12:15:39 -0800381 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
382 goto err;
383 }
384
385 ret = BN_bn2bin(shared_key, out);
386
387err:
388 if (ctx != NULL) {
389 BN_CTX_end(ctx);
390 BN_CTX_free(ctx);
391 }
392
393 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800394}
395
396int DH_size(const DH *dh) { return BN_num_bytes(dh->p); }
397
Adam Langleyf4e42722015-06-04 17:45:09 -0700398unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); }
399
400int DH_up_ref(DH *dh) {
401 CRYPTO_refcount_inc(&dh->references);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800402 return 1;
403}
404
405static 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
Adam Langleye9ada862015-05-11 17:20:37 -0700415 BN_free(*dst);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800416 *dst = a;
417 return 1;
418}
419
420static 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
Adam Langleye9ada862015-05-11 17:20:37 -0700438 OPENSSL_free(to->seed);
439 to->seed = NULL;
440 to->seedlen = 0;
441
Adam Langleyd9e397b2015-01-22 14:27:53 -0800442 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
453DH *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
Adam Langley4139edb2016-01-13 15:00:54 -0800467int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800468 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
Adam Langleye9ada862015-05-11 17:20:37 -0700469 int index;
Adam Langley4139edb2016-01-13 15:00:54 -0800470 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
471 free_func)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700472 return -1;
473 }
474 return index;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800475}
476
477int DH_set_ex_data(DH *d, int idx, void *arg) {
Adam Langleyfad63272015-11-12 12:15:39 -0800478 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800479}
480
481void *DH_get_ex_data(DH *d, int idx) {
Adam Langleyfad63272015-11-12 12:15:39 -0800482 return CRYPTO_get_ex_data(&d->ex_data, idx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800483}