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