EVP_ENCRYPTINIT(3SSL)
๐ EVP_ENCRYPTINIT(3SSL) ๐
ok bestie, sit down, hydrate, because this is the EVP_ENCRYPTINIT(3SSL) manpage from OpenSSL and it is not a small page. it's giving "the entire EVP cipher API walked into the group chat at the same time" ๐ like, the NAME section alone is a whole guest list. we're doing it in parts, so breathe.
this part? just the guest list. nobody is encrypting anything yet, we are literally naming names. every single function below is documented on this page โ that's the whole point of a NAME section, and she is thorough ๐ธ
no cap, it's a lot. but we're not skipping a single one, because that's how manpages work and because i respect you too much to lie to you.
โจ NAME โจ
the tagline for the whole squad is literally EVP cipher routines. that's it, that's the one-liner.
so here's everybody, cute and grouped so your eyeballs don't fall out ๐
๐ she fetches, she refs, she frees (the object's whole life cycle)
EVP_CIPHER_fetchEVP_CIPHER_up_refEVP_CIPHER_free
๐ฆ the context girlies โ new, reset, free, dup, copy
EVP_CIPHER_CTX_newEVP_CIPHER_CTX_resetEVP_CIPHER_CTX_freeEVP_CIPHER_CTX_dupEVP_CIPHER_CTX_copy
๐ฅ the init / update / final pipeline (encrypt side)
EVP_EncryptInit_exEVP_EncryptInit_ex2EVP_EncryptUpdateEVP_EncryptFinal_ex
๐ธ the init / update / final pipeline (decrypt side)
EVP_DecryptInit_exEVP_DecryptInit_ex2EVP_DecryptUpdateEVP_DecryptFinal_ex
โจ the "either direction, i don't care" versions
EVP_CipherInit_exEVP_CipherInit_ex2EVP_CipherInit_SKEYEVP_CipherUpdateEVP_CipherFinal_ex
๐ the knobs and levers on the context
EVP_CIPHER_CTX_set_key_lengthEVP_CIPHER_CTX_ctrl
๐ the no-suffix originals (the blueprint, the blueprint)
EVP_EncryptInitEVP_EncryptFinalEVP_DecryptInitEVP_DecryptFinalEVP_CipherInitEVP_CipherFinalEVP_Cipher
๐ง pipeline era โ yes there's a whole separate squad for this
EVP_CIPHER_can_pipelineEVP_CipherPipelineEncryptInitEVP_CipherPipelineDecryptInitEVP_CipherPipelineUpdateEVP_CipherPipelineFinal
๐ญ looking a cipher up by name, nid or object
EVP_get_cipherbynameEVP_get_cipherbynidEVP_get_cipherbyobj
๐ whose cipher even is this โ identity and description
EVP_CIPHER_is_aEVP_CIPHER_get0_nameEVP_CIPHER_get0_descriptionEVP_CIPHER_names_do_allEVP_CIPHER_get0_providerEVP_CIPHER_get_nid
๐ params era (the gettable / settable crowd)
EVP_CIPHER_get_paramsEVP_CIPHER_gettable_paramsEVP_CIPHER_get_block_sizeEVP_CIPHER_get_key_lengthEVP_CIPHER_get_iv_lengthEVP_CIPHER_get_flagsEVP_CIPHER_get_modeEVP_CIPHER_get_type
๐ cipher context interrogation, part one
EVP_CIPHER_CTX_cipherEVP_CIPHER_CTX_get0_cipherEVP_CIPHER_CTX_get1_cipherEVP_CIPHER_CTX_get0_nameEVP_CIPHER_CTX_get_nidEVP_CIPHER_CTX_get_paramsEVP_CIPHER_gettable_ctx_paramsEVP_CIPHER_CTX_gettable_paramsEVP_CIPHER_CTX_set_paramsEVP_CIPHER_settable_ctx_paramsEVP_CIPHER_CTX_settable_params
๐ซถ cipher context interrogation, part two (sizes & tag)
EVP_CIPHER_CTX_get_block_sizeEVP_CIPHER_CTX_get_key_lengthEVP_CIPHER_CTX_get_iv_lengthEVP_CIPHER_CTX_get_tag_length
โญ flags, flags, flags
EVP_CIPHER_CTX_flagsEVP_CIPHER_CTX_set_flagsEVP_CIPHER_CTX_clear_flagsEVP_CIPHER_CTX_test_flags
๐ฎ misc context introspection
EVP_CIPHER_CTX_get_typeEVP_CIPHER_CTX_get_modeEVP_CIPHER_CTX_get_numEVP_CIPHER_CTX_set_numEVP_CIPHER_CTX_is_encrypting
๐ ASN.1 and padding business
EVP_CIPHER_param_to_asn1EVP_CIPHER_asn1_to_paramEVP_CIPHER_CTX_set_paddingEVP_enc_null
๐ the sweep-everything-and-everyone one
EVP_CIPHER_do_all_provided
๐ง the short, chill short-form squad
EVP_CIPHER_nidEVP_CIPHER_nameEVP_CIPHER_block_sizeEVP_CIPHER_key_lengthEVP_CIPHER_iv_lengthEVP_CIPHER_flagsEVP_CIPHER_modeEVP_CIPHER_type
๐ and the matching short forms, but for the context
EVP_CIPHER_CTX_encryptingEVP_CIPHER_CTX_nidEVP_CIPHER_CTX_block_sizeEVP_CIPHER_CTX_key_lengthEVP_CIPHER_CTX_iv_lengthEVP_CIPHER_CTX_tag_lengthEVP_CIPHER_CTX_numEVP_CIPHER_CTX_typeEVP_CIPHER_CTX_mode
ok so that's the whole NAME section. every single one of those is fair game for this page, and yes, they're all real, and yes, one manpage really does carry all of them ๐ญ props to her, honestly.
next up we get into the SYNOPSIS, where all the arguments and return types show up and it gets very real ๐
โจ SYNOPSIS โจ
#include <openssl/evp.h>
EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
const char *properties);
int EVP_CIPHER_up_ref(EVP_CIPHER *cipher);
void EVP_CIPHER_free(EVP_CIPHER *cipher);
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void);
int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx);
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx);
EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in);
int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in);
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
ENGINE *impl, const unsigned char *key, const unsigned char *iv);
int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv,
const OSSL_PARAM params[]);
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
int *outl, const unsigned char *in, int inl);
int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
ENGINE *impl, const unsigned char *key, const unsigned char *iv);
int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv,
const OSSL_PARAM params[]);
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
int *outl, const unsigned char *in, int inl);
int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc);
int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv,
int enc, const OSSL_PARAM params[]);
int EVP_CipherInit_SKEY(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
EVP_SKEY *skey, const unsigned char *iv, size_t iv_len,
int enc, const OSSL_PARAM params[]);
int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
int *outl, const unsigned char *in, int inl);
int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv);
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv);
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv, int enc);
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, unsigned int inl);
int EVP_CIPHER_can_pipeline(const EVP_CIPHER *cipher, int enc);
int EVP_CipherPipelineEncryptInit(EVP_CIPHER_CTX *ctx,
const EVP_CIPHER *cipher,
const unsigned char *key, size_t keylen,
size_t numpipes,
const unsigned char **iv, size_t ivlen);
int EVP_CipherPipelineDecryptInit(EVP_CIPHER_CTX *ctx,
const EVP_CIPHER *cipher,
const unsigned char *key, size_t keylen,
size_t numpipes,
const unsigned char **iv, size_t ivlen);
int EVP_CipherPipelineUpdate(EVP_CIPHER_CTX *ctx,
unsigned char **out, size_t *outl,
const size_t *outsize,
const unsigned char **in, const size_t *inl);
int EVP_CipherPipelineFinal(EVP_CIPHER_CTX *ctx,
unsigned char **outm, size_t *outl,
const size_t *outsize);
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int cmd, int p1, void *p2);
int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key);
void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags);
void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags);
int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags);
const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
const EVP_CIPHER *EVP_get_cipherbynid(int nid);
const EVP_CIPHER *EVP_get_cipherbyobj(const ASN1_OBJECT *a);
int EVP_CIPHER_get_nid(const EVP_CIPHER *e);
int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name);
int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
void (*fn)(const char *name, void *data),
void *data);
const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher);
const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher);
const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher);
int EVP_CIPHER_get_block_size(const EVP_CIPHER *e);
int EVP_CIPHER_get_key_length(const EVP_CIPHER *e);
int EVP_CIPHER_get_iv_length(const EVP_CIPHER *e);
unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *e);
unsigned long EVP_CIPHER_get_mode(const EVP_CIPHER *e);
int EVP_CIPHER_get_type(const EVP_CIPHER *cipher);
const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx);
EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx);
const char *EVP_CIPHER_CTX_get0_name(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[]);
int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[]);
int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[]);
const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher);
const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher);
const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher);
const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *ctx);
const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_type(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_mode(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num);
int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
void (*fn)(EVP_CIPHER *cipher, void *arg),
void *arg);
#define EVP_CIPHER_nid EVP_CIPHER_get_nid
#define EVP_CIPHER_name EVP_CIPHER_get0_name
#define EVP_CIPHER_block_size EVP_CIPHER_get_block_size
#define EVP_CIPHER_key_length EVP_CIPHER_get_key_length
#define EVP_CIPHER_iv_length EVP_CIPHER_get_iv_length
#define EVP_CIPHER_flags EVP_CIPHER_get_flags
#define EVP_CIPHER_mode EVP_CIPHER_get_mode
#define EVP_CIPHER_type EVP_CIPHER_get_type
#define EVP_CIPHER_CTX_encrypting EVP_CIPHER_CTX_is_encrypting
#define EVP_CIPHER_CTX_nid EVP_CIPHER_CTX_get_nid
#define EVP_CIPHER_CTX_block_size EVP_CIPHER_CTX_get_block_size
#define EVP_CIPHER_CTX_key_length EVP_CIPHER_CTX_get_key_length
#define EVP_CIPHER_CTX_iv_length EVP_CIPHER_CTX_get_iv_length
#define EVP_CIPHER_CTX_tag_length EVP_CIPHER_CTX_get_tag_length
#define EVP_CIPHER_CTX_num EVP_CIPHER_CTX_get_num
#define EVP_CIPHER_CTX_type EVP_CIPHER_CTX_get_type
#define EVP_CIPHER_CTX_mode EVP_CIPHER_CTX_get_mode
The following function has been deprecated since OpenSSL 3.0, and can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):
const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx);
The following function has been deprecated since OpenSSL 1.1.0, and can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):
int EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx);
๐ DESCRIPTION ๐
Okay bestie, so the EVP cipher routines are basically a โจhigh-levelโจ interface to certain symmetric ciphers. That's the whole vibe. ๐ซถ
And the EVP_CIPHER type? That's a structure for cipher method implementation. Structural queen. ๐
๐ธ EVP_CIPHER_fetch()
Fetches the cipher implementation for the given algorithm from any provider offering it, within the criteria given by the properties. If you wanna go deeper on that, check out "ALGORITHM FETCHING" in crypto(7) for further information.
โ ๏ธ Heads up: the returned value must eventually be freed with EVP_CIPHER_free().
Also, fetched EVP_CIPHER structures are reference counted. So track those refs, bestie, they don't clean up after themselves. ๐
๐ EVP_CIPHER_up_ref()
Increments the reference count for an EVP_CIPHER structure. That's it, that's the function. Slay. โจ
๐ EVP_CIPHER_free()
Decrements the reference count for the fetched EVP_CIPHER structure. If the reference count drops to 0 then the structure is freed. And if the argument is NULL, nothing is done. Chill behaviour, honestly. ๐
๐ฆ EVP_CIPHER_CTX_new()
Allocates and returns a cipher context. New ctx, who dis. ๐
๐ EVP_CIPHER_CTX_free()
Clears all information from a cipher context and frees any allocated memory associated with it, including ctx itself. You should call this after all operations using a cipher are complete so that sensitive information does not remain in memory. Like, don't just leave your secrets lying around in RAM, that's not the move. ๐ If the argument is NULL, nothing is done.
โญ EVP_CIPHER_CTX_dup()
Can be used to duplicate the cipher state from in. This is lowkey iconic when you wanna avoid multiple EVP_CIPHER_fetch() calls, or if large amounts of data are to be fed which only differ in the last few bytes. Copy-paste but make it cryptography. ๐ซ
๐ง EVP_CIPHER_CTX_copy()
Can be used to copy the cipher state from in to out. Simple as that. ๐ซถ
๐ EVP_CIPHER_CTX_ctrl()
Okay so this one is a legacy method. Past its era, respectfully. EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() is the mechanism that should be used to set and get parameters that are used by providers. That's the main character now. โญ
Performs cipher-specific control actions on the context ctx. The control command is hanging out in cmd, and any extra lil arguments go in p1 and p2. ๐
EVP_CIPHER_CTX_ctrl()must be called afterEVP_CipherInit_ex2(). Other restrictions may apply depending on the control type and the cipher implementation โ so yeah, it depends, bestie.
If this function happens to be used with a fetched EVP_CIPHER, it will translate the controls that are known to OpenSSL into OSSL_PARAM(3) parameters with keys defined by OpenSSL, and then call EVP_CIPHER_CTX_get_params() or EVP_CIPHER_CTX_set_params() as is appropriate for each control command. It's giving translation services, and it's serving. โจ
See "CONTROLS" below for more info, including what translations are being done. ๐
EVP_CIPHER_get_params()
Retrieves the requested list of algorithm params from a CIPHER cipher. Peep "PARAMETERS" below for the deets. ๐
EVP_CIPHER_CTX_get_params()
Retrieves the requested list of params from a CIPHER context ctx. "PARAMETERS" below has your back. ๐ฆ
EVP_CIPHER_CTX_set_params()
Sets the list of params into a CIPHER context ctx. Again: "PARAMETERS", below. ๐ธ
EVP_CIPHER_gettable_params()
Gets you a constant OSSL_PARAM(3) array that describes the retrievable parameters you can use with EVP_CIPHER_get_params(). it's the menu, basically. ๐
EVP_CIPHER_gettable_ctx_params() and EVP_CIPHER_CTX_gettable_params()
Gets you a constant OSSL_PARAM(3) array that describes the retrievable parameters that can be used with EVP_CIPHER_CTX_get_params(). ๐ซถ
The difference, exactly:
EVP_CIPHER_gettable_ctx_params()returns the parameters that can be retrieved from the algorithm.EVP_CIPHER_CTX_gettable_params()returns the parameters that can be retrieved in the context's current state.
EVP_CIPHER_settable_ctx_params() and EVP_CIPHER_CTX_settable_params()
Gets you a constant OSSL_PARAM(3) array that describes the settable parameters that can be used with EVP_CIPHER_CTX_set_params(). ๐
And the same split as above, no cap:
EVP_CIPHER_settable_ctx_params()returns the parameters that can be set from the algorithm.EVP_CIPHER_CTX_settable_params()returns the parameters that can be set in the context's current state.
EVP_EncryptInit_ex2()
Sets up the cipher context ctx for encryption with cipher type. ctx MUST NOT be NULL. ๐ญ
type is typically supplied by calling EVP_CIPHER_fetch(). It may also be set using legacy functions such as EVP_aes_256_cbc(), but bestie, this is not recommended for new applications. ๐
key is the symmetric key to use and iv is the IV to use (if necessary) โ the actual number of bytes used for the key and the IV depends on the cipher, so don't just assume, k? The parameters params will be set on the context after initialisation.
It is possible to set all parameters to NULL except type in an initial call, and supply the remaining parameters in subsequent calls, all of which have type set to NULL. This is done when the default cipher parameters are not appropriate. ๐ง
For EVP_CIPH_GCM_MODE the IV will be generated internally if it is not specified. iconic behaviour honestly. โจ
EVP_EncryptInit_ex()
This legacy function is the same as EVP_EncryptInit_ex2() except params will be NULL. Also: impl must be NULL. that's the whole vibe. ๐
EVP_EncryptUpdate()
Encrypts inl bytes from the buffer in and writes the encrypted version to out. ๐ฅ
The pointers out and in may point to the same location, in which case the encryption will be done in-place. However, in-place encryption is guaranteed to work only if the encryption context (ctx) has processed data in multiples of the block size. If the context contains an incomplete data block from previous operations, in-place encryption will fail. sorry bestie, that's just the rules.
If out and in point to different locations, the two buffers must be disjoint โ otherwise the operation might fail or the outcome might be undefined. ๐จ
ctx MUST NOT be NULL.
This function can be called multiple times to encrypt successive blocks of data. The amount of data written depends on the block alignment of the encrypted data, so here's the tea:
- For most ciphers and modes, the amount of data written can be anything from zero bytes to
(inl + cipher_block_size - 1)bytes. - For wrap cipher modes, the amount of data written can be anything from zero bytes to
(inl rounded up to cipher_block_size + cipher_block_size)bytes. - For stream ciphers, the amount of data written can be anything from zero bytes to inl bytes.
Thus, the buffer pointed to by out must contain sufficient room for the operation being performed. The actual number of bytes written is placed in outl. ๐
If padding is enabled (the default ๐) then EVP_EncryptFinal_ex() encrypts the "final" data, that is any data that remains in a partial block. It uses standard block padding (aka PKCS padding) as described in the NOTES section, below. The encrypted final data is written to out, which should have sufficient space for one cipher block. The number of bytes written is placed in outl. After this function is called, the encryption operation is finished and no further calls to EVP_EncryptUpdate() should be made. periodt. โ
If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more data, and it will return an error if any data remains in a partial block โ that is, if the total data length is not a multiple of the block size. ๐
EVP_DecryptInit_ex2(), EVP_DecryptInit_ex(), EVP_DecryptUpdate() and EVP_DecryptFinal_ex()
These functions are the corresponding decryption operations. ๐ฆ
EVP_DecryptFinal() will return an error code if padding is enabled and the final block is not correctly formatted. The parameters and restrictions are identical to the encryption operations. And yeah โ ctx MUST NOT be NULL. ๐ญ
EVP_CipherInit_ex2(), EVP_CipherInit_ex(), EVP_CipherUpdate() and EVP_CipherFinal_ex()
These functions can be used for decryption or encryption โ flexible queens. ๐ The operation performed depends on the value of the enc parameter:
- set it to
1for encryption - set it to
0for decryption - set it to
-1to leave the value unchanged (the actual value of 'enc' being supplied in a previous call)
EVP_CipherInit_SKEY()
This function is similar to EVP_CipherInit_ex2() but accepts a symmetric key object of type EVP_SKEY as a key. same energy, different key delivery. ๐
EVP_CIPHER_CTX_reset()
Clears all information from a cipher context and frees up any allocated memory associated with it โ except the ctx itself. ๐งน
This function should be called anytime ctx is reused by another EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal() series of calls. reuse responsibly, bestie.
EVP_EncryptInit(), EVP_DecryptInit() and EVP_CipherInit()
Behave in a similar way to EVP_EncryptInit_ex(), EVP_DecryptInit_ex() and EVP_CipherInit_ex() โ except if the type is not a fetched cipher, they use the default implementation of the type. ๐
EVP_EncryptFinal(), EVP_DecryptFinal() and EVP_CipherFinal()
Identical to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and EVP_CipherFinal_ex(). ๐ซถ
In previous releases they also cleaned up the ctx, but this is no longer done and EVP_CIPHER_CTX_cleanup() must be called to free any context resources. not me still thinking about the old behaviour ๐
EVP_Cipher()
Encrypts or decrypts a maximum inl amount of bytes from in and leaves the result in out. ๐ฅ
For legacy ciphers: if the cipher doesn't have the flag EVP_CIPH_FLAG_CUSTOM_CIPHER set, then inl must be a multiple of EVP_CIPHER_get_block_size(). If it isn't, the result is undefined. ๐ญ If the cipher has that flag set, then inl can be any size.
Due to the constraints of the API contract of this function it shouldn't be used in applications, please consider usingEVP_CipherUpdate()andEVP_CipherFinal_ex()instead. this one's giving "do not perceive me" energy ๐
EVP_CIPHER_can_pipeline()
This function checks if an EVP_CIPHER fetched using EVP_CIPHER_fetch() supports cipher pipelining. If the cipher supports pipelining, it returns 1, otherwise 0. โ
This function will return 0 for non-fetched ciphers such as EVP_aes_128_gcm(). And: there are currently no built-in ciphers that support pipelining. ๐
Cipher pipelining support allows an application to submit multiple chunks of data in one set of EVP_CipherUpdate() / EVP_CipherFinal calls, thereby allowing the provided implementation to take advantage of parallel computing. This is beneficial for hardware accelerators as pipeline amortizes the latency over multiple chunks. it's giving efficiency slay. โก
For non-fetched ciphers, EVP_CipherPipelineEncryptInit() or EVP_CipherPipelineDecryptInit() may be directly called, which will perform a fetch and return an error if a pipeline supported implementation is not found.
EVP_CipherPipelineEncryptInit(), EVP_CipherPipelineDecryptInit(), EVP_CipherPipelineUpdate() and EVP_CipherPipelineFinal()
These functions can be used to perform multiple encryption or decryption operations in parallel. ๐
EVP_CIPHER_can_pipeline() may be called to check if the cipher supports pipelining. These functions are analogous to EVP_EncryptInit_ex2(), EVP_DecryptInit_ex2(), EVP_CipherUpdate() and EVP_CipherFinal() but take an array of pointers for iv, input and output buffers. ๐ฆ
The key, of length keylen, is the symmetric key to use. The numpipes parameter specifies the number of parallel operations to perform. The numpipes cannot exceed EVP_MAX_PIPES. โจ
The iv parameter is an array of buffer pointers, containing IVs. The array size must be equal to numpipes. The size of each IV buffer must be equal to ivlen. When IV is not provided, iv must be NULL, rather than an array of NULL pointers.
The in parameter takes an array of buffer pointers, each pointing to a buffer containing the input data. The buffers can be of different sizes. The inl parameter is an array of size_t, each specifying the size of the corresponding input buffer.
The out and outm parameters are arrays of buffer pointers, each pointing to a buffer where the output data will be written. The outsize parameter is an array of size_t, each specifying the size of the corresponding output buffer. The outl parameter is an array of size_t which will be updated with the size of the output data written to the corresponding output buffer.
For the size requirement of the output buffers, see the description of EVP_CipherUpdate(). ๐
The EVP_CipherPipelineUpdate() function can be called multiple times to encrypt successive blocks of data. For AAD data, the out, and outsize parameter should be NULL, rather than an array of NULL pointers. ๐
๐ซ EVP_get_cipherbyname(), EVP_get_cipherbynid() and EVP_get_cipherbyobj()
Hands you back an EVP_CIPHER structure when you pass in a cipher name, a cipher NID, or an ASN1_OBJECT structure respectively. Cute little trio fr. ๐ซถ
EVP_get_cipherbyname() will straight up return NULL for algorithms like "AES-128-SIV", "AES-128-CBC-CTS" and "CAMELLIA-128-CBC-CTS" โ the ones that used to only be reachable through the low level interfaces. So if you're sitting there like "where is my cipher ๐ญ", that's why bestie.
EVP_get_cipherbyname() is only still around for backwards compatibility with OpenSSL before version 3, and it is not the same vibe as EVP_CIPHER_fetch() because it does not attempt to "fetch" an implementation of the cipher. Also ๐ it only knows about ciphers that are built-in to OpenSSL and have an associated NID. Same energy for EVP_get_cipherbynid() and EVP_get_cipherbyobj() โ they also return objects with no associated implementation.
So when you actually use the cipher objects these functions hand you (like in a call to EVP_EncryptInit_ex()) an implementation of the cipher gets implicitly fetched from the loaded providers. And that fetch could fail if no suitable implementation is available. ๐ Use EVP_CIPHER_fetch() instead when you wanna explicitly fetch the algorithm and an associated implementation from a provider. That's the move.
See "ALGORITHM FETCHING" in crypto(7) for more deets about fetching.
Oh and the cipher objects these functions return do not need to be freed with EVP_CIPHER_free(). Free real estate, no cleanup, we love that for us. โจ
๐ซ EVP_CIPHER_get_nid() and EVP_CIPHER_CTX_get_nid()
Return the NID of a cipher when you pass an EVP_CIPHER or an EVP_CIPHER_CTX structure. The actual NID value is an internal value which may not have a corresponding OBJECT IDENTIFIER, iykyk. NID_undef gets returned if the nid is unknown, or if the cipher hasn't been properly initialized via a call to EVP_CipherInit.
๐ซ EVP_CIPHER_CTX_set_flags(), EVP_CIPHER_CTX_clear_flags() and EVP_CIPHER_CTX_test_flags()
Sets, clears and tests ctx flags. Peep "FLAGS" below for the whole tea. โ
For provided ciphers, EVP_CIPHER_CTX_set_flags() should only be called after the fetched cipher has been assigned to the ctx. And it is recommended to use "PARAMETERS" instead. Noted bestie ๐.
๐ซ EVP_CIPHER_CTX_set_padding()
Enables or disables padding. You should call this after the context is set up for encryption or decryption with EVP_EncryptInit_ex2(), EVP_DecryptInit_ex2(), EVP_CipherInit_ex2(), or EVP_CipherInit_SKEY(). By default encryption operations are padded using standard block padding, and the padding is checked and removed when decrypting. ๐
If the pad parameter is zero then no padding is performed, and then the total amount of data encrypted or decrypted must be a multiple of the block size or an error will occur. Bestie if it's not a multiple, it's giving error, no cap.
x MUST NOT be NULL. ๐จ
๐ซ EVP_CIPHER_get_key_length() and EVP_CIPHER_CTX_get_key_length()
Return the key length of a cipher when passed an EVP_CIPHER or an EVP_CIPHER_CTX structure. The constant EVP_MAX_KEY_LENGTH is the maximum key length for all ciphers. ๐
Note ๐: even though EVP_CIPHER_get_key_length() is fixed for a given cipher, the value of EVP_CIPHER_CTX_get_key_length() may be different for variable key length ciphers. So don't be out here assuming they match, ok?
๐ซ EVP_CIPHER_CTX_set_key_length()
Sets the key length of the cipher context. If the cipher is a fixed length cipher then attempting to set the key length to any value other than the fixed value is an error. Periodt. ๐
๐ซ EVP_CIPHER_get_iv_length() and EVP_CIPHER_CTX_get_iv_length()
Return the IV length of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX. It will return zero if:
- the cipher does not use an IV ๐ซ
- the cipher has not yet been initialized within the
EVP_CIPHER_CTX - or if the passed cipher is
NULL
The constant EVP_MAX_IV_LENGTH is the maximum IV length for all ciphers.
๐ซ EVP_CIPHER_CTX_get_tag_length()
Returns the tag length of an AEAD cipher when you pass it an EVP_CIPHER_CTX. It will return zero if the cipher does not support a tag. And it returns a default value if the tag length has not been set. ๐ท๏ธ
๐ซ EVP_CIPHER_get_block_size() and EVP_CIPHER_CTX_get_block_size()
Return the block size of a cipher when passed an EVP_CIPHER or EVP_CIPHER_CTX structure. The constant EVP_MAX_BLOCK_LENGTH is also the maximum block length for all ciphers.
A value of 0 gets returned if, with EVP_CIPHER_get_block_size(), the cipher e is NULL; or, with EVP_CIPHER_CTX_get_block_size(), the context ctx is NULL or has not been properly initialized with a call to EVP_CipherInit. ๐ฆ
๐ซ EVP_CIPHER_get_type() and EVP_CIPHER_CTX_get_type()
Return the type of the passed cipher or context. This "type" is the actual NID of the cipher OBJECT IDENTIFIER and as such it ignores the cipher parameters ๐ณ โ like, 40 bit RC2 and 128 bit RC2 have the same NID. If the cipher does not have an object identifier or does not have ASN1 support, this function will return NID_undef.
๐ซ EVP_CIPHER_is_a()
Returns 1 if cipher is an implementation of an algorithm that's identifiable with name, otherwise 0. If cipher is a legacy cipher (read: it's the return value from the likes of EVP_aes128() rather than the result of an EVP_CIPHER_fetch()), then only cipher names registered with the default library context (see OSSL_LIB_CTX(3)) will be considered. ๐
๐ซ EVP_CIPHER_get0_name() and EVP_CIPHER_CTX_get0_name()
Return the name of the passed cipher or context. For fetched ciphers with multiple names, only one of them is returned. See also EVP_CIPHER_names_do_all(). And bestie โ
cipher MUST NOT be NULL. ๐ญ
๐ซ EVP_CIPHER_names_do_all()
Traverses all names for the cipher, and calls fn with each name and data. This is only useful with fetched EVP_CIPHERs. โจ
๐ซ EVP_CIPHER_get0_description()
Returns a description of the cipher, meant for display and human consumption. The description is at the discretion of the cipher implementation. So basically whatever the cipher feels like saying about itself ๐ .
๐ซ EVP_CIPHER_get0_provider()
Returns an OSSL_PROVIDER pointer to the provider that implements the given EVP_CIPHER.
๐ซ EVP_CIPHER_CTX_get0_cipher()
Returns the EVP_CIPHER structure when passed an EVP_CIPHER_CTX structure. EVP_CIPHER_CTX_get1_cipher() is the same except the ownership is passed to the caller. Both functions return NULL on error. ๐
๐ซ EVP_CIPHER_get_mode() and EVP_CIPHER_CTX_get_mode()
Return the block cipher mode:
EVP_CIPH_ECB_MODEEVP_CIPH_CBC_MODEEVP_CIPH_CFB_MODEEVP_CIPH_OFB_MODEEVP_CIPH_CTR_MODEEVP_CIPH_GCM_MODEEVP_CIPH_CCM_MODEEVP_CIPH_XTS_MODEEVP_CIPH_WRAP_MODEEVP_CIPH_OCB_MODEEVP_CIPH_SIV_MODE
If the cipher is a stream cipher then EVP_CIPH_STREAM_CIPHER is returned. ๐
๐ซ EVP_CIPHER_get_flags()
Returns any flags associated with the cipher. See "FLAGS" for a list of currently defined flags. ๐ฉ
๐ซ EVP_CIPHER_CTX_get_num() and EVP_CIPHER_CTX_set_num()
Gets or sets the cipher specific "num" parameter for the associated ctx. Built-in ciphers typically use this to track how much of the current underlying block has been "used" already. Lowkey their little usage counter. ๐ข
๐ซ EVP_CIPHER_CTX_is_encrypting()
Reports whether the ctx is being used for encryption or decryption. ๐
๐ซ EVP_CIPHER_CTX_flags()
A deprecated macro calling "EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))".
Do not use. ๐ซ๐ญ Like, bestie, please. She's deprecated for a reason.
๐ซ EVP_CIPHER_param_to_asn1()
Sets the AlgorithmIdentifier "parameter" based on the passed cipher. This will typically include any parameters and an IV. The cipher IV (if any) must be set when this call is made. And this call should be made before the cipher is actually "used" (so, before any EVP_EncryptUpdate(), EVP_DecryptUpdate() calls for example). ๐
This function may fail if the cipher does not have any ASN1 support, or if an uninitialized cipher is passed to it. ๐
๐ซ EVP_CIPHER_asn1_to_param()
Sets the cipher parameters based on an ASN1 AlgorithmIdentifier "parameter". The precise effect depends on the cipher. In the case of RC2, for example, it will set the IV and effective key length. ๐
This function should be called after the base cipher type is set but before the key is set. Like so:
EVP_CipherInit()will be called with the IV and key set toNULLEVP_CIPHER_asn1_to_param()will be called- finally
EVP_CipherInit()again with all parameters except the key set toNULL
It is possible for this function to fail if the cipher does not have any ASN1 support, or the parameters cannot be set (for example the RC2 effective key length is not supported. ๐
๐ซ EVP_CIPHER_CTX_rand_key()
Generates a random key of the appropriate length based on the cipher context. The EVP_CIPHER can provide its own random key generation routine to support keys of a specific form. key must point to a buffer at least as big as the value returned by EVP_CIPHER_CTX_get_key_length(). ๐ฒ
๐ซ EVP_CIPHER_do_all_provided()
Traverses all ciphers implemented by all activated providers in the given library context libctx, and for each of the implementations, calls the given function fn with the implementation method and the given arg as argument. Doing the most, every single one ๐ซถ.
โจ PARAMETERS โจ
See OSSL_PARAM(3) for info about passing parameters.
๐ง Gettable EVP_CIPHER parameters
When EVP_CIPHER_fetch() is called it internally calls EVP_CIPHER_get_params() and caches the results. Sneaky but efficient ๐
.
EVP_CIPHER_get_params() can be used with the following OSSL_PARAM(3) keys:
"mode"(OSSL_CIPHER_PARAM_MODE)<unsigned integer>โ Gets the mode for the associated cipher algorithmcipher. See"EVP_CIPHER_get_mode() and EVP_CIPHER_CTX_get_mode()"for a list of valid modes. UseEVP_CIPHER_get_mode()to retrieve the cached value. ๐"keylen"(OSSL_CIPHER_PARAM_KEYLEN)<unsigned integer>โ Gets the key length for the associated cipher algorithmcipher. UseEVP_CIPHER_get_key_length()to retrieve the cached value. ๐ -"ivlen"(OSSL_CIPHER_PARAM_IVLEN)<unsigned integer>โ Gets the IV length for the associated cipher algorithmcipher. UseEVP_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 algorithmcipher. The block size should be1for stream ciphers. ๐
Note that the block size for a cipher may be different to the block size for the underlying encryption/decryption primitive. For example AES in CTR mode has a block size of 1 (because it operates like a stream cipher), even though AES has a block size of 16. Use EVP_CIPHER_get_block_size() to retrieve the cached value. ๐ฆ
"aead"(OSSL_CIPHER_PARAM_AEAD)<integer>โ gives u 1 if this is an AEAD cipher algorithm, and 0 if it's not. Wanna grab that cached value urself? Use(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER). slay ๐"custom-iv"(OSSL_CIPHER_PARAM_CUSTOM_IV)<integer>โ 1 if the cipher algorithmcipherhas a custom IV, 0 if not. And bestie, if a custom IV is in play, then storing it and initializing it is left entirely to the implementation โ not ur problem. Wanna peek at the cached value?(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_CUSTOM_IV)."cts"(OSSL_CIPHER_PARAM_CTS)<integer>โ 1 if the cipher algorithmcipheruses ciphertext stealing, else 0. rn this is used to flag that the cipher is a one shot that only allows a single call toEVP_CipherUpdate(). Cached value lives at(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CTS)."tls-multi"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK)<integer>โ 1 if the cipher algorithmciphersupports interleaving of crypto blocks, 0 if not. ngl the interleaving is an optimization only applicable to certain TLS ciphers, so don't come for it. Cached value:(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)."has-randkey"(OSSL_CIPHER_PARAM_HAS_RANDKEY)<integer>โ 1 if the cipher algorithmciphersupports the gettableEVP_CIPHER_CTXparameterOSSL_CIPHER_PARAM_RANDOM_KEY. ๐ and likeโฆ only DES and 3DES set this to 1, every other OpenSSL cipher returns 0. no crumbs for anyone else."encrypt-then-mac"(OSSL_CIPHER_PARAM_ENCRYPT_THEN_MAC)<integer>โ 1 if the cipher algorithmciphersupports TLS Encrypt-then-MAC, otherwise u get a 0."decrypt-only"(OSSL_CIPHER_PARAM_DECRYPT_ONLY)<integer>โ 1 if the cipher algorithmcipherimplementation supports only the decryption operation, like the 3DES ciphers in thefipsprovider. Otherwise u get 0, or the parameter might not even be present at all. yeah. it's giving mystery โจ
๐ซถ Gettable and Settable EVP_CIPHER_CTX parameters ๐ซถ
These OSSL_PARAM(3) keys work with both EVP_CIPHER_CTX_get_params() and EVP_CIPHER_CTX_set_params(). versatile queens ๐
"padding"(OSSL_CIPHER_PARAM_PADDING)<unsigned integer>โ gets or sets the padding mode for the cipher contextctx. Padding is enabled if the value is 1, and disabled if it's 0. See alsoEVP_CIPHER_CTX_set_padding()."num"(OSSL_CIPHER_PARAM_NUM)<unsigned integer>โ gets or sets the cipher specific"num"parameter for the cipher contextctx. Built-in ciphers typically use this to track how much of the current underlying block has already been "used". See alsoEVP_CIPHER_CTX_get_num()andEVP_CIPHER_CTX_set_num()."keylen"(OSSL_CIPHER_PARAM_KEYLEN)<unsigned integer>โ gets or sets the key length for the cipher contextctx. Heads up: the length of the"keylen"parameter should not exceed that of asize_t. See alsoEVP_CIPHER_CTX_get_key_length()andEVP_CIPHER_CTX_set_key_length()."tag"(OSSL_CIPHER_PARAM_AEAD_TAG)<octet string>โ gets or sets the AEAD tag for the associated cipher contextctx. Check"AEAD INTERFACE"inEVP_EncryptInit(3)for the deets."pipeline-tag"(OSSL_CIPHER_PARAM_PIPELINE_AEAD_TAG)<octet ptr>โ gets or sets the AEAD tag when ur using cipher pipelining. The pointer must point to an array of buffers, where the aead tag will be read from or written to. And the array size must be equal tonumpipesused inEVP_CipherPipelineEncryptInit()orEVP_CipherPipelineDecryptInit(). no shortcuts bestie ๐"keybits"(OSSL_CIPHER_PARAM_RC2_KEYBITS)<unsigned integer>โ gets or sets the effective keybits used for a RC2 cipher. Same lil warning as before: the length of the"keybits"parameter should not exceed that of asize_t."rounds"(OSSL_CIPHER_PARAM_ROUNDS)<unsigned integer>โ gets or sets the number of rounds to be used for a cipher. This one's for the RC5 cipher. ๐"algorithm-id"(OSSL_CIPHER_PARAM_ALGORITHM_ID)<octet string>โ used to get the DER encoded AlgorithmIdentifier from the cipher implementation. Functions likeEVP_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 likeEVP_CIPHER_CTX_set_algor_params(3)andEVP_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 byEVP_CIPHER_param_to_asn1(3)andEVP_CIPHER_asn1_to_param(3). it's giving legacy era, we don't use it anymore fr ๐"cts_mode"(OSSL_CIPHER_PARAM_CTS_MODE)<UTF8 string>โ gets or sets the cipher text stealing mode. For all modes the output size is the same as the input size, and the input length must be greater than or equal to the block size. (The block size for AES and CAMELLIA is 16 bytes โ noting it down bestie ๐)
Valid values for the mode are:
"CS1"โ the NIST variant of cipher text stealing. For input lengths that are multiples of the block size it is equivalent to using a"AES-XXX-CBC"or"CAMELLIA-XXX-CBC"cipher, otherwise the second last cipher text block is a partial block."CS2"โ for input lengths that are multiples of the block size it is equivalent to using a"AES-XXX-CBC"or"CAMELLIA-XXX-CBC"cipher, otherwise it is the same as"CS3"mode."CS3"โ the Kerberos5 variant of cipher text stealing, which always swaps the last cipher text block with the previous block (which may be a partial or full block depending on the input length). If the input length is exactly one full block then this is equivalent to using a"AES-XXX-CBC"or"CAMELLIA-XXX-CBC"cipher.
The default is "CS1". And this is only supported for "AES-128-CBC-CTS", "AES-192-CBC-CTS", "AES-256-CBC-CTS", "CAMELLIA-128-CBC-CTS", "CAMELLIA-192-CBC-CTS" and "CAMELLIA-256-CBC-CTS". periodt ๐
"tls1multi_interleave"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE)<unsigned integer>โ sets or gets the number of records being sent in one go for a tls1 multiblock cipher operation (either 4 or 8 records). pick ur lane, 4 or 8, there's no in between โจ
๐ Gettable EVP_CIPHER_CTX parameters ๐
The following OSSL_PARAM(3) keys can be used with EVP_CIPHER_CTX_get_params() โ read-only cuties only, no setting:
"ivlen"(OSSL_CIPHER_PARAM_IVLENandOSSL_CIPHER_PARAM_AEAD_IVLEN)<unsigned integer>โ gets the IV length for the cipher contextctx. And yk the drill: the length of the"ivlen"parameter should not exceed that of asize_t. See alsoEVP_CIPHER_CTX_get_iv_length()."iv"(OSSL_CIPHER_PARAM_IV)<octet string OR octet ptr>โ gets the IV used to initialize the associated cipher contextctx. See alsoEVP_CIPHER_CTX_get_original_iv()."updated-iv"(OSSL_CIPHER_PARAM_UPDATED_IV)<octet string OR octet ptr>โ gets the updated pseudo-IV state for the associated cipher context, e.g., the previous ciphertext block for CBC mode, or the iteratively encrypted IV value for OFB mode. Heads up tho: octet pointer access is deprecated and is provided only for backwards compatibility with historical libcrypto APIs. See alsoEVP_CIPHER_CTX_get_updated_iv()."randkey"(OSSL_CIPHER_PARAM_RANDOM_KEY)<octet string>โ gets an implementation specific randomly generated key for the associated cipher contextctx. rn this is only supported by DES and 3DES (which set the key to odd parity). fcfs, everyone else stay seated ๐ช"taglen"(OSSL_CIPHER_PARAM_AEAD_TAGLEN)<unsigned integer>โ gets the tag length to be used for an AEAD cipher for the associated cipher contextctx. It gets a default value if it hasn't been set. The length of the"taglen"parameter should not exceed that of asize_t. See alsoEVP_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 contextctx. And once again: the length of the"tlsaadpad"parameter should not exceed that of asize_t."tlsivgen"(OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN)<octet string>โ gets the invocation field generated for encryption. Can only be called after"tlsivfixed"is set. And this is only used for GCM mode. ๐"tls1multi_enclen"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN)<unsigned integer>โ get the total length of the record returned from the"tls1multi_enc"operation. it's giving receipts ๐"tls1multi_maxbufsz"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE)<unsigned integer>โ gets the maximum record length for a TLS1 multiblock cipher operation. bestie. u already know. the length of the"tls1multi_maxbufsz"parameter should not exceed that of asize_t. ๐"tls1multi_aadpacklen"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN)<unsigned integer>โ gets the result of running the"tls1multi_aad"operation."tls-mac"(OSSL_CIPHER_PARAM_TLS_MAC)<octet ptr>โ used to pass the TLS MAC data. short n sweet โจ"fips-indicator"(OSSL_CIPHER_PARAM_FIPS_APPROVED_INDICATOR)<integer>โ this option is used by the OpenSSL FIPS provider.
It's a getter that returns 1 if the operation is FIPS approved, or 0 otherwise. U can use it after calling a cipher final operation such as EVP_EncryptFinal_ex(). It may return 0 if the "encrypt-check" option is set to 0. For GCM encryption it also returns 0 if the IV was supplied externally instead of being generated by the FIPS provider. The separate "iv-generated" parameter reports the IV source directly, but callers can use "fips-indicator" to determine the overall approval status. And for GCM encryption and decryption, tag lengths other than 4, 8, or 12 through 16 bytes also cause this indicator to return 0. note it down ๐
"iv-generated"(OSSL_CIPHER_PARAM_AEAD_IV_GENERATED)<unsigned integer>โ an indicator that returns 1 if an IV was generated internally during encryption, or 0 otherwise. GCM ciphers may use this after calling a cipher final operation such asEVP_EncryptFinal_ex(). GCM should generate an IV internally if the IV isn't specified during a cipher initialisation call such asEVP_CipherInit_ex(). See FIPS 140-3 IG C.H for information related to IV requirements. โจ
๐ธ Settable EVP_CIPHER_CTX parameters ๐ธ
The following OSSL_PARAM(3) keys can be used with EVP_CIPHER_CTX_set_params() โ set it and forget it energy:
"mackey"(OSSL_CIPHER_PARAM_AEAD_MAC_KEY)<octet string>โ sets the MAC key used by composite AEAD ciphers such asAES-CBC-HMAC-SHA256. go off, composite queen ๐"speed"(OSSL_CIPHER_PARAM_SPEED) โ unsigned integer energy. Sets the speed option for the cipher context it's attached to. Ngl this one's only supported by AES SIV ciphers, and those lowkey refuse to do multiple operations by default ๐ โ๏ธ. Set"speed"to1and suddenly you're allowed to do another encrypt or decrypt operation. It's giving performance testing, fr fr."use-bits"(OSSL_CIPHER_PARAM_USE_BITS) โ unsigned integer bestie. This decides whether the input lengthinlthat you pass toEVP_EncryptUpdate(),EVP_DecryptUpdate()andEVP_CipherUpdate()is the number of bits or the number of bytes. Set"use-bits"to1and we're counting bits ๐ . The default? Bytes. Also heads up, this is only used for CFB1 ciphers.
You can also set this with EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS) โจ
"tls-version"(OSSL_CIPHER_PARAM_TLS_VERSION) โ integer. Sets the TLS version. That's it, that's the whole vibe."tls-mac-size"(OSSL_CIPHER_PARAM_TLS_MAC_SIZE) โ unsigned integer. Set the TLS MAC size ๐."tlsaad"(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD) โ octet string. Sets TLSv1.2 AAD information for the associated cipher contextctx. And like, TLSv1.2 AAD information is always 13 bytes long, no exceptions, and it's exactly as defined for the"additional_data"field described in section 6.2.3.3 of RFC5246 ๐."tlsivfixed"(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED) โ octet string. Sets the fixed portion of an IV for an AEAD cipher that's being used in TLS record encryption/decryption for the associated cipher context. Strap in bestie, this one's a whole saga:
TLS record encryption/decryption always happens "in place", so the input and output buffers are always the exact same memory location ๐ฏ. AEAD IVs in TLSv1.2 are made of an implicit "fixed" part plus an explicit part that shifts with every single record. Setting a TLS fixed IV flips a cipher into encrypt/decrypt TLS records mode. TLS records get encrypted/decrypted using a single OSSL_FUNC_cipher_cipher call per record. For a record decryption, the first bytes of the input buffer are the explicit part of the IV, and the final bytes of the input buffer are the AEAD tag. How long the explicit IV part is and how long the tag is? Depends on the cipher in use, and it's defined in the RFC for the relevant ciphersuite ๐. And so "in place" decryption can work, the plaintext output should be written to the same spot in the output buffer that the ciphertext payload was read from โ i.e. immediately after the explicit IV.
When you're encrypting a record, the first bytes of the input buffer should be empty so there's room for the explicit IV, and same for the final bytes where the tag gets written. The length of the input buffer will include the explicit IV length, the payload, and the tag bytes. The cipher implementation's job is to generate the explicit IV and write it to the beginning of the output buffer, do "in place" encryption of the payload and write that to the output buffer, and finally slap the tag onto the end of the output buffer ๐.
Whether you're encrypting or decrypting, the value written to *outl in the OSSL_FUNC_cipher_cipher call should be the length of the payload excluding the explicit IV length and the tag length. Don't forget that part, it's sneaky ๐ญ
"tlsivinv"(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV) โ octet string. Sets the invocation field used for decryption. Can only be called after"tlsivfixed"is set, so order matters bestie ๐. Also this is only used for GCM mode."tls1multi_enc"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC) โ octet string. Triggers a multiblock TLS1 encrypt operation for a TLS1 aware cipher that supports yeeting 4 or 8 records in one go ๐ฅ. The cipher does both the MAC and encrypt stages and constructs the record headers itself, main character energy."tls1multi_enc"supplies the output buffer for the encrypt operation, and"tls1multi_encin"&"tls1multi_interleave"must also be set so there are values to feed the encrypt operation."tls1multi_encin"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN) โ octet string. Supplies the data to encrypt for a TLS1 multiblock cipher operation ๐."tls1multi_maxsndfrag"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT) โ unsigned integer. Sets the maximum send fragment size for a TLS1 multiblock cipher operation. It must be set before you use"tls1multi_maxbufsz"(order is everything ๐ ). Also the length of the"tls1multi_maxsndfrag"parameter should not exceed that of asize_t."tls1multi_aad"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD) โ octet string. Sets the authenticated additional data used by a TLS1 multiblock cipher operation. The supplied data is 13 bytes of record data, laid out like this:- Bytes 0-7: The sequence number of the first record
- Byte 8: The record type
- Byte 9-10: The protocol version
- Byte 11-12: Input length (Always 0)
"tls1multi_interleave" must also be set for this operation, okurrr โจ
"xts_standard"(OSSL_CIPHER_PARAM_XTS_STANDARD) โ UTF8 string. Sets the XTS standard to use with the SM4-XTS algorithm. XTS mode has two implementations: one is standardized in IEEE Std. 1619-2007 and has been widely used (e.g., XTS AES), and the other was proposed recently (GB/T 17964-2021, implemented in May 2022) and is currently only used in SM4 ๐ฆ.
The big difference between them is the multiplication by the primitive element ฮฑ to calculate the tweak values. IEEE Std 1619-2007 noted that the multiplication "is a left shift of each byte by one bit with carry propagating from one byte to the next one", which means in each byte the leftmost bit is the most significant bit. But in GB/T 17964-2021, the rightmost bit is the most significant bit, so the multiplication becomes a right shift of each byte by one bit with carry propagating from one byte to the next one. Literally mirror images bestie ๐ช
Valid values for the mode are:
"GB"โ The GB/T 17964-2021 variant of SM4-XTS algorithm."IEEE"โ The IEEE Std. 1619-2007 variant of SM4-XTS algorithm.
The default value is "GB" ๐
"encrypt-check"(OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK) โ integer. This option is used by the OpenSSL FIPS provider ๐ง.
If required, this parameter should be set early via a cipher encrypt init function such as EVP_EncryptInit_ex2(). The default value of 1 causes an error when an encryption operation is triggered. Setting this to 0 will ignore the error and set the approved "fips-indicator" to 0.
โ ๏ธ Heads up bestie: this option breaks FIPS compliance if it causes the approved"fips-indicator"to return0. So like, know what you're doing ๐
๐ CONTROLS ๐
So the mappings from EVP_CIPHER_CTX_ctrl() identifiers over to PARAMETERS are all listed in the next bit, bestie. If you want the deep lore on each one, go peek at the PARAMETERS section.
EVP_CIPHER_CTX_ctrl() is your girl for sending these standard controls:
EVP_CTRL_AEAD_SET_IVLENandEVP_CTRL_GET_IVLENโ when you use these with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()andEVP_CIPHER_CTX_get_params()get called with anOSSL_PARAM(3)item whose key is"ivlen"(OSSL_CIPHER_PARAM_IVLEN).EVP_CTRL_AEAD_SET_IV_FIXEDโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item keyed"tlsivfixed"(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED).EVP_CTRL_AEAD_SET_MAC_KEYโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item keyed"mackey"(OSSL_CIPHER_PARAM_AEAD_MAC_KEY).EVP_CTRL_AEAD_SET_TAGandEVP_CTRL_AEAD_GET_TAGโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()andEVP_CIPHER_CTX_get_params()get called with anOSSL_PARAM(3)item keyed"tag"(OSSL_CIPHER_PARAM_AEAD_TAG).EVP_CTRL_CCM_SET_Lโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item keyed"ivlen"(OSSL_CIPHER_PARAM_IVLEN) with a value of(15 - L).EVP_CTRL_COPYโ there is literally noOSSL_PARAMmapping for this one, bestie. UseEVP_CIPHER_CTX_copy()instead.EVP_CTRL_GCM_SET_IV_INVโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item keyed"tlsivinv"(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV).EVP_CTRL_RAND_KEYโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item keyed"randkey"(OSSL_CIPHER_PARAM_RANDOM_KEY).EVP_CTRL_SET_KEY_LENGTHโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item keyed"keylen"(OSSL_CIPHER_PARAM_KEYLEN).EVP_CTRL_SET_RC2_KEY_BITSandEVP_CTRL_GET_RC2_KEY_BITSโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()andEVP_CIPHER_CTX_get_params()get called with anOSSL_PARAM(3)item keyed"keybits"(OSSL_CIPHER_PARAM_RC2_KEYBITS).EVP_CTRL_SET_RC5_ROUNDSandEVP_CTRL_GET_RC5_ROUNDSโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()andEVP_CIPHER_CTX_get_params()get called with anOSSL_PARAM(3)item keyed"rounds"(OSSL_CIPHER_PARAM_ROUNDS).EVP_CTRL_SET_SPEEDโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item keyed"speed"(OSSL_CIPHER_PARAM_SPEED).EVP_CTRL_GCM_IV_GENโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_get_params()gets called with anOSSL_PARAM(3)item keyed"tlsivgen"(OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN).EVP_CTRL_AEAD_TLS1_AADโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item keyed"tlsaad"(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD), and thenEVP_CIPHER_CTX_get_params()gets called with a key of"tlsaadpad"(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD).EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZEโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item keyedOSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, and thenEVP_CIPHER_CTX_get_params()gets called with a key of"tls1multi_maxbufsz"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE).EVP_CTRL_TLS1_1_MULTIBLOCK_AADโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called withOSSL_PARAM(3)items keyed"tls1multi_aad"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD) and"tls1multi_interleave"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE), and thenEVP_CIPHER_CTX_get_params()gets called with keys of"tls1multi_aadpacklen"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN) and"tls1multi_interleave"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE).EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPTโ with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called withOSSL_PARAM(3)items keyed"tls1multi_enc"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC),"tls1multi_encin"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN) and"tls1multi_interleave"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE), and thenEVP_CIPHER_CTX_get_params()gets called with a key of"tls1multi_enclen"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN).
๐ฉ FLAGS ๐ฉ
EVP_CIPHER_CTX_set_flags(), EVP_CIPHER_CTX_clear_flags() and EVP_CIPHER_CTX_test_flags() are the trio you use to mess with and check these EVP_CIPHER_CTX flags:
EVP_CIPH_NO_PADDINGโ used byEVP_CIPHER_CTX_set_padding(). Also go look at Gettable and Settable EVP_CIPHER_CTX parameters โ"padding".EVP_CIPH_FLAG_LENGTH_BITSโ see Settable EVP_CIPHER_CTX parameters โ"use-bits".EVP_CIPHER_CTX_FLAG_WRAP_ALLOWโ legacy purposes only, bestie. This flag had to be set to signal that the cipher handled wrapping.
EVP_CIPHER_flags() also uses the following flags, and these ones have mappings over to Gettable EVP_CIPHER parameters:
EVP_CIPH_FLAG_AEAD_CIPHERโ see Gettable EVP_CIPHER parameters โ"aead".EVP_CIPH_CUSTOM_IVโ see Gettable EVP_CIPHER parameters โ"custom-iv".EVP_CIPH_FLAG_CTSโ see Gettable EVP_CIPHER parameters โ"cts".EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK; โ see Gettable EVP_CIPHER parameters โ"tls-multi".EVP_CIPH_RAND_KEYโ see Gettable EVP_CIPHER parameters โ"has-randkey".EVP_CIPH_FLAG_ENC_THEN_MACโ see Gettable EVP_CIPHER parameters โ"encrypt-then-mac".EVP_CIPH_STREAM_CIPHER,EVP_CIPH_ECB_MODEEVP_CIPH_CBC_MODE,EVP_CIPH_CFB_MODE,EVP_CIPH_OFB_MODE,EVP_CIPH_CTR_MODE,EVP_CIPH_GCM_MODE,EVP_CIPH_CCM_MODE,EVP_CIPH_XTS_MODE,EVP_CIPH_WRAP_MODE,EVP_CIPH_OCB_MODE,EVP_CIPH_SIV_MODEโ see Gettable EVP_CIPHER parameters โ"mode".
EVP_CIPHER_flags() also uses the flag EVP_CIPH_FLAG_CUSTOM_CIPHER to signal that the cipher supports any input length when it's used with the EVP_Cipher() function. More deets live in EVP_Cipher().
And these flags down here are defined for legacy purposes and are not returned by EVP_CIPHER_flags():
EVP_CIPH_VARIABLE_LENGTHEVP_CIPH_ALWAYS_CALL_INITEVP_CIPH_CTRL_INITEVP_CIPH_CUSTOM_KEY_LENGTHEVP_CIPH_CUSTOM_COPYEVP_CIPH_FLAG_DEFAULT_ASN1
๐ RETURN VALUES ๐
EVP_CIPHER_fetch()โ returns a pointer to aEVP_CIPHERwhen it slays, andNULLwhen it flops.EVP_CIPHER_up_ref()โ returns1on success or0otherwise.EVP_CIPHER_CTX_new()โ returns a pointer to a freshly createdEVP_CIPHER_CTXon success andNULLon failure.EVP_CIPHER_CTX_dup()โ returns a newEVP_CIPHER_CTXif it worked, orNULLon failure.EVP_CIPHER_CTX_copy()โ returns1if it worked or0for failure.EVP_EncryptInit_ex2(),EVP_EncryptUpdate()andEVP_EncryptFinal_ex()โ return1for success and0for failure.EVP_DecryptInit_ex2()andEVP_DecryptUpdate()โ return1for success and0for failure. AndEVP_DecryptFinal_ex()returns0if the decrypt failed, or1for success.EVP_CipherInit_ex2(),EVP_CipherInit_SKEY()andEVP_CipherUpdate()โ return1for success and0for failure.EVP_CipherFinal_ex()returns0for an encryption/decryption failure or1for success.EVP_Cipher()โ returns1on success and<= 0on failure, if the flagEVP_CIPH_FLAG_CUSTOM_CIPHERis not set for the cipher, or if the cipher hasn't been initialized via a call toEVP_CipherInit_ex2. But if the flagEVP_CIPH_FLAG_CUSTOM_CIPHERis set for the cipher,EVP_Cipher()returns the number of bytes written to out for encryption/decryption, or the number of bytes authenticated in a call that specifies AAD for an AEAD cipher.EVP_CIPHER_can_pipeline()โ returns1if the cipher can be used in a pipeline,0otherwise.EVP_CipherPipelineEncryptInit()andEVP_CipherPipelineDecryptInit()โ return1for success and0for failure.EVP_CipherPipelineUpdate()andEVP_CipherPipelineFinal()โ return1for success and0for failure.EVP_CIPHER_CTX_reset()โ returns1for success and0for failure.EVP_get_cipherbyname(),EVP_get_cipherbynid()andEVP_get_cipherbyobj()โ return anEVP_CIPHERstructure orNULLon error.EVP_CIPHER_get_nid()andEVP_CIPHER_CTX_get_nid()โ return a NID.EVP_CIPHER_get_block_size()andEVP_CIPHER_CTX_get_block_size()โ return the block size, or0on error.EVP_CIPHER_get_key_length()andEVP_CIPHER_CTX_get_key_length()โ return the key length.EVP_CIPHER_CTX_set_padding()โ always returns1. Iconic consistency, honestly.EVP_CIPHER_get_iv_length()andEVP_CIPHER_CTX_get_iv_length()โ return the IV length,zeroif the cipher doesn't use an IV, and a negative value on error.EVP_CIPHER_CTX_get_tag_length()โ returns the tag length orzeroif the cipher doesn't use a tag.EVP_CIPHER_get_type()andEVP_CIPHER_CTX_get_type()โ return the NID of the cipher's OBJECT IDENTIFIER, orNID_undefif it has no defined OBJECT IDENTIFIER.EVP_CIPHER_CTX_cipher()โ returns anEVP_CIPHERstructure.EVP_CIPHER_CTX_get_num()โ returns a nonnegative num value, orEVP_CTRL_RET_UNSUPPORTEDif the implementation doesn't support the call or on any other error.EVP_CIPHER_CTX_set_num()โ returns1on success and0if the implementation doesn't support the call or on any other error.EVP_CIPHER_CTX_is_encrypting()โ returns1if the ctx is set up for encryption,0otherwise.EVP_CIPHER_param_to_asn1()andEVP_CIPHER_asn1_to_param()โ return greater than zero for success and zero or a negative number on failure.EVP_CIPHER_CTX_rand_key()โ returns1for success and zero or a negative number for failure.EVP_CIPHER_names_do_all()โ returns1if the callback was called for all names. A return value of0means the callback was not called for any names.EVP_CIPHER_get_params(),EVP_CIPHER_CTX_get_params()andEVP_CIPHER_CTX_set_params()โ return1for success and0for failure.
๐ CIPHER LISTING ๐
Okay so, every algorithm has a fixed key length unless it says otherwise, babes.
Go check SEE ALSO for the full list of ciphers you can get through the EVP interface. ๐
EVP_enc_null()โ the Null cipher: it literally does nothing. Queen of minding her own business. ๐
๐ AEAD INTERFACE ๐
okay bestie so the EVP interface for Authenticated Encryption with Associated Data (AEAD) modes? It's subtly altered โจ and there are extra ctrl operations that show up depending on which mode you picked. slay.
So like, if you wanna specify additional authenticated data (AAD), you make a call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with the output parameter out set to NULL. In that case, on success, the parameter outl gets set to the number of AAD bytes processed in that call (aka, the value of inl), and it does not include any plaintext or ciphertext bytes processed by other calls. ๐
If you're not using any AAD, you can just skip this call entirely. (Check the mode-specific notes below tho, there are exceptions ๐)
When you're decrypting, the return value of EVP_DecryptFinal() or EVP_CipherFinal() is what tells you whether the operation was successful. If it doesn't indicate success, the authentication operation has failed and any output data MUST NOT be used, because it is corrupted. like, it's giving garbage, throw it away ๐ญ
pls note that the number of authenticated bytes returned by EVP_CipherUpdate() depends on the cipher you're using. Stream ciphers, like ChaCha20 or ciphers in GCM mode, can handle 1 byte at a time, which gives you an effective "block" size of 1. Meanwhile ciphers in OCB mode must process data one block at a time, and the block size is what gets returned.
Regardless of the returned size, it is safe to pass unpadded data to an EVP_CipherUpdate() call in a single operation. ๐
๐ GCM and OCB Modes ๐
The following ctrls are supported in GCM and OCB modes.
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)โ Sets the IV length. This call can only be made before specifying an IV. If you don't call it, a default IV length is used.
For GCM AES and OCB AES the default is 12 (i.e. 96 bits). For OCB mode the maximum is 15.
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)โ Writestaglenbytes of the tag value to the buffer indicated bytag. This call can only be made when encrypting data and after all data has been processed (e.g. after anEVP_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 totaglenbytes fromtag.taglenmust be between1and16inclusive. The tag must be set prior to any call toEVP_DecryptFinal()orEVP_DecryptFinal_ex().
For GCM, this call is only valid when decrypting data.
For OCB, this call is valid when decrypting data to set the expected tag, and when encrypting to set the desired tag length.
In OCB mode, calling this with tag set to NULL sets the tag length. The tag length can only be set before specifying an IV. If this is not called prior to setting the IV, then a default tag length is used.
For OCB AES, the default tag length is 16 (i.e. 128 bits). It is also the maximum tag length for OCB.
๐ CCM Mode ๐
The EVP interface for CCM mode is similar to that of the GCM mode but with a few additional requirements and different ctrl values.
For CCM mode, the total plaintext or ciphertext length MUST be passed to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with the output and input parameters (in and out) set to NULL and the length passed in the inl parameter. no skipping this step bestie, it's mandatory fr fr.
The following ctrls are supported in CCM mode.
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)โ This call is made to set the expected CCM tag value when decrypting, or the length of the tag (with thetagparameter set toNULL) when encrypting. The tag length is often referred to as M. If not set a default value is used (12for 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 (8for 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 by15 - Lso it is7by default for AES.
๐ฆ SIV Mode ๐ฆ
Both the AES-SIV and AES-GCM-SIV ciphers fall under this mode.
For SIV mode ciphers the behaviour of the EVP interface is subtly altered and several additional ctrl operations are supported.
To specify any additional authenticated data (AAD) and/or a Nonce, a call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with the output parameter out set to NULL.
RFC5297 states that the Nonce is the last piece of AAD before the actual encrypt/decrypt takes place. The API does not differentiate the Nonce from other AAD. ๐คทโ๏ธ
When decrypting the return value of EVP_DecryptFinal() or EVP_CipherFinal() indicates if the operation was successful. If it does not indicate success the authentication operation has failed and any output data MUST NOT be used as it is corrupted.
The API does not store the SIV (Synthetic Initialization Vector) in the cipher text. Instead, it is stored as the tag within the EVP_CIPHER_CTX. The SIV must be retrieved from the context after encryption, and set into the context before decryption. ๐
This differs from RFC5297 in that the cipher output from encryption, and the cipher input to decryption, does not contain the SIV. This also means that the plain text and cipher text lengths are identical. iconic, honestly.
The following ctrls are supported in SIV mode, and are used to get and set the Synthetic Initialization Vector:
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag);โ Writestaglenbytes of the tag value (the Synthetic Initialization Vector) to the buffer indicated bytag. This call can only be made when encrypting data and after all data has been processed (e.g. after anEVP_EncryptFinal()call). For SIV mode thetaglenmust be16.EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag);โ Sets the expected tag (the Synthetic Initialization Vector) totaglenbytes fromtag. This call is only legal when decrypting data and must be made before any data is processed (e.g. before anyEVP_DecryptUpdate()calls). For SIV mode thetaglenmust be16.
SIV mode makes two passes over the input data, so only one call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with out set to a non-NULL value. A call to EVP_DecryptFinal() or EVP_CipherFinal() is not required, but it will indicate if the update operation succeeded. ๐
๐ ChaCha20-Poly1305 ๐
The following ctrls are supported for the ChaCha20-Poly1305 AEAD algorithm.
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)โ Sets the nonce length. This call is now redundant since the only valid value is the default length of12(i.e.96bits). Prior to OpenSSL 3.0 a nonce of less than12bytes could be used to automatically pad the iv with leading0bytes to make it12bytes in length.EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag)โ Writestaglenbytes of the tag value to the buffer indicated bytag. This call can only be made when encrypting data and after all data has been processed (e.g. after anEVP_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 totaglenbytes fromtag. The tag length can only be set before specifying an IV.taglenmust be between1and16(POLY1305_BLOCK_SIZE) inclusive. This call is only valid when decrypting data.
โจ NOTES โจ
Where possible the EVP interface to symmetric ciphers should be used in preference to the low-level interfaces. Why? Because then the code becomes transparent to the cipher used and much more flexible. ๐
Plus, the EVP interface will ensure the use of platform specific cryptographic acceleration such as AES-NI (the low-level interfaces do not provide that guarantee). so like, obviously use EVP, bestie.
PKCS padding works by adding n padding bytes of value n to make the total length of the encrypted data a multiple of the block size. Padding is always added, so if the data is already a multiple of the block size, n will equal the block size. For example if the block size is 8 and 11 bytes are to be encrypted then 5 padding bytes of value 5 will be added. math queen behavior ๐งฎโจ
When decrypting the final block is checked to see if it has the correct form.
Although the decryption operation can produce an error if padding is enabled, it is not a strong test that the input data or key is correct. A random block has better than 1 in 256 chance of being of the correct format, and problems with the input data earlier on will not produce a final decrypt error. ๐
If padding is disabled then the decryption operation will always succeed if the total amount of data decrypted is a multiple of the block size.
โ ๏ธ heads up: the functionsEVP_EncryptInit(),EVP_EncryptInit_ex(),EVP_EncryptFinal(),EVP_DecryptInit(),EVP_DecryptInit_ex(),EVP_CipherInit(),EVP_CipherInit_ex()andEVP_CipherFinal()are obsolete but are retained for compatibility with existing code. New code should useEVP_EncryptInit_ex2(),EVP_EncryptFinal_ex(),EVP_DecryptInit_ex2(),EVP_DecryptFinal_ex(),EVP_CipherInit_ex2()andEVP_CipherFinal_ex()because they can reuse an existing context without allocating and freeing it up on each call. stop using the old ones, i'm begging ๐ญ
There are some differences between functions EVP_CipherInit() and EVP_CipherInit_ex(), significant in some circumstances. EVP_CipherInit() fills the passed context object with zeros. As a consequence, EVP_CipherInit() does not allow step-by-step initialization of the ctx when the key and iv are passed in separate calls. It also means that the flags set for the CTX are removed, and it is especially important for the EVP_CIPHER_CTX_FLAG_WRAP_ALLOW flag treated specially in EVP_CipherInit_ex(). so like, know the difference, it matters fr fr.
๐จ Ignoring failure returns of theEVP_CIPHER_CTXinitialization functions can lead to subsequent undefined behavior when calling the functions that update or finalize the context. The only valid calls on theEVP_CIPHER_CTXwhen initialization fails are calls that attempt another initialization of the context or release the context. literally do not ignore those return values bestie.
EVP_get_cipherbynid(), and EVP_get_cipherbyobj() are implemented as macros.
๐ BUGS ๐
Okay bestie, listen up, because this part is giving "handle with care" energy:
EVP_MAX_KEY_LENGTHandEVP_MAX_IV_LENGTHonly refer to the internal ciphers with default key lengths. If custom ciphers go over those values, the results are straight up unpredictable ๐ญ. Why? Because it's become standard practice to define a generic key as a fixedunsigned chararray containingEVP_MAX_KEY_LENGTHbytes. That's it, that's the whole reason.- The ASN1 code is incomplete (and sometimes inaccurate) ๐ โ it's only been tested for certain common S/MIME ciphers (
RC2,DES, tripleDES) inCBCmode. So like, don't come for it about edge cases, it simply has not been there.
โจ EXAMPLES โจ
Not me giving you four whole examples because I love you ๐ซถ. Let's go.
๐ธ Encrypt a string using IDEA
int do_crypt(char *outfile)
{
unsigned char outbuf[1024];
int outlen, tmplen;
/*
* Bogus key and IV: we'd normally set these from
* another source.
*/
unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
unsigned char iv[] = {1,2,3,4,5,6,7,8};
char intext[] = "Some Crypto Text";
EVP_CIPHER_CTX *ctx;
FILE *out;
ctx = EVP_CIPHER_CTX_new();
if (!EVP_EncryptInit_ex2(ctx, EVP_idea_cbc(), key, iv, NULL)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
/*
* Buffer passed to EVP_EncryptFinal() must be after data just
* encrypted to avoid overwriting it.
*/
if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
outlen += tmplen;
EVP_CIPHER_CTX_free(ctx);
/*
* Need binary mode for fopen because encrypted data is
* binary data. Also cannot use strlen() on it because
* it won't be NUL terminated and may contain embedded
* NULs.
*/
out = fopen(outfile, "wb");
if (out == NULL) {
/* Error */
return 0;
}
fwrite(outbuf, 1, outlen, out);
fclose(out);
return 1;
}
See that comment about outbuf + outlen? That's the whole vibe โ the buffer you hand to EVP_EncryptFinal() has to sit after the data you just encrypted, otherwise you're overwriting your own slay ๐
. And yes you need "wb" for fopen because encrypted data is binary, and you absolutely cannot strlen() it because it won't be NUL terminated and might contain embedded NULs. Not me warning you twice about the same thing. Iconic behavior.
The ciphertext from the above example can be decrypted using the openssl utility with this command line (shown on two lines for clarity, because we're being polite):
openssl idea -d \
-K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 <filename
๐ฆ General encryption and decryption function example using FILE I/O and AES128 with a 128-bit key
int do_crypt(FILE *in, FILE *out, int do_encrypt)
{
/* Allow enough space in output buffer for additional block */
unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
int inlen, outlen;
EVP_CIPHER_CTX *ctx;
/*
* Bogus key and IV: we'd normally set these from
* another source.
*/
unsigned char key[] = "0123456789abcdeF";
unsigned char iv[] = "1234567887654321";
/* Don't set key or IV right away; we want to check lengths */
ctx = EVP_CIPHER_CTX_new();
if (!EVP_CipherInit_ex2(ctx, EVP_aes_128_cbc(), NULL, NULL,
do_encrypt, NULL)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
OPENSSL_assert(EVP_CIPHER_CTX_get_key_length(ctx) == 16);
OPENSSL_assert(EVP_CIPHER_CTX_get_iv_length(ctx) == 16);
/* Now we can set key and IV */
if (!EVP_CipherInit_ex2(ctx, NULL, key, iv, do_encrypt, NULL)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
for (;;) {
inlen = fread(inbuf, 1, 1024, in);
if (inlen <= 0)
break;
if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
fwrite(outbuf, 1, outlen, out);
}
if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
fwrite(outbuf, 1, outlen, out);
EVP_CIPHER_CTX_free(ctx);
return 1;
}
Lowkey the little OPENSSL_assert moment is the main character here โ check your lengths before you commit, queen ๐.
๐ Encryption using AES-CBC with a 256-bit key with "CS1" ciphertext stealing
int encrypt(const unsigned char *key, const unsigned char *iv,
const unsigned char *msg, size_t msg_len, unsigned char *out)
{
/*
* This assumes that key size is 32 bytes and the iv is 16 bytes.
* For ciphertext stealing mode the length of the ciphertext "out" will be
* the same size as the plaintext size "msg_len".
* The "msg_len" can be any size >= 16.
*/
int ret = 0, encrypt = 1, outlen, len;
EVP_CIPHER_CTX *ctx = NULL;
EVP_CIPHER *cipher = NULL;
OSSL_PARAM params[2];
ctx = EVP_CIPHER_CTX_new();
cipher = EVP_CIPHER_fetch(NULL, "AES-256-CBC-CTS", NULL);
if (ctx == NULL || cipher == NULL)
goto err;
/*
* The default is "CS1" so this is not really needed,
* but would be needed to set either "CS2" or "CS3".
*/
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
"CS1", 0);
params[1] = OSSL_PARAM_construct_end();
if (!EVP_CipherInit_ex2(ctx, cipher, key, iv, encrypt, params))
goto err;
/* NOTE: CTS mode does not support multiple calls to EVP_CipherUpdate() */
if (!EVP_CipherUpdate(ctx, out, &outlen, msg, msg_len))
goto err;
if (!EVP_CipherFinal_ex(ctx, out + outlen, &len))
goto err;
ret = 1;
err:
EVP_CIPHER_free(cipher);
EVP_CIPHER_CTX_free(ctx);
return ret;
}
๐จ ThatNOTEis not decorative, bestie: CTS mode does not support multiple calls toEVP_CipherUpdate(). One shot. Periodt.
Also yes, the default is "CS1", so setting it in params[0] is not really needed โ it's just showing you where you'd put "CS2" or "CS3" instead. iykyk ๐
๐ SEE ALSO ๐
Go read your other girls:
evp(7)property(7)"ALGORITHM FETCHING" in crypto(7)provider-cipher(7)life_cycle-cipher(7)
Supported ciphers are listed in:
EVP_aes_128_gcm(3)EVP_aria_128_gcm(3)EVP_bf_cbc(3)EVP_camellia_128_ecb(3)EVP_cast5_cbc(3)EVP_chacha20(3)EVP_des_cbc(3)EVP_desx_cbc(3)EVP_idea_cbc(3)EVP_rc2_cbc(3)EVP_rc4(3)EVP_rc5_32_12_16_cbc(3)EVP_seed_cbc(3)EVP_sm4_cbc(3)
๐ HISTORY ๐
The lore, in order, no skips:
- Support for
OCBmode was added in OpenSSL 1.1.0. EVP_CIPHER_CTXwas made opaque in OpenSSL 1.1.0. As a result,EVP_CIPHER_CTX_reset()appeared andEVP_CIPHER_CTX_cleanup()disappeared.EVP_CIPHER_CTX_init()remains as an alias forEVP_CIPHER_CTX_reset().- The
EVP_CIPHER_CTX_cipher()function was deprecated in OpenSSL 3.0; useEVP_CIPHER_CTX_get0_cipher()instead. (Not the old one, babe.) - These functions were added in 3.0:
EVP_EncryptInit_ex2(),EVP_DecryptInit_ex2(),EVP_CipherInit_ex2(),EVP_CIPHER_fetch(),EVP_CIPHER_free(),EVP_CIPHER_up_ref(),EVP_CIPHER_CTX_get0_cipher(),EVP_CIPHER_CTX_get1_cipher(),EVP_CIPHER_get_params(),EVP_CIPHER_CTX_set_params(),EVP_CIPHER_CTX_get_params(),EVP_CIPHER_gettable_params(),EVP_CIPHER_settable_ctx_params(),EVP_CIPHER_gettable_ctx_params(),EVP_CIPHER_CTX_settable_params()andEVP_CIPHER_CTX_gettable_params(). - These functions were renamed to include
"get"or"get0"in their names in OpenSSL 3.0, respectively:EVP_CIPHER_nid(),EVP_CIPHER_name(),EVP_CIPHER_block_size(),EVP_CIPHER_key_length(),EVP_CIPHER_iv_length(),EVP_CIPHER_flags(),EVP_CIPHER_mode(),EVP_CIPHER_type(),EVP_CIPHER_CTX_nid(),EVP_CIPHER_CTX_block_size(),EVP_CIPHER_CTX_key_length(),EVP_CIPHER_CTX_iv_length(),EVP_CIPHER_CTX_tag_length(),EVP_CIPHER_CTX_num(),EVP_CIPHER_CTX_type(), andEVP_CIPHER_CTX_mode(). The old names are kept as non-deprecated alias macros, so don't panic ๐ซถ. EVP_CIPHER_CTX_encrypting()was renamed toEVP_CIPHER_CTX_is_encrypting()in OpenSSL 3.0. The old name is kept as a non-deprecated alias macro.- The
EVP_CIPHER_CTX_flags()macro was deprecated in OpenSSL 1.1.0. EVP_CIPHER_CTX_dup()was added in OpenSSL 3.2.EVP_CipherInit_SKEY()was added in OpenSSL 3.5.- Before OpenSSL 3.5, passing a
NULLctxtoEVP_CIPHER_CTX_get_block_size()would give you a NULL pointer dereference ๐ญ, instead of a0return value indicating an error. Glow-up achieved, honestly.
๐ COPYRIGHT ๐
Copyright 2000-2026 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
And that's a wrap on the whole page, bestie ๐ You made it to the end of EVP_ENCRYPTINIT(3SSL) โ this was 4.0.3, dated 2026-09-29, and honestly? You ate. Now go encrypt something responsibly, and remember: free your ctx, always ๐โจ
๐ฆ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 ยท Apache-2.0
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms