EVP_ENCRYPTINIT(3SSL)

๐ŸŽ€ EVP_ENCRYPTINIT(3SSL) ๐ŸŽ€

ok bestie, sit down, hydrate, because this is the EVP_ENCRYPTINIT(3SSL) manpage from OpenSSL and it is not a small page. it's giving "the entire EVP cipher API walked into the group chat at the same time" ๐Ÿ’– like, the NAME section alone is a whole guest list. we're doing it in parts, so breathe.

this part? just the guest list. nobody is encrypting anything yet, we are literally naming names. every single function below is documented on this page โ€” that's the whole point of a NAME section, and she is thorough ๐ŸŒธ

no cap, it's a lot. but we're not skipping a single one, because that's how manpages work and because i respect you too much to lie to you.


โœจ NAME โœจ

the tagline for the whole squad is literally EVP cipher routines. that's it, that's the one-liner.

so here's everybody, cute and grouped so your eyeballs don't fall out ๐Ÿ‘‘

๐Ÿ’œ she fetches, she refs, she frees (the object's whole life cycle)

  • EVP_CIPHER_fetch
  • EVP_CIPHER_up_ref
  • EVP_CIPHER_free

๐Ÿฆ‹ the context girlies โ€” new, reset, free, dup, copy

  • EVP_CIPHER_CTX_new
  • EVP_CIPHER_CTX_reset
  • EVP_CIPHER_CTX_free
  • EVP_CIPHER_CTX_dup
  • EVP_CIPHER_CTX_copy

๐Ÿ”ฅ the init / update / final pipeline (encrypt side)

  • EVP_EncryptInit_ex
  • EVP_EncryptInit_ex2
  • EVP_EncryptUpdate
  • EVP_EncryptFinal_ex

๐ŸŒธ the init / update / final pipeline (decrypt side)

  • EVP_DecryptInit_ex
  • EVP_DecryptInit_ex2
  • EVP_DecryptUpdate
  • EVP_DecryptFinal_ex

โœจ the "either direction, i don't care" versions

  • EVP_CipherInit_ex
  • EVP_CipherInit_ex2
  • EVP_CipherInit_SKEY
  • EVP_CipherUpdate
  • EVP_CipherFinal_ex

๐Ÿ’… the knobs and levers on the context

  • EVP_CIPHER_CTX_set_key_length
  • EVP_CIPHER_CTX_ctrl

๐ŸŽ€ the no-suffix originals (the blueprint, the blueprint)

  • EVP_EncryptInit
  • EVP_EncryptFinal
  • EVP_DecryptInit
  • EVP_DecryptFinal
  • EVP_CipherInit
  • EVP_CipherFinal
  • EVP_Cipher

๐Ÿงš pipeline era โ€” yes there's a whole separate squad for this

  • EVP_CIPHER_can_pipeline
  • EVP_CipherPipelineEncryptInit
  • EVP_CipherPipelineDecryptInit
  • EVP_CipherPipelineUpdate
  • EVP_CipherPipelineFinal

๐Ÿ”ญ looking a cipher up by name, nid or object

  • EVP_get_cipherbyname
  • EVP_get_cipherbynid
  • EVP_get_cipherbyobj

๐Ÿ‘‘ whose cipher even is this โ€” identity and description

  • EVP_CIPHER_is_a
  • EVP_CIPHER_get0_name
  • EVP_CIPHER_get0_description
  • EVP_CIPHER_names_do_all
  • EVP_CIPHER_get0_provider
  • EVP_CIPHER_get_nid

๐ŸŒˆ params era (the gettable / settable crowd)

  • EVP_CIPHER_get_params
  • EVP_CIPHER_gettable_params
  • EVP_CIPHER_get_block_size
  • EVP_CIPHER_get_key_length
  • EVP_CIPHER_get_iv_length
  • EVP_CIPHER_get_flags
  • EVP_CIPHER_get_mode
  • EVP_CIPHER_get_type

๐Ÿ“ cipher context interrogation, part one

  • EVP_CIPHER_CTX_cipher
  • EVP_CIPHER_CTX_get0_cipher
  • EVP_CIPHER_CTX_get1_cipher
  • EVP_CIPHER_CTX_get0_name
  • EVP_CIPHER_CTX_get_nid
  • EVP_CIPHER_CTX_get_params
  • EVP_CIPHER_gettable_ctx_params
  • EVP_CIPHER_CTX_gettable_params
  • EVP_CIPHER_CTX_set_params
  • EVP_CIPHER_settable_ctx_params
  • EVP_CIPHER_CTX_settable_params

๐Ÿซถ cipher context interrogation, part two (sizes & tag)

  • EVP_CIPHER_CTX_get_block_size
  • EVP_CIPHER_CTX_get_key_length
  • EVP_CIPHER_CTX_get_iv_length
  • EVP_CIPHER_CTX_get_tag_length

โญ flags, flags, flags

  • EVP_CIPHER_CTX_flags
  • EVP_CIPHER_CTX_set_flags
  • EVP_CIPHER_CTX_clear_flags
  • EVP_CIPHER_CTX_test_flags

๐Ÿ”ฎ misc context introspection

  • EVP_CIPHER_CTX_get_type
  • EVP_CIPHER_CTX_get_mode
  • EVP_CIPHER_CTX_get_num
  • EVP_CIPHER_CTX_set_num
  • EVP_CIPHER_CTX_is_encrypting

๐ŸŒ™ ASN.1 and padding business

  • EVP_CIPHER_param_to_asn1
  • EVP_CIPHER_asn1_to_param
  • EVP_CIPHER_CTX_set_padding
  • EVP_enc_null

๐Ÿ’€ the sweep-everything-and-everyone one

  • EVP_CIPHER_do_all_provided

๐Ÿง the short, chill short-form squad

  • EVP_CIPHER_nid
  • EVP_CIPHER_name
  • EVP_CIPHER_block_size
  • EVP_CIPHER_key_length
  • EVP_CIPHER_iv_length
  • EVP_CIPHER_flags
  • EVP_CIPHER_mode
  • EVP_CIPHER_type

๐ŸŽ and the matching short forms, but for the context

  • EVP_CIPHER_CTX_encrypting
  • EVP_CIPHER_CTX_nid
  • EVP_CIPHER_CTX_block_size
  • EVP_CIPHER_CTX_key_length
  • EVP_CIPHER_CTX_iv_length
  • EVP_CIPHER_CTX_tag_length
  • EVP_CIPHER_CTX_num
  • EVP_CIPHER_CTX_type
  • EVP_CIPHER_CTX_mode

ok so that's the whole NAME section. every single one of those is fair game for this page, and yes, they're all real, and yes, one manpage really does carry all of them ๐Ÿ˜ญ props to her, honestly.

next up we get into the SYNOPSIS, where all the arguments and return types show up and it gets very real ๐Ÿ‘€

โœจ SYNOPSIS โœจ

#include <openssl/evp.h>

EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
                             const char *properties);
int EVP_CIPHER_up_ref(EVP_CIPHER *cipher);
void EVP_CIPHER_free(EVP_CIPHER *cipher);
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void);
int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx);
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx);
EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in);
int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in);

int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                       ENGINE *impl, const unsigned char *key, const unsigned char *iv);
int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                        const unsigned char *key, const unsigned char *iv,
                        const OSSL_PARAM params[]);
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
                      int *outl, const unsigned char *in, int inl);
int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);

int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                       ENGINE *impl, const unsigned char *key, const unsigned char *iv);
int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                        const unsigned char *key, const unsigned char *iv,
                        const OSSL_PARAM params[]);
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
                      int *outl, const unsigned char *in, int inl);
int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);

int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                      ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc);
int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                       const unsigned char *key, const unsigned char *iv,
                       int enc, const OSSL_PARAM params[]);
int EVP_CipherInit_SKEY(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
                        EVP_SKEY *skey, const unsigned char *iv, size_t iv_len,
                        int enc, const OSSL_PARAM params[]);
int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
                     int *outl, const unsigned char *in, int inl);
int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);

int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                    const unsigned char *key, const unsigned char *iv);
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);

int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                    const unsigned char *key, const unsigned char *iv);
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);

int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                   const unsigned char *key, const unsigned char *iv, int enc);
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);

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

int EVP_CIPHER_can_pipeline(const EVP_CIPHER *cipher, int enc);
int EVP_CipherPipelineEncryptInit(EVP_CIPHER_CTX *ctx,
                                  const EVP_CIPHER *cipher,
                                  const unsigned char *key, size_t keylen,
                                  size_t numpipes,
                                  const unsigned char **iv, size_t ivlen);
int EVP_CipherPipelineDecryptInit(EVP_CIPHER_CTX *ctx,
                                  const EVP_CIPHER *cipher,
                                  const unsigned char *key, size_t keylen,
                                  size_t numpipes,
                                  const unsigned char **iv, size_t ivlen);
int EVP_CipherPipelineUpdate(EVP_CIPHER_CTX *ctx,
                             unsigned char **out, size_t *outl,
                             const size_t *outsize,
                             const unsigned char **in, const size_t *inl);
int EVP_CipherPipelineFinal(EVP_CIPHER_CTX *ctx,
                            unsigned char **outm, size_t *outl,
                            const size_t *outsize);

int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int cmd, int p1, void *p2);
int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key);
void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags);
void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags);
int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags);

const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
const EVP_CIPHER *EVP_get_cipherbynid(int nid);
const EVP_CIPHER *EVP_get_cipherbyobj(const ASN1_OBJECT *a);

int EVP_CIPHER_get_nid(const EVP_CIPHER *e);
int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name);
int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
                            void (*fn)(const char *name, void *data),
                            void *data);
const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher);
const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher);
const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher);
int EVP_CIPHER_get_block_size(const EVP_CIPHER *e);
int EVP_CIPHER_get_key_length(const EVP_CIPHER *e);
int EVP_CIPHER_get_iv_length(const EVP_CIPHER *e);
unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *e);
unsigned long EVP_CIPHER_get_mode(const EVP_CIPHER *e);
int EVP_CIPHER_get_type(const EVP_CIPHER *cipher);

const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx);
EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx);
const char *EVP_CIPHER_CTX_get0_name(const EVP_CIPHER_CTX *ctx);

int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[]);
int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[]);
int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[]);
const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher);
const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher);
const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher);
const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *ctx);
const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_type(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_mode(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num);
int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx);

int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);

void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
                                void (*fn)(EVP_CIPHER *cipher, void *arg),
                                void *arg);

#define EVP_CIPHER_nid EVP_CIPHER_get_nid
#define EVP_CIPHER_name EVP_CIPHER_get0_name
#define EVP_CIPHER_block_size EVP_CIPHER_get_block_size
#define EVP_CIPHER_key_length EVP_CIPHER_get_key_length
#define EVP_CIPHER_iv_length EVP_CIPHER_get_iv_length
#define EVP_CIPHER_flags EVP_CIPHER_get_flags
#define EVP_CIPHER_mode EVP_CIPHER_get_mode
#define EVP_CIPHER_type EVP_CIPHER_get_type
#define EVP_CIPHER_CTX_encrypting EVP_CIPHER_CTX_is_encrypting
#define EVP_CIPHER_CTX_nid EVP_CIPHER_CTX_get_nid
#define EVP_CIPHER_CTX_block_size EVP_CIPHER_CTX_get_block_size
#define EVP_CIPHER_CTX_key_length EVP_CIPHER_CTX_get_key_length
#define EVP_CIPHER_CTX_iv_length EVP_CIPHER_CTX_get_iv_length
#define EVP_CIPHER_CTX_tag_length EVP_CIPHER_CTX_get_tag_length
#define EVP_CIPHER_CTX_num EVP_CIPHER_CTX_get_num
#define EVP_CIPHER_CTX_type EVP_CIPHER_CTX_get_type
#define EVP_CIPHER_CTX_mode EVP_CIPHER_CTX_get_mode

The following function has been deprecated since OpenSSL 3.0, and can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):

const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx);

The following function has been deprecated since OpenSSL 1.1.0, and can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):

int EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx);

๐Ÿ’– DESCRIPTION ๐Ÿ’–

Okay bestie, so the EVP cipher routines are basically a โœจhigh-levelโœจ interface to certain symmetric ciphers. That's the whole vibe. ๐Ÿซถ

And the EVP_CIPHER type? That's a structure for cipher method implementation. Structural queen. ๐Ÿ‘‘

๐ŸŒธ EVP_CIPHER_fetch()

Fetches the cipher implementation for the given algorithm from any provider offering it, within the criteria given by the properties. If you wanna go deeper on that, check out "ALGORITHM FETCHING" in crypto(7) for further information.

โš ๏ธ Heads up: the returned value must eventually be freed with EVP_CIPHER_free().

Also, fetched EVP_CIPHER structures are reference counted. So track those refs, bestie, they don't clean up after themselves. ๐Ÿ’…

๐Ÿ“ EVP_CIPHER_up_ref()

Increments the reference count for an EVP_CIPHER structure. That's it, that's the function. Slay. โœจ

๐Ÿ’œ EVP_CIPHER_free()

Decrements the reference count for the fetched EVP_CIPHER structure. If the reference count drops to 0 then the structure is freed. And if the argument is NULL, nothing is done. Chill behaviour, honestly. ๐Ÿ˜Œ

๐Ÿฆ‹ EVP_CIPHER_CTX_new()

Allocates and returns a cipher context. New ctx, who dis. ๐ŸŽ€

๐ŸŒˆ EVP_CIPHER_CTX_free()

Clears all information from a cipher context and frees any allocated memory associated with it, including ctx itself. You should call this after all operations using a cipher are complete so that sensitive information does not remain in memory. Like, don't just leave your secrets lying around in RAM, that's not the move. ๐Ÿ™ˆ If the argument is NULL, nothing is done.

โญ EVP_CIPHER_CTX_dup()

Can be used to duplicate the cipher state from in. This is lowkey iconic when you wanna avoid multiple EVP_CIPHER_fetch() calls, or if large amounts of data are to be fed which only differ in the last few bytes. Copy-paste but make it cryptography. ๐Ÿ’ซ

๐Ÿงš EVP_CIPHER_CTX_copy()

Can be used to copy the cipher state from in to out. Simple as that. ๐Ÿซถ

๐Ÿ‘‘ EVP_CIPHER_CTX_ctrl()

Okay so this one is a legacy method. Past its era, respectfully. EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() is the mechanism that should be used to set and get parameters that are used by providers. That's the main character now. โญ

Performs cipher-specific control actions on the context ctx. The control command is hanging out in cmd, and any extra lil arguments go in p1 and p2. ๐Ÿ’…

EVP_CIPHER_CTX_ctrl() must be called after EVP_CipherInit_ex2(). Other restrictions may apply depending on the control type and the cipher implementation โ€” so yeah, it depends, bestie.

If this function happens to be used with a fetched EVP_CIPHER, it will translate the controls that are known to OpenSSL into OSSL_PARAM(3) parameters with keys defined by OpenSSL, and then call EVP_CIPHER_CTX_get_params() or EVP_CIPHER_CTX_set_params() as is appropriate for each control command. It's giving translation services, and it's serving. โœจ

See "CONTROLS" below for more info, including what translations are being done. ๐Ÿ‘‘


EVP_CIPHER_get_params()

Retrieves the requested list of algorithm params from a CIPHER cipher. Peep "PARAMETERS" below for the deets. ๐ŸŽ€

EVP_CIPHER_CTX_get_params()

Retrieves the requested list of params from a CIPHER context ctx. "PARAMETERS" below has your back. ๐Ÿฆ‹

EVP_CIPHER_CTX_set_params()

Sets the list of params into a CIPHER context ctx. Again: "PARAMETERS", below. ๐ŸŒธ

EVP_CIPHER_gettable_params()

Gets you a constant OSSL_PARAM(3) array that describes the retrievable parameters you can use with EVP_CIPHER_get_params(). it's the menu, basically. ๐Ÿ“œ

EVP_CIPHER_gettable_ctx_params() and EVP_CIPHER_CTX_gettable_params()

Gets you a constant OSSL_PARAM(3) array that describes the retrievable parameters that can be used with EVP_CIPHER_CTX_get_params(). ๐Ÿซถ

The difference, exactly:

  • EVP_CIPHER_gettable_ctx_params() returns the parameters that can be retrieved from the algorithm.
  • EVP_CIPHER_CTX_gettable_params() returns the parameters that can be retrieved in the context's current state.

EVP_CIPHER_settable_ctx_params() and EVP_CIPHER_CTX_settable_params()

Gets you a constant OSSL_PARAM(3) array that describes the settable parameters that can be used with EVP_CIPHER_CTX_set_params(). ๐Ÿ’–

And the same split as above, no cap:

  • EVP_CIPHER_settable_ctx_params() returns the parameters that can be set from the algorithm.
  • EVP_CIPHER_CTX_settable_params() returns the parameters that can be set in the context's current state.

EVP_EncryptInit_ex2()

Sets up the cipher context ctx for encryption with cipher type. ctx MUST NOT be NULL. ๐Ÿ˜ญ

type is typically supplied by calling EVP_CIPHER_fetch(). It may also be set using legacy functions such as EVP_aes_256_cbc(), but bestie, this is not recommended for new applications. ๐Ÿ™ˆ

key is the symmetric key to use and iv is the IV to use (if necessary) โ€” the actual number of bytes used for the key and the IV depends on the cipher, so don't just assume, k? The parameters params will be set on the context after initialisation.

It is possible to set all parameters to NULL except type in an initial call, and supply the remaining parameters in subsequent calls, all of which have type set to NULL. This is done when the default cipher parameters are not appropriate. ๐Ÿงš

For EVP_CIPH_GCM_MODE the IV will be generated internally if it is not specified. iconic behaviour honestly. โœจ

EVP_EncryptInit_ex()

This legacy function is the same as EVP_EncryptInit_ex2() except params will be NULL. Also: impl must be NULL. that's the whole vibe. ๐Ÿ’€

EVP_EncryptUpdate()

Encrypts inl bytes from the buffer in and writes the encrypted version to out. ๐Ÿ”ฅ

The pointers out and in may point to the same location, in which case the encryption will be done in-place. However, in-place encryption is guaranteed to work only if the encryption context (ctx) has processed data in multiples of the block size. If the context contains an incomplete data block from previous operations, in-place encryption will fail. sorry bestie, that's just the rules.

If out and in point to different locations, the two buffers must be disjoint โ€” otherwise the operation might fail or the outcome might be undefined. ๐Ÿšจ

ctx MUST NOT be NULL.

This function can be called multiple times to encrypt successive blocks of data. The amount of data written depends on the block alignment of the encrypted data, so here's the tea:

  • For most ciphers and modes, the amount of data written can be anything from zero bytes to (inl + cipher_block_size - 1) bytes.
  • For wrap cipher modes, the amount of data written can be anything from zero bytes to (inl rounded up to cipher_block_size + cipher_block_size) bytes.
  • For stream ciphers, the amount of data written can be anything from zero bytes to inl bytes.

Thus, the buffer pointed to by out must contain sufficient room for the operation being performed. The actual number of bytes written is placed in outl. ๐Ÿ‘‘

If padding is enabled (the default ๐ŸŽ€) then EVP_EncryptFinal_ex() encrypts the "final" data, that is any data that remains in a partial block. It uses standard block padding (aka PKCS padding) as described in the NOTES section, below. The encrypted final data is written to out, which should have sufficient space for one cipher block. The number of bytes written is placed in outl. After this function is called, the encryption operation is finished and no further calls to EVP_EncryptUpdate() should be made. periodt. โœ‹

If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more data, and it will return an error if any data remains in a partial block โ€” that is, if the total data length is not a multiple of the block size. ๐Ÿ’€

EVP_DecryptInit_ex2(), EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex()

These functions are the corresponding decryption operations. ๐Ÿฆ‹

EVP_DecryptFinal() will return an error code if padding is enabled and the final block is not correctly formatted. The parameters and restrictions are identical to the encryption operations. And yeah โ€” ctx MUST NOT be NULL. ๐Ÿ˜ญ

EVP_CipherInit_ex2(), EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex()

These functions can be used for decryption or encryption โ€” flexible queens. ๐Ÿ’… The operation performed depends on the value of the enc parameter:

  • set it to 1 for encryption
  • set it to 0 for decryption
  • set it to -1 to leave the value unchanged (the actual value of 'enc' being supplied in a previous call)

EVP_CipherInit_SKEY()

This function is similar to EVP_CipherInit_ex2() but accepts a symmetric key object of type EVP_SKEY as a key. same energy, different key delivery. ๐ŸŽ

EVP_CIPHER_CTX_reset()

Clears all information from a cipher context and frees up any allocated memory associated with it โ€” except the ctx itself. ๐Ÿงน

This function should be called anytime ctx is reused by another EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal() series of calls. reuse responsibly, bestie.

EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit()

Behave in a similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex() and EVP_CipherInit_ex() โ€” except if the type is not a fetched cipher, they use the default implementation of the type. ๐ŸŒˆ

EVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal()

Identical to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and EVP_CipherFinal_ex(). ๐Ÿซถ

In previous releases they also cleaned up the ctx, but this is no longer done and EVP_CIPHER_CTX_cleanup() must be called to free any context resources. not me still thinking about the old behaviour ๐Ÿ’€

EVP_Cipher()

Encrypts or decrypts a maximum inl amount of bytes from in and leaves the result in out. ๐Ÿ”ฅ

For legacy ciphers: if the cipher doesn't have the flag EVP_CIPH_FLAG_CUSTOM_CIPHER set, then inl must be a multiple of EVP_CIPHER_get_block_size(). If it isn't, the result is undefined. ๐Ÿ˜ญ If the cipher has that flag set, then inl can be any size.

Due to the constraints of the API contract of this function it shouldn't be used in applications, please consider using EVP_CipherUpdate() and EVP_CipherFinal_ex() instead. this one's giving "do not perceive me" energy ๐Ÿ™ˆ

EVP_CIPHER_can_pipeline()

This function checks if an EVP_CIPHER fetched using EVP_CIPHER_fetch() supports cipher pipelining. If the cipher supports pipelining, it returns 1, otherwise 0. โœ…

This function will return 0 for non-fetched ciphers such as EVP_aes_128_gcm(). And: there are currently no built-in ciphers that support pipelining. ๐Ÿ’€

Cipher pipelining support allows an application to submit multiple chunks of data in one set of EVP_CipherUpdate() / EVP_CipherFinal calls, thereby allowing the provided implementation to take advantage of parallel computing. This is beneficial for hardware accelerators as pipeline amortizes the latency over multiple chunks. it's giving efficiency slay. โšก

For non-fetched ciphers, EVP_CipherPipelineEncryptInit() or EVP_CipherPipelineDecryptInit() may be directly called, which will perform a fetch and return an error if a pipeline supported implementation is not found.

EVP_CipherPipelineEncryptInit(), EVP_CipherPipelineDecryptInit(), EVP_CipherPipelineUpdate() and EVP_CipherPipelineFinal()

These functions can be used to perform multiple encryption or decryption operations in parallel. ๐Ÿ‘‘

EVP_CIPHER_can_pipeline() may be called to check if the cipher supports pipelining. These functions are analogous to EVP_EncryptInit_ex2(), EVP_DecryptInit_ex2(), EVP_CipherUpdate() and EVP_CipherFinal() but take an array of pointers for iv, input and output buffers. ๐Ÿฆ‹

The key, of length keylen, is the symmetric key to use. The numpipes parameter specifies the number of parallel operations to perform. The numpipes cannot exceed EVP_MAX_PIPES. โœจ

The iv parameter is an array of buffer pointers, containing IVs. The array size must be equal to numpipes. The size of each IV buffer must be equal to ivlen. When IV is not provided, iv must be NULL, rather than an array of NULL pointers.

The in parameter takes an array of buffer pointers, each pointing to a buffer containing the input data. The buffers can be of different sizes. The inl parameter is an array of size_t, each specifying the size of the corresponding input buffer.

The out and outm parameters are arrays of buffer pointers, each pointing to a buffer where the output data will be written. The outsize parameter is an array of size_t, each specifying the size of the corresponding output buffer. The outl parameter is an array of size_t which will be updated with the size of the output data written to the corresponding output buffer.

For the size requirement of the output buffers, see the description of EVP_CipherUpdate(). ๐Ÿ‘€

The EVP_CipherPipelineUpdate() function can be called multiple times to encrypt successive blocks of data. For AAD data, the out, and outsize parameter should be NULL, rather than an array of NULL pointers. ๐ŸŽ€

๐Ÿ’ซ EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()

Hands you back an EVP_CIPHER structure when you pass in a cipher name, a cipher NID, or an ASN1_OBJECT structure respectively. Cute little trio fr. ๐Ÿซถ

EVP_get_cipherbyname() will straight up return NULL for algorithms like "AES-128-SIV", "AES-128-CBC-CTS" and "CAMELLIA-128-CBC-CTS" โ€” the ones that used to only be reachable through the low level interfaces. So if you're sitting there like "where is my cipher ๐Ÿ˜ญ", that's why bestie.

EVP_get_cipherbyname() is only still around for backwards compatibility with OpenSSL before version 3, and it is not the same vibe as EVP_CIPHER_fetch() because it does not attempt to "fetch" an implementation of the cipher. Also ๐Ÿ‘€ it only knows about ciphers that are built-in to OpenSSL and have an associated NID. Same energy for EVP_get_cipherbynid() and EVP_get_cipherbyobj() โ€” they also return objects with no associated implementation.

So when you actually use the cipher objects these functions hand you (like in a call to EVP_EncryptInit_ex()) an implementation of the cipher gets implicitly fetched from the loaded providers. And that fetch could fail if no suitable implementation is available. ๐Ÿ’€ Use EVP_CIPHER_fetch() instead when you wanna explicitly fetch the algorithm and an associated implementation from a provider. That's the move.

See "ALGORITHM FETCHING" in crypto(7) for more deets about fetching.

Oh and the cipher objects these functions return do not need to be freed with EVP_CIPHER_free(). Free real estate, no cleanup, we love that for us. โœจ


๐Ÿ’ซ EVP_CIPHER_get_nid() and EVP_CIPHER_CTX_get_nid()

Return the NID of a cipher when you pass an EVP_CIPHER or an EVP_CIPHER_CTX structure. The actual NID value is an internal value which may not have a corresponding OBJECT IDENTIFIER, iykyk. NID_undef gets returned if the nid is unknown, or if the cipher hasn't been properly initialized via a call to EVP_CipherInit.


๐Ÿ’ซ EVP_CIPHER_CTX_set_flags(), EVP_CIPHER_CTX_clear_flags() and EVP_CIPHER_CTX_test_flags()

Sets, clears and tests ctx flags. Peep "FLAGS" below for the whole tea. โ˜•

For provided ciphers, EVP_CIPHER_CTX_set_flags() should only be called after the fetched cipher has been assigned to the ctx. And it is recommended to use "PARAMETERS" instead. Noted bestie ๐Ÿ“.


๐Ÿ’ซ EVP_CIPHER_CTX_set_padding()

Enables or disables padding. You should call this after the context is set up for encryption or decryption with EVP_EncryptInit_ex2(), EVP_DecryptInit_ex2(), EVP_CipherInit_ex2(), or EVP_CipherInit_SKEY(). By default encryption operations are padded using standard block padding, and the padding is checked and removed when decrypting. ๐ŸŽ€

If the pad parameter is zero then no padding is performed, and then the total amount of data encrypted or decrypted must be a multiple of the block size or an error will occur. Bestie if it's not a multiple, it's giving error, no cap.

x MUST NOT be NULL. ๐Ÿšจ

๐Ÿ’ซ EVP_CIPHER_get_key_length() and EVP_CIPHER_CTX_get_key_length()

Return the key length of a cipher when passed an EVP_CIPHER or an EVP_CIPHER_CTX structure. The constant EVP_MAX_KEY_LENGTH is the maximum key length for all ciphers. ๐ŸŒˆ

Note ๐Ÿ‘‘: even though EVP_CIPHER_get_key_length() is fixed for a given cipher, the value of EVP_CIPHER_CTX_get_key_length() may be different for variable key length ciphers. So don't be out here assuming they match, ok?


๐Ÿ’ซ EVP_CIPHER_CTX_set_key_length()

Sets the key length of the cipher context. If the cipher is a fixed length cipher then attempting to set the key length to any value other than the fixed value is an error. Periodt. ๐Ÿ’…


๐Ÿ’ซ EVP_CIPHER_get_iv_length() and EVP_CIPHER_CTX_get_iv_length()

Return the IV length of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX. It will return zero if:

  • the cipher does not use an IV ๐Ÿšซ
  • the cipher has not yet been initialized within the EVP_CIPHER_CTX
  • or if the passed cipher is NULL

The constant EVP_MAX_IV_LENGTH is the maximum IV length for all ciphers.


๐Ÿ’ซ EVP_CIPHER_CTX_get_tag_length()

Returns the tag length of an AEAD cipher when you pass it an EVP_CIPHER_CTX. It will return zero if the cipher does not support a tag. And it returns a default value if the tag length has not been set. ๐Ÿท๏ธ


๐Ÿ’ซ EVP_CIPHER_get_block_size() and EVP_CIPHER_CTX_get_block_size()

Return the block size of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX structure. The constant EVP_MAX_BLOCK_LENGTH is also the maximum block length for all ciphers.

A value of 0 gets returned if, with EVP_CIPHER_get_block_size(), the cipher e is NULL; or, with EVP_CIPHER_CTX_get_block_size(), the context ctx is NULL or has not been properly initialized with a call to EVP_CipherInit. ๐Ÿฆ‹


๐Ÿ’ซ EVP_CIPHER_get_type() and EVP_CIPHER_CTX_get_type()

Return the type of the passed cipher or context. This "type" is the actual NID of the cipher OBJECT IDENTIFIER and as such it ignores the cipher parameters ๐Ÿ˜ณ โ€” like, 40 bit RC2 and 128 bit RC2 have the same NID. If the cipher does not have an object identifier or does not have ASN1 support, this function will return NID_undef.


๐Ÿ’ซ EVP_CIPHER_is_a()

Returns 1 if cipher is an implementation of an algorithm that's identifiable with name, otherwise 0. If cipher is a legacy cipher (read: it's the return value from the likes of EVP_aes128() rather than the result of an EVP_CIPHER_fetch()), then only cipher names registered with the default library context (see OSSL_LIB_CTX(3)) will be considered. ๐Ÿ‘‘


๐Ÿ’ซ EVP_CIPHER_get0_name() and EVP_CIPHER_CTX_get0_name()

Return the name of the passed cipher or context. For fetched ciphers with multiple names, only one of them is returned. See also EVP_CIPHER_names_do_all(). And bestie โ€”

cipher MUST NOT be NULL. ๐Ÿ˜ญ

๐Ÿ’ซ EVP_CIPHER_names_do_all()

Traverses all names for the cipher, and calls fn with each name and data. This is only useful with fetched EVP_CIPHERs. โœจ


๐Ÿ’ซ EVP_CIPHER_get0_description()

Returns a description of the cipher, meant for display and human consumption. The description is at the discretion of the cipher implementation. So basically whatever the cipher feels like saying about itself ๐Ÿ’….


๐Ÿ’ซ EVP_CIPHER_get0_provider()

Returns an OSSL_PROVIDER pointer to the provider that implements the given EVP_CIPHER.


๐Ÿ’ซ EVP_CIPHER_CTX_get0_cipher()

Returns the EVP_CIPHER structure when passed an EVP_CIPHER_CTX structure. EVP_CIPHER_CTX_get1_cipher() is the same except the ownership is passed to the caller. Both functions return NULL on error. ๐Ÿ™ˆ


๐Ÿ’ซ EVP_CIPHER_get_mode() and EVP_CIPHER_CTX_get_mode()

Return the block cipher mode:

  • EVP_CIPH_ECB_MODE
  • EVP_CIPH_CBC_MODE
  • EVP_CIPH_CFB_MODE
  • EVP_CIPH_OFB_MODE
  • EVP_CIPH_CTR_MODE
  • EVP_CIPH_GCM_MODE
  • EVP_CIPH_CCM_MODE
  • EVP_CIPH_XTS_MODE
  • EVP_CIPH_WRAP_MODE
  • EVP_CIPH_OCB_MODE
  • EVP_CIPH_SIV_MODE

