EVP_PKEY_DERIVE(3ossl)

✨ EVP_PKEY_DERIVE(3ossl) ✨

okay bestie, sit down, hydrate, and let me tell you about the 3ossl girlies' fave glow-up: deriving a shared secret πŸ”πŸ’– this whole page is the OpenSSL manual for EVP_PKEY_DERIVE, and no cap, it's lowkey iconic.


πŸŽ€ NAME πŸŽ€

EVP_PKEY_derive_init, EVP_PKEY_derive_init_ex, EVP_PKEY_derive_set_peer_ex, EVP_PKEY_derive_set_peer, EVP_PKEY_derive β€” derive public key algorithm shared secret πŸ’«


🌸 SYNOPSIS 🌸

#include <openssl/evp.h>

int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
int EVP_PKEY_derive_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]);
int EVP_PKEY_derive_set_peer_ex(EVP_PKEY_CTX *ctx, EVP_PKEY *peer,
                                int validate_peer);
int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);

πŸ’… DESCRIPTION πŸ’…

so EVP_PKEY_derive_init() sets up a public key algorithm context, ctx, for shared secret derivation. it uses whatever algorithm the context got when you made it with EVP_PKEY_CTX_new(3) or any of its little variants 🫢 that algorithm gets used to fetch an EVP_KEYEXCH method implicitly β€” if you wanna know the tea on implicit fetches, check "Implicit fetch" in provider(7), it's giving lore πŸ“–

EVP_PKEY_derive_init_ex() is literally the same as EVP_PKEY_derive_init() but it ALSO sets the passed parameters params on the context before it returns. extra, but make it fashion ✨

EVP_PKEY_derive_set_peer_ex() sets the peer key β€” which, normally, is gonna be a public key πŸ”‘ the validate_peer girlie will validate that public key if this value is non zero. yes ma'am, she checks πŸ‘‘

EVP_PKEY_derive_set_peer() is basically EVP_PKEY_derive_set_peer_ex() with validate_peer set to 1. simple. clean. periodt πŸ’…

EVP_PKEY_derive() derives a shared secret using ctx πŸ” if key is NULL, then the maximum size of the output buffer gets written to the keylen parameter. if key is not NULL, then before the call the keylen parameter should contain the length of the key buffer β€” and if the call succeeds, the shared secret gets written to key, and the amount of data written gets written to keylen. chef's kiss 🀌


πŸ“ NOTES πŸ“

after you call EVP_PKEY_derive_init(), you can do algorithm-specific control operations to set up any appropriate parameters for the operation. customize her πŸ’–

the function EVP_PKEY_derive() can be called more than once on the same context if you're doing several operations with the same parameters. she's reusable, we love a sustainable queen β™»οΈβœ¨


πŸ’€ RETURN VALUES πŸ’€

EVP_PKEY_derive_init() and EVP_PKEY_derive() return 1 for success and 0 or a negative value for failure 😭 and specifically: a return value of -2 means the operation is not supported by the public key algorithm. not me getting rejected by the algorithm πŸ’”


🌈 EXAMPLES 🌈

derive shared secret (for example DH or EC keys) β€” here's the whole thing, bestie, copy it exactly:

#include <openssl/evp.h>
#include <openssl/rsa.h>

EVP_PKEY_CTX *ctx;
ENGINE *eng;
unsigned char *skey;
size_t skeylen;
EVP_PKEY *pkey, *peerkey;
/* NB: assumes pkey, eng, peerkey have been already set up */

ctx = EVP_PKEY_CTX_new(pkey, eng);
if (!ctx)
    /* Error occurred */
if (EVP_PKEY_derive_init(ctx) <= 0)
    /* Error */
if (EVP_PKEY_derive_set_peer(ctx, peerkey) <= 0)
    /* Error */

/* Determine buffer length */
if (EVP_PKEY_derive(ctx, NULL, &skeylen) <= 0)
    /* Error */

skey = OPENSSL_malloc(skeylen);

if (!skey)
    /* malloc failure */

if (EVP_PKEY_derive(ctx, skey, &skeylen) <= 0)
    /* Error */

/* Shared secret is skey bytes written to buffer skey */

πŸ‘€ 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_verify_recover(3), EVP_KEYEXCH_fetch(3) β€” go read the whole friend group 🫢


πŸ¦‹ HISTORY πŸ¦‹

EVP_PKEY_derive_init(), EVP_PKEY_derive_set_peer() and EVP_PKEY_derive() were originally added in OpenSSL 1.0.0. the OGs πŸ’œ

EVP_PKEY_derive_init_ex() and EVP_PKEY_derive_set_peer_ex() were added in OpenSSL 3.0. the newer era ✨


Copyright 2006-2022 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>.

fr fr read the license bestie, it's not a vibe to skip it πŸ’…

and that's the whole page, bestie πŸŽ€ this was version 3.5.9, dated 2026-09-29, and it's the EVP_PKEY_DERIVE(3ossl) manual β€” go derive your shared secrets responsibly, mwah πŸ’–βœ¨

πŸ“¦ where this page comes from