EVP_PKEY_decrypt.pod 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_decrypt_init, EVP_PKEY_decrypt_init_ex,
  4. EVP_PKEY_decrypt - decrypt using a public key algorithm
  5. =head1 SYNOPSIS
  6. #include <openssl/evp.h>
  7. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx);
  8. int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
  9. int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
  10. unsigned char *out, size_t *outlen,
  11. const unsigned char *in, size_t inlen);
  12. =head1 DESCRIPTION
  13. The EVP_PKEY_decrypt_init() function initializes a public key algorithm
  14. context using key I<pkey> for a decryption operation.
  15. The EVP_PKEY_decrypt_init_ex() function initializes a public key algorithm
  16. context using key I<pkey> for a decryption operation and sets the
  17. algorithm specific I<params>.
  18. The EVP_PKEY_decrypt() function performs a public key decryption operation
  19. using I<ctx>. The data to be decrypted is specified using the I<in> and
  20. I<inlen> parameters. If I<out> is NULL then the minimum required size of
  21. the output buffer is written to the I<*outlen> parameter.
  22. If I<out> is not NULL then before the call the I<*outlen> parameter must
  23. contain the length of the I<out> buffer. If the call is successful the
  24. decrypted data is written to I<out> and the amount of the decrypted data
  25. written to I<*outlen>, otherwise an error is returned.
  26. =head1 NOTES
  27. After the call to EVP_PKEY_decrypt_init() algorithm specific control
  28. operations can be performed to set any appropriate parameters for the
  29. operation. These operations can be included in the EVP_PKEY_decrypt_init_ex()
  30. call.
  31. The function EVP_PKEY_decrypt() can be called more than once on the same
  32. context if several operations are performed using the same parameters.
  33. =head1 RETURN VALUES
  34. EVP_PKEY_decrypt_init(), EVP_PKEY_decrypt_init_ex() and EVP_PKEY_decrypt()
  35. return 1 for success and 0 or a negative value for failure. In particular a
  36. return value of -2 indicates the operation is not supported by the public key
  37. algorithm.
  38. =head1 WARNINGS
  39. In OpenSSL versions before 3.2.0, when used in PKCS#1 v1.5 padding,
  40. both the return value from the EVP_PKEY_decrypt() and the B<outlen> provided
  41. information useful in mounting a Bleichenbacher attack against the
  42. used private key. They had to processed in a side-channel free way.
  43. Since version 3.2.0, the EVP_PKEY_decrypt() method when used with PKCS#1
  44. v1.5 padding doesn't return an error in case it detects an error in padding,
  45. instead it returns a pseudo-randomly generated message, removing the need
  46. of side-channel secure code from applications using OpenSSL.
  47. =head1 EXAMPLES
  48. Decrypt data using OAEP (for RSA keys):
  49. #include <openssl/evp.h>
  50. #include <openssl/rsa.h>
  51. EVP_PKEY_CTX *ctx;
  52. ENGINE *eng;
  53. unsigned char *out, *in;
  54. size_t outlen, inlen;
  55. EVP_PKEY *key;
  56. /*
  57. * NB: assumes key, eng, in, inlen are already set up
  58. * and that key is an RSA private key
  59. */
  60. ctx = EVP_PKEY_CTX_new(key, eng);
  61. if (!ctx)
  62. /* Error occurred */
  63. if (EVP_PKEY_decrypt_init(ctx) <= 0)
  64. /* Error */
  65. if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
  66. /* Error */
  67. /* Determine buffer length */
  68. if (EVP_PKEY_decrypt(ctx, NULL, &outlen, in, inlen) <= 0)
  69. /* Error */
  70. out = OPENSSL_malloc(outlen);
  71. if (!out)
  72. /* malloc failure */
  73. if (EVP_PKEY_decrypt(ctx, out, &outlen, in, inlen) <= 0)
  74. /* Error */
  75. /* Decrypted data is outlen bytes written to buffer out */
  76. =head1 SEE ALSO
  77. L<EVP_PKEY_CTX_new(3)>,
  78. L<EVP_PKEY_encrypt(3)>,
  79. L<EVP_PKEY_sign(3)>,
  80. L<EVP_PKEY_verify(3)>,
  81. L<EVP_PKEY_verify_recover(3)>,
  82. L<EVP_PKEY_derive(3)>
  83. =head1 HISTORY
  84. These functions were added in OpenSSL 1.0.0.
  85. =head1 COPYRIGHT
  86. Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  87. Licensed under the Apache License 2.0 (the "License"). You may not use
  88. this file except in compliance with the License. You can obtain a copy
  89. in the file LICENSE in the source distribution or at
  90. L<https://www.openssl.org/source/license.html>.
  91. =cut