Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 59 | #include <string.h> |
| 60 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 68 | #include "../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 69 | |
| 70 | |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 71 | #define OPENSSL_DH_MAX_MODULUS_BITS 10000 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 72 | |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 73 | static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT; |
| 74 | |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 75 | DH *DH_new(void) { |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 76 | DH *dh = OPENSSL_malloc(sizeof(DH)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 77 | if (dh == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 78 | OPENSSL_PUT_ERROR(DH, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 79 | return NULL; |
| 80 | } |
| 81 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 82 | OPENSSL_memset(dh, 0, sizeof(DH)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 83 | |
Adam Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 84 | CRYPTO_MUTEX_init(&dh->method_mont_p_lock); |
| 85 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 86 | dh->references = 1; |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 87 | CRYPTO_new_ex_data(&dh->ex_data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 88 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 89 | return dh; |
| 90 | } |
| 91 | |
| 92 | void DH_free(DH *dh) { |
| 93 | if (dh == NULL) { |
| 94 | return; |
| 95 | } |
| 96 | |
Adam Langley | 0da323a | 2015-05-15 12:49:30 -0700 | [diff] [blame] | 97 | if (!CRYPTO_refcount_dec_and_test_zero(&dh->references)) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 101 | CRYPTO_free_ex_data(&g_ex_data_class, dh, &dh->ex_data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 102 | |
David Benjamin | 911cfb7 | 2015-10-25 12:40:26 -0400 | [diff] [blame] | 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); |
Adam Langley | 683d7bd | 2015-04-13 11:04:14 -0700 | [diff] [blame] | 112 | CRYPTO_MUTEX_cleanup(&dh->method_mont_p_lock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 113 | |
| 114 | OPENSSL_free(dh); |
| 115 | } |
| 116 | |
David Benjamin | 5a91503 | 2016-08-09 11:04:24 -0400 | [diff] [blame] | 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 | |
| 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 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | int DH_generate_parameters_ex(DH *dh, int prime_bits, int generator, BN_GENCB *cb) { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 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. |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 161 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 162 | // 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 Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 165 | |
| 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 173 | } |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 174 | 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 Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 181 | // Make sure |dh| has the necessary elements |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 182 | 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 Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 214 | // BN_set_word(t3,7); just have to miss |
| 215 | // out on these ones :-( |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 216 | g = 5; |
| 217 | } else { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 218 | // 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 Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 222 | 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 | |
| 242 | err: |
| 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | int DH_generate_key(DH *dh) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 255 | int ok = 0; |
| 256 | int generate_new_key = 0; |
David Benjamin | cd24a39 | 2015-11-11 13:23:05 -0800 | [diff] [blame] | 257 | BN_CTX *ctx = NULL; |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 258 | BIGNUM *pub_key = NULL, *priv_key = NULL; |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 259 | |
David Benjamin | cd24a39 | 2015-11-11 13:23:05 -0800 | [diff] [blame] | 260 | 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 Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 265 | ctx = BN_CTX_new(); |
| 266 | if (ctx == NULL) { |
| 267 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 268 | } |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 269 | |
| 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 Smith | d035730 | 2016-03-25 10:11:04 -1000 | [diff] [blame] | 289 | if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock, |
| 290 | dh->p, ctx)) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 291 | goto err; |
| 292 | } |
| 293 | |
| 294 | if (generate_new_key) { |
| 295 | if (dh->q) { |
Brian Smith | 4edca0b | 2016-07-25 10:36:58 -1000 | [diff] [blame] | 296 | if (!BN_rand_range_ex(priv_key, 2, dh->q)) { |
| 297 | goto err; |
| 298 | } |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 299 | } else { |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 300 | // secret exponent length |
Adam Langley | aa24851 | 2016-08-10 15:26:41 -0700 | [diff] [blame] | 301 | 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 Benjamin | d224d52 | 2016-08-16 10:03:45 -0400 | [diff] [blame] | 311 | if (!BN_rand(priv_key, priv_bits, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 312 | goto err; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
David Benjamin | 0a211df | 2016-12-17 15:25:55 -0500 | [diff] [blame] | 317 | if (!BN_mod_exp_mont_consttime(pub_key, dh->g, priv_key, dh->p, ctx, |
Brian Smith | 0e01eb5 | 2016-04-21 16:30:37 -1000 | [diff] [blame] | 318 | dh->method_mont_p)) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 319 | goto err; |
| 320 | } |
| 321 | |
| 322 | dh->pub_key = pub_key; |
| 323 | dh->priv_key = priv_key; |
| 324 | ok = 1; |
| 325 | |
| 326 | err: |
| 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 341 | int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 342 | BN_CTX *ctx = NULL; |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 343 | BIGNUM *shared_key; |
| 344 | int ret = -1; |
| 345 | int check_result; |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 346 | |
| 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 Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | } |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 351 | |
| 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 Smith | d035730 | 2016-03-25 10:11:04 -1000 | [diff] [blame] | 367 | if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock, |
| 368 | dh->p, ctx)) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 369 | 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 Benjamin | 0a211df | 2016-12-17 15:25:55 -0500 | [diff] [blame] | 377 | if (!BN_mod_exp_mont_consttime(shared_key, peers_key, dh->priv_key, dh->p, |
| 378 | ctx, dh->method_mont_p)) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 379 | OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB); |
| 380 | goto err; |
| 381 | } |
| 382 | |
| 383 | ret = BN_bn2bin(shared_key, out); |
| 384 | |
| 385 | err: |
| 386 | if (ctx != NULL) { |
| 387 | BN_CTX_end(ctx); |
| 388 | BN_CTX_free(ctx); |
| 389 | } |
| 390 | |
| 391 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | int DH_size(const DH *dh) { return BN_num_bytes(dh->p); } |
| 395 | |
Adam Langley | a7997f1 | 2015-05-14 17:38:50 -0700 | [diff] [blame] | 396 | unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); } |
| 397 | |
Adam Langley | 0da323a | 2015-05-15 12:49:30 -0700 | [diff] [blame] | 398 | int DH_up_ref(DH *dh) { |
| 399 | CRYPTO_refcount_inc(&dh->references); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | static 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 Benjamin | 22ccc2d | 2015-04-22 13:50:28 -0400 | [diff] [blame] | 413 | BN_free(*dst); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | *dst = a; |
| 415 | return 1; |
| 416 | } |
| 417 | |
| 418 | static 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 Benjamin | 22ccc2d | 2015-04-22 13:50:28 -0400 | [diff] [blame] | 436 | OPENSSL_free(to->seed); |
| 437 | to->seed = NULL; |
| 438 | to->seedlen = 0; |
| 439 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 440 | 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 | |
| 451 | DH *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 Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 465 | int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 466 | CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 467 | int index; |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 468 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 469 | free_func)) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 470 | return -1; |
| 471 | } |
| 472 | return index; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | int DH_set_ex_data(DH *d, int idx, void *arg) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 476 | return CRYPTO_set_ex_data(&d->ex_data, idx, arg); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void *DH_get_ex_data(DH *d, int idx) { |
David Benjamin | e8f783a | 2015-10-30 16:53:00 -0400 | [diff] [blame] | 480 | return CRYPTO_get_ex_data(&d->ex_data, idx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | } |