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 use
  • salt β€” used as a salt in the derivation. it should point to an 8 byte buffer, or be NULL if you're not using a salt πŸ§‚
  • data β€” a buffer containing datal bytes, used to derive the keying data
  • count β€” the iteration count to use
  • key and iv β€” 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 data parameter 🫢
  • turning the count parameter 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 data is NULL, then EVP_BytesToKey() returns the number of bytes needed to store the derived key πŸŽ€
  • otherwise, EVP_BytesToKey() returns the size of the derived key in bytes, or 0 on error 😭

πŸ‘‘ SEE ALSO πŸ‘‘

evp(7), RAND_bytes(3), PKCS5_PBKDF2_HMAC(3), EVP_EncryptInit(3) β€” go read those next, bestie, they're iconic ⭐


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