1.\" $OpenBSD: EVP_AEAD_CTX_init.3,v 1.3 2015/11/02 15:40:53 reyk 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: November 2 2015 $ 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_chacha20_poly1305_ietf 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_chacha20_poly1305_ietf 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.Xr EVP_AEAD_overhead 3 . 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_chacha20_poly1305_ietf 228ChaCha20 with a Poly1305 authenticator for IETF Protocols. 229The IETF standardised variant of the AEAD is incompatible with the 230original version. 231It uses a constant salt that is prepended to the nonce. 232.El 233.Pp 234Where possible the 235.Sy EVP_AEAD 236interface to AEAD ciphers should be used in preference to the older 237.Sy EVP 238variants or to the low level interfaces. 239This is because the code then becomes transparent to the AEAD cipher 240used and much more flexible, 241it is also safer to use as it prevents common mistakes with the native APIs. 242.Sh RETURN VALUES 243.Fn EVP_AEAD_CTX_init , 244.Fn EVP_AEAD_CTX_open , 245and 246.Fn EVP_AEAD_CTX_seal 247return 1 for success or zero for failure. 248.Pp 249.Fn EVP_AEAD_key_length 250returns the length of the key used for this AEAD. 251.Pp 252.Fn EVP_AEAD_max_overhead 253returns the maximum number of additional bytes added by the act of 254sealing data with the AEAD. 255.Pp 256.Fn EVP_AEAD_max_tag_len 257returns the maximum tag length when using this AEAD. 258This is the largest value that can be passed as a tag length to 259.Fn EVP_AEAD_CTX_init . 260.Pp 261.Fn EVP_AEAD_nonce_length 262returns the length of the per-message nonce. 263.Sh EXAMPLES 264Encrypt a string using ChaCha20-Poly1305: 265.Bd -literal 266.\" XXX 267const EVP_AEAD *aead = EVP_aead_chacha20_poly1305(); 268static const unsigned char nonce[32] = {0}; 269size_t buf_len, nonce_len; 270EVP_AEAD_CTX ctx; 271 272EVP_AEAD_CTX_init(&ctx, aead, key32, EVP_AEAD_key_length(aead), 273 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL); 274nonce_len = EVP_AEAD_nonce_length(aead); 275 276EVP_AEAD_CTX_seal(&ctx, out, &out_len, BUFSIZE, nonce, 277 nonce_len, in, in_len, NULL, 0); 278 279EVP_AEAD_CTX_cleanup(&ctx); 280.Ed 281.Sh SEE ALSO 282.Xr evp 3 283.Sh STANDARDS 284.Rs 285.%A A. Langley 286.%A W. Chang 287.%D November 2013 288.%R draft-agl-tls-chacha20poly1305-04 289.%T ChaCha20 and Poly1305 based Cipher Suites for TLS 290.Re 291.Pp 292.Rs 293.%A Y. Nir 294.%A A. Langley 295.%D May 2015 296.%R RFC 7539 297.%T ChaCha20 and Poly1305 for IETF Protocols 298.Re 299.Pp 300.Sh HISTORY 301AEAD is based on the implementation by 302.An Adam Langley 303for Chromium/BoringSSL and first appeared in 304.Ox 5.6 . 305