EVP_PKEY_DECRYPT(3ossl)
π EVP_PKEY_DECRYPT β ossl manual (OpenSSL, bestie)
okay so this is the EVP_PKEY_DECRYPT(3ossl) page, and it's literally all about decrypting stuff with a public key algorithm. yes, decrypting β the other direction from the encrypt page. stay with me π«Ά
β¨ NAME β¨
EVP_PKEY_decrypt_init, EVP_PKEY_decrypt_init_ex, EVP_PKEY_decrypt β decrypt using a public key algorithm. that's the whole vibe. π
πΈ SYNOPSIS πΈ
#include <openssl/evp.h>
int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx);
int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen);
like, three functions, one header, zero drama. copy-paste mentality fr fr. π
π¦ DESCRIPTION π¦
okay so, the trio, one by one:
EVP_PKEY_decrypt_init()sets up a public key algorithm context for a decryption operation using keypkey. init era. π±EVP_PKEY_decrypt_init_ex()does basically the same thing β initializes a public key algorithm context using keypkeyfor a decryption operation β but it also sets the algorithm specificparams. so it's the init with extra sparkle β¨.EVP_PKEY_decrypt()is the main character here π. it performs a public key decryption operation usingctx. the data you wanna decrypt goes in via theinandinlenparameters.
now the fun part, the two-pass thing (iykyk):
- if
outisNULL, then the minimum required size of the output buffer gets written to the*outlenparameter. that's your little pre-flight check π«. - if
outis notNULL, then before the call the*outlenparameter must contain the length of theoutbuffer. if the call succeeds, the decrypted data is written tooutand the amount of decrypted data gets written to*outlen. otherwise, an error is returned. no cap. π
π NOTES π
so after you call EVP_PKEY_decrypt_init(), you can do algorithm specific control operations to set any appropriate parameters for the operation. and plot twist β those operations can also be included in the EVP_PKEY_decrypt_init_ex() call. convenient much? π
and EVP_PKEY_decrypt() can be called more than once on the same context if you're doing several operations with the same parameters. reuse slay β»οΈ.
β RETURN VALUES β
all three β EVP_PKEY_decrypt_init(), EVP_PKEY_decrypt_init_ex() and EVP_PKEY_decrypt() β return 1 for success, and 0 or a negative value for failure. classic.
but specifically: a return value of -2 means the operation is not supported by the public key algorithm. so if you see a -2, that's the algorithm saying "not my job, babe" π
.
π₯ WARNINGS π₯
pay attention here, this one's serious. like, actual security serious. π¨
in OpenSSL versions before 3.2.0, when used in PKCS#1 v1.5 padding, both the return value from EVP_PKEY_decrypt() and the outlen leaked information useful in mounting a Bleichenbacher attack against the private key you're using. π they had to be processed in a side-channel free way. no shortcuts, no vibes-based error handling.
since version 3.2.0, the EVP_PKEY_decrypt() method when used with PKCS#1 v1.5 padding as implemented in the default provider implements the implicit rejection mechanism (see OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION in provider-asym_cipher(7)). what that means in group-chat terms: it doesn't return an error when it detects an error in padding β instead it returns a pseudo-randomly generated message. which removes the need for side-channel secure code from applications using OpenSSL. iconic behavior honestly π.
but β if OpenSSL is configured to use a provider that doesn't implement implicit rejection, the code still needs to handle the returned values using side-channel free code. that part didn't go anywhere.
and side-channel free handling of the error stack can be performed using either:
- a pair of unconditional
ERR_set_mark(3)andERR_pop_to_mark(3)calls, or - by using the
ERR_clear_error(3)call.
pick your fighter, but pick one. periodt. π«Ά
π EXAMPLES π
decrypt data using OAEP (for RSA keys), bestie:
#include <openssl/evp.h>
#include <openssl/rsa.h>
EVP_PKEY_CTX *ctx;
ENGINE *eng;
unsigned char *out, *in;
size_t outlen, inlen;
EVP_PKEY *key;
/*
* NB: assumes key, eng, in, inlen are already set up
* and that key is an RSA private key
*/
ctx = EVP_PKEY_CTX_new(key, eng);
if (!ctx)
/* Error occurred */
if (EVP_PKEY_decrypt_init(ctx) <= 0)
/* Error */
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_OAEP_PADDING) <= 0)
/* Error */
/* Determine buffer length */
if (EVP_PKEY_decrypt(ctx, NULL, &outlen, in, inlen) <= 0)
/* Error */
out = OPENSSL_malloc(outlen);
if (!out)
/* malloc failure */
if (EVP_PKEY_decrypt(ctx, out, &outlen, in, inlen) <= 0)
/* Error */
/* Decrypted data is outlen bytes written to buffer out */
see the two-call pattern? first call with NULL to find out how big out needs to be, OPENSSL_malloc it, second call to actually fill it. that's the whole dance π. and yes, the comment says it assumes key, eng, in, inlen are already set up and that key is an RSA private key β don't come for me, I'm just the messenger π§.
π SEE ALSO π
go read these too, they're all linked:
EVP_PKEY_CTX_new(3)EVP_PKEY_encrypt(3)EVP_PKEY_sign(3)EVP_PKEY_verify(3)EVP_PKEY_verify_recover(3)EVP_PKEY_derive(3)
π HISTORY π
these functions were added in OpenSSL 1.0.0. vintage. they've been serving since before some of us could read. β
π COPYRIGHT π
Copyright 2006-2025 The OpenSSL Project Authors. All Rights Reserved.
licensed under the Apache License 2.0 (the "License"). you may not use this file except in compliance with the License. you can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
and that's the page !! π version 3.5.0, dated 2026-09-25, straight from the EVP_PKEY_DECRYPT(3ossl) manpage. go decrypt something responsibly, bestie β and remember the side-channel thing, that one's not a joke ππ«Ά
π¦ where this page comes from
- opensuse-leap 16.0: openssl-3-doc 3.5.0-160000.11.1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms