EVP_PKEY_VERIFY_RECOVER(3ossl)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’– EVP_PKEY_VERIFY_RECOVER(3ossl) πŸ’–

okay so bestie, this is a page straight out of the OpenSSL manual (section 3ossl, iykyk) and it's all about recovering a signature using a public key algorithm. yes, recovering. like getting the original signed data back out. it's giving forensic queen energy πŸ‘‘


✨ NAME ✨

EVP_PKEY_verify_recover_init, EVP_PKEY_verify_recover_init_ex, EVP_PKEY_verify_recover_init_ex2, EVP_PKEY_verify_recover β€” recover signature using a public key algorithm πŸŽ€


🌸 SYNOPSIS 🌸

#include <openssl/evp.h>

int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx);
int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx,
                                    const OSSL_PARAM params[]);
int EVP_PKEY_verify_recover_init_ex2(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *algo,
                                     const OSSL_PARAM params[]);
int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
                            unsigned char *rout, size_t *routlen,
                            const unsigned char *sig, size_t siglen);

πŸ¦‹ DESCRIPTION πŸ¦‹

so EVP_PKEY_verify_recover_init() sets up a public key algorithm context ctx for signing, using whatever algorithm got baked in when the context was created with EVP_PKEY_CTX_new(3) or any of its variants. the algorithm is used to fetch an EVP_SIGNATURE method implicitly β€” if you wanna know more about implicit fetches, go read "Implicit fetch" in provider(7), she explains it better than I ever could πŸ’…

EVP_PKEY_verify_recover_init_ex() is literally the same as EVP_PKEY_verify_recover_init() but it also sets the parameters params you passed on the context before it returns. cute little bonus, no cap ✨

EVP_PKEY_verify_recover_init_ex2() is the same as EVP_PKEY_verify_recover_init_ex(), but it works with an explicitly fetched EVP_SIGNATURE algo. 🚨 important: a context ctx with no pre-loaded key cannot be used with this function. and depending on which algorithm got fetched, some details around how the input to EVP_PKEY_verify() is treated may already be pre-determined β€” and in that case those details normally can't be changed. "NOTES" below has the deeper lore, keep scrolling 🫢

then there's EVP_PKEY_verify_recover() β€” she recovers the signed data using ctx. the signature goes in via the sig and siglen parameters. and here's the routine that trips people up, so listen up:

  • if rout is NULL, the maximum size of the output buffer gets written to routlen. that's your "how much room do I need" call, periodt.
  • if rout is not NULL, then before the call routlen should contain the length of the rout buffer. if the call succeeds, the recovered data gets written to rout, and the amount of data actually written goes into routlen. slay 🌈

πŸ’€ NOTES πŸ’€

normally, an application only cares whether a signature verification operation succeeded β€” in those cases you should just use the EVP_verify() function. simple, clean, no drama.

but sometimes πŸ‘ you πŸ‘ need πŸ‘ the data that was originally signed, obtained using a signing operation. only certain public key algorithms can recover a signature this way (for example RSA in PKCS padding mode). so like, check your algorithm first, bestie πŸ™ˆ

after you call EVP_PKEY_verify_recover_init(), you can do algorithm-specific control operations to set any appropriate parameters for the operation. go off, customize your vibe.

after you call EVP_PKEY_verify_recover_init_ex2(), those algorithm-specific control operations may not even be needed, if the chosen algorithm implies those controls are pre-set (and immutable). she comes pre-styled πŸ’…

and EVP_PKEY_verify_recover() can be called more than once on the same context if you're doing several operations with the same parameters. reuse is iconic, we love a sustainable queen ♻️


⭐ RETURN VALUES ⭐

EVP_PKEY_verify_recover_init() and EVP_PKEY_verify_recover() return 1 for success, and 0 or a negative value for failure. and specifically: a return value of -2 means the operation is not supported by the public key algorithm. that's your "girl, wrong algorithm" signal 😭


πŸŽ€ EXAMPLES πŸŽ€

Recover digest originally signed using PKCS#1 and SHA256 digest:

#include <openssl/evp.h>
#include <openssl/rsa.h>

EVP_PKEY_CTX *ctx;
unsigned char *rout, *sig;
size_t routlen, siglen;
EVP_PKEY *verify_key;

/*
 * NB: assumes verify_key, sig and siglen are already set up
 * and that verify_key is an RSA public key
 */
ctx = EVP_PKEY_CTX_new(verify_key, NULL /* no engine */);
if (!ctx)
    /* Error occurred */
if (EVP_PKEY_verify_recover_init(ctx) <= 0)
    /* Error */
if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
    /* Error */
if (EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()) <= 0)
    /* Error */

/* Determine buffer length */
if (EVP_PKEY_verify_recover(ctx, NULL, &routlen, sig, siglen) <= 0)
    /* Error */

rout = OPENSSL_malloc(routlen);

if (!rout)
    /* malloc failure */

if (EVP_PKEY_verify_recover(ctx, rout, &routlen, sig, siglen) <= 0)
    /* Error */

/* Recovered data is routlen bytes written to buffer rout */

see how she does the NULL call first to size the buffer, then mallocs, then calls again for real? that's the whole two-step. it's giving "measure twice, cut once" but make it cryptography βœ‚οΈπŸ”₯


πŸ”₯ SEE ALSO πŸ”₯

EVP_PKEY_CTX_new(3), EVP_PKEY_encrypt(3), EVP_PKEY_decrypt(3), EVP_PKEY_sign(3), EVP_PKEY_verify(3), EVP_PKEY_derive(3)

(the whole friend group, honestly 🫢)


πŸ‘‘ HISTORY πŸ‘‘

the EVP_PKEY_verify_recover_init() and EVP_PKEY_verify_recover() functions were added in OpenSSL 1.0.0. vintage, but still thriving.

the EVP_PKEY_verify_recover_init_ex() function was added in OpenSSL 3.0.


Copyright 2013-2024 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 whole page bestie, no notes ✨ mwah πŸ’‹

🌸 this has been EVP_PKEY_VERIFY_RECOVER(3ossl) from OpenSSL 3.5.6, dated 2026-04-07 β€” go forth and recover those signatures, you absolute main character πŸ§šβ€β™€οΈπŸ’–
πŸ“¦ where this page comes from