EVP_PKEY_CTX_SET_TLS1_PRF_MD(3ossl)

πŸ’– EVP_PKEY_CTX_SET_TLS1_PRF_MD πŸ’–

okay bestie, pull up a chair, because we're talking about the OpenSSL page EVP_PKEY_CTX_SET_TLS1_PRF_MD(3ossl) and honestly? it's giving ✨cryptographic key derivation✨ and i am obsessed.


🌸 NAME 🌸

EVP_PKEY_CTX_set_tls1_prf_md, EVP_PKEY_CTX_set1_tls1_prf_secret, EVP_PKEY_CTX_add1_tls1_prf_seed β€” the TLS PRF key derivation algorithm. that's the whole vibe. periodt. πŸ’…


✨ SYNOPSIS ✨

#include <openssl/kdf.h>

int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *pctx, const EVP_MD *md);
int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *pctx,
                                      unsigned char *sec, int seclen);
int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *pctx,
                                    unsigned char *seed, int seedlen);

like, three little functions, three little glow-ups, no notes πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

so the EVP_PKEY_TLS1_PRF algorithm is literally the PRF key derivation function for TLS. it has no associated private key (it's not that kind of girl) and it only does key derivation, using EVP_PKEY_derive(3). main character energy only.

EVP_PKEY_set_tls1_prf_md() sets the message digest that's associated with the TLS PRF. and here's the tea: EVP_md5_sha1() is treated as a special case β€” it uses the PRF algorithm with both MD5 and SHA1, exactly as used in TLS 1.0 and 1.1. iconic behavior honestly.

EVP_PKEY_CTX_set_tls1_prf_secret() sets the secret value of the TLS PRF to seclen bytes of the buffer sec. heads up though: any existing secret value is replaced and any seed is reset. so like, don't come crying to me when your old seed ghosted you. 😭

EVP_PKEY_CTX_add1_tls1_prf_seed() sets the seed to seedlen bytes of seed. and if a seed is already set? it just gets appended to the existing value. no replacement, no drama, just stacking. 🫢


πŸŽ€ STRING CTRLS πŸŽ€

the TLS PRF also supports string based control operations via EVP_PKEY_CTX_ctrl_str(3). here's the breakdown bestie:

  • the type parameter "md" β€” uses the supplied value as the name of the digest algorithm to use.
  • the type parameters "secret" and "seed" β€” use the supplied value parameter as a secret or seed value.
  • the names "hexsecret" and "hexseed" β€” same energy, except they take a hex string which gets converted to binary. hex girl summer πŸ”₯

πŸ’œ NOTES πŸ’œ

you get a context for the TLS PRF by calling:

EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
okay so real talk: the digest, secret value and seed must be set before a key is derived, or an error occurs. no skipping steps, bestie, this is not a vibe check you can fail. πŸ’€

the total length of all seeds cannot exceed 1024 bytes in length β€” which, ngl, should be more than enough for any normal use of the TLS PRF. like, that's a lot of seed. 🌱

the output length of the PRF is specified by the length parameter in the EVP_PKEY_derive() function. and because the output length is variable, setting the buffer to NULL is not meaningful for the TLS PRF. so don't do it. πŸ™ˆ

oh and β€” optimised versions of the TLS PRF can be implemented in an ENGINE. fancy. πŸ‘‘


🌈 RETURN VALUES 🌈

all of these functions 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. that's the "it's not you, it's the algorithm" of it all. πŸ’”


⭐ EXAMPLES ⭐

this example derives 10 bytes using SHA-256 with the secret key "secret" and seed value "seed":

EVP_PKEY_CTX *pctx;
unsigned char out[10];
size_t outlen = sizeof(out);

pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL);
if (EVP_PKEY_derive_init(pctx) <= 0)
    /* Error */
if (EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_sha256()) <= 0)
    /* Error */
if (EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, "secret", 6) <= 0)
    /* Error */
if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, "seed", 4) <= 0)
    /* Error */
if (EVP_PKEY_derive(pctx, out, &outlen) <= 0)
    /* Error */

look at her go. ate and left no crumbs. ✨


🌸 SEE ALSO 🌸

EVP_PKEY_CTX_new(3), EVP_PKEY_CTX_ctrl_str(3), EVP_PKEY_derive(3)

go read those next, i'm literally begging you, iykyk 🧚


πŸ¦‹ HISTORY πŸ¦‹

all of the functions described here were converted from macros to functions in OpenSSL 3.0. glow-up of the century honestly. πŸ’…


Copyright 2016-2020 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 that's the whole page bestie, you're officially fluent πŸ’œ this was EVP_PKEY_CTX_SET_TLS1_PRF_MD(3ossl) from OpenSSL 3.5.9, dated 2026-09-2, and no cap, you ate this. bye bestie, go derive some keys responsibly πŸŽ€βœ¨πŸ«Ά

πŸ“¦ where this page comes from