xref: /freebsd/crypto/openssl/doc/man3/EVP_PKEY_encrypt.pod (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5*b077aed3SPierre ProncheryEVP_PKEY_encrypt_init_ex,
6e71b7053SJung-uk KimEVP_PKEY_encrypt_init, EVP_PKEY_encrypt - encrypt using a public key algorithm
7e71b7053SJung-uk Kim
8e71b7053SJung-uk Kim=head1 SYNOPSIS
9e71b7053SJung-uk Kim
10e71b7053SJung-uk Kim #include <openssl/evp.h>
11e71b7053SJung-uk Kim
12e71b7053SJung-uk Kim int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx);
13*b077aed3SPierre Pronchery int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
14e71b7053SJung-uk Kim int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
15e71b7053SJung-uk Kim                      unsigned char *out, size_t *outlen,
16e71b7053SJung-uk Kim                      const unsigned char *in, size_t inlen);
17e71b7053SJung-uk Kim
18e71b7053SJung-uk Kim=head1 DESCRIPTION
19e71b7053SJung-uk Kim
20e71b7053SJung-uk KimThe EVP_PKEY_encrypt_init() function initializes a public key algorithm
21e71b7053SJung-uk Kimcontext using key B<pkey> for an encryption operation.
22e71b7053SJung-uk Kim
23*b077aed3SPierre ProncheryThe EVP_PKEY_encrypt_init_ex() function initializes a public key algorithm
24*b077aed3SPierre Proncherycontext using key B<pkey> for an encryption operation and sets the
25*b077aed3SPierre Proncheryalgorithm specific B<params>.
26*b077aed3SPierre Pronchery
27e71b7053SJung-uk KimThe EVP_PKEY_encrypt() function performs a public key encryption operation
28e71b7053SJung-uk Kimusing B<ctx>. The data to be encrypted is specified using the B<in> and
29e71b7053SJung-uk KimB<inlen> parameters. If B<out> is B<NULL> then the maximum size of the output
30e71b7053SJung-uk Kimbuffer is written to the B<outlen> parameter. If B<out> is not B<NULL> then
31e71b7053SJung-uk Kimbefore the call the B<outlen> parameter should contain the length of the
32e71b7053SJung-uk KimB<out> buffer, if the call is successful the encrypted data is written to
33e71b7053SJung-uk KimB<out> and the amount of data written to B<outlen>.
34e71b7053SJung-uk Kim
35e71b7053SJung-uk Kim=head1 NOTES
36e71b7053SJung-uk Kim
37e71b7053SJung-uk KimAfter the call to EVP_PKEY_encrypt_init() algorithm specific control
38e71b7053SJung-uk Kimoperations can be performed to set any appropriate parameters for the
39*b077aed3SPierre Proncheryoperation.  These operations can be included in the EVP_PKEY_encrypt_init_ex()
40*b077aed3SPierre Proncherycall.
41e71b7053SJung-uk Kim
42e71b7053SJung-uk KimThe function EVP_PKEY_encrypt() can be called more than once on the same
43e71b7053SJung-uk Kimcontext if several operations are performed using the same parameters.
44e71b7053SJung-uk Kim
45e71b7053SJung-uk Kim=head1 RETURN VALUES
46e71b7053SJung-uk Kim
47*b077aed3SPierre ProncheryEVP_PKEY_encrypt_init(), EVP_PKEY_encrypt_init_ex() and EVP_PKEY_encrypt()
48*b077aed3SPierre Proncheryreturn 1 for success and 0 or a negative value for failure. In particular a
49*b077aed3SPierre Proncheryreturn value of -2 indicates the operation is not supported by the public key
50*b077aed3SPierre Proncheryalgorithm.
51e71b7053SJung-uk Kim
52da327cd2SJung-uk Kim=head1 EXAMPLES
53e71b7053SJung-uk Kim
54e71b7053SJung-uk KimEncrypt data using OAEP (for RSA keys). See also L<PEM_read_PUBKEY(3)> or
55e71b7053SJung-uk KimL<d2i_X509(3)> for means to load a public key. You may also simply
56e71b7053SJung-uk Kimset 'eng = NULL;' to start with the default OpenSSL RSA implementation:
57e71b7053SJung-uk Kim
58e71b7053SJung-uk Kim #include <openssl/evp.h>
59e71b7053SJung-uk Kim #include <openssl/rsa.h>
60e71b7053SJung-uk Kim #include <openssl/engine.h>
61e71b7053SJung-uk Kim
62e71b7053SJung-uk Kim EVP_PKEY_CTX *ctx;
63e71b7053SJung-uk Kim ENGINE *eng;
64e71b7053SJung-uk Kim unsigned char *out, *in;
65e71b7053SJung-uk Kim size_t outlen, inlen;
66e71b7053SJung-uk Kim EVP_PKEY *key;
67e71b7053SJung-uk Kim
68e71b7053SJung-uk Kim /*
69e71b7053SJung-uk Kim  * NB: assumes eng, key, in, inlen are already set up,
70e71b7053SJung-uk Kim  * and that key is an RSA public key
71e71b7053SJung-uk Kim  */
72e71b7053SJung-uk Kim ctx = EVP_PKEY_CTX_new(key, eng);
73e71b7053SJung-uk Kim if (!ctx)
74e71b7053SJung-uk Kim     /* Error occurred */
75e71b7053SJung-uk Kim if (EVP_PKEY_encrypt_init(ctx) <= 0)
76e71b7053SJung-uk Kim     /* Error */
77*b077aed3SPierre Pronchery if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
78e71b7053SJung-uk Kim     /* Error */
79e71b7053SJung-uk Kim
80e71b7053SJung-uk Kim /* Determine buffer length */
81e71b7053SJung-uk Kim if (EVP_PKEY_encrypt(ctx, NULL, &outlen, in, inlen) <= 0)
82e71b7053SJung-uk Kim     /* Error */
83e71b7053SJung-uk Kim
84e71b7053SJung-uk Kim out = OPENSSL_malloc(outlen);
85e71b7053SJung-uk Kim
86e71b7053SJung-uk Kim if (!out)
87e71b7053SJung-uk Kim     /* malloc failure */
88e71b7053SJung-uk Kim
89e71b7053SJung-uk Kim if (EVP_PKEY_encrypt(ctx, out, &outlen, in, inlen) <= 0)
90e71b7053SJung-uk Kim     /* Error */
91e71b7053SJung-uk Kim
92e71b7053SJung-uk Kim /* Encrypted data is outlen bytes written to buffer out */
93e71b7053SJung-uk Kim
94e71b7053SJung-uk Kim=head1 SEE ALSO
95e71b7053SJung-uk Kim
96e71b7053SJung-uk KimL<d2i_X509(3)>,
97e71b7053SJung-uk KimL<ENGINE_by_id(3)>,
98e71b7053SJung-uk KimL<EVP_PKEY_CTX_new(3)>,
99e71b7053SJung-uk KimL<EVP_PKEY_decrypt(3)>,
100e71b7053SJung-uk KimL<EVP_PKEY_sign(3)>,
101e71b7053SJung-uk KimL<EVP_PKEY_verify(3)>,
102e71b7053SJung-uk KimL<EVP_PKEY_verify_recover(3)>,
103e71b7053SJung-uk KimL<EVP_PKEY_derive(3)>
104e71b7053SJung-uk Kim
105e71b7053SJung-uk Kim=head1 HISTORY
106e71b7053SJung-uk Kim
1076935a639SJung-uk KimThese functions were added in OpenSSL 1.0.0.
108e71b7053SJung-uk Kim
109e71b7053SJung-uk Kim=head1 COPYRIGHT
110e71b7053SJung-uk Kim
111*b077aed3SPierre ProncheryCopyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
112e71b7053SJung-uk Kim
113*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
114e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
115e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
116e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
117e71b7053SJung-uk Kim
118e71b7053SJung-uk Kim=cut
119