EVP_PKEY_encapsulate.3ossl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_PKEY_ENCAPSULATE 3ossl"
  58. .TH EVP_PKEY_ENCAPSULATE 3ossl 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_PKEY_encapsulate_init, EVP_PKEY_auth_encapsulate_init, EVP_PKEY_encapsulate
  65. \&\- Key encapsulation using a KEM algorithm with a public key
  66. .SH SYNOPSIS
  67. .IX Header "SYNOPSIS"
  68. .Vb 1
  69. \& #include <openssl/evp.h>
  70. \&
  71. \& int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
  72. \& int EVP_PKEY_auth_encapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpriv,
  73. \& const OSSL_PARAM params[]);
  74. \& int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx,
  75. \& unsigned char *wrappedkey, size_t *wrappedkeylen,
  76. \& unsigned char *genkey, size_t *genkeylen);
  77. .Ve
  78. .SH DESCRIPTION
  79. .IX Header "DESCRIPTION"
  80. The \fBEVP_PKEY_encapsulate_init()\fR function initializes a public key algorithm
  81. context \fIctx\fR for an encapsulation operation and then sets the \fIparams\fR
  82. on the context in the same way as calling \fBEVP_PKEY_CTX_set_params\fR\|(3).
  83. Note that \fIctx\fR is usually is produced using \fBEVP_PKEY_CTX_new_from_pkey\fR\|(3),
  84. specifying the public key to use.
  85. .PP
  86. The \fBEVP_PKEY_auth_encapsulate_init()\fR function is similar to
  87. \&\fBEVP_PKEY_encapsulate_init()\fR but also passes an \fIauthpriv\fR authentication private
  88. key that is used during encapsulation.
  89. .PP
  90. The \fBEVP_PKEY_encapsulate()\fR function performs a public key encapsulation
  91. operation using \fIctx\fR.
  92. The symmetric secret generated in \fIgenkey\fR can be used as key material.
  93. The ciphertext in \fIwrappedkey\fR is its encapsulated form, which can be sent
  94. to another party, who can use \fBEVP_PKEY_decapsulate\fR\|(3) to retrieve it
  95. using their private key.
  96. If \fIwrappedkey\fR is NULL then the maximum size of the output buffer
  97. is written to the \fI*wrappedkeylen\fR parameter unless \fIwrappedkeylen\fR is NULL
  98. and the maximum size of the generated key buffer is written to \fI*genkeylen\fR
  99. unless \fIgenkeylen\fR is NULL.
  100. If \fIwrappedkey\fR is not NULL and the call is successful then the
  101. internally generated key is written to \fIgenkey\fR and its size is written to
  102. \&\fI*genkeylen\fR. The encapsulated version of the generated key is written to
  103. \&\fIwrappedkey\fR and its size is written to \fI*wrappedkeylen\fR. Note that if
  104. \&\fIwrappedlen\fR is not NULL, then the value it points to must initially hold the size of
  105. the \fIunwrapped\fR buffer so that its size can be validated by the call, ensuring
  106. it is large enough to hold the result written to \fIwrapped\fR.
  107. .SH NOTES
  108. .IX Header "NOTES"
  109. After the call to \fBEVP_PKEY_encapsulate_init()\fR algorithm-specific parameters
  110. for the operation may be set or modified using \fBEVP_PKEY_CTX_set_params\fR\|(3).
  111. .SH "RETURN VALUES"
  112. .IX Header "RETURN VALUES"
  113. \&\fBEVP_PKEY_encapsulate_init()\fR, \fBEVP_PKEY_auth_encapsulate_init()\fR and
  114. \&\fBEVP_PKEY_encapsulate()\fR return 1 for success and 0 or a negative value for
  115. failure. In particular a return value of \-2 indicates the operation is not
  116. supported by the public key algorithm.
  117. .SH EXAMPLES
  118. .IX Header "EXAMPLES"
  119. Encapsulate an RSASVE key (for RSA keys).
  120. .PP
  121. .Vb 1
  122. \& #include <openssl/evp.h>
  123. \&
  124. \& /*
  125. \& * NB: assumes rsa_pub_key is an public key of another party.
  126. \& */
  127. \&
  128. \& EVP_PKEY_CTX *ctx = NULL;
  129. \& size_t secretlen = 0, outlen = 0;
  130. \& unsigned char *out = NULL, *secret = NULL;
  131. \&
  132. \& ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_pub_key, NULL);
  133. \& if (ctx == NULL)
  134. \& /* Error */
  135. \& if (EVP_PKEY_encapsulate_init(ctx, NULL) <= 0)
  136. \& /* Error */
  137. \&
  138. \& /* Set the mode \- only \*(AqRSASVE\*(Aq is currently supported */
  139. \& if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
  140. \& /* Error */
  141. \& /* Determine buffer length */
  142. \& if (EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen) <= 0)
  143. \& /* Error */
  144. \&
  145. \& out = OPENSSL_malloc(outlen);
  146. \& secret = OPENSSL_malloc(secretlen);
  147. \& if (out == NULL || secret == NULL)
  148. \& /* malloc failure */
  149. \&
  150. \& /*
  151. \& * The generated \*(Aqsecret\*(Aq can be used as key material.
  152. \& * The encapsulated \*(Aqout\*(Aq can be sent to another party who can
  153. \& * decapsulate it using their private key to retrieve the \*(Aqsecret\*(Aq.
  154. \& */
  155. \& if (EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen) <= 0)
  156. \& /* Error */
  157. .Ve
  158. .SH "SEE ALSO"
  159. .IX Header "SEE ALSO"
  160. \&\fBEVP_PKEY_CTX_new_from_pkey\fR\|(3),
  161. \&\fBEVP_PKEY_decapsulate\fR\|(3),
  162. \&\fBEVP_KEM\-RSA\fR\|(7), \fBEVP_KEM\-X25519\fR\|(7), \fBEVP_KEM\-EC\fR\|(7)
  163. .SH HISTORY
  164. .IX Header "HISTORY"
  165. These functions \fBEVP_PKEY_encapsulate_init()\fR and \fBEVP_PKEY_encapsulate()\fR were
  166. added in OpenSSL 3.0.
  167. The function \fBEVP_PKEY_auth_encapsulate_init()\fR was added in OpenSSL 3.2.
  168. .SH COPYRIGHT
  169. .IX Header "COPYRIGHT"
  170. Copyright 2020\-2024 The OpenSSL Project Authors. All Rights Reserved.
  171. .PP
  172. Licensed under the Apache License 2.0 (the "License"). You may not use
  173. this file except in compliance with the License. You can obtain a copy
  174. in the file LICENSE in the source distribution or at
  175. <https://www.openssl.org/source/license.html>.