EVP_ENCRYPTINIT(3ossl)

πŸ“¦ version 15.1, in freebsd 15.1

🌸 NAME 🌸

okay bestie, welcome to EVP_ENCRYPTINIT(3ossl) β€” the OpenSSL manual page that is literally the EVP cipher routines master list πŸ’…. It is giving β€œhuge friend group, and every single one is invited,” so here is the roll call exactly as the page lists it ✨:

  • EVP_CIPHER_fetch, EVP_CIPHER_up_ref, EVP_CIPHER_free,
  • EVP_CIPHER_CTX_new, EVP_CIPHER_CTX_reset, EVP_CIPHER_CTX_free,
  • EVP_CIPHER_CTX_dup, EVP_CIPHER_CTX_copy, EVP_EncryptInit_ex,
  • EVP_EncryptInit_ex2, EVP_EncryptUpdate, EVP_EncryptFinal_ex,
  • EVP_DecryptInit_ex, EVP_DecryptInit_ex2, EVP_DecryptUpdate,
  • EVP_DecryptFinal_ex, EVP_CipherInit_ex, EVP_CipherInit_ex2,
  • EVP_CipherInit_SKEY, EVP_CipherUpdate, EVP_CipherFinal_ex,
  • EVP_CIPHER_CTX_set_key_length, EVP_CIPHER_CTX_ctrl, EVP_EncryptInit,
  • EVP_EncryptFinal, EVP_DecryptInit, EVP_DecryptFinal, EVP_CipherInit,
  • EVP_CipherFinal, EVP_Cipher, EVP_CIPHER_can_pipeline,
  • EVP_CipherPipelineEncryptInit, EVP_CipherPipelineDecryptInit,
  • EVP_CipherPipelineUpdate, EVP_CipherPipelineFinal, EVP_get_cipherbyname,
  • EVP_get_cipherbynid, EVP_get_cipherbyobj, 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,
  • 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, 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, EVP_CIPHER_CTX_get_block_size,
  • EVP_CIPHER_CTX_get_key_length, EVP_CIPHER_CTX_get_iv_length,
  • EVP_CIPHER_CTX_get_tag_length, EVP_CIPHER_CTX_flags,
  • EVP_CIPHER_CTX_set_flags, EVP_CIPHER_CTX_clear_flags,
  • EVP_CIPHER_CTX_test_flags, 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, EVP_CIPHER_param_to_asn1,
  • EVP_CIPHER_asn1_to_param, EVP_CIPHER_CTX_set_padding, EVP_enc_null,
  • EVP_CIPHER_do_all_provided, 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_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 β€” and that whole iconic roster is summed up as EVP cipher routines 🫢.

✨ SYNOPSIS ✨

okay bestie, here's the whole cast of characters. no cap, it's a long list β€” but every single one of them is important, so no skimming πŸ’…

#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

okay so there are like two tiny old-school functions left over that you might not even see depending on your build πŸ‘€

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, the moment you've all been waiting for πŸŽ€ The EVP cipher routines are a high-level interface to certain symmetric ciphers. high-level, as in: you don't gotta micro-manage the math, the interface handles the vibes for you ✨

The EVP_CIPHER type is a structure for cipher method implementation. that's it, that's the whole glow-up β€” it's the struct holding the cipher method's implementation. iconic.

πŸ¦‹ EVP_CIPHER_fetch() πŸ¦‹

Fetches the cipher implementation for the given algorithm from any provider offering it, within the criteria given by the properties. See "ALGORITHM FETCHING" in crypto(7) for further information β€” that's where the deets live, iykyk.

The returned value must eventually be freed with EVP_CIPHER_free(). no leaks bestie, we don't do that here πŸ’…

Fetched EVP_CIPHER structures are reference counted. yes queen, refcounted.

🌸 EVP_CIPHER_up_ref() 🌸

Increments the reference count for an EVP_CIPHER structure. one more bestie holding the door open for it 🫢

πŸŽ€ 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. If the argument is NULL, nothing is done. literally nothing, zero drama, the function just minds its own business πŸ™ˆ

πŸ’œ EVP_CIPHER_CTX_new() πŸ’œ

Allocates and returns a cipher context. fresh out of the metaphorical box, main character energy 🌟

🌈 EVP_CIPHER_CTX_free() 🌈

Clears all information from a cipher context and frees any allocated memory associated with it, including ctx itself. This function should be called after all operations using a cipher are complete so sensitive information does not remain in memory. If the argument is NULL, nothing is done. πŸ”₯ yes, wipe the sensitive stuff β€” we are not leaving key material lying around, that's giving chaos.

🧚 EVP_CIPHER_CTX_dup() 🧚

Can be used to duplicate the cipher state from in. This is useful to avoid multiple EVP_CIPHER_fetch() calls or if large amounts of data are to be fed which only differ in the last few bytes. clone the vibe, skip the re-fetching ✨

⭐ EVP_CIPHER_CTX_copy() ⭐

Can be used to copy the cipher state from in to out. simple as that, no cap.

πŸ“ EVP_CIPHER_CTX_ctrl() πŸ“

This is a legacy method. 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. like, respectfully, use the new ones β€” this one's giving "old phone in a drawer" πŸ“΅

EVP_CIPHER_CTX_ctrl()</p>

Does the cipher-specific control thing on the context ctx β€” think of it as hitting the special buttons on a specific cipher. The control command goes in cmd, and any extra args ride along in p1 and p2. Bestie, listen: 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 like… vibe check your cipher first ✨

If this function happens to get used with a fetched EVP_CIPHER, it'll 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 appropriate for each control command. Glow-up via translation, love that for us.

See "CONTROLS" below for more info, including what translations are actually being done.

EVP_CIPHER_get_params()</p>

Pulls the requested list of algorithm params from a CIPHER cipher. Check "PARAMETERS" below for the deets.

EVP_CIPHER_CTX_get_params()</p>

Pulls the requested list of params from the CIPHER context ctx. Again, "PARAMETERS" below has the lore.

EVP_CIPHER_CTX_set_params()</p>

Sets the list of params into a CIPHER context ctx. "PARAMETERS", below, always below πŸ’…

EVP_CIPHER_gettable_params()</p>

Grabs you a constant OSSL_PARAM(3) array that describes the retrievable parameters you can use with EVP_CIPHER_get_params().

EVP_CIPHER_gettable_ctx_params()</p> and EVP_CIPHER_CTX_gettable_params()</p>

Both hand you a constant OSSL_PARAM(3) array describing the retrievable parameters usable with EVP_CIPHER_CTX_get_params(). The difference, bestie: EVP_CIPHER_gettable_ctx_params() returns the parameters retrievable from the algorithm, whereas EVP_CIPHER_CTX_gettable_params() returns the parameters retrievable in the context's current state. Same energy, different scope πŸ¦‹

EVP_CIPHER_settable_ctx_params()</p> and EVP_CIPHER_CTX_settable_params()</p>

Same deal but for setting: both give you a constant OSSL_PARAM(3) array describing the settable parameters usable with EVP_CIPHER_CTX_set_params(). EVP_CIPHER_settable_ctx_params() returns the parameters that can be set from the algorithm, while EVP_CIPHER_CTX_settable_params() returns the parameters that can be set in the context's current state. Iconic parallel structure, no notes πŸ‘‘

EVP_EncryptInit_ex2()</p>

Sets up cipher context ctx for encryption with cipher type. ctx MUST NOT be NULL β€” like, do not do that, fr fr 😭 type is typically supplied by calling EVP_CIPHER_fetch(). type may also be set using legacy functions such as EVP_aes_256_cbc(), but 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 IV depends on the cipher. The parameters params will be set on the context after initialisation. It's 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 β€” you do this when the default cipher parameters aren't appropriate. Oh and for EVP_CIPH_GCM_MODE, the IV will be generated internally if it's not specified. Surprise IV! 🎁

EVP_EncryptInit_ex()</p>

This legacy function is similar to EVP_EncryptInit_ex2() when impl is NULL. The implementation of the type from the impl engine will be used if it exists.

EVP_EncryptUpdate()</p>

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 β€” and this is the important part β€” 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. Also ctx MUST NOT be NULL.

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. Undefined behaviour is not the vibe πŸ’€

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. 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 + cipher_block_size) bytes. For stream ciphers, the amount of data written can be anything from zero bytes to inl bytes. So 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. Operation over, go home 🧚

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. Warning received, loud and clear πŸ”₯

EVP_DecryptInit_ex2()</p>, EVP_DecryptInit_ex()</p>, EVP_DecryptUpdate()</p> and EVP_DecryptFinal_ex()</p>

These 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. ctx MUST NOT be NULL. Symmetry slay πŸ’…

EVP_CipherInit_ex2()</p>, EVP_CipherInit_ex()</p>, EVP_CipherUpdate()</p> and EVP_CipherFinal_ex()</p>

These can be used for decryption or encryption β€” main character energy, doing both 😌 The operation performed depends on the value of the enc parameter. It should be set to 1 for encryption, 0 for decryption, and -1 to leave the value unchanged (the actual value of enc being supplied in a previous call).

EVP_CipherInit_SKEY()</p>

Similar to EVP_CipherInit_ex2() but it accepts a symmetric key object of type EVP_SKEY as a key. Fancy key object instead of raw key bytes, we love a glow-up πŸ”‘

EVP_CIPHER_CTX_reset()</p>

Clears all information from a cipher context and frees up any allocated memory associated with it β€” except the ctx itself, which stays. This function should be called anytime ctx is reused by another EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal() series of calls. Clean slate, fresh start, periodt ✨

EVP_EncryptInit()</p>, EVP_DecryptInit()</p> and EVP_CipherInit()</p>

Behave similarly 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()</p>, EVP_DecryptFinal()</p> and EVP_CipherFinal()</p>

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 β€” EVP_CIPHER_CTX_cleanup() must be called to free any context resources. Heads up, bestie, the old auto-cleanup is gone πŸ™ˆ

EVP_Cipher()</p>

Encrypts or decrypts a maximum of 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 does have 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. Like genuinely, take the scenic route 🚧

EVP_CIPHER_can_pipeline()</p>

Checks whether an EVP_CIPHER fetched using EVP_CIPHER_fetch() supports cipher pipelining. If the cipher supports pipelining it returns 1, otherwise 0. It will return 0 for non-fetched ciphers such as EVP_aes_128_gcm(). And rn? There are currently no built-in ciphers that support pipelining. Empty roster, tragic πŸ’€

Cipher pipelining support lets an application submit multiple chunks of data in one set of EVP_CipherUpdate()/EVP_CipherFinal calls, which lets the provided implementation take advantage of parallel computing. This is beneficial for hardware accelerators, since a pipeline amortizes the latency over multiple chunks. Efficiency era πŸ”₯

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

EVP_CipherPipelineEncryptInit()</p>, EVP_CipherPipelineDecryptInit()</p>, EVP_CipherPipelineUpdate()</p> and EVP_CipherPipelineFinal()</p>

These let you perform multiple encryption or decryption operations in parallel. Call EVP_CIPHER_can_pipeline() to check if the cipher supports pipelining first. These functions are analogous to EVP_EncryptInit_ex2(), EVP_DecryptInit_ex2(), EVP_CipherUpdate() and EVP_CipherFinal() β€” but they 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. 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, and the size of each IV buffer must be equal to ivlen. When an IV is not provided, iv must be NULL, rather than an array of NULL pointers (the details matter, bestie). The in parameter takes an array of buffer pointers, each pointing to a buffer containing the input data β€” and 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(). πŸ‘‘

EVP_CipherPipelineUpdate() can be called multiple times to encrypt successive blocks of data. For AAD data, the out and outsize parameters should be NULL, rather than an array of NULL pointers. Again: the details matter 🌸

  • EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj() β€” bestie, these hand you back an EVP_CIPHER structure when you pass a cipher name, a cipher NID or an ASN1_OBJECT structure, respectively. πŸ’… EVP_get_cipherbyname() will return NULL for algorithms such as "AES-128-SIV", "AES-128-CBC-CTS" and "CAMELLIA-128-CBC-CTS" which were previously only accessible via low level interfaces. The EVP_get_cipherbyname() function is present for backwards compatibility with OpenSSL prior to version 3 and is different to the EVP_CIPHER_fetch() function since it does not attempt to "fetch" an implementation of the cipher. Additionally, it only knows about ciphers that are built-in to OpenSSL and have an associated NID. Similarly EVP_get_cipherbynid() and EVP_get_cipherbyobj() also return objects without an associated implementation. When the cipher objects returned by these functions are used (such as in a call to EVP_EncryptInit_ex()) an implementation of the cipher will be implicitly fetched from the loaded providers. This fetch could fail if no suitable implementation is available. Use EVP_CIPHER_fetch() instead to explicitly fetch the algorithm and an associated implementation from a provider. See "ALGORITHM FETCHING" in crypto(7) for more information about fetching. The cipher objects returned from these functions do not need to be freed with EVP_CIPHER_free(). 🫢
  • EVP_CIPHER_get_nid() and EVP_CIPHER_CTX_get_nid() β€” returns the NID of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX structure. The actual NID value is an internal value which may not have a corresponding OBJECT IDENTIFIER. NID_undef is returned in the event that the nid is unknown or if the cipher has not 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. See "FLAGS" below for more information. For provided ciphers EVP_CIPHER_CTX_set_flags() should be called only after the fetched cipher has been assigned to the ctx. It is recommended to use "PARAMETERS" instead. ✨
  • EVP_CIPHER_CTX_set_padding() β€” enables or disables padding. This function should be called 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, the total amount of data encrypted or decrypted must then be a multiple of the block size or an error will occur. 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 EVP_CIPHER_CTX structure. The constant EVP_MAX_KEY_LENGTH is the maximum key length for all ciphers. Note: although 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. πŸ”‘
  • 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. 😀
  • 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, if 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 passed a EVP_CIPHER_CTX. It will return zero if the cipher does not support a tag. 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 is 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 (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 (it's the return value from the likes of EVP_aes128() rather than the result of an EVP_CIPHER_fetch()), 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(). 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. πŸŽ€
  • 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 or 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. πŸ”’
  • 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. πŸ’€
  • 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. This call should be made before the cipher is actually "used" (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. For example EVP_CipherInit() will be called with the IV and key set to NULL, EVP_CIPHER_asn1_to_param() will be called and 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. 🌟

πŸ’« PARAMETERS πŸ’«

See OSSL_PARAM(3) for information about passing parameters. 🫢

🧚 Gettable EVP_CIPHER parameters 🧚

When EVP_CIPHER_fetch() is called it internally calls EVP_CIPHER_get_params() and caches the results. πŸ’…

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> β€” Gets 1 if this is an AEAD cipher algorithm, otherwise it gets 0. Use (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) to retrieve the cached value. cached and cute, bestie ✨
  • "custom-iv" (OSSL_CIPHER_PARAM_CUSTOM_IV) <integer> β€” Gets 1 if the cipher algorithm cipher has a custom IV, otherwise it gets 0. Storing and initializing the IV is left entirely to the implementation, if a custom IV is used. Use (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_CUSTOM_IV) to retrieve the cached value. implementation said β€œI got this” πŸ’…
  • "cts" (OSSL_CIPHER_PARAM_CTS) <integer> β€” Gets 1 if the cipher algorithm cipher uses ciphertext stealing, otherwise it gets 0. This is currently used to indicate that the cipher is a one shot that only allows a single call to EVP_CipherUpdate(). Use (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CTS) to retrieve the cached value. one shot only, no cap 🎯
  • "tls-multi" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK) <integer> β€” Gets 1 if the cipher algorithm cipher supports interleaving of crypto blocks, otherwise it gets 0. The interleaving is an optimization only applicable to certain TLS ciphers. Use (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) to retrieve the cached value. optimization era 🌈
  • "has-randkey" (OSSL_CIPHER_PARAM_HAS_RANDKEY) <integer> β€” Gets 1 if the cipher algorithm cipher supports the gettable EVP_CIPHER_CTX parameter OSSL_CIPHER_PARAM_RANDOM_KEY. Only DES and 3DES set this to 1, all other OpenSSL ciphers return 0. DES and 3DES are the only members of this club πŸ‘‘
  • "decrypt-only" (OSSL_CIPHER_PARAM_DECRYPT_ONLY) <integer β€” Gets 1 if the cipher algorithm cipher implementation supports only the decryption operation such as the 3DES ciphers in the fips provider. Otherwise gets 0 or the parameter might not be present at all. sometimes it’s just not there, and that’s valid 🫢

✨ Gettable and Settable EVP_CIPHER_CTX parameters ✨

The following OSSL_PARAM(3) keys can be used with both EVP_CIPHER_CTX_get_params() and EVP_CIPHER_CTX_set_params(). double-duty icons, literally πŸ’–

  • "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 the value is 0. See also EVP_CIPHER_CTX_set_padding(). padding on or padding off, choose your fighter ✨
  • "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 been "used" already. See also EVP_CIPHER_CTX_get_num() and EVP_CIPHER_CTX_set_num(). block usage tracker behavior πŸ”₯
  • "keylen" (OSSL_CIPHER_PARAM_KEYLEN) <unsigned integer> β€” Gets or sets the key length for the cipher context ctx. 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(). respect the size_t limit, bestie πŸ’…
  • "tag" (OSSL_CIPHER_PARAM_AEAD_TAG) <octet string> β€” Gets or sets the AEAD tag for the associated cipher context ctx. See "AEAD INTERFACE" in EVP_EncryptInit(3). tag, you’re it πŸŽ€
  • "pipeline-tag" (OSSL_CIPHER_PARAM_PIPELINE_AEAD_TAG) <octet ptr> β€” Gets or sets the AEAD tag when using cipher pipelining. The pointer must point to an array of buffers, where the aead tag will be read from or written to. The array size must be equal to numpipes used in EVP_CipherPipelineEncryptInit() or EVP_CipherPipelineDecryptInit(). array size must match numpipes, no vibes-based guessing 😭
  • "keybits" (OSSL_CIPHER_PARAM_RC2_KEYBITS) <unsigned integer> β€” Gets or sets the effective keybits used for a RC2 cipher. The length of the "keybits" parameter should not exceed that of a size_t. RC2 said β€œbits, not bytes” ✨
  • "rounds" (OSSL_CIPHER_PARAM_ROUNDS) <unsigned integer> β€” Gets or sets the number of rounds to be used for a cipher. This is used by the RC5 cipher. round and round and round πŸŒ€
  • "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. DER encoded and doing the most πŸ’–
  • "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. pass the params, periodt 🫢
  • "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). old alias, still showing up to the function 😭
  • "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. The input length must be greater than or equal to the block size. (The block size for AES and CAMELLIA is 16 bytes).

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. NIST variant behavior 🌸
  • "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. sometimes CS2 says β€œsame as CS3” πŸ’…
  • "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. Kerberos5 variant said swap era πŸ”

The default is "CS1". 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". only those six, iykyk πŸŽ€

  • "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). four or eight, no in-between 🌈

πŸ’– Gettable EVP_CIPHER_CTX parameters πŸ’–

The following OSSL_PARAM(3) keys can be used with EVP_CIPHER_CTX_get_params(). get it, bestie ✨

  • "ivlen" (OSSL_CIPHER_PARAM_IVLEN and <OSSL_CIPHER_PARAM_AEAD_IVLEN) <unsigned integer> β€” Gets the IV length for the cipher context ctx. The length of the "ivlen" parameter should not exceed that of a size_t. See also EVP_CIPHER_CTX_get_iv_length(). IV length check, no cap πŸ’…
  • "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(). original IV behavior πŸŽ€
  • "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. Note that octet pointer access is deprecated and is provided only for backwards compatibility with historical libcrypto APIs. See also EVP_CIPHER_CTX_get_updated_iv(). deprecated pointer access, bestie β€” it’s giving legacy 😭
  • "randkey" (OSSL_CIPHER_PARAM_RANDOM_KEY) <octet string> β€” Gets an implementation specific randomly generated key for the associated cipher context ctx. This is currently only supported by DES and 3DES (which set the key to odd parity). random key with odd parity energy ✨
  • "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 has not been set. The length of the "taglen" parameter should not exceed that of a size_t. See also EVP_CIPHER_CTX_get_tag_length(). default tag length said β€œif not set, I got you” 🫢
  • "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. The length of the "tlsaadpad" parameter should not exceed that of a size_t. pad length, TLS record edition πŸ’–
  • "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. This is only used for GCM mode. only after "tlsivfixed", and only GCM πŸ‘‘
  • "tls1multi_enclen" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN) <unsigned integer> β€” Get the total length of the record returned from the "tls1multi_enc" operation. total record length behavior 🌈
  • "tls1multi_maxbufsz" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE) <unsigned integer> β€” Gets the maximum record length for a TLS1 multiblock cipher operation. The length of the "tls1multi_maxbufsz" parameter should not exceed that of a size_t. max buffer size, but make it size_t-safe ✨
  • "tls1multi_aadpacklen" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN) <unsigned integer> β€” Gets the result of running the "tls1multi_aad" operation. result of "tls1multi_aad" β€” cute πŸŽ€
  • "tls-mac" (OSSL_CIPHER_PARAM_TLS_MAC) <octet ptr> β€” Used to pass the TLS MAC data. MAC data pass-through icon πŸ’…
  • "fips-indicator" (OSSL_CIPHER_PARAM_FIPS_APPROVED_INDICATOR) <integer> β€” This option is used by the OpenSSL FIPS provider.

A getter that returns 1 if the operation is FIPS approved, or 0 otherwise. This may be used after calling a cipher final operation such as EVP_EncryptFinal_ex(). It may return 0 if the "encrypt-check" option is set to 0. FIPS approved indicator, literally πŸ‘‘

  • "iv-generated" (OSSL_CIPHER_PARAM_AEAD_IV_GENERATED) <unsigned integer> β€” An indicator that returns 1 if an IV was generated internally during encryption, or O otherwise. This may be used by GCM ciphers after calling a cipher final operation such as EVP_EncryptFinal_ex(). GCM should generate an IV internally if the IV is not specified during a cipher initialisation call such as EVP_CipherInit_ex(). See FIPS 140-3 IG C.H for information related to IV requirements. IV generated indicator said β€œinternally, bestie” 🫢

🌸 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? no, set it and slay ✨

  • "mackey" (OSSL_CIPHER_PARAM_AEAD_MAC_KEY) <octet string> β€” Sets the MAC key used by composite AEAD ciphers such as AES-CBC-HMAC-SHA256. composite AEAD said β€œneed my MAC key” πŸ’…
  • "speed" (OSSL_CIPHER_PARAM_SPEED) <unsigned integer> β€” Sets the speed option for the associated cipher context. This is only supported by AES SIV ciphers which disallow multiple operations by default. Setting "speed" to 1 allows another encrypt or decrypt operation to be performed. This is used for performance testing. performance testing behavior, periodt πŸ”₯
  • "use-bits" (OSSL_CIPHER_PARAM_USE_BITS) <unsigned integer> β€” Determines if the input length inl passed to EVP_EncryptUpdate(), EVP_DecryptUpdate() and EVP_CipherUpdate() is the number of bits or number of bytes. Setting "use-bits" to 1 uses bits. The default is in bytes. This is only used for CFB1 ciphers.

This can be set using EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS). bits or bytes? CFB1 said β€œyes” 😭

  • "tls-version" (OSSL_CIPHER_PARAM_TLS_VERSION) <integer> β€” Sets the TLS version. TLS version, set and done πŸ’œ
  • "tls-mac-size" (OSSL_CIPHER_PARAM_TLS_MAC_SIZE) <unsigned integer> β€” Sets the TLS MAC size. That's it, that's the whole vibe ✨
  • "tlsaad" (OSSL_CIPHER_PARAM_AEAD_TLS1_AAD) <octet string> β€” Sets the TLSv1.2 AAD info for the cipher context ctx it's attached to. The TLSv1.2 AAD info is always 13 bytes long, no notes, and it's exactly what the "additional_data" field in section 6.2.3.3 of RFC5246 describes. iykyk πŸŽ€
  • "tlsivfixed" (OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED) <octet string> β€” Sets the fixed portion of an IV for an AEAD cipher that's doing TLS record encryption/decryption for the cipher context it belongs to. Big facts coming, bestie, stay with me:

TLS record encryption/decryption is always done "in place", so the input and output buffers are literally the same memory location. AEAD IVs in TLSv1.2 are made of an implicit "fixed" part plus an explicit part that changes with every single record. Setting a TLS fixed IV is the thing that flips a cipher into TLS-record-encrypting/decrypting mode. TLS records get encrypted/decrypted with one 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 you're using and is defined in the RFC for that relevant ciphersuite. To make "in place" decryption work, the plaintext output should be written to the same spot in the output buffer that the ciphertext payload was read from β€” like, immediately after the explicit IV. 🫢

When encrypting a record, the first bytes of the input buffer should be left empty so there's room for the explicit IV, and same for the final bytes where the tag goes. The input buffer length includes the explicit IV length, the payload, and the tag bytes. The cipher implementation is supposed to generate the explicit IV and write it to the start of the output buffer, do "in place" encryption of the payload and write that into the output buffer, and finally slap the tag onto the end of the output buffer. Iconic behaviour tbh πŸ’…

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.

  • "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. Also this one is only used for GCM mode, no cap.
  • "tls1multi_enc" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC) <octet string> β€” Triggers a multiblock TLS1 encrypt operation for a TLS1-aware cipher that supports sending 4 or 8 records in one go πŸ”₯ The cipher does both the MAC and encrypt stages and constructs the record headers itself. "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 has to be set before you use "tls1multi_maxbufsz". Also the length of the "tls1multi_maxsndfrag" parameter should not exceed that of a size_t. Respect the limits bestie 🧚
  • "tls1multi_aad" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD) <octet string> β€” Sets the authenticated additional data used by a TLS1 multiblock cipher operation. The data you supply 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: the input length (always 0)

"tls1multi_interleave" must also be set for this operation to work. ✨

  • "xts_standard" (OSSL_CIPHER_PARAM_XTS_STANDARD) <UTF8 string> β€” Sets which XTS standard to use with the SM4-XTS algorithm. Okay so XTS mode has two implementations: one is standardised in IEEE Std. 1619-2007 and has been used everywhere forever (e.g., XTS AES), and the other one is more recent (GB/T 17964-2021, implemented in May 2022) and is currently only used in SM4. πŸ¦‹

The main difference between them is the multiplication by the primitive element Ξ± used to calculate the tweak values. IEEE Std 1619-2007 says the multiplication "is a left shift of each byte by one bit with carry propagating from one byte to the next one" β€” meaning that 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. Same energy, opposite direction, lowkey fascinating 😭

Valid values for the mode are:

  • "GB" β€” the GB/T 17964-2021 variant of the SM4-XTS algorithm.
  • "IEEE" β€” the IEEE Std. 1619-2007 variant of the 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 it's required, this parameter should be set early via a cipher encrypt init function such as EVP_EncryptInit_ex2(). Its default value of 1 causes an error when an encryption operation is triggered. Setting it to 0 will ignore that error and set the approved "fips-indicator" to 0.

⚠️ Fr fr warning: this option breaks FIPS compliance if it causes the approved "fips-indicator" to return 0. Like, actually breaks it. Don't come for me, I'm just the messenger πŸ’€

πŸŽ›οΈ CONTROLS πŸŽ›οΈ

The mappings from EVP_CIPHER_CTX_ctrl() identifiers to PARAMETERS are listed in this next bit. Peep the "PARAMETERS" section above for more details. πŸ‘€

EVP_CIPHER_CTX_ctrl() can be used to send these standard controls:

  • EVP_CTRL_AEAD_SET_IVLEN and EVP_CTRL_GET_IVLEN β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() get called with an OSSL_PARAM(3) item with the key "ivlen" (OSSL_CIPHER_PARAM_IVLEN).
  • EVP_CTRL_AEAD_SET_IV_FIXED β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item with the key "tlsivfixed" (OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED).
  • EVP_CTRL_AEAD_SET_MAC_KEY β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item with the key "mackey" (OSSL_CIPHER_PARAM_AEAD_MAC_KEY).
  • EVP_CTRL_AEAD_SET_TAG and EVP_CTRL_AEAD_GET_TAG β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() get called with an OSSL_PARAM(3) item with the key "tag" (OSSL_CIPHER_PARAM_AEAD_TAG).
  • EVP_CTRL_CCM_SET_L β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item with the key "ivlen" (OSSL_CIPHER_PARAM_IVLEN) with a value of (15 - L).
  • EVP_CTRL_COPY β€” There is no OSSL_PARAM mapping for this one. Use EVP_CIPHER_CTX_copy() instead. Periodt. πŸ’…
  • EVP_CTRL_GCM_SET_IV_INV β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item with the key "tlsivinv" (OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV).
  • EVP_CTRL_RAND_KEY β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item with the key "randkey" (OSSL_CIPHER_PARAM_RANDOM_KEY).
  • EVP_CTRL_SET_KEY_LENGTH β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item with the key "keylen" (OSSL_CIPHER_PARAM_KEYLEN).
  • EVP_CTRL_SET_RC2_KEY_BITS and EVP_CTRL_GET_RC2_KEY_BITS β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() get called with an OSSL_PARAM(3) item with the key "keybits" (OSSL_CIPHER_PARAM_RC2_KEYBITS).
  • EVP_CTRL_SET_RC5_ROUNDS and EVP_CTRL_GET_RC5_ROUNDS β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() get called with an OSSL_PARAM(3) item with the key "rounds" (OSSL_CIPHER_PARAM_ROUNDS).
  • EVP_CTRL_SET_SPEED β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item with the key "speed" (OSSL_CIPHER_PARAM_SPEED). Vroom vroom 🏎️
  • EVP_CTRL_GCM_IV_GEN β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_get_params() gets called with an OSSL_PARAM(3) item with the key "tlsivgen" (OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN).
  • EVP_CTRL_AEAD_TLS1_AAD β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item with the key "tlsaad" (OSSL_CIPHER_PARAM_AEAD_TLS1_AAD), followed by EVP_CIPHER_CTX_get_params() with a key of "tlsaadpad" (OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD). Two steps, one control, we love a combo ✨
  • EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with an OSSL_PARAM(3) item with the key OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, followed by EVP_CIPHER_CTX_get_params() with a key of "tls1multi_maxbufsz" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE).
  • EVP_CTRL_TLS1_1_MULTIBLOCK_AAD β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with OSSL_PARAM(3) items with the keys "tls1multi_aad" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD) and "tls1multi_interleave" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE), followed by EVP_CIPHER_CTX_get_params() 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 β€” When used with a fetched EVP_CIPHER, EVP_CIPHER_CTX_set_params() gets called with OSSL_PARAM(3) items with the keys "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), followed by EVP_CIPHER_CTX_get_params() with a key of "tls1multi_enclen" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN).

πŸ’– that's the whole controls roster, bestie β€” every single one accounted for πŸ’–

✨ FLAGS ✨

Okay so bestie, EVP_CIPHER_CTX_set_flags(), EVP_CIPHER_CTX_clear_flags() and EVP_CIPHER_CTX_test_flags() are your little toolkit for poking at these EVP_CIPHER_CTX flags β€” set 'em, clear 'em, test 'em, whatever πŸ’…

  • EVP_CIPH_NO_PADDING β€” this one's used by EVP_CIPHER_CTX_set_padding(). Also go peek at "Gettable and Settable EVP_CIPHER_CTX parameters" under "padding" πŸŽ€
  • EVP_CIPH_FLAG_LENGTH_BITS β€” check "Settable EVP_CIPHER_CTX parameters" under "use-bits".
  • EVP_CIPHER_CTX_FLAG_WRAP_ALLOW β€” legacy purposes only, fr. Back in the day you had to set this flag to say "yeah this cipher handles wrapping".

Then EVP_CIPHER_flags() rocks the following flags, and these babies have mappings over to "Gettable EVP_CIPHER parameters":

  • EVP_CIPH_FLAG_AEAD_CIPHER β€” see "Gettable EVP_CIPHER parameters" under "aead".
  • EVP_CIPH_CUSTOM_IV β€” see "Gettable EVP_CIPHER parameters" under "custom-iv".
  • EVP_CIPH_FLAG_CTS β€” see "Gettable EVP_CIPHER parameters" under "cts".
  • EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK; β€” see "Gettable EVP_CIPHER parameters" under "tls-multi".
  • EVP_CIPH_RAND_KEY β€” see "Gettable EVP_CIPHER parameters" under "has-randkey".

And EVP_CIPHER_flags() also uses these ones β€” legacy purposes only, bestie, no cap:

  • EVP_CIPH_VARIABLE_LENGTH
  • EVP_CIPH_FLAG_CUSTOM_CIPHER
  • EVP_CIPH_ALWAYS_CALL_INIT
  • EVP_CIPH_CTRL_INIT
  • EVP_CIPH_CUSTOM_KEY_LENGTH
  • EVP_CIPH_CUSTOM_COPY
  • EVP_CIPH_FLAG_DEFAULT_ASN1
For the whole tea on all of the above flags, see EVP_CIPHER_meth_set_flags(3) 🫢

πŸ’– RETURN VALUES πŸ’–

This part is giving "receipts" energy, so let's go down the list, one by one:

  • EVP_CIPHER_fetch() β€” returns a pointer to an EVP_CIPHER when it slays, and NULL when it flops.
  • EVP_CIPHER_up_ref() β€” 1 for success, 0 otherwise.
  • EVP_CIPHER_CTX_new() β€” returns a pointer to a freshly created EVP_CIPHER_CTX on success, NULL on failure.
  • EVP_CIPHER_CTX_dup() β€” returns a new EVP_CIPHER_CTX if it worked, or NULL on failure.
  • EVP_CIPHER_CTX_copy() β€” 1 if successful, 0 for failure.
  • EVP_EncryptInit_ex2(), EVP_EncryptUpdate() and EVP_EncryptFinal_ex() β€” 1 for success and 0 for failure.
  • EVP_DecryptInit_ex2() and EVP_DecryptUpdate() β€” 1 for success, 0 for failure. But like, watch out: EVP_DecryptFinal_ex() returns 0 if the decrypt failed or 1 for success πŸ‘€
  • EVP_CipherInit_ex2(), EVP_CipherInit_SKEY() and EVP_CipherUpdate() β€” 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. And if EVP_CIPH_FLAG_CUSTOM_CIPHER is set for the cipher, then EVP_Cipher() returns the number of bytes written to out for encryption/decryption, or the number of bytes authenticated in a call specifying AAD for an AEAD cipher. Iconic dual behaviour, honestly.
  • EVP_CIPHER_can_pipeline() β€” 1 if the cipher can be used in a pipeline, 0 otherwise.
  • EVP_CipherPipelineEncryptInit() and EVP_CipherPipelineDecryptInit() β€” 1 for success and 0 for failure.
  • EVP_CipherPipelineUpdate() and EVP_CipherPipelineFinal() β€” 1 for success and 0 for failure.
  • EVP_CIPHER_CTX_reset() β€” 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. Always. It's that girl.
  • 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() β€” 1 on success and 0 if the implementation doesn't support the call or on any other error.
  • EVP_CIPHER_CTX_is_encrypting() β€” 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() β€” 1 for success and zero or a negative number for failure.
  • EVP_CIPHER_names_do_all() β€” 1 if the callback was called for all names. A return value of 0 means the callback wasn't called for any names. Kinda harsh but okay πŸ’€
  • EVP_CIPHER_get_params(), EVP_CIPHER_CTX_get_params() and EVP_CIPHER_CTX_set_params() β€” 1 for success and 0 for failure.

🌸 CIPHER LISTING 🌸

Heads up bestie: all algorithms have a fixed key length unless otherwise stated. That's the default vibe.

And for the full list of ciphers you can get through the EVP interface, go look at "SEE ALSO" β€” it's all there, iykyk ✨

  • EVP_enc_null() β€” Null cipher: literally does nothing. Not a single thing. It's giving "main character who took a nap" πŸ˜΄πŸ’œ

✨ AEAD INTERFACE ✨

okay bestie, so the EVP interface for Authenticated Encryption with Associated Data (AEAD) modes is subtly altered, and depending on the mode you pick, there are several extra ctrl operations supported. slay, but pay attention πŸ’…

To 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. When that works, the parameter outl is set to the number of AAD bytes processed in that call (that is, the value of inl), and it does not include any plaintext or ciphertext bytes processed by other calls. periodt.