If the cipher is a stream cipher then EVP_CIPH_STREAM_CIPHER is returned. ๐ŸŒŠ


๐Ÿ’ซ EVP_CIPHER_get_flags()

Returns any flags associated with the cipher. See "FLAGS" for a list of currently defined flags. ๐Ÿšฉ


๐Ÿ’ซ EVP_CIPHER_CTX_get_num() and EVP_CIPHER_CTX_set_num()

Gets or sets the cipher specific "num" parameter for the associated ctx. Built-in ciphers typically use this to track how much of the current underlying block has been "used" already. Lowkey their little usage counter. ๐Ÿ”ข


๐Ÿ’ซ EVP_CIPHER_CTX_is_encrypting()

Reports whether the ctx is being used for encryption or decryption. ๐Ÿ”


๐Ÿ’ซ EVP_CIPHER_CTX_flags()

A deprecated macro calling "EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))".

Do not use. ๐Ÿšซ๐Ÿ˜ญ Like, bestie, please. She's deprecated for a reason.

๐Ÿ’ซ EVP_CIPHER_param_to_asn1()

Sets the AlgorithmIdentifier "parameter" based on the passed cipher. This will typically include any parameters and an IV. The cipher IV (if any) must be set when this call is made. And this call should be made before the cipher is actually "used" (so, before any EVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). ๐Ÿ“

This function may fail if the cipher does not have any ASN1 support, or if an uninitialized cipher is passed to it. ๐Ÿ’€


๐Ÿ’ซ EVP_CIPHER_asn1_to_param()

Sets the cipher parameters based on an ASN1 AlgorithmIdentifier "parameter". The precise effect depends on the cipher. In the case of RC2, for example, it will set the IV and effective key length. ๐ŸŽ€

This function should be called after the base cipher type is set but before the key is set. Like so:

  1. EVP_CipherInit() will be called with the IV and key set to NULL
  2. EVP_CIPHER_asn1_to_param() will be called
  3. finally EVP_CipherInit() again with all parameters except the key set to NULL

It is possible for this function to fail if the cipher does not have any ASN1 support, or the parameters cannot be set (for example the RC2 effective key length is not supported. ๐Ÿ™ˆ


๐Ÿ’ซ EVP_CIPHER_CTX_rand_key()

Generates a random key of the appropriate length based on the cipher context. The EVP_CIPHER can provide its own random key generation routine to support keys of a specific form. key must point to a buffer at least as big as the value returned by EVP_CIPHER_CTX_get_key_length(). ๐ŸŽฒ


๐Ÿ’ซ EVP_CIPHER_do_all_provided()

Traverses all ciphers implemented by all activated providers in the given library context libctx, and for each of the implementations, calls the given function fn with the implementation method and the given arg as argument. Doing the most, every single one ๐Ÿซถ.


โœจ PARAMETERS โœจ

See OSSL_PARAM(3) for info about passing parameters.

๐Ÿงš Gettable EVP_CIPHER parameters

When EVP_CIPHER_fetch() is called it internally calls EVP_CIPHER_get_params() and caches the results. Sneaky but efficient ๐Ÿ’….

EVP_CIPHER_get_params() can be used with the following OSSL_PARAM(3) keys:

  • "mode" (OSSL_CIPHER_PARAM_MODE) <unsigned integer> โ€” Gets the mode for the associated cipher algorithm cipher. See "EVP_CIPHER_get_mode() and EVP_CIPHER_CTX_get_mode()" for a list of valid modes. Use EVP_CIPHER_get_mode() to retrieve the cached value. ๐ŸŒˆ
  • "keylen" (OSSL_CIPHER_PARAM_KEYLEN) <unsigned integer> โ€” Gets the key length for the associated cipher algorithm cipher. Use EVP_CIPHER_get_key_length() to retrieve the cached value. ๐Ÿ”‘ -
  • "ivlen" (OSSL_CIPHER_PARAM_IVLEN) <unsigned integer> โ€” Gets the IV length for the associated cipher algorithm cipher. Use EVP_CIPHER_get_iv_length() to retrieve the cached value. ๐ŸŒŠ
  • "blocksize" (OSSL_CIPHER_PARAM_BLOCK_SIZE) <unsigned integer> โ€” Gets the block size for the associated cipher algorithm cipher. The block size should be 1 for stream ciphers. ๐Ÿ‘€

Note that the block size for a cipher may be different to the block size for the underlying encryption/decryption primitive. For example AES in CTR mode has a block size of 1 (because it operates like a stream cipher), even though AES has a block size of 16. Use EVP_CIPHER_get_block_size() to retrieve the cached value. ๐Ÿฆ‹

  • "aead" (OSSL_CIPHER_PARAM_AEAD) <integer> โ€” gives u 1 if this is an AEAD cipher algorithm, and 0 if it's not. Wanna grab that cached value urself? Use (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER). slay ๐Ÿ’…
  • "custom-iv" (OSSL_CIPHER_PARAM_CUSTOM_IV) <integer> โ€” 1 if the cipher algorithm cipher has a custom IV, 0 if not. And bestie, if a custom IV is in play, then storing it and initializing it is left entirely to the implementation โ€” not ur problem. Wanna peek at the cached value? (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_CUSTOM_IV).
  • "cts" (OSSL_CIPHER_PARAM_CTS) <integer> โ€” 1 if the cipher algorithm cipher uses ciphertext stealing, else 0. rn this is used to flag that the cipher is a one shot that only allows a single call to EVP_CipherUpdate(). Cached value lives at (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CTS).
  • "tls-multi" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK) <integer> โ€” 1 if the cipher algorithm cipher supports interleaving of crypto blocks, 0 if not. ngl the interleaving is an optimization only applicable to certain TLS ciphers, so don't come for it. Cached value: (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK).
  • "has-randkey" (OSSL_CIPHER_PARAM_HAS_RANDKEY) <integer> โ€” 1 if the cipher algorithm cipher supports the gettable EVP_CIPHER_CTX parameter OSSL_CIPHER_PARAM_RANDOM_KEY. ๐Ÿ™ˆ and likeโ€ฆ only DES and 3DES set this to 1, every other OpenSSL cipher returns 0. no crumbs for anyone else.
  • "encrypt-then-mac" (OSSL_CIPHER_PARAM_ENCRYPT_THEN_MAC) <integer> โ€” 1 if the cipher algorithm cipher supports TLS Encrypt-then-MAC, otherwise u get a 0.
  • "decrypt-only" (OSSL_CIPHER_PARAM_DECRYPT_ONLY) <integer> โ€” 1 if the cipher algorithm cipher implementation supports only the decryption operation, like the 3DES ciphers in the fips provider. Otherwise u get 0, or the parameter might not even be present at all. yeah. it's giving mystery โœจ

๐Ÿซถ Gettable and Settable EVP_CIPHER_CTX parameters ๐Ÿซถ

These OSSL_PARAM(3) keys work with both EVP_CIPHER_CTX_get_params() and EVP_CIPHER_CTX_set_params(). versatile queens ๐Ÿ‘‘

  • "padding" (OSSL_CIPHER_PARAM_PADDING) <unsigned integer> โ€” gets or sets the padding mode for the cipher context ctx. Padding is enabled if the value is 1, and disabled if it's 0. See also EVP_CIPHER_CTX_set_padding().
  • "num" (OSSL_CIPHER_PARAM_NUM) <unsigned integer> โ€” gets or sets the cipher specific "num" parameter for the cipher context ctx. Built-in ciphers typically use this to track how much of the current underlying block has already been "used". See also EVP_CIPHER_CTX_get_num() and EVP_CIPHER_CTX_set_num().
  • "keylen" (OSSL_CIPHER_PARAM_KEYLEN) <unsigned integer> โ€” gets or sets the key length for the cipher context ctx. Heads up: the length of the "keylen" parameter should not exceed that of a size_t. See also EVP_CIPHER_CTX_get_key_length() and EVP_CIPHER_CTX_set_key_length().
  • "tag" (OSSL_CIPHER_PARAM_AEAD_TAG) <octet string> โ€” gets or sets the AEAD tag for the associated cipher context ctx. Check "AEAD INTERFACE" in EVP_EncryptInit(3) for the deets.
  • "pipeline-tag" (OSSL_CIPHER_PARAM_PIPELINE_AEAD_TAG) <octet ptr> โ€” gets or sets the AEAD tag when ur using cipher pipelining. The pointer must point to an array of buffers, where the aead tag will be read from or written to. And the array size must be equal to numpipes used in EVP_CipherPipelineEncryptInit() or EVP_CipherPipelineDecryptInit(). no shortcuts bestie ๐Ÿ’€
  • "keybits" (OSSL_CIPHER_PARAM_RC2_KEYBITS) <unsigned integer> โ€” gets or sets the effective keybits used for a RC2 cipher. Same lil warning as before: the length of the "keybits" parameter should not exceed that of a size_t.
  • "rounds" (OSSL_CIPHER_PARAM_ROUNDS) <unsigned integer> โ€” gets or sets the number of rounds to be used for a cipher. This one's for the RC5 cipher. ๐ŸŽ€
  • "algorithm-id" (OSSL_CIPHER_PARAM_ALGORITHM_ID) <octet string> โ€” used to get the DER encoded AlgorithmIdentifier from the cipher implementation. Functions like EVP_PKEY_CTX_get_algor(3) use this parameter.
  • "algorithm-id-params" (OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS) <octet string> โ€” used to pass the DER encoded AlgorithmIdentifier parameter to or from the cipher implementation. Functions like EVP_CIPHER_CTX_set_algor_params(3) and EVP_CIPHER_CTX_get_algor_params(3) use this parameter.
  • "alg_id_params" (OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD) <octet string> โ€” a deprecated alias for "algorithm-id-params", only used by EVP_CIPHER_param_to_asn1(3) and EVP_CIPHER_asn1_to_param(3). it's giving legacy era, we don't use it anymore fr ๐Ÿ’€
  • "cts_mode" (OSSL_CIPHER_PARAM_CTS_MODE) <UTF8 string> โ€” gets or sets the cipher text stealing mode. For all modes the output size is the same as the input size, and the input length must be greater than or equal to the block size. (The block size for AES and CAMELLIA is 16 bytes โ€” noting it down bestie ๐Ÿ“)

Valid values for the mode are:

  • "CS1" โ€” the NIST variant of cipher text stealing. For input lengths that are multiples of the block size it is equivalent to using a "AES-XXX-CBC" or "CAMELLIA-XXX-CBC" cipher, otherwise the second last cipher text block is a partial block.
  • "CS2" โ€” for input lengths that are multiples of the block size it is equivalent to using a "AES-XXX-CBC" or "CAMELLIA-XXX-CBC" cipher, otherwise it is the same as "CS3" mode.
  • "CS3" โ€” the Kerberos5 variant of cipher text stealing, which always swaps the last cipher text block with the previous block (which may be a partial or full block depending on the input length). If the input length is exactly one full block then this is equivalent to using a "AES-XXX-CBC" or "CAMELLIA-XXX-CBC" cipher.

