EVP_PKEY_SIGN(3ossl)
π EVP_PKEY_SIGN(3ossl) β the OpenSSL signing glow-up π
okay so bestie, you know how a manpage header is like EVP_PKEY_SIGN(3ossl) and OpenSSL up in the corner? that's this page. it's the whole "sign stuff with a public key algorithm" saga, and honestly it ate. let's get into it π«Ά
β¨ NAME β¨
EVP_PKEY_sign_init, EVP_PKEY_sign_init_ex, EVP_PKEY_sign_init_ex2, EVP_PKEY_sign, EVP_PKEY_sign_message_init, EVP_PKEY_sign_message_update, EVP_PKEY_sign_message_final β all of them, together, signing using a public key algorithm. that's the whole vibe π
πΈ SYNOPSIS πΈ
#include <openssl/evp.h>
int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
int EVP_PKEY_sign_init_ex2(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *algo,
const OSSL_PARAM params[]);
int EVP_PKEY_sign_message_init(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *algo,
const OSSL_PARAM params[]);
int EVP_PKEY_sign_message_update(EVP_PKEY_CTX *ctx,
unsigned char *in, size_t inlen);
int EVP_PKEY_sign_message_final(EVP_PKEY_CTX *ctx, unsigned char *sig,
size_t *siglen, size_t sigsize);
int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
unsigned char *sig, size_t *siglen,
const unsigned char *tbs, size_t tbslen);
no cap, that's a lot of functions, but they all make sense once you meet them individually π
π¦ DESCRIPTION π¦
EVP_PKEY_sign_init()β bestie this one initializes a public key algorithm contextctxfor signing, using whatever algorithm was given when the context got created viaEVP_PKEY_CTX_new(3)or its little variants. the algorithm is used to fetch aEVP_SIGNATUREmethod implicitly, and if you wanna know what that even means, go read "Implicit fetch" inprovider(7)β it explains implicit fetches way better than I could over text πEVP_PKEY_sign_init_ex()β literally the same asEVP_PKEY_sign_init(), except it also sets the passed parametersparamson the context before it returns. that's it. that's the whole upgrade. iconic behavior honestly β¨EVP_PKEY_sign_init_ex2()β this one initializes a public key algorithm contextctxfor signing a pre-computed message digest, using the algorithm given byalgoand the key given throughEVP_PKEY_CTX_new(3)orEVP_PKEY_CTX_new_from_pkey(3). important: a contextctxthat has no pre-loaded key cannot be used with this function, periodt. it does almost the exact same thing asEVP_PKEY_sign_init_ex(), except it's uniquely intended for a pre-computed message digest, and it lets you pre-determine the exact conditions for that message digest β if, say, a composite signature algorithm (think RSA-SHA256) got fetched π. after you call this, setting parameters that mess with the digest implementation or padding is not normally supported, so likeβ¦ don't πEVP_PKEY_sign_message_init()β initializes a public key algorithm contextctxfor signing a message of unlimited size, using the algorithm given byalgoand the key given throughEVP_PKEY_CTX_new(3)orEVP_PKEY_CTX_new_from_pkey(3). you can pass the message one-shot style withEVP_PKEY_sign(), or do the combo ofEVP_PKEY_sign_message_update()+EVP_PKEY_sign_message_final(). this function is what lets you use algorithms that can process input of arbitrary length β ED25519, RSA-SHA256, and similar girlies πEVP_PKEY_sign_message_update()β addsinlenbytes frominto the data that's gonna be processed for the signature. the signature algorithm spec and implementation get to decide how those input bytes are processed and whether there's a limit on the total input size. scroll down to "NOTES" for the deeper lore on that one π§EVP_PKEY_sign_message_final()β signs the processed data and drops it intosig, then puts the number of signature bytes into*siglenβ¦ but only if that number of bytes doesn't surpass the size given bysigsize. and here's the cute part:sigmay beNULL, and in that case only*siglengets updated with the number of signature bytes. size-query era πEVP_PKEY_sign()β the one-shot baddie that works with all the init functions above. the behavior is giving "depends on how you initialized":- when initialization happened with
EVP_PKEY_sign_init(),EVP_PKEY_sign_init_ex()orEVP_PKEY_sign_init_ex2(), the data specified bytbsandtbslenis signed after appropriate padding. - when initialization happened with
EVP_PKEY_sign_message_init(), the data specified bytbsandtbslenis digested by the implied message digest algorithm first, and then the result is signed after appropriate padding. - if
sigisNULL, the maximum size of the output buffer gets written to thesiglenparameter π - if
sigis notNULL, then before the call thesiglenparameter should contain the length of thesigbuffer, and if the call succeeds, the signature is written tosigand the amount of data written goes intosiglen. slay π
- when initialization happened with
π NOTES π
π General π
okay so some signature implementations are literally just accumulating your input data and doing zero further processing before signing it β they expect the input to be a digest already. others compress the data, typically by internally producing a digest and signing that result. and some overachievers support both modes of operation at the same time π. the caller (that's you, bestie) is expected to know how the chosen algorithm is supposed to behave and under what conditions. iykyk.
for example:
- an RSA implementation can be expected to only expect a message digest as input.
- ED25519 can be expected to process the input with a hash, i.e. produce the message digest internally.
- RSA-SHA256 can be expected to handle either mode of operation, depending on whether the operation was initialized with
EVP_PKEY_sign_init_ex2()or withEVP_PKEY_sign_message_init(). main character energy, honestly π
similarly, an RSA implementation usually expects you to set additional details, like the message digest algorithm that the input is supposed to be digested with, plus the padding mode (peep EVP_PKEY_CTX_set_signature_md(3) and EVP_PKEY_CTX_set_rsa_padding(3) and similar others) β while an RSA-SHA256 implementation usually has all those details pre-set and immutable. no take-backsies π₯
β οΈ okay this next bit is important, don't scroll past it: the functions described here can't be used to combine separate algorithms. specifically, neitherEVP_PKEY_CTX_set_signature_md(3)nor theOSSL_PARAMparameter"digest"(OSSL_SIGNATURE_PARAM_DIGEST) can be used to combine a signature algorithm with a hash algorithm to process the input. in other words: you can't specify actxpre-loaded with an RSA pkey, or analgothat fetched"RSA", and then try to specify SHA256 separately to get the functionality of RSA-SHA256. not the vibe. if combining algorithms in that manner is what you desire, please useEVP_DigestSignInit(3)and associated functions instead π«Ά
π Performing multiple signatures π
- when initialized using
EVP_PKEY_sign_init_ex()orEVP_PKEY_sign_init_ex2(),EVP_PKEY_sign()can be called more than once on the same context, so you can have several one-shot operations performed using the same parameters. reusing the context? we love a sustainable queen πΈ - when initialized using
EVP_PKEY_sign_message_init(), it is not possible to callEVP_PKEY_sign()multiple times. one and done, bestie π
β RETURN VALUES β
all the functions return 1 for success, and 0 or a negative value for failure. that's the whole scale β¨
and in particular β listen β EVP_PKEY_sign_init() and its other variants may return -2 to indicate that the operation is not supported by the public key algorithm. not a crash, not a vibe check failure, just -2 π
(part 1 done, part 2 is still coming bestie, don't go anywhere π)
β¨ EXAMPLES β¨
okay bestie, this is the part where it actually gets good π β four whole worked examples, like a little tutorial arc. every code block is verbatim, so copy-paste away, no cap.
RSA with PKCS#1 padding for SHA256
so you wanna sign data using RSA with PKCS#1 padding and a SHA256 digest as input. here's the whole vibe:
#include <openssl/evp.h>
#include <openssl/rsa.h>
EVP_PKEY_CTX *ctx;
/* md is a SHA-256 digest in this example. */
unsigned char *md, *sig;
size_t mdlen = 32, siglen;
EVP_PKEY *signing_key;
/*
* NB: assumes signing_key and md are set up before the next
* step. signing_key must be an RSA private key and md must
* point to the SHA-256 digest to be signed.
*/
ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
if (ctx == NULL)
/* Error occurred */
if (EVP_PKEY_sign_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_sign(ctx, NULL, &siglen, md, mdlen) <= 0)
/* Error */
sig = OPENSSL_malloc(siglen);
if (sig == NULL)
/* malloc failure */
if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
/* Error */
/* Signature is siglen bytes written to buffer sig */
that two-pass thing where you call EVP_PKEY_sign() with a NULL buffer first just to learn the length? yeah, that's the whole move. icon behavior honestly π₯
RSA-SHA256 with a pre-computed digest
next up: signing a digest with RSA-SHA256 using one-shot functions. important note bestie β RSA-SHA256 is assumed to be an implementation of "sha256WithRSAEncryption", for which the padding is pre-determined to be RSA_PKCS1_PADDING, and the input digest is assumed to have been computed using SHA256. so like, the algorithm already knows what it's doing, you don't gotta tell it twice π«Ά
#include <openssl/evp.h>
#include <openssl/rsa.h>
EVP_PKEY_CTX *ctx;
/* md is a SHA-256 digest in this example. */
unsigned char *md, *sig;
size_t mdlen = 32, siglen;
EVP_PKEY *signing_key;
/*
* NB: assumes signing_key and md are set up before the next
* step. signing_key must be an RSA private key and md must
* point to the SHA-256 digest to be signed.
*/
ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
alg = EVP_SIGNATURE_fetch(NULL, "RSA-SHA256", NULL);
if (ctx == NULL)
/* Error occurred */
if (EVP_PKEY_sign_init_ex2(ctx, alg, NULL) <= 0)
/* Error */
/* Determine buffer length */
if (EVP_PKEY_sign(ctx, NULL, &siglen, md, mdlen) <= 0)
/* Error */
sig = OPENSSL_malloc(siglen);
if (sig == NULL)
/* malloc failure */
if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
/* Error */
/* Signature is siglen bytes written to buffer sig */
see how we swapped in EVP_PKEY_sign_init_ex2() and fetched the alg with EVP_SIGNATURE_fetch()? main character energy, truly π
RSA-SHA256, one-shot
okay so now we're signing a document with RSA-SHA256 using one-shot functions. same deal as before: RSA-SHA256 is assumed to be an implementation of "sha256WithRSAEncryption", for which the padding is pre-determined to be RSA_PKCS1_PADDING.
#include <openssl/evp.h>
#include <openssl/rsa.h>
EVP_PKEY_CTX *ctx;
/* in is the input in this example. */
unsigned char *in, *sig;
/* inlen is the length of the input in this example. */
size_t inlen, siglen;
EVP_PKEY *signing_key;
EVP_SIGNATURE *alg;
/*
* NB: assumes signing_key, in and inlen are set up before
* the next step. signing_key must be an RSA private key,
* in must point to data to be digested and signed, and
* inlen must be the size of the data in bytes.
*/
ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
alg = EVP_SIGNATURE_fetch(NULL, "RSA-SHA256", NULL);
if (ctx == NULL || alg == NULL)
/* Error occurred */
if (EVP_PKEY_sign_message_init(ctx, alg, NULL) <= 0)
/* Error */
/* Determine sig buffer length */
if (EVP_PKEY_sign(ctx, NULL, &siglen, in, inlen) <= 0)
/* Error */
sig = OPENSSL_malloc(siglen);
if (sig == NULL)
/* malloc failure */
if (EVP_PKEY_sign(ctx, sig, &siglen, in, inlen) <= 0)
/* Error */
/* Signature is siglen bytes written to buffer sig */
β¨ EVP_PKEY_sign_message_init() is the glow-up here β it lets the algorithm handle the digesting and the signing in one go, and you just hand it raw data. literally so convenient.
RSA-SHA256, using update and final
this one is the same as the previous example, but allowing stream-like functionality. so you can feed it data in chunks like a lil conveyor belt π
#include <openssl/evp.h>
#include <openssl/rsa.h>
EVP_PKEY_CTX *ctx;
/* in is the input in this example. */
unsigned char *in, *sig;
/* inlen is the length of the input in this example. */
size_t inlen, siglen;
EVP_PKEY *signing_key;
EVP_SIGNATURE *alg;
/*
* NB: assumes signing_key, in and inlen are set up before
* the next step. signing_key must be an RSA private key,
* in must point to data to be digested and signed, and
* inlen must be the size of the data in bytes.
*/
ctx = EVP_PKEY_CTX_new(signing_key, NULL /* no engine */);
alg = EVP_SIGNATURE_fetch(NULL, "RSA-SHA256", NULL);
if (ctx == NULL || alg == NULL)
/* Error occurred */
if (EVP_PKEY_sign_message_init(ctx, alg, NULL) <= 0)
/* Error */
while (inlen > 0) {
if (EVP_PKEY_sign_message_update(ctx, in, inlen)) <= 0)
/* Error */
if (inlen > 256) {
inlen -= 256;
in += 256;
} else {
inlen = 0;
}
}
/* Determine sig buffer length */
if (EVP_PKEY_sign_message_final(ctx, NULL, &siglen) <= 0)
/* Error */
sig = OPENSSL_malloc(siglen);
if (sig == NULL)
/* malloc failure */
if (EVP_PKEY_sign_message_final(ctx, sig, &siglen) <= 0)
/* Error */
/* Signature is siglen bytes written to buffer sig */
ngl bestie, clock the if (EVP_PKEY_sign_message_update(ctx, in, inlen)) <= 0) line β that's exactly how it's printed in the original, extra paren and all, so we're keeping it character for character π don't "fix" it, just be aware.
the loop chews through 256 bytes at a time with EVP_PKEY_sign_message_update(), then EVP_PKEY_sign_message_final() seals the deal β once to get the length, once to actually write it. chef's kiss π
π SEE ALSO π
go read these next, bestie, they're all linked lore:
EVP_PKEY_CTX_new(3)EVP_PKEY_CTX_ctrl(3)EVP_PKEY_encrypt(3)EVP_PKEY_decrypt(3)EVP_PKEY_verify(3)EVP_PKEY_verify_recover(3)EVP_PKEY_derive(3)
π HISTORY π
the receipts, in chronological order:
EVP_PKEY_sign_init()andEVP_PKEY_sign()β added in OpenSSL 1.0.0. the OGs π§EVP_PKEY_sign_init_ex()β added in OpenSSL 3.0.EVP_PKEY_sign_init_ex2(),EVP_PKEY_sign_message_init(),EVP_PKEY_sign_message_update()andEVP_PKEY_sign_message_final()β where added in OpenSSL 3.4. (yes, "where" β that's the original's wording, we're not touching it π)
Β©οΈ 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 whole page, bestie!! ππ this was EVP_PKEY_SIGN(3ossl), straight out of OpenSSL 3.5.6, dated 2026-04-07 β go forth and sign things, you absolute icon πΈβ¨π«Ά
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms