EVP_PKEY_encapsulate.pod 4.1 KB

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