blob: 3356776ff58f8fadc88d632e7eded27adfb4fb18 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
Adam Langley2b2d66d2015-01-30 17:08:37 -080059#include <string.h>
60
Adam Langley95c29f32014-06-20 12:00:00 -070061#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 Langley683d7bd2015-04-13 11:04:14 -070068#include "../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070069
70
David Benjamine8f783a2015-10-30 16:53:00 -040071#define OPENSSL_DH_MAX_MODULUS_BITS 10000
Adam Langley95c29f32014-06-20 12:00:00 -070072
David Benjamin9f33fc62015-04-15 17:29:53 -040073static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
74
David Benjamine8f783a2015-10-30 16:53:00 -040075DH *DH_new(void) {
Brian Smith5ba06892016-02-07 09:36:04 -100076 DH *dh = OPENSSL_malloc(sizeof(DH));
Adam Langley95c29f32014-06-20 12:00:00 -070077 if (dh == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -040078 OPENSSL_PUT_ERROR(DH, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -070079 return NULL;
80 }
81
David Benjamin17cf2cb2016-12-13 01:07:13 -050082 OPENSSL_memset(dh, 0, sizeof(DH));
Adam Langley95c29f32014-06-20 12:00:00 -070083
Adam Langley683d7bd2015-04-13 11:04:14 -070084 CRYPTO_MUTEX_init(&dh->method_mont_p_lock);
85
Adam Langley95c29f32014-06-20 12:00:00 -070086 dh->references = 1;
David Benjamin8a589332015-12-04 23:14:35 -050087 CRYPTO_new_ex_data(&dh->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -070088
Adam Langley95c29f32014-06-20 12:00:00 -070089 return dh;
90}
91
92void DH_free(DH *dh) {
93 if (dh == NULL) {
94 return;
95 }
96
Adam Langley0da323a2015-05-15 12:49:30 -070097 if (!CRYPTO_refcount_dec_and_test_zero(&dh->references)) {
Adam Langley95c29f32014-06-20 12:00:00 -070098 return;
99 }
100
David Benjamin9f33fc62015-04-15 17:29:53 -0400101 CRYPTO_free_ex_data(&g_ex_data_class, dh, &dh->ex_data);
Adam Langley95c29f32014-06-20 12:00:00 -0700102
David Benjamin911cfb72015-10-25 12:40:26 -0400103 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 Langley683d7bd2015-04-13 11:04:14 -0700112 CRYPTO_MUTEX_cleanup(&dh->method_mont_p_lock);
Adam Langley95c29f32014-06-20 12:00:00 -0700113
114 OPENSSL_free(dh);
115}
116
David Benjamin5a915032016-08-09 11:04:24 -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 Langley95c29f32014-06-20 12:00:00 -0700140int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb) {
David Benjamin808f8322017-08-18 14:06:02 -0400141 // 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.
David Benjamine8f783a2015-10-30 16:53:00 -0400161
David Benjamin808f8322017-08-18 14:06:02 -0400162 // Actually there is no reason to insist that 'generator' be a generator.
163 // It's just as OK (and in some sense better) to use a generator of the
164 // order-q subgroup.
David Benjamine8f783a2015-10-30 16:53:00 -0400165
166 BIGNUM *t1, *t2;
167 int g, ok = 0;
168 BN_CTX *ctx = NULL;
169
170 ctx = BN_CTX_new();
171 if (ctx == NULL) {
172 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700173 }
David Benjamine8f783a2015-10-30 16:53:00 -0400174 BN_CTX_start(ctx);
175 t1 = BN_CTX_get(ctx);
176 t2 = BN_CTX_get(ctx);
177 if (t1 == NULL || t2 == NULL) {
178 goto err;
179 }
180
David Benjamin808f8322017-08-18 14:06:02 -0400181 // Make sure |dh| has the necessary elements
David Benjamine8f783a2015-10-30 16:53:00 -0400182 if (dh->p == NULL) {
183 dh->p = BN_new();
184 if (dh->p == NULL) {
185 goto err;
186 }
187 }
188 if (dh->g == NULL) {
189 dh->g = BN_new();
190 if (dh->g == NULL) {
191 goto err;
192 }
193 }
194
195 if (generator <= 1) {
196 OPENSSL_PUT_ERROR(DH, DH_R_BAD_GENERATOR);
197 goto err;
198 }
199 if (generator == DH_GENERATOR_2) {
200 if (!BN_set_word(t1, 24)) {
201 goto err;
202 }
203 if (!BN_set_word(t2, 11)) {
204 goto err;
205 }
206 g = 2;
207 } else if (generator == DH_GENERATOR_5) {
208 if (!BN_set_word(t1, 10)) {
209 goto err;
210 }
211 if (!BN_set_word(t2, 3)) {
212 goto err;
213 }
David Benjamin808f8322017-08-18 14:06:02 -0400214 // BN_set_word(t3,7); just have to miss
215 // out on these ones :-(
David Benjamine8f783a2015-10-30 16:53:00 -0400216 g = 5;
217 } else {
David Benjamin808f8322017-08-18 14:06:02 -0400218 // in the general case, don't worry if 'generator' is a
219 // generator or not: since we are using safe primes,
220 // it will generate either an order-q or an order-2q group,
221 // which both is OK
David Benjamine8f783a2015-10-30 16:53:00 -0400222 if (!BN_set_word(t1, 2)) {
223 goto err;
224 }
225 if (!BN_set_word(t2, 1)) {
226 goto err;
227 }
228 g = generator;
229 }
230
231 if (!BN_generate_prime_ex(dh->p, prime_bits, 1, t1, t2, cb)) {
232 goto err;
233 }
234 if (!BN_GENCB_call(cb, 3, 0)) {
235 goto err;
236 }
237 if (!BN_set_word(dh->g, g)) {
238 goto err;
239 }
240 ok = 1;
241
242err:
243 if (!ok) {
244 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
245 }
246
247 if (ctx != NULL) {
248 BN_CTX_end(ctx);
249 BN_CTX_free(ctx);
250 }
251 return ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700252}
253
254int DH_generate_key(DH *dh) {
David Benjamine8f783a2015-10-30 16:53:00 -0400255 int ok = 0;
256 int generate_new_key = 0;
David Benjamincd24a392015-11-11 13:23:05 -0800257 BN_CTX *ctx = NULL;
David Benjamine8f783a2015-10-30 16:53:00 -0400258 BIGNUM *pub_key = NULL, *priv_key = NULL;
David Benjamine8f783a2015-10-30 16:53:00 -0400259
David Benjamincd24a392015-11-11 13:23:05 -0800260 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
261 OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
262 goto err;
263 }
264
David Benjamine8f783a2015-10-30 16:53:00 -0400265 ctx = BN_CTX_new();
266 if (ctx == NULL) {
267 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700268 }
David Benjamine8f783a2015-10-30 16:53:00 -0400269
270 if (dh->priv_key == NULL) {
271 priv_key = BN_new();
272 if (priv_key == NULL) {
273 goto err;
274 }
275 generate_new_key = 1;
276 } else {
277 priv_key = dh->priv_key;
278 }
279
280 if (dh->pub_key == NULL) {
281 pub_key = BN_new();
282 if (pub_key == NULL) {
283 goto err;
284 }
285 } else {
286 pub_key = dh->pub_key;
287 }
288
Brian Smithd0357302016-03-25 10:11:04 -1000289 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
290 dh->p, ctx)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400291 goto err;
292 }
293
294 if (generate_new_key) {
295 if (dh->q) {
Brian Smith4edca0b2016-07-25 10:36:58 -1000296 if (!BN_rand_range_ex(priv_key, 2, dh->q)) {
297 goto err;
298 }
David Benjamine8f783a2015-10-30 16:53:00 -0400299 } else {
David Benjamin808f8322017-08-18 14:06:02 -0400300 // secret exponent length
Adam Langleyaa248512016-08-10 15:26:41 -0700301 unsigned priv_bits = dh->priv_length;
302 if (priv_bits == 0) {
303 const unsigned p_bits = BN_num_bits(dh->p);
304 if (p_bits == 0) {
305 goto err;
306 }
307
308 priv_bits = p_bits - 1;
309 }
310
David Benjamind224d522016-08-16 10:03:45 -0400311 if (!BN_rand(priv_key, priv_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400312 goto err;
313 }
314 }
315 }
316
David Benjamin0a211df2016-12-17 15:25:55 -0500317 if (!BN_mod_exp_mont_consttime(pub_key, dh->g, priv_key, dh->p, ctx,
Brian Smith0e01eb52016-04-21 16:30:37 -1000318 dh->method_mont_p)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400319 goto err;
320 }
321
322 dh->pub_key = pub_key;
323 dh->priv_key = priv_key;
324 ok = 1;
325
326err:
327 if (ok != 1) {
328 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
329 }
330
331 if (dh->pub_key == NULL) {
332 BN_free(pub_key);
333 }
334 if (dh->priv_key == NULL) {
335 BN_free(priv_key);
336 }
337 BN_CTX_free(ctx);
338 return ok;
Adam Langley95c29f32014-06-20 12:00:00 -0700339}
340
Adam Langleyded93582014-07-31 15:23:51 -0700341int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
David Benjamine8f783a2015-10-30 16:53:00 -0400342 BN_CTX *ctx = NULL;
David Benjamine8f783a2015-10-30 16:53:00 -0400343 BIGNUM *shared_key;
344 int ret = -1;
345 int check_result;
David Benjamine8f783a2015-10-30 16:53:00 -0400346
347 if (BN_num_bits(dh->p) > OPENSSL_DH_MAX_MODULUS_BITS) {
348 OPENSSL_PUT_ERROR(DH, DH_R_MODULUS_TOO_LARGE);
349 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700350 }
David Benjamine8f783a2015-10-30 16:53:00 -0400351
352 ctx = BN_CTX_new();
353 if (ctx == NULL) {
354 goto err;
355 }
356 BN_CTX_start(ctx);
357 shared_key = BN_CTX_get(ctx);
358 if (shared_key == NULL) {
359 goto err;
360 }
361
362 if (dh->priv_key == NULL) {
363 OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE);
364 goto err;
365 }
366
Brian Smithd0357302016-03-25 10:11:04 -1000367 if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock,
368 dh->p, ctx)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400369 goto err;
370 }
371
372 if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
373 OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
374 goto err;
375 }
376
David Benjamin0a211df2016-12-17 15:25:55 -0500377 if (!BN_mod_exp_mont_consttime(shared_key, peers_key, dh->priv_key, dh->p,
378 ctx, dh->method_mont_p)) {
David Benjamine8f783a2015-10-30 16:53:00 -0400379 OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
380 goto err;
381 }
382
383 ret = BN_bn2bin(shared_key, out);
384
385err:
386 if (ctx != NULL) {
387 BN_CTX_end(ctx);
388 BN_CTX_free(ctx);
389 }
390
391 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700392}
393
394int DH_size(const DH *dh) { return BN_num_bytes(dh->p); }
395
Adam Langleya7997f12015-05-14 17:38:50 -0700396unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); }
397
Adam Langley0da323a2015-05-15 12:49:30 -0700398int DH_up_ref(DH *dh) {
399 CRYPTO_refcount_inc(&dh->references);
Adam Langley95c29f32014-06-20 12:00:00 -0700400 return 1;
401}
402
403static int int_dh_bn_cpy(BIGNUM **dst, const BIGNUM *src) {
404 BIGNUM *a = NULL;
405
406 if (src) {
407 a = BN_dup(src);
408 if (!a) {
409 return 0;
410 }
411 }
412
David Benjamin22ccc2d2015-04-22 13:50:28 -0400413 BN_free(*dst);
Adam Langley95c29f32014-06-20 12:00:00 -0700414 *dst = a;
415 return 1;
416}
417
418static int int_dh_param_copy(DH *to, const DH *from, int is_x942) {
419 if (is_x942 == -1) {
420 is_x942 = !!from->q;
421 }
422 if (!int_dh_bn_cpy(&to->p, from->p) ||
423 !int_dh_bn_cpy(&to->g, from->g)) {
424 return 0;
425 }
426
427 if (!is_x942) {
428 return 1;
429 }
430
431 if (!int_dh_bn_cpy(&to->q, from->q) ||
432 !int_dh_bn_cpy(&to->j, from->j)) {
433 return 0;
434 }
435
David Benjamin22ccc2d2015-04-22 13:50:28 -0400436 OPENSSL_free(to->seed);
437 to->seed = NULL;
438 to->seedlen = 0;
439
Adam Langley95c29f32014-06-20 12:00:00 -0700440 if (from->seed) {
441 to->seed = BUF_memdup(from->seed, from->seedlen);
442 if (!to->seed) {
443 return 0;
444 }
445 to->seedlen = from->seedlen;
446 }
447
448 return 1;
449}
450
451DH *DHparams_dup(const DH *dh) {
452 DH *ret = DH_new();
453 if (!ret) {
454 return NULL;
455 }
456
457 if (!int_dh_param_copy(ret, dh, -1)) {
458 DH_free(ret);
459 return NULL;
460 }
461
462 return ret;
463}
464
David Benjamin8a589332015-12-04 23:14:35 -0500465int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
David Benjamind94682d2017-05-14 17:10:18 -0400466 CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400467 int index;
David Benjamind94682d2017-05-14 17:10:18 -0400468 if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
David Benjamin8a589332015-12-04 23:14:35 -0500469 free_func)) {
David Benjamin9f33fc62015-04-15 17:29:53 -0400470 return -1;
471 }
472 return index;
Adam Langley95c29f32014-06-20 12:00:00 -0700473}
474
475int DH_set_ex_data(DH *d, int idx, void *arg) {
David Benjamine8f783a2015-10-30 16:53:00 -0400476 return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
Adam Langley95c29f32014-06-20 12:00:00 -0700477}
478
479void *DH_get_ex_data(DH *d, int idx) {
David Benjamine8f783a2015-10-30 16:53:00 -0400480 return CRYPTO_get_ex_data(&d->ex_data, idx);
Adam Langley95c29f32014-06-20 12:00:00 -0700481}