2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
13 #include <openssl/bn.h>
14 #include <openssl/objects.h>
15 #include "internal/bn_dh.h"
17 static DH *dh_param_init(const BIGNUM *p, int32_t nbits)
23 dh->g = (BIGNUM *)&_bignum_const_2;
28 DH *DH_new_by_nid(int nid)
32 return dh_param_init(&_bignum_ffdhe2048_p, 225);
34 return dh_param_init(&_bignum_ffdhe3072_p, 275);
36 return dh_param_init(&_bignum_ffdhe4096_p, 325);
38 return dh_param_init(&_bignum_ffdhe6144_p, 375);
40 return dh_param_init(&_bignum_ffdhe8192_p, 400);
42 DHerr(DH_F_DH_NEW_BY_NID, DH_R_INVALID_PARAMETER_NID);
47 int DH_get_nid(const DH *dh)
51 if (BN_get_word(dh->g) != 2)
53 if (!BN_cmp(dh->p, &_bignum_ffdhe2048_p))
55 else if (!BN_cmp(dh->p, &_bignum_ffdhe3072_p))
57 else if (!BN_cmp(dh->p, &_bignum_ffdhe4096_p))
59 else if (!BN_cmp(dh->p, &_bignum_ffdhe6144_p))
61 else if (!BN_cmp(dh->p, &_bignum_ffdhe8192_p))
66 BIGNUM *q = BN_dup(dh->p);
68 /* Check q = p * 2 + 1 we already know q is odd, so just shift right */
69 if (q == NULL || !BN_rshift1(q, q) || !BN_cmp(dh->q, q))