]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man7/EVP_PKEY-DH.pod
Fixes related to separation of DH and DHX types
[thirdparty/openssl.git] / doc / man7 / EVP_PKEY-DH.pod
CommitLineData
b8086652
SL
1=pod
2
3=head1 NAME
4
f1ffaaee
SL
5EVP_PKEY-DH, EVP_PKEY-DHX, EVP_KEYMGMT-DH
6- EVP_PKEY DH and DHX keytype and algorithm support
b8086652
SL
7
8=head1 DESCRIPTION
9
10For B<DH> FFC key agreement, two classes of domain parameters can be used:
11"safe" domain parameters that are associated with approved named safe-prime
12groups, and a class of "FIPS 186-type" domain parameters. FIPS 186-type domain
13parameters should only be used for backward compatibility with existing
14applications that cannot be upgraded to use the approved safe-prime groups.
15
16See L<EVP_PKEY-FFC(7)> for more information about FFC keys.
17
f1ffaaee
SL
18The B<DH> key type uses PKCS#3 format which saves p and g, but not the 'q' value.
19The B<DHX> key type uses X9.42 format which saves the value of 'q' and this
20must be used for FIPS186-4.
21
22For B<DHX> that is not a named group the FIPS186-4 standard specifies that the
b8086652
SL
23values used for FFC parameter generation are also required for parameter
24validation. This means that optional FFC domain parameter values for
f1ffaaee
SL
25I<seed>, I<pcounter> and I<gindex> or I<hindex> may need to be stored for
26validation purposes.
27For B<DHX> the I<seed> and I<pcounter> can be stored in ASN1 data
28(but the I<gindex> or I<hindex> can not be stored).
b8086652 29
f1ffaaee 30=head2 DH and DHX domain parameters
b8086652
SL
31
32In addition to the common FCC parameters that all FFC keytypes should support
f1ffaaee
SL
33(see L<EVP_PKEY-FFC(7)/FFC parameters>) the B<DHX> and B<DH> keytype
34implementations support the following:
b8086652
SL
35
36=over 4
37
023b188c 38=item "group" (B<OSSL_PKEY_PARAM_GROUP_NAME>) <UTF8 string>
b8086652 39
f1ffaaee
SL
40Sets or gets a string that associates a B<DH> or B<DHX> named safe prime group
41with known values for I<p>, I<q> and I<g>.
b8086652
SL
42
43The following values can be used by the OpenSSL's default and FIPS providers:
44"ffdhe2048", "ffdhe3072", "ffdhe4096", "ffdhe6144", "ffdhe8192",
45"modp_2048", "modp_3072", "modp_4096", "modp_6144", "modp_8192".
46
47The following additional values can also be used by OpenSSL's default provider:
48"modp_1536", "dh_1024_160", "dh_2048_224", "dh_2048_256".
49
f1ffaaee 50DH/DHX named groups can be easily validated since the parameters are well known.
b8086652
SL
51For protocols that only transfer I<p> and I<g> the value of I<q> can also be
52retrieved.
53
f1ffaaee 54=back
b8086652 55
f1ffaaee 56=head2 DH and DHX additional parameters
b8086652 57
f1ffaaee 58=over 4
b8086652 59
5ac8fb58 60=item "encoded-pub-key" (B<OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY>) <octet string>
6a9bd929
MC
61
62Used for getting and setting the encoding of the DH public key used in a key
63exchange message for the TLS protocol.
f1ffaaee
SL
64See EVP_PKEY_set1_encoded_public_key() and EVP_PKEY_get1_encoded_public_key().
65
66=back
67
68=head2 DH additional domain parameters
69
70=over 4
71
72=item "safeprime-generator" (B<OSSL_PKEY_PARAM_DH_GENERATOR>) <integer>
73
74Used for DH generation of safe primes using the old safe prime generator code.
75The default value is 2.
76It is recommended to use a named safe prime group instead, if domain parameter
77validation is required.
78
79Randomly generated safe primes are not allowed by FIPS, so setting this value
80for the OpenSSL FIPS provider will instead choose a named safe prime group
81based on the size of I<p>.
6a9bd929 82
b8086652
SL
83=back
84
f1ffaaee 85=head2 DH and DHX domain parameter / key generation parameters
b8086652 86
05fa5fde
MC
87In addition to the common FFC key generation parameters that all FFC key types
88should support (see L<EVP_PKEY-FFC(7)/FFC key generation parameters>) the
f1ffaaee 89B<DH> and B<DHX> keytype implementation supports the following:
b8086652
SL
90
91=over 4
92
93=item "type" (B<OSSL_PKEY_PARAM_FFC_TYPE>) <utf8_string>
94
95Sets the type of parameter generation. For B<DH> valid values are:
96
97=over 4
98
99=item "fips186_4"
100
101=item "default"
102
103=item "fips186_2"
104
105These are described in L<EVP_PKEY-FFC(7)/FFC key generation parameters>
106
107=item "group"
108
109This specifies that a named safe prime name will be chosen using the "pbits"
110type.
111
112=item "generator"
113
114A safe prime generator. See the "safeprime-generator" type above.
f1ffaaee 115This is only valid for B<DH> keys.
b8086652
SL
116
117=back
118
119=item "pbits" (B<OSSL_PKEY_PARAM_FFC_PBITS>) <unsigned integer>
120
121Sets the size (in bits) of the prime 'p'.
122
123For "fips186_4" this must be 2048.
124For "fips186_2" this must be 1024.
125For "group" this can be any one of 2048, 3072, 4096, 6144 or 8192.
126
127=item "priv_len" (B<OSSL_PKEY_PARAM_DH_PRIV_LEN>) <integer>
128
129An optional value to set the maximum length of the generated private key.
8c1cbc72 130The default value used if this is not set is the maximum value of
b8086652
SL
131BN_num_bits(I<q>)). The minimum value that this can be set to is 2 * s.
132Where s is the security strength of the key which has values of
133112, 128, 152, 176 and 200 for key sizes of 2048, 3072, 4096, 6144 and 8192.
134
135=back
136
137=head1 EXAMPLES
138
139An B<EVP_PKEY> context can be obtained by calling:
140
141 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
142
185e1aa2 143A B<DH> key can be generated with a named safe prime group by calling:
b8086652
SL
144
145 int priv_len = 2 * 112;
146 OSSL_PARAM params[3];
147 EVP_PKEY *pkey = NULL;
148 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
149
150 params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
151 /* "priv_len" is optional */
152 params[1] = OSSL_PARAM_construct_int("priv_len", &priv_len);
153 params[2] = OSSL_PARAM_construct_end();
154
155 EVP_PKEY_keygen_init(pctx);
156 EVP_PKEY_CTX_set_params(pctx, params);
157 EVP_PKEY_gen(pctx, &pkey);
158 ...
159 EVP_PKEY_free(key);
160 EVP_PKEY_CTX_free(pctx);
161
185e1aa2 162B<DHX> domain parameters can be generated according to B<FIPS 186-4> by calling:
05fa5fde 163
b8086652
SL
164 unsigned int pbits = 2048;
165 unsigned int qbits = 256;
b8086652
SL
166 OSSL_PARAM params[5];
167 EVP_PKEY *param_key = NULL;
168 EVP_PKEY_CTX *pctx = NULL;
169
185e1aa2 170 pctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL);
b8086652 171 EVP_PKEY_paramgen_init(pctx);
2edb571b 172
b8086652
SL
173 params[0] = OSSL_PARAM_construct_uint("pbits", &pbits);
174 params[1] = OSSL_PARAM_construct_uint("qbits", &qbits);
185e1aa2
TM
175 params[2] = OSSL_PARAM_construct_utf8_string("type", "fips186_4", 0);
176 params[3] = OSSL_PARAM_construct_utf8_string("digest", "SHA256", 0);
b8086652
SL
177 params[4] = OSSL_PARAM_construct_end();
178 EVP_PKEY_CTX_set_params(pctx, params);
179
180 EVP_PKEY_gen(pctx, &param_key);
181
182 EVP_PKEY_print_params(bio_out, param_key, 0, NULL);
183 ...
184 EVP_PKEY_free(param_key);
185 EVP_PKEY_CTX_free(pctx);
186
185e1aa2 187A B<DH> key can be generated using domain parameters by calling:
b8086652
SL
188
189 EVP_PKEY *key = NULL;
190 EVP_PKEY_CTX *gctx = EVP_PKEY_CTX_new_from_pkey(NULL, param_key, NULL);
191
192 EVP_PKEY_keygen_init(gctx);
193 EVP_PKEY_gen(gctx, &key);
194 EVP_PKEY_print_private(bio_out, key, 0, NULL);
195 ...
196 EVP_PKEY_free(key);
197 EVP_PKEY_CTX_free(gctx);
198
185e1aa2
TM
199To validate B<FIPS 186-4> B<DHX> domain parameters decoded from B<PEM> or
200B<DER> data, additional values used during generation may be required to
201be set into the key.
202
203EVP_PKEY_todata(), OSSL_PARAM_merge(), and EVP_PKEY_fromdata() are useful
204to add these parameters to the original key or domain parameters before
205the actual validation.
206
207 EVP_PKEY *received_domp = ...; /* parameters received and decoded */
208 unsigned char *seed = ...; /* and additional parameters received */
209 size_t seedlen = ...; /* by other means, required */
210 int gindex = ...; /* for the validation */
211 int pcounter = ...;
212 int hindex = ...;
213 OSSL_PARAM extra_params[5];
214 OSSL_PARAM *domain_params = NULL;
215 OSSL_PARAM *merged_params = NULL;
216 EVP_PKEY_CTX *ctx = NULL, *validate_ctx = NULL;
217 EVP_PKEY *complete_domp = NULL;
218
219 EVP_PKEY_todata(received_domp, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
220 &domain_params);
221 extra_params[0] = OSSL_PARAM_construct_octet_string("seed", seed, seedlen);
222 extra_params[1] = OSSL_PARAM_construct_int("gindex", &gindex);
223 extra_params[2] = OSSL_PARAM_construct_int("pcounter", &pcounter);
224 extra_params[3] = OSSL_PARAM_construct_int("hindex", &hindex);
225 extra_params[4] = OSSL_PARAM_construct_end();
226 merged_params = OSSL_PARAM_merge(domain_params, extra_params);
227
228 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL);
229 EVP_PKEY_fromdata_init(ctx);
230 EVP_PKEY_fromdata(ctx, &complete_domp, OSSL_KEYMGMT_SELECT_ALL,
231 merged_params);
232
233 validate_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, complete_domp, NULL);
234 if (EVP_PKEY_param_check(validate_ctx) > 0)
235 /* validation_passed(); */
236 else
237 /* validation_failed(); */
238
239 OSSL_PARAM_free(domain_params);
240 OSSL_PARAM_free(merged_params);
241 EVP_PKEY_CTX_free(ctx);
242 EVP_PKEY_CTX_free(validate_ctx);
243 EVP_PKEY_free(complete_domp);
b8086652
SL
244
245=head1 CONFORMING TO
246
247=over 4
248
249=item RFC 7919 (TLS ffdhe named safe prime groups)
250
251=item RFC 3526 (IKE modp named safe prime groups)
252
253=item RFC 5114 (Additional DH named groups for dh_1024_160", "dh_2048_224"
254 and "dh_2048_256").
255
256=back
257
258The following sections of SP800-56Ar3:
259
260=over 4
261
262=item 5.5.1.1 FFC Domain Parameter Selection/Generation
263
264=item Appendix D: FFC Safe-prime Groups
265
266=back
267
268The following sections of FIPS 186-4:
269
270=over 4
271
272=item A.1.1.2 Generation of Probable Primes p and q Using an Approved Hash Function.
273
274=item A.2.3 Generation of canonical generator g.
275
276=item A.2.1 Unverifiable Generation of the Generator g.
277
278=back
279
280=head1 SEE ALSO
281
282L<EVP_PKEY-FFC(7)>,
283L<EVP_KEYEXCH-DH(7)>
284L<EVP_PKEY(3)>,
285L<provider-keymgmt(7)>,
286L<EVP_KEYMGMT(3)>,
287L<OSSL_PROVIDER-default(7)>,
288L<OSSL_PROVIDER-FIPS(7)>
289
290=head1 COPYRIGHT
291
f5afac4b 292Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
b8086652
SL
293
294Licensed under the Apache License 2.0 (the "License"). You may not use
295this file except in compliance with the License. You can obtain a copy
296in the file LICENSE in the source distribution or at
297L<https://www.openssl.org/source/license.html>.
298
299=cut