2
0

EVP_PKEY_encapsulate.pod 3.1 KB

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