The default is "CS1". And this is only supported for "AES-128-CBC-CTS", "AES-192-CBC-CTS", "AES-256-CBC-CTS", "CAMELLIA-128-CBC-CTS", "CAMELLIA-192-CBC-CTS" and "CAMELLIA-256-CBC-CTS". periodt ๐Ÿ’…

  • "tls1multi_interleave" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE) <unsigned integer> โ€” sets or gets the number of records being sent in one go for a tls1 multiblock cipher operation (either 4 or 8 records). pick ur lane, 4 or 8, there's no in between โœจ

๐Ÿ’– Gettable EVP_CIPHER_CTX parameters ๐Ÿ’–

The following OSSL_PARAM(3) keys can be used with EVP_CIPHER_CTX_get_params() โ€” read-only cuties only, no setting:

  • "ivlen" (OSSL_CIPHER_PARAM_IVLEN and OSSL_CIPHER_PARAM_AEAD_IVLEN) <unsigned integer> โ€” gets the IV length for the cipher context ctx. And yk the drill: the length of the "ivlen" parameter should not exceed that of a size_t. See also EVP_CIPHER_CTX_get_iv_length().
  • "iv" (OSSL_CIPHER_PARAM_IV) <octet string OR octet ptr> โ€” gets the IV used to initialize the associated cipher context ctx. See also EVP_CIPHER_CTX_get_original_iv().
  • "updated-iv" (OSSL_CIPHER_PARAM_UPDATED_IV) <octet string OR octet ptr> โ€” gets the updated pseudo-IV state for the associated cipher context, e.g., the previous ciphertext block for CBC mode, or the iteratively encrypted IV value for OFB mode. Heads up tho: octet pointer access is deprecated and is provided only for backwards compatibility with historical libcrypto APIs. See also EVP_CIPHER_CTX_get_updated_iv().
  • "randkey" (OSSL_CIPHER_PARAM_RANDOM_KEY) <octet string> โ€” gets an implementation specific randomly generated key for the associated cipher context ctx. rn this is only supported by DES and 3DES (which set the key to odd parity). fcfs, everyone else stay seated ๐Ÿช‘
  • "taglen" (OSSL_CIPHER_PARAM_AEAD_TAGLEN) <unsigned integer> โ€” gets the tag length to be used for an AEAD cipher for the associated cipher context ctx. It gets a default value if it hasn't been set. The length of the "taglen" parameter should not exceed that of a size_t. See also EVP_CIPHER_CTX_get_tag_length().
  • "tlsaadpad" (OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD) <unsigned integer> โ€” gets the length of the tag that will be added to a TLS record for the AEAD tag, for the associated cipher context ctx. And once again: the length of the "tlsaadpad" parameter should not exceed that of a size_t.
  • "tlsivgen" (OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN) <octet string> โ€” gets the invocation field generated for encryption. Can only be called after "tlsivfixed" is set. And this is only used for GCM mode. ๐Ÿ“
  • "tls1multi_enclen" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN) <unsigned integer> โ€” get the total length of the record returned from the "tls1multi_enc" operation. it's giving receipts ๐Ÿ“
  • "tls1multi_maxbufsz" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE) <unsigned integer> โ€” gets the maximum record length for a TLS1 multiblock cipher operation. bestie. u already know. the length of the "tls1multi_maxbufsz" parameter should not exceed that of a size_t. ๐Ÿ’€
  • "tls1multi_aadpacklen" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN) <unsigned integer> โ€” gets the result of running the "tls1multi_aad" operation.
  • "tls-mac" (OSSL_CIPHER_PARAM_TLS_MAC) <octet ptr> โ€” used to pass the TLS MAC data. short n sweet โœจ
  • "fips-indicator" (OSSL_CIPHER_PARAM_FIPS_APPROVED_INDICATOR) <integer> โ€” this option is used by the OpenSSL FIPS provider.

It's a getter that returns 1 if the operation is FIPS approved, or 0 otherwise. U can use it after calling a cipher final operation such as EVP_EncryptFinal_ex(). It may return 0 if the "encrypt-check" option is set to 0. For GCM encryption it also returns 0 if the IV was supplied externally instead of being generated by the FIPS provider. The separate "iv-generated" parameter reports the IV source directly, but callers can use "fips-indicator" to determine the overall approval status. And for GCM encryption and decryption, tag lengths other than 4, 8, or 12 through 16 bytes also cause this indicator to return 0. note it down ๐Ÿ““

  • "iv-generated" (OSSL_CIPHER_PARAM_AEAD_IV_GENERATED) <unsigned integer> โ€” an indicator that returns 1 if an IV was generated internally during encryption, or 0 otherwise. GCM ciphers may use this after calling a cipher final operation such as EVP_EncryptFinal_ex(). GCM should generate an IV internally if the IV isn't specified during a cipher initialisation call such as EVP_CipherInit_ex(). See FIPS 140-3 IG C.H for information related to IV requirements. โœจ

๐ŸŒธ Settable EVP_CIPHER_CTX parameters ๐ŸŒธ

The following OSSL_PARAM(3) keys can be used with EVP_CIPHER_CTX_set_params() โ€” set it and forget it energy:

  • "mackey" (OSSL_CIPHER_PARAM_AEAD_MAC_KEY) <octet string> โ€” sets the MAC key used by composite AEAD ciphers such as AES-CBC-HMAC-SHA256. go off, composite queen ๐Ÿ‘‘
  • "speed" (OSSL_CIPHER_PARAM_SPEED) โ€” unsigned integer energy. Sets the speed option for the cipher context it's attached to. Ngl this one's only supported by AES SIV ciphers, and those lowkey refuse to do multiple operations by default ๐Ÿ™…โ™€๏ธ. Set "speed" to 1 and suddenly you're allowed to do another encrypt or decrypt operation. It's giving performance testing, fr fr.
  • "use-bits" (OSSL_CIPHER_PARAM_USE_BITS) โ€” unsigned integer bestie. This decides whether the input length inl that you pass to EVP_EncryptUpdate(), EVP_DecryptUpdate() and EVP_CipherUpdate() is the number of bits or the number of bytes. Set "use-bits" to 1 and we're counting bits ๐Ÿ’…. The default? Bytes. Also heads up, this is only used for CFB1 ciphers.

You can also set this with EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) โœจ

  • "tls-version" (OSSL_CIPHER_PARAM_TLS_VERSION) โ€” integer. Sets the TLS version. That's it, that's the whole vibe.
  • "tls-mac-size" (OSSL_CIPHER_PARAM_TLS_MAC_SIZE) โ€” unsigned integer. Set the TLS MAC size ๐ŸŽ€.
  • "tlsaad" (OSSL_CIPHER_PARAM_AEAD_TLS1_AAD) โ€” octet string. Sets TLSv1.2 AAD information for the associated cipher context ctx. And like, TLSv1.2 AAD information is always 13 bytes long, no exceptions, and it's exactly as defined for the "additional_data" field described in section 6.2.3.3 of RFC5246 ๐Ÿ“œ.
  • "tlsivfixed" (OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED) โ€” octet string. Sets the fixed portion of an IV for an AEAD cipher that's being used in TLS record encryption/decryption for the associated cipher context. Strap in bestie, this one's a whole saga:

TLS record encryption/decryption always happens "in place", so the input and output buffers are always the exact same memory location ๐Ÿ‘ฏ. AEAD IVs in TLSv1.2 are made of an implicit "fixed" part plus an explicit part that shifts with every single record. Setting a TLS fixed IV flips a cipher into encrypt/decrypt TLS records mode. TLS records get encrypted/decrypted using a single OSSL_FUNC_cipher_cipher call per record. For a record decryption, the first bytes of the input buffer are the explicit part of the IV, and the final bytes of the input buffer are the AEAD tag. How long the explicit IV part is and how long the tag is? Depends on the cipher in use, and it's defined in the RFC for the relevant ciphersuite ๐Ÿ“–. And so "in place" decryption can work, the plaintext output should be written to the same spot in the output buffer that the ciphertext payload was read from โ€” i.e. immediately after the explicit IV.

When you're encrypting a record, the first bytes of the input buffer should be empty so there's room for the explicit IV, and same for the final bytes where the tag gets written. The length of the input buffer will include the explicit IV length, the payload, and the tag bytes. The cipher implementation's job is to generate the explicit IV and write it to the beginning of the output buffer, do "in place" encryption of the payload and write that to the output buffer, and finally slap the tag onto the end of the output buffer ๐ŸŽ€.

Whether you're encrypting or decrypting, the value written to *outl in the OSSL_FUNC_cipher_cipher call should be the length of the payload excluding the explicit IV length and the tag length. Don't forget that part, it's sneaky ๐Ÿ˜ญ

  • "tlsivinv" (OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV) โ€” octet string. Sets the invocation field used for decryption. Can only be called after "tlsivfixed" is set, so order matters bestie ๐Ÿ™ˆ. Also this is only used for GCM mode.
  • "tls1multi_enc" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC) โ€” octet string. Triggers a multiblock TLS1 encrypt operation for a TLS1 aware cipher that supports yeeting 4 or 8 records in one go ๐Ÿ”ฅ. The cipher does both the MAC and encrypt stages and constructs the record headers itself, main character energy. "tls1multi_enc" supplies the output buffer for the encrypt operation, and "tls1multi_encin" & "tls1multi_interleave" must also be set so there are values to feed the encrypt operation.
  • "tls1multi_encin" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN) โ€” octet string. Supplies the data to encrypt for a TLS1 multiblock cipher operation ๐Ÿ’–.
  • "tls1multi_maxsndfrag" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT) โ€” unsigned integer. Sets the maximum send fragment size for a TLS1 multiblock cipher operation. It must be set before you use "tls1multi_maxbufsz" (order is everything ๐Ÿ’…). Also the length of the "tls1multi_maxsndfrag" parameter should not exceed that of a size_t.
  • "tls1multi_aad" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD) โ€” octet string. Sets the authenticated additional data used by a TLS1 multiblock cipher operation. The supplied data is 13 bytes of record data, laid out like this:
    • Bytes 0-7: The sequence number of the first record
    • Byte 8: The record type
    • Byte 9-10: The protocol version
    • Byte 11-12: Input length (Always 0)

"tls1multi_interleave" must also be set for this operation, okurrr โœจ

  • "xts_standard" (OSSL_CIPHER_PARAM_XTS_STANDARD) โ€” UTF8 string. Sets the XTS standard to use with the SM4-XTS algorithm. XTS mode has two implementations: one is standardized in IEEE Std. 1619-2007 and has been widely used (e.g., XTS AES), and the other was proposed recently (GB/T 17964-2021, implemented in May 2022) and is currently only used in SM4 ๐Ÿฆ‹.

