EVP_PKEY_encapsulate.pod 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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>. Note that if
  36. I<wrappedlen> is not NULL, then the value it points to must initially hold the size of
  37. the I<unwrapped> buffer so that its size can be validated by the call, ensuring
  38. it is large enough to hold the result written to I<wrapped>.
  39. =head1 NOTES
  40. After the call to EVP_PKEY_encapsulate_init() algorithm-specific parameters
  41. for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>.
  42. =head1 RETURN VALUES
  43. EVP_PKEY_encapsulate_init(), EVP_PKEY_auth_encapsulate_init() and
  44. EVP_PKEY_encapsulate() return 1 for success and 0 or a negative value for
  45. failure. In particular a return value of -2 indicates the operation is not
  46. supported by the public key algorithm.
  47. =head1 EXAMPLES
  48. Encapsulate an RSASVE key (for RSA keys).
  49. #include <openssl/evp.h>
  50. /*
  51. * NB: assumes rsa_pub_key is an public key of another party.
  52. */
  53. EVP_PKEY_CTX *ctx = NULL;
  54. size_t secretlen = 0, outlen = 0;
  55. unsigned char *out = NULL, *secret = NULL;
  56. ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_pub_key, NULL);
  57. if (ctx == NULL)
  58. /* Error */
  59. if (EVP_PKEY_encapsulate_init(ctx, NULL) <= 0)
  60. /* Error */
  61. /* Set the mode - only 'RSASVE' is currently supported */
  62. if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
  63. /* Error */
  64. /* Determine buffer length */
  65. if (EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen) <= 0)
  66. /* Error */
  67. out = OPENSSL_malloc(outlen);
  68. secret = OPENSSL_malloc(secretlen);
  69. if (out == NULL || secret == NULL)
  70. /* malloc failure */
  71. /*
  72. * The generated 'secret' can be used as key material.
  73. * The encapsulated 'out' can be sent to another party who can
  74. * decapsulate it using their private key to retrieve the 'secret'.
  75. */
  76. if (EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen) <= 0)
  77. /* Error */
  78. =head1 SEE ALSO
  79. L<EVP_PKEY_CTX_new_from_pkey(3)>,
  80. L<EVP_PKEY_decapsulate(3)>,
  81. L<EVP_KEM-RSA(7)>, L<EVP_KEM-X25519(7)>, L<EVP_KEM-EC(7)>
  82. =head1 HISTORY
  83. These functions EVP_PKEY_encapsulate_init() and EVP_PKEY_encapsulate() were
  84. added in OpenSSL 3.0.
  85. The function EVP_PKEY_auth_encapsulate_init() was added in OpenSSL 3.2.
  86. =head1 COPYRIGHT
  87. Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
  88. Licensed under the Apache License 2.0 (the "License"). You may not use
  89. this file except in compliance with the License. You can obtain a copy
  90. in the file LICENSE in the source distribution or at
  91. L<https://www.openssl.org/source/license.html>.
  92. =cut