5 EVP_PKEY-DH, EVP_KEYMGMT-DH - EVP_PKEY DH keytype and algorithm support
9 For B<DH> FFC key agreement, two classes of domain parameters can be used:
10 "safe" domain parameters that are associated with approved named safe-prime
11 groups, and a class of "FIPS 186-type" domain parameters. FIPS 186-type domain
12 parameters should only be used for backward compatibility with existing
13 applications that cannot be upgraded to use the approved safe-prime groups.
15 See L<EVP_PKEY-FFC(7)> for more information about FFC keys.
17 For B<DH> that is not a named group) the FIPS186-4 standard specifies that the
18 values used for FFC parameter generation are also required for parameter
19 validation. This means that optional FFC domain parameter values for
20 I<seed>, I<pcounter> and I<gindex> may need to be stored for validation purposes.
21 For B<DH> the I<seed> and I<pcounter> can be stored in ASN1 data
22 (but the I<gindex> is not).
26 In addition to the common FCC parameters that all FFC keytypes should support
27 (see L<EVP_PKEY-FFC(7)/FFC parameters>)) the B<DH> keytype
28 implementation supports the following:
32 =item "group" (B<OSSL_PKEY_PARAM_DH_GROUP>) <UTF8 string>
34 Set or gets a string that associates a B<DH> named safe prime group with known
35 values for I<p>, I<q> and I<g>.
37 The following values can be used by the OpenSSL's default and FIPS providers:
38 "ffdhe2048", "ffdhe3072", "ffdhe4096", "ffdhe6144", "ffdhe8192",
39 "modp_2048", "modp_3072", "modp_4096", "modp_6144", "modp_8192".
41 The following additional values can also be used by OpenSSL's default provider:
42 "modp_1536", "dh_1024_160", "dh_2048_224", "dh_2048_256".
44 DH named groups can be easily validated since the parameters are well known.
45 For protocols that only transfer I<p> and I<g> the value of I<q> can also be
48 =item "safeprime-generator" (B<OSSL_PKEY_PARAM_DH_GENERATOR>) <integer>
50 Used for DH generation of safe primes using the old generator code.
51 It is recommended to use a named safe prime group instead, if domain parameter
52 validation is required. The default value is 2.
54 These are not named safe prime groups so setting this value for the OpenSSL FIPS
55 provider will instead choose a named safe prime group based on the size of I<p>.
59 =head2 DH domain parameter / key generation parameters
61 In addition to the common FCC key generation parameters that all FFC key types
62 should support (see L<EVP_PKEY-FFC(7)/FFC key generation parameters>)) the
63 B<DH> keytype implementation supports the following:
67 =item "type" (B<OSSL_PKEY_PARAM_FFC_TYPE>) <utf8_string>
69 Sets the type of parameter generation. For B<DH> valid values are:
79 These are described in L<EVP_PKEY-FFC(7)/FFC key generation parameters>
83 This specifies that a named safe prime name will be chosen using the "pbits"
88 A safe prime generator. See the "safeprime-generator" type above.
92 =item "pbits" (B<OSSL_PKEY_PARAM_FFC_PBITS>) <unsigned integer>
94 Sets the size (in bits) of the prime 'p'.
96 For "fips186_4" this must be 2048.
97 For "fips186_2" this must be 1024.
98 For "group" this can be any one of 2048, 3072, 4096, 6144 or 8192.
100 =item "priv_len" (B<OSSL_PKEY_PARAM_DH_PRIV_LEN>) <integer>
102 An optional value to set the maximum length of the generated private key.
103 The default valure used if this is not set is the maximum value of
104 BN_num_bits(I<q>)). The minimum value that this can be set to is 2 * s.
105 Where s is the security strength of the key which has values of
106 112, 128, 152, 176 and 200 for key sizes of 2048, 3072, 4096, 6144 and 8192.
112 An B<EVP_PKEY> context can be obtained by calling:
114 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
116 An B<DH> key can be generated with a named safe prime group by calling:
118 int priv_len = 2 * 112;
119 OSSL_PARAM params[3];
120 EVP_PKEY *pkey = NULL;
121 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
123 params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
124 /* "priv_len" is optional */
125 params[1] = OSSL_PARAM_construct_int("priv_len", &priv_len);
126 params[2] = OSSL_PARAM_construct_end();
128 EVP_PKEY_keygen_init(pctx);
129 EVP_PKEY_CTX_set_params(pctx, params);
130 EVP_PKEY_gen(pctx, &pkey);
133 EVP_PKEY_CTX_free(pctx);
135 Legacy B<DH> domain parameters can be generated by calling:
136 unsigned int pbits = 2048;
137 unsigned int qbits = 256;
139 OSSL_PARAM params[5];
140 EVP_PKEY *param_key = NULL;
141 EVP_PKEY_CTX *pctx = NULL;
143 pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
144 EVP_PKEY_paramgen_init(pctx);
146 params[0] = OSSL_PARAM_construct_uint("pbits", &pbits);
147 params[1] = OSSL_PARAM_construct_uint("qbits", &qbits);
148 params[2] = OSSL_PARAM_construct_int("gindex", &gindex);
149 params[3] = OSSL_PARAM_construct_utf8_string("digest", "SHA384", 0);
150 params[4] = OSSL_PARAM_construct_end();
151 EVP_PKEY_CTX_set_params(pctx, params);
153 EVP_PKEY_gen(pctx, ¶m_key);
155 EVP_PKEY_print_params(bio_out, param_key, 0, NULL);
157 EVP_PKEY_free(param_key);
158 EVP_PKEY_CTX_free(pctx);
160 An B<DH> key can be generated using domain parameters by calling:
162 EVP_PKEY *key = NULL;
163 EVP_PKEY_CTX *gctx = EVP_PKEY_CTX_new_from_pkey(NULL, param_key, NULL);
165 EVP_PKEY_keygen_init(gctx);
166 EVP_PKEY_gen(gctx, &key);
167 EVP_PKEY_print_private(bio_out, key, 0, NULL);
170 EVP_PKEY_CTX_free(gctx);
172 =for comment TODO(3.0): To validate domain parameters, additional values used
173 during generation may be required to be set into the key.
179 =item RFC 7919 (TLS ffdhe named safe prime groups)
181 =item RFC 3526 (IKE modp named safe prime groups)
183 =item RFC 5114 (Additional DH named groups for dh_1024_160", "dh_2048_224"
188 The following sections of SP800-56Ar3:
192 =item 5.5.1.1 FFC Domain Parameter Selection/Generation
194 =item Appendix D: FFC Safe-prime Groups
198 The following sections of FIPS 186-4:
202 =item A.1.1.2 Generation of Probable Primes p and q Using an Approved Hash Function.
204 =item A.2.3 Generation of canonical generator g.
206 =item A.2.1 Unverifiable Generation of the Generator g.
215 L<provider-keymgmt(7)>,
217 L<OSSL_PROVIDER-default(7)>,
218 L<OSSL_PROVIDER-FIPS(7)>
222 Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
224 Licensed under the Apache License 2.0 (the "License"). You may not use
225 this file except in compliance with the License. You can obtain a copy
226 in the file LICENSE in the source distribution or at
227 L<https://www.openssl.org/source/license.html>.