EVP_AEAD_CTX_init(3) - OpenBSD manual pages (original) (raw)
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
SYNOPSIS
/* -lcrypto */#include <openssl/evp.h>
EVP_AEAD_CTX *EVP_AEAD_CTX_new(void);
voidEVP_AEAD_CTX_free(EVP_AEAD_CTX *ctx);
intEVP_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);
voidEVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx);
intEVP_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);
intEVP_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_tEVP_AEAD_key_length(const EVP_AEAD *aead);
size_tEVP_AEAD_max_overhead(const EVP_AEAD *aead);
size_tEVP_AEAD_max_tag_len(const EVP_AEAD *aead);
size_tEVP_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
AEAD (Authenticated Encryption with Additional Data) couples confidentiality and integrity in a single primitive. AEAD algorithms take a key and can then seal and open individual messages. Each message has a unique, per-message nonce and, optionally, additional data which is authenticated but not included in the output.
EVP_AEAD_CTX_new() allocates a new context for use withEVP_AEAD_CTX_init(). It can be cleaned up for reuse with EVP_AEAD_CTX_cleanup() and must be freed withEVP_AEAD_CTX_free().
EVP_AEAD_CTX_free() cleans up ctx and frees the space allocated to it.
EVP_AEAD_CTX_init() initializes the context ctx for the given AEAD algorithm aead. The engine argument must be NULL for the default implementation; other values are not supported. Authentication tags may be truncated by passing a tag length. A tag_len argument of EVP_AEAD_DEFAULT_TAG_LENGTH, which has the value 0, causes the default tag length to be used.
EVP_AEAD_CTX_cleanup() frees any data allocated for the context ctx. AfterEVP_AEAD_CTX_cleanup(), ctx is in the same state as after EVP_AEAD_CTX_new().
EVP_AEAD_CTX_open() authenticates the input in and optional additional data ad, 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_len should be at least the same as the input length in_len. On successful returnout_len is set to the actual number of bytes written. The length of the nonce specified withnonce_len must be equal to the result of EVP_AEAD_nonce_length for this AEAD.EVP_AEAD_CTX_open() never results in partial output. If max_out_len is insufficient, zero will be returned and out_len will be set to zero. If the input and output are aliased then out must be <=in.
EVP_AEAD_CTX_seal() encrypts and authenticates the input and authenticates any additional data provided in ad, the encrypted input and authentication tag being written as output out. This function may be called (with the same EVP_AEAD_CTX) concurrently with itself or with EVP_AEAD_CTX_open(). At mostmax_out_len bytes are written as output and, in order to ensure success, this value should be the in_len plus the result of EVP_AEAD_max_overhead(). On successful return, out_len is set to the actual number of bytes written. The length of the nonce specified with nonce_len must be equal to the result ofEVP_AEAD_nonce_length() for this AEAD. EVP_AEAD_CTX_seal() never results in a partial output. If max_out_len is insufficient, zero will be returned and out_len will be set to zero. If the input and output are aliased then out must be <= in.
EVP_AEAD_key_length(),EVP_AEAD_max_overhead(),EVP_AEAD_max_tag_len(), andEVP_AEAD_nonce_length() provide information about the AEAD algorithm aead.
EVP_AEAD_max_tag_len() returns the maximum tag length that can be used with the givenaead. This is the largest value that can be passed as the tag_len argument toEVP_AEAD_CTX_init(). No built-inEVP_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:
AES-128 in Galois Counter Mode, using a key_len of 16 bytes and a nonce_len of 12 bytes.
AES-256 in Galois Counter Mode, using a key_len of 32 bytes and a nonce_len of 12 bytes.
ChaCha20 with a Poly1305 authenticator, using akey_len of 32 bytes and anonce_len of 12 bytes. The constantEVP_CHACHAPOLY_TLS_TAG_LEN specifies the length of the authentication tag in bytes and has a value of 16.
XChaCha20 with a Poly1305 authenticator, using akey_len of 32 bytes and anonce_len of 24 bytes.
Unless compatibility with other implementations like OpenSSL or BoringSSL is required, using theEVP_AEAD interface 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.
RETURN VALUES
EVP_AEAD_CTX_new() returns the newEVP_AEAD_CTX object on success; otherwiseNULL is returned and errno is set to ENOMEM.
EVP_AEAD_CTX_init(),EVP_AEAD_CTX_open(), andEVP_AEAD_CTX_seal() return 1 for success or zero for failure.
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:
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);
SEE ALSO
STANDARDS
A. Langley,W. Chang, N. Mavrogiannopoulos, J. Strombergson, andS. 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 inOpenBSD 5.6.
EVP_AEAD_CTX_new() andEVP_AEAD_CTX_free() first appeared inOpenBSD 7.1.
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:
A. Langley andW. Chang, ChaCha20 and Poly1305 based Cipher Suites for TLS,draft-agl-tls-chacha20poly1305-04,November 2013.
Y. Nir andA. Langley, ChaCha20 and Poly1305 for IETF Protocols,RFC 8439, June 2018.
In particular, the original version used anonce_len of 8 bytes.