1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty * Copyright (c) 2016 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_HMAC_H__
26*0957b409SSimon J. Gerraty #define BR_BEARSSL_HMAC_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_hash.h"
32*0957b409SSimon J. Gerraty
33*0957b409SSimon J. Gerraty #ifdef __cplusplus
34*0957b409SSimon J. Gerraty extern "C" {
35*0957b409SSimon J. Gerraty #endif
36*0957b409SSimon J. Gerraty
37*0957b409SSimon J. Gerraty /** \file bearssl_hmac.h
38*0957b409SSimon J. Gerraty *
39*0957b409SSimon J. Gerraty * # HMAC
40*0957b409SSimon J. Gerraty *
41*0957b409SSimon J. Gerraty * HMAC is initialized with a key and an underlying hash function; it
42*0957b409SSimon J. Gerraty * then fills a "key context". That context contains the processed
43*0957b409SSimon J. Gerraty * key.
44*0957b409SSimon J. Gerraty *
45*0957b409SSimon J. Gerraty * With the key context, a HMAC context can be initialized to process
46*0957b409SSimon J. Gerraty * the input bytes and obtain the MAC output. The key context is not
47*0957b409SSimon J. Gerraty * modified during that process, and can be reused.
48*0957b409SSimon J. Gerraty *
49*0957b409SSimon J. Gerraty * IMPORTANT: HMAC shall be used only with functions that have the
50*0957b409SSimon J. Gerraty * following properties:
51*0957b409SSimon J. Gerraty *
52*0957b409SSimon J. Gerraty * - hash output size does not exceed 64 bytes;
53*0957b409SSimon J. Gerraty * - hash internal state size does not exceed 64 bytes;
54*0957b409SSimon J. Gerraty * - internal block length is a power of 2 between 16 and 256 bytes.
55*0957b409SSimon J. Gerraty */
56*0957b409SSimon J. Gerraty
57*0957b409SSimon J. Gerraty /**
58*0957b409SSimon J. Gerraty * \brief HMAC key context.
59*0957b409SSimon J. Gerraty *
60*0957b409SSimon J. Gerraty * The HMAC key context is initialised with a hash function implementation
61*0957b409SSimon J. Gerraty * and a secret key. Contents are opaque (callers should not access them
62*0957b409SSimon J. Gerraty * directly). The caller is responsible for allocating the context where
63*0957b409SSimon J. Gerraty * appropriate. Context initialisation and usage incurs no dynamic
64*0957b409SSimon J. Gerraty * allocation, so there is no release function.
65*0957b409SSimon J. Gerraty */
66*0957b409SSimon J. Gerraty typedef struct {
67*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
68*0957b409SSimon J. Gerraty const br_hash_class *dig_vtable;
69*0957b409SSimon J. Gerraty unsigned char ksi[64], kso[64];
70*0957b409SSimon J. Gerraty #endif
71*0957b409SSimon J. Gerraty } br_hmac_key_context;
72*0957b409SSimon J. Gerraty
73*0957b409SSimon J. Gerraty /**
74*0957b409SSimon J. Gerraty * \brief HMAC key context initialisation.
75*0957b409SSimon J. Gerraty *
76*0957b409SSimon J. Gerraty * Initialise the key context with the provided key, using the hash function
77*0957b409SSimon J. Gerraty * identified by `digest_vtable`. This supports arbitrary key lengths.
78*0957b409SSimon J. Gerraty *
79*0957b409SSimon J. Gerraty * \param kc HMAC key context to initialise.
80*0957b409SSimon J. Gerraty * \param digest_vtable pointer to the hash function implementation vtable.
81*0957b409SSimon J. Gerraty * \param key pointer to the HMAC secret key.
82*0957b409SSimon J. Gerraty * \param key_len HMAC secret key length (in bytes).
83*0957b409SSimon J. Gerraty */
84*0957b409SSimon J. Gerraty void br_hmac_key_init(br_hmac_key_context *kc,
85*0957b409SSimon J. Gerraty const br_hash_class *digest_vtable, const void *key, size_t key_len);
86*0957b409SSimon J. Gerraty
87*0957b409SSimon J. Gerraty /*
88*0957b409SSimon J. Gerraty * \brief Get the underlying hash function.
89*0957b409SSimon J. Gerraty *
90*0957b409SSimon J. Gerraty * This function returns a pointer to the implementation vtable of the
91*0957b409SSimon J. Gerraty * hash function used for this HMAC key context.
92*0957b409SSimon J. Gerraty *
93*0957b409SSimon J. Gerraty * \param kc HMAC key context.
94*0957b409SSimon J. Gerraty * \return the hash function implementation.
95*0957b409SSimon J. Gerraty */
br_hmac_key_get_digest(const br_hmac_key_context * kc)96*0957b409SSimon J. Gerraty static inline const br_hash_class *br_hmac_key_get_digest(
97*0957b409SSimon J. Gerraty const br_hmac_key_context *kc)
98*0957b409SSimon J. Gerraty {
99*0957b409SSimon J. Gerraty return kc->dig_vtable;
100*0957b409SSimon J. Gerraty }
101*0957b409SSimon J. Gerraty
102*0957b409SSimon J. Gerraty /**
103*0957b409SSimon J. Gerraty * \brief HMAC computation context.
104*0957b409SSimon J. Gerraty *
105*0957b409SSimon J. Gerraty * The HMAC computation context maintains the state for a single HMAC
106*0957b409SSimon J. Gerraty * computation. It is modified as input bytes are injected. The context
107*0957b409SSimon J. Gerraty * is caller-allocated and has no release function since it does not
108*0957b409SSimon J. Gerraty * dynamically allocate external resources. Its contents are opaque.
109*0957b409SSimon J. Gerraty */
110*0957b409SSimon J. Gerraty typedef struct {
111*0957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
112*0957b409SSimon J. Gerraty br_hash_compat_context dig;
113*0957b409SSimon J. Gerraty unsigned char kso[64];
114*0957b409SSimon J. Gerraty size_t out_len;
115*0957b409SSimon J. Gerraty #endif
116*0957b409SSimon J. Gerraty } br_hmac_context;
117*0957b409SSimon J. Gerraty
118*0957b409SSimon J. Gerraty /**
119*0957b409SSimon J. Gerraty * \brief HMAC computation initialisation.
120*0957b409SSimon J. Gerraty *
121*0957b409SSimon J. Gerraty * Initialise a HMAC context with a key context. The key context is
122*0957b409SSimon J. Gerraty * unmodified. Relevant data from the key context is immediately copied;
123*0957b409SSimon J. Gerraty * the key context can thus be independently reused, modified or released
124*0957b409SSimon J. Gerraty * without impacting this HMAC computation.
125*0957b409SSimon J. Gerraty *
126*0957b409SSimon J. Gerraty * An explicit output length can be specified; the actual output length
127*0957b409SSimon J. Gerraty * will be the minimum of that value and the natural HMAC output length.
128*0957b409SSimon J. Gerraty * If `out_len` is 0, then the natural HMAC output length is selected. The
129*0957b409SSimon J. Gerraty * "natural output length" is the output length of the underlying hash
130*0957b409SSimon J. Gerraty * function.
131*0957b409SSimon J. Gerraty *
132*0957b409SSimon J. Gerraty * \param ctx HMAC context to initialise.
133*0957b409SSimon J. Gerraty * \param kc HMAC key context (already initialised with the key).
134*0957b409SSimon J. Gerraty * \param out_len HMAC output length (0 to select "natural length").
135*0957b409SSimon J. Gerraty */
136*0957b409SSimon J. Gerraty void br_hmac_init(br_hmac_context *ctx,
137*0957b409SSimon J. Gerraty const br_hmac_key_context *kc, size_t out_len);
138*0957b409SSimon J. Gerraty
139*0957b409SSimon J. Gerraty /**
140*0957b409SSimon J. Gerraty * \brief Get the HMAC output size.
141*0957b409SSimon J. Gerraty *
142*0957b409SSimon J. Gerraty * The HMAC output size is the number of bytes that will actually be
143*0957b409SSimon J. Gerraty * produced with `br_hmac_out()` with the provided context. This function
144*0957b409SSimon J. Gerraty * MUST NOT be called on a non-initialised HMAC computation context.
145*0957b409SSimon J. Gerraty * The returned value is the minimum of the HMAC natural length (output
146*0957b409SSimon J. Gerraty * size of the underlying hash function) and the `out_len` parameter which
147*0957b409SSimon J. Gerraty * was used with the last `br_hmac_init()` call on that context (if the
148*0957b409SSimon J. Gerraty * initialisation `out_len` parameter was 0, then this function will
149*0957b409SSimon J. Gerraty * return the HMAC natural length).
150*0957b409SSimon J. Gerraty *
151*0957b409SSimon J. Gerraty * \param ctx the (already initialised) HMAC computation context.
152*0957b409SSimon J. Gerraty * \return the HMAC actual output size.
153*0957b409SSimon J. Gerraty */
154*0957b409SSimon J. Gerraty static inline size_t
br_hmac_size(br_hmac_context * ctx)155*0957b409SSimon J. Gerraty br_hmac_size(br_hmac_context *ctx)
156*0957b409SSimon J. Gerraty {
157*0957b409SSimon J. Gerraty return ctx->out_len;
158*0957b409SSimon J. Gerraty }
159*0957b409SSimon J. Gerraty
160*0957b409SSimon J. Gerraty /*
161*0957b409SSimon J. Gerraty * \brief Get the underlying hash function.
162*0957b409SSimon J. Gerraty *
163*0957b409SSimon J. Gerraty * This function returns a pointer to the implementation vtable of the
164*0957b409SSimon J. Gerraty * hash function used for this HMAC context.
165*0957b409SSimon J. Gerraty *
166*0957b409SSimon J. Gerraty * \param hc HMAC context.
167*0957b409SSimon J. Gerraty * \return the hash function implementation.
168*0957b409SSimon J. Gerraty */
br_hmac_get_digest(const br_hmac_context * hc)169*0957b409SSimon J. Gerraty static inline const br_hash_class *br_hmac_get_digest(
170*0957b409SSimon J. Gerraty const br_hmac_context *hc)
171*0957b409SSimon J. Gerraty {
172*0957b409SSimon J. Gerraty return hc->dig.vtable;
173*0957b409SSimon J. Gerraty }
174*0957b409SSimon J. Gerraty
175*0957b409SSimon J. Gerraty /**
176*0957b409SSimon J. Gerraty * \brief Inject some bytes in HMAC.
177*0957b409SSimon J. Gerraty *
178*0957b409SSimon J. Gerraty * The provided `len` bytes are injected as extra input in the HMAC
179*0957b409SSimon J. Gerraty * computation incarnated by the `ctx` HMAC context. It is acceptable
180*0957b409SSimon J. Gerraty * that `len` is zero, in which case `data` is ignored (and may be
181*0957b409SSimon J. Gerraty * `NULL`) and this function does nothing.
182*0957b409SSimon J. Gerraty */
183*0957b409SSimon J. Gerraty void br_hmac_update(br_hmac_context *ctx, const void *data, size_t len);
184*0957b409SSimon J. Gerraty
185*0957b409SSimon J. Gerraty /**
186*0957b409SSimon J. Gerraty * \brief Compute the HMAC output.
187*0957b409SSimon J. Gerraty *
188*0957b409SSimon J. Gerraty * The destination buffer MUST be large enough to accommodate the result;
189*0957b409SSimon J. Gerraty * its length is at most the "natural length" of HMAC (i.e. the output
190*0957b409SSimon J. Gerraty * length of the underlying hash function). The context is NOT modified;
191*0957b409SSimon J. Gerraty * further bytes may be processed. Thus, "partial HMAC" values can be
192*0957b409SSimon J. Gerraty * efficiently obtained.
193*0957b409SSimon J. Gerraty *
194*0957b409SSimon J. Gerraty * Returned value is the output length (in bytes).
195*0957b409SSimon J. Gerraty *
196*0957b409SSimon J. Gerraty * \param ctx HMAC computation context.
197*0957b409SSimon J. Gerraty * \param out destination buffer for the HMAC output.
198*0957b409SSimon J. Gerraty * \return the produced value length (in bytes).
199*0957b409SSimon J. Gerraty */
200*0957b409SSimon J. Gerraty size_t br_hmac_out(const br_hmac_context *ctx, void *out);
201*0957b409SSimon J. Gerraty
202*0957b409SSimon J. Gerraty /**
203*0957b409SSimon J. Gerraty * \brief Constant-time HMAC computation.
204*0957b409SSimon J. Gerraty *
205*0957b409SSimon J. Gerraty * This function compute the HMAC output in constant time. Some extra
206*0957b409SSimon J. Gerraty * input bytes are processed, then the output is computed. The extra
207*0957b409SSimon J. Gerraty * input consists in the `len` bytes pointed to by `data`. The `len`
208*0957b409SSimon J. Gerraty * parameter must lie between `min_len` and `max_len` (inclusive);
209*0957b409SSimon J. Gerraty * `max_len` bytes are actually read from `data`. Computing time (and
210*0957b409SSimon J. Gerraty * memory access pattern) will not depend upon the data byte contents or
211*0957b409SSimon J. Gerraty * the value of `len`.
212*0957b409SSimon J. Gerraty *
213*0957b409SSimon J. Gerraty * The output is written in the `out` buffer, that MUST be large enough
214*0957b409SSimon J. Gerraty * to receive it.
215*0957b409SSimon J. Gerraty *
216*0957b409SSimon J. Gerraty * The difference `max_len - min_len` MUST be less than 2<sup>30</sup>
217*0957b409SSimon J. Gerraty * (i.e. about one gigabyte).
218*0957b409SSimon J. Gerraty *
219*0957b409SSimon J. Gerraty * This function computes the output properly only if the underlying
220*0957b409SSimon J. Gerraty * hash function uses MD padding (i.e. MD5, SHA-1, SHA-224, SHA-256,
221*0957b409SSimon J. Gerraty * SHA-384 or SHA-512).
222*0957b409SSimon J. Gerraty *
223*0957b409SSimon J. Gerraty * The provided context is NOT modified.
224*0957b409SSimon J. Gerraty *
225*0957b409SSimon J. Gerraty * \param ctx the (already initialised) HMAC computation context.
226*0957b409SSimon J. Gerraty * \param data the extra input bytes.
227*0957b409SSimon J. Gerraty * \param len the extra input length (in bytes).
228*0957b409SSimon J. Gerraty * \param min_len minimum extra input length (in bytes).
229*0957b409SSimon J. Gerraty * \param max_len maximum extra input length (in bytes).
230*0957b409SSimon J. Gerraty * \param out destination buffer for the HMAC output.
231*0957b409SSimon J. Gerraty * \return the produced value length (in bytes).
232*0957b409SSimon J. Gerraty */
233*0957b409SSimon J. Gerraty size_t br_hmac_outCT(const br_hmac_context *ctx,
234*0957b409SSimon J. Gerraty const void *data, size_t len, size_t min_len, size_t max_len,
235*0957b409SSimon J. Gerraty void *out);
236*0957b409SSimon J. Gerraty
237*0957b409SSimon J. Gerraty #ifdef __cplusplus
238*0957b409SSimon J. Gerraty }
239*0957b409SSimon J. Gerraty #endif
240*0957b409SSimon J. Gerraty
241*0957b409SSimon J. Gerraty #endif
242