The big difference between them is the multiplication by the primitive element ฮฑ to calculate the tweak values. IEEE Std 1619-2007 noted that the multiplication "is a left shift of each byte by one bit with carry propagating from one byte to the next one", which means in each byte the leftmost bit is the most significant bit. But in GB/T 17964-2021, the rightmost bit is the most significant bit, so the multiplication becomes a right shift of each byte by one bit with carry propagating from one byte to the next one. Literally mirror images bestie ๐Ÿชž

Valid values for the mode are:

  • "GB" โ€” The GB/T 17964-2021 variant of SM4-XTS algorithm.
  • "IEEE" โ€” The IEEE Std. 1619-2007 variant of SM4-XTS algorithm.

The default value is "GB" ๐Ÿ‘‘

  • "encrypt-check" (OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK) โ€” integer. This option is used by the OpenSSL FIPS provider ๐Ÿงš.

If required, this parameter should be set early via a cipher encrypt init function such as EVP_EncryptInit_ex2(). The default value of 1 causes an error when an encryption operation is triggered. Setting this to 0 will ignore the error and set the approved "fips-indicator" to 0.

โš ๏ธ Heads up bestie: this option breaks FIPS compliance if it causes the approved "fips-indicator" to return 0. So like, know what you're doing ๐Ÿ’€

๐Ÿ’… CONTROLS ๐Ÿ’…

So the mappings from EVP_CIPHER_CTX_ctrl() identifiers over to PARAMETERS are all listed in the next bit, bestie. If you want the deep lore on each one, go peek at the PARAMETERS section.

EVP_CIPHER_CTX_ctrl() is your girl for sending these standard controls:

  • EVP_CTRL_AEAD_SET_IVLEN and EVP_CTRL_GET_IVLEN โ€” when you use these with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() get called with an OSSL_PARAM(3) item whose key is "ivlen" (OSSL_CIPHER_PARAM_IVLEN).
  • EVP_CTRL_AEAD_SET_IV_FIXED โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item keyed "tlsivfixed" (OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED).
  • EVP_CTRL_AEAD_SET_MAC_KEY โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item keyed "mackey" (OSSL_CIPHER_PARAM_AEAD_MAC_KEY).
  • EVP_CTRL_AEAD_SET_TAG and EVP_CTRL_AEAD_GET_TAG โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() get called with an OSSL_PARAM(3) item keyed "tag" (OSSL_CIPHER_PARAM_AEAD_TAG).
  • EVP_CTRL_CCM_SET_L โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item keyed "ivlen" (OSSL_CIPHER_PARAM_IVLEN) with a value of (15 - L).
  • EVP_CTRL_COPY โ€” there is literally no OSSL_PARAM mapping for this one, bestie. Use EVP_CIPHER_CTX_copy() instead.
  • EVP_CTRL_GCM_SET_IV_INV โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item keyed "tlsivinv" (OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV).
  • EVP_CTRL_RAND_KEY โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item keyed "randkey" (OSSL_CIPHER_PARAM_RANDOM_KEY).
  • EVP_CTRL_SET_KEY_LENGTH โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item keyed "keylen" (OSSL_CIPHER_PARAM_KEYLEN).
  • EVP_CTRL_SET_RC2_KEY_BITS and EVP_CTRL_GET_RC2_KEY_BITS โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() get called with an OSSL_PARAM(3) item keyed "keybits" (OSSL_CIPHER_PARAM_RC2_KEYBITS).
  • EVP_CTRL_SET_RC5_ROUNDS and EVP_CTRL_GET_RC5_ROUNDS โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() get called with an OSSL_PARAM(3) item keyed "rounds" (OSSL_CIPHER_PARAM_ROUNDS).
  • EVP_CTRL_SET_SPEED โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item keyed "speed" (OSSL_CIPHER_PARAM_SPEED).
  • EVP_CTRL_GCM_IV_GEN โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_get_params() gets called with an OSSL_PARAM(3) item keyed "tlsivgen" (OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN).
  • EVP_CTRL_AEAD_TLS1_AAD โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item keyed "tlsaad" (OSSL_CIPHER_PARAM_AEAD_TLS1_AAD), and then EVP_CIPHER_CTX_get_params() gets called with a key of "tlsaadpad" (OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD).
  • EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item keyed OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, and then EVP_CIPHER_CTX_get_params() gets called with a key of "tls1multi_maxbufsz" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE).
  • EVP_CTRL_TLS1_1_MULTIBLOCK_AAD โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with OSSL_PARAM(3) items keyed "tls1multi_aad" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD) and "tls1multi_interleave" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE), and then EVP_CIPHER_CTX_get_params() gets called with keys of "tls1multi_aadpacklen" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN) and "tls1multi_interleave" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE).
  • EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT โ€” with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with OSSL_PARAM(3) items keyed "tls1multi_enc" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC), "tls1multi_encin" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN) and "tls1multi_interleave" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE), and then EVP_CIPHER_CTX_get_params() gets called with a key of "tls1multi_enclen" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN).

๐Ÿšฉ FLAGS ๐Ÿšฉ

EVP_CIPHER_CTX_set_flags(), EVP_CIPHER_CTX_clear_flags() and EVP_CIPHER_CTX_test_flags() are the trio you use to mess with and check these EVP_CIPHER_CTX flags:

  • EVP_CIPH_NO_PADDING โ€” used by EVP_CIPHER_CTX_set_padding(). Also go look at Gettable and Settable EVP_CIPHER_CTX parameters โ†’ "padding".
  • EVP_CIPH_FLAG_LENGTH_BITS โ€” see Settable EVP_CIPHER_CTX parameters โ†’ "use-bits".
  • EVP_CIPHER_CTX_FLAG_WRAP_ALLOW โ€” legacy purposes only, bestie. This flag had to be set to signal that the cipher handled wrapping.

EVP_CIPHER_flags() also uses the following flags, and these ones have mappings over to Gettable EVP_CIPHER parameters:

  • EVP_CIPH_FLAG_AEAD_CIPHER โ€” see Gettable EVP_CIPHER parameters โ†’ "aead".
  • EVP_CIPH_CUSTOM_IV โ€” see Gettable EVP_CIPHER parameters โ†’ "custom-iv".
  • EVP_CIPH_FLAG_CTS โ€” see Gettable EVP_CIPHER parameters โ†’ "cts".
  • EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK; โ€” see Gettable EVP_CIPHER parameters โ†’ "tls-multi".
  • EVP_CIPH_RAND_KEY โ€” see Gettable EVP_CIPHER parameters โ†’ "has-randkey".
  • EVP_CIPH_FLAG_ENC_THEN_MAC โ€” see Gettable EVP_CIPHER parameters โ†’ "encrypt-then-mac".
  • EVP_CIPH_STREAM_CIPHER, EVP_CIPH_ECB_MODE EVP_CIPH_CBC_MODE, EVP_CIPH_CFB_MODE, EVP_CIPH_OFB_MODE, EVP_CIPH_CTR_MODE, EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE, EVP_CIPH_XTS_MODE, EVP_CIPH_WRAP_MODE, EVP_CIPH_OCB_MODE, EVP_CIPH_SIV_MODE โ€” see Gettable EVP_CIPHER parameters โ†’ "mode".

EVP_CIPHER_flags() also uses the flag EVP_CIPH_FLAG_CUSTOM_CIPHER to signal that the cipher supports any input length when it's used with the EVP_Cipher() function. More deets live in EVP_Cipher().

And these flags down here are defined for legacy purposes and are not returned by EVP_CIPHER_flags():

  • EVP_CIPH_VARIABLE_LENGTH
  • EVP_CIPH_ALWAYS_CALL_INIT
  • EVP_CIPH_CTRL_INIT
  • EVP_CIPH_CUSTOM_KEY_LENGTH
  • EVP_CIPH_CUSTOM_COPY
  • EVP_CIPH_FLAG_DEFAULT_ASN1

๐ŸŒˆ RETURN VALUES ๐ŸŒˆ

  • EVP_CIPHER_fetch() โ€” returns a pointer to a EVP_CIPHER when it slays, and NULL when it flops.
  • EVP_CIPHER_up_ref() โ€” returns 1 on success or 0 otherwise.
  • EVP_CIPHER_CTX_new() โ€” returns a pointer to a freshly created EVP_CIPHER_CTX on success and NULL on failure.
  • EVP_CIPHER_CTX_dup() โ€” returns a new EVP_CIPHER_CTX if it worked, or NULL on failure.
  • EVP_CIPHER_CTX_copy() โ€” returns 1 if it worked or 0 for failure.
  • EVP_EncryptInit_ex2(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex() โ€” return 1 for success and 0 for failure.
  • EVP_DecryptInit_ex2() and EVP_DecryptUpdate() โ€” return 1 for success and 0 for failure. And EVP_DecryptFinal_ex() returns 0 if the decrypt failed, or 1 for success.
  • EVP_CipherInit_ex2(), EVP_CipherInit_SKEY() and EVP_CipherUpdate() โ€” return 1 for success and 0 for failure. EVP_CipherFinal_ex() returns 0 for an encryption/decryption failure or 1 for success.
  • EVP_Cipher() โ€” returns 1 on success and <= 0 on failure, if the flag EVP_CIPH_FLAG_CUSTOM_CIPHER is not set for the cipher, or if the cipher hasn't been initialized via a call to EVP_CipherInit_ex2. But if the flag EVP_CIPH_FLAG_CUSTOM_CIPHER is set for the cipher, EVP_Cipher() returns the number of bytes written to out for encryption/decryption, or the number of bytes authenticated in a call that specifies AAD for an AEAD cipher.
  • EVP_CIPHER_can_pipeline() โ€” returns 1 if the cipher can be used in a pipeline, 0 otherwise.
  • EVP_CipherPipelineEncryptInit() and EVP_CipherPipelineDecryptInit() โ€” return 1 for success and 0 for failure.
  • EVP_CipherPipelineUpdate() and EVP_CipherPipelineFinal() โ€” return 1 for success and 0 for failure.
  • EVP_CIPHER_CTX_reset() โ€” returns 1 for success and 0 for failure.
  • EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj() โ€” return an EVP_CIPHER structure or NULL on error.
  • EVP_CIPHER_get_nid() and EVP_CIPHER_CTX_get_nid() โ€” return a NID.
  • EVP_CIPHER_get_block_size() and EVP_CIPHER_CTX_get_block_size() โ€” return the block size, or 0 on error.
  • EVP_CIPHER_get_key_length() and EVP_CIPHER_CTX_get_key_length() โ€” return the key length.
  • EVP_CIPHER_CTX_set_padding() โ€” always returns 1. Iconic consistency, honestly.
  • EVP_CIPHER_get_iv_length() and EVP_CIPHER_CTX_get_iv_length() โ€” return the IV length, zero if the cipher doesn't use an IV, and a negative value on error.
  • EVP_CIPHER_CTX_get_tag_length() โ€” returns the tag length or zero if the cipher doesn't use a tag.
  • EVP_CIPHER_get_type() and EVP_CIPHER_CTX_get_type() โ€” return the NID of the cipher's OBJECT IDENTIFIER, or NID_undef if it has no defined OBJECT IDENTIFIER.
  • EVP_CIPHER_CTX_cipher() โ€” returns an EVP_CIPHER structure.
  • EVP_CIPHER_CTX_get_num() โ€” returns a nonnegative num value, or EVP_CTRL_RET_UNSUPPORTED if the implementation doesn't support the call or on any other error.
  • EVP_CIPHER_CTX_set_num() โ€” returns 1 on success and 0 if the implementation doesn't support the call or on any other error.
  • EVP_CIPHER_CTX_is_encrypting() โ€” returns 1 if the ctx is set up for encryption, 0 otherwise.
  • EVP_CIPHER_param_to_asn1() and EVP_CIPHER_asn1_to_param() โ€” return greater than zero for success and zero or a negative number on failure.
  • EVP_CIPHER_CTX_rand_key() โ€” returns 1 for success and zero or a negative number for failure.
  • EVP_CIPHER_names_do_all() โ€” returns 1 if the callback was called for all names. A return value of 0 means the callback was not called for any names.
  • EVP_CIPHER_get_params(), EVP_CIPHER_CTX_get_params() and EVP_CIPHER_CTX_set_params() โ€” return 1 for success and 0 for failure.

๐Ÿ” CIPHER LISTING ๐Ÿ”

Okay so, every algorithm has a fixed key length unless it says otherwise, babes.

Go check SEE ALSO for the full list of ciphers you can get through the EVP interface. ๐Ÿ‘‘

  • EVP_enc_null() โ€” the Null cipher: it literally does nothing. Queen of minding her own business. ๐Ÿ’€

๐Ÿ’– AEAD INTERFACE ๐Ÿ’–

okay bestie so the EVP interface for Authenticated Encryption with Associated Data (AEAD) modes? It's subtly altered โœจ and there are extra ctrl operations that show up depending on which mode you picked. slay.

So like, if you wanna specify additional authenticated data (AAD), you make a call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with the output parameter out set to NULL. In that case, on success, the parameter outl gets set to the number of AAD bytes processed in that call (aka, the value of inl), and it does not include any plaintext or ciphertext bytes processed by other calls. ๐Ÿ’…

If you're not using any AAD, you can just skip this call entirely. (Check the mode-specific notes below tho, there are exceptions ๐Ÿ‘€)

When you're decrypting, the return value of EVP_DecryptFinal() or EVP_CipherFinal() is what tells you whether the operation was successful. If it doesn't indicate success, the authentication operation has failed and any output data MUST NOT be used, because it is corrupted. like, it's giving garbage, throw it away ๐Ÿ˜ญ

pls note that the number of authenticated bytes returned by EVP_CipherUpdate() depends on the cipher you're using. Stream ciphers, like ChaCha20 or ciphers in GCM mode, can handle 1 byte at a time, which gives you an effective "block" size of 1. Meanwhile ciphers in OCB mode must process data one block at a time, and the block size is what gets returned.

Regardless of the returned size, it is safe to pass unpadded data to an EVP_CipherUpdate() call in a single operation. ๐Ÿ‘

๐ŸŒˆ GCM and OCB Modes ๐ŸŒˆ

The following ctrls are supported in GCM and OCB modes.

  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL) โ€” Sets the IV length. This call can only be made before specifying an IV. If you don't call it, a default IV length is used.

For GCM AES and OCB AES the default is 12 (i.e. 96 bits). For OCB mode the maximum is 15.

  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag) โ€” Writes taglen bytes of the tag value to the buffer indicated by tag. This call can only be made when encrypting data and after all data has been processed (e.g. after an EVP_EncryptFinal() call).

For OCB, taglen must either be 16 or the value previously set via EVP_CTRL_AEAD_SET_TAG.

  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag) โ€” When decrypting, this call sets the expected tag to taglen bytes from tag. taglen must be between 1 and 16 inclusive. The tag must be set prior to any call to EVP_DecryptFinal() or EVP_DecryptFinal_ex().

For GCM, this call is only valid when decrypting data.

For OCB, this call is valid when decrypting data to set the expected tag, and when encrypting to set the desired tag length.

In OCB mode, calling this with tag set to NULL sets the tag length. The tag length can only be set before specifying an IV. If this is not called prior to setting the IV, then a default tag length is used.

For OCB AES, the default tag length is 16 (i.e. 128 bits). It is also the maximum tag length for OCB.


๐ŸŽ€ CCM Mode ๐ŸŽ€

The EVP interface for CCM mode is similar to that of the GCM mode but with a few additional requirements and different ctrl values.

For CCM mode, the total plaintext or ciphertext length MUST be passed to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with the output and input parameters (in and out) set to NULL and the length passed in the inl parameter. no skipping this step bestie, it's mandatory fr fr.

The following ctrls are supported in CCM mode.

  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag) โ€” This call is made to set the expected CCM tag value when decrypting, or the length of the tag (with the tag parameter set to NULL) when encrypting. The tag length is often referred to as M. If not set a default value is used (12 for AES). When decrypting, the tag needs to be set before passing in data to be decrypted, but as in GCM and OCB mode, it can be set after passing additional authenticated data (see "AEAD INTERFACE").
  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, ivlen, NULL) โ€” Sets the CCM L value. If not set a default is used (8 for AES).
  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL) โ€” Sets the CCM nonce (IV) length. This call can only be made before specifying a nonce value. The nonce length is given by 15 - L so it is 7 by default for AES.

๐Ÿฆ‹ SIV Mode ๐Ÿฆ‹

Both the AES-SIV and AES-GCM-SIV ciphers fall under this mode.

For SIV mode ciphers the behaviour of the EVP interface is subtly altered and several additional ctrl operations are supported.

To specify any additional authenticated data (AAD) and/or a Nonce, a call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with the output parameter out set to NULL.

RFC5297 states that the Nonce is the last piece of AAD before the actual encrypt/decrypt takes place. The API does not differentiate the Nonce from other AAD. ๐Ÿคทโ™€๏ธ

When decrypting the return value of EVP_DecryptFinal() or EVP_CipherFinal() indicates if the operation was successful. If it does not indicate success the authentication operation has failed and any output data MUST NOT be used as it is corrupted.

The API does not store the SIV (Synthetic Initialization Vector) in the cipher text. Instead, it is stored as the tag within the EVP_CIPHER_CTX. The SIV must be retrieved from the context after encryption, and set into the context before decryption. ๐Ÿ‘‘

This differs from RFC5297 in that the cipher output from encryption, and the cipher input to decryption, does not contain the SIV. This also means that the plain text and cipher text lengths are identical. iconic, honestly.

The following ctrls are supported in SIV mode, and are used to get and set the Synthetic Initialization Vector:

  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag); โ€” Writes taglen bytes of the tag value (the Synthetic Initialization Vector) to the buffer indicated by tag. This call can only be made when encrypting data and after all data has been processed (e.g. after an EVP_EncryptFinal() call). For SIV mode the taglen must be 16.
  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag); โ€” Sets the expected tag (the Synthetic Initialization Vector) to taglen bytes from tag. This call is only legal when decrypting data and must be made before any data is processed (e.g. before any EVP_DecryptUpdate() calls). For SIV mode the taglen must be 16.

SIV mode makes two passes over the input data, so only one call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with out set to a non-NULL value. A call to EVP_DecryptFinal() or EVP_CipherFinal() is not required, but it will indicate if the update operation succeeded. ๐Ÿ’–


๐Ÿ’œ ChaCha20-Poly1305 ๐Ÿ’œ

The following ctrls are supported for the ChaCha20-Poly1305 AEAD algorithm.

  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL) โ€” Sets the nonce length. This call is now redundant since the only valid value is the default length of 12 (i.e. 96 bits). Prior to OpenSSL 3.0 a nonce of less than 12 bytes could be used to automatically pad the iv with leading 0 bytes to make it 12 bytes in length.
  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag) โ€” Writes taglen bytes of the tag value to the buffer indicated by tag. This call can only be made when encrypting data and after all data has been processed (e.g. after an EVP_EncryptFinal() call).

taglen specified here must be 16 (POLY1305_BLOCK_SIZE, i.e. 128-bits) or less.

  • EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag) โ€” Sets the expected tag to taglen bytes from tag. The tag length can only be set before specifying an IV. taglen must be between 1 and 16 (POLY1305_BLOCK_SIZE) inclusive. This call is only valid when decrypting data.

โœจ NOTES โœจ

Where possible the EVP interface to symmetric ciphers should be used in preference to the low-level interfaces. Why? Because then the code becomes transparent to the cipher used and much more flexible. ๐Ÿ’… Plus, the EVP interface will ensure the use of platform specific cryptographic acceleration such as AES-NI (the low-level interfaces do not provide that guarantee). so like, obviously use EVP, bestie.

PKCS padding works by adding n padding bytes of value n to make the total length of the encrypted data a multiple of the block size. Padding is always added, so if the data is already a multiple of the block size, n will equal the block size. For example if the block size is 8 and 11 bytes are to be encrypted then 5 padding bytes of value 5 will be added. math queen behavior ๐Ÿงฎโœจ

When decrypting the final block is checked to see if it has the correct form.

Although the decryption operation can produce an error if padding is enabled, it is not a strong test that the input data or key is correct. A random block has better than 1 in 256 chance of being of the correct format, and problems with the input data earlier on will not produce a final decrypt error. ๐Ÿ’€

If padding is disabled then the decryption operation will always succeed if the total amount of data decrypted is a multiple of the block size.

โš ๏ธ heads up: the functions EVP_EncryptInit(), EVP_EncryptInit_ex(), EVP_EncryptFinal(), EVP_DecryptInit(), EVP_DecryptInit_ex(), EVP_CipherInit(), EVP_CipherInit_ex() and EVP_CipherFinal() are obsolete but are retained for compatibility with existing code. New code should use EVP_EncryptInit_ex2(), EVP_EncryptFinal_ex(), EVP_DecryptInit_ex2(), EVP_DecryptFinal_ex(), EVP_CipherInit_ex2() and EVP_CipherFinal_ex() because they can reuse an existing context without allocating and freeing it up on each call. stop using the old ones, i'm begging ๐Ÿ˜ญ

