EVP_CIPHER_CTX_INIT(3)

πŸ“¦ version 4.1.0, in opensuse-leap 16.0

πŸ’… EVP_CIPHER_CTX_INIT(3) β€” Library Functions Manual

okay so bestie, buckle up πŸŽ€ this page is literally the "obsolete EVP cipher functions" page β€” the library-functions-manual one β€” and it's giving retirement home for C functions. we're talking EVP_CIPHER_CTX_init, EVP_CIPHER_CTX_cleanup, and EVP_Cipher, and honestly? they're all deprecated. like, all three. the whole squad. πŸ™ˆ


✨ NAME ✨

EVP_CIPHER_CTX_init, EVP_CIPHER_CTX_cleanup, EVP_Cipher β€” obsolete EVP cipher functions 🫢


🌸 SYNOPSIS 🌸

#include <openssl/evp.h>

int
EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx);

int
EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *ctx);

int
EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    const unsigned char *in, unsigned int in_len);

πŸ¦‹ DESCRIPTION πŸ¦‹

the init one (deprecated, gorl πŸ’€)

EVP_CIPHER_CTX_init() is a deprecated function that could be used to clear a cipher context on the stack back in the day β€” y'know, before EVP_CIPHER_CTX was made opaque. if you call it on a cipher context you just got back from EVP_CIPHER_CTX_new(3), it literally does nothing β€” no effect, zero, nada ✨

but calling it on a cipher context that was already used? yeah that may leak memory with older versions of the library 😭 so instead, do yourself a favor and use EVP_CIPHER_CTX_reset(3) or EVP_CIPHER_CTX_free(3). periodt.

the cleanup one (a deprecated alias, iykyk)

EVP_CIPHER_CTX_cleanup() is a deprecated alias for EVP_CIPHER_CTX_reset(3). it clears all the information from ctx and frees all allocated memory associated with it β€” everything except the ctx object itself 🫢 so the context container stays, but the vibes inside get yeeted.

the Cipher one (exposing dev secrets, not slay)

EVP_Cipher() exposes implementation details of EVP_CipherUpdate(3) and EVP_CipherFinal(3) that should never have become part of the public API. like, who let that happen 😭 not me, not you.

if the flag EVP_CIPH_FLAG_CUSTOM_CIPHER is set for the cipher used by ctx, behaviour depends on in:

  • if that argument is NULL and in_len is 0, behaviour is similar to EVP_CipherFinal(3)
  • if in_len is not 0, behaviour is undefined πŸ’€ (yes, undefined, like your ex's plans)
  • if in is not NULL, behaviour is similar to EVP_CipherUpdate(3)

in both of those cases, the exceptions to the similarity are that arguments and return values differ. so like… similar but not same same, yfm?

if the flag EVP_CIPH_FLAG_CUSTOM_CIPHER is not set for the cipher used by ctx, it encrypts or decrypts aligned blocks of data whose lengths match the cipher block size. it requires:

  • that the previous encryption or decryption operation using the same ctx, if there was any, ended exactly on a block boundary
  • that in_len is an integer multiple of the cipher block size

if either of these conditions is violated, EVP_Cipher() silently produces incorrect results 😭😭 that's the worst kind of bug, the quiet kind. for that reason, using the function EVP_CipherUpdate(3) instead is strongly recommended. the latter can safely handle partial blocks, and even if in_len actually is a multiple of the cipher block size for all calls, the overhead incurred by using EVP_CipherUpdate(3) is minimal. so like… just use the good one, bestie 🫢


πŸ‘‘ RETURN VALUES πŸ‘‘

  • EVP_CIPHER_CTX_init() β€” always returns 1. iconic, unbothered, never fails.
  • EVP_CIPHER_CTX_cleanup() β€” returns 1 for success or 0 for failure.
  • with EVP_CIPH_FLAG_CUSTOM_CIPHER, EVP_Cipher() returns the number of bytes written to out for success or -1 for failure.
  • without EVP_CIPH_FLAG_CUSTOM_CIPHER, EVP_Cipher() returns 1 for success or 0 for failure.

πŸ’œ SEE ALSO πŸ’œ

evp(3), EVP_EncryptInit(3) β€” go read those, they're the main characters here 🌈


⭐ HISTORY ⭐

  • EVP_Cipher() first appeared in SSLeay 0.6.5.
  • EVP_CIPHER_CTX_cleanup() first appeared in SSLeay 0.8.0.
  • EVP_CIPHER_CTX_init() first appeared in SSLeay 0.9.0.
  • all these functions have been available since OpenBSD 2.4.

so yeah, they've been around since forever β€” genuine elders of the codebase 🧚


πŸ“ CAVEATS πŸ“

checking the return value of EVP_Cipher() requires unusual caution: 0 signals success if EVP_CIPH_FLAG_CUSTOM_CIPHER is set β€” or failure otherwise. yes literally the same number means opposite things depending on a flag πŸ’€ that is not a vibe check that is a vibe trap, be careful out there fr fr.

mwah πŸ’‹ this lil page was brought to you by Debian, dated December 6, 2024 β€” EVP_CIPHER_CTX_INIT(3), out. go reset your contexts properly, ily 🫢✨

πŸ“¦ where this page comes from