EVP_PKEY_SIGN(3)
π EVP_PKEY_SIGN(3) β like, the signing one
okay so bestie, welcome to the Library Functions Manual page for EVP_PKEY_sign_init and EVP_PKEY_sign π this is from libcrypto and it's literally the whole "sign stuff with a public key algorithm" situation. buckle up, it's giving cryptography π
β¨ NAME β¨
EVP_PKEY_sign_init, EVP_PKEY_sign β sign using a public key algorithm π₯
πΈ SYNOPSIS πΈ
library βlibcryptoβ
#include <openssl/evp.h>
int
EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx);
int
EVP_PKEY_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
const unsigned char *tbs, size_t tbslen);
π¦ DESCRIPTION π¦
so EVP_PKEY_sign_init() sets up a public key algorithm context for a signing operation, using the key ctx->pkey. that's it, that's the whole vibe. it's the "get ready to sign" moment π
then EVP_PKEY_sign() actually does the signing operation with ctx. the data you wanna sign goes in through the tbs and tbslen parameters π
here's the iconic two-step for the buffer, don't skip it:
- if
sigisNULL, the maximum size of the output buffer gets written to thesiglenparameter. so you call it once withNULLjust to ask "how big should my buffer be, bestie?" π«Ά - if
sigis notNULL, then before the call yoursiglenparameter needs to already contain the length of thesigbuffer. - if the call succeeds, the signature gets written to
sig, and the amount of data actually written goes intosiglenβ¨
β οΈ major heads-up:EVP_PKEY_sign()does not hash the data you're signing. so like, it's normally used to sign digests. if you wanna sign arbitrary messages, go look at theEVP_DigestSignInit(3)andEVP_SignInit(3)signing interfaces instead β that's your lane, not this one π
after you've called EVP_PKEY_sign_init(), you can do algorithm-specific control operations to set any appropriate parameters for the operation; peep EVP_PKEY_CTX_ctrl(3) for that π
also, EVP_PKEY_sign() can be called more than once on the same context if you're doing several operations with the same parameters. reuse, slay, periodt π
π RETURN VALUES π
EVP_PKEY_sign_init()andEVP_PKEY_sign()return1for success π- they return
0or a negative value for failure π - and specifically: a return value of
-2means the operation isn't supported by the public key algorithm. not us, not our problem, the algorithm said no π
β EXAMPLES β
sign data using RSA with PKCS#1 padding and a SHA-256 digest β this is the whole routine, copy-paste energy fr fr:
#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)
/* 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 = malloc(siglen);
if (!sig)
/* malloc failure */
if (EVP_PKEY_sign(ctx, sig, &siglen, md, mdlen) <= 0)
/* Error */
/* Signature is siglen bytes written to buffer sig */
see how it calls EVP_PKEY_sign() with NULL first to get siglen, then mallocs, then calls it again for real? that's the pattern, bestie, that's the move πβ¨
π SEE ALSO π
EVP_PKEY_CTX_ctrl(3)EVP_PKEY_CTX_new(3)EVP_PKEY_decrypt(3)EVP_PKEY_derive(3)EVP_PKEY_encrypt(3)EVP_PKEY_verify(3)EVP_PKEY_verify_recover(3)
π§ HISTORY π§
EVP_PKEY_sign_init() and EVP_PKEY_sign() first appeared in OpenSSL 1.0.0 and have been available since OpenBSD 4.9 π iconic longevity, honestly.
and that's the whole page babes π this one's from Debian, dated June 8, 2025 β signed, sealed, and delivered with a siglen of pure love π«Άπ ngl writing this made me want to go sign something immediately. mwah π
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2006, 2009, 2013, 2014 The OpenSSL Project
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms