EVP_PKEY_encapsulate.pod 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_encapsulate_init, EVP_PKEY_auth_encapsulate_init, EVP_PKEY_encapsulate
  4. - Key encapsulation using a public key algorithm
  5. =head1 SYNOPSIS
  6. #include <openssl/evp.h>
  7. int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
  8. int EVP_PKEY_auth_encapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpriv,
  9. const OSSL_PARAM params[]);
  10. int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx,
  11. unsigned char *out, size_t *outlen,
  12. unsigned char *genkey, size_t *genkeylen);
  13. =head1 DESCRIPTION
  14. The EVP_PKEY_encapsulate_init() function initializes a public key algorithm
  15. context I<ctx> for an encapsulation operation and then sets the I<params>
  16. on the context in the same way as calling L<EVP_PKEY_CTX_set_params(3)>.
  17. The EVP_PKEY_auth_encapsulate_init() function is similiar to
  18. EVP_PKEY_encapsulate_init() but also passes an I<authpriv> authentication private
  19. key that is used during encapsulation.
  20. The EVP_PKEY_encapsulate() function performs a public key encapsulation
  21. operation using I<ctx> with the name I<name>.
  22. If I<out> is B<NULL> then the maximum size of the output buffer is written to the
  23. I<*outlen> parameter and the maximum size of the generated key buffer is written
  24. to I<*genkeylen>. If I<out> is not B<NULL> and the call is successful then the
  25. internally generated key is written to I<genkey> and its size is written to
  26. I<*genkeylen>. The encapsulated version of the generated key is written to
  27. I<out> and its size is written to I<*outlen>.
  28. =head1 NOTES
  29. After the call to EVP_PKEY_encapsulate_init() algorithm specific parameters
  30. for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>.
  31. =head1 RETURN VALUES
  32. EVP_PKEY_encapsulate_init(), EVP_PKEY_auth_encapsulate_init() and
  33. EVP_PKEY_encapsulate() return 1 for success and 0 or a negative value for
  34. failure. In particular a return value of -2 indicates the operation is not
  35. supported by the public key algorithm.
  36. =head1 EXAMPLES
  37. Encapsulate an RSASVE key (for RSA keys).
  38. #include <openssl/evp.h>
  39. /*
  40. * NB: assumes rsa_pub_key is an public key of another party.
  41. */
  42. EVP_PKEY_CTX *ctx = NULL;
  43. size_t secretlen = 0, outlen = 0;
  44. unsigned char *out = NULL, *secret = NULL;
  45. ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_pub_key, NULL);
  46. if (ctx = NULL)
  47. /* Error */
  48. if (EVP_PKEY_encapsulate_init(ctx, NULL) <= 0)
  49. /* Error */
  50. /* Set the mode - only 'RSASVE' is currently supported */
  51. if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
  52. /* Error */
  53. /* Determine buffer length */
  54. if (EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen) <= 0)
  55. /* Error */
  56. out = OPENSSL_malloc(outlen);
  57. secret = OPENSSL_malloc(secretlen);
  58. if (out == NULL || secret == NULL)
  59. /* malloc failure */
  60. /*
  61. * The generated 'secret' can be used as key material.
  62. * The encapsulated 'out' can be sent to another party who can
  63. * decapsulate it using their private key to retrieve the 'secret'.
  64. */
  65. if (EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen) <= 0)
  66. /* Error */
  67. =head1 SEE ALSO
  68. L<EVP_PKEY_CTX_new(3)>,
  69. L<EVP_PKEY_decapsulate(3)>,
  70. L<EVP_KEM-RSA(7)>, L<EVP_KEM-X25519(7)>, L<EVP_KEM-EC(7)>
  71. =head1 HISTORY
  72. These functions EVP_PKEY_encapsulate_init() and EVP_PKEY_encapsulate() were
  73. added in OpenSSL 3.0.
  74. The function EVP_PKEY_auth_encapsulate_init() was added in OpenSSL 3.2.
  75. =head1 COPYRIGHT
  76. Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
  77. Licensed under the Apache License 2.0 (the "License"). You may not use
  78. this file except in compliance with the License. You can obtain a copy
  79. in the file LICENSE in the source distribution or at
  80. L<https://www.openssl.org/source/license.html>.
  81. =cut