EVP_PKEY_SIGN(3ossl)
π EVP_PKEY_SIGN(3ossl) β signing stuff, explained by your favorite Unix bestie π
okay so this is the <pink>OpenSSL</pink> manual page EVP_PKEY_SIGN(3ossl) and it's literally about signing using a public key algorithm. grab a boba, we're getting 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 these are the sign using a public key algorithm girlies π
πΈ 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);
π¦ DESCRIPTION π¦
okay bestie, here's the tea on every single one of these βπ
EVP_PKEY_sign_init()β initializes a public key algorithm contextctxfor signing, using the algorithm that was given when the context was created withEVP_PKEY_CTX_new(3)or variants thereof. the algorithm is used to fetch anEVP_SIGNATUREmethod implicitly β see "Implicit fetch" inprovider(7)for more info on implicit fetches. iykyk π₯EVP_PKEY_sign_init_ex()β same asEVP_PKEY_sign_init()but it additionally sets the passed parametersparamson the context before returning. it's giving init-but-with-extras β¨EVP_PKEY_sign_init_ex2()β 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). and listen β a contextctxwithout a pre-loaded key cannot be used with this function, no cap. this function provides almost the same functionality asEVP_PKEY_sign_init_ex(), but it's uniquely intended to be used with a pre-computed message digest, and it lets you pre-determine the exact conditions for that message digest, if a composite signature algorithm (such asRSA-SHA256) was fetched. following a call to this function, setting parameters that modifies the digest implementation or padding is not normally supported πEVP_PKEY_sign_message_init()β initializes a public key algorithm contextctxfor signing an unlimited size message using the algorithm given byalgoand the key given throughEVP_PKEY_CTX_new(3)orEVP_PKEY_CTX_new_from_pkey(3). passing the message is supported both in a one-shot fashion usingEVP_PKEY_sign(), and through the combination ofEVP_PKEY_sign_message_update()andEVP_PKEY_sign_message_final(). this function enables using algorithms that can process input of arbitrary length, such asED25519,RSA-SHA256and similar. main character energy fr fr πEVP_PKEY_sign_message_update()β addsinlenbytes frominto the data to be processed for signature. the signature algorithm specification and implementation determine how the input bytes are processed and if there's a limit on the total size of the input. see "NOTES" below for a deeper explanation πEVP_PKEY_sign_message_final()β signs the processed data and places the data insig, and the number of signature bytes in*siglen, if the number of bytes doesn't surpass the size given bysigsize.sigmay beNULL, and in that case, only*siglenis updated with the number of signature bytes. lowkey handy π«ΆEVP_PKEY_sign()β a one-shot function that can be used with all the init functions above. and the behavior depends on which init you used, so pay attention bestie:- when initialization was done 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 was done with
EVP_PKEY_sign_message_init(), the data specified bytbsandtbslenis digested by the implied message digest algorithm, and the result is signed after appropriate padding. - if
sigisNULLthen the maximum size of the output buffer is written to thesiglenparameter. - if
sigis notNULL, then before the call thesiglenparameter should contain the length of thesigbuffer, and if the call is successful the signature is written tosigand the amount of data written tosiglen. periodt π
- when initialization was done with
π₯ NOTES π₯
π General π
some signature implementations only accumulate the input data and do no further processing before signing it (they expect the input to be a digest), while others compress the data β typically by internally producing a digest β and signing the result. some of them support both modes of operation at the same time. the caller is expected to know how the chosen algorithm is supposed to behave and under what conditions. no hand-holding, bestie π
for example, an RSA implementation can be expected to only expect a message digest as input, while ED25519 can be expected to process the input with a hash β i.e. to produce the message digest internally β and while RSA-SHA256 can be expected to handle either mode of operation, depending on if the operation was initialized with EVP_PKEY_sign_init_ex2() or with EVP_PKEY_sign_message_init(). it's giving β¨versatileβ¨
similarly, an RSA implementation usually expects additional details to be set, like the message digest algorithm that the input is supposed to be digested with, as well as the padding mode (see 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 these details pre-set and immutable. so don't come for her settings, she's not changing them π
important warning bestie: the functions described here can't be used to combine separate algorithms. in particular, 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, it's not possible to specify actxpre-loaded with anRSApkey, or analgothat fetched"RSA"and try to specifySHA256separately to get the functionality ofRSA-SHA256. if combining algorithms in that manner is desired, please useEVP_DigestSignInit(3)and associated functions. slay responsibly π
π Performing multiple signatures π
when initialized using EVP_PKEY_sign_init_ex() or EVP_PKEY_sign_init_ex2(), EVP_PKEY_sign() can be called more than once on the same context to have several one-shot operations performed using the same parameters. reusing the vibe, we love efficiency β¨
when initialized using EVP_PKEY_sign_message_init(), it's not possible to call EVP_PKEY_sign() multiple times. one and done, that's the rule π
β RETURN VALUES β
all functions return 1 for success and 0 or a negative value for failure. simple as that π
in particular, EVP_PKEY_sign_init() and its other variants may return -2 to indicate that the operation is not supported by the public key algorithm. it's not a no, it's an "i literally can't" π
π EXAMPLES π
okay bestie, let's get into the actual code because this is where it gets iconic β¨
πΈ RSA with PKCS#1 padding for SHA256 πΈ
first up: 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 */
π¦ RSA-SHA256 with a pre-computed digest π¦
next: sign a digest with RSA-SHA256 using one-shot functions. important tea π«: 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. like, that's just the deal, no cap.
#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 */
π RSA-SHA256, one-shot π
third: sign a document with RSA-SHA256 using one-shot functions. same note applies: RSA-SHA256 is assumed to be an implementation of "sha256WithRSAEncryption", for which the padding is pre-determined to be RSA_PKCS1_PADDING. got it? good π
#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 */
π RSA-SHA256, using update and final π
fourth and final example: this is the same as the previous example, but allowing stream-like functionality. slay, we love a flexible queen π
#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 */
π SEE ALSO π
go give these other icons a little kiss too, bestie π
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 β
timeline tea π°οΈ:
EVP_PKEY_sign_init()andEVP_PKEY_sign()were added in OpenSSL 1.0.0.EVP_PKEY_sign_init_ex()was 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()were added in OpenSSL 3.4.
π COPYRIGHT π
Copyright 2006-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>.
okay bestie that's the whole page, mwah π β this was EVP_PKEY_SIGN(3ossl) from OpenSSL 3.5.0, dated 2026-09-25. now go sign something iconic β¨
π¦ 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