EVP_PKEY_decrypt.pod 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 be 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 as implemented in the B<default> provider implements
  45. the implicit rejection mechanism (see
  46. B<OSSL_PKEY_PARAM_IMPLICIT_REJECTION> in L<provider-asym_cipher(7)>).
  47. That means it doesn't return an error when it detects an error in padding,
  48. instead it returns a pseudo-randomly generated message, removing the need
  49. of side-channel secure code from applications using OpenSSL.
  50. If OpenSSL is configured to use a provider that doesn't implement implicit
  51. rejection, the code still needs to handle the returned values
  52. using side-channel free code.
  53. Side-channel free handling of the error stack can be peformed using
  54. either a pair of unconditional L<ERR_set_mark(3)> and L<ERR_pop_to_mark(3)>
  55. calls or by using the L<ERR_clear_error(3)> call.
  56. =head1 EXAMPLES
  57. Decrypt data using OAEP (for RSA keys):
  58. #include <openssl/evp.h>
  59. #include <openssl/rsa.h>
  60. EVP_PKEY_CTX *ctx;
  61. ENGINE *eng;
  62. unsigned char *out, *in;
  63. size_t outlen, inlen;
  64. EVP_PKEY *key;
  65. /*
  66. * NB: assumes key, eng, in, inlen are already set up
  67. * and that key is an RSA private key
  68. */
  69. ctx = EVP_PKEY_CTX_new(key, eng);
  70. if (!ctx)
  71. /* Error occurred */
  72. if (EVP_PKEY_decrypt_init(ctx) <= 0)
  73. /* Error */
  74. if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
  75. /* Error */
  76. /* Determine buffer length */
  77. if (EVP_PKEY_decrypt(ctx, NULL, &outlen, in, inlen) <= 0)
  78. /* Error */
  79. out = OPENSSL_malloc(outlen);
  80. if (!out)
  81. /* malloc failure */
  82. if (EVP_PKEY_decrypt(ctx, out, &outlen, in, inlen) <= 0)
  83. /* Error */
  84. /* Decrypted data is outlen bytes written to buffer out */
  85. =head1 SEE ALSO
  86. L<EVP_PKEY_CTX_new(3)>,
  87. L<EVP_PKEY_encrypt(3)>,
  88. L<EVP_PKEY_sign(3)>,
  89. L<EVP_PKEY_verify(3)>,
  90. L<EVP_PKEY_verify_recover(3)>,
  91. L<EVP_PKEY_derive(3)>
  92. =head1 HISTORY
  93. These functions were added in OpenSSL 1.0.0.
  94. =head1 COPYRIGHT
  95. Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  96. Licensed under the Apache License 2.0 (the "License"). You may not use
  97. this file except in compliance with the License. You can obtain a copy
  98. in the file LICENSE in the source distribution or at
  99. L<https://www.openssl.org/source/license.html>.
  100. =cut