EVP_ENCRYPTINIT(3ossl)
πΈ NAME πΈ
okay bestie, welcome to EVP_ENCRYPTINIT(3ossl) β the OpenSSL manual page that is literally the EVP cipher routines master list π
. It is giving βhuge friend group, and every single one is invited,β so here is the roll call exactly as the page lists it β¨:
EVP_CIPHER_fetch,EVP_CIPHER_up_ref,EVP_CIPHER_free,EVP_CIPHER_CTX_new,EVP_CIPHER_CTX_reset,EVP_CIPHER_CTX_free,EVP_CIPHER_CTX_dup,EVP_CIPHER_CTX_copy,EVP_EncryptInit_ex,EVP_EncryptInit_ex2,EVP_EncryptUpdate,EVP_EncryptFinal_ex,EVP_DecryptInit_ex,EVP_DecryptInit_ex2,EVP_DecryptUpdate,EVP_DecryptFinal_ex,EVP_CipherInit_ex,EVP_CipherInit_ex2,EVP_CipherInit_SKEY,EVP_CipherUpdate,EVP_CipherFinal_ex,EVP_CIPHER_CTX_set_key_length,EVP_CIPHER_CTX_ctrl,EVP_EncryptInit,EVP_EncryptFinal,EVP_DecryptInit,EVP_DecryptFinal,EVP_CipherInit,EVP_CipherFinal,EVP_Cipher,EVP_CIPHER_can_pipeline,EVP_CipherPipelineEncryptInit,EVP_CipherPipelineDecryptInit,EVP_CipherPipelineUpdate,EVP_CipherPipelineFinal,EVP_get_cipherbyname,EVP_get_cipherbynid,EVP_get_cipherbyobj,EVP_CIPHER_is_a,EVP_CIPHER_get0_name,EVP_CIPHER_get0_description,EVP_CIPHER_names_do_all,EVP_CIPHER_get0_provider,EVP_CIPHER_get_nid,EVP_CIPHER_get_params,EVP_CIPHER_gettable_params,EVP_CIPHER_get_block_size,EVP_CIPHER_get_key_length,EVP_CIPHER_get_iv_length,EVP_CIPHER_get_flags,EVP_CIPHER_get_mode,EVP_CIPHER_get_type,EVP_CIPHER_CTX_cipher,EVP_CIPHER_CTX_get0_cipher,EVP_CIPHER_CTX_get1_cipher,EVP_CIPHER_CTX_get0_name,EVP_CIPHER_CTX_get_nid,EVP_CIPHER_CTX_get_params,EVP_CIPHER_gettable_ctx_params,EVP_CIPHER_CTX_gettable_params,EVP_CIPHER_CTX_set_params,EVP_CIPHER_settable_ctx_params,EVP_CIPHER_CTX_settable_params,EVP_CIPHER_CTX_get_block_size,EVP_CIPHER_CTX_get_key_length,EVP_CIPHER_CTX_get_iv_length,EVP_CIPHER_CTX_get_tag_length,EVP_CIPHER_CTX_flags,EVP_CIPHER_CTX_set_flags,EVP_CIPHER_CTX_clear_flags,EVP_CIPHER_CTX_test_flags,EVP_CIPHER_CTX_get_type,EVP_CIPHER_CTX_get_mode,EVP_CIPHER_CTX_get_num,EVP_CIPHER_CTX_set_num,EVP_CIPHER_CTX_is_encrypting,EVP_CIPHER_param_to_asn1,EVP_CIPHER_asn1_to_param,EVP_CIPHER_CTX_set_padding,EVP_enc_null,EVP_CIPHER_do_all_provided,EVP_CIPHER_nid,EVP_CIPHER_name,EVP_CIPHER_block_size,EVP_CIPHER_key_length,EVP_CIPHER_iv_length,EVP_CIPHER_flags,EVP_CIPHER_mode,EVP_CIPHER_type,EVP_CIPHER_CTX_encrypting,EVP_CIPHER_CTX_nid,EVP_CIPHER_CTX_block_size,EVP_CIPHER_CTX_key_length,EVP_CIPHER_CTX_iv_length,EVP_CIPHER_CTX_tag_length,EVP_CIPHER_CTX_num,EVP_CIPHER_CTX_type,EVP_CIPHER_CTX_modeβ and that whole iconic roster is summed up as EVP cipher routines π«Ά.
β¨ SYNOPSIS β¨
okay bestie, here's the whole cast of characters. no cap, it's a long list β but every single one of them is important, so no skimming π
#include <openssl/evp.h>
EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
const char *properties);
int EVP_CIPHER_up_ref(EVP_CIPHER *cipher);
void EVP_CIPHER_free(EVP_CIPHER *cipher);
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void);
int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx);
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx);
EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in);
int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in);
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
ENGINE *impl, const unsigned char *key, const unsigned char *iv);
int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv,
const OSSL_PARAM params[]);
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
int *outl, const unsigned char *in, int inl);
int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
ENGINE *impl, const unsigned char *key, const unsigned char *iv);
int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv,
const OSSL_PARAM params[]);
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
int *outl, const unsigned char *in, int inl);
int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
ENGINE *impl, const unsigned char *key, const unsigned char *iv, int enc);
int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv,
int enc, const OSSL_PARAM params[]);
int EVP_CipherInit_SKEY(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
EVP_SKEY *skey, const unsigned char *iv, size_t iv_len,
int enc, const OSSL_PARAM params[]);
int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
int *outl, const unsigned char *in, int inl);
int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv);
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);
int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv);
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
const unsigned char *key, const unsigned char *iv, int enc);
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl);
int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
const unsigned char *in, unsigned int inl);
int EVP_CIPHER_can_pipeline(const EVP_CIPHER *cipher, int enc);
int EVP_CipherPipelineEncryptInit(EVP_CIPHER_CTX *ctx,
const EVP_CIPHER *cipher,
const unsigned char *key, size_t keylen,
size_t numpipes,
const unsigned char **iv, size_t ivlen);
int EVP_CipherPipelineDecryptInit(EVP_CIPHER_CTX *ctx,
const EVP_CIPHER *cipher,
const unsigned char *key, size_t keylen,
size_t numpipes,
const unsigned char **iv, size_t ivlen);
int EVP_CipherPipelineUpdate(EVP_CIPHER_CTX *ctx,
unsigned char **out, size_t *outl,
const size_t *outsize,
const unsigned char **in, const size_t *inl);
int EVP_CipherPipelineFinal(EVP_CIPHER_CTX *ctx,
unsigned char **outm, size_t *outl,
const size_t *outsize);
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *x, int padding);
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *x, int keylen);
int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int cmd, int p1, void *p2);
int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key);
void EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags);
void EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags);
int EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags);
const EVP_CIPHER *EVP_get_cipherbyname(const char *name);
const EVP_CIPHER *EVP_get_cipherbynid(int nid);
const EVP_CIPHER *EVP_get_cipherbyobj(const ASN1_OBJECT *a);
int EVP_CIPHER_get_nid(const EVP_CIPHER *e);
int EVP_CIPHER_is_a(const EVP_CIPHER *cipher, const char *name);
int EVP_CIPHER_names_do_all(const EVP_CIPHER *cipher,
void (*fn)(const char *name, void *data),
void *data);
const char *EVP_CIPHER_get0_name(const EVP_CIPHER *cipher);
const char *EVP_CIPHER_get0_description(const EVP_CIPHER *cipher);
const OSSL_PROVIDER *EVP_CIPHER_get0_provider(const EVP_CIPHER *cipher);
int EVP_CIPHER_get_block_size(const EVP_CIPHER *e);
int EVP_CIPHER_get_key_length(const EVP_CIPHER *e);
int EVP_CIPHER_get_iv_length(const EVP_CIPHER *e);
unsigned long EVP_CIPHER_get_flags(const EVP_CIPHER *e);
unsigned long EVP_CIPHER_get_mode(const EVP_CIPHER *e);
int EVP_CIPHER_get_type(const EVP_CIPHER *cipher);
const EVP_CIPHER *EVP_CIPHER_CTX_get0_cipher(const EVP_CIPHER_CTX *ctx);
EVP_CIPHER *EVP_CIPHER_CTX_get1_cipher(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_nid(const EVP_CIPHER_CTX *ctx);
const char *EVP_CIPHER_CTX_get0_name(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[]);
int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[]);
int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[]);
const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher);
const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher);
const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher);
const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *ctx);
const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_block_size(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_key_length(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_iv_length(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_tag_length(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_type(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_mode(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_get_num(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_CTX_set_num(EVP_CIPHER_CTX *ctx, int num);
int EVP_CIPHER_CTX_is_encrypting(const EVP_CIPHER_CTX *ctx);
int EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
int EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
void (*fn)(EVP_CIPHER *cipher, void *arg),
void *arg);
#define EVP_CIPHER_nid EVP_CIPHER_get_nid
#define EVP_CIPHER_name EVP_CIPHER_get0_name
#define EVP_CIPHER_block_size EVP_CIPHER_get_block_size
#define EVP_CIPHER_key_length EVP_CIPHER_get_key_length
#define EVP_CIPHER_iv_length EVP_CIPHER_get_iv_length
#define EVP_CIPHER_flags EVP_CIPHER_get_flags
#define EVP_CIPHER_mode EVP_CIPHER_get_mode
#define EVP_CIPHER_type EVP_CIPHER_get_type
#define EVP_CIPHER_CTX_encrypting EVP_CIPHER_CTX_is_encrypting
#define EVP_CIPHER_CTX_nid EVP_CIPHER_CTX_get_nid
#define EVP_CIPHER_CTX_block_size EVP_CIPHER_CTX_get_block_size
#define EVP_CIPHER_CTX_key_length EVP_CIPHER_CTX_get_key_length
#define EVP_CIPHER_CTX_iv_length EVP_CIPHER_CTX_get_iv_length
#define EVP_CIPHER_CTX_tag_length EVP_CIPHER_CTX_get_tag_length
#define EVP_CIPHER_CTX_num EVP_CIPHER_CTX_get_num
#define EVP_CIPHER_CTX_type EVP_CIPHER_CTX_get_type
#define EVP_CIPHER_CTX_mode EVP_CIPHER_CTX_get_mode
okay so there are like two tiny old-school functions left over that you might not even see depending on your build π
The following function has been deprecated since OpenSSL 3.0, and can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):
const EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx);
The following function has been deprecated since OpenSSL 1.1.0, and can be hidden entirely by defining OPENSSL_API_COMPAT with a suitable version value, see openssl_user_macros(7):
int EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx);
π DESCRIPTION π
okay, the moment you've all been waiting for π The EVP cipher routines are a high-level interface to certain symmetric ciphers. high-level, as in: you don't gotta micro-manage the math, the interface handles the vibes for you β¨
The EVP_CIPHER type is a structure for cipher method implementation. that's it, that's the whole glow-up β it's the struct holding the cipher method's implementation. iconic.
π¦ EVP_CIPHER_fetch() π¦
Fetches the cipher implementation for the given algorithm from any provider offering it, within the criteria given by the properties. See "ALGORITHM FETCHING" in crypto(7) for further information β that's where the deets live, iykyk.
The returned value must eventually be freed with EVP_CIPHER_free(). no leaks bestie, we don't do that here π
Fetched EVP_CIPHER structures are reference counted. yes queen, refcounted.
πΈ EVP_CIPHER_up_ref() πΈ
Increments the reference count for an EVP_CIPHER structure. one more bestie holding the door open for it π«Ά
π EVP_CIPHER_free() π
Decrements the reference count for the fetched EVP_CIPHER structure. If the reference count drops to 0 then the structure is freed. If the argument is NULL, nothing is done. literally nothing, zero drama, the function just minds its own business π
π EVP_CIPHER_CTX_new() π
Allocates and returns a cipher context. fresh out of the metaphorical box, main character energy π
π EVP_CIPHER_CTX_free() π
Clears all information from a cipher context and frees any allocated memory associated with it, including ctx itself. This function should be called after all operations using a cipher are complete so sensitive information does not remain in memory. If the argument is NULL, nothing is done. π₯ yes, wipe the sensitive stuff β we are not leaving key material lying around, that's giving chaos.
π§ EVP_CIPHER_CTX_dup() π§
Can be used to duplicate the cipher state from in. This is useful to avoid multiple EVP_CIPHER_fetch() calls or if large amounts of data are to be fed which only differ in the last few bytes. clone the vibe, skip the re-fetching β¨
β EVP_CIPHER_CTX_copy() β
Can be used to copy the cipher state from in to out. simple as that, no cap.
π EVP_CIPHER_CTX_ctrl() π
This is a legacy method. EVP_CIPHER_CTX_set_params() and EVP_CIPHER_CTX_get_params() is the mechanism that should be used to set and get parameters that are used by providers. like, respectfully, use the new ones β this one's giving "old phone in a drawer" π΅
EVP_CIPHER_CTX_ctrl()</p>
Does the cipher-specific control thing on the context ctx β think of it as hitting the special buttons on a specific cipher. The control command goes in cmd, and any extra args ride along in p1 and p2. Bestie, listen: EVP_CIPHER_CTX_ctrl() must be called after EVP_CipherInit_ex2(). Other restrictions may apply depending on the control type and the cipher implementation, so likeβ¦ vibe check your cipher first β¨
If this function happens to get used with a fetched EVP_CIPHER, it'll translate the controls that are known to OpenSSL into OSSL_PARAM(3) parameters with keys defined by OpenSSL, and then call EVP_CIPHER_CTX_get_params() or EVP_CIPHER_CTX_set_params() as appropriate for each control command. Glow-up via translation, love that for us.
See "CONTROLS" below for more info, including what translations are actually being done.
EVP_CIPHER_get_params()</p>
Pulls the requested list of algorithm params from a CIPHER cipher. Check "PARAMETERS" below for the deets.
EVP_CIPHER_CTX_get_params()</p>
Pulls the requested list of params from the CIPHER context ctx. Again, "PARAMETERS" below has the lore.
EVP_CIPHER_CTX_set_params()</p>
Sets the list of params into a CIPHER context ctx. "PARAMETERS", below, always below π
EVP_CIPHER_gettable_params()</p>
Grabs you a constant OSSL_PARAM(3) array that describes the retrievable parameters you can use with EVP_CIPHER_get_params().
EVP_CIPHER_gettable_ctx_params()</p> and EVP_CIPHER_CTX_gettable_params()</p>
Both hand you a constant OSSL_PARAM(3) array describing the retrievable parameters usable with EVP_CIPHER_CTX_get_params(). The difference, bestie: EVP_CIPHER_gettable_ctx_params() returns the parameters retrievable from the algorithm, whereas EVP_CIPHER_CTX_gettable_params() returns the parameters retrievable in the context's current state. Same energy, different scope π¦
EVP_CIPHER_settable_ctx_params()</p> and EVP_CIPHER_CTX_settable_params()</p>
Same deal but for setting: both give you a constant OSSL_PARAM(3) array describing the settable parameters usable with EVP_CIPHER_CTX_set_params(). EVP_CIPHER_settable_ctx_params() returns the parameters that can be set from the algorithm, while EVP_CIPHER_CTX_settable_params() returns the parameters that can be set in the context's current state. Iconic parallel structure, no notes π
EVP_EncryptInit_ex2()</p>
Sets up cipher context ctx for encryption with cipher type. ctx MUST NOT be NULL β like, do not do that, fr fr π type is typically supplied by calling EVP_CIPHER_fetch(). type may also be set using legacy functions such as EVP_aes_256_cbc(), but this is not recommended for new applications. key is the symmetric key to use, and iv is the IV to use (if necessary) β the actual number of bytes used for the key and IV depends on the cipher. The parameters params will be set on the context after initialisation. It's possible to set all parameters to NULL except type in an initial call and supply the remaining parameters in subsequent calls, all of which have type set to NULL β you do this when the default cipher parameters aren't appropriate. Oh and for EVP_CIPH_GCM_MODE, the IV will be generated internally if it's not specified. Surprise IV! π
EVP_EncryptInit_ex()</p>
This legacy function is similar to EVP_EncryptInit_ex2() when impl is NULL. The implementation of the type from the impl engine will be used if it exists.
EVP_EncryptUpdate()</p>
Encrypts inl bytes from the buffer in and writes the encrypted version to out. The pointers out and in may point to the same location, in which case the encryption will be done in-place π«Ά However β and this is the important part β in-place encryption is guaranteed to work only if the encryption context (ctx) has processed data in multiples of the block size. If the context contains an incomplete data block from previous operations, in-place encryption will fail. Also ctx MUST NOT be NULL.
If out and in point to different locations, the two buffers must be disjoint, otherwise the operation might fail or the outcome might be undefined. Undefined behaviour is not the vibe π
This function can be called multiple times to encrypt successive blocks of data. The amount of data written depends on the block alignment of the encrypted data. For most ciphers and modes, the amount of data written can be anything from zero bytes to (inl + cipher_block_size - 1) bytes. For wrap cipher modes, the amount of data written can be anything from zero bytes to (inl + cipher_block_size) bytes. For stream ciphers, the amount of data written can be anything from zero bytes to inl bytes. So the buffer pointed to by out must contain sufficient room for the operation being performed. The actual number of bytes written is placed in outl.
If padding is enabled (the default) then EVP_EncryptFinal_ex() encrypts the "final" data, that is any data that remains in a partial block. It uses standard block padding (aka PKCS padding) as described in the NOTES section, below. The encrypted final data is written to out, which should have sufficient space for one cipher block. The number of bytes written is placed in outl. After this function is called the encryption operation is finished and no further calls to EVP_EncryptUpdate() should be made. Operation over, go home π§
If padding is disabled then EVP_EncryptFinal_ex() will not encrypt any more data and it will return an error if any data remains in a partial block β that is, if the total data length is not a multiple of the block size. Warning received, loud and clear π₯
EVP_DecryptInit_ex2()</p>, EVP_DecryptInit_ex()</p>, EVP_DecryptUpdate()</p> and EVP_DecryptFinal_ex()</p>
These are the corresponding decryption operations. EVP_DecryptFinal() will return an error code if padding is enabled and the final block is not correctly formatted. The parameters and restrictions are identical to the encryption operations. ctx MUST NOT be NULL. Symmetry slay π
EVP_CipherInit_ex2()</p>, EVP_CipherInit_ex()</p>, EVP_CipherUpdate()</p> and EVP_CipherFinal_ex()</p>
These can be used for decryption or encryption β main character energy, doing both π The operation performed depends on the value of the enc parameter. It should be set to 1 for encryption, 0 for decryption, and -1 to leave the value unchanged (the actual value of enc being supplied in a previous call).
EVP_CipherInit_SKEY()</p>
Similar to EVP_CipherInit_ex2() but it accepts a symmetric key object of type EVP_SKEY as a key. Fancy key object instead of raw key bytes, we love a glow-up π
EVP_CIPHER_CTX_reset()</p>
Clears all information from a cipher context and frees up any allocated memory associated with it β except the ctx itself, which stays. This function should be called anytime ctx is reused by another EVP_CipherInit() / EVP_CipherUpdate() / EVP_CipherFinal() series of calls. Clean slate, fresh start, periodt β¨
EVP_EncryptInit()</p>, EVP_DecryptInit()</p> and EVP_CipherInit()</p>
Behave similarly to EVP_EncryptInit_ex(), EVP_DecryptInit_ex() and EVP_CipherInit_ex() β except if the type is not a fetched cipher, they use the default implementation of the type.
EVP_EncryptFinal()</p>, EVP_DecryptFinal()</p> and EVP_CipherFinal()</p>
Identical to EVP_EncryptFinal_ex(), EVP_DecryptFinal_ex() and EVP_CipherFinal_ex(). In previous releases they also cleaned up the ctx, but this is no longer done β EVP_CIPHER_CTX_cleanup() must be called to free any context resources. Heads up, bestie, the old auto-cleanup is gone π
EVP_Cipher()</p>
Encrypts or decrypts a maximum of inl amount of bytes from in and leaves the result in out.
For legacy ciphers: if the cipher doesn't have the flag EVP_CIPH_FLAG_CUSTOM_CIPHER set, then inl must be a multiple of EVP_CIPHER_get_block_size(). If it isn't, the result is undefined. If the cipher does have that flag set, then inl can be any size.
Due to the constraints of the API contract of this function, it shouldn't be used in applications. Please consider usingEVP_CipherUpdate()andEVP_CipherFinal_ex()instead. Like genuinely, take the scenic route π§
EVP_CIPHER_can_pipeline()</p>
Checks whether an EVP_CIPHER fetched using EVP_CIPHER_fetch() supports cipher pipelining. If the cipher supports pipelining it returns 1, otherwise 0. It will return 0 for non-fetched ciphers such as EVP_aes_128_gcm(). And rn? There are currently no built-in ciphers that support pipelining. Empty roster, tragic π
Cipher pipelining support lets an application submit multiple chunks of data in one set of EVP_CipherUpdate()/EVP_CipherFinal calls, which lets the provided implementation take advantage of parallel computing. This is beneficial for hardware accelerators, since a pipeline amortizes the latency over multiple chunks. Efficiency era π₯
For non-fetched ciphers, EVP_CipherPipelineEncryptInit() or EVP_CipherPipelineDecryptInit() may be called directly, which will perform a fetch and return an error if a pipeline-supported implementation is not found.
EVP_CipherPipelineEncryptInit()</p>, EVP_CipherPipelineDecryptInit()</p>, EVP_CipherPipelineUpdate()</p> and EVP_CipherPipelineFinal()</p>
These let you perform multiple encryption or decryption operations in parallel. Call EVP_CIPHER_can_pipeline() to check if the cipher supports pipelining first. These functions are analogous to EVP_EncryptInit_ex2(), EVP_DecryptInit_ex2(), EVP_CipherUpdate() and EVP_CipherFinal() β but they take an array of pointers for iv, input and output buffers π¦
The key, of length keylen, is the symmetric key to use. The numpipes parameter specifies the number of parallel operations to perform. numpipes cannot exceed EVP_MAX_PIPES. The iv parameter is an array of buffer pointers containing IVs β the array size must be equal to numpipes, and the size of each IV buffer must be equal to ivlen. When an IV is not provided, iv must be NULL, rather than an array of NULL pointers (the details matter, bestie). The in parameter takes an array of buffer pointers, each pointing to a buffer containing the input data β and the buffers can be of different sizes. The inl parameter is an array of size_t, each specifying the size of the corresponding input buffer. The out and outm parameters are arrays of buffer pointers, each pointing to a buffer where the output data will be written. The outsize parameter is an array of size_t, each specifying the size of the corresponding output buffer. The outl parameter is an array of size_t which will be updated with the size of the output data written to the corresponding output buffer. For the size requirement of the output buffers, see the description of EVP_CipherUpdate(). π
EVP_CipherPipelineUpdate() can be called multiple times to encrypt successive blocks of data. For AAD data, the out and outsize parameters should be NULL, rather than an array of NULL pointers. Again: the details matter πΈ
EVP_get_cipherbyname(),EVP_get_cipherbynid()andEVP_get_cipherbyobj()β bestie, these hand you back anEVP_CIPHERstructure when you pass a cipher name, a cipher NID or anASN1_OBJECTstructure, respectively. πEVP_get_cipherbyname()will returnNULLfor algorithms such as"AES-128-SIV","AES-128-CBC-CTS"and"CAMELLIA-128-CBC-CTS"which were previously only accessible via low level interfaces. TheEVP_get_cipherbyname()function is present for backwards compatibility with OpenSSL prior to version 3 and is different to theEVP_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. SimilarlyEVP_get_cipherbynid()andEVP_get_cipherbyobj()also return objects without an associated implementation. When the cipher objects returned by these functions are used (such as in a call toEVP_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. UseEVP_CIPHER_fetch()instead to explicitly fetch the algorithm and an associated implementation from a provider. See "ALGORITHM FETCHING" incrypto(7)for more information about fetching. The cipher objects returned from these functions do not need to be freed withEVP_CIPHER_free(). π«ΆEVP_CIPHER_get_nid()andEVP_CIPHER_CTX_get_nid()β returns the NID of a cipher when passed anEVP_CIPHERorEVP_CIPHER_CTXstructure. The actual NID value is an internal value which may not have a corresponding OBJECT IDENTIFIER.NID_undefis returned in the event that the nid is unknown or if the cipher has not been properly initialized via a call toEVP_CipherInit. πEVP_CIPHER_CTX_set_flags(),EVP_CIPHER_CTX_clear_flags()andEVP_CIPHER_CTX_test_flags()β sets, clears and tests ctx flags. See "FLAGS" below for more information. For provided ciphersEVP_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 withEVP_EncryptInit_ex2(),EVP_DecryptInit_ex2(),EVP_CipherInit_ex2(), orEVP_CipherInit_SKEY(). By default encryption operations are padded using standard block padding and the padding is checked and removed when decrypting. If thepadparameter is zero then no padding is performed, the total amount of data encrypted or decrypted must then be a multiple of the block size or an error will occur. x MUST NOT be NULL. π¨EVP_CIPHER_get_key_length()andEVP_CIPHER_CTX_get_key_length()β return the key length of a cipher when passed anEVP_CIPHERorEVP_CIPHER_CTXstructure. The constantEVP_MAX_KEY_LENGTHis the maximum key length for all ciphers. Note: althoughEVP_CIPHER_get_key_length()is fixed for a given cipher, the value ofEVP_CIPHER_CTX_get_key_length()may be different for variable key length ciphers. πEVP_CIPHER_CTX_set_key_length()β sets the key length of the cipher context. If the cipher is a fixed length cipher then attempting to set the key length to any value other than the fixed value is an error. π€EVP_CIPHER_get_iv_length()andEVP_CIPHER_CTX_get_iv_length()β return the IV length of a cipher when passed anEVP_CIPHERorEVP_CIPHER_CTX. It will return zero if the cipher does not use an IV, if the cipher has not yet been initialized within theEVP_CIPHER_CTX, or if the passed cipher isNULL. The constantEVP_MAX_IV_LENGTHis the maximum IV length for all ciphers. πEVP_CIPHER_CTX_get_tag_length()β returns the tag length of an AEAD cipher when passed aEVP_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()andEVP_CIPHER_CTX_get_block_size()β return the block size of a cipher when passed anEVP_CIPHERorEVP_CIPHER_CTXstructure. The constantEVP_MAX_BLOCK_LENGTHis also the maximum block length for all ciphers. A value of 0 is returned if, withEVP_CIPHER_get_block_size(), the ciphereisNULL, or, withEVP_CIPHER_CTX_get_block_size(), the contextctxisNULLor has not been properly initialized with a call toEVP_CipherInit. π§±EVP_CIPHER_get_type()andEVP_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 returnNID_undef. πEVP_CIPHER_is_a()β returns 1 ifcipheris an implementation of an algorithm that's identifiable withname, otherwise 0. Ifcipheris a legacy cipher (it's the return value from the likes ofEVP_aes128()rather than the result of anEVP_CIPHER_fetch()), only cipher names registered with the default library context (seeOSSL_LIB_CTX(3)) will be considered. π¦EVP_CIPHER_get0_name()andEVP_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 alsoEVP_CIPHER_names_do_all().cipherMUST NOT beNULL. βEVP_CIPHER_names_do_all()β traverses all names for the cipher, and callsfnwith each name anddata. This is only useful with fetchedEVP_CIPHERs. πΈEVP_CIPHER_get0_description()β returns a description of the cipher, meant for display and human consumption. The description is at the discretion of the cipher implementation. πEVP_CIPHER_get0_provider()β returns anOSSL_PROVIDERpointer to the provider that implements the givenEVP_CIPHER. πEVP_CIPHER_CTX_get0_cipher()β returns theEVP_CIPHERstructure when passed anEVP_CIPHER_CTXstructure.EVP_CIPHER_CTX_get1_cipher()is the same except the ownership is passed to the caller. Both functions returnNULLon error. πEVP_CIPHER_get_mode()andEVP_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_MODEorEVP_CIPH_SIV_MODE. If the cipher is a stream cipher thenEVP_CIPH_STREAM_CIPHERis 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()andEVP_CIPHER_CTX_set_num()β gets or sets the cipher specific "num" parameter for the associatedctx. Built-in ciphers typically use this to track how much of the current underlying block has been "used" already. π’EVP_CIPHER_CTX_is_encrypting()β reports whether thectxis being used for encryption or decryption. πEVP_CIPHER_CTX_flags()β a deprecated macro calling"EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))". Do not use. πEVP_CIPHER_param_to_asn1()β sets the AlgorithmIdentifier "parameter" based on the passed cipher. This will typically include any parameters and an IV. The cipher IV (if any) must be set when this call is made. This call should be made before the cipher is actually "used" (before anyEVP_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 exampleEVP_CipherInit()will be called with the IV and key set toNULL,EVP_CIPHER_asn1_to_param()will be called and finallyEVP_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. TheEVP_CIPHERcan provide its own random key generation routine to support keys of a specific form.keymust point to a buffer at least as big as the value returned byEVP_CIPHER_CTX_get_key_length(). π²EVP_CIPHER_do_all_provided()β traverses all ciphers implemented by all activated providers in the given library contextlibctx, and for each of the implementations, calls the given functionfnwith the implementation method and the givenargas argument. π
π« PARAMETERS π«
See OSSL_PARAM(3) for information about passing parameters. π«Ά
π§ Gettable EVP_CIPHER parameters π§
When EVP_CIPHER_fetch() is called it internally calls EVP_CIPHER_get_params() and caches the results. π
EVP_CIPHER_get_params() can be used with the following OSSL_PARAM(3) keys: β¨
"mode"(OSSL_CIPHER_PARAM_MODE)<unsigned integer>β gets the mode for the associated cipher algorithm cipher. See "EVP_CIPHER_get_mode()andEVP_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 algorithm cipher. 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 algorithm cipher. 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 algorithm cipher. The block size should be 1 for stream ciphers. Note that the block size for a cipher may be different to the block size for the underlying encryption/decryption primitive. For example AES in CTR mode has a block size of 1 (because it operates like a stream cipher), even though AES has a block size of 16. UseEVP_CIPHER_get_block_size()to retrieve the cached value. π§±"aead"(OSSL_CIPHER_PARAM_AEAD)<integer>β Gets1if this is an AEAD cipher algorithm, otherwise it gets0. Use(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)to retrieve the cached value. cached and cute, bestie β¨"custom-iv"(OSSL_CIPHER_PARAM_CUSTOM_IV)<integer>β Gets1if the cipher algorithmcipherhas a custom IV, otherwise it gets0. Storing and initializing the IV is left entirely to the implementation, if a custom IV is used. Use(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_CUSTOM_IV)to retrieve the cached value. implementation said βI got thisβ π"cts"(OSSL_CIPHER_PARAM_CTS)<integer>β Gets1if the cipher algorithmcipheruses ciphertext stealing, otherwise it gets0. This is currently used to indicate that the cipher is a one shot that only allows a single call toEVP_CipherUpdate(). Use(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_CTS)to retrieve the cached value. one shot only, no cap π―"tls-multi"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK)<integer>β Gets1if the cipher algorithmciphersupports interleaving of crypto blocks, otherwise it gets0. The interleaving is an optimization only applicable to certain TLS ciphers. Use(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)to retrieve the cached value. optimization era π"has-randkey"(OSSL_CIPHER_PARAM_HAS_RANDKEY)<integer>β Gets1if the cipher algorithmciphersupports the gettableEVP_CIPHER_CTXparameterOSSL_CIPHER_PARAM_RANDOM_KEY. Only DES and 3DES set this to1, all other OpenSSL ciphers return0. DES and 3DES are the only members of this club π"decrypt-only"(OSSL_CIPHER_PARAM_DECRYPT_ONLY)<integerβ Gets1if the cipher algorithmcipherimplementation supports only the decryption operation such as the 3DES ciphers in the fips provider. Otherwise gets0or the parameter might not be present at all. sometimes itβs just not there, and thatβs valid π«Ά
β¨ Gettable and Settable EVP_CIPHER_CTX parameters β¨
The following OSSL_PARAM(3) keys can be used with both EVP_CIPHER_CTX_get_params() and EVP_CIPHER_CTX_set_params(). double-duty icons, literally π
"padding"(OSSL_CIPHER_PARAM_PADDING)<unsigned integer>β Gets or sets the padding mode for the cipher contextctx. Padding is enabled if the value is1, and disabled if the value is0. See alsoEVP_CIPHER_CTX_set_padding(). padding on or padding off, choose your fighter β¨"num"(OSSL_CIPHER_PARAM_NUM)<unsigned integer>β Gets or sets the cipher specific"num"parameter for the cipher contextctx. Built-in ciphers typically use this to track how much of the current underlying block has been"used"already. See alsoEVP_CIPHER_CTX_get_num()andEVP_CIPHER_CTX_set_num(). block usage tracker behavior π₯"keylen"(OSSL_CIPHER_PARAM_KEYLEN)<unsigned integer>β Gets or sets the key length for the cipher contextctx. 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(). respect thesize_tlimit, bestie π"tag"(OSSL_CIPHER_PARAM_AEAD_TAG)<octet string>β Gets or sets the AEAD tag for the associated cipher contextctx. See"AEAD INTERFACE"inEVP_EncryptInit(3). tag, youβre it π"pipeline-tag"(OSSL_CIPHER_PARAM_PIPELINE_AEAD_TAG)<octet ptr>β Gets or sets the AEAD tag when using cipher pipelining. The pointer must point to an array of buffers, where the aead tag will be read from or written to. The array size must be equal tonumpipesused inEVP_CipherPipelineEncryptInit()orEVP_CipherPipelineDecryptInit(). array size must matchnumpipes, no vibes-based guessing π"keybits"(OSSL_CIPHER_PARAM_RC2_KEYBITS)<unsigned integer>β Gets or sets the effective keybits used for a RC2 cipher. The length of the"keybits"parameter should not exceed that of asize_t. RC2 said βbits, not bytesβ β¨"rounds"(OSSL_CIPHER_PARAM_ROUNDS)<unsigned integer>β Gets or sets the number of rounds to be used for a cipher. This is used by the RC5 cipher. round and round and round π"algorithm-id"(OSSL_CIPHER_PARAM_ALGORITHM_ID)<octet string>β Used to get the DER encoded AlgorithmIdentifier from the cipher implementation. Functions likeEVP_PKEY_CTX_get_algor(3)use this parameter. DER encoded and doing the most π"algorithm-id-params"(OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS)<octet string>β Used to pass the DER encoded AlgorithmIdentifier parameter to or from the cipher implementation. Functions likeEVP_CIPHER_CTX_set_algor_params(3)andEVP_CIPHER_CTX_get_algor_params(3)use this parameter. pass the params, periodt π«Ά"alg_id_params"(OSSL_CIPHER_PARAM_ALGORITHM_ID_PARAMS_OLD)<octet string>β A deprecated alias for"algorithm-id-params", only used byEVP_CIPHER_param_to_asn1(3)andEVP_CIPHER_asn1_to_param(3). old alias, still showing up to the function π"cts_mode"(OSSL_CIPHER_PARAM_CTS_MODE)<UTF8 string>β Gets or sets the cipher text stealing mode. For all modes the output size is the same as the input size. The input length must be greater than or equal to the block size. (The block size for AES and CAMELLIA is16bytes).
Valid values for the mode are:
"CS1"β The NIST variant of cipher text stealing. For input lengths that are multiples of the block size it is equivalent to using a"AES-XXX-CBC"or"CAMELLIA-XXX-CBC"cipher otherwise the second last cipher text block is a partial block. NIST variant behavior πΈ"CS2"β For input lengths that are multiples of the block size it is equivalent to using a"AES-XXX-CBC"or"CAMELLIA-XXX-CBC"cipher, otherwise it is the same as"CS3"mode. sometimes CS2 says βsame as CS3β π"CS3"β The Kerberos5 variant of cipher text stealing which always swaps the last cipher text block with the previous block (which may be a partial or full block depending on the input length). If the input length is exactly one full block then this is equivalent to using a"AES-XXX-CBC"or"CAMELLIA-XXX-CBC"cipher. Kerberos5 variant said swap era π
The default is "CS1". This is only supported for "AES-128-CBC-CTS", "AES-192-CBC-CTS", "AES-256-CBC-CTS", "CAMELLIA-128-CBC-CTS", "CAMELLIA-192-CBC-CTS" and "CAMELLIA-256-CBC-CTS". only those six, iykyk π
"tls1multi_interleave"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE)<unsigned integer>β Sets or gets the number of records being sent in one go for a tls1 multiblock cipher operation (either4or8records). four or eight, no in-between π
π Gettable EVP_CIPHER_CTX parameters π
The following OSSL_PARAM(3) keys can be used with EVP_CIPHER_CTX_get_params(). get it, bestie β¨
"ivlen"(OSSL_CIPHER_PARAM_IVLENand<OSSL_CIPHER_PARAM_AEAD_IVLEN)<unsigned integer>β Gets the IV length for the cipher contextctx. The length of the"ivlen"parameter should not exceed that of asize_t. See alsoEVP_CIPHER_CTX_get_iv_length(). IV length check, no cap π"iv"(OSSL_CIPHER_PARAM_IV)<octet string OR octet ptr>β Gets the IV used to initialize the associated cipher contextctx. See alsoEVP_CIPHER_CTX_get_original_iv(). original IV behavior π"updated-iv"(OSSL_CIPHER_PARAM_UPDATED_IV)<octet string OR octet ptr>β Gets the updated pseudo-IV state for the associated cipher context, e.g., the previous ciphertext block for CBC mode or the iteratively encrypted IV value for OFB mode. Note that octet pointer access is deprecated and is provided only for backwards compatibility with historical libcrypto APIs. See alsoEVP_CIPHER_CTX_get_updated_iv(). deprecated pointer access, bestie β itβs giving legacy π"randkey"(OSSL_CIPHER_PARAM_RANDOM_KEY)<octet string>β Gets an implementation specific randomly generated key for the associated cipher contextctx. This is currently only supported by DES and 3DES (which set the key to odd parity). random key with odd parity energy β¨"taglen"(OSSL_CIPHER_PARAM_AEAD_TAGLEN)<unsigned integer>β Gets the tag length to be used for an AEAD cipher for the associated cipher contextctx. It gets a default value if it has not been set. The length of the"taglen"parameter should not exceed that of asize_t. See alsoEVP_CIPHER_CTX_get_tag_length(). default tag length said βif not set, I got youβ π«Ά"tlsaadpad"(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD)<unsigned integer>β Gets the length of the tag that will be added to a TLS record for the AEAD tag for the associated cipher contextctx. The length of the"tlsaadpad"parameter should not exceed that of asize_t. pad length, TLS record edition π"tlsivgen"(OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN)<octet string>β Gets the invocation field generated for encryption. Can only be called after"tlsivfixed"is set. This is only used for GCM mode. only after"tlsivfixed", and only GCM π"tls1multi_enclen"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN)<unsigned integer>β Get the total length of the record returned from the"tls1multi_enc"operation. total record length behavior π"tls1multi_maxbufsz"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE)<unsigned integer>β Gets the maximum record length for a TLS1 multiblock cipher operation. The length of the"tls1multi_maxbufsz"parameter should not exceed that of asize_t. max buffer size, but make itsize_t-safe β¨"tls1multi_aadpacklen"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN)<unsigned integer>β Gets the result of running the"tls1multi_aad"operation. result of"tls1multi_aad"β cute π"tls-mac"(OSSL_CIPHER_PARAM_TLS_MAC)<octet ptr>β Used to pass the TLS MAC data. MAC data pass-through icon π"fips-indicator"(OSSL_CIPHER_PARAM_FIPS_APPROVED_INDICATOR)<integer>β This option is used by the OpenSSL FIPS provider.
A getter that returns 1 if the operation is FIPS approved, or 0 otherwise. This may be used after calling a cipher final operation such as EVP_EncryptFinal_ex(). It may return 0 if the "encrypt-check" option is set to 0. FIPS approved indicator, literally π
"iv-generated"(OSSL_CIPHER_PARAM_AEAD_IV_GENERATED)<unsigned integer>β An indicator that returns1if an IV was generated internally during encryption, orOotherwise. This may be used by GCM ciphers after calling a cipher final operation such asEVP_EncryptFinal_ex(). GCM should generate an IV internally if the IV is not specified during a cipher initialisation call such asEVP_CipherInit_ex(). See FIPS 140-3 IG C.H for information related to IV requirements. IV generated indicator said βinternally, bestieβ π«Ά
πΈ Settable EVP_CIPHER_CTX parameters πΈ
The following OSSL_PARAM(3) keys can be used with EVP_CIPHER_CTX_set_params(). set it and forget it? no, set it and slay β¨
"mackey"(OSSL_CIPHER_PARAM_AEAD_MAC_KEY)<octet string>β Sets the MAC key used by composite AEAD ciphers such as AES-CBC-HMAC-SHA256. composite AEAD said βneed my MAC keyβ π"speed"(OSSL_CIPHER_PARAM_SPEED)<unsigned integer>β Sets the speed option for the associated cipher context. This is only supported by AES SIV ciphers which disallow multiple operations by default. Setting"speed"to1allows another encrypt or decrypt operation to be performed. This is used for performance testing. performance testing behavior, periodt π₯"use-bits"(OSSL_CIPHER_PARAM_USE_BITS)<unsigned integer>β Determines if the input lengthinlpassed toEVP_EncryptUpdate(),EVP_DecryptUpdate()andEVP_CipherUpdate()is the number of bits or number of bytes. Setting"use-bits"to1uses bits. The default is in bytes. This is only used for CFB1 ciphers.
This can be set using EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS). bits or bytes? CFB1 said βyesβ π
"tls-version"(OSSL_CIPHER_PARAM_TLS_VERSION)<integer>β Sets the TLS version. TLS version, set and done π"tls-mac-size"(OSSL_CIPHER_PARAM_TLS_MAC_SIZE) <unsigned integer> β Sets the TLS MAC size. That's it, that's the whole vibe β¨"tlsaad"(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD) <octet string> β Sets the TLSv1.2 AAD info for the cipher contextctxit's attached to. The TLSv1.2 AAD info is always 13 bytes long, no notes, and it's exactly what the"additional_data"field in section 6.2.3.3 of RFC5246 describes. iykyk π"tlsivfixed"(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED) <octet string> β Sets the fixed portion of an IV for an AEAD cipher that's doing TLS record encryption/decryption for the cipher context it belongs to. Big facts coming, bestie, stay with me:
TLS record encryption/decryption is always done "in place", so the input and output buffers are literally the same memory location. AEAD IVs in TLSv1.2 are made of an implicit "fixed" part plus an explicit part that changes with every single record. Setting a TLS fixed IV is the thing that flips a cipher into TLS-record-encrypting/decrypting mode. TLS records get encrypted/decrypted with one single OSSL_FUNC_cipher_cipher call per record.
For a record decryption, the first bytes of the input buffer are the explicit part of the IV and the final bytes of the input buffer are the AEAD tag. How long the explicit IV part is and how long the tag is depends on the cipher you're using and is defined in the RFC for that relevant ciphersuite. To make "in place" decryption work, the plaintext output should be written to the same spot in the output buffer that the ciphertext payload was read from β like, immediately after the explicit IV. π«Ά
When encrypting a record, the first bytes of the input buffer should be left empty so there's room for the explicit IV, and same for the final bytes where the tag goes. The input buffer length includes the explicit IV length, the payload, and the tag bytes. The cipher implementation is supposed to generate the explicit IV and write it to the start of the output buffer, do "in place" encryption of the payload and write that into the output buffer, and finally slap the tag onto the end of the output buffer. Iconic behaviour tbh π
Whether you're encrypting or decrypting, the value written to *outl in the OSSL_FUNC_cipher_cipher call should be the length of the payload, excluding the explicit IV length and the tag length.
"tlsivinv"(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV) <octet string> β Sets the invocation field used for decryption. Can only be called after"tlsivfixed"is set. Also this one is only used for GCM mode, no cap."tls1multi_enc"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC) <octet string> β Triggers a multiblock TLS1 encrypt operation for a TLS1-aware cipher that supports sending 4 or 8 records in one go π₯ The cipher does both the MAC and encrypt stages and constructs the record headers itself."tls1multi_enc"supplies the output buffer for the encrypt operation, and"tls1multi_encin"&"tls1multi_interleave"must also be set so there are values to feed the encrypt operation."tls1multi_encin"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN) <octet string> β Supplies the data to encrypt for a TLS1 multiblock cipher operation."tls1multi_maxsndfrag"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT) <unsigned integer> β Sets the maximum send fragment size for a TLS1 multiblock cipher operation. It has to be set before you use"tls1multi_maxbufsz". Also the length of the"tls1multi_maxsndfrag"parameter should not exceed that of asize_t. Respect the limits bestie π§"tls1multi_aad"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD) <octet string> β Sets the authenticated additional data used by a TLS1 multiblock cipher operation. The data you supply is 13 bytes of record data, laid out like this:- Bytes 0-7: the sequence number of the first record
- Byte 8: the record type
- Byte 9-10: the protocol version
- Byte 11-12: the input length (always
0)
"tls1multi_interleave" must also be set for this operation to work. β¨
"xts_standard"(OSSL_CIPHER_PARAM_XTS_STANDARD) <UTF8 string> β Sets which XTS standard to use with the SM4-XTS algorithm. Okay so XTS mode has two implementations: one is standardised in IEEE Std. 1619-2007 and has been used everywhere forever (e.g., XTS AES), and the other one is more recent (GB/T 17964-2021, implemented in May 2022) and is currently only used in SM4. π¦
The main difference between them is the multiplication by the primitive element Ξ± used to calculate the tweak values. IEEE Std 1619-2007 says the multiplication "is a left shift of each byte by one bit with carry propagating from one byte to the next one" β meaning that in each byte, the leftmost bit is the most significant bit. But in GB/T 17964-2021, the rightmost bit is the most significant bit, so the multiplication becomes a right shift of each byte by one bit with carry propagating from one byte to the next one. Same energy, opposite direction, lowkey fascinating π
Valid values for the mode are:
"GB"β the GB/T 17964-2021 variant of the SM4-XTS algorithm."IEEE"β the IEEE Std. 1619-2007 variant of the SM4-XTS algorithm.
The default value is "GB".
"encrypt-check"(OSSL_CIPHER_PARAM_FIPS_ENCRYPT_CHECK) <integer> β This option is used by the OpenSSL FIPS provider π
If it's required, this parameter should be set early via a cipher encrypt init function such as EVP_EncryptInit_ex2(). Its default value of 1 causes an error when an encryption operation is triggered. Setting it to 0 will ignore that error and set the approved "fips-indicator" to 0.
β οΈ Fr fr warning: this option breaks FIPS compliance if it causes the approved"fips-indicator"to return0. Like, actually breaks it. Don't come for me, I'm just the messenger π
ποΈ CONTROLS ποΈ
The mappings from EVP_CIPHER_CTX_ctrl() identifiers to PARAMETERS are listed in this next bit. Peep the "PARAMETERS" section above for more details. π
EVP_CIPHER_CTX_ctrl() can be used to send these standard controls:
EVP_CTRL_AEAD_SET_IVLENandEVP_CTRL_GET_IVLENβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()andEVP_CIPHER_CTX_get_params()get called with anOSSL_PARAM(3)item with the key"ivlen"(OSSL_CIPHER_PARAM_IVLEN).EVP_CTRL_AEAD_SET_IV_FIXEDβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item with the key"tlsivfixed"(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED).EVP_CTRL_AEAD_SET_MAC_KEYβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item with the key"mackey"(OSSL_CIPHER_PARAM_AEAD_MAC_KEY).EVP_CTRL_AEAD_SET_TAGandEVP_CTRL_AEAD_GET_TAGβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()andEVP_CIPHER_CTX_get_params()get called with anOSSL_PARAM(3)item with the key"tag"(OSSL_CIPHER_PARAM_AEAD_TAG).EVP_CTRL_CCM_SET_Lβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item with the key"ivlen"(OSSL_CIPHER_PARAM_IVLEN) with a value of(15 - L).EVP_CTRL_COPYβ There is noOSSL_PARAMmapping for this one. UseEVP_CIPHER_CTX_copy()instead. Periodt. πEVP_CTRL_GCM_SET_IV_INVβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item with the key"tlsivinv"(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV).EVP_CTRL_RAND_KEYβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item with the key"randkey"(OSSL_CIPHER_PARAM_RANDOM_KEY).EVP_CTRL_SET_KEY_LENGTHβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item with the key"keylen"(OSSL_CIPHER_PARAM_KEYLEN).EVP_CTRL_SET_RC2_KEY_BITSandEVP_CTRL_GET_RC2_KEY_BITSβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()andEVP_CIPHER_CTX_get_params()get called with anOSSL_PARAM(3)item with the key"keybits"(OSSL_CIPHER_PARAM_RC2_KEYBITS).EVP_CTRL_SET_RC5_ROUNDSandEVP_CTRL_GET_RC5_ROUNDSβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()andEVP_CIPHER_CTX_get_params()get called with anOSSL_PARAM(3)item with the key"rounds"(OSSL_CIPHER_PARAM_ROUNDS).EVP_CTRL_SET_SPEEDβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item with the key"speed"(OSSL_CIPHER_PARAM_SPEED). Vroom vroom ποΈEVP_CTRL_GCM_IV_GENβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_get_params()gets called with anOSSL_PARAM(3)item with the key"tlsivgen"(OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN).EVP_CTRL_AEAD_TLS1_AADβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item with the key"tlsaad"(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD), followed byEVP_CIPHER_CTX_get_params()with a key of"tlsaadpad"(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD). Two steps, one control, we love a combo β¨EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZEβ When used with a fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called with anOSSL_PARAM(3)item with the keyOSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, followed byEVP_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 fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called withOSSL_PARAM(3)items with the keys"tls1multi_aad"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD) and"tls1multi_interleave"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE), followed byEVP_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 fetchedEVP_CIPHER,EVP_CIPHER_CTX_set_params()gets called withOSSL_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 byEVP_CIPHER_CTX_get_params()with a key of"tls1multi_enclen"(OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN).
π that's the whole controls roster, bestie β every single one accounted for π
β¨ FLAGS β¨
Okay so bestie, EVP_CIPHER_CTX_set_flags(), EVP_CIPHER_CTX_clear_flags() and EVP_CIPHER_CTX_test_flags() are your little toolkit for poking at these EVP_CIPHER_CTX flags β set 'em, clear 'em, test 'em, whatever π
EVP_CIPH_NO_PADDINGβ this one's used byEVP_CIPHER_CTX_set_padding(). Also go peek at "Gettable and Settable EVP_CIPHER_CTX parameters" under "padding" πEVP_CIPH_FLAG_LENGTH_BITSβ check "Settable EVP_CIPHER_CTX parameters" under "use-bits".EVP_CIPHER_CTX_FLAG_WRAP_ALLOWβ legacy purposes only, fr. Back in the day you had to set this flag to say "yeah this cipher handles wrapping".
Then EVP_CIPHER_flags() rocks the following flags, and these babies have mappings over to "Gettable EVP_CIPHER parameters":
EVP_CIPH_FLAG_AEAD_CIPHERβ see "Gettable EVP_CIPHER parameters" under "aead".EVP_CIPH_CUSTOM_IVβ see "Gettable EVP_CIPHER parameters" under "custom-iv".EVP_CIPH_FLAG_CTSβ see "Gettable EVP_CIPHER parameters" under "cts".EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK;β see "Gettable EVP_CIPHER parameters" under "tls-multi".EVP_CIPH_RAND_KEYβ see "Gettable EVP_CIPHER parameters" under "has-randkey".
And EVP_CIPHER_flags() also uses these ones β legacy purposes only, bestie, no cap:
EVP_CIPH_VARIABLE_LENGTHEVP_CIPH_FLAG_CUSTOM_CIPHEREVP_CIPH_ALWAYS_CALL_INITEVP_CIPH_CTRL_INITEVP_CIPH_CUSTOM_KEY_LENGTHEVP_CIPH_CUSTOM_COPYEVP_CIPH_FLAG_DEFAULT_ASN1
For the whole tea on all of the above flags, see EVP_CIPHER_meth_set_flags(3) π«Ά
π RETURN VALUES π
This part is giving "receipts" energy, so let's go down the list, one by one:
EVP_CIPHER_fetch()β returns a pointer to anEVP_CIPHERwhen it slays, andNULLwhen it flops.EVP_CIPHER_up_ref()β1for success,0otherwise.EVP_CIPHER_CTX_new()β returns a pointer to a freshly createdEVP_CIPHER_CTXon success,NULLon failure.EVP_CIPHER_CTX_dup()β returns a newEVP_CIPHER_CTXif it worked, orNULLon failure.EVP_CIPHER_CTX_copy()β1if successful,0for failure.EVP_EncryptInit_ex2(),EVP_EncryptUpdate()andEVP_EncryptFinal_ex()β1for success and0for failure.EVP_DecryptInit_ex2()andEVP_DecryptUpdate()β1for success,0for failure. But like, watch out:EVP_DecryptFinal_ex()returns0if the decrypt failed or1for success πEVP_CipherInit_ex2(),EVP_CipherInit_SKEY()andEVP_CipherUpdate()β1for 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. And ifEVP_CIPH_FLAG_CUSTOM_CIPHERis set for the cipher, thenEVP_Cipher()returns the number of bytes written to out for encryption/decryption, or the number of bytes authenticated in a call specifying AAD for an AEAD cipher. Iconic dual behaviour, honestly.EVP_CIPHER_can_pipeline()β1if the cipher can be used in a pipeline,0otherwise.EVP_CipherPipelineEncryptInit()andEVP_CipherPipelineDecryptInit()β1for success and0for failure.EVP_CipherPipelineUpdate()andEVP_CipherPipelineFinal()β1for success and0for failure.EVP_CIPHER_CTX_reset()β1for 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. Always. It's that girl.EVP_CIPHER_get_iv_length()andEVP_CIPHER_CTX_get_iv_length()β return the IV length, zero if the cipher doesn't use an IV, and a negative value on error.EVP_CIPHER_CTX_get_tag_length()β returns the tag length, or zero if the cipher doesn't use a tag.EVP_CIPHER_get_type()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()β1on success and0if the implementation doesn't support the call or on any other error.EVP_CIPHER_CTX_is_encrypting()β1if 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()β1for success and zero or a negative number for failure.EVP_CIPHER_names_do_all()β1if the callback was called for all names. A return value of0means the callback wasn't called for any names. Kinda harsh but okay πEVP_CIPHER_get_params(),EVP_CIPHER_CTX_get_params()andEVP_CIPHER_CTX_set_params()β1for success and0for failure.
πΈ CIPHER LISTING πΈ
Heads up bestie: all algorithms have a fixed key length unless otherwise stated. That's the default vibe.
And for the full list of ciphers you can get through the EVP interface, go look at "SEE ALSO" β it's all there, iykyk β¨
EVP_enc_null()β Null cipher: literally does nothing. Not a single thing. It's giving "main character who took a nap" π΄π
β¨ AEAD INTERFACE β¨
okay bestie, so the EVP interface for Authenticated Encryption with Associated Data (AEAD) modes is subtly altered, and depending on the mode you pick, there are several extra ctrl operations supported. slay, but pay attention π
To specify additional authenticated data (AAD), you make a call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with the output parameter out set to NULL. When that works, the parameter outl is set to the number of AAD bytes processed in that call (that is, the value of inl), and it does not include any plaintext or ciphertext bytes processed by other calls. periodt.
If no AAD is used, this call can be omitted. See the mode-specific notes below for any exceptions π
When decrypting, the return value of EVP_DecryptFinal() or EVP_CipherFinal() indicates whether the operation was successful. If it does not indicate success, the authentication operation has failed and any output data MUST NOT be used as it is corrupted. like, do not touch it, bestie π
Please note that the number of authenticated bytes returned by EVP_CipherUpdate() depends on the cipher used. Stream ciphers, such as ChaCha20 or ciphers in GCM mode, can handle 1 byte at a time, resulting in an effective βblockβ size of 1. Conversely, ciphers in OCB mode must process data one block at a time, and the block size is returned. different vibes, same API π
Regardless of the returned size, it is safe to pass unpadded data to an EVP_CipherUpdate() call in a single operation. no cap.
π GCM and OCB Modes π
The following ctrls are supported in GCM and OCB modes.
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)β sets the IV length. This call can only be made before specifying an IV. If you donβt call it, a default IV length is used. For GCM AES and OCB AES the default is12(i.e.96bits). For OCB mode the maximum is15.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 anEVP_EncryptFinal()call). For OCB, taglen must either be16or the value previously set viaEVP_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 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 toNULLsets 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 is16(i.e.128bits). It is also the maximum tag length for OCB.
π§ CCM Mode π§
The EVP interface for CCM mode is similar to that of the GCM mode but with a few additional requirements and different ctrl values. okay so this one has β¨rulesβ¨
For CCM mode, the total plaintext or ciphertext length MUST be passed to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() with the output and input parameters (in and out) set to NULL and the length passed in the inl parameter. like, the whole length, bestie, donβt skip it π
The following ctrls are supported in CCM mode.
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen, tag)β this call is made to set the expected CCM tag value when decrypting or the length of the tag (with the tag parameter set toNULL) when encrypting. The tag length is often referred to asM. 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 CCMLvalue. 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. iconic, honestly π
For SIV mode ciphers the behaviour of the EVP interface is subtly altered and several additional ctrl operations are supported.
To specify any additional authenticated data (AAD) and/or a Nonce, a call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with the output parameter out set to NULL.
RFC5297 states that the Nonce is the last piece of AAD before the actual encrypt/decrypt takes place. The API does not differentiate the Nonce from other AAD. iykyk πΈ
When decrypting the return value of EVP_DecryptFinal() or EVP_CipherFinal() indicates if the operation was successful. If it does not indicate success the authentication operation has failed and any output data MUST NOT be used as it is corrupted.
The API does not store the SIV (Synthetic Initialization Vector) in the cipher text. Instead, it is stored as the tag within the EVP_CIPHER_CTX. The SIV must be retrieved from the context after encryption, and set into the context before decryption. main character energy, stored separately π
This differs from RFC5297 in that the cipher output from encryption, and the cipher input to decryption, does not contain the SIV. This also means that the plain text and cipher text lengths are identical. cute and equal β¨
The following ctrls are supported in SIV mode, and are used to get and set the Synthetic Initialization Vector:
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag);β writes taglen bytes of the tag value (the Synthetic Initialization Vector) to the buffer indicated by tag. This call can only be made when encrypting data and after all data has been processed (e.g. after anEVP_EncryptFinal()call). For SIV mode the taglen must be16.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 anyEVP_DecryptUpdate()calls). For SIV mode the taglen must be16.
SIV mode makes two passes over the input data, thus, only one call to EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should be made with out set to a non-NULL value. A call to EVP_DecryptFinal() or EVP_CipherFinal() is not required, but will indicate if the update operation succeeded. two passes, one update, no chaos π
π ChaCha20-Poly1305 π
The following ctrls are supported for the ChaCha20-Poly1305 AEAD algorithm.
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)β sets the nonce length. This call is now redundant since the only valid value is the default length of12(i.e.96bits). Prior to OpenSSL3.0a 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)β 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 anEVP_EncryptFinal()call). taglen specified here must be16(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 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. This is because the code then becomes transparent to the cipher used and much more flexible. Additionally, the EVP interface will ensure the use of platform specific cryptographic acceleration such as AES-NI (the low-level interfaces do not provide the guarantee). literally let EVP handle it, bestie π
PKCS padding works by adding n padding bytes of value n to make the total length of the encrypted data a multiple of the block size. Padding is always added so if the data is already a multiple of the block size n will equal the block size. For example if the block size is 8 and 11 bytes are to be encrypted then 5 padding bytes of value 5 will be added. math but make it cute π
When decrypting the final block is checked to see if it has the correct form.
Although the decryption operation can produce an error if padding is enabled, it is not a strong test that the input data or key is correct. A random block has better than 1 in 256 chance of being of the correct format and problems with the input data earlier on will not produce a final decrypt error. so like, donβt treat padding errors as a whole security vibe check π
If padding is disabled then the decryption operation will always succeed if the total amount of data decrypted is a multiple of the block size.
The functions EVP_EncryptInit(), EVP_EncryptInit_ex(), EVP_EncryptFinal(), EVP_DecryptInit(), EVP_DecryptInit_ex(), EVP_CipherInit(), EVP_CipherInit_ex() and EVP_CipherFinal() are obsolete but are retained for compatibility with existing code. New code should use EVP_EncryptInit_ex2(), EVP_EncryptFinal_ex(), EVP_DecryptInit_ex2(), EVP_DecryptFinal_ex(), EVP_CipherInit_ex2() and EVP_CipherFinal_ex() because they can reuse an existing context without allocating and freeing it up on each call. upgrade your code, itβs giving efficiency π
There are some differences between functions EVP_CipherInit() and EVP_CipherInit_ex(), significant in some circumstances. EVP_CipherInit() fills the passed context object with zeros. As a consequence, EVP_CipherInit() does not allow step-by-step initialization of the ctx when the key and iv are passed in separate calls. It also means that the flags set for the CTX are removed, and it is especially important for the EVP_CIPHER_CTX_FLAG_WRAP_ALLOW flag treated specially in EVP_CipherInit_ex(). okay so this one is a whole personality difference π
Ignoring failure returns of the EVP_CIPHER_CTX initialization functions can lead to subsequent undefined behavior when calling the functions that update or finalize the context. The only valid calls on the EVP_CIPHER_CTX when initialization fails are calls that attempt another initialization of the context or release the context. fr fr, check your return values π«Ά
EVP_get_cipherbynid(), and EVP_get_cipherbyobj() are implemented as macros.
π BUGS π
okay bestie, listen up, because there's some stuff that will absolutely bite you if you're not paying attention:
EVP_MAX_KEY_LENGTHandEVP_MAX_IV_LENGTHonly refer to the internal ciphers with default key lengths. That's it. That's the whole scope. If your custom ciphers go over those values, the results are unpredictable β like, genuinely no idea what happens, and neither does anyone else π. Why? Because it's become standard practice to define a generic key as a fixedunsigned chararray containingEVP_MAX_KEY_LENGTHbytes. That's just the vibe now, we don't make the rules.- The ASN1 code is incomplete (and sometimes inaccurate β π rude but true). It's only been tested for certain common S/MIME ciphers (
RC2,DES, tripleDES) in CBC mode. Anything outside that little bubble? Untested territory, bestie. Enter at your own risk.
β¨ EXAMPLES β¨
π§ Encrypt a string using IDEA
okay so this first one is a whole little moment β encrypting a string with IDEA. Look at her go:
int do_crypt(char *outfile)
{
unsigned char outbuf[1024];
int outlen, tmplen;
/*
* Bogus key and IV: we'd normally set these from
* another source.
*/
unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
unsigned char iv[] = {1,2,3,4,5,6,7,8};
char intext[] = "Some Crypto Text";
EVP_CIPHER_CTX *ctx;
FILE *out;
ctx = EVP_CIPHER_CTX_new();
if (!EVP_EncryptInit_ex2(ctx, EVP_idea_cbc(), key, iv, NULL)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
if (!EVP_EncryptUpdate(ctx, outbuf, &outlen, intext, strlen(intext))) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
/*
* Buffer passed to EVP_EncryptFinal() must be after data just
* encrypted to avoid overwriting it.
*/
if (!EVP_EncryptFinal_ex(ctx, outbuf + outlen, &tmplen)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
outlen += tmplen;
EVP_CIPHER_CTX_free(ctx);
/*
* Need binary mode for fopen because encrypted data is
* binary data. Also cannot use strlen() on it because
* it won't be NUL terminated and may contain embedded
* NULs.
*/
out = fopen(outfile, "wb");
if (out == NULL) {
/* Error */
return 0;
}
fwrite(outbuf, 1, outlen, out);
fclose(out);
return 1;
}
π«Ά Note the little comment block about the buffer passed to EVP_EncryptFinal() needing to go after the data you just encrypted β that's not decoration, that's literally to avoid overwriting it. Main character attention to detail, honestly.
And the ciphertext from that example? You can decrypt it with the openssl utility using this command line (shown on two lines for clarity β she's long, okay?):
openssl idea -d \
-K 000102030405060708090A0B0C0D0E0F -iv 0102030405060708 <filename
π¦ General encryption and decryption function example
Now for the general one β using FILE I/O and AES128 with a 128-bit key. Both encrypt and decrypt, because she contains multitudes:
int do_crypt(FILE *in, FILE *out, int do_encrypt)
{
/* Allow enough space in output buffer for additional block */
unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];
int inlen, outlen;
EVP_CIPHER_CTX *ctx;
/*
* Bogus key and IV: we'd normally set these from
* another source.
*/
unsigned char key[] = "0123456789abcdeF";
unsigned char iv[] = "1234567887654321";
/* Don't set key or IV right away; we want to check lengths */
ctx = EVP_CIPHER_CTX_new();
if (!EVP_CipherInit_ex2(ctx, EVP_aes_128_cbc(), NULL, NULL,
do_encrypt, NULL)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
OPENSSL_assert(EVP_CIPHER_CTX_get_key_length(ctx) == 16);
OPENSSL_assert(EVP_CIPHER_CTX_get_iv_length(ctx) == 16);
/* Now we can set key and IV */
if (!EVP_CipherInit_ex2(ctx, NULL, key, iv, do_encrypt, NULL)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
for (;;) {
inlen = fread(inbuf, 1, 1024, in);
if (inlen <= 0)
break;
if (!EVP_CipherUpdate(ctx, outbuf, &outlen, inbuf, inlen)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
fwrite(outbuf, 1, outlen, out);
}
if (!EVP_CipherFinal_ex(ctx, outbuf, &outlen)) {
/* Error */
EVP_CIPHER_CTX_free(ctx);
return 0;
}
fwrite(outbuf, 1, outlen, out);
EVP_CIPHER_CTX_free(ctx);
return 1;
}
πΈ See how she doesn't set the key or IV right away? That's on purpose β she wants to check lengths first withOPENSSL_assert(EVP_CIPHER_CTX_get_key_length(ctx) == 16);andOPENSSL_assert(EVP_CIPHER_CTX_get_iv_length(ctx) == 16);. Only then does she commit. Queen behavior.
π₯ Encryption using AES-CBC with a 256-bit key with "CS1" ciphertext stealing
Last example icon β this one's doing ciphertext stealing, which is exactly as cool as it sounds:
int encrypt(const unsigned char *key, const unsigned char *iv,
const unsigned char *msg, size_t msg_len, unsigned char *out)
{
/*
* This assumes that key size is 32 bytes and the iv is 16 bytes.
* For ciphertext stealing mode the length of the ciphertext "out" will be
* the same size as the plaintext size "msg_len".
* The "msg_len" can be any size >= 16.
*/
int ret = 0, encrypt = 1, outlen, len;
EVP_CIPHER_CTX *ctx = NULL;
EVP_CIPHER *cipher = NULL;
OSSL_PARAM params[2];
ctx = EVP_CIPHER_CTX_new();
cipher = EVP_CIPHER_fetch(NULL, "AES-256-CBC-CTS", NULL);
if (ctx == NULL || cipher == NULL)
goto err;
/*
* The default is "CS1" so this is not really needed,
* but would be needed to set either "CS2" or "CS3".
*/
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_CIPHER_PARAM_CTS_MODE,
"CS1", 0);
params[1] = OSSL_PARAM_construct_end();
if (!EVP_CipherInit_ex2(ctx, cipher, key, iv, encrypt, params))
goto err;
/* NOTE: CTS mode does not support multiple calls to EVP_CipherUpdate() */
if (!EVP_CipherUpdate(ctx, out, &outlen, msg, msg_len))
goto err;
if (!EVP_CipherFinal_ex(ctx, out + outlen, &len))
goto err;
ret = 1;
err:
EVP_CIPHER_free(cipher);
EVP_CIPHER_CTX_free(ctx);
return ret;
}
β οΈ real talk: CTS mode does not support multiple calls toEVP_CipherUpdate()β there's literally a/* NOTE: */about it in the code. So don't do that. One call, bestie. Periodt.
π SEE ALSO π
go read these next, iykyk:
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 π
storytime with me, bestie β here's the lore π :
- Support for OCB mode 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. AndEVP_CIPHER_CTX_init()remains as an alias forEVP_CIPHER_CTX_reset(). She's still here, just under a different name π«Ά.- The
EVP_CIPHER_CTX_cipher()function was deprecated in OpenSSL 3.0; useEVP_CIPHER_CTX_get0_cipher()instead. (respectfully, move on.) - These functions were added in 3.0:
EVP_EncryptInit_ex2()EVP_DecryptInit_ex2()EVP_CipherInit_ex2()EVP_CIPHER_fetch()EVP_CIPHER_free()EVP_CIPHER_up_ref()EVP_CIPHER_CTX_get0_cipher()EVP_CIPHER_CTX_get1_cipher()EVP_CIPHER_get_params()EVP_CIPHER_CTX_set_params()EVP_CIPHER_CTX_get_params()EVP_CIPHER_gettable_params()EVP_CIPHER_settable_ctx_params()EVP_CIPHER_gettable_ctx_params()EVP_CIPHER_CTX_settable_params()EVP_CIPHER_CTX_gettable_params()
- These functions were renamed to include
"get"or"get0"in their names in OpenSSL 3.0, respectively. The old names are kept as non-deprecated alias macros (so nothing breaks, bless):EVP_CIPHER_nid()EVP_CIPHER_name()EVP_CIPHER_block_size()EVP_CIPHER_key_length()EVP_CIPHER_iv_length()EVP_CIPHER_flags()EVP_CIPHER_mode()EVP_CIPHER_type()EVP_CIPHER_CTX_nid()EVP_CIPHER_CTX_block_size()EVP_CIPHER_CTX_key_length()EVP_CIPHER_CTX_iv_length()EVP_CIPHER_CTX_tag_length()EVP_CIPHER_CTX_num()EVP_CIPHER_CTX_type()EVP_CIPHER_CTX_mode()
- The
EVP_CIPHER_CTX_encrypting()function was renamed toEVP_CIPHER_CTX_is_encrypting()in OpenSSL 3.0. The old name is kept as a non-deprecated alias macro. (she goes by both, it's fine.) - The
EVP_CIPHER_CTX_flags()macro was deprecated in OpenSSL 1.1.0. EVP_CIPHER_CTX_dup()was added in OpenSSL 3.2.EVP_CipherInit_SKEY()was added in OpenSSL 3.5.- Prior to OpenSSL 3.5, passing a
NULLctxtoEVP_CIPHER_CTX_get_block_size()would result in aNULLpointer dereference, rather than a0return value indicating an error. π So yeah β that was a thing that happened. Glad it's fixed, fr fr.
π 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 the whole page, bestie! this edition is OpenSSL 3.5.6, dated 2026-04-07, and it's filed under EVP_ENCRYPTINIT(3ossl). go forth and encrypt responsibly, ily, bye! ππ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms