1 .\" $OpenBSD: EVP_PKEY_encrypt.3,v 1.4 2016/11/27 15:23:29 schwarze Exp $
2 .\" OpenSSL b97fdb57 Nov 11 09:33:09 2016 +0100
4 .\" This file was written by Dr. Stephen Henson <steve@openssl.org>.
5 .\" Copyright (c) 2006, 2009, 2013, 2014, 2016 The OpenSSL Project.
6 .\" All rights reserved.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in
17 .\" the documentation and/or other materials provided with the
20 .\" 3. All advertising materials mentioning features or use of this
21 .\" software must display the following acknowledgment:
22 .\" "This product includes software developed by the OpenSSL Project
23 .\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 .\" endorse or promote products derived from this software without
27 .\" prior written permission. For written permission, please contact
28 .\" openssl-core@openssl.org.
30 .\" 5. Products derived from this software may not be called "OpenSSL"
31 .\" nor may "OpenSSL" appear in their names without prior written
32 .\" permission of the OpenSSL Project.
34 .\" 6. Redistributions of any form whatsoever must retain the following
36 .\" "This product includes software developed by the OpenSSL Project
37 .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
52 .Dd $Mdocdate: November 27 2016 $
53 .Dt EVP_PKEY_ENCRYPT 3
56 .Nm EVP_PKEY_encrypt_init ,
58 .Nd encrypt using a public key algorithm
62 .Fo EVP_PKEY_encrypt_init
63 .Fa "EVP_PKEY_CTX *ctx"
67 .Fa "EVP_PKEY_CTX *ctx"
68 .Fa "unsigned char *out"
70 .Fa "const unsigned char *in"
75 .Fn EVP_PKEY_encrypt_init
76 function initializes a public key algorithm context using key
78 for an encryption operation.
82 function performs a public key encryption operation using
84 The data to be encrypted is specified using the
93 then the maximum size of the output buffer is written to the
100 then before the call the
102 parameter should contain the length of the
105 If the call is successful the encrypted data is written to
107 and the amount of data written to
111 .Fn EVP_PKEY_encrypt_init ,
112 algorithm specific control operations can be performed to set any
113 appropriate parameters for the operation.
117 can be called more than once on the same context if several operations
118 are performed using the same parameters.
120 .Fn EVP_PKEY_encrypt_init
123 return 1 for success and 0 or a negative value for failure.
124 In particular, a return value of -2 indicates the operation is not
125 supported by the public key algorithm.
127 Encrypt data using OAEP (for RSA keys).
129 .Xr PEM_read_PUBKEY 3
132 for means to load a public key.
133 You may also simply set
135 to start with the default OpenSSL RSA implementation:
136 .Bd -literal -offset indent
137 #include <openssl/evp.h>
138 #include <openssl/rsa.h>
139 #include <openssl/engine.h>
143 unsigned char *out, *in;
144 size_t outlen, inlen;
146 /* NB: assumes eng, key in, inlen are already set up
147 * and that key is an RSA public key
149 ctx = EVP_PKEY_CTX_new(key, eng);
152 if (EVP_PKEY_encrypt_init(ctx) <= 0)
154 if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
157 /* Determine buffer length */
158 if (EVP_PKEY_encrypt(ctx, NULL, &outlen, in, inlen) <= 0)
161 out = malloc(outlen);
166 if (EVP_PKEY_encrypt(ctx, out, &outlen, in, inlen) <= 0)
169 /* Encrypted data is outlen bytes written to buffer out */
172 .Xr EVP_PKEY_CTX_new 3 ,
173 .Xr EVP_PKEY_decrypt 3 ,
174 .Xr EVP_PKEY_derive 3 ,
175 .Xr EVP_PKEY_sign 3 ,
176 .Xr EVP_PKEY_verify 3 ,
177 .Xr EVP_PKEY_verify_recover 3
179 These functions were first added to OpenSSL 1.0.0.