Add support for DH 'modp' group parameters (RFC 3526)
[openssl.git] / crypto / dh / dh_group_params.c
1 /*
2  * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /* DH parameters from RFC7919 and RFC3526 */
11
12 #include <stdio.h>
13 #include "internal/cryptlib.h"
14 #include "dh_local.h"
15 #include <openssl/bn.h>
16 #include <openssl/objects.h>
17 #include "crypto/bn_dh.h"
18
19 static DH *dh_param_init(int nid, const BIGNUM *p, int32_t nbits)
20 {
21     BIGNUM *q = NULL;
22     DH *dh = DH_new();
23
24     if (dh == NULL)
25         return NULL;
26
27     q = BN_dup(p);
28     /* Set q = (p - 1) / 2 (p is known to be odd so just shift right ) */
29     if (q == NULL || !BN_rshift1(q, q)) {
30         BN_free(q);
31         DH_free(dh);
32         return NULL;
33     }
34     dh->params.nid = nid;
35     dh->params.p = (BIGNUM *)p;
36     dh->params.q = (BIGNUM *)q;
37     dh->params.g = (BIGNUM *)&_bignum_const_2;
38     /* Private key length = 2 * max_target_security_strength */
39     dh->length = nbits;
40     dh->dirty_cnt++;
41     return dh;
42 }
43
44 DH *DH_new_by_nid(int nid)
45 {
46     /*
47      * The last parameter specified in these fields is
48      * 2 * max_target_security_strength.
49      * See SP800-56Ar3 Table(s) 25 & 26.
50      */
51     switch (nid) {
52     case NID_ffdhe2048:
53         return dh_param_init(nid, &_bignum_ffdhe2048_p, 225);
54     case NID_ffdhe3072:
55         return dh_param_init(nid, &_bignum_ffdhe3072_p, 275);
56     case NID_ffdhe4096:
57         return dh_param_init(nid, &_bignum_ffdhe4096_p, 325);
58     case NID_ffdhe6144:
59         return dh_param_init(nid, &_bignum_ffdhe6144_p, 375);
60     case NID_ffdhe8192:
61         return dh_param_init(nid, &_bignum_ffdhe8192_p, 400);
62 #ifndef FIPS_MODE
63     case NID_modp_1536:
64         return dh_param_init(nid, &_bignum_modp_1536_p, 190);
65 #endif
66     case NID_modp_2048:
67         return dh_param_init(nid, &_bignum_modp_2048_p, 225);
68     case NID_modp_3072:
69         return dh_param_init(nid, &_bignum_modp_3072_p, 275);
70     case NID_modp_4096:
71         return dh_param_init(nid, &_bignum_modp_4096_p, 325);
72     case NID_modp_6144:
73         return dh_param_init(nid, &_bignum_modp_6144_p, 375);
74     case NID_modp_8192:
75         return dh_param_init(nid, &_bignum_modp_8192_p, 400);
76     default:
77         DHerr(DH_F_DH_NEW_BY_NID, DH_R_INVALID_PARAMETER_NID);
78         return NULL;
79     }
80 }
81
82 int DH_get_nid(DH *dh)
83 {
84     int nid = dh->params.nid;
85
86     if (nid != NID_undef)
87         return nid;
88
89     if (BN_get_word(dh->params.g) != 2)
90         return NID_undef;
91     if (!BN_cmp(dh->params.p, &_bignum_ffdhe2048_p))
92         nid = NID_ffdhe2048;
93     else if (!BN_cmp(dh->params.p, &_bignum_ffdhe3072_p))
94         nid = NID_ffdhe3072;
95     else if (!BN_cmp(dh->params.p, &_bignum_ffdhe4096_p))
96         nid = NID_ffdhe4096;
97     else if (!BN_cmp(dh->params.p, &_bignum_ffdhe6144_p))
98         nid = NID_ffdhe6144;
99     else if (!BN_cmp(dh->params.p, &_bignum_ffdhe8192_p))
100         nid = NID_ffdhe8192;
101 #ifndef FIPS_MODE
102     else if (!BN_cmp(dh->params.p, &_bignum_modp_1536_p))
103         nid = NID_modp_1536;
104 #endif
105     else if (!BN_cmp(dh->params.p, &_bignum_modp_2048_p))
106         nid = NID_modp_2048;
107     else if (!BN_cmp(dh->params.p, &_bignum_modp_3072_p))
108         nid = NID_modp_3072;
109     else if (!BN_cmp(dh->params.p, &_bignum_modp_4096_p))
110         nid = NID_modp_4096;
111     else if (!BN_cmp(dh->params.p, &_bignum_modp_6144_p))
112         nid = NID_modp_6144;
113     else if (!BN_cmp(dh->params.p, &_bignum_modp_8192_p))
114         nid = NID_modp_8192;
115     else
116         return NID_undef;
117
118     /* Verify q is correct if it exists - reset the nid if it is not correct */
119     if (dh->params.q != NULL) {
120         BIGNUM *q = BN_dup(dh->params.p);
121
122         /* Check q = p * 2 + 1 we already know q is odd, so just shift right */
123         if (q == NULL || !BN_rshift1(q, q) || (BN_cmp(dh->params.q, q) != 0))
124             nid = NID_undef;
125         BN_free(q);
126     }
127     dh->params.nid = nid; /* cache the nid */
128     return nid;
129 }