EVP_PKEY-DH.pod 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY-DH, EVP_PKEY-DHX, EVP_KEYMGMT-DH, EVP_KEYMGMT-DHX
  4. - EVP_PKEY DH and DHX keytype and algorithm support
  5. =head1 DESCRIPTION
  6. For B<DH> FFC key agreement, two classes of domain parameters can be used:
  7. "safe" domain parameters that are associated with approved named safe-prime
  8. groups, and a class of "FIPS186-type" domain parameters. FIPS186-type domain
  9. parameters should only be used for backward compatibility with existing
  10. applications that cannot be upgraded to use the approved safe-prime groups.
  11. See L<EVP_PKEY-FFC(7)> for more information about FFC keys.
  12. The B<DH> key type uses PKCS#3 format which saves I<p> and I<g>, but not the
  13. I<q> value.
  14. The B<DHX> key type uses X9.42 format which saves the value of I<q> and this
  15. must be used for FIPS186-4. If key validation is required, users should be aware
  16. of the nuances associated with FIPS186-4 style parameters as discussed in
  17. L</DH key validation>.
  18. =head2 DH and DHX domain parameters
  19. In addition to the common FCC parameters that all FFC keytypes should support
  20. (see L<EVP_PKEY-FFC(7)/FFC parameters>) the B<DHX> and B<DH> keytype
  21. implementations support the following:
  22. =over 4
  23. =item "group" (B<OSSL_PKEY_PARAM_GROUP_NAME>) <UTF8 string>
  24. Sets or gets a string that associates a B<DH> or B<DHX> named safe prime group
  25. with known values for I<p>, I<q> and I<g>.
  26. The following values can be used by the OpenSSL's default and FIPS providers:
  27. "ffdhe2048", "ffdhe3072", "ffdhe4096", "ffdhe6144", "ffdhe8192",
  28. "modp_2048", "modp_3072", "modp_4096", "modp_6144", "modp_8192".
  29. The following additional values can also be used by OpenSSL's default provider:
  30. "modp_1536", "dh_1024_160", "dh_2048_224", "dh_2048_256".
  31. DH/DHX named groups can be easily validated since the parameters are well known.
  32. For protocols that only transfer I<p> and I<g> the value of I<q> can also be
  33. retrieved.
  34. =back
  35. =head2 DH and DHX additional parameters
  36. =over 4
  37. =item "encoded-pub-key" (B<OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY>) <octet string>
  38. Used for getting and setting the encoding of the DH public key used in a key
  39. exchange message for the TLS protocol.
  40. See EVP_PKEY_set1_encoded_public_key() and EVP_PKEY_get1_encoded_public_key().
  41. =back
  42. =head2 DH additional domain parameters
  43. =over 4
  44. =item "safeprime-generator" (B<OSSL_PKEY_PARAM_DH_GENERATOR>) <integer>
  45. Used for DH generation of safe primes using the old safe prime generator code.
  46. The default value is 2.
  47. It is recommended to use a named safe prime group instead, if domain parameter
  48. validation is required.
  49. Randomly generated safe primes are not allowed by FIPS, so setting this value
  50. for the OpenSSL FIPS provider will instead choose a named safe prime group
  51. based on the size of I<p>.
  52. =back
  53. =head2 DH and DHX domain parameter / key generation parameters
  54. In addition to the common FFC key generation parameters that all FFC key types
  55. should support (see L<EVP_PKEY-FFC(7)/FFC key generation parameters>) the
  56. B<DH> and B<DHX> keytype implementation supports the following:
  57. =over 4
  58. =item "type" (B<OSSL_PKEY_PARAM_FFC_TYPE>) <UTF8 string>
  59. Sets the type of parameter generation. For B<DH> valid values are:
  60. =over 4
  61. =item "fips186_4"
  62. =item "default"
  63. =item "fips186_2"
  64. These are described in L<EVP_PKEY-FFC(7)/FFC key generation parameters>
  65. =item "group"
  66. This specifies that a named safe prime name will be chosen using the "pbits"
  67. type.
  68. =item "generator"
  69. A safe prime generator. See the "safeprime-generator" type above.
  70. This is only valid for B<DH> keys.
  71. =back
  72. =item "pbits" (B<OSSL_PKEY_PARAM_FFC_PBITS>) <unsigned integer>
  73. Sets the size (in bits) of the prime 'p'.
  74. For "fips186_4" this must be 2048.
  75. For "fips186_2" this must be 1024.
  76. For "group" this can be any one of 2048, 3072, 4096, 6144 or 8192.
  77. =item "priv_len" (B<OSSL_PKEY_PARAM_DH_PRIV_LEN>) <integer>
  78. An optional value to set the maximum length of the generated private key.
  79. The default value used if this is not set is the maximum value of
  80. BN_num_bits(I<q>)). The minimum value that this can be set to is 2 * s.
  81. Where s is the security strength of the key which has values of
  82. 112, 128, 152, 176 and 200 for key sizes of 2048, 3072, 4096, 6144 and 8192.
  83. =back
  84. =head2 DH key validation
  85. For B<DHX> that is not a named group the FIPS186-4 standard specifies that the
  86. values used for FFC parameter generation are also required for parameter
  87. validation. This means that optional FFC domain parameter values for
  88. I<seed>, I<pcounter> and I<gindex> or I<hindex> may need to be stored for
  89. validation purposes.
  90. For B<DHX> the I<seed> and I<pcounter> can be stored in ASN1 data
  91. (but the I<gindex> or I<hindex> cannot be stored). It is recommended to use a
  92. named safe prime group instead.
  93. For DH keys, L<EVP_PKEY_param_check(3)> behaves in the following way:
  94. The OpenSSL FIPS provider tests if the parameters are either an approved safe
  95. prime group OR that the FFC parameters conform to FIPS186-4 as defined in
  96. SP800-56Ar3 I<Assurances of Domain-Parameter Validity>.
  97. The OpenSSL default provider uses simpler checks that allows there to be no I<q>
  98. value for backwards compatibility.
  99. For DH keys, L<EVP_PKEY_param_check_quick(3)> is equivalent to
  100. L<EVP_PKEY_param_check(3)>.
  101. For DH keys, L<EVP_PKEY_public_check(3)> conforms to
  102. SP800-56Ar3 I<FFC Full Public-Key Validation>.
  103. For DH keys, L<EVP_PKEY_public_check_quick(3)> conforms to
  104. SP800-56Ar3 I<FFC Partial Public-Key Validation> when the
  105. DH key is an approved named safe prime group, otherwise it is the same as
  106. L<EVP_PKEY_public_check(3)>.
  107. For DH Keys, L<EVP_PKEY_private_check(3)> tests that the private key is in the
  108. correct range according to SP800-56Ar3. The OpenSSL FIPS provider requires the
  109. value of I<q> to be set (note that this is set for named safe prime groups).
  110. For backwards compatibility the OpenSSL default provider only requires I<p> to
  111. be set.
  112. For DH keys, L<EVP_PKEY_pairwise_check(3)> conforms to
  113. SP800-56Ar3 I<Owner Assurance of Pair-wise Consistency>.
  114. =head1 EXAMPLES
  115. An B<EVP_PKEY> context can be obtained by calling:
  116. EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
  117. A 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);
  122. params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
  123. /* "priv_len" is optional */
  124. params[1] = OSSL_PARAM_construct_int("priv_len", &priv_len);
  125. params[2] = OSSL_PARAM_construct_end();
  126. EVP_PKEY_keygen_init(pctx);
  127. EVP_PKEY_CTX_set_params(pctx, params);
  128. EVP_PKEY_generate(pctx, &pkey);
  129. ...
  130. EVP_PKEY_free(pkey);
  131. EVP_PKEY_CTX_free(pctx);
  132. B<DHX> domain parameters can be generated according to B<FIPS186-4> by calling:
  133. int gindex = 2;
  134. unsigned int pbits = 2048;
  135. unsigned int qbits = 256;
  136. OSSL_PARAM params[6];
  137. EVP_PKEY *param_key = NULL;
  138. EVP_PKEY_CTX *pctx = NULL;
  139. pctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL);
  140. EVP_PKEY_paramgen_init(pctx);
  141. params[0] = OSSL_PARAM_construct_uint("pbits", &pbits);
  142. params[1] = OSSL_PARAM_construct_uint("qbits", &qbits);
  143. params[2] = OSSL_PARAM_construct_int("gindex", &gindex);
  144. params[3] = OSSL_PARAM_construct_utf8_string("type", "fips186_4", 0);
  145. params[4] = OSSL_PARAM_construct_utf8_string("digest", "SHA256", 0);
  146. params[5] = OSSL_PARAM_construct_end();
  147. EVP_PKEY_CTX_set_params(pctx, params);
  148. EVP_PKEY_generate(pctx, &param_key);
  149. EVP_PKEY_print_params(bio_out, param_key, 0, NULL);
  150. ...
  151. EVP_PKEY_free(param_key);
  152. EVP_PKEY_CTX_free(pctx);
  153. A B<DH> key can be generated using domain parameters by calling:
  154. EVP_PKEY *key = NULL;
  155. EVP_PKEY_CTX *gctx = EVP_PKEY_CTX_new_from_pkey(NULL, param_key, NULL);
  156. EVP_PKEY_keygen_init(gctx);
  157. EVP_PKEY_generate(gctx, &key);
  158. EVP_PKEY_print_private(bio_out, key, 0, NULL);
  159. ...
  160. EVP_PKEY_free(key);
  161. EVP_PKEY_CTX_free(gctx);
  162. To validate B<FIPS186-4> B<DHX> domain parameters decoded from B<PEM> or
  163. B<DER> data, additional values used during generation may be required to
  164. be set into the key.
  165. EVP_PKEY_todata(), OSSL_PARAM_merge(), and EVP_PKEY_fromdata() are useful
  166. to add these parameters to the original key or domain parameters before
  167. the actual validation. In production code the return values should be checked.
  168. EVP_PKEY *received_domp = ...; /* parameters received and decoded */
  169. unsigned char *seed = ...; /* and additional parameters received */
  170. size_t seedlen = ...; /* by other means, required */
  171. int gindex = ...; /* for the validation */
  172. int pcounter = ...;
  173. int hindex = ...;
  174. OSSL_PARAM extra_params[4];
  175. OSSL_PARAM *domain_params = NULL;
  176. OSSL_PARAM *merged_params = NULL;
  177. EVP_PKEY_CTX *ctx = NULL, *validate_ctx = NULL;
  178. EVP_PKEY *complete_domp = NULL;
  179. EVP_PKEY_todata(received_domp, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  180. &domain_params);
  181. extra_params[0] = OSSL_PARAM_construct_octet_string("seed", seed, seedlen);
  182. /*
  183. * NOTE: For unverifiable g use "hindex" instead of "gindex"
  184. * extra_params[1] = OSSL_PARAM_construct_int("hindex", &hindex);
  185. */
  186. extra_params[1] = OSSL_PARAM_construct_int("gindex", &gindex);
  187. extra_params[2] = OSSL_PARAM_construct_int("pcounter", &pcounter);
  188. extra_params[3] = OSSL_PARAM_construct_end();
  189. merged_params = OSSL_PARAM_merge(domain_params, extra_params);
  190. ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL);
  191. EVP_PKEY_fromdata_init(ctx);
  192. EVP_PKEY_fromdata(ctx, &complete_domp, OSSL_KEYMGMT_SELECT_ALL,
  193. merged_params);
  194. validate_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, complete_domp, NULL);
  195. if (EVP_PKEY_param_check(validate_ctx) > 0)
  196. /* validation_passed(); */
  197. else
  198. /* validation_failed(); */
  199. OSSL_PARAM_free(domain_params);
  200. OSSL_PARAM_free(merged_params);
  201. EVP_PKEY_CTX_free(ctx);
  202. EVP_PKEY_CTX_free(validate_ctx);
  203. EVP_PKEY_free(complete_domp);
  204. =head1 CONFORMING TO
  205. =over 4
  206. =item RFC 7919 (TLS ffdhe named safe prime groups)
  207. =item RFC 3526 (IKE modp named safe prime groups)
  208. =item RFC 5114 (Additional DH named groups for dh_1024_160", "dh_2048_224"
  209. and "dh_2048_256").
  210. =back
  211. The following sections of SP800-56Ar3:
  212. =over 4
  213. =item 5.5.1.1 FFC Domain Parameter Selection/Generation
  214. =item Appendix D: FFC Safe-prime Groups
  215. =back
  216. The following sections of FIPS186-4:
  217. =over 4
  218. =item A.1.1.2 Generation of Probable Primes p and q Using an Approved Hash Function.
  219. =item A.2.3 Generation of canonical generator g.
  220. =item A.2.1 Unverifiable Generation of the Generator g.
  221. =back
  222. =head1 SEE ALSO
  223. L<EVP_PKEY-FFC(7)>,
  224. L<EVP_KEYEXCH-DH(7)>
  225. L<EVP_PKEY(3)>,
  226. L<provider-keymgmt(7)>,
  227. L<EVP_KEYMGMT(3)>,
  228. L<OSSL_PROVIDER-default(7)>,
  229. L<OSSL_PROVIDER-FIPS(7)>
  230. =head1 COPYRIGHT
  231. Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
  232. Licensed under the Apache License 2.0 (the "License"). You may not use
  233. this file except in compliance with the License. You can obtain a copy
  234. in the file LICENSE in the source distribution or at
  235. L<https://www.openssl.org/source/license.html>.
  236. =cut