1.\" $OpenBSD: EVP_AEAD_CTX_init.3,v 1.9 2019/06/06 01:06:58 schwarze Exp $ 2.\" 3.\" Copyright (c) 2014, Google Inc. 4.\" Parts of the text were written by Adam Langley and David Benjamin. 5.\" Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org> 6.\" 7.\" Permission to use, copy, modify, and/or distribute this software for any 8.\" purpose with or without fee is hereby granted, provided that the above 9.\" copyright notice and this permission notice appear in all copies. 10.\" 11.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18.\" 19.Dd $Mdocdate: June 6 2019 $ 20.Dt EVP_AEAD_CTX_INIT 3 21.Os 22.Sh NAME 23.Nm EVP_AEAD_CTX_init , 24.Nm EVP_AEAD_CTX_cleanup , 25.Nm EVP_AEAD_CTX_open , 26.Nm EVP_AEAD_CTX_seal , 27.Nm EVP_AEAD_key_length , 28.Nm EVP_AEAD_max_overhead , 29.Nm EVP_AEAD_max_tag_len , 30.Nm EVP_AEAD_nonce_length , 31.Nm EVP_aead_aes_128_gcm , 32.Nm EVP_aead_aes_256_gcm , 33.Nm EVP_aead_chacha20_poly1305 , 34.Nm EVP_aead_xchacha20_poly1305 35.Nd authenticated encryption with additional data 36.Sh SYNOPSIS 37.In openssl/evp.h 38.Ft int 39.Fo EVP_AEAD_CTX_init 40.Fa "EVP_AEAD_CTX *ctx" 41.Fa "const EVP_AEAD *aead" 42.Fa "const unsigned char *key" 43.Fa "size_t key_len" 44.Fa "size_t tag_len" 45.Fa "ENGINE *impl" 46.Fc 47.Ft void 48.Fo EVP_AEAD_CTX_cleanup 49.Fa "EVP_AEAD_CTX *ctx" 50.Fc 51.Ft int 52.Fo EVP_AEAD_CTX_open 53.Fa "const EVP_AEAD_CTX *ctx" 54.Fa "unsigned char *out" 55.Fa "size_t *out_len" 56.Fa "size_t max_out_len" 57.Fa "const unsigned char *nonce" 58.Fa "size_t nonce_len" 59.Fa "const unsigned char *in" 60.Fa "size_t in_len" 61.Fa "const unsigned char *ad" 62.Fa "size_t ad_len" 63.Fc 64.Ft int 65.Fo EVP_AEAD_CTX_seal 66.Fa "const EVP_AEAD_CTX *ctx" 67.Fa "unsigned char *out" 68.Fa "size_t *out_len" 69.Fa "size_t max_out_len" 70.Fa "const unsigned char *nonce" 71.Fa "size_t nonce_len" 72.Fa "const unsigned char *in" 73.Fa "size_t in_len" 74.Fa "const unsigned char *ad" 75.Fa "size_t ad_len" 76.Fc 77.Ft size_t 78.Fo EVP_AEAD_key_length 79.Fa "const EVP_AEAD *aead" 80.Fc 81.Ft size_t 82.Fo EVP_AEAD_max_overhead 83.Fa "const EVP_AEAD *aead" 84.Fc 85.Ft size_t 86.Fo EVP_AEAD_max_tag_len 87.Fa "const EVP_AEAD *aead" 88.Fc 89.Ft size_t 90.Fo EVP_AEAD_nonce_length 91.Fa "const EVP_AEAD *aead" 92.Fc 93.Ft const EVP_AEAD * 94.Fo EVP_aead_aes_128_gcm 95.Fa void 96.Fc 97.Ft const EVP_AEAD * 98.Fo EVP_aead_aes_256_gcm 99.Fa void 100.Fc 101.Ft const EVP_AEAD * 102.Fo EVP_aead_chacha20_poly1305 103.Fa void 104.Fc 105.Ft const EVP_AEAD * 106.Fo EVP_aead_xchacha20_poly1305 107.Fa void 108.Fc 109.Sh DESCRIPTION 110AEAD (Authenticated Encryption with Additional Data) couples 111confidentiality and integrity in a single primitive. 112AEAD algorithms take a key and can then seal and open individual 113messages. 114Each message has a unique, per-message nonce and, optionally, additional 115data which is authenticated but not included in the output. 116.Pp 117.Fn EVP_AEAD_CTX_init 118initializes the context 119.Fa ctx 120for the given AEAD algorithm 121.Fa aead . 122The 123.Fa impl 124argument must be 125.Dv NULL 126for the default implementation; 127other values are currently not supported. 128Authentication tags may be truncated by passing a tag length. 129A tag length of zero indicates the default tag length should be used. 130.Pp 131.Fn EVP_AEAD_CTX_cleanup 132frees any data allocated for the context 133.Fa ctx . 134.Pp 135.Fn EVP_AEAD_CTX_open 136authenticates the input 137.Fa in 138and optional additional data 139.Fa ad , 140decrypting the input and writing it as output 141.Fa out . 142This function may be called (with the same 143.Vt EVP_AEAD_CTX ) 144concurrently with itself or with 145.Fn EVP_AEAD_CTX_seal . 146At most the number of input bytes are written as output. 147In order to ensure success, 148.Fa max_out_len 149should be at least the same as the input length 150.Fa in_len . 151On successful return 152.Fa out_len 153is set to the actual number of bytes written. 154The length of the 155.Fa nonce 156specified with 157.Fa nonce_len 158must be equal to the result of EVP_AEAD_nonce_length for this AEAD. 159.Fn EVP_AEAD_CTX_open 160never results in partial output. 161If 162.Fa max_out_len 163is insufficient, zero will be returned and 164.Fa out_len 165will be set to zero. 166If the input and output are aliased then 167.Fa out 168must be <= 169.Fa in . 170.Pp 171.Fn EVP_AEAD_CTX_seal 172encrypts and authenticates the input and authenticates any additional 173data provided in 174.Fa ad , 175the encrypted input and authentication tag being written as output 176.Fa out . 177This function may be called (with the same 178.Vt EVP_AEAD_CTX ) 179concurrently with itself or with 180.Fn EVP_AEAD_CTX_open . 181At most 182.Fa max_out_len 183bytes are written as output and, in order to ensure success, this value 184should be the 185.Fa in_len 186plus the result of 187.Fn EVP_AEAD_max_overhead . 188On successful return, 189.Fa out_len 190is set to the actual number of bytes written. 191The length of the 192.Fa nonce 193specified with 194.Fa nonce_len 195must be equal to the result of 196.Fn EVP_AEAD_nonce_length 197for this AEAD. 198.Fn EVP_AEAD_CTX_seal 199never results in a partial output. 200If 201.Fa max_out_len 202is insufficient, zero will be returned and 203.Fa out_len 204will be set to zero. 205If the input and output are aliased then 206.Fa out 207must be <= 208.Fa in . 209.Pp 210.Fn EVP_AEAD_key_length , 211.Fn EVP_AEAD_max_overhead , 212.Fn EVP_AEAD_max_tag_len , 213and 214.Fn EVP_AEAD_nonce_length 215provide information about the AEAD algorithm 216.Fa aead . 217.Pp 218All cipher algorithms have a fixed key length unless otherwise stated. 219The following ciphers are available: 220.Bl -tag -width Ds -offset indent 221.It Fn EVP_aead_aes_128_gcm 222AES-128 in Galois Counter Mode. 223.It Fn EVP_aead_aes_256_gcm 224AES-256 in Galois Counter Mode. 225.It Fn EVP_aead_chacha20_poly1305 226ChaCha20 with a Poly1305 authenticator. 227.It Fn EVP_aead_xchacha20_poly1305 228XChaCha20 with a Poly1305 authenticator. 229.El 230.Pp 231Where possible the 232.Sy EVP_AEAD 233interface to AEAD ciphers should be used in preference to the older 234.Sy EVP 235variants or to the low level interfaces. 236This is because the code then becomes transparent to the AEAD cipher 237used and much more flexible. 238It is also safer to use as it prevents common mistakes with the native APIs. 239.Sh RETURN VALUES 240.Fn EVP_AEAD_CTX_init , 241.Fn EVP_AEAD_CTX_open , 242and 243.Fn EVP_AEAD_CTX_seal 244return 1 for success or zero for failure. 245.Pp 246.Fn EVP_AEAD_key_length 247returns the length of the key used for this AEAD. 248.Pp 249.Fn EVP_AEAD_max_overhead 250returns the maximum number of additional bytes added by the act of 251sealing data with the AEAD. 252.Pp 253.Fn EVP_AEAD_max_tag_len 254returns the maximum tag length when using this AEAD. 255This is the largest value that can be passed as a tag length to 256.Fn EVP_AEAD_CTX_init . 257.Pp 258.Fn EVP_AEAD_nonce_length 259returns the length of the per-message nonce. 260.Sh EXAMPLES 261Encrypt a string using ChaCha20-Poly1305: 262.Bd -literal -offset indent 263const EVP_AEAD *aead = EVP_aead_chacha20_poly1305(); 264static const unsigned char nonce[32] = {0}; 265size_t buf_len, nonce_len; 266EVP_AEAD_CTX ctx; 267 268EVP_AEAD_CTX_init(&ctx, aead, key32, EVP_AEAD_key_length(aead), 269 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL); 270nonce_len = EVP_AEAD_nonce_length(aead); 271 272EVP_AEAD_CTX_seal(&ctx, out, &out_len, BUFSIZE, nonce, 273 nonce_len, in, in_len, NULL, 0); 274 275EVP_AEAD_CTX_cleanup(&ctx); 276.Ed 277.Sh SEE ALSO 278.Xr evp 3 , 279.Xr EVP_EncryptInit 3 280.Sh STANDARDS 281.Rs 282.%A A. Langley 283.%A W. Chang 284.%D November 2013 285.%R draft-agl-tls-chacha20poly1305-04 286.%T ChaCha20 and Poly1305 based Cipher Suites for TLS 287.Re 288.Pp 289.Rs 290.%A Y. Nir 291.%A A. Langley 292.%D May 2015 293.%R RFC 7539 294.%T ChaCha20 and Poly1305 for IETF Protocols 295.Re 296.Pp 297.Rs 298.%A S. Arciszewski 299.%D October 2018 300.%R draft-arciszewski-xchacha-02 301.%T XChaCha: eXtended-nonce ChaCha and AEAD_XChaCha20_Poly1305 302.Re 303.Sh HISTORY 304AEAD is based on the implementation by 305.An Adam Langley 306for Chromium/BoringSSL and first appeared in 307.Ox 5.6 . 308