1EVP_PKEY_ENCRYPT(3)                 OpenSSL                EVP_PKEY_ENCRYPT(3)
2
3
4

NAME

6       EVP_PKEY_encrypt_init, EVP_PKEY_encrypt - encrypt using a public key
7       algorithm
8

SYNOPSIS

10        #include <openssl/evp.h>
11
12        int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx);
13        int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
14                             unsigned char *out, size_t *outlen,
15                             const unsigned char *in, size_t inlen);
16

DESCRIPTION

18       The EVP_PKEY_encrypt_init() function initializes a public key algorithm
19       context using key pkey for an encryption operation.
20
21       The EVP_PKEY_encrypt() function performs a public key encryption
22       operation using ctx. The data to be encrypted is specified using the in
23       and inlen parameters. If out is NULL then the maximum size of the
24       output buffer is written to the outlen parameter. If out is not NULL
25       then before the call the outlen parameter should contain the length of
26       the out buffer, if the call is successful the encrypted data is written
27       to out and the amount of data written to outlen.
28

NOTES

30       After the call to EVP_PKEY_encrypt_init() algorithm specific control
31       operations can be performed to set any appropriate parameters for the
32       operation.
33
34       The function EVP_PKEY_encrypt() can be called more than once on the
35       same context if several operations are performed using the same
36       parameters.
37

RETURN VALUES

39       EVP_PKEY_encrypt_init() and EVP_PKEY_encrypt() return 1 for success and
40       0 or a negative value for failure. In particular a return value of -2
41       indicates the operation is not supported by the public key algorithm.
42

EXAMPLES

44       Encrypt data using OAEP (for RSA keys). See also PEM_read_PUBKEY(3) or
45       d2i_X509(3) for means to load a public key. You may also simply set
46       'eng = NULL;' to start with the default OpenSSL RSA implementation:
47
48        #include <openssl/evp.h>
49        #include <openssl/rsa.h>
50        #include <openssl/engine.h>
51
52        EVP_PKEY_CTX *ctx;
53        ENGINE *eng;
54        unsigned char *out, *in;
55        size_t outlen, inlen;
56        EVP_PKEY *key;
57
58        /*
59         * NB: assumes eng, key, in, inlen are already set up,
60         * and that key is an RSA public key
61         */
62        ctx = EVP_PKEY_CTX_new(key, eng);
63        if (!ctx)
64            /* Error occurred */
65        if (EVP_PKEY_encrypt_init(ctx) <= 0)
66            /* Error */
67        if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_OAEP_PADDING) <= 0)
68            /* Error */
69
70        /* Determine buffer length */
71        if (EVP_PKEY_encrypt(ctx, NULL, &outlen, in, inlen) <= 0)
72            /* Error */
73
74        out = OPENSSL_malloc(outlen);
75
76        if (!out)
77            /* malloc failure */
78
79        if (EVP_PKEY_encrypt(ctx, out, &outlen, in, inlen) <= 0)
80            /* Error */
81
82        /* Encrypted data is outlen bytes written to buffer out */
83

SEE ALSO

85       d2i_X509(3), ENGINE_by_id(3), EVP_PKEY_CTX_new(3), EVP_PKEY_decrypt(3),
86       EVP_PKEY_sign(3), EVP_PKEY_verify(3), EVP_PKEY_verify_recover(3),
87       EVP_PKEY_derive(3)
88

HISTORY

90       These functions were added in OpenSSL 1.0.0.
91
93       Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
94
95       Licensed under the OpenSSL license (the "License").  You may not use
96       this file except in compliance with the License.  You can obtain a copy
97       in the file LICENSE in the source distribution or at
98       <https://www.openssl.org/source/license.html>.
99
100
101
1021.1.1q                            2023-02-06               EVP_PKEY_ENCRYPT(3)
Impressum