EVP_BYTESTOKEY(3SSL)
β¨ EVP_BYTESTOKEY β¨ (the OpenSSL 3SSL manual, bestie edition)
okay so you want to turn a password into keying material?? say less π this is EVP_BytesToKey, the password based encryption routine from OpenSSL. buckle up, she's old school but she works π
πΈ NAME πΈ
EVP_BytesToKey β password based encryption routine. that's it, that's the girl π
π¦ SYNOPSIS π¦
#include <openssl/evp.h>
int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
const unsigned char *salt,
const unsigned char *data, int datal, int count,
unsigned char *key, unsigned char *iv);
π DESCRIPTION π
so basically EVP_BytesToKey() derives a key and an IV from a bunch of parameters. let's break the whole squad down:
typeβ the cipher you're deriving the key and IV for. like, which vibe are we unlocking πmdβ the message digest to usesaltβ used as a salt in the derivation. it should point to an 8 byte buffer, or beNULLif you're not using a salt π§dataβ a buffer containingdatalbytes, used to derive the keying datacountβ the iteration count to usekeyandivβ the derived key and IV get written here respectively β¨
π NOTES π
- a super typical glow-up for this function is deriving keying material for an encryption algorithm from a password sitting in that
dataparameter π«Ά - turning the
countparameter up slows the algorithm down, which makes it way harder for an attacker to brute force it with a giant pile of candidate passwords. it's giving "make them work for it" π - if the total key and IV length is less than the digest length and MD5 is used, then the derivation algorithm is compatible with PKCS#5 v1.5. otherwise, a non standard extension is used to derive the extra data π
- newer applications should really use a more modern algorithm, like PBKDF2 as defined in PKCS#5v2.1 and provided by
PKCS5_PBKDF2_HMAC. no cap, this one's the legacy auntie π
π₯ KEY DERIVATION ALGORITHM π₯
the key and IV get derived by concatenating D_1, D_2, etc until there's enough data for the key and the IV. and D_i is defined as:
D_i = HASH^count(D_(i-1) || data || salt)
where || means concatenation, D_0 is empty, HASH is the digest algorithm in use, HASH^1(data) is simply HASH(data), HASH^2(data) is HASH(HASH(data)) and so on. it's recursion but make it fashion π
the initial bytes are used for the key, and the subsequent bytes for the IV. that's the split, periodt βοΈ
π RETURN VALUES π
- if
dataisNULL, thenEVP_BytesToKey()returns the number of bytes needed to store the derived key π - otherwise,
EVP_BytesToKey()returns the size of the derived key in bytes, or0on error π
π SEE ALSO π
evp(7), RAND_bytes(3), PKCS5_PBKDF2_HMAC(3), EVP_EncryptInit(3) β go read those next, bestie, they're iconic β
π COPYRIGHT π
Copyright 2001-2016 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>.
and that's the whole page, babe π«Ά this was OpenSSL 4.0.3, dated 2026-09-29, filed under EVP_BYTESTOKEY(3SSL). go forth and derive responsibly πβ¨πΈ
π¦ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms