EVP_ENCRYPTINIT(3ossl)

๐Ÿ“ฆ version 3.5.0, in opensuse-leap 16.0

okay bestie, welcome to the EVP_ENCRYPTINIT(3ossl) manual page โ€” OpenSSL's whole EVP cipher routine universe. this part is just the iconic name-drop section, and yes, it's a lot of names. every single one is here, no cap.

โœจ NAME โœจ

these are literally the EVP cipher routines โ€” the full cast list, main-character energy only:

  • 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_get_app_data
  • EVP_CIPHER_CTX_set_app_data
  • 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 the shared description for all of them? EVP cipher routines. periodt. ๐Ÿ’…

okay that's the name section โ€” next parts are gonna get into the actual functions, so stay tuned, bestie ๐Ÿซถโœจ

โœจ SYNOPSIS โœจ

okay so first things first, bestie โ€” here's the whole lineup of things this header gives you. Yes it's long. No we're not skipping any of it. Every single prototype, exactly as written, because typos in signatures are a whole villain arc:

#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);

the actual encrypting and decrypting squad ๐Ÿ’…:

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);

the "actually I don't care which direction, I'll tell you with enc" versions ๐Ÿฆ‹:

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);

and the legacy no-_ex girlies (they exist, we don't gatekeep, but likeโ€ฆ read the return values section later okay?) ๐ŸŽ€:

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);

pipeline mode, the whole "do a bunch of these at once" era ๐Ÿ”ฅ:

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);

knobs, dials and little context tweaks โœจ:

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);

looking stuff up the old-school way ๐Ÿ™ˆ:

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);

all the "tell me about this cipher" introspection ๐Ÿ’…:

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);

the params / discovery era ๐Ÿงš:

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);
void *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx);
void EVP_CIPHER_CTX_set_app_data(const EVP_CIPHER_CTX *ctx, void *data);
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);

ASN.1 glue, because sometimes ciphers have to talk to certificates ๐Ÿ˜ญ:

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);

and the "give me literally every cipher your provider has, I'm nosy" one ๐Ÿ‘‘:

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

okay and here's the part where lowercase names are just aliases for the fancy new names โ€” they're macros, they're free, they're iconic, use whichever feels right:

#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

๐Ÿซถ deprecation corner, bestie. 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);
๐Ÿ’€ and this one's even older โ€” 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 so the EVP cipher routines are basically the high-level, no-thoughts-just-vibes interface to certain symmetric ciphers. like, you don't wanna be out here poking at raw cipher internals, and you literally don't have to. ๐ŸŽ€

the EVP_CIPHER type is a structure for cipher method implementation. that's it, that's the whole description of it, it's a struct that represents a cipher method's implementation. ๐Ÿฆ‹


๐Ÿ’– EVP_CIPHER_fetch()

fetches the cipher implementation for the given algorithm from any provider offering it, within the criteria given by the properties. it's giving "I don't care who provides it, just get me one that matches these vibes." for the full lore, see "ALGORITHM FETCHING" in crypto(7).

the returned value must eventually be freed with EVP_CIPHER_free(). don't just leave it lying around, that's not cute.

fetched EVP_CIPHER structures are reference counted. ๐Ÿ‘‘

๐Ÿ’– EVP_CIPHER_up_ref()

increments the reference count for an EVP_CIPHER structure. one more friend holding a hand. ๐Ÿซถ

๐Ÿ’– 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 โ€” no crash, no drama, it just moves on with its day. โœจ

๐Ÿ’– EVP_CIPHER_CTX_new()

allocates and returns a cipher context. a fresh little baby ctx, brand new, ready for anything. ๐Ÿ’…

๐Ÿ’– 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. like, literally, wipe your keys before you dip. ๐Ÿ” if the argument is NULL, nothing is done.

๐Ÿ’– 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. it's the "save point before the boss fight" of cryptography, no cap. ๐ŸŽฎ

๐Ÿ’– EVP_CIPHER_CTX_copy()

can be used to copy the cipher state from in to out. same energy as dup but you already have the destination, so you're being efficient about it. slay. ๐Ÿ’…

๐Ÿ’… EVP_CIPHER_CTX_ctrl()

okay so this one is a legacy method, bestie โ€” like, it still works, it's just not the vibe anymore. The mechanism that should be used to set and get parameters that are used by providers is EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params(). Periodt.

It performs cipher-specific control actions on the context ctx. The control command is indicated in cmd, and any extra 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 like, read the room, iykyk.

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. Iconic translator behaviour tbh ๐Ÿฆ‹

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. See "PARAMETERS" below for more. Slay.

๐ŸŽ€ EVP_CIPHER_CTX_get_params()

Retrieves the requested list of params from CIPHER context ctx. See "PARAMETERS" below for more. Yes bestie, read PARAMETERS.

๐ŸŽ€ EVP_CIPHER_CTX_set_params()

Sets the list of params into a CIPHER context ctx. See "PARAMETERS" below for more. (Yes, PARAMETERS again. She's the main character of this page, honestly.)

๐ŸŽ€ EVP_CIPHER_gettable_params()

Get a constant OSSL_PARAM(3) array that describes the retrievable parameters that can be used with EVP_CIPHER_get_params(). It's giving "here's the menu, pick your fave" โœจ

๐ŸŽ€ EVP_CIPHER_gettable_ctx_params() and EVP_CIPHER_CTX_gettable_params()

Get a constant OSSL_PARAM(3) array that describes the retrievable parameters that can be used with EVP_CIPHER_CTX_get_params().

The difference is subtle but so real:

  • 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.

See? Different vibes entirely ๐Ÿ’…

๐ŸŽ€ EVP_CIPHER_settable_ctx_params() and EVP_CIPHER_CTX_settable_params()

Get a constant OSSL_PARAM(3) array that describes the settable parameters that can be used with EVP_CIPHER_CTX_set_params().

Same energy as above, opposite direction:

  • 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()

Okay this is like... the actual setup girlie. Sets up cipher context ctx for encryption with cipher type.

  • ctx MUST NOT be NULL. Not playing about that one ๐Ÿ™ˆ
  • 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 โ€” think of it as the flip phone of cipher selection. Cute, nostalgic, don't build with it.
  • 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.
  • The parameters params will be set on the context after initialisation.

Now here's the fun part: it is possible to set all parameters to NULL except type in an initial call, and then 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. Very "I'll tell you the rest later, bestie" of you ๐Ÿ’–

For EVP_CIPH_GCM_MODE the IV will be generated internally if it is not specified. Not me just handing out IVs for free ๐ŸŽ

๐ŸŽ€ EVP_EncryptInit_ex()

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. (If it doesn't exist... well. That's a different story, and by "different story" I mean it won't be used. No cap.)


๐ŸŒธ 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. BUT โ€” and this is the part everyone skips, so listen up bestie โ€” 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. ๐Ÿ’€ The disrespect.

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. Like, actually undefined. Not "undefined" in the cute way. In the C way. ๐Ÿ˜ญ

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, the receipts:

  • 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.

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. Size your buffers correctly or the universe will simply take you out. ๐Ÿ”ฅ


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. She's done, she's leaving the group chat ๐Ÿ‘‘

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. No padding, no mercy, no leftover bytes. ๐Ÿšซ


๐Ÿฆ‹ EVP_DecryptInit_ex2(), EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex()

These functions are the corresponding decryption operations. Like, same thing but backwards, bestie.

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 yes โ€” ctx MUST NOT be NULL. (It's always that. ctx is never allowed to be NULL. It's the one rule of the house.) ๐Ÿ 

๐Ÿ’œ EVP_CipherInit_ex2(), EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex()

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

  • set it to 1 for encryption,
  • 0 for decryption,
  • -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 but with โœจobjectโœจ energy.

๐Ÿงš EVP_CIPHER_CTX_reset()

Clears all information from a cipher context and frees up any allocated memory associated with it โ€” except the ctx itself, because she lives to serve another day ๐Ÿ’…

This function should be called anytime ctx is reused by another EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal() series of calls. Clean slate, fresh start, new era.

๐ŸŽ€ EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit()

Behave in a similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex() and EVP_CipherInit_ex() โ€” except that if the type is not a fetched cipher, they use the default implementation of the type. Sneaky little default-haver.

๐ŸŽ€ 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 finding out the old versions did extra unpaid labour ๐Ÿ˜ญ if you're porting old code, this is your reminder: the cleanup does not happen for you anymore.

โญ EVP_Cipher()

Encrypts or decrypts a maximum inl amount of bytes from in and leaves the result in out. Casual. Effortless. One function.

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. Free spirit.
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. She said what she said. This is a "do not perceive her" situation ๐Ÿ™ˆ

๐Ÿ‘‘ 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. Slay ๐Ÿ”ฅ
  • Otherwise it returns 0. ๐Ÿ’€

This function will return 0 for non-fetched ciphers such as EVP_aes_128_gcm(). And heads up: there are currently no built-in ciphers that support pipelining. So like... the feature is real, the built-ins just haven't caught up yet. She's ahead of her time.

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. It's giving teamwork. It's giving group project where everyone actually pulls their weight for once ๐Ÿซถ

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. No silent disappointment โ€” she tells you.


๐ŸŒˆ EVP_CipherPipelineEncryptInit(), EVP_CipherPipelineDecryptInit(), EVP_CipherPipelineUpdate() and EVP_CipherPipelineFinal()

These functions can be used to perform multiple encryption or decryption operations in parallel. Call EVP_CIPHER_can_pipeline() to check if the cipher supports pipelining first โ€” vibe check before you commit, always.

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. Everything at once, no waiting your turn.

Now let's go through the parameters, because there are a lot of them and they all matter:

  • 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. (Respect the cap, bestie ๐Ÿ’…)
  • 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. Yes, there's a difference, and yes, it will get you. ๐Ÿซ 
  • 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 โ€” she's flexible like that.
  • 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(). (You know her. You met her earlier. She's not repeating herself.) โœจ

  • EVP_CipherPipelineUpdate() โ€” you can call this one multiple times to encrypt successive blocks of data, no cap. ๐Ÿ’ซ For AAD data the out and outsize parameter should be NULL, not an array of NULL pointers โ€” that's the vibe check here, okay?

EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj() Returns an EVP_CIPHER structure when you pass it 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" โ€” those were previously only accessible via low level interfaces, so like, they're not on the guest list anymore. ๐Ÿ™ˆ

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 โ€” it's giving "name only, no actual implementation", and honestly iconic of them. ๐Ÿ’…

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() โ€” no cleanup arc required, bestie. ๐Ÿซถ

EVP_CIPHER_get_nid() and EVP_CIPHER_CTX_get_nid() Return 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, and 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. like, literally do not.

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 โ€” so don't @ me when the numbers don't match. ๐Ÿ˜Œ

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 an 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. zero means "nope, not today". ๐Ÿ’€

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. iykyk ๐ŸŒธ

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 โ€” main character energy, only one gets the spotlight. ๐Ÿ‘‘ 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 โ€” so it's giving "the implementation writes its own bio". ๐ŸŽ€

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. pick your fighter ๐Ÿ’…

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 โ€” it's literally the "how much of this block have we eaten" 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 seriously, don't. ๐Ÿ’€

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 โ€” bespoke key gen, we love to see it. โœจ key must point to a buffer at least as big as the value returned by EVP_CIPHER_CTX_get_key_length(). no skimping on the buffer size bestie.

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 โ€” like screenshotting the stats so it doesn't have to re-check later. ๐Ÿ“ธ

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> โ€” grabs the block size for the cipher algorithm cipher. And like, plot twist: the block size is 1 for stream ciphers. Also heads up that the block size of a cipher can genuinely differ from the block size of the underlying encryption/decryption primitive โ€” iconic of AES in CTR mode to have a block size of 1 (because it behaves like a stream cipher) even though AES itself is a block size of 16. Want the cached value? Use EVP_CIPHER_get_block_size().
  • "aead" (OSSL_CIPHER_PARAM_AEAD) <integer> โ€” gets 1 if this is an AEAD cipher algorithm, otherwise 0. Use (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) to grab the cached value.
  • "custom-iv" (OSSL_CIPHER_PARAM_CUSTOM_IV) <integer> โ€” gets 1 if the cipher algorithm cipher has a custom IV, otherwise 0. When a custom IV is in play, storing and initializing the IV is left entirely to the implementation. Use (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_CUSTOM_IV) for the cached value.
  • "cts" (OSSL_CIPHER_PARAM_CTS) <integer> โ€” gets 1 if the cipher algorithm cipher uses ciphertext stealing, otherwise 0. Right now this is used to signal 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) for the cached value.
  • "tls-multi" (OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK) <integer> โ€” gets 1 if the cipher algorithm cipher supports interleaving of crypto blocks, otherwise 0. The interleaving is an optimization that only applies to certain TLS ciphers. Use (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) for the cached value.
  • "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 โ€” literally every other OpenSSL cipher returns 0.
  • "decrypt-only" (OSSL_CIPHER_PARAM_DECRYPT_ONLY) <integer โ€” gets 1 if the cipher algorithm cipher implementation supports only the decryption operation (like the 3DES ciphers in the fips provider). Otherwise it gets 0, or the parameter might not be present at all.

๐Ÿ’œ 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() โ€” dual threat behavior, we love to see it ๐Ÿซถ

  • "padding" (OSSL_CIPHER_PARAM_PADDING) <unsigned integer> โ€” gets or sets the padding mode for the cipher context ctx. Padding is enabled when the value is 1, disabled when 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. 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. See "AEAD Interface" in EVP_EncryptInit(3).
  • "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().
  • "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.
  • "rounds" (OSSL_CIPHER_PARAM_ROUNDS) <unsigned integer> โ€” gets or sets the number of rounds to be used for a cipher. The RC5 cipher is the one using this.
  • "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, bestie ๐Ÿ’€
  • "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.
  • "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". 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".

  • "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).

๐ŸŒˆ Gettable EVP_CIPHER_CTX parameters

These OSSL_PARAM(3) keys are the ones you can use with EVP_CIPHER_CTX_get_params() โ€” read-only era ๐Ÿ’…

  • "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" (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 bestie: octet pointer access is deprecated and is only there 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. Currently only DES and 3DES support this (and they set the key to odd parity).
  • "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. 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. 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.
  • "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.
  • "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.
  • "fips-indicator" (OSSL_CIPHER_PARAM_FIPS_APPROVED_INDICATOR) <integer> โ€” this one is used by the OpenSSL FIPS provider. โœจ

It's a getter that returns 1 if the operation is FIPS approved, or 0 otherwise. You'd use this after calling a cipher final operation such as EVP_EncryptFinal_ex(). It may return 0 if the "encrypt-check" option is set to 0.

  • "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. 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 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.

๐ŸŽ€ Settable EVP_CIPHER_CTX parameters

And these OSSL_PARAM(3) keys are the ones for EVP_CIPHER_CTX_set_params() โ€” the "you get to decide" corner ๐Ÿฆ‹

  • "mackey" (OSSL_CIPHER_PARAM_AEAD_MAC_KEY) <octet string> โ€” sets the MAC key used by composite AEAD ciphers such as AES-CBC-HMAC-SHA256.
  • "speed" (OSSL_CIPHER_PARAM_SPEED) <unsigned integer> โ€” sets the speed option for the associated cipher context. Only AES SIV ciphers support this, and they disallow multiple operations by default. Setting "speed" to 1 allows another encrypt or decrypt operation to be performed. This is used for performance testing.
  • "use-bits" (OSSL_CIPHER_PARAM_USE_BITS) โ€” unsigned integer. Okay so this one decides whether the input length inl that you pass to EVP_EncryptUpdate(), EVP_DecryptUpdate() and EVP_CipherUpdate() is counted in bits or in bytes. Set "use-bits" to 1 and it's bits, bestie. The default? Bytes, obviously. ๐Ÿ’… And heads up: this is only used for CFB1 ciphers. You can set it 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 the TLSv1.2 AAD info for the associated cipher context ctx. And fr fr, TLSv1.2 AAD info is always 13 bytes long and is exactly as defined for the "additional_data" field described in section 6.2.3.3 of RFC5246. No cap.
  • "tlsivfixed" (OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED) โ€” octet string. Sets the fixed portion of an IV for an AEAD cipher used in TLS record encryption/decryption for the associated cipher context. Lowkey a big one so let me break it all the way down:

TLS record encryption/decryption always happens "in place", meaning the input and output buffers are always 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 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 you're using and is defined in the RFC for the relevant ciphersuite. To allow for "in place" decryption, the plaintext output should be written to the same location in the output buffer that the ciphertext payload was read from โ€” i.e. immediately after the explicit IV. ๐Ÿซถ

When encrypting a record, the first bytes of the input buffer should be empty so there's space for the explicit IV, and same for the final bytes where the tag will get written. The length of the input buffer includes the length of the explicit IV, the payload, and the tag bytes. The cipher implementation should 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 add 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. Note that down, bestie. โœจ

  • "tlsivinv" (OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV) โ€” octet string. Sets the invocation field used for decryption. You can only call this after "tlsivfixed" is set โ€” order matters, no skipping steps. 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 sending 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 honestly. "tls1multi_enc" supplies the output buffer for the encrypt operation, and "tls1multi_encin" & "tls1multi_interleave" must also be set so the operation actually has values to work with.
  • "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", and the length of the "tls1multi_maxsndfrag" parameter should not exceed that of a size_t. Respect the limits, periodt.
  • "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
    • Bytes 9-10: the protocol version
    • Bytes 11-12: input length (always 0)

"tls1multi_interleave" must also be set for this operation, iykyk. ๐ŸŒธ

  • "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 is proposed more recently (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 ฮฑ when calculating 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", which means 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. ๐Ÿ’€

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 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. โš ๏ธ And this is important: this option breaks FIPS compliance if it causes the approved "fips-indicator" to return 0. Like, actually breaks it, not in a cute way.


๐ŸŽ›๏ธ CONTROLS ๐ŸŽ›๏ธ

The mappings from EVP_CIPHER_CTX_ctrl() identifiers to PARAMETERS are listed in the following section โ€” peep the "PARAMETERS" section for more details, bestie.

EVP_CIPHER_CTX_ctrl() can be used to send the following 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's no OSSL_PARAM mapping for this one. Use EVP_CIPHER_CTX_copy() instead. ๐Ÿ™ˆ
  • 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).
  • 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() get 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-step, stay with me. โœจ
  • 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). ๐Ÿ†

๐Ÿšฉ FLAGS

Okay bestie, so these three little icons โ€” EVP_CIPHER_CTX_set_flags(), EVP_CIPHER_CTX_clear_flags() and EVP_CIPHER_CTX_test_flags() โ€” are literally the vibe-check squad for flags. You use them to manipulate and test these EVP_CIPHER_CTX flags:

  • EVP_CIPH_NO_PADDING โ€” this one's used by EVP_CIPHER_CTX_set_padding(). Head over to the "Gettable and Settable EVP_CIPHER_CTX parameters" section, under padding, for the tea ๐Ÿซ–
  • EVP_CIPH_FLAG_LENGTH_BITS โ€” see the "Settable EVP_CIPHER_CTX parameters" section, under use-bits.
  • EVP_CIPHER_CTX_FLAG_WRAP_ALLOW โ€” legacy purposes only, no cap. Like, back in the day you had to set this flag to indicate that the cipher handled wrapping. That's it, that's the whole job.

Then there's EVP_CIPHER_flags(), which uses these following flags, and they've got mappings to "Gettable EVP_CIPHER parameters":

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

And EVP_CIPHER_flags() also uses these next ones for legacy purposes only (so, vintage era, respectfully ๐Ÿ‘‘):

  • 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 further information related to all the flags right above, please see EVP_CIPHER_meth_set_flags(3). She's got the lore โœจ

๐Ÿ” RETURN VALUES

Time for the receipts, bestie โ€” who returns what, and when ๐Ÿ’…

  • EVP_CIPHER_fetch() returns a pointer to a EVP_CIPHER for success and NULL for failure.
  • EVP_CIPHER_up_ref() returns 1 for success or 0 otherwise.
  • EVP_CIPHER_CTX_new() returns a pointer to a newly created EVP_CIPHER_CTX for success and NULL for failure.
  • EVP_CIPHER_CTX_dup() returns a new EVP_CIPHER_CTX if successful or NULL on failure.
  • EVP_CIPHER_CTX_copy() returns 1 if successful 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. 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 โ€” that's if the flag EVP_CIPH_FLAG_CUSTOM_CIPHER is not set for the cipher, or if the cipher has not been initialized via a call to EVP_CipherInit_ex2. But if the flag 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. Two completely different moods, iykyk ๐Ÿ˜ญ
  • 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. Unbothered. Consistent. ๐Ÿ‘‘
  • EVP_CIPHER_get_iv_length() and EVP_CIPHER_CTX_get_iv_length() return the IV length, zero if the cipher does not use an IV and a negative value on error.
  • EVP_CIPHER_CTX_get_tag_length() return the tag length or zero if the cipher does not 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 does not support the call or on any other error.
  • EVP_CIPHER_CTX_set_num() returns 1 on success and 0 if the implementation does not 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 that the callback was not called for any names.

๐Ÿ“œ CIPHER LISTING

Heads up, babe: all algorithms have a fixed key length unless otherwise stated. That's just the rules of the house ๐Ÿ โœจ

And for the full list of ciphers available through the EVP interface, go peek at "SEE ALSO" โ€” she's got the directory, bestie.

  • EVP_enc_null() โ€” the Null cipher: does literally nothing. Zero thoughts, head empty, no encryption ๐Ÿซง

โœจ AEAD INTERFACE โœจ

Okay so bestie, the EVP interface for Authenticated Encryption with Associated Data (AEAD) modes is subtly altered, and several additional ctrl operations are supported depending on the mode you specify. ๐Ÿ’…

To specify additional authenticated data (AAD), 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 is set to the number of bytes authenticated. โœจ

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. ๐Ÿ˜ญ

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. ๐Ÿฆ‹

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 it is not called, 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. ๐Ÿซถ

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. ๐ŸŽ€

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. ๐Ÿ’…

๐Ÿ’… 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). ๐Ÿ’–

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.

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.

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.

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().

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. ๐Ÿ’€

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

