EVP_PKEY_VERIFY_RECOVER(3ossl)
π EVP_PKEY_VERIFY_RECOVER π
okay bestie, gather round, because we're about to talk about the OpenSSL 3ossl manual page for EVP_PKEY_verify_recover_init, EVP_PKEY_verify_recover_init_ex, EVP_PKEY_verify_recover_init_ex2 and EVP_PKEY_verify_recover β aka the β¨ signature-recovery girlies β¨. this is the evp.h corner of the library and it's lowkey iconic.
β¨ NAME β¨
EVP_PKEY_verify_recover_init, EVP_PKEY_verify_recover_init_ex, EVP_PKEY_verify_recover_init_ex2, EVP_PKEY_verify_recover β recover a 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() initializes a public key algorithm context ctx for signing, using the algorithm that got picked when the context was created with EVP_PKEY_CTX_new(3) or one of its little variants. that algorithm is what gets used to fetch an EVP_SIGNATURE method implicitly β and if you wanna know the tea about implicit fetches, go read "Implicit fetch" in provider(7). it's all there, fr fr.
EVP_PKEY_verify_recover_init_ex() is literally the same as EVP_PKEY_verify_recover_init(), except it also sets the parameters you pass in params on the context before it hands control back to you. cute, right?
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. heads up though: a context ctx with no pre-loaded key cannot be used with this function. nope. not happening. π and depending on which algorithm got fetched, certain details revolving around the treatment of the input to EVP_PKEY_verify() may be pre-determined, and in that case those details may normally not be changed. if you want the deep explanation, scroll to "NOTES" below. i got you. π«Ά
EVP_PKEY_verify_recover() recovers signed data using ctx. the signature is specified with the sig and siglen parameters. now listen carefully because this part is the whole vibe:
- if
routisNULL, then the maximum size of the output buffer gets written to theroutlenparameter. π - if
routis notNULL, then before the call, theroutlenparameter should contain the length of theroutbuffer. if the call is successful, the recovered data is written torout, and the amount of data written goes intoroutlen. β
π NOTES π
ngl, normally an application is only interested in whether a signature verification operation is successful β and in those cases you should just use the EVP_verify() function. that's the move.
sometimes, though, it's genuinely useful to obtain the data that was originally signed using a signing operation. the catch: only certain public key algorithms can recover a signature this way (for example, RSA in PKCS padding mode). so don't be surprised if your fave algorithm isn't invited to the party. π
after the call to EVP_PKEY_verify_recover_init(), algorithm-specific control operations can be performed to set any appropriate parameters for the operation.
after the call to EVP_PKEY_verify_recover_init_ex2(), algorithm-specific control operations may not be needed if the chosen algorithm implies that those controls are pre-set (and immutable). the algorithm said "i already did it" and honestly? slay.
and the function 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 the context, bestie, it's giving efficient. π
β RETURN VALUES β
EVP_PKEY_verify_recover_init()andEVP_PKEY_verify_recover()return1for success, and0or a negative value for failure.- and specifically, a return value of
-2indicates the operation is not supported by the public key algorithm. π
so like, always check your return values. -2 is the "this algorithm said no β€οΈ" number. don't ignore it.
π EXAMPLES π
here's the glow-up: recovering a digest originally signed using PKCS#1 and SHA256 digest. verbatim, no notes:
#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 the two-pass thing? first pass with NULL to find out how big the buffer needs to be, second pass with the actual buffer. it's giving β¨planning aheadβ¨.
π 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)
go read those too, i'm literally begging you, they're all in the same friend group. π«Ά
π HISTORY π
- the
EVP_PKEY_verify_recover_init()andEVP_PKEY_verify_recover()functions were added in OpenSSL 1.0.0. throwback era. π°οΈ - the
EVP_PKEY_verify_recover_init_ex()function was added in OpenSSL 3.0.
πΈ COPYRIGHT πΈ
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!! this one shipped with OpenSSL 3.5.9 and it's dated 2026-09-29 β so yeah, you're reading certified fresh documentation π β¨ thanks for sticking with me through the signature recovery lore, you absolutely ate this. mwah. ππ
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms