Fixed behaviour in case of rejected renegotiation.
[BearSSL] / inc / bearssl_hmac.h
1 /*
2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #ifndef BR_BEARSSL_HMAC_H__
26 #define BR_BEARSSL_HMAC_H__
27
28 #include <stddef.h>
29 #include <stdint.h>
30
31 #include "bearssl_hash.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /** \file bearssl_hmac.h
38 *
39 * # HMAC
40 *
41 * HMAC is initialized with a key and an underlying hash function; it
42 * then fills a "key context". That context contains the processed
43 * key.
44 *
45 * With the key context, a HMAC context can be initialized to process
46 * the input bytes and obtain the MAC output. The key context is not
47 * modified during that process, and can be reused.
48 *
49 * IMPORTANT: HMAC shall be used only with functions that have the
50 * following properties:
51 *
52 * - hash output size does not exceed 64 bytes;
53 * - hash internal state size does not exceed 64 bytes;
54 * - internal block length is a power of 2 between 16 and 256 bytes.
55 */
56
57 /**
58 * \brief HMAC key context.
59 *
60 * The HMAC key context is initialised with a hash function implementation
61 * and a secret key. Contents are opaque (callers should not access them
62 * directly). The caller is responsible for allocating the context where
63 * appropriate. Context initialisation and usage incurs no dynamic
64 * allocation, so there is no release function.
65 */
66 typedef struct {
67 #ifndef BR_DOXYGEN_IGNORE
68 const br_hash_class *dig_vtable;
69 unsigned char ksi[64], kso[64];
70 #endif
71 } br_hmac_key_context;
72
73 /**
74 * \brief HMAC key context initialisation.
75 *
76 * Initialise the key context with the provided key, using the hash function
77 * identified by `digest_vtable`. This supports arbitrary key lengths.
78 *
79 * \param kc HMAC key context to initialise.
80 * \param digest_vtable pointer to the hash function implementation vtable.
81 * \param key pointer to the HMAC secret key.
82 * \param key_len HMAC secret key length (in bytes).
83 */
84 void br_hmac_key_init(br_hmac_key_context *kc,
85 const br_hash_class *digest_vtable, const void *key, size_t key_len);
86
87 /**
88 * \brief HMAC computation context.
89 *
90 * The HMAC computation context maintains the state for a single HMAC
91 * computation. It is modified as input bytes are injected. The context
92 * is caller-allocated and has no release function since it does not
93 * dynamically allocate external resources. Its contents are opaque.
94 */
95 typedef struct {
96 #ifndef BR_DOXYGEN_IGNORE
97 br_hash_compat_context dig;
98 unsigned char kso[64];
99 size_t out_len;
100 #endif
101 } br_hmac_context;
102
103 /**
104 * \brief HMAC computation initialisation.
105 *
106 * Initialise a HMAC context with a key context. The key context is
107 * unmodified. Relevant data from the key context is immediately copied;
108 * the key context can thus be independently reused, modified or released
109 * without impacting this HMAC computation.
110 *
111 * An explicit output length can be specified; the actual output length
112 * will be the minimum of that value and the natural HMAC output length.
113 * If `out_len` is 0, then the natural HMAC output length is selected. The
114 * "natural output length" is the output length of the underlying hash
115 * function.
116 *
117 * \param ctx HMAC context to initialise.
118 * \param kc HMAC key context (already initialised with the key).
119 * \param out_len HMAC output length (0 to select "natural length").
120 */
121 void br_hmac_init(br_hmac_context *ctx,
122 const br_hmac_key_context *kc, size_t out_len);
123
124 /**
125 * \brief Get the HMAC output size.
126 *
127 * The HMAC output size is the number of bytes that will actually be
128 * produced with `br_hmac_out()` with the provided context. This function
129 * MUST NOT be called on a non-initialised HMAC computation context.
130 * The returned value is the minimum of the HMAC natural length (output
131 * size of the underlying hash function) and the `out_len` parameter which
132 * was used with the last `br_hmac_init()` call on that context (if the
133 * initialisation `out_len` parameter was 0, then this function will
134 * return the HMAC natural length).
135 *
136 * \param ctx the (already initialised) HMAC computation context.
137 * \return the HMAC actual output size.
138 */
139 static inline size_t
140 br_hmac_size(br_hmac_context *ctx)
141 {
142 return ctx->out_len;
143 }
144
145 /**
146 * \brief Inject some bytes in HMAC.
147 *
148 * The provided `len` bytes are injected as extra input in the HMAC
149 * computation incarnated by the `ctx` HMAC context. It is acceptable
150 * that `len` is zero, in which case `data` is ignored (and may be
151 * `NULL`) and this function does nothing.
152 */
153 void br_hmac_update(br_hmac_context *ctx, const void *data, size_t len);
154
155 /**
156 * \brief Compute the HMAC output.
157 *
158 * The destination buffer MUST be large enough to accomodate the result;
159 * its length is at most the "natural length" of HMAC (i.e. the output
160 * length of the underlying hash function). The context is NOT modified;
161 * further bytes may be processed. Thus, "partial HMAC" values can be
162 * efficiently obtained.
163 *
164 * Returned value is the output length (in bytes).
165 *
166 * \param ctx HMAC computation context.
167 * \param out destination buffer for the HMAC output.
168 * \return the produced value length (in bytes).
169 */
170 size_t br_hmac_out(const br_hmac_context *ctx, void *out);
171
172 /**
173 * \brief Constant-time HMAC computation.
174 *
175 * This function compute the HMAC output in constant time. Some extra
176 * input bytes are processed, then the output is computed. The extra
177 * input consists in the `len` bytes pointed to by `data`. The `len`
178 * parameter must lie between `min_len` and `max_len` (inclusive);
179 * `max_len` bytes are actually read from `data`. Computing time (and
180 * memory access pattern) will not depend upon the data byte contents or
181 * the value of `len`.
182 *
183 * The output is written in the `out` buffer, that MUST be large enough
184 * to receive it.
185 *
186 * The difference `max_len - min_len` MUST be less than 2<sup>30</sup>
187 * (i.e. about one gigabyte).
188 *
189 * This function computes the output properly only if the underlying
190 * hash function uses MD padding (i.e. MD5, SHA-1, SHA-224, SHA-256,
191 * SHA-384 or SHA-512).
192 *
193 * The provided context is NOT modified.
194 *
195 * \param ctx the (already initialised) HMAC computation context.
196 * \param data the extra input bytes.
197 * \param len the extra input length (in bytes).
198 * \param min_len minimum extra input length (in bytes).
199 * \param max_len maximum extra input length (in bytes).
200 * \param out destination buffer for the HMAC output.
201 * \return the produced value length (in bytes).
202 */
203 size_t br_hmac_outCT(const br_hmac_context *ctx,
204 const void *data, size_t len, size_t min_len, size_t max_len,
205 void *out);
206
207 #ifdef __cplusplus
208 }
209 #endif
210
211 #endif