2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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;
29 DH *DH_new_by_nid(int nid)
33 return dh_param_init(&_bignum_ffdhe2048_p, 225);
35 return dh_param_init(&_bignum_ffdhe3072_p, 275);
37 return dh_param_init(&_bignum_ffdhe4096_p, 325);
39 return dh_param_init(&_bignum_ffdhe6144_p, 375);
41 return dh_param_init(&_bignum_ffdhe8192_p, 400);
43 DHerr(DH_F_DH_NEW_BY_NID, DH_R_INVALID_PARAMETER_NID);
48 int DH_get_nid(const DH *dh)
52 if (BN_get_word(dh->g) != 2)
54 if (!BN_cmp(dh->p, &_bignum_ffdhe2048_p))
56 else if (!BN_cmp(dh->p, &_bignum_ffdhe3072_p))
58 else if (!BN_cmp(dh->p, &_bignum_ffdhe4096_p))
60 else if (!BN_cmp(dh->p, &_bignum_ffdhe6144_p))
62 else if (!BN_cmp(dh->p, &_bignum_ffdhe8192_p))
67 BIGNUM *q = BN_dup(dh->p);
69 /* Check q = p * 2 + 1 we already know q is odd, so just shift right */
70 if (q == NULL || !BN_rshift1(q, q) || !BN_cmp(dh->q, q))