๐Ÿ’€ BUGS ๐Ÿ’€

EVP_MAX_KEY_LENGTH and EVP_MAX_IV_LENGTH only refer to the internal ciphers with default key lengths. If custom ciphers exceed these values the results are unpredictable. This is because it has become standard practice to define a generic key as a fixed unsigned char array containing EVP_MAX_KEY_LENGTH bytes. ๐Ÿ˜ญ

The ASN1 code is incomplete (and sometimes inaccurate) it has only been tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode.

๐ŸŽฌ EXAMPLES

Okay bestie, this is the part where it all comes together โ€” actual code, do not panic, I promise it slaps ๐Ÿ’–

Encrypt a string using IDEA

Lowkey a classic. Look at it:

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;
}

Notice the outbuf + outlen in EVP_EncryptFinal_ex? That's the whole "don't overwrite your own freshly-encrypted data" thing โ€” it's giving main character energy and it matters, bestie ๐Ÿซถ


The ciphertext from the above example can be decrypted using the openssl utility with the command line (shown on two lines for clarity):

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

Like, this one does both directions and it's so versatile for it:

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 the two-step init sitch? First call passes NULL for key and IV so you can peep the lengths, second call actually hands them over. That's the flex ๐Ÿ’…

Encryption using AES-CBC with a 256-bit key with "CS1" ciphertext stealing

Ooh, ciphertext stealing, very demure, very mindful, very OSSL_PARAM ๐ŸŽ€

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;
}
โš ๏ธ bestie, read this note or perish: CTS mode does not support multiple calls to EVP_CipherUpdate() โ€” one call, that's it, that's the whole vibe. And remember the fetched cipher needs EVP_CIPHER_free(), no skipping ๐Ÿ’€

๐Ÿ‘€ SEE ALSO

The whole friend group, fr fr:

Supported ciphers are listed in:


๐Ÿ•ฐ๏ธ HISTORY

Storytime, gather round, this is 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. 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.
  • The 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() functions were added in 3.0.
  • The 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() 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.
  • 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.
  • 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.
โš ๏ธ heads up: that last one is a genuine "please don't do this on old versions" situation โ€” it's a crash, not a graceful error. Fix your version or fix your NULL ๐Ÿ’€

Copyright 2000-2025 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, bestie!! ๐ŸŽ€โœจ this whole iconic page is from OpenSSL 3.5.0, dated 2026-09-25, and it lives under the name EVP_ENCRYPTINIT(3ossl) โ€” go forth and encrypt responsibly, ily, mwah ๐Ÿ’–๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from