| // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/dh.h> |
| 16 |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <openssl/bn.h> |
| 20 | #include <openssl/digest.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 | #include <openssl/thread.h> |
| 24 |
| 25 | #include "../../internal.h" |
| 26 | #include "../bn/internal.h" |
| 27 | #include "../service_indicator/internal.h" |
| 28 | #include "internal.h" |
| 29 |
| 30 |
| 31 | DH *DH_new(void) { |
| 32 | DH *dh = reinterpret_cast<DH *>(OPENSSL_zalloc(sizeof(DH))); |
| 33 | if (dh == NULL) { |
| 34 | return NULL; |
| 35 | } |
| 36 |
| 37 | CRYPTO_MUTEX_init(&dh->method_mont_p_lock); |
| 38 | dh->references = 1; |
| 39 | return dh; |
| 40 | } |
| 41 |
| 42 | void DH_free(DH *dh) { |
| 43 | if (dh == NULL) { |
| 44 | return; |
| 45 | } |
| 46 |
| 47 | if (!CRYPTO_refcount_dec_and_test_zero(&dh->references)) { |
| 48 | return; |
| 49 | } |
| 50 |
| 51 | BN_MONT_CTX_free(dh->method_mont_p); |
| 52 | BN_clear_free(dh->p); |
| 53 | BN_clear_free(dh->g); |
| 54 | BN_clear_free(dh->q); |
| 55 | BN_clear_free(dh->pub_key); |
| 56 | BN_clear_free(dh->priv_key); |
| 57 | CRYPTO_MUTEX_cleanup(&dh->method_mont_p_lock); |
| 58 |
| 59 | OPENSSL_free(dh); |
| 60 | } |
| 61 |
| 62 | unsigned DH_bits(const DH *dh) { return BN_num_bits(dh->p); } |
| 63 |
| 64 | const BIGNUM *DH_get0_pub_key(const DH *dh) { return dh->pub_key; } |
| 65 |
| 66 | const BIGNUM *DH_get0_priv_key(const DH *dh) { return dh->priv_key; } |
| 67 |
| 68 | const BIGNUM *DH_get0_p(const DH *dh) { return dh->p; } |
| 69 |
| 70 | const BIGNUM *DH_get0_q(const DH *dh) { return dh->q; } |
| 71 |
| 72 | const BIGNUM *DH_get0_g(const DH *dh) { return dh->g; } |
| 73 |
| 74 | void DH_get0_key(const DH *dh, const BIGNUM **out_pub_key, |
| 75 | const BIGNUM **out_priv_key) { |
| 76 | if (out_pub_key != NULL) { |
| 77 | *out_pub_key = dh->pub_key; |
| 78 | } |
| 79 | if (out_priv_key != NULL) { |
| 80 | *out_priv_key = dh->priv_key; |
| 81 | } |
| 82 | } |
| 83 |
| 84 | int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) { |
| 85 | if (pub_key != NULL) { |
| 86 | BN_free(dh->pub_key); |
| 87 | dh->pub_key = pub_key; |
| 88 | } |
| 89 |
| 90 | if (priv_key != NULL) { |
| 91 | BN_free(dh->priv_key); |
| 92 | dh->priv_key = priv_key; |
| 93 | } |
| 94 |
| 95 | return 1; |
| 96 | } |
| 97 |
| 98 | void DH_get0_pqg(const DH *dh, const BIGNUM **out_p, const BIGNUM **out_q, |
| 99 | const BIGNUM **out_g) { |
| 100 | if (out_p != NULL) { |
| 101 | *out_p = dh->p; |
| 102 | } |
| 103 | if (out_q != NULL) { |
| 104 | *out_q = dh->q; |
| 105 | } |
| 106 | if (out_g != NULL) { |
| 107 | *out_g = dh->g; |
| 108 | } |
| 109 | } |
| 110 |
| 111 | int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) { |
| 112 | if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL)) { |
| 113 | return 0; |
| 114 | } |
| 115 |
| 116 | if (p != NULL) { |
| 117 | BN_free(dh->p); |
| 118 | dh->p = p; |
| 119 | } |
| 120 |
| 121 | if (q != NULL) { |
| 122 | BN_free(dh->q); |
| 123 | dh->q = q; |
| 124 | } |
| 125 |
| 126 | if (g != NULL) { |
| 127 | BN_free(dh->g); |
| 128 | dh->g = g; |
| 129 | } |
| 130 |
| 131 | // Invalidate the cached Montgomery parameters. |
| 132 | BN_MONT_CTX_free(dh->method_mont_p); |
| 133 | dh->method_mont_p = NULL; |
| 134 | return 1; |
| 135 | } |
| 136 |
| 137 | int DH_set_length(DH *dh, unsigned priv_length) { |
| 138 | dh->priv_length = priv_length; |
| 139 | return 1; |
| 140 | } |
| 141 |
| 142 | int DH_generate_key(DH *dh) { |
| 143 | boringssl_ensure_ffdh_self_test(); |
| 144 |
| 145 | if (!dh_check_params_fast(dh)) { |
| 146 | return 0; |
| 147 | } |
| 148 |
| 149 | int ok = 0; |
| 150 | int generate_new_key = 0; |
| 151 | BN_CTX *ctx = NULL; |
| 152 | BIGNUM *pub_key = NULL, *priv_key = NULL, *priv_key_limit = NULL; |
| 153 |
| 154 | ctx = BN_CTX_new(); |
| 155 | if (ctx == NULL) { |
| 156 | goto err; |
| 157 | } |
| 158 |
| 159 | if (dh->priv_key == NULL) { |
| 160 | priv_key = BN_new(); |
| 161 | if (priv_key == NULL) { |
| 162 | goto err; |
| 163 | } |
| 164 | generate_new_key = 1; |
| 165 | } else { |
| 166 | priv_key = dh->priv_key; |
| 167 | } |
| 168 |
| 169 | if (dh->pub_key == NULL) { |
| 170 | pub_key = BN_new(); |
| 171 | if (pub_key == NULL) { |
| 172 | goto err; |
| 173 | } |
| 174 | } else { |
| 175 | pub_key = dh->pub_key; |
| 176 | } |
| 177 |
| 178 | if (!BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock, |
| 179 | dh->p, ctx)) { |
| 180 | goto err; |
| 181 | } |
| 182 |
| 183 | if (generate_new_key) { |
| 184 | if (dh->q) { |
| 185 | // Section 5.6.1.1.4 of SP 800-56A Rev3 generates a private key uniformly |
| 186 | // from [1, min(2^N-1, q-1)]. |
| 187 | // |
| 188 | // Although SP 800-56A Rev3 now permits a private key length N, |
| 189 | // |dh->priv_length| historically was ignored when q is available. We |
| 190 | // continue to ignore it and interpret such a configuration as N = len(q). |
| 191 | if (!BN_rand_range_ex(priv_key, 1, dh->q)) { |
| 192 | goto err; |
| 193 | } |
| 194 | } else { |
| 195 | // If q is unspecified, we expect p to be a safe prime, with g generating |
| 196 | // the (p-1)/2 subgroup. So, we use q = (p-1)/2. (If g generates a smaller |
| 197 | // prime-order subgroup, q will still divide (p-1)/2.) |
| 198 | // |
| 199 | // We set N from |dh->priv_length|. Section 5.6.1.1.4 of SP 800-56A Rev3 |
| 200 | // says to reject N > len(q), or N > num_bits(p) - 1. However, this logic |
| 201 | // originally aligned with PKCS#3, which allows num_bits(p). Instead, we |
| 202 | // clamp |dh->priv_length| before invoking the algorithm. |
| 203 |
| 204 | // Compute M = min(2^N, q). |
| 205 | priv_key_limit = BN_new(); |
| 206 | if (priv_key_limit == NULL) { |
| 207 | goto err; |
| 208 | } |
| 209 | if (dh->priv_length == 0 || dh->priv_length >= BN_num_bits(dh->p) - 1) { |
| 210 | // M = q = (p - 1) / 2. |
| 211 | if (!BN_rshift1(priv_key_limit, dh->p)) { |
| 212 | goto err; |
| 213 | } |
| 214 | } else { |
| 215 | // M = 2^N. |
| 216 | if (!BN_set_bit(priv_key_limit, dh->priv_length)) { |
| 217 | goto err; |
| 218 | } |
| 219 | } |
| 220 |
| 221 | // Choose a private key uniformly from [1, M-1]. |
| 222 | if (!BN_rand_range_ex(priv_key, 1, priv_key_limit)) { |
| 223 | goto err; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 |
| 228 | if (!BN_mod_exp_mont_consttime(pub_key, dh->g, priv_key, dh->p, ctx, |
| 229 | dh->method_mont_p)) { |
| 230 | goto err; |
| 231 | } |
| 232 |
| 233 | dh->pub_key = pub_key; |
| 234 | dh->priv_key = priv_key; |
| 235 | ok = 1; |
| 236 |
| 237 | err: |
| 238 | if (ok != 1) { |
| 239 | OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB); |
| 240 | } |
| 241 |
| 242 | if (dh->pub_key == NULL) { |
| 243 | BN_free(pub_key); |
| 244 | } |
| 245 | if (dh->priv_key == NULL) { |
| 246 | BN_free(priv_key); |
| 247 | } |
| 248 | BN_free(priv_key_limit); |
| 249 | BN_CTX_free(ctx); |
| 250 | return ok; |
| 251 | } |
| 252 |
| 253 | static int dh_compute_key(DH *dh, BIGNUM *out_shared_key, |
| 254 | const BIGNUM *peers_key, BN_CTX *ctx) { |
| 255 | if (!dh_check_params_fast(dh)) { |
| 256 | return 0; |
| 257 | } |
| 258 |
| 259 | if (dh->priv_key == NULL) { |
| 260 | OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE); |
| 261 | return 0; |
| 262 | } |
| 263 |
| 264 | int check_result; |
| 265 | if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) { |
| 266 | OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY); |
| 267 | return 0; |
| 268 | } |
| 269 |
| 270 | int ret = 0; |
| 271 | BN_CTX_start(ctx); |
| 272 | BIGNUM *p_minus_1 = BN_CTX_get(ctx); |
| 273 |
| 274 | if (!p_minus_1 || |
| 275 | !BN_MONT_CTX_set_locked(&dh->method_mont_p, &dh->method_mont_p_lock, |
| 276 | dh->p, ctx)) { |
| 277 | goto err; |
| 278 | } |
| 279 |
| 280 | if (!BN_mod_exp_mont_consttime(out_shared_key, peers_key, dh->priv_key, dh->p, |
| 281 | ctx, dh->method_mont_p) || |
| 282 | !BN_copy(p_minus_1, dh->p) || !BN_sub_word(p_minus_1, 1)) { |
| 283 | OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB); |
| 284 | goto err; |
| 285 | } |
| 286 |
| 287 | // This performs the check required by SP 800-56Ar3 section 5.7.1.1 step two. |
| 288 | if (BN_cmp_word(out_shared_key, 1) <= 0 || |
| 289 | BN_cmp(out_shared_key, p_minus_1) == 0) { |
| 290 | OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY); |
| 291 | goto err; |
| 292 | } |
| 293 |
| 294 | ret = 1; |
| 295 |
| 296 | err: |
| 297 | BN_CTX_end(ctx); |
| 298 | return ret; |
| 299 | } |
| 300 |
| 301 | int dh_compute_key_padded_no_self_test(unsigned char *out, |
| 302 | const BIGNUM *peers_key, DH *dh) { |
| 303 | BN_CTX *ctx = BN_CTX_new(); |
| 304 | if (ctx == NULL) { |
| 305 | return -1; |
| 306 | } |
| 307 | BN_CTX_start(ctx); |
| 308 |
| 309 | int dh_size = DH_size(dh); |
| 310 | int ret = -1; |
| 311 | BIGNUM *shared_key = BN_CTX_get(ctx); |
| 312 | if (shared_key && dh_compute_key(dh, shared_key, peers_key, ctx) && |
| 313 | BN_bn2bin_padded(out, dh_size, shared_key)) { |
| 314 | ret = dh_size; |
| 315 | } |
| 316 |
| 317 | BN_CTX_end(ctx); |
| 318 | BN_CTX_free(ctx); |
| 319 | return ret; |
| 320 | } |
| 321 |
| 322 | int DH_compute_key_padded(unsigned char *out, const BIGNUM *peers_key, DH *dh) { |
| 323 | boringssl_ensure_ffdh_self_test(); |
| 324 |
| 325 | return dh_compute_key_padded_no_self_test(out, peers_key, dh); |
| 326 | } |
| 327 |
| 328 | int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) { |
| 329 | boringssl_ensure_ffdh_self_test(); |
| 330 |
| 331 | BN_CTX *ctx = BN_CTX_new(); |
| 332 | if (ctx == NULL) { |
| 333 | return -1; |
| 334 | } |
| 335 | BN_CTX_start(ctx); |
| 336 |
| 337 | int ret = -1; |
| 338 | BIGNUM *shared_key = BN_CTX_get(ctx); |
| 339 | if (shared_key && dh_compute_key(dh, shared_key, peers_key, ctx)) { |
| 340 | // A |BIGNUM|'s byte count fits in |int|. |
| 341 | ret = (int)BN_bn2bin(shared_key, out); |
| 342 | } |
| 343 |
| 344 | BN_CTX_end(ctx); |
| 345 | BN_CTX_free(ctx); |
| 346 | return ret; |
| 347 | } |
| 348 |
| 349 | int DH_compute_key_hashed(DH *dh, uint8_t *out, size_t *out_len, |
| 350 | size_t max_out_len, const BIGNUM *peers_key, |
| 351 | const EVP_MD *digest) { |
| 352 | *out_len = SIZE_MAX; |
| 353 |
| 354 | const size_t digest_len = EVP_MD_size(digest); |
| 355 | if (digest_len > max_out_len) { |
| 356 | return 0; |
| 357 | } |
| 358 |
| 359 | FIPS_service_indicator_lock_state(); |
| 360 |
| 361 | int ret = 0; |
| 362 | const size_t dh_len = DH_size(dh); |
| 363 | uint8_t *shared_bytes = reinterpret_cast<uint8_t *>(OPENSSL_malloc(dh_len)); |
| 364 | unsigned out_len_unsigned; |
| 365 | if (!shared_bytes || |
| 366 | // SP 800-56A is ambiguous about whether the output should be padded prior |
| 367 | // to revision three. But revision three, section C.1, awkwardly specifies |
| 368 | // padding to the length of p. |
| 369 | // |
| 370 | // Also, padded output avoids side-channels, so is always strongly |
| 371 | // advisable. |
| 372 | DH_compute_key_padded(shared_bytes, peers_key, dh) != (int)dh_len || |
| 373 | !EVP_Digest(shared_bytes, dh_len, out, &out_len_unsigned, digest, NULL) || |
| 374 | out_len_unsigned != digest_len) { |
| 375 | goto err; |
| 376 | } |
| 377 |
| 378 | *out_len = digest_len; |
| 379 | ret = 1; |
| 380 |
| 381 | err: |
| 382 | FIPS_service_indicator_unlock_state(); |
| 383 | OPENSSL_free(shared_bytes); |
| 384 | return ret; |
| 385 | } |
| 386 |
| 387 | int DH_size(const DH *dh) { return BN_num_bytes(dh->p); } |
| 388 |
| 389 | unsigned DH_num_bits(const DH *dh) { return BN_num_bits(dh->p); } |
| 390 |
| 391 | int DH_up_ref(DH *dh) { |
| 392 | CRYPTO_refcount_inc(&dh->references); |
| 393 | return 1; |
| 394 | } |
| 395 |
| 396 | DH *DH_get_rfc7919_2048(void) { |
| 397 | // This is the prime from https://tools.ietf.org/html/rfc7919#appendix-A.1, |
| 398 | // which is specifically approved for FIPS in appendix D of SP 800-56Ar3. |
| 399 | static const BN_ULONG kFFDHE2048Data[] = { |
| 400 | TOBN(0xffffffff, 0xffffffff), TOBN(0x886b4238, 0x61285c97), |
| 401 | TOBN(0xc6f34a26, 0xc1b2effa), TOBN(0xc58ef183, 0x7d1683b2), |
| 402 | TOBN(0x3bb5fcbc, 0x2ec22005), TOBN(0xc3fe3b1b, 0x4c6fad73), |
| 403 | TOBN(0x8e4f1232, 0xeef28183), TOBN(0x9172fe9c, 0xe98583ff), |
| 404 | TOBN(0xc03404cd, 0x28342f61), TOBN(0x9e02fce1, 0xcdf7e2ec), |
| 405 | TOBN(0x0b07a7c8, 0xee0a6d70), TOBN(0xae56ede7, 0x6372bb19), |
| 406 | TOBN(0x1d4f42a3, 0xde394df4), TOBN(0xb96adab7, 0x60d7f468), |
| 407 | TOBN(0xd108a94b, 0xb2c8e3fb), TOBN(0xbc0ab182, 0xb324fb61), |
| 408 | TOBN(0x30acca4f, 0x483a797a), TOBN(0x1df158a1, 0x36ade735), |
| 409 | TOBN(0xe2a689da, 0xf3efe872), TOBN(0x984f0c70, 0xe0e68b77), |
| 410 | TOBN(0xb557135e, 0x7f57c935), TOBN(0x85636555, 0x3ded1af3), |
| 411 | TOBN(0x2433f51f, 0x5f066ed0), TOBN(0xd3df1ed5, 0xd5fd6561), |
| 412 | TOBN(0xf681b202, 0xaec4617a), TOBN(0x7d2fe363, 0x630c75d8), |
| 413 | TOBN(0xcc939dce, 0x249b3ef9), TOBN(0xa9e13641, 0x146433fb), |
| 414 | TOBN(0xd8b9c583, 0xce2d3695), TOBN(0xafdc5620, 0x273d3cf1), |
| 415 | TOBN(0xadf85458, 0xa2bb4a9a), TOBN(0xffffffff, 0xffffffff), |
| 416 | }; |
| 417 |
| 418 | BIGNUM *const ffdhe2048_p = BN_new(); |
| 419 | BIGNUM *const ffdhe2048_q = BN_new(); |
| 420 | BIGNUM *const ffdhe2048_g = BN_new(); |
| 421 | DH *const dh = DH_new(); |
| 422 |
| 423 | if (!ffdhe2048_p || !ffdhe2048_q || !ffdhe2048_g || !dh) { |
| 424 | goto err; |
| 425 | } |
| 426 |
| 427 | bn_set_static_words(ffdhe2048_p, kFFDHE2048Data, |
| 428 | OPENSSL_ARRAY_SIZE(kFFDHE2048Data)); |
| 429 |
| 430 | if (!BN_rshift1(ffdhe2048_q, ffdhe2048_p) || !BN_set_word(ffdhe2048_g, 2) || |
| 431 | !DH_set0_pqg(dh, ffdhe2048_p, ffdhe2048_q, ffdhe2048_g)) { |
| 432 | goto err; |
| 433 | } |
| 434 |
| 435 | return dh; |
| 436 |
| 437 | err: |
| 438 | BN_free(ffdhe2048_p); |
| 439 | BN_free(ffdhe2048_q); |
| 440 | BN_free(ffdhe2048_g); |
| 441 | DH_free(dh); |
| 442 | return NULL; |
| 443 | } |
| 444 |