EVP_PKEY_encapsulate.pod 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_encapsulate_init, EVP_PKEY_encapsulate
  4. - Key encapsulation using a KEM algorithm with a public key
  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 *wrappedkey, size_t *wrappedkeylen,
  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. Note that I<ctx> is usually is produced using L<EVP_PKEY_CTX_new_from_pkey(3)>,
  16. specifying the public key to use.
  17. The EVP_PKEY_encapsulate() function performs a public key encapsulation
  18. operation using I<ctx>.
  19. The symmetric secret generated in I<genkey> can be used as key material.
  20. The ciphertext in I<wrappedkey> is its encapsulated form, which can be sent
  21. to another party, who can use L<EVP_PKEY_decapsulate(3)> to retrieve it
  22. using their private key.
  23. If I<wrappedkey> is NULL then the maximum size of the output buffer
  24. is written to the I<*wrappedkeylen> parameter unless I<wrappedkeylen> is NULL
  25. and the maximum size of the generated key buffer is written to I<*genkeylen>
  26. unless I<genkeylen> is NULL.
  27. If I<wrappedkey> is not NULL and the call is successful then the
  28. internally generated key is written to I<genkey> and its size is written to
  29. I<*genkeylen>. The encapsulated version of the generated key is written to
  30. I<wrappedkey> and its size is written to I<*wrappedkeylen>.
  31. =head1 NOTES
  32. After the call to EVP_PKEY_encapsulate_init() algorithm-specific parameters
  33. for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>.
  34. =head1 RETURN VALUES
  35. EVP_PKEY_encapsulate_init() and EVP_PKEY_encapsulate() return 1 for
  36. success and 0 or a negative value for failure. In particular a return value of -2
  37. indicates the operation is not supported by the public key algorithm.
  38. =head1 EXAMPLES
  39. Encapsulate an RSASVE key (for RSA keys).
  40. #include <openssl/evp.h>
  41. /*
  42. * NB: assumes rsa_pub_key is an public key of another party.
  43. */
  44. EVP_PKEY_CTX *ctx = NULL;
  45. size_t secretlen = 0, outlen = 0;
  46. unsigned char *out = NULL, *secret = NULL;
  47. ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_pub_key, NULL);
  48. if (ctx = NULL)
  49. /* Error */
  50. if (EVP_PKEY_encapsulate_init(ctx, NULL) <= 0)
  51. /* Error */
  52. /* Set the mode - only 'RSASVE' is currently supported */
  53. if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
  54. /* Error */
  55. /* Determine buffer length */
  56. if (EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen) <= 0)
  57. /* Error */
  58. out = OPENSSL_malloc(outlen);
  59. secret = OPENSSL_malloc(secretlen);
  60. if (out == NULL || secret == NULL)
  61. /* malloc failure */
  62. /*
  63. * The generated 'secret' can be used as key material.
  64. * The encapsulated 'out' can be sent to another party who can
  65. * decapsulate it using their private key to retrieve the 'secret'.
  66. */
  67. if (EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen) <= 0)
  68. /* Error */
  69. =head1 SEE ALSO
  70. L<EVP_PKEY_CTX_new_from_pkey(3)>,
  71. L<EVP_PKEY_decapsulate(3)>,
  72. L<EVP_KEM-RSA(7)>,
  73. =head1 HISTORY
  74. These functions were added in OpenSSL 3.0.
  75. =head1 COPYRIGHT
  76. Copyright 2020-2023 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