EVP_PKEY_decapsulate.pod 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_decapsulate_init, EVP_PKEY_auth_decapsulate_init, EVP_PKEY_decapsulate
  4. - Key decapsulation using a KEM algorithm with a private key
  5. =head1 SYNOPSIS
  6. #include <openssl/evp.h>
  7. int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
  8. int EVP_PKEY_auth_decapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpub,
  9. const OSSL_PARAM params[]);
  10. int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx,
  11. unsigned char *unwrapped, size_t *unwrappedlen,
  12. const unsigned char *wrapped, size_t wrappedlen);
  13. =head1 DESCRIPTION
  14. The EVP_PKEY_decapsulate_init() function initializes a private key algorithm
  15. context I<ctx> for a decapsulation 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> usually is produced using L<EVP_PKEY_CTX_new_from_pkey(3)>,
  18. specifying the private key to use.
  19. The EVP_PKEY_auth_decapsulate_init() function is similar to
  20. EVP_PKEY_decapsulate_init() but also passes an I<authpub> authentication public
  21. key that is used during decapsulation.
  22. The EVP_PKEY_decapsulate() function performs a private key decapsulation
  23. operation using I<ctx>. The data to be decapsulated is specified using the
  24. I<wrapped> and I<wrappedlen> parameters.
  25. If I<unwrapped> is NULL then the size of the output secret buffer
  26. is written to I<*unwrappedlen>. If I<unwrapped> is not NULL and the
  27. call is successful then the decapsulated secret data is written to I<unwrapped>
  28. and the amount of data written to I<*unwrappedlen>. Note that, if I<unwrappedlen>
  29. is not NULL in this call, the value it points to must be initialised to the length of
  30. I<unwrapped>, so that the call can validate it is of sufficient size to hold the
  31. result of the operation.
  32. =head1 NOTES
  33. After the call to EVP_PKEY_decapsulate_init() algorithm-specific parameters
  34. for the operation may be set or modified using L<EVP_PKEY_CTX_set_params(3)>.
  35. =head1 RETURN VALUES
  36. EVP_PKEY_decapsulate_init(), EVP_PKEY_auth_decapsulate_init() and
  37. EVP_PKEY_decapsulate() return 1 for success and 0 or a negative value for
  38. failure. In particular a return value of -2 indicates the operation is not
  39. supported by the private key algorithm.
  40. =head1 EXAMPLES
  41. Decapsulate data using RSA:
  42. #include <openssl/evp.h>
  43. /*
  44. * NB: assumes rsa_priv_key is an RSA private key,
  45. * and that in, inlen are already set up to contain encapsulated data.
  46. */
  47. EVP_PKEY_CTX *ctx = NULL;
  48. size_t secretlen = 0;
  49. unsigned char *secret = NULL;;
  50. ctx = EVP_PKEY_CTX_new_from_pkey(libctx, rsa_priv_key, NULL);
  51. if (ctx == NULL)
  52. /* Error */
  53. if (EVP_PKEY_decapsulate_init(ctx, NULL) <= 0)
  54. /* Error */
  55. /* Set the mode - only 'RSASVE' is currently supported */
  56. if (EVP_PKEY_CTX_set_kem_op(ctx, "RSASVE") <= 0)
  57. /* Error */
  58. /* Determine buffer length */
  59. if (EVP_PKEY_decapsulate(ctx, NULL, &secretlen, in, inlen) <= 0)
  60. /* Error */
  61. secret = OPENSSL_malloc(secretlen);
  62. if (secret == NULL)
  63. /* malloc failure */
  64. /* Decapsulated secret data is secretlen bytes long */
  65. if (EVP_PKEY_decapsulate(ctx, secret, &secretlen, in, inlen) <= 0)
  66. /* Error */
  67. =head1 SEE ALSO
  68. L<EVP_PKEY_CTX_new_from_pkey(3)>,
  69. L<EVP_PKEY_encapsulate(3)>,
  70. L<EVP_KEM-RSA(7)>, L<EVP_KEM-X25519(7)>, L<EVP_KEM-EC(7)>
  71. =head1 HISTORY
  72. The functions EVP_PKEY_decapsulate_init() and EVP_PKEY_decapsulate() were added
  73. in OpenSSL 3.0.
  74. The function EVP_PKEY_auth_decapsulate_init() was added in OpenSSL 3.2.
  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