EVP_KEYEXCH-DH.7ossl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "EVP_KEYEXCH-DH 7ossl"
  58. .TH EVP_KEYEXCH-DH 7ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. EVP_KEYEXCH\-DH
  65. \&\- DH Key Exchange algorithm support
  66. .SH DESCRIPTION
  67. .IX Header "DESCRIPTION"
  68. Key exchange support for the \fBDH\fR and \fBDHX\fR key types.
  69. .PP
  70. Please note that although both key types support the same key exchange
  71. operations, they cannot be used together in a single key exchange. It
  72. is not possible to use a private key of the \fBDH\fR type in key exchange
  73. with the public key of \fBDHX\fR type and vice versa.
  74. .SS "DH and DHX key exchange parameters"
  75. .IX Subsection "DH and DHX key exchange parameters"
  76. .IP """pad"" (\fBOSSL_EXCHANGE_PARAM_PAD\fR) <unsigned integer>" 4
  77. .IX Item """pad"" (OSSL_EXCHANGE_PARAM_PAD) <unsigned integer>"
  78. Sets the padding mode for the associated key exchange ctx.
  79. Setting a value of 1 will turn padding on.
  80. Setting a value of 0 will turn padding off.
  81. If padding is off then the derived shared secret may be smaller than the
  82. largest possible secret size.
  83. If padding is on then the derived shared secret will have its first bytes
  84. filled with zeros where necessary to make the shared secret the same size as
  85. the largest possible secret size.
  86. The padding mode parameter is ignored (and padding implicitly enabled) when
  87. the KDF type is set to "X942KDF\-ASN1" (\fBOSSL_KDF_NAME_X942KDF_ASN1\fR).
  88. .IP """kdf-type"" (\fBOSSL_EXCHANGE_PARAM_KDF_TYPE\fR) <UTF8 string>" 4
  89. .IX Item """kdf-type"" (OSSL_EXCHANGE_PARAM_KDF_TYPE) <UTF8 string>"
  90. .PD 0
  91. .IP """kdf-digest"" (\fBOSSL_EXCHANGE_PARAM_KDF_DIGEST\fR) <UTF8 string>" 4
  92. .IX Item """kdf-digest"" (OSSL_EXCHANGE_PARAM_KDF_DIGEST) <UTF8 string>"
  93. .IP """kdf-digest-props"" (\fBOSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS\fR) <UTF8 string>" 4
  94. .IX Item """kdf-digest-props"" (OSSL_EXCHANGE_PARAM_KDF_DIGEST_PROPS) <UTF8 string>"
  95. .IP """kdf-outlen"" (\fBOSSL_EXCHANGE_PARAM_KDF_OUTLEN\fR) <unsigned integer>" 4
  96. .IX Item """kdf-outlen"" (OSSL_EXCHANGE_PARAM_KDF_OUTLEN) <unsigned integer>"
  97. .IP """kdf-ukm"" (\fBOSSL_EXCHANGE_PARAM_KDF_UKM\fR) <octet string>" 4
  98. .IX Item """kdf-ukm"" (OSSL_EXCHANGE_PARAM_KDF_UKM) <octet string>"
  99. .IP """fips-indicator"" (\fBOSSL_EXCHANGE_PARAM_FIPS_APPROVED_INDICATOR\fR) <integer>" 4
  100. .IX Item """fips-indicator"" (OSSL_EXCHANGE_PARAM_FIPS_APPROVED_INDICATOR) <integer>"
  101. .IP """key-check"" (\fBOSSL_EXCHANGE_PARAM_FIPS_KEY_CHECK\fR) <integer>" 4
  102. .IX Item """key-check"" (OSSL_EXCHANGE_PARAM_FIPS_KEY_CHECK) <integer>"
  103. .IP """digest-check"" (\fBOSSL_EXCHANGE_PARAM_FIPS_DIGEST_CHECK\fR) <integer>" 4
  104. .IX Item """digest-check"" (OSSL_EXCHANGE_PARAM_FIPS_DIGEST_CHECK) <integer>"
  105. .PD
  106. See "Common Key Exchange parameters" in \fBprovider\-keyexch\fR\|(7).
  107. .IP """cekalg"" (\fBOSSL_KDF_PARAM_CEK_ALG\fR) <octet string ptr>" 4
  108. .IX Item """cekalg"" (OSSL_KDF_PARAM_CEK_ALG) <octet string ptr>"
  109. See "KDF Parameters" in \fBprovider\-kdf\fR\|(7).
  110. .SH EXAMPLES
  111. .IX Header "EXAMPLES"
  112. The examples assume a host and peer both generate keys using the same
  113. named group (or domain parameters). See "Examples" in \fBEVP_PKEY\-DH\fR\|(7).
  114. Both the host and peer transfer their public key to each other.
  115. .PP
  116. To convert the peer's generated key pair to a public key in DER format in order
  117. to transfer to the host:
  118. .PP
  119. .Vb 3
  120. \& EVP_PKEY *peer_key; /* It is assumed this contains the peers generated key */
  121. \& unsigned char *peer_pub_der = NULL;
  122. \& int peer_pub_der_len;
  123. \&
  124. \& peer_pub_der_len = i2d_PUBKEY(peer_key, &peer_pub_der);
  125. \& ...
  126. \& OPENSSL_free(peer_pub_der);
  127. .Ve
  128. .PP
  129. To convert the received peer's public key from DER format on the host:
  130. .PP
  131. .Vb 4
  132. \& const unsigned char *pd = peer_pub_der;
  133. \& EVP_PKEY *peer_pub_key = d2i_PUBKEY(NULL, &pd, peer_pub_der_len);
  134. \& ...
  135. \& EVP_PKEY_free(peer_pub_key);
  136. .Ve
  137. .PP
  138. To derive a shared secret on the host using the host's key and the peer's public
  139. key:
  140. .PP
  141. .Vb 8
  142. \& /* It is assumed that the host_key and peer_pub_key are set up */
  143. \& void derive_secret(EVP_KEY *host_key, EVP_PKEY *peer_pub_key)
  144. \& {
  145. \& unsigned int pad = 1;
  146. \& OSSL_PARAM params[2];
  147. \& unsigned char *secret = NULL;
  148. \& size_t secret_len = 0;
  149. \& EVP_PKEY_CTX *dctx = EVP_PKEY_CTX_new_from_pkey(NULL, host_key, NULL);
  150. \&
  151. \& EVP_PKEY_derive_init(dctx);
  152. \&
  153. \& /* Optionally set the padding */
  154. \& params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &pad);
  155. \& params[1] = OSSL_PARAM_construct_end();
  156. \& EVP_PKEY_CTX_set_params(dctx, params);
  157. \&
  158. \& EVP_PKEY_derive_set_peer(dctx, peer_pub_key);
  159. \&
  160. \& /* Get the size by passing NULL as the buffer */
  161. \& EVP_PKEY_derive(dctx, NULL, &secret_len);
  162. \& secret = OPENSSL_zalloc(secret_len);
  163. \&
  164. \& EVP_PKEY_derive(dctx, secret, &secret_len);
  165. \& ...
  166. \& OPENSSL_clear_free(secret, secret_len);
  167. \& EVP_PKEY_CTX_free(dctx);
  168. \& }
  169. .Ve
  170. .PP
  171. Very similar code can be used by the peer to derive the same shared secret
  172. using the host's public key and the peer's generated key pair.
  173. .SH "SEE ALSO"
  174. .IX Header "SEE ALSO"
  175. \&\fBEVP_PKEY\-DH\fR\|(7),
  176. \&\fBEVP_PKEY\-FFC\fR\|(7),
  177. \&\fBEVP_PKEY\fR\|(3),
  178. \&\fBprovider\-keyexch\fR\|(7),
  179. \&\fBprovider\-keymgmt\fR\|(7),
  180. \&\fBOSSL_PROVIDER\-default\fR\|(7),
  181. \&\fBOSSL_PROVIDER\-FIPS\fR\|(7),
  182. .SH COPYRIGHT
  183. .IX Header "COPYRIGHT"
  184. Copyright 2020\-2024 The OpenSSL Project Authors. All Rights Reserved.
  185. .PP
  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. <https://www.openssl.org/source/license.html>.