EVP_PKEY_SIGN(3)

πŸ“¦ version 4.4.0, in alpine 3.24, arch rolling

πŸ’… 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 sig is NULL, the maximum size of the output buffer gets written to the siglen parameter. so you call it once with NULL just to ask "how big should my buffer be, bestie?" 🫢
  • if sig is not NULL, then before the call your siglen parameter needs to already contain the length of the sig buffer.
  • if the call succeeds, the signature gets written to sig, and the amount of data actually written goes into siglen ✨
⚠️ 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 the EVP_DigestSignInit(3) and EVP_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() and EVP_PKEY_sign() return 1 for success πŸŽ‰
  • they return 0 or a negative value for failure πŸ’€
  • and specifically: a return value of -2 means 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 πŸ‘‘


🧚 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