If no AAD is used, this call can be omitted. See the mode-specific notes below for any exceptions πŸ‘€

When decrypting, the return value of EVP_DecryptFinal() or EVP_CipherFinal() indicates whether 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. like, do not touch it, bestie 😭

Please note that the number of authenticated bytes returned by EVP_CipherUpdate() depends on the cipher used. Stream ciphers, such as ChaCha20 or ciphers in GCM mode, can handle 1 byte at a time, resulting in an effective β€œblock” size of 1. Conversely, ciphers in OCB mode must process data one block at a time, and the block size is returned. different vibes, same API 🌈

Regardless of the returned size, it is safe to pass unpadded data to an EVP_CipherUpdate() call in a single operation. no cap.


πŸ” 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. okay so this one has ✨rules✨

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. like, the whole length, bestie, don’t skip it πŸ’€

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. iconic, honestly πŸ‘‘

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. iykyk 🌸

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. main character energy, stored separately πŸ’…

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. cute and equal ✨

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, thus, 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 will indicate if the update operation succeeded. two passes, one update, no chaos 😌


πŸŽ€ 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. This is because the code then becomes transparent to the cipher used and much more flexible. Additionally, the EVP interface will ensure the use of platform specific cryptographic acceleration such as AES-NI (the low-level interfaces do not provide the guarantee). literally let EVP handle it, 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 but make it cute πŸŽ€

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. so like, don’t treat padding errors as a whole security vibe check πŸ’€

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.

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. upgrade your code, it’s giving efficiency 🌟

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(). okay so this one is a whole personality difference 😭

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. fr fr, check your return values 🫢

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

πŸ’€ BUGS πŸ’€

okay bestie, listen up, because there's some stuff that will absolutely bite you if you're not paying attention:

  • EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with default key lengths. That's it. That's the whole scope. If your custom ciphers go over those values, the results are unpredictable β€” like, genuinely no idea what happens, and neither does anyone else 😭. 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 just the vibe now, we don't make the rules.
  • The ASN1 code is incomplete (and sometimes inaccurate β€” πŸ™ˆ rude but true). It's only been tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode. Anything outside that little bubble? Untested territory, bestie. Enter at your own risk.

✨ EXAMPLES ✨

🧚 Encrypt a string using IDEA

okay so this first one is a whole little moment β€” encrypting a string with IDEA. Look at her go:

 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;
 }
🫢 Note the little comment block about the buffer passed to EVP_EncryptFinal() needing to go after the data you just encrypted β€” that's not decoration, that's literally to avoid overwriting it. Main character attention to detail, honestly.

And the ciphertext from that example? You can decrypt it with the openssl utility using this command line (shown on two lines for clarity β€” she's long, okay?):

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

πŸ¦‹ General encryption and decryption function example

Now for the general one β€” using FILE I/O and AES128 with a 128-bit key. Both encrypt and decrypt, because she contains multitudes:

 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;
 }
🌸 See how she doesn't set the key or IV right away? That's on purpose β€” she wants to check lengths first with OPENSSL_assert(EVP_CIPHER_CTX_get_key_length(ctx) == 16); and OPENSSL_assert(EVP_CIPHER_CTX_get_iv_length(ctx) == 16);. Only then does she commit. Queen behavior.

πŸ”₯ Encryption using AES-CBC with a 256-bit key with "CS1" ciphertext stealing

Last example icon β€” this one's doing ciphertext stealing, which is exactly as cool as it sounds:

 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;
 }
⚠️ real talk: CTS mode does not support multiple calls to EVP_CipherUpdate() β€” there's literally a /* NOTE: */ about it in the code. So don't do that. One call, bestie. Periodt.

πŸ‘€ SEE ALSO πŸ‘€

go read these next, iykyk:

Supported ciphers are listed in:


🌈 HISTORY 🌈

storytime with me, bestie β€” here's the lore πŸ’…:

  • 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. And EVP_CIPHER_CTX_init() remains as an alias for EVP_CIPHER_CTX_reset(). She's still here, just under a different name 🫢.
  • The EVP_CIPHER_CTX_cipher() function was deprecated in OpenSSL 3.0; use EVP_CIPHER_CTX_get0_cipher() instead. (respectfully, move on.)
  • 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()
    • EVP_CIPHER_CTX_gettable_params()
  • These functions were renamed to include "get" or "get0" in their names in OpenSSL 3.0, respectively. The old names are kept as non-deprecated alias macros (so nothing breaks, bless):
    • 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()
    • EVP_CIPHER_CTX_mode()
  • The EVP_CIPHER_CTX_encrypting() function was renamed to EVP_CIPHER_CTX_is_encrypting() in OpenSSL 3.0. The old name is kept as a non-deprecated alias macro. (she goes by both, it's fine.)
  • 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.
  • Prior to OpenSSL 3.5, passing a NULL ctx to EVP_CIPHER_CTX_get_block_size() would result in a NULL pointer dereference, rather than a 0 return value indicating an error. 😭 So yeah β€” that was a thing that happened. Glad it's fixed, fr fr.

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 the whole page, bestie! this edition is OpenSSL 3.5.6, dated 2026-04-07, and it's filed under EVP_ENCRYPTINIT(3ossl). go forth and encrypt responsibly, ily, bye! πŸ‘‘πŸ’–
πŸ“¦ where this page comes from