EVP_PKEY_DECRYPT(3ossl)

πŸ“¦ version 3.5.0, in opensuse-leap 16.0

πŸ’… 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 key pkey. init era. 🌱
  • EVP_PKEY_decrypt_init_ex() does basically the same thing β€” initializes a public key algorithm context using key pkey for a decryption operation β€” but it also sets the algorithm specific params. so it's the init with extra sparkle ✨.
  • EVP_PKEY_decrypt() is the main character here πŸ‘‘. it performs a public key decryption operation using ctx. the data you wanna decrypt goes in via the in and inlen parameters.

now the fun part, the two-pass thing (iykyk):

  • if out is NULL, then the minimum required size of the output buffer gets written to the *outlen parameter. that's your little pre-flight check πŸ›«.
  • if out is not NULL, then before the call the *outlen parameter must contain the length of the out buffer. if the call succeeds, the decrypted data is written to out and 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:

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:


πŸ’œ HISTORY πŸ’œ

these functions were added in OpenSSL 1.0.0. vintage. they've been serving since before some of us could read. ⭐


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