EVP_AEAD_CTX_INIT(3)
π EVP_AEAD_CTX_INIT(3) π
okay so this is the EVP_AEAD_CTX_INIT(3) page, from the Library Functions Manual, and it's all about authenticated encryption with additional data β the libcrypto flavor, bestie. buckle up π
β¨ NAME β¨
EVP_AEAD_CTX_new, EVP_AEAD_CTX_free, EVP_AEAD_CTX_init, EVP_AEAD_CTX_cleanup, EVP_AEAD_CTX_open, EVP_AEAD_CTX_seal, EVP_AEAD_key_length, EVP_AEAD_max_overhead, EVP_AEAD_max_tag_len, EVP_AEAD_nonce_length, EVP_aead_aes_128_gcm, EVP_aead_aes_256_gcm, EVP_aead_chacha20_poly1305, EVP_aead_xchacha20_poly1305 β authenticated encryption with additional data πΈ
like, that's the whole squad fr fr π
π¦ SYNOPSIS π¦
library βlibcryptoβ
#include <openssl/evp.h>
EVP_AEAD_CTX *
EVP_AEAD_CTX_new(void);
void
EVP_AEAD_CTX_free(EVP_AEAD_CTX *ctx);
int
EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
const unsigned char *key, size_t key_len, size_t tag_len,
ENGINE *engine);
void
EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx);
int
EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, unsigned char *out,
size_t *out_len, size_t max_out_len, const unsigned char *nonce,
size_t nonce_len, const unsigned char *in, size_t in_len,
const unsigned char *ad, size_t ad_len);
int
EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, unsigned char *out,
size_t *out_len, size_t max_out_len, const unsigned char *nonce,
size_t nonce_len, const unsigned char *in, size_t in_len,
const unsigned char *ad, size_t ad_len);
size_t
EVP_AEAD_key_length(const EVP_AEAD *aead);
size_t
EVP_AEAD_max_overhead(const EVP_AEAD *aead);
size_t
EVP_AEAD_max_tag_len(const EVP_AEAD *aead);
size_t
EVP_AEAD_nonce_length(const EVP_AEAD *aead);
const EVP_AEAD *
EVP_aead_aes_128_gcm(void);
const EVP_AEAD *
EVP_aead_aes_256_gcm(void);
const EVP_AEAD *
EVP_aead_chacha20_poly1305(void);
const EVP_AEAD *
EVP_aead_xchacha20_poly1305(void);
π DESCRIPTION π
so AEAD stands for Authenticated Encryption with Additional Data, and the whole vibe is that it couples confidentiality and integrity into a single primitive. iconic behavior honestly π
AEAD algorithms take a key, and then they can seal and open individual messages. every single message gets its own unique per-message nonce, and optionally some additional data which gets authenticated but is not included in the output. iykyk π₯
EVP_AEAD_CTX_new()β allocates a shiny new context for use withEVP_AEAD_CTX_init(). it can be cleaned up for reuse withEVP_AEAD_CTX_cleanup()and must be freed withEVP_AEAD_CTX_free(). don't leave it hanging bestie πEVP_AEAD_CTX_free()β cleans upctxand frees the space allocated to it. the respectful goodbye π«ΆEVP_AEAD_CTX_init()β initializes the contextctxfor the given AEAD algorithmaead. theengineargument must beNULLfor the default implementation; other values are not supported (so like, don't try it π). authentication tags may be truncated by passing a tag length. atag_lenargument ofEVP_AEAD_DEFAULT_TAG_LENGTH, which has the value0, causes the default tag length to be used.EVP_AEAD_CTX_cleanup()β frees any data allocated for the contextctx. afterEVP_AEAD_CTX_cleanup(),ctxis in the same state as afterEVP_AEAD_CTX_new(). it's giving factory reset β¨EVP_AEAD_CTX_open()β authenticates the inputinand optional additional dataad, decrypting the input and writing it as outputout. this function may be called (with the sameEVP_AEAD_CTX) concurrently with itself or withEVP_AEAD_CTX_seal(). at most the number of input bytes are written as output. in order to ensure success,max_out_lenshould be at least the same as the input lengthin_len. on successful returnout_lenis set to the actual number of bytes written. the length of the nonce specified withnonce_lenmust be equal to the result ofEVP_AEAD_nonce_lengthfor this AEAD.EVP_AEAD_CTX_open()never results in partial output. ifmax_out_lenis insufficient, zero will be returned andout_lenwill be set to zero. if the input and output are aliased thenoutmust be <=in. no cap.EVP_AEAD_CTX_seal()β encrypts and authenticates the input and authenticates any additional data provided inad, the encrypted input and authentication tag being written as outputout. this function may be called (with the sameEVP_AEAD_CTX) concurrently with itself or withEVP_AEAD_CTX_open(). at mostmax_out_lenbytes are written as output and, in order to ensure success, this value should be thein_lenplus the result ofEVP_AEAD_max_overhead(). on successful return,out_lenis set to the actual number of bytes written. the length of the nonce specified withnonce_lenmust be equal to the result ofEVP_AEAD_nonce_length()for this AEAD.EVP_AEAD_CTX_seal()never results in a partial output. ifmax_out_lenis insufficient, zero will be returned andout_lenwill be set to zero. if the input and output are aliased thenoutmust be <=in. same deal π
so EVP_AEAD_key_length(), EVP_AEAD_max_overhead(), EVP_AEAD_max_tag_len(), and EVP_AEAD_nonce_length() all provide information about the AEAD algorithm aead. the info squad π
EVP_AEAD_max_tag_len() returns the maximum tag length that can be used with the given aead. this is the largest value that can be passed as the tag_len argument to EVP_AEAD_CTX_init(). no built-in EVP_AEAD object has a maximum tag length larger than the constant EVP_AEAD_MAX_TAG_LENGTH. π
all cipher algorithms have a fixed key length unless otherwise stated. the following ciphers are available:
EVP_aead_aes_128_gcm()β AES-128 in Galois Counter Mode, using akey_lenof 16 bytes and anonce_lenof 12 bytes. πEVP_aead_aes_256_gcm()β AES-256 in Galois Counter Mode, using akey_lenof 32 bytes and anonce_lenof 12 bytes. πͺEVP_aead_chacha20_poly1305()β ChaCha20 with a Poly1305 authenticator, using akey_lenof 32 bytes and anonce_lenof 12 bytes. the constantEVP_CHACHAPOLY_TLS_TAG_LENspecifies the length of the authentication tag in bytes and has a value of16. πEVP_aead_xchacha20_poly1305()β XChaCha20 with a Poly1305 authenticator, using akey_lenof 32 bytes and anonce_lenof 24 bytes. the extra-X one, slay π¦
unless compatibility with other implementations like OpenSSL or BoringSSL is required, using theEVP_AEADinterface to AEAD ciphers is recommended in preference to the functions documented in theEVP_EncryptInit(3),EVP_aes_256_gcm(3), andEVP_chacha20_poly1305(3)manual pages. the code then becomes transparent to the AEAD cipher used and much more flexible. it is also safer to use as it prevents common mistakes with the EVP APIs. like, it literally protects you from yourself π β¨
π RETURN VALUES π
EVP_AEAD_CTX_new()returns the newEVP_AEAD_CTXobject on success; otherwiseNULLis returned anderrnois set toENOMEM. πEVP_AEAD_CTX_init(),EVP_AEAD_CTX_open(), andEVP_AEAD_CTX_seal()return1for success or zero for failure. one or zero, that's the whole story πEVP_AEAD_key_length()returns the length of the key used for this AEAD.EVP_AEAD_max_overhead()returns the maximum number of additional bytes added by the act of sealing data with the AEAD.EVP_AEAD_max_tag_len()returns the maximum tag length when using this AEAD.EVP_AEAD_nonce_length()returns the length of the per-message nonce.
β EXAMPLES β
encrypt a string using ChaCha20-Poly1305, bestie:
const EVP_AEAD *aead = EVP_aead_chacha20_poly1305();
static const unsigned char nonce[32] = {0};
size_t buf_len, nonce_len;
EVP_AEAD_CTX *ctx;
ctx = EVP_AEAD_CTX_new();
EVP_AEAD_CTX_init(ctx, aead, key32, EVP_AEAD_key_length(aead),
EVP_AEAD_DEFAULT_TAG_LENGTH, NULL);
nonce_len = EVP_AEAD_nonce_length(aead);
EVP_AEAD_CTX_seal(ctx, out, &out_len, BUFSIZE, nonce,
nonce_len, in, in_len, NULL, 0);
EVP_AEAD_CTX_free(ctx);
also note: nonce is declared as static const unsigned char nonce[32] = {0}; in this example, but the real nonce length comes from EVP_AEAD_nonce_length(aead) β keep that detail, it's in the code for a reason π§
π₯ SEE ALSO π₯
π STANDARDS π
- A. Langley, W. Chang, N. Mavrogiannopoulos, J. Strombergson, and S. Josefsson, ChaCha20-Poly1305 Cipher Suites for Transport Layer Security (TLS), RFC 7905, June 2016.
- S. Arciszewski, XChaCha: eXtended-nonce ChaCha and AEAD_XChaCha20_Poly1305, draft-arciszewski-xchacha-02, October 2018.
π HISTORY π
AEAD is based on the implementation by Adam Langley for Chromium/BoringSSL and first appeared in OpenBSD 5.6. OG icon behavior π
EVP_AEAD_CTX_new() and EVP_AEAD_CTX_free() first appeared in OpenBSD 7.1. the newer additions to the family π±
π CAVEATS π
β οΈ the original publications and code by Adam Langley used a modified AEAD construction that is incompatible with the common style used by AEAD in TLS and incompatible with RFC 7905. like, do not mix these up, fr fr.
- A. Langley and W. Chang, ChaCha20 and Poly1305 based Cipher Suites for TLS, draft-agl-tls-chacha20poly1305-04, November 2013.
- Y. Nir and A. Langley, ChaCha20 and Poly1305 for IETF Protocols, RFC 8439, June 2018.
in particular, the original version used a nonce_len of 8 bytes. eight!! not twelve!! that's the whole trap π
alright bestie, that's the whole page β this lil guide is from the Debian build, dated June 8, 2025 (EVP_AEAD_CTX_INIT(3), Library Functions Manual). go forth and seal responsibly ππ«Άβ¨
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2014, Google Inc
- Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
- Copyright (c) 2023 Ingo Schwarze <schwarze@openbsd.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms