xref: /freebsd/contrib/bearssl/inc/bearssl_aead.h (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty  *
4*0957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty  * the following conditions:
11*0957b409SSimon J. Gerraty  *
12*0957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty  *
15*0957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty  * SOFTWARE.
23*0957b409SSimon J. Gerraty  */
24*0957b409SSimon J. Gerraty 
25*0957b409SSimon J. Gerraty #ifndef BR_BEARSSL_AEAD_H__
26*0957b409SSimon J. Gerraty #define BR_BEARSSL_AEAD_H__
27*0957b409SSimon J. Gerraty 
28*0957b409SSimon J. Gerraty #include <stddef.h>
29*0957b409SSimon J. Gerraty #include <stdint.h>
30*0957b409SSimon J. Gerraty 
31*0957b409SSimon J. Gerraty #include "bearssl_block.h"
32*0957b409SSimon J. Gerraty #include "bearssl_hash.h"
33*0957b409SSimon J. Gerraty 
34*0957b409SSimon J. Gerraty #ifdef __cplusplus
35*0957b409SSimon J. Gerraty extern "C" {
36*0957b409SSimon J. Gerraty #endif
37*0957b409SSimon J. Gerraty 
38*0957b409SSimon J. Gerraty /** \file bearssl_aead.h
39*0957b409SSimon J. Gerraty  *
40*0957b409SSimon J. Gerraty  * # Authenticated Encryption with Additional Data
41*0957b409SSimon J. Gerraty  *
42*0957b409SSimon J. Gerraty  * This file documents the API for AEAD encryption.
43*0957b409SSimon J. Gerraty  *
44*0957b409SSimon J. Gerraty  *
45*0957b409SSimon J. Gerraty  * ## Procedural API
46*0957b409SSimon J. Gerraty  *
47*0957b409SSimon J. Gerraty  * An AEAD algorithm processes messages and provides confidentiality
48*0957b409SSimon J. Gerraty  * (encryption) and checked integrity (MAC). It uses the following
49*0957b409SSimon J. Gerraty  * parameters:
50*0957b409SSimon J. Gerraty  *
51*0957b409SSimon J. Gerraty  *   - A symmetric key. Exact size depends on the AEAD algorithm.
52*0957b409SSimon J. Gerraty  *
53*0957b409SSimon J. Gerraty  *   - A nonce (IV). Size depends on the AEAD algorithm; for most
54*0957b409SSimon J. Gerraty  *     algorithms, it is crucial for security that any given nonce
55*0957b409SSimon J. Gerraty  *     value is never used twice for the same key and distinct
56*0957b409SSimon J. Gerraty  *     messages.
57*0957b409SSimon J. Gerraty  *
58*0957b409SSimon J. Gerraty  *   - Data to encrypt and protect.
59*0957b409SSimon J. Gerraty  *
60*0957b409SSimon J. Gerraty  *   - Additional authenticated data, which is covered by the MAC but
61*0957b409SSimon J. Gerraty  *     otherwise left untouched (i.e. not encrypted).
62*0957b409SSimon J. Gerraty  *
63*0957b409SSimon J. Gerraty  * The AEAD algorithm encrypts the data, and produces an authentication
64*0957b409SSimon J. Gerraty  * tag. It is assumed that the encrypted data, the tag, the additional
65*0957b409SSimon J. Gerraty  * authenticated data and the nonce are sent to the receiver; the
66*0957b409SSimon J. Gerraty  * additional data and the nonce may be implicit (e.g. using elements of
67*0957b409SSimon J. Gerraty  * the underlying transport protocol, such as record sequence numbers).
68*0957b409SSimon J. Gerraty  * The receiver will recompute the tag value and compare it with the one
69*0957b409SSimon J. Gerraty  * received; if they match, then the data is correct, and can be
70*0957b409SSimon J. Gerraty  * decrypted and used; otherwise, at least one of the elements was
71*0957b409SSimon J. Gerraty  * altered in transit, normally leading to wholesale rejection of the
72*0957b409SSimon J. Gerraty  * complete message.
73*0957b409SSimon J. Gerraty  *
74*0957b409SSimon J. Gerraty  * For each AEAD algorithm, identified by a symbolic name (hereafter
75*0957b409SSimon J. Gerraty  * denoted as "`xxx`"), the following functions are defined:
76*0957b409SSimon J. Gerraty  *
77*0957b409SSimon J. Gerraty  *   - `br_xxx_init()`
78*0957b409SSimon J. Gerraty  *
79*0957b409SSimon J. Gerraty  *     Initialise the AEAD algorithm, on a provided context structure.
80*0957b409SSimon J. Gerraty  *     Exact parameters depend on the algorithm, and may include
81*0957b409SSimon J. Gerraty  *     pointers to extra implementations and context structures. The
82*0957b409SSimon J. Gerraty  *     secret key is provided at this point, either directly or
83*0957b409SSimon J. Gerraty  *     indirectly.
84*0957b409SSimon J. Gerraty  *
85*0957b409SSimon J. Gerraty  *   - `br_xxx_reset()`
86*0957b409SSimon J. Gerraty  *
87*0957b409SSimon J. Gerraty  *     Start a new AEAD computation. The nonce value is provided as
88*0957b409SSimon J. Gerraty  *     parameter to this function.
89*0957b409SSimon J. Gerraty  *
90*0957b409SSimon J. Gerraty  *   - `br_xxx_aad_inject()`
91*0957b409SSimon J. Gerraty  *
92*0957b409SSimon J. Gerraty  *     Inject some additional authenticated data. Additional data may
93*0957b409SSimon J. Gerraty  *     be provided in several chunks of arbitrary length.
94*0957b409SSimon J. Gerraty  *
95*0957b409SSimon J. Gerraty  *   - `br_xxx_flip()`
96*0957b409SSimon J. Gerraty  *
97*0957b409SSimon J. Gerraty  *     This function MUST be called after injecting all additional
98*0957b409SSimon J. Gerraty  *     authenticated data, and before beginning to encrypt the plaintext
99*0957b409SSimon J. Gerraty  *     (or decrypt the ciphertext).
100*0957b409SSimon J. Gerraty  *
101*0957b409SSimon J. Gerraty  *   - `br_xxx_run()`
102*0957b409SSimon J. Gerraty  *
103*0957b409SSimon J. Gerraty  *     Process some plaintext (to encrypt) or ciphertext (to decrypt).
104*0957b409SSimon J. Gerraty  *     Encryption/decryption is done in place. Data may be provided in
105*0957b409SSimon J. Gerraty  *     several chunks of arbitrary length.
106*0957b409SSimon J. Gerraty  *
107*0957b409SSimon J. Gerraty  *   - `br_xxx_get_tag()`
108*0957b409SSimon J. Gerraty  *
109*0957b409SSimon J. Gerraty  *     Compute the authentication tag. All message data (encrypted or
110*0957b409SSimon J. Gerraty  *     decrypted) must have been injected at that point. Also, this
111*0957b409SSimon J. Gerraty  *     call may modify internal context elements, so it may be called
112*0957b409SSimon J. Gerraty  *     only once for a given AEAD computation.
113*0957b409SSimon J. Gerraty  *
114*0957b409SSimon J. Gerraty  *   - `br_xxx_check_tag()`
115*0957b409SSimon J. Gerraty  *
116*0957b409SSimon J. Gerraty  *     An alternative to `br_xxx_get_tag()`, meant to be used by the
117*0957b409SSimon J. Gerraty  *     receiver: the authentication tag is internally recomputed, and
118*0957b409SSimon J. Gerraty  *     compared with the one provided as parameter.
119*0957b409SSimon J. Gerraty  *
120*0957b409SSimon J. Gerraty  * This API makes the following assumptions on the AEAD algorithm:
121*0957b409SSimon J. Gerraty  *
122*0957b409SSimon J. Gerraty  *   - Encryption does not expand the size of the ciphertext; there is
123*0957b409SSimon J. Gerraty  *     no padding. This is true of most modern AEAD modes such as GCM.
124*0957b409SSimon J. Gerraty  *
125*0957b409SSimon J. Gerraty  *   - The additional authenticated data must be processed first,
126*0957b409SSimon J. Gerraty  *     before the encrypted/decrypted data.
127*0957b409SSimon J. Gerraty  *
128*0957b409SSimon J. Gerraty  *   - Nonce, plaintext and additional authenticated data all consist
129*0957b409SSimon J. Gerraty  *     in an integral number of bytes. There is no provision to use
130*0957b409SSimon J. Gerraty  *     elements whose length in bits is not a multiple of 8.
131*0957b409SSimon J. Gerraty  *
132*0957b409SSimon J. Gerraty  * Each AEAD algorithm has its own requirements and limits on the sizes
133*0957b409SSimon J. Gerraty  * of additional data and plaintext. This API does not provide any
134*0957b409SSimon J. Gerraty  * way to report invalid usage; it is up to the caller to ensure that
135*0957b409SSimon J. Gerraty  * the provided key, nonce, and data elements all fit the algorithm's
136*0957b409SSimon J. Gerraty  * requirements.
137*0957b409SSimon J. Gerraty  *
138*0957b409SSimon J. Gerraty  *
139*0957b409SSimon J. Gerraty  * ## Object-Oriented API
140*0957b409SSimon J. Gerraty  *
141*0957b409SSimon J. Gerraty  * Each context structure begins with a field (called `vtable`) that
142*0957b409SSimon J. Gerraty  * points to an instance of a structure that references the relevant
143*0957b409SSimon J. Gerraty  * functions through pointers. Each such structure contains the
144*0957b409SSimon J. Gerraty  * following:
145*0957b409SSimon J. Gerraty  *
146*0957b409SSimon J. Gerraty  *   - `reset`
147*0957b409SSimon J. Gerraty  *
148*0957b409SSimon J. Gerraty  *     Pointer to the reset function, that allows starting a new
149*0957b409SSimon J. Gerraty  *     computation.
150*0957b409SSimon J. Gerraty  *
151*0957b409SSimon J. Gerraty  *   - `aad_inject`
152*0957b409SSimon J. Gerraty  *
153*0957b409SSimon J. Gerraty  *     Pointer to the additional authenticated data injection function.
154*0957b409SSimon J. Gerraty  *
155*0957b409SSimon J. Gerraty  *   - `flip`
156*0957b409SSimon J. Gerraty  *
157*0957b409SSimon J. Gerraty  *     Pointer to the function that transitions from additional data
158*0957b409SSimon J. Gerraty  *     to main message data processing.
159*0957b409SSimon J. Gerraty  *
160*0957b409SSimon J. Gerraty  *   - `get_tag`
161*0957b409SSimon J. Gerraty  *
162*0957b409SSimon J. Gerraty  *     Pointer to the function that computes and returns the tag.
163*0957b409SSimon J. Gerraty  *
164*0957b409SSimon J. Gerraty  *   - `check_tag`
165*0957b409SSimon J. Gerraty  *
166*0957b409SSimon J. Gerraty  *     Pointer to the function that computes and verifies the tag against
167*0957b409SSimon J. Gerraty  *     a received value.
168*0957b409SSimon J. Gerraty  *
169*0957b409SSimon J. Gerraty  * Note that there is no OOP method for context initialisation: the
170*0957b409SSimon J. Gerraty  * various AEAD algorithms have different requirements that would not
171*0957b409SSimon J. Gerraty  * map well to a single initialisation API.
172*0957b409SSimon J. Gerraty  *
173*0957b409SSimon J. Gerraty  * The OOP API is not provided for CCM, due to its specific requirements
174*0957b409SSimon J. Gerraty  * (length of plaintext must be known in advance).
175*0957b409SSimon J. Gerraty  */
176*0957b409SSimon J. Gerraty 
177*0957b409SSimon J. Gerraty /**
178*0957b409SSimon J. Gerraty  * \brief Class type of an AEAD algorithm.
179*0957b409SSimon J. Gerraty  */
180*0957b409SSimon J. Gerraty typedef struct br_aead_class_ br_aead_class;
181*0957b409SSimon J. Gerraty struct br_aead_class_ {
182*0957b409SSimon J. Gerraty 
183*0957b409SSimon J. Gerraty 	/**
184*0957b409SSimon J. Gerraty 	 * \brief Size (in bytes) of authentication tags created by
185*0957b409SSimon J. Gerraty 	 * this AEAD algorithm.
186*0957b409SSimon J. Gerraty 	 */
187*0957b409SSimon J. Gerraty 	size_t tag_size;
188*0957b409SSimon J. Gerraty 
189*0957b409SSimon J. Gerraty 	/**
190*0957b409SSimon J. Gerraty 	 * \brief Reset an AEAD context.
191*0957b409SSimon J. Gerraty 	 *
192*0957b409SSimon J. Gerraty 	 * This function resets an already initialised AEAD context for
193*0957b409SSimon J. Gerraty 	 * a new computation run. Implementations and keys are
194*0957b409SSimon J. Gerraty 	 * conserved. This function can be called at any time; it
195*0957b409SSimon J. Gerraty 	 * cancels any ongoing AEAD computation that uses the provided
196*0957b409SSimon J. Gerraty 	 * context structure.
197*0957b409SSimon J. Gerraty 
198*0957b409SSimon J. Gerraty 	 * The provided IV is a _nonce_. Each AEAD algorithm has its
199*0957b409SSimon J. Gerraty 	 * own requirements on IV size and contents; for most of them,
200*0957b409SSimon J. Gerraty 	 * it is crucial to security that each nonce value is used
201*0957b409SSimon J. Gerraty 	 * only once for a given secret key.
202*0957b409SSimon J. Gerraty 	 *
203*0957b409SSimon J. Gerraty 	 * \param cc    AEAD context structure.
204*0957b409SSimon J. Gerraty 	 * \param iv    AEAD nonce to use.
205*0957b409SSimon J. Gerraty 	 * \param len   AEAD nonce length (in bytes).
206*0957b409SSimon J. Gerraty 	 */
207*0957b409SSimon J. Gerraty 	void (*reset)(const br_aead_class **cc, const void *iv, size_t len);
208*0957b409SSimon J. Gerraty 
209*0957b409SSimon J. Gerraty 	/**
210*0957b409SSimon J. Gerraty 	 * \brief Inject additional authenticated data.
211*0957b409SSimon J. Gerraty 	 *
212*0957b409SSimon J. Gerraty 	 * The provided data is injected into a running AEAD
213*0957b409SSimon J. Gerraty 	 * computation. Additional data must be injected _before_ the
214*0957b409SSimon J. Gerraty 	 * call to `flip()`. Additional data can be injected in several
215*0957b409SSimon J. Gerraty 	 * chunks of arbitrary length.
216*0957b409SSimon J. Gerraty 	 *
217*0957b409SSimon J. Gerraty 	 * \param cc     AEAD context structure.
218*0957b409SSimon J. Gerraty 	 * \param data   pointer to additional authenticated data.
219*0957b409SSimon J. Gerraty 	 * \param len    length of additional authenticated data (in bytes).
220*0957b409SSimon J. Gerraty 	 */
221*0957b409SSimon J. Gerraty 	void (*aad_inject)(const br_aead_class **cc,
222*0957b409SSimon J. Gerraty 		const void *data, size_t len);
223*0957b409SSimon J. Gerraty 
224*0957b409SSimon J. Gerraty 	/**
225*0957b409SSimon J. Gerraty 	 * \brief Finish injection of additional authenticated data.
226*0957b409SSimon J. Gerraty 	 *
227*0957b409SSimon J. Gerraty 	 * This function MUST be called before beginning the actual
228*0957b409SSimon J. Gerraty 	 * encryption or decryption (with `run()`), even if no
229*0957b409SSimon J. Gerraty 	 * additional authenticated data was injected. No additional
230*0957b409SSimon J. Gerraty 	 * authenticated data may be injected after this function call.
231*0957b409SSimon J. Gerraty 	 *
232*0957b409SSimon J. Gerraty 	 * \param cc   AEAD context structure.
233*0957b409SSimon J. Gerraty 	 */
234*0957b409SSimon J. Gerraty 	void (*flip)(const br_aead_class **cc);
235*0957b409SSimon J. Gerraty 
236*0957b409SSimon J. Gerraty 	/**
237*0957b409SSimon J. Gerraty 	 * \brief Encrypt or decrypt some data.
238*0957b409SSimon J. Gerraty 	 *
239*0957b409SSimon J. Gerraty 	 * Data encryption or decryption can be done after `flip()` has
240*0957b409SSimon J. Gerraty 	 * been called on the context. If `encrypt` is non-zero, then
241*0957b409SSimon J. Gerraty 	 * the provided data shall be plaintext, and it is encrypted in
242*0957b409SSimon J. Gerraty 	 * place. Otherwise, the data shall be ciphertext, and it is
243*0957b409SSimon J. Gerraty 	 * decrypted in place.
244*0957b409SSimon J. Gerraty 	 *
245*0957b409SSimon J. Gerraty 	 * Data may be provided in several chunks of arbitrary length.
246*0957b409SSimon J. Gerraty 	 *
247*0957b409SSimon J. Gerraty 	 * \param cc        AEAD context structure.
248*0957b409SSimon J. Gerraty 	 * \param encrypt   non-zero for encryption, zero for decryption.
249*0957b409SSimon J. Gerraty 	 * \param data      data to encrypt or decrypt.
250*0957b409SSimon J. Gerraty 	 * \param len       data length (in bytes).
251*0957b409SSimon J. Gerraty 	 */
252*0957b409SSimon J. Gerraty 	void (*run)(const br_aead_class **cc, int encrypt,
253*0957b409SSimon J. Gerraty 		void *data, size_t len);
254*0957b409SSimon J. Gerraty 
255*0957b409SSimon J. Gerraty 	/**
256*0957b409SSimon J. Gerraty 	 * \brief Compute authentication tag.
257*0957b409SSimon J. Gerraty 	 *
258*0957b409SSimon J. Gerraty 	 * Compute the AEAD authentication tag. The tag length depends
259*0957b409SSimon J. Gerraty 	 * on the AEAD algorithm; it is written in the provided `tag`
260*0957b409SSimon J. Gerraty 	 * buffer. This call terminates the AEAD run: no data may be
261*0957b409SSimon J. Gerraty 	 * processed with that AEAD context afterwards, until `reset()`
262*0957b409SSimon J. Gerraty 	 * is called to initiate a new AEAD run.
263*0957b409SSimon J. Gerraty 	 *
264*0957b409SSimon J. Gerraty 	 * The tag value must normally be sent along with the encrypted
265*0957b409SSimon J. Gerraty 	 * data. When decrypting, the tag value must be recomputed and
266*0957b409SSimon J. Gerraty 	 * compared with the received tag: if the two tag values differ,
267*0957b409SSimon J. Gerraty 	 * then either the tag or the encrypted data was altered in
268*0957b409SSimon J. Gerraty 	 * transit. As an alternative to this function, the
269*0957b409SSimon J. Gerraty 	 * `check_tag()` function may be used to compute and check the
270*0957b409SSimon J. Gerraty 	 * tag value.
271*0957b409SSimon J. Gerraty 	 *
272*0957b409SSimon J. Gerraty 	 * Tag length depends on the AEAD algorithm.
273*0957b409SSimon J. Gerraty 	 *
274*0957b409SSimon J. Gerraty 	 * \param cc    AEAD context structure.
275*0957b409SSimon J. Gerraty 	 * \param tag   destination buffer for the tag.
276*0957b409SSimon J. Gerraty 	 */
277*0957b409SSimon J. Gerraty 	void (*get_tag)(const br_aead_class **cc, void *tag);
278*0957b409SSimon J. Gerraty 
279*0957b409SSimon J. Gerraty 	/**
280*0957b409SSimon J. Gerraty 	 * \brief Compute and check authentication tag.
281*0957b409SSimon J. Gerraty 	 *
282*0957b409SSimon J. Gerraty 	 * This function is an alternative to `get_tag()`, and is
283*0957b409SSimon J. Gerraty 	 * normally used on the receiving end (i.e. when decrypting
284*0957b409SSimon J. Gerraty 	 * messages). The tag value is recomputed and compared with the
285*0957b409SSimon J. Gerraty 	 * provided tag value. If they match, 1 is returned; on
286*0957b409SSimon J. Gerraty 	 * mismatch, 0 is returned. A returned value of 0 means that the
287*0957b409SSimon J. Gerraty 	 * data or the tag was altered in transit, normally leading to
288*0957b409SSimon J. Gerraty 	 * wholesale rejection of the complete message.
289*0957b409SSimon J. Gerraty 	 *
290*0957b409SSimon J. Gerraty 	 * Tag length depends on the AEAD algorithm.
291*0957b409SSimon J. Gerraty 	 *
292*0957b409SSimon J. Gerraty 	 * \param cc    AEAD context structure.
293*0957b409SSimon J. Gerraty 	 * \param tag   tag value to compare with.
294*0957b409SSimon J. Gerraty 	 * \return  1 on success (exact match of tag value), 0 otherwise.
295*0957b409SSimon J. Gerraty 	 */
296*0957b409SSimon J. Gerraty 	uint32_t (*check_tag)(const br_aead_class **cc, const void *tag);
297*0957b409SSimon J. Gerraty 
298*0957b409SSimon J. Gerraty 	/**
299*0957b409SSimon J. Gerraty 	 * \brief Compute authentication tag (with truncation).
300*0957b409SSimon J. Gerraty 	 *
301*0957b409SSimon J. Gerraty 	 * This function is similar to `get_tag()`, except that the tag
302*0957b409SSimon J. Gerraty 	 * length is provided. Some AEAD algorithms allow several tag
303*0957b409SSimon J. Gerraty 	 * lengths, usually by truncating the normal tag. Shorter tags
304*0957b409SSimon J. Gerraty 	 * mechanically increase success probability of forgeries.
305*0957b409SSimon J. Gerraty 	 * The range of allowed tag lengths depends on the algorithm.
306*0957b409SSimon J. Gerraty 	 *
307*0957b409SSimon J. Gerraty 	 * \param cc    AEAD context structure.
308*0957b409SSimon J. Gerraty 	 * \param tag   destination buffer for the tag.
309*0957b409SSimon J. Gerraty 	 * \param len   tag length (in bytes).
310*0957b409SSimon J. Gerraty 	 */
311*0957b409SSimon J. Gerraty 	void (*get_tag_trunc)(const br_aead_class **cc, void *tag, size_t len);
312*0957b409SSimon J. Gerraty 
313*0957b409SSimon J. Gerraty 	/**
314*0957b409SSimon J. Gerraty 	 * \brief Compute and check authentication tag (with truncation).
315*0957b409SSimon J. Gerraty 	 *
316*0957b409SSimon J. Gerraty 	 * This function is similar to `check_tag()` except that it
317*0957b409SSimon J. Gerraty 	 * works over an explicit tag length. See `get_tag()` for a
318*0957b409SSimon J. Gerraty 	 * discussion of explicit tag lengths; the range of allowed tag
319*0957b409SSimon J. Gerraty 	 * lengths depends on the algorithm.
320*0957b409SSimon J. Gerraty 	 *
321*0957b409SSimon J. Gerraty 	 * \param cc    AEAD context structure.
322*0957b409SSimon J. Gerraty 	 * \param tag   tag value to compare with.
323*0957b409SSimon J. Gerraty 	 * \param len   tag length (in bytes).
324*0957b409SSimon J. Gerraty 	 * \return  1 on success (exact match of tag value), 0 otherwise.
325*0957b409SSimon J. Gerraty 	 */
326*0957b409SSimon J. Gerraty 	uint32_t (*check_tag_trunc)(const br_aead_class **cc,
327*0957b409SSimon J. Gerraty 		const void *tag, size_t len);
328*0957b409SSimon J. Gerraty };
329*0957b409SSimon J. Gerraty 
330*0957b409SSimon J. Gerraty /**
331*0957b409SSimon J. Gerraty  * \brief Context structure for GCM.
332*0957b409SSimon J. Gerraty  *
333*0957b409SSimon J. Gerraty  * GCM is an AEAD mode that combines a block cipher in CTR mode with a
334*0957b409SSimon J. Gerraty  * MAC based on GHASH, to provide authenticated encryption:
335*0957b409SSimon J. Gerraty  *
336*0957b409SSimon J. Gerraty  *   - Any block cipher with 16-byte blocks can be used with GCM.
337*0957b409SSimon J. Gerraty  *
338*0957b409SSimon J. Gerraty  *   - The nonce can have any length, from 0 up to 2^64-1 bits; however,
339*0957b409SSimon J. Gerraty  *     96-bit nonces (12 bytes) are recommended (nonces with a length
340*0957b409SSimon J. Gerraty  *     distinct from 12 bytes are internally hashed, which risks reusing
341*0957b409SSimon J. Gerraty  *     nonce value with a small but not always negligible probability).
342*0957b409SSimon J. Gerraty  *
343*0957b409SSimon J. Gerraty  *   - Additional authenticated data may have length up to 2^64-1 bits.
344*0957b409SSimon J. Gerraty  *
345*0957b409SSimon J. Gerraty  *   - Message length may range up to 2^39-256 bits at most.
346*0957b409SSimon J. Gerraty  *
347*0957b409SSimon J. Gerraty  *   - The authentication tag has length 16 bytes.
348*0957b409SSimon J. Gerraty  *
349*0957b409SSimon J. Gerraty  * The GCM initialisation function receives as parameter an
350*0957b409SSimon J. Gerraty  * _initialised_ block cipher implementation context, with the secret
351*0957b409SSimon J. Gerraty  * key already set. A pointer to that context will be kept within the
352*0957b409SSimon J. Gerraty  * GCM context structure. It is up to the caller to allocate and
353*0957b409SSimon J. Gerraty  * initialise that block cipher context.
354*0957b409SSimon J. Gerraty  */
355*0957b409SSimon J. Gerraty typedef struct {
356*0957b409SSimon J. Gerraty 	/** \brief Pointer to vtable for this context. */
357*0957b409SSimon J. Gerraty 	const br_aead_class *vtable;
358*0957b409SSimon J. Gerraty 
359*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
360*0957b409SSimon J. Gerraty 	const br_block_ctr_class **bctx;
361*0957b409SSimon J. Gerraty 	br_ghash gh;
362*0957b409SSimon J. Gerraty 	unsigned char h[16];
363*0957b409SSimon J. Gerraty 	unsigned char j0_1[12];
364*0957b409SSimon J. Gerraty 	unsigned char buf[16];
365*0957b409SSimon J. Gerraty 	unsigned char y[16];
366*0957b409SSimon J. Gerraty 	uint32_t j0_2, jc;
367*0957b409SSimon J. Gerraty 	uint64_t count_aad, count_ctr;
368*0957b409SSimon J. Gerraty #endif
369*0957b409SSimon J. Gerraty } br_gcm_context;
370*0957b409SSimon J. Gerraty 
371*0957b409SSimon J. Gerraty /**
372*0957b409SSimon J. Gerraty  * \brief Initialize a GCM context.
373*0957b409SSimon J. Gerraty  *
374*0957b409SSimon J. Gerraty  * A block cipher implementation, with its initialised context structure,
375*0957b409SSimon J. Gerraty  * is provided. The block cipher MUST use 16-byte blocks in CTR mode,
376*0957b409SSimon J. Gerraty  * and its secret key MUST have been already set in the provided context.
377*0957b409SSimon J. Gerraty  * A GHASH implementation must also be provided. The parameters are linked
378*0957b409SSimon J. Gerraty  * in the GCM context.
379*0957b409SSimon J. Gerraty  *
380*0957b409SSimon J. Gerraty  * After this function has been called, the `br_gcm_reset()` function must
381*0957b409SSimon J. Gerraty  * be called, to provide the IV for GCM computation.
382*0957b409SSimon J. Gerraty  *
383*0957b409SSimon J. Gerraty  * \param ctx    GCM context structure.
384*0957b409SSimon J. Gerraty  * \param bctx   block cipher context (already initialised with secret key).
385*0957b409SSimon J. Gerraty  * \param gh     GHASH implementation.
386*0957b409SSimon J. Gerraty  */
387*0957b409SSimon J. Gerraty void br_gcm_init(br_gcm_context *ctx,
388*0957b409SSimon J. Gerraty 	const br_block_ctr_class **bctx, br_ghash gh);
389*0957b409SSimon J. Gerraty 
390*0957b409SSimon J. Gerraty /**
391*0957b409SSimon J. Gerraty  * \brief Reset a GCM context.
392*0957b409SSimon J. Gerraty  *
393*0957b409SSimon J. Gerraty  * This function resets an already initialised GCM context for a new
394*0957b409SSimon J. Gerraty  * computation run. Implementations and keys are conserved. This function
395*0957b409SSimon J. Gerraty  * can be called at any time; it cancels any ongoing GCM computation that
396*0957b409SSimon J. Gerraty  * uses the provided context structure.
397*0957b409SSimon J. Gerraty  *
398*0957b409SSimon J. Gerraty  * The provided IV is a _nonce_. It is critical to GCM security that IV
399*0957b409SSimon J. Gerraty  * values are not repeated for the same encryption key. IV can have
400*0957b409SSimon J. Gerraty  * arbitrary length (up to 2^64-1 bits), but the "normal" length is
401*0957b409SSimon J. Gerraty  * 96 bits (12 bytes).
402*0957b409SSimon J. Gerraty  *
403*0957b409SSimon J. Gerraty  * \param ctx   GCM context structure.
404*0957b409SSimon J. Gerraty  * \param iv    GCM nonce to use.
405*0957b409SSimon J. Gerraty  * \param len   GCM nonce length (in bytes).
406*0957b409SSimon J. Gerraty  */
407*0957b409SSimon J. Gerraty void br_gcm_reset(br_gcm_context *ctx, const void *iv, size_t len);
408*0957b409SSimon J. Gerraty 
409*0957b409SSimon J. Gerraty /**
410*0957b409SSimon J. Gerraty  * \brief Inject additional authenticated data into GCM.
411*0957b409SSimon J. Gerraty  *
412*0957b409SSimon J. Gerraty  * The provided data is injected into a running GCM computation. Additional
413*0957b409SSimon J. Gerraty  * data must be injected _before_ the call to `br_gcm_flip()`.
414*0957b409SSimon J. Gerraty  * Additional data can be injected in several chunks of arbitrary length;
415*0957b409SSimon J. Gerraty  * the maximum total size of additional authenticated data is 2^64-1
416*0957b409SSimon J. Gerraty  * bits.
417*0957b409SSimon J. Gerraty  *
418*0957b409SSimon J. Gerraty  * \param ctx    GCM context structure.
419*0957b409SSimon J. Gerraty  * \param data   pointer to additional authenticated data.
420*0957b409SSimon J. Gerraty  * \param len    length of additional authenticated data (in bytes).
421*0957b409SSimon J. Gerraty  */
422*0957b409SSimon J. Gerraty void br_gcm_aad_inject(br_gcm_context *ctx, const void *data, size_t len);
423*0957b409SSimon J. Gerraty 
424*0957b409SSimon J. Gerraty /**
425*0957b409SSimon J. Gerraty  * \brief Finish injection of additional authenticated data into GCM.
426*0957b409SSimon J. Gerraty  *
427*0957b409SSimon J. Gerraty  * This function MUST be called before beginning the actual encryption
428*0957b409SSimon J. Gerraty  * or decryption (with `br_gcm_run()`), even if no additional authenticated
429*0957b409SSimon J. Gerraty  * data was injected. No additional authenticated data may be injected
430*0957b409SSimon J. Gerraty  * after this function call.
431*0957b409SSimon J. Gerraty  *
432*0957b409SSimon J. Gerraty  * \param ctx   GCM context structure.
433*0957b409SSimon J. Gerraty  */
434*0957b409SSimon J. Gerraty void br_gcm_flip(br_gcm_context *ctx);
435*0957b409SSimon J. Gerraty 
436*0957b409SSimon J. Gerraty /**
437*0957b409SSimon J. Gerraty  * \brief Encrypt or decrypt some data with GCM.
438*0957b409SSimon J. Gerraty  *
439*0957b409SSimon J. Gerraty  * Data encryption or decryption can be done after `br_gcm_flip()`
440*0957b409SSimon J. Gerraty  * has been called on the context. If `encrypt` is non-zero, then the
441*0957b409SSimon J. Gerraty  * provided data shall be plaintext, and it is encrypted in place.
442*0957b409SSimon J. Gerraty  * Otherwise, the data shall be ciphertext, and it is decrypted in place.
443*0957b409SSimon J. Gerraty  *
444*0957b409SSimon J. Gerraty  * Data may be provided in several chunks of arbitrary length. The maximum
445*0957b409SSimon J. Gerraty  * total length for data is 2^39-256 bits, i.e. about 65 gigabytes.
446*0957b409SSimon J. Gerraty  *
447*0957b409SSimon J. Gerraty  * \param ctx       GCM context structure.
448*0957b409SSimon J. Gerraty  * \param encrypt   non-zero for encryption, zero for decryption.
449*0957b409SSimon J. Gerraty  * \param data      data to encrypt or decrypt.
450*0957b409SSimon J. Gerraty  * \param len       data length (in bytes).
451*0957b409SSimon J. Gerraty  */
452*0957b409SSimon J. Gerraty void br_gcm_run(br_gcm_context *ctx, int encrypt, void *data, size_t len);
453*0957b409SSimon J. Gerraty 
454*0957b409SSimon J. Gerraty /**
455*0957b409SSimon J. Gerraty  * \brief Compute GCM authentication tag.
456*0957b409SSimon J. Gerraty  *
457*0957b409SSimon J. Gerraty  * Compute the GCM authentication tag. The tag is a 16-byte value which
458*0957b409SSimon J. Gerraty  * is written in the provided `tag` buffer. This call terminates the
459*0957b409SSimon J. Gerraty  * GCM run: no data may be processed with that GCM context afterwards,
460*0957b409SSimon J. Gerraty  * until `br_gcm_reset()` is called to initiate a new GCM run.
461*0957b409SSimon J. Gerraty  *
462*0957b409SSimon J. Gerraty  * The tag value must normally be sent along with the encrypted data.
463*0957b409SSimon J. Gerraty  * When decrypting, the tag value must be recomputed and compared with
464*0957b409SSimon J. Gerraty  * the received tag: if the two tag values differ, then either the tag
465*0957b409SSimon J. Gerraty  * or the encrypted data was altered in transit. As an alternative to
466*0957b409SSimon J. Gerraty  * this function, the `br_gcm_check_tag()` function can be used to
467*0957b409SSimon J. Gerraty  * compute and check the tag value.
468*0957b409SSimon J. Gerraty  *
469*0957b409SSimon J. Gerraty  * \param ctx   GCM context structure.
470*0957b409SSimon J. Gerraty  * \param tag   destination buffer for the tag (16 bytes).
471*0957b409SSimon J. Gerraty  */
472*0957b409SSimon J. Gerraty void br_gcm_get_tag(br_gcm_context *ctx, void *tag);
473*0957b409SSimon J. Gerraty 
474*0957b409SSimon J. Gerraty /**
475*0957b409SSimon J. Gerraty  * \brief Compute and check GCM authentication tag.
476*0957b409SSimon J. Gerraty  *
477*0957b409SSimon J. Gerraty  * This function is an alternative to `br_gcm_get_tag()`, normally used
478*0957b409SSimon J. Gerraty  * on the receiving end (i.e. when decrypting value). The tag value is
479*0957b409SSimon J. Gerraty  * recomputed and compared with the provided tag value. If they match, 1
480*0957b409SSimon J. Gerraty  * is returned; on mismatch, 0 is returned. A returned value of 0 means
481*0957b409SSimon J. Gerraty  * that the data or the tag was altered in transit, normally leading to
482*0957b409SSimon J. Gerraty  * wholesale rejection of the complete message.
483*0957b409SSimon J. Gerraty  *
484*0957b409SSimon J. Gerraty  * \param ctx   GCM context structure.
485*0957b409SSimon J. Gerraty  * \param tag   tag value to compare with (16 bytes).
486*0957b409SSimon J. Gerraty  * \return  1 on success (exact match of tag value), 0 otherwise.
487*0957b409SSimon J. Gerraty  */
488*0957b409SSimon J. Gerraty uint32_t br_gcm_check_tag(br_gcm_context *ctx, const void *tag);
489*0957b409SSimon J. Gerraty 
490*0957b409SSimon J. Gerraty /**
491*0957b409SSimon J. Gerraty  * \brief Compute GCM authentication tag (with truncation).
492*0957b409SSimon J. Gerraty  *
493*0957b409SSimon J. Gerraty  * This function is similar to `br_gcm_get_tag()`, except that it allows
494*0957b409SSimon J. Gerraty  * the tag to be truncated to a smaller length. The intended tag length
495*0957b409SSimon J. Gerraty  * is provided as `len` (in bytes); it MUST be no more than 16, but
496*0957b409SSimon J. Gerraty  * it may be smaller. Note that decreasing tag length mechanically makes
497*0957b409SSimon J. Gerraty  * forgeries easier; NIST SP 800-38D specifies that the tag length shall
498*0957b409SSimon J. Gerraty  * lie between 12 and 16 bytes (inclusive), but may be truncated down to
499*0957b409SSimon J. Gerraty  * 4 or 8 bytes, for specific applications that can tolerate it. It must
500*0957b409SSimon J. Gerraty  * also be noted that successful forgeries leak information on the
501*0957b409SSimon J. Gerraty  * authentication key, making subsequent forgeries easier. Therefore,
502*0957b409SSimon J. Gerraty  * tag truncation, and in particular truncation to sizes lower than 12
503*0957b409SSimon J. Gerraty  * bytes, shall be envisioned only with great care.
504*0957b409SSimon J. Gerraty  *
505*0957b409SSimon J. Gerraty  * The tag is written in the provided `tag` buffer. This call terminates
506*0957b409SSimon J. Gerraty  * the GCM run: no data may be processed with that GCM context
507*0957b409SSimon J. Gerraty  * afterwards, until `br_gcm_reset()` is called to initiate a new GCM
508*0957b409SSimon J. Gerraty  * run.
509*0957b409SSimon J. Gerraty  *
510*0957b409SSimon J. Gerraty  * The tag value must normally be sent along with the encrypted data.
511*0957b409SSimon J. Gerraty  * When decrypting, the tag value must be recomputed and compared with
512*0957b409SSimon J. Gerraty  * the received tag: if the two tag values differ, then either the tag
513*0957b409SSimon J. Gerraty  * or the encrypted data was altered in transit. As an alternative to
514*0957b409SSimon J. Gerraty  * this function, the `br_gcm_check_tag_trunc()` function can be used to
515*0957b409SSimon J. Gerraty  * compute and check the tag value.
516*0957b409SSimon J. Gerraty  *
517*0957b409SSimon J. Gerraty  * \param ctx   GCM context structure.
518*0957b409SSimon J. Gerraty  * \param tag   destination buffer for the tag.
519*0957b409SSimon J. Gerraty  * \param len   tag length (16 bytes or less).
520*0957b409SSimon J. Gerraty  */
521*0957b409SSimon J. Gerraty void br_gcm_get_tag_trunc(br_gcm_context *ctx, void *tag, size_t len);
522*0957b409SSimon J. Gerraty 
523*0957b409SSimon J. Gerraty /**
524*0957b409SSimon J. Gerraty  * \brief Compute and check GCM authentication tag (with truncation).
525*0957b409SSimon J. Gerraty  *
526*0957b409SSimon J. Gerraty  * This function is an alternative to `br_gcm_get_tag_trunc()`, normally used
527*0957b409SSimon J. Gerraty  * on the receiving end (i.e. when decrypting value). The tag value is
528*0957b409SSimon J. Gerraty  * recomputed and compared with the provided tag value. If they match, 1
529*0957b409SSimon J. Gerraty  * is returned; on mismatch, 0 is returned. A returned value of 0 means
530*0957b409SSimon J. Gerraty  * that the data or the tag was altered in transit, normally leading to
531*0957b409SSimon J. Gerraty  * wholesale rejection of the complete message.
532*0957b409SSimon J. Gerraty  *
533*0957b409SSimon J. Gerraty  * Tag length MUST be 16 bytes or less. The normal GCM tag length is 16
534*0957b409SSimon J. Gerraty  * bytes. See `br_check_tag_trunc()` for some discussion on the potential
535*0957b409SSimon J. Gerraty  * perils of truncating authentication tags.
536*0957b409SSimon J. Gerraty  *
537*0957b409SSimon J. Gerraty  * \param ctx   GCM context structure.
538*0957b409SSimon J. Gerraty  * \param tag   tag value to compare with.
539*0957b409SSimon J. Gerraty  * \param len   tag length (in bytes).
540*0957b409SSimon J. Gerraty  * \return  1 on success (exact match of tag value), 0 otherwise.
541*0957b409SSimon J. Gerraty  */
542*0957b409SSimon J. Gerraty uint32_t br_gcm_check_tag_trunc(br_gcm_context *ctx,
543*0957b409SSimon J. Gerraty 	const void *tag, size_t len);
544*0957b409SSimon J. Gerraty 
545*0957b409SSimon J. Gerraty /**
546*0957b409SSimon J. Gerraty  * \brief Class instance for GCM.
547*0957b409SSimon J. Gerraty  */
548*0957b409SSimon J. Gerraty extern const br_aead_class br_gcm_vtable;
549*0957b409SSimon J. Gerraty 
550*0957b409SSimon J. Gerraty /**
551*0957b409SSimon J. Gerraty  * \brief Context structure for EAX.
552*0957b409SSimon J. Gerraty  *
553*0957b409SSimon J. Gerraty  * EAX is an AEAD mode that combines a block cipher in CTR mode with
554*0957b409SSimon J. Gerraty  * CBC-MAC using the same block cipher and the same key, to provide
555*0957b409SSimon J. Gerraty  * authenticated encryption:
556*0957b409SSimon J. Gerraty  *
557*0957b409SSimon J. Gerraty  *   - Any block cipher with 16-byte blocks can be used with EAX
558*0957b409SSimon J. Gerraty  *     (technically, other block sizes are defined as well, but this
559*0957b409SSimon J. Gerraty  *     is not implemented by these functions; shorter blocks also
560*0957b409SSimon J. Gerraty  *     imply numerous security issues).
561*0957b409SSimon J. Gerraty  *
562*0957b409SSimon J. Gerraty  *   - The nonce can have any length, as long as nonce values are
563*0957b409SSimon J. Gerraty  *     not reused (thus, if nonces are randomly selected, the nonce
564*0957b409SSimon J. Gerraty  *     size should be such that reuse probability is negligible).
565*0957b409SSimon J. Gerraty  *
566*0957b409SSimon J. Gerraty  *   - Additional authenticated data length is unlimited.
567*0957b409SSimon J. Gerraty  *
568*0957b409SSimon J. Gerraty  *   - Message length is unlimited.
569*0957b409SSimon J. Gerraty  *
570*0957b409SSimon J. Gerraty  *   - The authentication tag has length 16 bytes.
571*0957b409SSimon J. Gerraty  *
572*0957b409SSimon J. Gerraty  * The EAX initialisation function receives as parameter an
573*0957b409SSimon J. Gerraty  * _initialised_ block cipher implementation context, with the secret
574*0957b409SSimon J. Gerraty  * key already set. A pointer to that context will be kept within the
575*0957b409SSimon J. Gerraty  * EAX context structure. It is up to the caller to allocate and
576*0957b409SSimon J. Gerraty  * initialise that block cipher context.
577*0957b409SSimon J. Gerraty  */
578*0957b409SSimon J. Gerraty typedef struct {
579*0957b409SSimon J. Gerraty 	/** \brief Pointer to vtable for this context. */
580*0957b409SSimon J. Gerraty 	const br_aead_class *vtable;
581*0957b409SSimon J. Gerraty 
582*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
583*0957b409SSimon J. Gerraty 	const br_block_ctrcbc_class **bctx;
584*0957b409SSimon J. Gerraty 	unsigned char L2[16];
585*0957b409SSimon J. Gerraty 	unsigned char L4[16];
586*0957b409SSimon J. Gerraty 	unsigned char nonce[16];
587*0957b409SSimon J. Gerraty 	unsigned char head[16];
588*0957b409SSimon J. Gerraty 	unsigned char ctr[16];
589*0957b409SSimon J. Gerraty 	unsigned char cbcmac[16];
590*0957b409SSimon J. Gerraty 	unsigned char buf[16];
591*0957b409SSimon J. Gerraty 	size_t ptr;
592*0957b409SSimon J. Gerraty #endif
593*0957b409SSimon J. Gerraty } br_eax_context;
594*0957b409SSimon J. Gerraty 
595*0957b409SSimon J. Gerraty /**
596*0957b409SSimon J. Gerraty  * \brief EAX captured state.
597*0957b409SSimon J. Gerraty  *
598*0957b409SSimon J. Gerraty  * Some internal values computed by EAX may be captured at various
599*0957b409SSimon J. Gerraty  * points, and reused for another EAX run with the same secret key,
600*0957b409SSimon J. Gerraty  * for lower per-message overhead. Captured values do not depend on
601*0957b409SSimon J. Gerraty  * the nonce.
602*0957b409SSimon J. Gerraty  */
603*0957b409SSimon J. Gerraty typedef struct {
604*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
605*0957b409SSimon J. Gerraty 	unsigned char st[3][16];
606*0957b409SSimon J. Gerraty #endif
607*0957b409SSimon J. Gerraty } br_eax_state;
608*0957b409SSimon J. Gerraty 
609*0957b409SSimon J. Gerraty /**
610*0957b409SSimon J. Gerraty  * \brief Initialize an EAX context.
611*0957b409SSimon J. Gerraty  *
612*0957b409SSimon J. Gerraty  * A block cipher implementation, with its initialised context
613*0957b409SSimon J. Gerraty  * structure, is provided. The block cipher MUST use 16-byte blocks in
614*0957b409SSimon J. Gerraty  * CTR + CBC-MAC mode, and its secret key MUST have been already set in
615*0957b409SSimon J. Gerraty  * the provided context. The parameters are linked in the EAX context.
616*0957b409SSimon J. Gerraty  *
617*0957b409SSimon J. Gerraty  * After this function has been called, the `br_eax_reset()` function must
618*0957b409SSimon J. Gerraty  * be called, to provide the nonce for EAX computation.
619*0957b409SSimon J. Gerraty  *
620*0957b409SSimon J. Gerraty  * \param ctx    EAX context structure.
621*0957b409SSimon J. Gerraty  * \param bctx   block cipher context (already initialised with secret key).
622*0957b409SSimon J. Gerraty  */
623*0957b409SSimon J. Gerraty void br_eax_init(br_eax_context *ctx, const br_block_ctrcbc_class **bctx);
624*0957b409SSimon J. Gerraty 
625*0957b409SSimon J. Gerraty /**
626*0957b409SSimon J. Gerraty  * \brief Capture pre-AAD state.
627*0957b409SSimon J. Gerraty  *
628*0957b409SSimon J. Gerraty  * This function precomputes key-dependent data, and stores it in the
629*0957b409SSimon J. Gerraty  * provided `st` structure. This structure should then be used with
630*0957b409SSimon J. Gerraty  * `br_eax_reset_pre_aad()`, or updated with `br_eax_get_aad_mac()`
631*0957b409SSimon J. Gerraty  * and then used with `br_eax_reset_post_aad()`.
632*0957b409SSimon J. Gerraty  *
633*0957b409SSimon J. Gerraty  * The EAX context structure is unmodified by this call.
634*0957b409SSimon J. Gerraty  *
635*0957b409SSimon J. Gerraty  * \param ctx   EAX context structure.
636*0957b409SSimon J. Gerraty  * \param st    recipient for captured state.
637*0957b409SSimon J. Gerraty  */
638*0957b409SSimon J. Gerraty void br_eax_capture(const br_eax_context *ctx, br_eax_state *st);
639*0957b409SSimon J. Gerraty 
640*0957b409SSimon J. Gerraty /**
641*0957b409SSimon J. Gerraty  * \brief Reset an EAX context.
642*0957b409SSimon J. Gerraty  *
643*0957b409SSimon J. Gerraty  * This function resets an already initialised EAX context for a new
644*0957b409SSimon J. Gerraty  * computation run. Implementations and keys are conserved. This function
645*0957b409SSimon J. Gerraty  * can be called at any time; it cancels any ongoing EAX computation that
646*0957b409SSimon J. Gerraty  * uses the provided context structure.
647*0957b409SSimon J. Gerraty  *
648*0957b409SSimon J. Gerraty  * It is critical to EAX security that nonce values are not repeated for
649*0957b409SSimon J. Gerraty  * the same encryption key. Nonces can have arbitrary length. If nonces
650*0957b409SSimon J. Gerraty  * are randomly generated, then a nonce length of at least 128 bits (16
651*0957b409SSimon J. Gerraty  * bytes) is recommended, to make nonce reuse probability sufficiently
652*0957b409SSimon J. Gerraty  * low.
653*0957b409SSimon J. Gerraty  *
654*0957b409SSimon J. Gerraty  * \param ctx     EAX context structure.
655*0957b409SSimon J. Gerraty  * \param nonce   EAX nonce to use.
656*0957b409SSimon J. Gerraty  * \param len     EAX nonce length (in bytes).
657*0957b409SSimon J. Gerraty  */
658*0957b409SSimon J. Gerraty void br_eax_reset(br_eax_context *ctx, const void *nonce, size_t len);
659*0957b409SSimon J. Gerraty 
660*0957b409SSimon J. Gerraty /**
661*0957b409SSimon J. Gerraty  * \brief Reset an EAX context with a pre-AAD captured state.
662*0957b409SSimon J. Gerraty  *
663*0957b409SSimon J. Gerraty  * This function is an alternative to `br_eax_reset()`, that reuses a
664*0957b409SSimon J. Gerraty  * previously captured state structure for lower per-message overhead.
665*0957b409SSimon J. Gerraty  * The state should have been populated with `br_eax_capture_state()`
666*0957b409SSimon J. Gerraty  * but not updated with `br_eax_get_aad_mac()`.
667*0957b409SSimon J. Gerraty  *
668*0957b409SSimon J. Gerraty  * After this function is called, additional authenticated data MUST
669*0957b409SSimon J. Gerraty  * be injected. At least one byte of additional authenticated data
670*0957b409SSimon J. Gerraty  * MUST be provided with `br_eax_aad_inject()`; computation result will
671*0957b409SSimon J. Gerraty  * be incorrect if `br_eax_flip()` is called right away.
672*0957b409SSimon J. Gerraty  *
673*0957b409SSimon J. Gerraty  * After injection of the AAD and call to `br_eax_flip()`, at least
674*0957b409SSimon J. Gerraty  * one message byte must be provided. Empty messages are not supported
675*0957b409SSimon J. Gerraty  * with this reset mode.
676*0957b409SSimon J. Gerraty  *
677*0957b409SSimon J. Gerraty  * \param ctx     EAX context structure.
678*0957b409SSimon J. Gerraty  * \param st      pre-AAD captured state.
679*0957b409SSimon J. Gerraty  * \param nonce   EAX nonce to use.
680*0957b409SSimon J. Gerraty  * \param len     EAX nonce length (in bytes).
681*0957b409SSimon J. Gerraty  */
682*0957b409SSimon J. Gerraty void br_eax_reset_pre_aad(br_eax_context *ctx, const br_eax_state *st,
683*0957b409SSimon J. Gerraty 	const void *nonce, size_t len);
684*0957b409SSimon J. Gerraty 
685*0957b409SSimon J. Gerraty /**
686*0957b409SSimon J. Gerraty  * \brief Reset an EAX context with a post-AAD captured state.
687*0957b409SSimon J. Gerraty  *
688*0957b409SSimon J. Gerraty  * This function is an alternative to `br_eax_reset()`, that reuses a
689*0957b409SSimon J. Gerraty  * previously captured state structure for lower per-message overhead.
690*0957b409SSimon J. Gerraty  * The state should have been populated with `br_eax_capture_state()`
691*0957b409SSimon J. Gerraty  * and then updated with `br_eax_get_aad_mac()`.
692*0957b409SSimon J. Gerraty  *
693*0957b409SSimon J. Gerraty  * After this function is called, message data MUST be injected. The
694*0957b409SSimon J. Gerraty  * `br_eax_flip()` function MUST NOT be called. At least one byte of
695*0957b409SSimon J. Gerraty  * message data MUST be provided with `br_eax_run()`; empty messages
696*0957b409SSimon J. Gerraty  * are not supported with this reset mode.
697*0957b409SSimon J. Gerraty  *
698*0957b409SSimon J. Gerraty  * \param ctx     EAX context structure.
699*0957b409SSimon J. Gerraty  * \param st      post-AAD captured state.
700*0957b409SSimon J. Gerraty  * \param nonce   EAX nonce to use.
701*0957b409SSimon J. Gerraty  * \param len     EAX nonce length (in bytes).
702*0957b409SSimon J. Gerraty  */
703*0957b409SSimon J. Gerraty void br_eax_reset_post_aad(br_eax_context *ctx, const br_eax_state *st,
704*0957b409SSimon J. Gerraty 	const void *nonce, size_t len);
705*0957b409SSimon J. Gerraty 
706*0957b409SSimon J. Gerraty /**
707*0957b409SSimon J. Gerraty  * \brief Inject additional authenticated data into EAX.
708*0957b409SSimon J. Gerraty  *
709*0957b409SSimon J. Gerraty  * The provided data is injected into a running EAX computation. Additional
710*0957b409SSimon J. Gerraty  * data must be injected _before_ the call to `br_eax_flip()`.
711*0957b409SSimon J. Gerraty  * Additional data can be injected in several chunks of arbitrary length;
712*0957b409SSimon J. Gerraty  * the total amount of additional authenticated data is unlimited.
713*0957b409SSimon J. Gerraty  *
714*0957b409SSimon J. Gerraty  * \param ctx    EAX context structure.
715*0957b409SSimon J. Gerraty  * \param data   pointer to additional authenticated data.
716*0957b409SSimon J. Gerraty  * \param len    length of additional authenticated data (in bytes).
717*0957b409SSimon J. Gerraty  */
718*0957b409SSimon J. Gerraty void br_eax_aad_inject(br_eax_context *ctx, const void *data, size_t len);
719*0957b409SSimon J. Gerraty 
720*0957b409SSimon J. Gerraty /**
721*0957b409SSimon J. Gerraty  * \brief Finish injection of additional authenticated data into EAX.
722*0957b409SSimon J. Gerraty  *
723*0957b409SSimon J. Gerraty  * This function MUST be called before beginning the actual encryption
724*0957b409SSimon J. Gerraty  * or decryption (with `br_eax_run()`), even if no additional authenticated
725*0957b409SSimon J. Gerraty  * data was injected. No additional authenticated data may be injected
726*0957b409SSimon J. Gerraty  * after this function call.
727*0957b409SSimon J. Gerraty  *
728*0957b409SSimon J. Gerraty  * \param ctx   EAX context structure.
729*0957b409SSimon J. Gerraty  */
730*0957b409SSimon J. Gerraty void br_eax_flip(br_eax_context *ctx);
731*0957b409SSimon J. Gerraty 
732*0957b409SSimon J. Gerraty /**
733*0957b409SSimon J. Gerraty  * \brief Obtain a copy of the MAC on additional authenticated data.
734*0957b409SSimon J. Gerraty  *
735*0957b409SSimon J. Gerraty  * This function may be called only after `br_eax_flip()`; it copies the
736*0957b409SSimon J. Gerraty  * AAD-specific MAC value into the provided state. The MAC value depends
737*0957b409SSimon J. Gerraty  * on the secret key and the additional data itself, but not on the
738*0957b409SSimon J. Gerraty  * nonce. The updated state `st` is meant to be used as parameter for a
739*0957b409SSimon J. Gerraty  * further `br_eax_reset_post_aad()` call.
740*0957b409SSimon J. Gerraty  *
741*0957b409SSimon J. Gerraty  * \param ctx   EAX context structure.
742*0957b409SSimon J. Gerraty  * \param st    captured state to update.
743*0957b409SSimon J. Gerraty  */
744*0957b409SSimon J. Gerraty static inline void
br_eax_get_aad_mac(const br_eax_context * ctx,br_eax_state * st)745*0957b409SSimon J. Gerraty br_eax_get_aad_mac(const br_eax_context *ctx, br_eax_state *st)
746*0957b409SSimon J. Gerraty {
747*0957b409SSimon J. Gerraty 	memcpy(st->st[1], ctx->head, sizeof ctx->head);
748*0957b409SSimon J. Gerraty }
749*0957b409SSimon J. Gerraty 
750*0957b409SSimon J. Gerraty /**
751*0957b409SSimon J. Gerraty  * \brief Encrypt or decrypt some data with EAX.
752*0957b409SSimon J. Gerraty  *
753*0957b409SSimon J. Gerraty  * Data encryption or decryption can be done after `br_eax_flip()`
754*0957b409SSimon J. Gerraty  * has been called on the context. If `encrypt` is non-zero, then the
755*0957b409SSimon J. Gerraty  * provided data shall be plaintext, and it is encrypted in place.
756*0957b409SSimon J. Gerraty  * Otherwise, the data shall be ciphertext, and it is decrypted in place.
757*0957b409SSimon J. Gerraty  *
758*0957b409SSimon J. Gerraty  * Data may be provided in several chunks of arbitrary length.
759*0957b409SSimon J. Gerraty  *
760*0957b409SSimon J. Gerraty  * \param ctx       EAX context structure.
761*0957b409SSimon J. Gerraty  * \param encrypt   non-zero for encryption, zero for decryption.
762*0957b409SSimon J. Gerraty  * \param data      data to encrypt or decrypt.
763*0957b409SSimon J. Gerraty  * \param len       data length (in bytes).
764*0957b409SSimon J. Gerraty  */
765*0957b409SSimon J. Gerraty void br_eax_run(br_eax_context *ctx, int encrypt, void *data, size_t len);
766*0957b409SSimon J. Gerraty 
767*0957b409SSimon J. Gerraty /**
768*0957b409SSimon J. Gerraty  * \brief Compute EAX authentication tag.
769*0957b409SSimon J. Gerraty  *
770*0957b409SSimon J. Gerraty  * Compute the EAX authentication tag. The tag is a 16-byte value which
771*0957b409SSimon J. Gerraty  * is written in the provided `tag` buffer. This call terminates the
772*0957b409SSimon J. Gerraty  * EAX run: no data may be processed with that EAX context afterwards,
773*0957b409SSimon J. Gerraty  * until `br_eax_reset()` is called to initiate a new EAX run.
774*0957b409SSimon J. Gerraty  *
775*0957b409SSimon J. Gerraty  * The tag value must normally be sent along with the encrypted data.
776*0957b409SSimon J. Gerraty  * When decrypting, the tag value must be recomputed and compared with
777*0957b409SSimon J. Gerraty  * the received tag: if the two tag values differ, then either the tag
778*0957b409SSimon J. Gerraty  * or the encrypted data was altered in transit. As an alternative to
779*0957b409SSimon J. Gerraty  * this function, the `br_eax_check_tag()` function can be used to
780*0957b409SSimon J. Gerraty  * compute and check the tag value.
781*0957b409SSimon J. Gerraty  *
782*0957b409SSimon J. Gerraty  * \param ctx   EAX context structure.
783*0957b409SSimon J. Gerraty  * \param tag   destination buffer for the tag (16 bytes).
784*0957b409SSimon J. Gerraty  */
785*0957b409SSimon J. Gerraty void br_eax_get_tag(br_eax_context *ctx, void *tag);
786*0957b409SSimon J. Gerraty 
787*0957b409SSimon J. Gerraty /**
788*0957b409SSimon J. Gerraty  * \brief Compute and check EAX authentication tag.
789*0957b409SSimon J. Gerraty  *
790*0957b409SSimon J. Gerraty  * This function is an alternative to `br_eax_get_tag()`, normally used
791*0957b409SSimon J. Gerraty  * on the receiving end (i.e. when decrypting value). The tag value is
792*0957b409SSimon J. Gerraty  * recomputed and compared with the provided tag value. If they match, 1
793*0957b409SSimon J. Gerraty  * is returned; on mismatch, 0 is returned. A returned value of 0 means
794*0957b409SSimon J. Gerraty  * that the data or the tag was altered in transit, normally leading to
795*0957b409SSimon J. Gerraty  * wholesale rejection of the complete message.
796*0957b409SSimon J. Gerraty  *
797*0957b409SSimon J. Gerraty  * \param ctx   EAX context structure.
798*0957b409SSimon J. Gerraty  * \param tag   tag value to compare with (16 bytes).
799*0957b409SSimon J. Gerraty  * \return  1 on success (exact match of tag value), 0 otherwise.
800*0957b409SSimon J. Gerraty  */
801*0957b409SSimon J. Gerraty uint32_t br_eax_check_tag(br_eax_context *ctx, const void *tag);
802*0957b409SSimon J. Gerraty 
803*0957b409SSimon J. Gerraty /**
804*0957b409SSimon J. Gerraty  * \brief Compute EAX authentication tag (with truncation).
805*0957b409SSimon J. Gerraty  *
806*0957b409SSimon J. Gerraty  * This function is similar to `br_eax_get_tag()`, except that it allows
807*0957b409SSimon J. Gerraty  * the tag to be truncated to a smaller length. The intended tag length
808*0957b409SSimon J. Gerraty  * is provided as `len` (in bytes); it MUST be no more than 16, but
809*0957b409SSimon J. Gerraty  * it may be smaller. Note that decreasing tag length mechanically makes
810*0957b409SSimon J. Gerraty  * forgeries easier; NIST SP 800-38D specifies that the tag length shall
811*0957b409SSimon J. Gerraty  * lie between 12 and 16 bytes (inclusive), but may be truncated down to
812*0957b409SSimon J. Gerraty  * 4 or 8 bytes, for specific applications that can tolerate it. It must
813*0957b409SSimon J. Gerraty  * also be noted that successful forgeries leak information on the
814*0957b409SSimon J. Gerraty  * authentication key, making subsequent forgeries easier. Therefore,
815*0957b409SSimon J. Gerraty  * tag truncation, and in particular truncation to sizes lower than 12
816*0957b409SSimon J. Gerraty  * bytes, shall be envisioned only with great care.
817*0957b409SSimon J. Gerraty  *
818*0957b409SSimon J. Gerraty  * The tag is written in the provided `tag` buffer. This call terminates
819*0957b409SSimon J. Gerraty  * the EAX run: no data may be processed with that EAX context
820*0957b409SSimon J. Gerraty  * afterwards, until `br_eax_reset()` is called to initiate a new EAX
821*0957b409SSimon J. Gerraty  * run.
822*0957b409SSimon J. Gerraty  *
823*0957b409SSimon J. Gerraty  * The tag value must normally be sent along with the encrypted data.
824*0957b409SSimon J. Gerraty  * When decrypting, the tag value must be recomputed and compared with
825*0957b409SSimon J. Gerraty  * the received tag: if the two tag values differ, then either the tag
826*0957b409SSimon J. Gerraty  * or the encrypted data was altered in transit. As an alternative to
827*0957b409SSimon J. Gerraty  * this function, the `br_eax_check_tag_trunc()` function can be used to
828*0957b409SSimon J. Gerraty  * compute and check the tag value.
829*0957b409SSimon J. Gerraty  *
830*0957b409SSimon J. Gerraty  * \param ctx   EAX context structure.
831*0957b409SSimon J. Gerraty  * \param tag   destination buffer for the tag.
832*0957b409SSimon J. Gerraty  * \param len   tag length (16 bytes or less).
833*0957b409SSimon J. Gerraty  */
834*0957b409SSimon J. Gerraty void br_eax_get_tag_trunc(br_eax_context *ctx, void *tag, size_t len);
835*0957b409SSimon J. Gerraty 
836*0957b409SSimon J. Gerraty /**
837*0957b409SSimon J. Gerraty  * \brief Compute and check EAX authentication tag (with truncation).
838*0957b409SSimon J. Gerraty  *
839*0957b409SSimon J. Gerraty  * This function is an alternative to `br_eax_get_tag_trunc()`, normally used
840*0957b409SSimon J. Gerraty  * on the receiving end (i.e. when decrypting value). The tag value is
841*0957b409SSimon J. Gerraty  * recomputed and compared with the provided tag value. If they match, 1
842*0957b409SSimon J. Gerraty  * is returned; on mismatch, 0 is returned. A returned value of 0 means
843*0957b409SSimon J. Gerraty  * that the data or the tag was altered in transit, normally leading to
844*0957b409SSimon J. Gerraty  * wholesale rejection of the complete message.
845*0957b409SSimon J. Gerraty  *
846*0957b409SSimon J. Gerraty  * Tag length MUST be 16 bytes or less. The normal EAX tag length is 16
847*0957b409SSimon J. Gerraty  * bytes. See `br_check_tag_trunc()` for some discussion on the potential
848*0957b409SSimon J. Gerraty  * perils of truncating authentication tags.
849*0957b409SSimon J. Gerraty  *
850*0957b409SSimon J. Gerraty  * \param ctx   EAX context structure.
851*0957b409SSimon J. Gerraty  * \param tag   tag value to compare with.
852*0957b409SSimon J. Gerraty  * \param len   tag length (in bytes).
853*0957b409SSimon J. Gerraty  * \return  1 on success (exact match of tag value), 0 otherwise.
854*0957b409SSimon J. Gerraty  */
855*0957b409SSimon J. Gerraty uint32_t br_eax_check_tag_trunc(br_eax_context *ctx,
856*0957b409SSimon J. Gerraty 	const void *tag, size_t len);
857*0957b409SSimon J. Gerraty 
858*0957b409SSimon J. Gerraty /**
859*0957b409SSimon J. Gerraty  * \brief Class instance for EAX.
860*0957b409SSimon J. Gerraty  */
861*0957b409SSimon J. Gerraty extern const br_aead_class br_eax_vtable;
862*0957b409SSimon J. Gerraty 
863*0957b409SSimon J. Gerraty /**
864*0957b409SSimon J. Gerraty  * \brief Context structure for CCM.
865*0957b409SSimon J. Gerraty  *
866*0957b409SSimon J. Gerraty  * CCM is an AEAD mode that combines a block cipher in CTR mode with
867*0957b409SSimon J. Gerraty  * CBC-MAC using the same block cipher and the same key, to provide
868*0957b409SSimon J. Gerraty  * authenticated encryption:
869*0957b409SSimon J. Gerraty  *
870*0957b409SSimon J. Gerraty  *   - Any block cipher with 16-byte blocks can be used with CCM
871*0957b409SSimon J. Gerraty  *     (technically, other block sizes are defined as well, but this
872*0957b409SSimon J. Gerraty  *     is not implemented by these functions; shorter blocks also
873*0957b409SSimon J. Gerraty  *     imply numerous security issues).
874*0957b409SSimon J. Gerraty  *
875*0957b409SSimon J. Gerraty  *   - The authentication tag length, and plaintext length, MUST be
876*0957b409SSimon J. Gerraty  *     known when starting processing data. Plaintext and ciphertext
877*0957b409SSimon J. Gerraty  *     can still be provided by chunks, but the total size must match
878*0957b409SSimon J. Gerraty  *     the value provided upon initialisation.
879*0957b409SSimon J. Gerraty  *
880*0957b409SSimon J. Gerraty  *   - The nonce length is constrained between 7 and 13 bytes (inclusive).
881*0957b409SSimon J. Gerraty  *     Furthermore, the plaintext length, when encoded, must fit over
882*0957b409SSimon J. Gerraty  *     15-nonceLen bytes; thus, if the nonce has length 13 bytes, then
883*0957b409SSimon J. Gerraty  *     the plaintext length cannot exceed 65535 bytes.
884*0957b409SSimon J. Gerraty  *
885*0957b409SSimon J. Gerraty  *   - Additional authenticated data length is practically unlimited
886*0957b409SSimon J. Gerraty  *     (formal limit is at 2^64 bytes).
887*0957b409SSimon J. Gerraty  *
888*0957b409SSimon J. Gerraty  *   - The authentication tag has length 4 to 16 bytes (even values only).
889*0957b409SSimon J. Gerraty  *
890*0957b409SSimon J. Gerraty  * The CCM initialisation function receives as parameter an
891*0957b409SSimon J. Gerraty  * _initialised_ block cipher implementation context, with the secret
892*0957b409SSimon J. Gerraty  * key already set. A pointer to that context will be kept within the
893*0957b409SSimon J. Gerraty  * CCM context structure. It is up to the caller to allocate and
894*0957b409SSimon J. Gerraty  * initialise that block cipher context.
895*0957b409SSimon J. Gerraty  */
896*0957b409SSimon J. Gerraty typedef struct {
897*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
898*0957b409SSimon J. Gerraty 	const br_block_ctrcbc_class **bctx;
899*0957b409SSimon J. Gerraty 	unsigned char ctr[16];
900*0957b409SSimon J. Gerraty 	unsigned char cbcmac[16];
901*0957b409SSimon J. Gerraty 	unsigned char tagmask[16];
902*0957b409SSimon J. Gerraty 	unsigned char buf[16];
903*0957b409SSimon J. Gerraty 	size_t ptr;
904*0957b409SSimon J. Gerraty 	size_t tag_len;
905*0957b409SSimon J. Gerraty #endif
906*0957b409SSimon J. Gerraty } br_ccm_context;
907*0957b409SSimon J. Gerraty 
908*0957b409SSimon J. Gerraty /**
909*0957b409SSimon J. Gerraty  * \brief Initialize a CCM context.
910*0957b409SSimon J. Gerraty  *
911*0957b409SSimon J. Gerraty  * A block cipher implementation, with its initialised context
912*0957b409SSimon J. Gerraty  * structure, is provided. The block cipher MUST use 16-byte blocks in
913*0957b409SSimon J. Gerraty  * CTR + CBC-MAC mode, and its secret key MUST have been already set in
914*0957b409SSimon J. Gerraty  * the provided context. The parameters are linked in the CCM context.
915*0957b409SSimon J. Gerraty  *
916*0957b409SSimon J. Gerraty  * After this function has been called, the `br_ccm_reset()` function must
917*0957b409SSimon J. Gerraty  * be called, to provide the nonce for CCM computation.
918*0957b409SSimon J. Gerraty  *
919*0957b409SSimon J. Gerraty  * \param ctx    CCM context structure.
920*0957b409SSimon J. Gerraty  * \param bctx   block cipher context (already initialised with secret key).
921*0957b409SSimon J. Gerraty  */
922*0957b409SSimon J. Gerraty void br_ccm_init(br_ccm_context *ctx, const br_block_ctrcbc_class **bctx);
923*0957b409SSimon J. Gerraty 
924*0957b409SSimon J. Gerraty /**
925*0957b409SSimon J. Gerraty  * \brief Reset a CCM context.
926*0957b409SSimon J. Gerraty  *
927*0957b409SSimon J. Gerraty  * This function resets an already initialised CCM context for a new
928*0957b409SSimon J. Gerraty  * computation run. Implementations and keys are conserved. This function
929*0957b409SSimon J. Gerraty  * can be called at any time; it cancels any ongoing CCM computation that
930*0957b409SSimon J. Gerraty  * uses the provided context structure.
931*0957b409SSimon J. Gerraty  *
932*0957b409SSimon J. Gerraty  * The `aad_len` parameter contains the total length, in bytes, of the
933*0957b409SSimon J. Gerraty  * additional authenticated data. It may be zero. That length MUST be
934*0957b409SSimon J. Gerraty  * exact.
935*0957b409SSimon J. Gerraty  *
936*0957b409SSimon J. Gerraty  * The `data_len` parameter contains the total length, in bytes, of the
937*0957b409SSimon J. Gerraty  * data that will be injected (plaintext or ciphertext). That length MUST
938*0957b409SSimon J. Gerraty  * be exact. Moreover, that length MUST be less than 2^(8*(15-nonce_len)).
939*0957b409SSimon J. Gerraty  *
940*0957b409SSimon J. Gerraty  * The nonce length (`nonce_len`), in bytes, must be in the 7..13 range
941*0957b409SSimon J. Gerraty  * (inclusive).
942*0957b409SSimon J. Gerraty  *
943*0957b409SSimon J. Gerraty  * The tag length (`tag_len`), in bytes, must be in the 4..16 range, and
944*0957b409SSimon J. Gerraty  * be an even integer. Short tags mechanically allow for higher forgery
945*0957b409SSimon J. Gerraty  * probabilities; hence, tag sizes smaller than 12 bytes shall be used only
946*0957b409SSimon J. Gerraty  * with care.
947*0957b409SSimon J. Gerraty  *
948*0957b409SSimon J. Gerraty  * It is critical to CCM security that nonce values are not repeated for
949*0957b409SSimon J. Gerraty  * the same encryption key. Random generation of nonces is not generally
950*0957b409SSimon J. Gerraty  * recommended, due to the relatively small maximum nonce value.
951*0957b409SSimon J. Gerraty  *
952*0957b409SSimon J. Gerraty  * Returned value is 1 on success, 0 on error. An error is reported if
953*0957b409SSimon J. Gerraty  * the tag or nonce length is out of range, or if the
954*0957b409SSimon J. Gerraty  * plaintext/ciphertext length cannot be encoded with the specified
955*0957b409SSimon J. Gerraty  * nonce length.
956*0957b409SSimon J. Gerraty  *
957*0957b409SSimon J. Gerraty  * \param ctx         CCM context structure.
958*0957b409SSimon J. Gerraty  * \param nonce       CCM nonce to use.
959*0957b409SSimon J. Gerraty  * \param nonce_len   CCM nonce length (in bytes, 7 to 13).
960*0957b409SSimon J. Gerraty  * \param aad_len     additional authenticated data length (in bytes).
961*0957b409SSimon J. Gerraty  * \param data_len    plaintext/ciphertext length (in bytes).
962*0957b409SSimon J. Gerraty  * \param tag_len     tag length (in bytes).
963*0957b409SSimon J. Gerraty  * \return  1 on success, 0 on error.
964*0957b409SSimon J. Gerraty  */
965*0957b409SSimon J. Gerraty int br_ccm_reset(br_ccm_context *ctx, const void *nonce, size_t nonce_len,
966*0957b409SSimon J. Gerraty 	uint64_t aad_len, uint64_t data_len, size_t tag_len);
967*0957b409SSimon J. Gerraty 
968*0957b409SSimon J. Gerraty /**
969*0957b409SSimon J. Gerraty  * \brief Inject additional authenticated data into CCM.
970*0957b409SSimon J. Gerraty  *
971*0957b409SSimon J. Gerraty  * The provided data is injected into a running CCM computation. Additional
972*0957b409SSimon J. Gerraty  * data must be injected _before_ the call to `br_ccm_flip()`.
973*0957b409SSimon J. Gerraty  * Additional data can be injected in several chunks of arbitrary length,
974*0957b409SSimon J. Gerraty  * but the total amount MUST exactly match the value which was provided
975*0957b409SSimon J. Gerraty  * to `br_ccm_reset()`.
976*0957b409SSimon J. Gerraty  *
977*0957b409SSimon J. Gerraty  * \param ctx    CCM context structure.
978*0957b409SSimon J. Gerraty  * \param data   pointer to additional authenticated data.
979*0957b409SSimon J. Gerraty  * \param len    length of additional authenticated data (in bytes).
980*0957b409SSimon J. Gerraty  */
981*0957b409SSimon J. Gerraty void br_ccm_aad_inject(br_ccm_context *ctx, const void *data, size_t len);
982*0957b409SSimon J. Gerraty 
983*0957b409SSimon J. Gerraty /**
984*0957b409SSimon J. Gerraty  * \brief Finish injection of additional authenticated data into CCM.
985*0957b409SSimon J. Gerraty  *
986*0957b409SSimon J. Gerraty  * This function MUST be called before beginning the actual encryption
987*0957b409SSimon J. Gerraty  * or decryption (with `br_ccm_run()`), even if no additional authenticated
988*0957b409SSimon J. Gerraty  * data was injected. No additional authenticated data may be injected
989*0957b409SSimon J. Gerraty  * after this function call.
990*0957b409SSimon J. Gerraty  *
991*0957b409SSimon J. Gerraty  * \param ctx   CCM context structure.
992*0957b409SSimon J. Gerraty  */
993*0957b409SSimon J. Gerraty void br_ccm_flip(br_ccm_context *ctx);
994*0957b409SSimon J. Gerraty 
995*0957b409SSimon J. Gerraty /**
996*0957b409SSimon J. Gerraty  * \brief Encrypt or decrypt some data with CCM.
997*0957b409SSimon J. Gerraty  *
998*0957b409SSimon J. Gerraty  * Data encryption or decryption can be done after `br_ccm_flip()`
999*0957b409SSimon J. Gerraty  * has been called on the context. If `encrypt` is non-zero, then the
1000*0957b409SSimon J. Gerraty  * provided data shall be plaintext, and it is encrypted in place.
1001*0957b409SSimon J. Gerraty  * Otherwise, the data shall be ciphertext, and it is decrypted in place.
1002*0957b409SSimon J. Gerraty  *
1003*0957b409SSimon J. Gerraty  * Data may be provided in several chunks of arbitrary length, provided
1004*0957b409SSimon J. Gerraty  * that the total length exactly matches the length provided to the
1005*0957b409SSimon J. Gerraty  * `br_ccm_reset()` call.
1006*0957b409SSimon J. Gerraty  *
1007*0957b409SSimon J. Gerraty  * \param ctx       CCM context structure.
1008*0957b409SSimon J. Gerraty  * \param encrypt   non-zero for encryption, zero for decryption.
1009*0957b409SSimon J. Gerraty  * \param data      data to encrypt or decrypt.
1010*0957b409SSimon J. Gerraty  * \param len       data length (in bytes).
1011*0957b409SSimon J. Gerraty  */
1012*0957b409SSimon J. Gerraty void br_ccm_run(br_ccm_context *ctx, int encrypt, void *data, size_t len);
1013*0957b409SSimon J. Gerraty 
1014*0957b409SSimon J. Gerraty /**
1015*0957b409SSimon J. Gerraty  * \brief Compute CCM authentication tag.
1016*0957b409SSimon J. Gerraty  *
1017*0957b409SSimon J. Gerraty  * Compute the CCM authentication tag. This call terminates the CCM
1018*0957b409SSimon J. Gerraty  * run: all data must have been injected with `br_ccm_run()` (in zero,
1019*0957b409SSimon J. Gerraty  * one or more successive calls). After this function has been called,
1020*0957b409SSimon J. Gerraty  * no more data can br processed; a `br_ccm_reset()` call is required
1021*0957b409SSimon J. Gerraty  * to start a new message.
1022*0957b409SSimon J. Gerraty  *
1023*0957b409SSimon J. Gerraty  * The tag length was provided upon context initialisation (last call
1024*0957b409SSimon J. Gerraty  * to `br_ccm_reset()`); it is returned by this function.
1025*0957b409SSimon J. Gerraty  *
1026*0957b409SSimon J. Gerraty  * The tag value must normally be sent along with the encrypted data.
1027*0957b409SSimon J. Gerraty  * When decrypting, the tag value must be recomputed and compared with
1028*0957b409SSimon J. Gerraty  * the received tag: if the two tag values differ, then either the tag
1029*0957b409SSimon J. Gerraty  * or the encrypted data was altered in transit. As an alternative to
1030*0957b409SSimon J. Gerraty  * this function, the `br_ccm_check_tag()` function can be used to
1031*0957b409SSimon J. Gerraty  * compute and check the tag value.
1032*0957b409SSimon J. Gerraty  *
1033*0957b409SSimon J. Gerraty  * \param ctx   CCM context structure.
1034*0957b409SSimon J. Gerraty  * \param tag   destination buffer for the tag (up to 16 bytes).
1035*0957b409SSimon J. Gerraty  * \return  the tag length (in bytes).
1036*0957b409SSimon J. Gerraty  */
1037*0957b409SSimon J. Gerraty size_t br_ccm_get_tag(br_ccm_context *ctx, void *tag);
1038*0957b409SSimon J. Gerraty 
1039*0957b409SSimon J. Gerraty /**
1040*0957b409SSimon J. Gerraty  * \brief Compute and check CCM authentication tag.
1041*0957b409SSimon J. Gerraty  *
1042*0957b409SSimon J. Gerraty  * This function is an alternative to `br_ccm_get_tag()`, normally used
1043*0957b409SSimon J. Gerraty  * on the receiving end (i.e. when decrypting value). The tag value is
1044*0957b409SSimon J. Gerraty  * recomputed and compared with the provided tag value. If they match, 1
1045*0957b409SSimon J. Gerraty  * is returned; on mismatch, 0 is returned. A returned value of 0 means
1046*0957b409SSimon J. Gerraty  * that the data or the tag was altered in transit, normally leading to
1047*0957b409SSimon J. Gerraty  * wholesale rejection of the complete message.
1048*0957b409SSimon J. Gerraty  *
1049*0957b409SSimon J. Gerraty  * \param ctx   CCM context structure.
1050*0957b409SSimon J. Gerraty  * \param tag   tag value to compare with (up to 16 bytes).
1051*0957b409SSimon J. Gerraty  * \return  1 on success (exact match of tag value), 0 otherwise.
1052*0957b409SSimon J. Gerraty  */
1053*0957b409SSimon J. Gerraty uint32_t br_ccm_check_tag(br_ccm_context *ctx, const void *tag);
1054*0957b409SSimon J. Gerraty 
1055*0957b409SSimon J. Gerraty #ifdef __cplusplus
1056*0957b409SSimon J. Gerraty }
1057*0957b409SSimon J. Gerraty #endif
1058*0957b409SSimon J. Gerraty 
1059*0957b409SSimon J. Gerraty #endif
1060