blob: 35e0f1399dfa9048fa3955d85b2122cda241819e [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#ifndef OPENSSL_HEADER_AEAD_H
16#define OPENSSL_HEADER_AEAD_H
17
18#include <openssl/base.h>
19
20#if defined(__cplusplus)
21extern "C" {
22#endif
23
24
25/* Authenticated Encryption with Additional Data.
26 *
Adam Langleyfad63272015-11-12 12:15:39 -080027 * AEAD couples confidentiality and integrity in a single primitive. AEAD
Adam Langleyd9e397b2015-01-22 14:27:53 -080028 * algorithms take a key and then can seal and open individual messages. Each
29 * message has a unique, per-message nonce and, optionally, additional data
30 * which is authenticated but not included in the ciphertext.
31 *
32 * The |EVP_AEAD_CTX_init| function initialises an |EVP_AEAD_CTX| structure and
33 * performs any precomputation needed to use |aead| with |key|. The length of
34 * the key, |key_len|, is given in bytes.
35 *
36 * The |tag_len| argument contains the length of the tags, in bytes, and allows
37 * for the processing of truncated authenticators. A zero value indicates that
38 * the default tag length should be used and this is defined as
39 * |EVP_AEAD_DEFAULT_TAG_LENGTH| in order to make the code clear. Using
40 * truncated tags increases an attacker's chance of creating a valid forgery.
41 * Be aware that the attacker's chance may increase more than exponentially as
42 * would naively be expected.
43 *
44 * When no longer needed, the initialised |EVP_AEAD_CTX| structure must be
45 * passed to |EVP_AEAD_CTX_cleanup|, which will deallocate any memory used.
46 *
47 * With an |EVP_AEAD_CTX| in hand, one can seal and open messages. These
48 * operations are intended to meet the standard notions of privacy and
49 * authenticity for authenticated encryption. For formal definitions see
50 * Bellare and Namprempre, "Authenticated encryption: relations among notions
51 * and analysis of the generic composition paradigm," Lecture Notes in Computer
52 * Science B<1976> (2000), 531–545,
53 * http://www-cse.ucsd.edu/~mihir/papers/oem.html.
54 *
55 * When sealing messages, a nonce must be given. The length of the nonce is
56 * fixed by the AEAD in use and is returned by |EVP_AEAD_nonce_length|. *The
57 * nonce must be unique for all messages with the same key*. This is critically
58 * important - nonce reuse may completely undermine the security of the AEAD.
59 * Nonces may be predictable and public, so long as they are unique. Uniqueness
60 * may be achieved with a simple counter or, if large enough, may be generated
61 * randomly. The nonce must be passed into the "open" operation by the receiver
62 * so must either be implicit (e.g. a counter), or must be transmitted along
63 * with the sealed message.
64 *
65 * The "seal" and "open" operations are atomic - an entire message must be
66 * encrypted or decrypted in a single call. Large messages may have to be split
Steven Valdezb0b45c62017-01-17 16:23:54 -050067 * up in order to accommodate this. When doing so, be mindful of the need not to
Adam Langleyd9e397b2015-01-22 14:27:53 -080068 * repeat nonces and the possibility that an attacker could duplicate, reorder
69 * or drop message chunks. For example, using a single key for a given (large)
70 * message and sealing chunks with nonces counting from zero would be secure as
71 * long as the number of chunks was securely transmitted. (Otherwise an
72 * attacker could truncate the message by dropping chunks from the end.)
73 *
74 * The number of chunks could be transmitted by prefixing it to the plaintext,
75 * for example. This also assumes that no other message would ever use the same
76 * key otherwise the rule that nonces must be unique for a given key would be
77 * violated.
78 *
79 * The "seal" and "open" operations also permit additional data to be
80 * authenticated via the |ad| parameter. This data is not included in the
81 * ciphertext and must be identical for both the "seal" and "open" call. This
82 * permits implicit context to be authenticated but may be empty if not needed.
83 *
84 * The "seal" and "open" operations may work in-place if the |out| and |in|
David Benjaminf0c4a6c2016-08-11 13:26:41 -040085 * arguments are equal. Otherwise, if |out| and |in| alias, input data may be
86 * overwritten before it is read. This situation will cause an error.
Adam Langleyd9e397b2015-01-22 14:27:53 -080087 *
88 * The "seal" and "open" operations return one on success and zero on error. */
89
90
91/* AEAD algorithms. */
92
93/* EVP_aead_aes_128_gcm is AES-128 in Galois Counter Mode. */
94OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm(void);
95
96/* EVP_aead_aes_256_gcm is AES-256 in Galois Counter Mode. */
97OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm(void);
98
Adam Langley4139edb2016-01-13 15:00:54 -080099/* EVP_aead_chacha20_poly1305 is the AEAD built from ChaCha20 and
100 * Poly1305 as described in RFC 7539. */
101OPENSSL_EXPORT const EVP_AEAD *EVP_aead_chacha20_poly1305(void);
102
Kenny Roote99801b2015-11-06 15:31:15 -0800103/* EVP_aead_chacha20_poly1305_old is an AEAD built from ChaCha20 and
104 * Poly1305 that is used in the experimental ChaCha20-Poly1305 TLS cipher
105 * suites. */
106OPENSSL_EXPORT const EVP_AEAD *EVP_aead_chacha20_poly1305_old(void);
107
Adam Langleye9ada862015-05-11 17:20:37 -0700108/* EVP_aead_aes_128_ctr_hmac_sha256 is AES-128 in CTR mode with HMAC-SHA256 for
109 * authentication. The nonce is 12 bytes; the bottom 32-bits are used as the
110 * block counter, thus the maximum plaintext size is 64GB. */
111OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_ctr_hmac_sha256(void);
112
Adam Langleyfad63272015-11-12 12:15:39 -0800113/* EVP_aead_aes_256_ctr_hmac_sha256 is AES-256 in CTR mode with HMAC-SHA256 for
Adam Langleye9ada862015-05-11 17:20:37 -0700114 * authentication. See |EVP_aead_aes_128_ctr_hmac_sha256| for details. */
115OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_ctr_hmac_sha256(void);
116
David Benjamin1b249672016-12-06 18:25:50 -0500117/* EVP_aead_aes_128_gcm_siv is AES-128 in GCM-SIV mode. See
118 * https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02 */
119OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_gcm_siv(void);
120
121/* EVP_aead_aes_256_gcm_siv is AES-256 in GCM-SIV mode. See
122 * https://tools.ietf.org/html/draft-irtf-cfrg-gcmsiv-02 */
123OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_gcm_siv(void);
124
Adam Langleyd9e397b2015-01-22 14:27:53 -0800125/* EVP_has_aes_hardware returns one if we enable hardware support for fast and
126 * constant-time AES-GCM. */
127OPENSSL_EXPORT int EVP_has_aes_hardware(void);
128
129
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130/* Utility functions. */
131
132/* EVP_AEAD_key_length returns the length, in bytes, of the keys used by
133 * |aead|. */
134OPENSSL_EXPORT size_t EVP_AEAD_key_length(const EVP_AEAD *aead);
135
136/* EVP_AEAD_nonce_length returns the length, in bytes, of the per-message nonce
137 * for |aead|. */
138OPENSSL_EXPORT size_t EVP_AEAD_nonce_length(const EVP_AEAD *aead);
139
140/* EVP_AEAD_max_overhead returns the maximum number of additional bytes added
141 * by the act of sealing data with |aead|. */
142OPENSSL_EXPORT size_t EVP_AEAD_max_overhead(const EVP_AEAD *aead);
143
144/* EVP_AEAD_max_tag_len returns the maximum tag length when using |aead|. This
145 * is the largest value that can be passed as |tag_len| to
146 * |EVP_AEAD_CTX_init|. */
147OPENSSL_EXPORT size_t EVP_AEAD_max_tag_len(const EVP_AEAD *aead);
148
149
150/* AEAD operations. */
151
152/* An EVP_AEAD_CTX represents an AEAD algorithm configured with a specific key
153 * and message-independent IV. */
154typedef struct evp_aead_ctx_st {
155 const EVP_AEAD *aead;
156 /* aead_state is an opaque pointer to whatever state the AEAD needs to
157 * maintain. */
158 void *aead_state;
159} EVP_AEAD_CTX;
160
161/* EVP_AEAD_MAX_KEY_LENGTH contains the maximum key length used by
162 * any AEAD defined in this header. */
163#define EVP_AEAD_MAX_KEY_LENGTH 80
164
165/* EVP_AEAD_MAX_NONCE_LENGTH contains the maximum nonce length used by
166 * any AEAD defined in this header. */
167#define EVP_AEAD_MAX_NONCE_LENGTH 16
168
169/* EVP_AEAD_MAX_OVERHEAD contains the maximum overhead used by any AEAD
170 * defined in this header. */
171#define EVP_AEAD_MAX_OVERHEAD 64
172
173/* EVP_AEAD_DEFAULT_TAG_LENGTH is a magic value that can be passed to
174 * EVP_AEAD_CTX_init to indicate that the default tag length for an AEAD should
175 * be used. */
176#define EVP_AEAD_DEFAULT_TAG_LENGTH 0
177
Kenny Rootb8494592015-09-25 02:29:14 +0000178/* EVP_AEAD_CTX_zero sets an uninitialized |ctx| to the zero state. It must be
179 * initialized with |EVP_AEAD_CTX_init| before use. It is safe, but not
180 * necessary, to call |EVP_AEAD_CTX_cleanup| in this state. This may be used for
181 * more uniform cleanup of |EVP_AEAD_CTX|. */
182OPENSSL_EXPORT void EVP_AEAD_CTX_zero(EVP_AEAD_CTX *ctx);
183
184/* EVP_AEAD_CTX_init initializes |ctx| for the given AEAD algorithm. The |impl|
185 * argument is ignored and should be NULL. Authentication tags may be truncated
186 * by passing a size as |tag_len|. A |tag_len| of zero indicates the default
187 * tag length and this is defined as EVP_AEAD_DEFAULT_TAG_LENGTH for
188 * readability.
Adam Langleye9ada862015-05-11 17:20:37 -0700189 *
190 * Returns 1 on success. Otherwise returns 0 and pushes to the error stack. In
191 * the error case, you do not need to call |EVP_AEAD_CTX_cleanup|, but it's
192 * harmless to do so. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800193OPENSSL_EXPORT int EVP_AEAD_CTX_init(EVP_AEAD_CTX *ctx, const EVP_AEAD *aead,
194 const uint8_t *key, size_t key_len,
195 size_t tag_len, ENGINE *impl);
196
Adam Langleye9ada862015-05-11 17:20:37 -0700197/* EVP_AEAD_CTX_cleanup frees any data allocated by |ctx|. It is a no-op to
198 * call |EVP_AEAD_CTX_cleanup| on a |EVP_AEAD_CTX| that has been |memset| to
199 * all zeros. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200OPENSSL_EXPORT void EVP_AEAD_CTX_cleanup(EVP_AEAD_CTX *ctx);
201
202/* EVP_AEAD_CTX_seal encrypts and authenticates |in_len| bytes from |in| and
203 * authenticates |ad_len| bytes from |ad| and writes the result to |out|. It
204 * returns one on success and zero otherwise.
205 *
206 * This function may be called (with the same |EVP_AEAD_CTX|) concurrently with
207 * itself or |EVP_AEAD_CTX_open|.
208 *
209 * At most |max_out_len| bytes are written to |out| and, in order to ensure
210 * success, |max_out_len| should be |in_len| plus the result of
Kenny Rootb3106a02015-05-08 13:38:31 -0700211 * |EVP_AEAD_max_overhead|. On successful return, |*out_len| is set to the
212 * actual number of bytes written.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800213 *
214 * The length of |nonce|, |nonce_len|, must be equal to the result of
215 * |EVP_AEAD_nonce_length| for this AEAD.
216 *
217 * |EVP_AEAD_CTX_seal| never results in a partial output. If |max_out_len| is
218 * insufficient, zero will be returned. (In this case, |*out_len| is set to
219 * zero.)
220 *
David Benjamin6e899c72016-06-09 18:02:18 -0400221 * If |in| and |out| alias then |out| must be == |in|. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800222OPENSSL_EXPORT int EVP_AEAD_CTX_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
223 size_t *out_len, size_t max_out_len,
224 const uint8_t *nonce, size_t nonce_len,
225 const uint8_t *in, size_t in_len,
226 const uint8_t *ad, size_t ad_len);
227
228/* EVP_AEAD_CTX_open authenticates |in_len| bytes from |in| and |ad_len| bytes
229 * from |ad| and decrypts at most |in_len| bytes into |out|. It returns one on
230 * success and zero otherwise.
231 *
232 * This function may be called (with the same |EVP_AEAD_CTX|) concurrently with
233 * itself or |EVP_AEAD_CTX_seal|.
234 *
235 * At most |in_len| bytes are written to |out|. In order to ensure success,
236 * |max_out_len| should be at least |in_len|. On successful return, |*out_len|
237 * is set to the the actual number of bytes written.
238 *
239 * The length of |nonce|, |nonce_len|, must be equal to the result of
240 * |EVP_AEAD_nonce_length| for this AEAD.
241 *
242 * |EVP_AEAD_CTX_open| never results in a partial output. If |max_out_len| is
243 * insufficient, zero will be returned. (In this case, |*out_len| is set to
244 * zero.)
245 *
David Benjamin6e899c72016-06-09 18:02:18 -0400246 * If |in| and |out| alias then |out| must be == |in|. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800247OPENSSL_EXPORT int EVP_AEAD_CTX_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
248 size_t *out_len, size_t max_out_len,
249 const uint8_t *nonce, size_t nonce_len,
250 const uint8_t *in, size_t in_len,
251 const uint8_t *ad, size_t ad_len);
252
David Benjamin7c0d06c2016-08-11 13:26:41 -0400253/* EVP_AEAD_CTX_aead returns the underlying AEAD for |ctx|, or NULL if one has
254 * not been set. */
255OPENSSL_EXPORT const EVP_AEAD *EVP_AEAD_CTX_aead(const EVP_AEAD_CTX *ctx);
256
Adam Langleyd9e397b2015-01-22 14:27:53 -0800257
David Benjamin4969cc92016-04-22 15:02:23 -0400258/* TLS-specific AEAD algorithms.
259 *
260 * These AEAD primitives do not meet the definition of generic AEADs. They are
261 * all specific to TLS and should not be used outside of that context. They must
262 * be initialized with |EVP_AEAD_CTX_init_with_direction|, are stateful, and may
263 * not be used concurrently. Any nonces are used as IVs, so they must be
264 * unpredictable. They only accept an |ad| parameter of length 11 (the standard
265 * TLS one with length omitted). */
266
David Benjamin4969cc92016-04-22 15:02:23 -0400267OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls(void);
268OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_tls_implicit_iv(void);
269OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha256_tls(void);
270
271OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls(void);
272OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_tls_implicit_iv(void);
273OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha256_tls(void);
274OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha384_tls(void);
275
276OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls(void);
277OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_tls_implicit_iv(void);
278
279OPENSSL_EXPORT const EVP_AEAD *EVP_aead_null_sha1_tls(void);
280
281
282/* SSLv3-specific AEAD algorithms.
283 *
284 * These AEAD primitives do not meet the definition of generic AEADs. They are
285 * all specific to SSLv3 and should not be used outside of that context. They
286 * must be initialized with |EVP_AEAD_CTX_init_with_direction|, are stateful,
287 * and may not be used concurrently. They only accept an |ad| parameter of
288 * length 9 (the standard TLS one with length and version omitted). */
289
David Benjamin4969cc92016-04-22 15:02:23 -0400290OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_ssl3(void);
291OPENSSL_EXPORT const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_ssl3(void);
292OPENSSL_EXPORT const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_ssl3(void);
293OPENSSL_EXPORT const EVP_AEAD *EVP_aead_null_sha1_ssl3(void);
294
295
Adam Langleye9ada862015-05-11 17:20:37 -0700296/* Obscure functions. */
297
David Benjamin4969cc92016-04-22 15:02:23 -0400298/* evp_aead_direction_t denotes the direction of an AEAD operation. */
299enum evp_aead_direction_t {
300 evp_aead_open,
301 evp_aead_seal,
302};
303
304/* EVP_AEAD_CTX_init_with_direction calls |EVP_AEAD_CTX_init| for normal
305 * AEADs. For TLS-specific and SSL3-specific AEADs, it initializes |ctx| for a
306 * given direction. */
307OPENSSL_EXPORT int EVP_AEAD_CTX_init_with_direction(
308 EVP_AEAD_CTX *ctx, const EVP_AEAD *aead, const uint8_t *key, size_t key_len,
309 size_t tag_len, enum evp_aead_direction_t dir);
310
Adam Langleyfad63272015-11-12 12:15:39 -0800311/* EVP_AEAD_CTX_get_iv sets |*out_len| to the length of the IV for |ctx| and
312 * sets |*out_iv| to point to that many bytes of the current IV. This is only
313 * meaningful for AEADs with implicit IVs (i.e. CBC mode in SSLv3 and TLS 1.0).
314 *
315 * It returns one on success or zero on error. */
316OPENSSL_EXPORT int EVP_AEAD_CTX_get_iv(const EVP_AEAD_CTX *ctx,
317 const uint8_t **out_iv, size_t *out_len);
318
Adam Langleye9ada862015-05-11 17:20:37 -0700319
Adam Langleyd9e397b2015-01-22 14:27:53 -0800320#if defined(__cplusplus)
321} /* extern C */
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400322
323#if !defined(BORINGSSL_NO_CXX)
324extern "C++" {
325
326namespace bssl {
327
328using ScopedEVP_AEAD_CTX =
329 internal::StackAllocated<EVP_AEAD_CTX, void, EVP_AEAD_CTX_zero,
330 EVP_AEAD_CTX_cleanup>;
331
332} // namespace bssl
333
334} // extern C++
335#endif
336
Adam Langleyd9e397b2015-01-22 14:27:53 -0800337#endif
338
339#endif /* OPENSSL_HEADER_AEAD_H */