2
0

EVP_PKEY_encrypt.pod 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_encrypt_init, EVP_PKEY_encrypt - encrypt using a public key algorithm
  4. =head1 SYNOPSIS
  5. #include <openssl/evp.h>
  6. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx);
  7. int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
  8. unsigned char *out, size_t *outlen,
  9. const unsigned char *in, size_t inlen);
  10. =head1 DESCRIPTION
  11. The EVP_PKEY_encrypt_init() function initializes a public key algorithm
  12. context using key B<pkey> for an encryption operation.
  13. The EVP_PKEY_encrypt() function performs a public key encryption operation
  14. using B<ctx>. The data to be encrypted is specified using the B<in> and
  15. B<inlen> parameters. If B<out> is B<NULL> then the maximum size of the output
  16. buffer is written to the B<outlen> parameter. If B<out> is not B<NULL> then
  17. before the call the B<outlen> parameter should contain the length of the
  18. B<out> buffer, if the call is successful the encrypted data is written to
  19. B<out> and the amount of data written to B<outlen>.
  20. =head1 NOTES
  21. After the call to EVP_PKEY_encrypt_init() algorithm specific control
  22. operations can be performed to set any appropriate parameters for the
  23. operation.
  24. The function EVP_PKEY_encrypt() can be called more than once on the same
  25. context if several operations are performed using the same parameters.
  26. =head1 RETURN VALUES
  27. EVP_PKEY_encrypt_init() and EVP_PKEY_encrypt() return 1 for success and 0
  28. or a negative value for failure. In particular a return value of -2
  29. indicates the operation is not supported by the public key algorithm.
  30. =head1 EXAMPLE
  31. Encrypt data using OAEP (for RSA keys). See also L<PEM_read_PUBKEY(3)|pem(3)> or
  32. L<d2i_X509(3)|d2i_X509(3)> for means to load a public key. You may also simply
  33. set 'eng = NULL;' to start with the default OpenSSL RSA implementation:
  34. #include <openssl/evp.h>
  35. #include <openssl/rsa.h>
  36. #include <openssl/engine.h>
  37. EVP_PKEY_CTX *ctx;
  38. ENGINE *eng;
  39. unsigned char *out, *in;
  40. size_t outlen, inlen;
  41. EVP_PKEY *key;
  42. /* NB: assumes eng, key, in, inlen are already set up,
  43. * and that key is an RSA public key
  44. */
  45. ctx = EVP_PKEY_CTX_new(key,eng);
  46. if (!ctx)
  47. /* Error occurred */
  48. if (EVP_PKEY_encrypt_init(ctx) <= 0)
  49. /* Error */
  50. if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_OAEP_PADDING) <= 0)
  51. /* Error */
  52. /* Determine buffer length */
  53. if (EVP_PKEY_encrypt(ctx, NULL, &outlen, in, inlen) <= 0)
  54. /* Error */
  55. out = OPENSSL_malloc(outlen);
  56. if (!out)
  57. /* malloc failure */
  58. if (EVP_PKEY_encrypt(ctx, out, &outlen, in, inlen) <= 0)
  59. /* Error */
  60. /* Encrypted data is outlen bytes written to buffer out */
  61. =head1 SEE ALSO
  62. L<d2i_X509(3)|d2i_X509(3)>,
  63. L<engine(3)|engine(3)>,
  64. L<EVP_PKEY_CTX_new(3)|EVP_PKEY_CTX_new(3)>,
  65. L<EVP_PKEY_decrypt(3)|EVP_PKEY_decrypt(3)>,
  66. L<EVP_PKEY_sign(3)|EVP_PKEY_sign(3)>,
  67. L<EVP_PKEY_verify(3)|EVP_PKEY_verify(3)>,
  68. L<EVP_PKEY_verify_recover(3)|EVP_PKEY_verify_recover(3)>,
  69. L<EVP_PKEY_derive(3)|EVP_PKEY_derive(3)>
  70. =head1 HISTORY
  71. These functions were first added to OpenSSL 1.0.0.
  72. =cut