There are some differences between functions EVP_CipherInit() and EVP_CipherInit_ex(), significant in some circumstances. EVP_CipherInit() fills the passed context object with zeros. As a consequence, EVP_CipherInit() does not allow step-by-step initialization of the ctx when the key and iv are passed in separate calls. It also means that the flags set for the CTX are removed, and it is especially important for the EVP_CIPHER_CTX_FLAG_WRAP_ALLOW flag treated specially in EVP_CipherInit_ex(). so like, know the difference, it matters fr fr.

๐Ÿšจ Ignoring failure returns of the EVP_CIPHER_CTX initialization functions can lead to subsequent undefined behavior when calling the functions that update or finalize the context. The only valid calls on the EVP_CIPHER_CTX when initialization fails are calls that attempt another initialization of the context or release the context. literally do not ignore those return values bestie.

EVP_get_cipherbynid(), and EVP_get_cipherbyobj() are implemented as macros.

๐Ÿ’€ BUGS ๐Ÿ’€

Okay bestie, listen up, because this part is giving "handle with care" energy:

  • EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with default key lengths. If custom ciphers go over those values, the results are straight up unpredictable ๐Ÿ˜ญ. Why? Because it's become standard practice to define a generic key as a fixed unsigned char array containing EVP_MAX_KEY_LENGTH bytes. That's it, that's the whole reason.
  • The ASN1 code is incomplete (and sometimes inaccurate) ๐Ÿ™ˆ โ€” it's only been tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode. So like, don't come for it about edge cases, it simply has not been there.

โœจ EXAMPLES โœจ

Not me giving you four whole examples because I love you ๐Ÿซถ. Let's go.

๐ŸŒธ Encrypt a string using IDEA

 int do_crypt(char *outfile)
 {
     unsigned char outbuf[1024];
     int outlen, tmplen;
     /*
      * Bogus key and IV: we'd normally set these from
      * another source.
      */
     unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
     unsigned char iv[] = {1,2,3,4,5,6,7,8};
     char intext[] = "Some Crypto Text";
     EVP_CIPHER_CTX *ctx;
     FILE *out;

     ctx = EVP_CIPHER_CTX_new();
     if (!EVP_EncryptInit_ex2(ctx, EVP_idea_cbc(), key, iv, NULL)) {
         /* Error */
         EVP_CIPHER_CTX_free(ctx);
         return 0;
     }

     if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
         /* Error */
         EVP_CIPHER_CTX_free(ctx);
         return 0;
     }
     /*
      * Buffer passed to EVP_EncryptFinal() must be after data just
      * encrypted to avoid overwriting it.
      */
     if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
         /* Error */
         EVP_CIPHER_CTX_free(ctx);
         return 0;
     }
     outlen += tmplen;
     EVP_CIPHER_CTX_free(ctx);
     /*
      * Need binary mode for fopen because encrypted data is
      * binary data. Also cannot use strlen() on it because
      * it won't be NUL terminated and may contain embedded
      * NULs.
      */
     out = fopen(outfile, "wb");
     if (out == NULL) {
         /* Error */
         return 0;
     }
     fwrite(outbuf, 1, outlen, out);
     fclose(out);
     return 1;
 }

See that comment about outbuf + outlen? That's the whole vibe โ€” the buffer you hand to EVP_EncryptFinal() has to sit after the data you just encrypted, otherwise you're overwriting your own slay ๐Ÿ’…. And yes you need "wb" for fopen because encrypted data is binary, and you absolutely cannot strlen() it because it won't be NUL terminated and might contain embedded NULs. Not me warning you twice about the same thing. Iconic behavior.

The ciphertext from the above example can be decrypted using the openssl utility with this command line (shown on two lines for clarity, because we're being polite):

 openssl idea -d \
     -K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 <filename

๐Ÿฆ‹ General encryption and decryption function example using FILE I/O and AES128 with a 128-bit key

 int do_crypt(FILE *in, FILE *out, int do_encrypt)
 {
     /* Allow enough space in output buffer for additional block */
     unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
     int inlen, outlen;
     EVP_CIPHER_CTX *ctx;
     /*
      * Bogus key and IV: we'd normally set these from
      * another source.
      */
     unsigned char key[] = "0123456789abcdeF";
     unsigned char iv[] = "1234567887654321";

     /* Don't set key or IV right away; we want to check lengths */
     ctx = EVP_CIPHER_CTX_new();
     if (!EVP_CipherInit_ex2(ctx, EVP_aes_128_cbc(), NULL, NULL,
                             do_encrypt, NULL)) {
         /* Error */
         EVP_CIPHER_CTX_free(ctx);
         return 0;
     }
     OPENSSL_assert(EVP_CIPHER_CTX_get_key_length(ctx) == 16);
     OPENSSL_assert(EVP_CIPHER_CTX_get_iv_length(ctx) == 16);

     /* Now we can set key and IV */
     if (!EVP_CipherInit_ex2(ctx, NULL, key, iv, do_encrypt, NULL)) {
         /* Error */
         EVP_CIPHER_CTX_free(ctx);
         return 0;
     }

     for (;;) {
         inlen = fread(inbuf, 1, 1024, in);
         if (inlen <= 0)
             break;
         if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
             /* Error */
             EVP_CIPHER_CTX_free(ctx);
             return 0;
         }
         fwrite(outbuf, 1, outlen, out);
     }
     if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
         /* Error */
         EVP_CIPHER_CTX_free(ctx);
         return 0;
     }
     fwrite(outbuf, 1, outlen, out);

     EVP_CIPHER_CTX_free(ctx);
     return 1;
 }

Lowkey the little OPENSSL_assert moment is the main character here โ€” check your lengths before you commit, queen ๐Ÿ‘‘.

๐Ÿ’œ Encryption using AES-CBC with a 256-bit key with "CS1" ciphertext stealing

 int encrypt(const unsigned char *key, const unsigned char *iv,
             const unsigned char *msg, size_t msg_len, unsigned char *out)
 {
    /*
     * This assumes that key size is 32 bytes and the iv is 16 bytes.
     * For ciphertext stealing mode the length of the ciphertext "out" will be
     * the same size as the plaintext size "msg_len".
     * The "msg_len" can be any size >= 16.
     */
     int ret = 0, encrypt = 1, outlen, len;
     EVP_CIPHER_CTX *ctx = NULL;
     EVP_CIPHER *cipher = NULL;
     OSSL_PARAM params[2];

     ctx = EVP_CIPHER_CTX_new();
     cipher = EVP_CIPHER_fetch(NULL, "AES-256-CBC-CTS", NULL);
     if (ctx == NULL || cipher == NULL)
         goto err;

     /*
      * The default is "CS1" so this is not really needed,
      * but would be needed to set either "CS2" or "CS3".
      */
     params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
                                                  "CS1", 0);
     params[1] = OSSL_PARAM_construct_end();

     if (!EVP_CipherInit_ex2(ctx, cipher, key, iv, encrypt, params))
         goto err;

     /* NOTE: CTS mode does not support multiple calls to EVP_CipherUpdate() */
     if (!EVP_CipherUpdate(ctx, out, &outlen, msg, msg_len))
         goto err;
      if (!EVP_CipherFinal_ex(ctx, out + outlen, &len))
         goto err;
     ret = 1;
 err:
     EVP_CIPHER_free(cipher);
     EVP_CIPHER_CTX_free(ctx);
     return ret;
 }
๐Ÿšจ That NOTE is not decorative, bestie: CTS mode does not support multiple calls to EVP_CipherUpdate(). One shot. Periodt.

Also yes, the default is "CS1", so setting it in params[0] is not really needed โ€” it's just showing you where you'd put "CS2" or "CS3" instead. iykyk ๐ŸŒˆ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

Go read your other girls:

Supported ciphers are listed in:


๐Ÿ“œ HISTORY ๐Ÿ“œ

The lore, in order, no skips:

  • Support for OCB mode was added in OpenSSL 1.1.0.
  • EVP_CIPHER_CTX was made opaque in OpenSSL 1.1.0. As a result, EVP_CIPHER_CTX_reset() appeared and EVP_CIPHER_CTX_cleanup() disappeared. EVP_CIPHER_CTX_init() remains as an alias for EVP_CIPHER_CTX_reset().
  • The EVP_CIPHER_CTX_cipher() function was deprecated in OpenSSL 3.0; use EVP_CIPHER_CTX_get0_cipher() instead. (Not the old one, babe.)
  • These functions were added in 3.0: EVP_EncryptInit_ex2(), EVP_DecryptInit_ex2(), EVP_CipherInit_ex2(), EVP_CIPHER_fetch(), EVP_CIPHER_free(), EVP_CIPHER_up_ref(), EVP_CIPHER_CTX_get0_cipher(), EVP_CIPHER_CTX_get1_cipher(), EVP_CIPHER_get_params(), EVP_CIPHER_CTX_set_params(), EVP_CIPHER_CTX_get_params(), EVP_CIPHER_gettable_params(), EVP_CIPHER_settable_ctx_params(), EVP_CIPHER_gettable_ctx_params(), EVP_CIPHER_CTX_settable_params() and EVP_CIPHER_CTX_gettable_params().
  • These functions were renamed to include "get" or "get0" in their names in OpenSSL 3.0, respectively: EVP_CIPHER_nid(), EVP_CIPHER_name(), EVP_CIPHER_block_size(), EVP_CIPHER_key_length(), EVP_CIPHER_iv_length(), EVP_CIPHER_flags(), EVP_CIPHER_mode(), EVP_CIPHER_type(), EVP_CIPHER_CTX_nid(), EVP_CIPHER_CTX_block_size(), EVP_CIPHER_CTX_key_length(), EVP_CIPHER_CTX_iv_length(), EVP_CIPHER_CTX_tag_length(), EVP_CIPHER_CTX_num(), EVP_CIPHER_CTX_type(), and EVP_CIPHER_CTX_mode(). The old names are kept as non-deprecated alias macros, so don't panic ๐Ÿซถ.
  • EVP_CIPHER_CTX_encrypting() was renamed to EVP_CIPHER_CTX_is_encrypting() in OpenSSL 3.0. The old name is kept as a non-deprecated alias macro.
  • The EVP_CIPHER_CTX_flags() macro was deprecated in OpenSSL 1.1.0.
  • EVP_CIPHER_CTX_dup() was added in OpenSSL 3.2.
  • EVP_CipherInit_SKEY() was added in OpenSSL 3.5.
  • Before OpenSSL 3.5, passing a NULL ctx to EVP_CIPHER_CTX_get_block_size() would give you a NULL pointer dereference ๐Ÿ˜ญ, instead of a 0 return value indicating an error. Glow-up achieved, honestly.

Copyright 2000-2026 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 a wrap on the whole page, bestie ๐Ÿ’– You made it to the end of EVP_ENCRYPTINIT(3SSL) โ€” this was 4.0.3, dated 2026-09-29, and honestly? You ate. Now go encrypt something responsibly, and remember: free your ctx, always ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from