EVP_PKEY-DH.pod 8.7 KB

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