EVP_PKEY_encrypt.pod 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. =pod
  2. =head1 NAME
  3. EVP_PKEY_encrypt_init_ex,
  4. EVP_PKEY_encrypt_init, EVP_PKEY_encrypt - encrypt using a public key algorithm
  5. =head1 SYNOPSIS
  6. #include <openssl/evp.h>
  7. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx);
  8. int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
  9. int EVP_PKEY_encrypt(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_encrypt_init() function initializes a public key algorithm
  14. context using key B<pkey> for an encryption operation.
  15. The EVP_PKEY_encrypt_init_ex() function initializes a public key algorithm
  16. context using key B<pkey> for an encryption operation and sets the
  17. algorithm specific B<params>.
  18. The EVP_PKEY_encrypt() function performs a public key encryption operation
  19. using B<ctx>. The data to be encrypted is specified using the B<in> and
  20. B<inlen> parameters. If B<out> is B<NULL> then the maximum size of the output
  21. buffer is written to the B<outlen> parameter. If B<out> is not B<NULL> then
  22. before the call the B<outlen> parameter should contain the length of the
  23. B<out> buffer, if the call is successful the encrypted data is written to
  24. B<out> and the amount of data written to B<outlen>.
  25. =head1 NOTES
  26. After the call to EVP_PKEY_encrypt_init() algorithm specific control
  27. operations can be performed to set any appropriate parameters for the
  28. operation. These operations can be included in the EVP_PKEY_encrypt_init_ex()
  29. call.
  30. The function EVP_PKEY_encrypt() can be called more than once on the same
  31. context if several operations are performed using the same parameters.
  32. =head1 RETURN VALUES
  33. EVP_PKEY_encrypt_init(), EVP_PKEY_encrypt_init_ex() and EVP_PKEY_encrypt()
  34. return 1 for success and 0 or a negative value for failure. In particular a
  35. return value of -2 indicates the operation is not supported by the public key
  36. algorithm.
  37. =head1 EXAMPLES
  38. Encrypt data using OAEP (for RSA keys). See also L<PEM_read_PUBKEY(3)> or
  39. L<d2i_X509(3)> for means to load a public key. You may also simply
  40. set 'eng = NULL;' to start with the default OpenSSL RSA implementation:
  41. #include <openssl/evp.h>
  42. #include <openssl/rsa.h>
  43. #include <openssl/engine.h>
  44. EVP_PKEY_CTX *ctx;
  45. ENGINE *eng;
  46. unsigned char *out, *in;
  47. size_t outlen, inlen;
  48. EVP_PKEY *key;
  49. /*
  50. * NB: assumes eng, key, in, inlen are already set up,
  51. * and that key is an RSA public key
  52. */
  53. ctx = EVP_PKEY_CTX_new(key, eng);
  54. if (!ctx)
  55. /* Error occurred */
  56. if (EVP_PKEY_encrypt_init(ctx) <= 0)
  57. /* Error */
  58. if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
  59. /* Error */
  60. /* Determine buffer length */
  61. if (EVP_PKEY_encrypt(ctx, NULL, &outlen, in, inlen) <= 0)
  62. /* Error */
  63. out = OPENSSL_malloc(outlen);
  64. if (!out)
  65. /* malloc failure */
  66. if (EVP_PKEY_encrypt(ctx, out, &outlen, in, inlen) <= 0)
  67. /* Error */
  68. /* Encrypted data is outlen bytes written to buffer out */
  69. =head1 SEE ALSO
  70. L<d2i_X509(3)>,
  71. L<ENGINE_by_id(3)>,
  72. L<EVP_PKEY_CTX_new(3)>,
  73. L<EVP_PKEY_decrypt(3)>,
  74. L<EVP_PKEY_sign(3)>,
  75. L<EVP_PKEY_verify(3)>,
  76. L<EVP_PKEY_verify_recover(3)>,
  77. L<EVP_PKEY_derive(3)>
  78. =head1 HISTORY
  79. These functions were added in OpenSSL 1.0.0.
  80. =head1 COPYRIGHT
  81. Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  82. Licensed under the Apache License 2.0 (the "License"). You may not use
  83. this file except in compliance with the License. You can obtain a copy
  84. in the file LICENSE in the source distribution or at
  85. L<https://www.openssl.org/source/license.html>.
  86. =cut