dbcc74cd1939bf7b2a140c883e52f70db2d2dd15
[BearSSL] / inc / bearssl_hash.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_HASH_H__
26 #define BR_BEARSSL_HASH_H__
27
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <string.h>
31
32 /** \file bearssl_hash.h
33 *
34 * # Hash Functions
35 *
36 * This file documents the API for hash functions.
37 *
38 * Implemented hash functions are MD5, SHA-1, SHA-224, SHA-256, SHA-384
39 * and SHA-512; these are the _standard hash functions_ (as specified in
40 * TLS). Also provided are MD5+SHA-1 (an aggregate hash function that
41 * computes both MD5 and SHA-1 on its input, and provides a 36-byte
42 * output), a multi-hasher system that computes some or all of the
43 * standard hash functions on the same input, and some GHASH
44 * implementations (GHASH is the sort-of keyed hash function used in GCM
45 * encryption mode).
46 *
47 * For each standard hash function (and also MD5+SHA-1), two similar API
48 * are provided: one consists in direct, named function calls, while the
49 * other uses function pointers through a vtable. The vtable incarnates
50 * object-oriented programming. An introduction on the OOP concept used
51 * here can be read on the BearSSL Web site:<br />
52 * &nbsp;&nbsp;&nbsp;[https://www.bearssl.org/oop.html](https://www.bearssl.org/oop.html)
53 */
54
55 /*
56 * Hash Functions
57 * --------------
58 *
59 * For hash function 'xxx', the following elements are defined:
60 *
61 * br_xxx_vtable
62 * An externally defined instance of br_hash_class.
63 *
64 * br_xxx_SIZE
65 * A macro that evaluates to the output size (in bytes) of the
66 * hash function.
67 *
68 * br_xxx_ID
69 * A macro that evaluates to a symbolic identifier for the hash
70 * function. Such identifiers are used with HMAC and signature
71 * algorithm implementations.
72 * NOTE: the numerical value of these identifiers MUST match the
73 * constants for hash function identification in TLS 1.2 (see RFC
74 * 5246, section 7.4.1.4.1). These are values 1 to 6, for MD5,
75 * SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512, respectively.
76 *
77 * br_xxx_context
78 * Context for an ongoing computation. It is allocated by the
79 * caller, and a pointer to it is passed to all functions. A
80 * context contains no interior pointer, so it can be moved around
81 * and cloned (with a simple memcpy() or equivalent) in order to
82 * capture the function state at some point. Computations that use
83 * distinct context structures are independent of each other. The
84 * first field of br_xxx_context is always a pointer to the
85 * br_xxx_vtable structure; br_xxx_init() sets that pointer.
86 *
87 * br_xxx_init(br_xxx_context *ctx)
88 * Initialize the provided context. Previous contents of the structure
89 * are ignored. This calls resets the context to the start of a new
90 * hash computation.
91 *
92 * br_xxx_update(br_xxx_context *ctx, const void *data, size_t len)
93 * Add some more bytes to the hash computation represented by the
94 * provided context.
95 *
96 * br_xxx_out(const br_xxx_context *ctx, void *out)
97 * Complete the hash computation and write the result in the provided
98 * buffer. The output buffer MUST be large enough to accomodate the
99 * result. The context is NOT modified by this operation, so this
100 * function can be used to get a "partial hash" while still keeping
101 * the possibility of adding more bytes to the input.
102 *
103 * br_xxx_state(const br_xxx_context *ctx, void *out)
104 * Get a copy of the "current state" for the computation so far. For
105 * MD functions (MD5, SHA-1, SHA-2 family), this is the running state
106 * resulting from the processing of the last complete input block.
107 * Returned value is the current input length (in bytes).
108 *
109 * br_xxx_set_state(br_xxx_context *ctx, const void *stb, uint64_t count)
110 * Set the internal state to the provided values. The 'stb' and 'count'
111 * values shall match that which was obtained from br_xxx_state(). This
112 * restores the hash state only if the state values were at an
113 * appropriate block boundary. This does NOT set the 'vtable' pointer
114 * in the context.
115 *
116 * Context structures can be discarded without any explicit deallocation.
117 * Hash function implementations are purely software and don't reserve
118 * any resources outside of the context structure itself.
119 *
120 * Implemented hash functions are:
121 *
122 * Function Name Output length State length
123 *
124 * MD5 md5 16 16
125 * SHA-1 sha1 20 20
126 * SHA-224 sha224 28 32
127 * SHA-256 sha256 32 32
128 * SHA-384 sha384 48 64
129 * SHA-512 sha512 64 64
130 * MD5+SHA-1 md5sha1 36 36
131 *
132 * (MD5+SHA-1 is the concatenation of MD5 and SHA-1 computed over the
133 * same input; in the implementation, the internal data buffer is
134 * shared, thus making it more memory-efficient than separate MD5 and
135 * SHA-1. It can be useful in implementing SSL 3.0, TLS 1.0 and TLS
136 * 1.1.)
137 *
138 *
139 * An object-oriented API is also available: the first field of the
140 * context is a pointer to a br_hash_class structure, that has the
141 * following contents:
142 *
143 * context_size total size of the required context structure
144 * desc descriptor (see below)
145 * init context initialization or reset (function pointer)
146 * update process some more bytes (function pointer)
147 * out get hash output so far (function pointer)
148 * state get copy of internal state (function pointer)
149 * set_state reset the internal state (function pointer)
150 *
151 * The descriptor is a combination of the following elements:
152 * bits 0 to 7 hash algorithm identifier
153 * bits 8 to 14 hash output size (in bytes)
154 * bits 15 to 22 hash internal state size (in bytes)
155 * bits 23 to 26 log (base 2) of hash internal block size (in bytes)
156 * bit 28 1 if using MD padding, 0 otherwise
157 * bit 29 1 if MD padding uses a 128-bit bit length, 0 otherwise
158 * bit 30 1 if MD padding is big-endian, 0 otherwise
159 *
160 * For function 'xxx', the br_xxx_init() function sets the first field
161 * to a pointer to the relevant br_hash_class instance (i.e.
162 * br_xxx_vtable).
163 *
164 * Users of this object-oriented API may make the following assumptions:
165 * Hash output size is no more than 64 bytes.
166 * Hash internal state size is no more than 64 bytes.
167 * Internal block size is a power of two, no less than 2^4 and no more
168 * than 2^8.
169 * For functions that do not have an internal block size that is a
170 * power of 2, the relevant element is 0.
171 */
172
173 /**
174 * \brief Class type for hash function implementations.
175 *
176 * A `br_hash_class` instance references the methods implementing a hash
177 * function. Constant instances of this structure are defined for each
178 * implemented hash function. Such instances are also called "vtables".
179 *
180 * Vtables are used to support object-oriented programming, as
181 * described on [the BearSSL Web site](https://www.bearssl.org/oop.html).
182 */
183 typedef struct br_hash_class_ br_hash_class;
184 struct br_hash_class_ {
185 /**
186 * \brief Size (in bytes) of the context structure appropriate for
187 * computing this hash function.
188 */
189 size_t context_size;
190
191 /**
192 * \brief Descriptor word that contains information about the hash
193 * function.
194 *
195 * For each word `xxx` described below, use `BR_HASHDESC_xxx_OFF`
196 * and `BR_HASHDESC_xxx_MASK` to access the specific value, as
197 * follows:
198 *
199 * (hf->desc >> BR_HASHDESC_xxx_OFF) & BR_HASHDESC_xxx_MASK
200 *
201 * The defined elements are:
202 *
203 * - `ID`: the symbolic identifier for the function, as defined
204 * in [TLS](https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1)
205 * (MD5 = 1, SHA-1 = 2,...).
206 *
207 * - `OUT`: hash output size, in bytes.
208 *
209 * - `STATE`: internal running state size, in bytes.
210 *
211 * - `LBLEN`: base-2 logarithm for the internal block size, as
212 * defined for HMAC processing (this is 6 for MD5, SHA-1, SHA-224
213 * and SHA-256, since these functions use 64-byte blocks; for
214 * SHA-384 and SHA-512, this is 7, corresponding to their
215 * 128-byte blocks).
216 *
217 * The descriptor may contain a few other flags.
218 */
219 uint32_t desc;
220
221 /**
222 * \brief Initialisation method.
223 *
224 * This method takes as parameter a pointer to a context area,
225 * that it initialises. The first field of the context is set
226 * to this vtable; other elements are initialised for a new hash
227 * computation.
228 *
229 * \param ctx pointer to (the first field of) the context.
230 */
231 void (*init)(const br_hash_class **ctx);
232
233 /**
234 * \brief Data injection method.
235 *
236 * The `len` bytes starting at address `data` are injected into
237 * the running hash computation incarnated by the specified
238 * context. The context is updated accordingly. It is allowed
239 * to have `len == 0`, in which case `data` is ignored (and could
240 * be `NULL`), and nothing happens.
241 * on the input data.
242 *
243 * \param ctx pointer to (the first field of) the context.
244 * \param data pointer to the first data byte to inject.
245 * \param len number of bytes to inject.
246 */
247 void (*update)(const br_hash_class **ctx, const void *data, size_t len);
248
249 /**
250 * \brief Produce hash output.
251 *
252 * The hash output corresponding to all data bytes injected in the
253 * context since the last `init()` call is computed, and written
254 * in the buffer pointed to by `dst`. The hash output size depends
255 * on the implemented hash function (e.g. 16 bytes for MD5).
256 * The context is _not_ modified by this call, so further bytes
257 * may be afterwards injected to continue the current computation.
258 *
259 * \param ctx pointer to (the first field of) the context.
260 * \param dst destination buffer for the hash output.
261 */
262 void (*out)(const br_hash_class *const *ctx, void *dst);
263
264 /**
265 * \brief Get running state.
266 *
267 * This method saves the current running state into the `dst`
268 * buffer. What constitutes the "running state" depends on the
269 * hash function; for Merkle-Damgård hash functions (like
270 * MD5 or SHA-1), this is the output obtained after processing
271 * each block. The number of bytes injected so far is returned.
272 * The context is not modified by this call.
273 *
274 * \param ctx pointer to (the first field of) the context.
275 * \param dst destination buffer for the state.
276 * \return the injected total byte length.
277 */
278 uint64_t (*state)(const br_hash_class *const *ctx, void *dst);
279
280 /**
281 * \brief Set running state.
282 *
283 * This methods replaces the running state for the function.
284 *
285 * \param ctx pointer to (the first field of) the context.
286 * \param stb source buffer for the state.
287 * \param count injected total byte length.
288 */
289 void (*set_state)(const br_hash_class **ctx,
290 const void *stb, uint64_t count);
291 };
292
293 #ifndef BR_DOXYGEN_IGNORE
294 #define BR_HASHDESC_ID(id) ((uint32_t)(id) << BR_HASHDESC_ID_OFF)
295 #define BR_HASHDESC_ID_OFF 0
296 #define BR_HASHDESC_ID_MASK 0xFF
297
298 #define BR_HASHDESC_OUT(size) ((uint32_t)(size) << BR_HASHDESC_OUT_OFF)
299 #define BR_HASHDESC_OUT_OFF 8
300 #define BR_HASHDESC_OUT_MASK 0x7F
301
302 #define BR_HASHDESC_STATE(size) ((uint32_t)(size) << BR_HASHDESC_STATE_OFF)
303 #define BR_HASHDESC_STATE_OFF 15
304 #define BR_HASHDESC_STATE_MASK 0xFF
305
306 #define BR_HASHDESC_LBLEN(ls) ((uint32_t)(ls) << BR_HASHDESC_LBLEN_OFF)
307 #define BR_HASHDESC_LBLEN_OFF 23
308 #define BR_HASHDESC_LBLEN_MASK 0x0F
309
310 #define BR_HASHDESC_MD_PADDING ((uint32_t)1 << 28)
311 #define BR_HASHDESC_MD_PADDING_128 ((uint32_t)1 << 29)
312 #define BR_HASHDESC_MD_PADDING_BE ((uint32_t)1 << 30)
313 #endif
314
315 /*
316 * Specific hash functions.
317 *
318 * Rules for contexts:
319 * -- No interior pointer.
320 * -- No pointer to external dynamically allocated resources.
321 * -- First field is called 'vtable' and is a pointer to a
322 * const-qualified br_hash_class instance (pointer is set by init()).
323 * -- SHA-224 and SHA-256 contexts are identical.
324 * -- SHA-384 and SHA-512 contexts are identical.
325 *
326 * Thus, contexts can be moved and cloned to capture the hash function
327 * current state; and there is no need for any explicit "release" function.
328 */
329
330 /**
331 * \brief Symbolic identifier for MD5.
332 */
333 #define br_md5_ID 1
334
335 /**
336 * \brief MD5 output size (in bytes).
337 */
338 #define br_md5_SIZE 16
339
340 /**
341 * \brief Constant vtable for MD5.
342 */
343 extern const br_hash_class br_md5_vtable;
344
345 /**
346 * \brief MD5 context.
347 *
348 * First field is a pointer to the vtable; it is set by the initialisation
349 * function. Other fields are not supposed to be accessed by user code.
350 */
351 typedef struct {
352 /**
353 * \brief Pointer to vtable for this context.
354 */
355 const br_hash_class *vtable;
356 #ifndef BR_DOXYGEN_IGNORE
357 unsigned char buf[64];
358 uint64_t count;
359 uint32_t val[4];
360 #endif
361 } br_md5_context;
362
363 /**
364 * \brief MD5 context initialisation.
365 *
366 * This function initialises or resets a context for a new MD5
367 * computation. It also sets the vtable pointer.
368 *
369 * \param ctx pointer to the context structure.
370 */
371 void br_md5_init(br_md5_context *ctx);
372
373 /**
374 * \brief Inject some data bytes in a running MD5 computation.
375 *
376 * The provided context is updated with some data bytes. If the number
377 * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
378 * and may be `NULL`, and this function does nothing.
379 *
380 * \param ctx pointer to the context structure.
381 * \param data pointer to the injected data.
382 * \param len injected data length (in bytes).
383 */
384 void br_md5_update(br_md5_context *ctx, const void *data, size_t len);
385
386 /**
387 * \brief Compute MD5 output.
388 *
389 * The MD5 output for the concatenation of all bytes injected in the
390 * provided context since the last initialisation or reset call, is
391 * computed and written in the buffer pointed to by `out`. The context
392 * itself is not modified, so extra bytes may be injected afterwards
393 * to continue that computation.
394 *
395 * \param ctx pointer to the context structure.
396 * \param out destination buffer for the hash output.
397 */
398 void br_md5_out(const br_md5_context *ctx, void *out);
399
400 /**
401 * \brief Save MD5 running state.
402 *
403 * The running state for MD5 (output of the last internal block
404 * processing) is written in the buffer pointed to by `out`. The
405 * number of bytes injected since the last initialisation or reset
406 * call is returned. The context is not modified.
407 *
408 * \param ctx pointer to the context structure.
409 * \param out destination buffer for the running state.
410 * \return the injected total byte length.
411 */
412 uint64_t br_md5_state(const br_md5_context *ctx, void *out);
413
414 /**
415 * \brief Restore MD5 running state.
416 *
417 * The running state for MD5 is set to the provided values.
418 *
419 * \param ctx pointer to the context structure.
420 * \param stb source buffer for the running state.
421 * \param count the injected total byte length.
422 */
423 void br_md5_set_state(br_md5_context *ctx, const void *stb, uint64_t count);
424
425 /**
426 * \brief Symbolic identifier for SHA-1.
427 */
428 #define br_sha1_ID 2
429
430 /**
431 * \brief SHA-1 output size (in bytes).
432 */
433 #define br_sha1_SIZE 20
434
435 /**
436 * \brief Constant vtable for SHA-1.
437 */
438 extern const br_hash_class br_sha1_vtable;
439
440 /**
441 * \brief SHA-1 context.
442 *
443 * First field is a pointer to the vtable; it is set by the initialisation
444 * function. Other fields are not supposed to be accessed by user code.
445 */
446 typedef struct {
447 /**
448 * \brief Pointer to vtable for this context.
449 */
450 const br_hash_class *vtable;
451 #ifndef BR_DOXYGEN_IGNORE
452 unsigned char buf[64];
453 uint64_t count;
454 uint32_t val[5];
455 #endif
456 } br_sha1_context;
457
458 /**
459 * \brief SHA-1 context initialisation.
460 *
461 * This function initialises or resets a context for a new SHA-1
462 * computation. It also sets the vtable pointer.
463 *
464 * \param ctx pointer to the context structure.
465 */
466 void br_sha1_init(br_sha1_context *ctx);
467
468 /**
469 * \brief Inject some data bytes in a running SHA-1 computation.
470 *
471 * The provided context is updated with some data bytes. If the number
472 * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
473 * and may be `NULL`, and this function does nothing.
474 *
475 * \param ctx pointer to the context structure.
476 * \param data pointer to the injected data.
477 * \param len injected data length (in bytes).
478 */
479 void br_sha1_update(br_sha1_context *ctx, const void *data, size_t len);
480
481 /**
482 * \brief Compute SHA-1 output.
483 *
484 * The SHA-1 output for the concatenation of all bytes injected in the
485 * provided context since the last initialisation or reset call, is
486 * computed and written in the buffer pointed to by `out`. The context
487 * itself is not modified, so extra bytes may be injected afterwards
488 * to continue that computation.
489 *
490 * \param ctx pointer to the context structure.
491 * \param out destination buffer for the hash output.
492 */
493 void br_sha1_out(const br_sha1_context *ctx, void *out);
494
495 /**
496 * \brief Save SHA-1 running state.
497 *
498 * The running state for SHA-1 (output of the last internal block
499 * processing) is written in the buffer pointed to by `out`. The
500 * number of bytes injected since the last initialisation or reset
501 * call is returned. The context is not modified.
502 *
503 * \param ctx pointer to the context structure.
504 * \param out destination buffer for the running state.
505 * \return the injected total byte length.
506 */
507 uint64_t br_sha1_state(const br_sha1_context *ctx, void *out);
508
509 /**
510 * \brief Restore SHA-1 running state.
511 *
512 * The running state for SHA-1 is set to the provided values.
513 *
514 * \param ctx pointer to the context structure.
515 * \param stb source buffer for the running state.
516 * \param count the injected total byte length.
517 */
518 void br_sha1_set_state(br_sha1_context *ctx, const void *stb, uint64_t count);
519
520 /* obsolete
521 #define br_sha1_ID 2
522 #define br_sha1_SIZE 20
523 extern const br_hash_class br_sha1_vtable;
524 typedef struct {
525 const br_hash_class *vtable;
526 unsigned char buf[64];
527 uint64_t count;
528 uint32_t val[5];
529 } br_sha1_context;
530 void br_sha1_init(br_sha1_context *ctx);
531 void br_sha1_update(br_sha1_context *ctx, const void *data, size_t len);
532 void br_sha1_out(const br_sha1_context *ctx, void *out);
533 uint64_t br_sha1_state(const br_sha1_context *ctx, void *out);
534 void br_sha1_set_state(br_sha1_context *ctx, const void *stb, uint64_t count);
535 */
536
537 /**
538 * \brief Symbolic identifier for SHA-224.
539 */
540 #define br_sha224_ID 3
541
542 /**
543 * \brief SHA-224 output size (in bytes).
544 */
545 #define br_sha224_SIZE 28
546
547 /**
548 * \brief Constant vtable for SHA-224.
549 */
550 extern const br_hash_class br_sha224_vtable;
551
552 /**
553 * \brief SHA-224 context.
554 *
555 * First field is a pointer to the vtable; it is set by the initialisation
556 * function. Other fields are not supposed to be accessed by user code.
557 */
558 typedef struct {
559 /**
560 * \brief Pointer to vtable for this context.
561 */
562 const br_hash_class *vtable;
563 #ifndef BR_DOXYGEN_IGNORE
564 unsigned char buf[64];
565 uint64_t count;
566 uint32_t val[8];
567 #endif
568 } br_sha224_context;
569
570 /**
571 * \brief SHA-224 context initialisation.
572 *
573 * This function initialises or resets a context for a new SHA-224
574 * computation. It also sets the vtable pointer.
575 *
576 * \param ctx pointer to the context structure.
577 */
578 void br_sha224_init(br_sha224_context *ctx);
579
580 /**
581 * \brief Inject some data bytes in a running SHA-224 computation.
582 *
583 * The provided context is updated with some data bytes. If the number
584 * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
585 * and may be `NULL`, and this function does nothing.
586 *
587 * \param ctx pointer to the context structure.
588 * \param data pointer to the injected data.
589 * \param len injected data length (in bytes).
590 */
591 void br_sha224_update(br_sha224_context *ctx, const void *data, size_t len);
592
593 /**
594 * \brief Compute SHA-224 output.
595 *
596 * The SHA-224 output for the concatenation of all bytes injected in the
597 * provided context since the last initialisation or reset call, is
598 * computed and written in the buffer pointed to by `out`. The context
599 * itself is not modified, so extra bytes may be injected afterwards
600 * to continue that computation.
601 *
602 * \param ctx pointer to the context structure.
603 * \param out destination buffer for the hash output.
604 */
605 void br_sha224_out(const br_sha224_context *ctx, void *out);
606
607 /**
608 * \brief Save SHA-224 running state.
609 *
610 * The running state for SHA-224 (output of the last internal block
611 * processing) is written in the buffer pointed to by `out`. The
612 * number of bytes injected since the last initialisation or reset
613 * call is returned. The context is not modified.
614 *
615 * \param ctx pointer to the context structure.
616 * \param out destination buffer for the running state.
617 * \return the injected total byte length.
618 */
619 uint64_t br_sha224_state(const br_sha224_context *ctx, void *out);
620
621 /**
622 * \brief Restore SHA-224 running state.
623 *
624 * The running state for SHA-224 is set to the provided values.
625 *
626 * \param ctx pointer to the context structure.
627 * \param stb source buffer for the running state.
628 * \param count the injected total byte length.
629 */
630 void br_sha224_set_state(br_sha224_context *ctx,
631 const void *stb, uint64_t count);
632
633 /* obsolete
634 #define br_sha224_ID 3
635 #define br_sha224_SIZE 28
636 extern const br_hash_class br_sha224_vtable;
637 typedef struct {
638 const br_hash_class *vtable;
639 unsigned char buf[64];
640 uint64_t count;
641 uint32_t val[8];
642 } br_sha224_context;
643 void br_sha224_init(br_sha224_context *ctx);
644 void br_sha224_update(br_sha224_context *ctx, const void *data, size_t len);
645 void br_sha224_out(const br_sha224_context *ctx, void *out);
646 uint64_t br_sha224_state(const br_sha224_context *ctx, void *out);
647 void br_sha224_set_state(br_sha224_context *ctx,
648 const void *stb, uint64_t count);
649 */
650
651 /**
652 * \brief Symbolic identifier for SHA-256.
653 */
654 #define br_sha256_ID 4
655
656 /**
657 * \brief SHA-256 output size (in bytes).
658 */
659 #define br_sha256_SIZE 32
660
661 /**
662 * \brief Constant vtable for SHA-256.
663 */
664 extern const br_hash_class br_sha256_vtable;
665
666 #ifdef BR_DOXYGEN_IGNORE
667 /**
668 * \brief SHA-256 context.
669 *
670 * First field is a pointer to the vtable; it is set by the initialisation
671 * function. Other fields are not supposed to be accessed by user code.
672 */
673 typedef struct {
674 /**
675 * \brief Pointer to vtable for this context.
676 */
677 const br_hash_class *vtable;
678 } br_sha256_context;
679 #else
680 typedef br_sha224_context br_sha256_context;
681 #endif
682
683 /**
684 * \brief SHA-256 context initialisation.
685 *
686 * This function initialises or resets a context for a new SHA-256
687 * computation. It also sets the vtable pointer.
688 *
689 * \param ctx pointer to the context structure.
690 */
691 void br_sha256_init(br_sha256_context *ctx);
692
693 #ifdef BR_DOXYGEN_IGNORE
694 /**
695 * \brief Inject some data bytes in a running SHA-256 computation.
696 *
697 * The provided context is updated with some data bytes. If the number
698 * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
699 * and may be `NULL`, and this function does nothing.
700 *
701 * \param ctx pointer to the context structure.
702 * \param data pointer to the injected data.
703 * \param len injected data length (in bytes).
704 */
705 void br_sha256_update(br_sha256_context *ctx, const void *data, size_t len);
706 #else
707 #define br_sha256_update br_sha224_update
708 #endif
709
710 /**
711 * \brief Compute SHA-256 output.
712 *
713 * The SHA-256 output for the concatenation of all bytes injected in the
714 * provided context since the last initialisation or reset call, is
715 * computed and written in the buffer pointed to by `out`. The context
716 * itself is not modified, so extra bytes may be injected afterwards
717 * to continue that computation.
718 *
719 * \param ctx pointer to the context structure.
720 * \param out destination buffer for the hash output.
721 */
722 void br_sha256_out(const br_sha256_context *ctx, void *out);
723
724 #if BR_DOXYGEN_IGNORE
725 /**
726 * \brief Save SHA-256 running state.
727 *
728 * The running state for SHA-256 (output of the last internal block
729 * processing) is written in the buffer pointed to by `out`. The
730 * number of bytes injected since the last initialisation or reset
731 * call is returned. The context is not modified.
732 *
733 * \param ctx pointer to the context structure.
734 * \param out destination buffer for the running state.
735 * \return the injected total byte length.
736 */
737 uint64_t br_sha256_state(const br_sha256_context *ctx, void *out);
738 #else
739 #define br_sha256_state br_sha224_state
740 #endif
741
742 #if BR_DOXYGEN_IGNORE
743 /**
744 * \brief Restore SHA-256 running state.
745 *
746 * The running state for SHA-256 is set to the provided values.
747 *
748 * \param ctx pointer to the context structure.
749 * \param stb source buffer for the running state.
750 * \param count the injected total byte length.
751 */
752 void br_sha256_set_state(br_sha256_context *ctx,
753 const void *stb, uint64_t count);
754 #else
755 #define br_sha256_set_state br_sha224_set_state
756 #endif
757
758 /* obsolete
759 #define br_sha256_ID 4
760 #define br_sha256_SIZE 32
761 extern const br_hash_class br_sha256_vtable;
762 typedef br_sha224_context br_sha256_context;
763 void br_sha256_init(br_sha256_context *ctx);
764 #define br_sha256_update br_sha224_update
765 void br_sha256_out(const br_sha256_context *ctx, void *out);
766 #define br_sha256_state br_sha224_state
767 #define br_sha256_set_state br_sha224_set_state
768 */
769
770 /**
771 * \brief Symbolic identifier for SHA-384.
772 */
773 #define br_sha384_ID 5
774
775 /**
776 * \brief SHA-384 output size (in bytes).
777 */
778 #define br_sha384_SIZE 48
779
780 /**
781 * \brief Constant vtable for SHA-384.
782 */
783 extern const br_hash_class br_sha384_vtable;
784
785 /**
786 * \brief SHA-384 context.
787 *
788 * First field is a pointer to the vtable; it is set by the initialisation
789 * function. Other fields are not supposed to be accessed by user code.
790 */
791 typedef struct {
792 /**
793 * \brief Pointer to vtable for this context.
794 */
795 const br_hash_class *vtable;
796 #ifndef BR_DOXYGEN_IGNORE
797 unsigned char buf[128];
798 uint64_t count;
799 uint64_t val[8];
800 #endif
801 } br_sha384_context;
802
803 /**
804 * \brief SHA-384 context initialisation.
805 *
806 * This function initialises or resets a context for a new SHA-384
807 * computation. It also sets the vtable pointer.
808 *
809 * \param ctx pointer to the context structure.
810 */
811 void br_sha384_init(br_sha384_context *ctx);
812
813 /**
814 * \brief Inject some data bytes in a running SHA-384 computation.
815 *
816 * The provided context is updated with some data bytes. If the number
817 * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
818 * and may be `NULL`, and this function does nothing.
819 *
820 * \param ctx pointer to the context structure.
821 * \param data pointer to the injected data.
822 * \param len injected data length (in bytes).
823 */
824 void br_sha384_update(br_sha384_context *ctx, const void *data, size_t len);
825
826 /**
827 * \brief Compute SHA-384 output.
828 *
829 * The SHA-384 output for the concatenation of all bytes injected in the
830 * provided context since the last initialisation or reset call, is
831 * computed and written in the buffer pointed to by `out`. The context
832 * itself is not modified, so extra bytes may be injected afterwards
833 * to continue that computation.
834 *
835 * \param ctx pointer to the context structure.
836 * \param out destination buffer for the hash output.
837 */
838 void br_sha384_out(const br_sha384_context *ctx, void *out);
839
840 /**
841 * \brief Save SHA-384 running state.
842 *
843 * The running state for SHA-384 (output of the last internal block
844 * processing) is written in the buffer pointed to by `out`. The
845 * number of bytes injected since the last initialisation or reset
846 * call is returned. The context is not modified.
847 *
848 * \param ctx pointer to the context structure.
849 * \param out destination buffer for the running state.
850 * \return the injected total byte length.
851 */
852 uint64_t br_sha384_state(const br_sha384_context *ctx, void *out);
853
854 /**
855 * \brief Restore SHA-384 running state.
856 *
857 * The running state for SHA-384 is set to the provided values.
858 *
859 * \param ctx pointer to the context structure.
860 * \param stb source buffer for the running state.
861 * \param count the injected total byte length.
862 */
863 void br_sha384_set_state(br_sha384_context *ctx,
864 const void *stb, uint64_t count);
865
866 /* obsolete
867 #define br_sha384_ID 5
868 #define br_sha384_SIZE 48
869 extern const br_hash_class br_sha384_vtable;
870 typedef struct {
871 const br_hash_class *vtable;
872 unsigned char buf[128];
873 uint64_t count;
874 uint64_t val[8];
875 } br_sha384_context;
876 void br_sha384_init(br_sha384_context *ctx);
877 void br_sha384_update(br_sha384_context *ctx, const void *data, size_t len);
878 void br_sha384_out(const br_sha384_context *ctx, void *out);
879 uint64_t br_sha384_state(const br_sha384_context *ctx, void *out);
880 void br_sha384_set_state(br_sha384_context *ctx,
881 const void *stb, uint64_t count);
882 */
883
884 /**
885 * \brief Symbolic identifier for SHA-512.
886 */
887 #define br_sha512_ID 6
888
889 /**
890 * \brief SHA-512 output size (in bytes).
891 */
892 #define br_sha512_SIZE 64
893
894 /**
895 * \brief Constant vtable for SHA-512.
896 */
897 extern const br_hash_class br_sha512_vtable;
898
899 #ifdef BR_DOXYGEN_IGNORE
900 /**
901 * \brief SHA-512 context.
902 *
903 * First field is a pointer to the vtable; it is set by the initialisation
904 * function. Other fields are not supposed to be accessed by user code.
905 */
906 typedef struct {
907 /**
908 * \brief Pointer to vtable for this context.
909 */
910 const br_hash_class *vtable;
911 unsigned char buf[128];
912 uint64_t count;
913 uint64_t val[8];
914 } br_sha512_context;
915 #else
916 typedef br_sha384_context br_sha512_context;
917 #endif
918
919 /**
920 * \brief SHA-512 context initialisation.
921 *
922 * This function initialises or resets a context for a new SHA-512
923 * computation. It also sets the vtable pointer.
924 *
925 * \param ctx pointer to the context structure.
926 */
927 void br_sha512_init(br_sha512_context *ctx);
928
929 #ifdef BR_DOXYGEN_IGNORE
930 /**
931 * \brief Inject some data bytes in a running SHA-512 computation.
932 *
933 * The provided context is updated with some data bytes. If the number
934 * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
935 * and may be `NULL`, and this function does nothing.
936 *
937 * \param ctx pointer to the context structure.
938 * \param data pointer to the injected data.
939 * \param len injected data length (in bytes).
940 */
941 void br_sha512_update(br_sha512_context *ctx, const void *data, size_t len);
942 #else
943 #define br_sha512_update br_sha384_update
944 #endif
945
946 /**
947 * \brief Compute SHA-512 output.
948 *
949 * The SHA-512 output for the concatenation of all bytes injected in the
950 * provided context since the last initialisation or reset call, is
951 * computed and written in the buffer pointed to by `out`. The context
952 * itself is not modified, so extra bytes may be injected afterwards
953 * to continue that computation.
954 *
955 * \param ctx pointer to the context structure.
956 * \param out destination buffer for the hash output.
957 */
958 void br_sha512_out(const br_sha512_context *ctx, void *out);
959
960 #ifdef BR_DOXYGEN_IGNORE
961 /**
962 * \brief Save SHA-512 running state.
963 *
964 * The running state for SHA-512 (output of the last internal block
965 * processing) is written in the buffer pointed to by `out`. The
966 * number of bytes injected since the last initialisation or reset
967 * call is returned. The context is not modified.
968 *
969 * \param ctx pointer to the context structure.
970 * \param out destination buffer for the running state.
971 * \return the injected total byte length.
972 */
973 uint64_t br_sha512_state(const br_sha512_context *ctx, void *out);
974 #else
975 #define br_sha512_state br_sha384_state
976 #endif
977
978 #ifdef BR_DOXYGEN_IGNORE
979 /**
980 * \brief Restore SHA-512 running state.
981 *
982 * The running state for SHA-512 is set to the provided values.
983 *
984 * \param ctx pointer to the context structure.
985 * \param stb source buffer for the running state.
986 * \param count the injected total byte length.
987 */
988 void br_sha512_set_state(br_sha512_context *ctx,
989 const void *stb, uint64_t count);
990 #else
991 #define br_sha512_set_state br_sha384_set_state
992 #endif
993
994 /* obsolete
995 #define br_sha512_ID 6
996 #define br_sha512_SIZE 64
997 extern const br_hash_class br_sha512_vtable;
998 typedef br_sha384_context br_sha512_context;
999 void br_sha512_init(br_sha512_context *ctx);
1000 #define br_sha512_update br_sha384_update
1001 void br_sha512_out(const br_sha512_context *ctx, void *out);
1002 #define br_sha512_state br_sha384_state
1003 #define br_sha512_set_state br_sha384_set_state
1004 */
1005
1006 /*
1007 * "md5sha1" is a special hash function that computes both MD5 and SHA-1
1008 * on the same input, and produces a 36-byte output (MD5 and SHA-1
1009 * concatenation, in that order). State size is also 36 bytes.
1010 */
1011
1012 /**
1013 * \brief Symbolic identifier for MD5+SHA-1.
1014 *
1015 * MD5+SHA-1 is the concatenation of MD5 and SHA-1, computed over the
1016 * same input. It is not one of the functions identified in TLS, so
1017 * we give it a symbolic identifier of value 0.
1018 */
1019 #define br_md5sha1_ID 0
1020
1021 /**
1022 * \brief MD5+SHA-1 output size (in bytes).
1023 */
1024 #define br_md5sha1_SIZE 36
1025
1026 /**
1027 * \brief Constant vtable for MD5+SHA-1.
1028 */
1029 extern const br_hash_class br_md5sha1_vtable;
1030
1031 /**
1032 * \brief MD5+SHA-1 context.
1033 *
1034 * First field is a pointer to the vtable; it is set by the initialisation
1035 * function. Other fields are not supposed to be accessed by user code.
1036 */
1037 typedef struct {
1038 /**
1039 * \brief Pointer to vtable for this context.
1040 */
1041 const br_hash_class *vtable;
1042 #ifndef BR_DOXYGEN_IGNORE
1043 unsigned char buf[64];
1044 uint64_t count;
1045 uint32_t val_md5[4];
1046 uint32_t val_sha1[5];
1047 #endif
1048 } br_md5sha1_context;
1049
1050 /**
1051 * \brief MD5+SHA-1 context initialisation.
1052 *
1053 * This function initialises or resets a context for a new SHA-512
1054 * computation. It also sets the vtable pointer.
1055 *
1056 * \param ctx pointer to the context structure.
1057 */
1058 void br_md5sha1_init(br_md5sha1_context *ctx);
1059
1060 /**
1061 * \brief Inject some data bytes in a running MD5+SHA-1 computation.
1062 *
1063 * The provided context is updated with some data bytes. If the number
1064 * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
1065 * and may be `NULL`, and this function does nothing.
1066 *
1067 * \param ctx pointer to the context structure.
1068 * \param data pointer to the injected data.
1069 * \param len injected data length (in bytes).
1070 */
1071 void br_md5sha1_update(br_md5sha1_context *ctx, const void *data, size_t len);
1072
1073 /**
1074 * \brief Compute MD5+SHA-1 output.
1075 *
1076 * The MD5+SHA-1 output for the concatenation of all bytes injected in the
1077 * provided context since the last initialisation or reset call, is
1078 * computed and written in the buffer pointed to by `out`. The context
1079 * itself is not modified, so extra bytes may be injected afterwards
1080 * to continue that computation.
1081 *
1082 * \param ctx pointer to the context structure.
1083 * \param out destination buffer for the hash output.
1084 */
1085 void br_md5sha1_out(const br_md5sha1_context *ctx, void *out);
1086
1087 /**
1088 * \brief Save MD5+SHA-1 running state.
1089 *
1090 * The running state for MD5+SHA-1 (output of the last internal block
1091 * processing) is written in the buffer pointed to by `out`. The
1092 * number of bytes injected since the last initialisation or reset
1093 * call is returned. The context is not modified.
1094 *
1095 * \param ctx pointer to the context structure.
1096 * \param out destination buffer for the running state.
1097 * \return the injected total byte length.
1098 */
1099 uint64_t br_md5sha1_state(const br_md5sha1_context *ctx, void *out);
1100
1101 /**
1102 * \brief Restore MD5+SHA-1 running state.
1103 *
1104 * The running state for MD5+SHA-1 is set to the provided values.
1105 *
1106 * \param ctx pointer to the context structure.
1107 * \param stb source buffer for the running state.
1108 * \param count the injected total byte length.
1109 */
1110 void br_md5sha1_set_state(br_md5sha1_context *ctx,
1111 const void *stb, uint64_t count);
1112
1113 /**
1114 * \brief Aggregate context for configurable hash function support.
1115 *
1116 * The `br_hash_compat_context` type is a type which is large enough to
1117 * serve as context for all standard hash functions defined above.
1118 */
1119 typedef union {
1120 const br_hash_class *vtable;
1121 br_md5_context md5;
1122 br_sha1_context sha1;
1123 br_sha224_context sha224;
1124 br_sha256_context sha256;
1125 br_sha384_context sha384;
1126 br_sha512_context sha512;
1127 br_md5sha1_context md5sha1;
1128 } br_hash_compat_context;
1129
1130 /*
1131 * The multi-hasher is a construct that handles hashing of the same input
1132 * data with several hash functions, with a single shared input buffer.
1133 * It can handle MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512
1134 * simultaneously, though which functions are activated depends on
1135 * the set implementation pointers.
1136 */
1137
1138 /**
1139 * \brief Multi-hasher context structure.
1140 *
1141 * The multi-hasher runs up to six hash functions in the standard TLS list
1142 * (MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512) in parallel, over
1143 * the same input.
1144 *
1145 * The multi-hasher does _not_ follow the OOP structure with a vtable.
1146 * Instead, it is configured with the vtables of the hash functions it
1147 * should run. Structure fields are not supposed to be accessed directly.
1148 */
1149 typedef struct {
1150 #ifndef BR_DOXYGEN_IGNORE
1151 unsigned char buf[128];
1152 uint64_t count;
1153 uint32_t val_32[25];
1154 uint64_t val_64[16];
1155 const br_hash_class *impl[6];
1156 #endif
1157 } br_multihash_context;
1158
1159 /**
1160 * \brief Clear a multi-hasher context.
1161 *
1162 * This should always be called once on a given context, _before_ setting
1163 * the implementation pointers.
1164 *
1165 * \param ctx the multi-hasher context.
1166 */
1167 void br_multihash_zero(br_multihash_context *ctx);
1168
1169 /**
1170 * \brief Set a hash function implementation.
1171 *
1172 * Implementations shall be set _after_ clearing the context (with
1173 * `br_multihash_zero()`) but _before_ initialising the computation
1174 * (with `br_multihash_init()`). The hash function implementation
1175 * MUST be one of the standard hash functions (MD5, SHA-1, SHA-224,
1176 * SHA-256, SHA-384 or SHA-512); it may also be `NULL` to remove
1177 * an implementation from the multi-hasher.
1178 *
1179 * \param ctx the multi-hasher context.
1180 * \param id the hash function symbolic identifier.
1181 * \param impl the hash function vtable, or `NULL`.
1182 */
1183 static inline void
1184 br_multihash_setimpl(br_multihash_context *ctx,
1185 int id, const br_hash_class *impl)
1186 {
1187 /*
1188 * This code relies on hash functions ID being values 1 to 6,
1189 * in the MD5 to SHA-512 order.
1190 */
1191 ctx->impl[id - 1] = impl;
1192 }
1193
1194 /**
1195 * \brief Get a hash function implementation.
1196 *
1197 * This function returns the currently configured vtable for a given
1198 * hash function (by symbolic ID). If no such function was configured in
1199 * the provided multi-hasher context, then this function returns `NULL`.
1200 *
1201 * \param ctx the multi-hasher context.
1202 * \param id the hash function symbolic identifier.
1203 * \return the hash function vtable, or `NULL`.
1204 */
1205 static inline const br_hash_class *
1206 br_multihash_getimpl(const br_multihash_context *ctx, int id)
1207 {
1208 return ctx->impl[id - 1];
1209 }
1210
1211 /**
1212 * \brief Reset a multi-hasher context.
1213 *
1214 * This function prepares the context for a new hashing computation,
1215 * for all implementations configured at that point.
1216 *
1217 * \param ctx the multi-hasher context.
1218 */
1219 void br_multihash_init(br_multihash_context *ctx);
1220
1221 /**
1222 * \brief Inject some data bytes in a running multi-hashing computation.
1223 *
1224 * The provided context is updated with some data bytes. If the number
1225 * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
1226 * and may be `NULL`, and this function does nothing.
1227 *
1228 * \param ctx pointer to the context structure.
1229 * \param data pointer to the injected data.
1230 * \param len injected data length (in bytes).
1231 */
1232 void br_multihash_update(br_multihash_context *ctx,
1233 const void *data, size_t len);
1234
1235 /**
1236 * \brief Compute a hash output from a multi-hasher.
1237 *
1238 * The hash output for the concatenation of all bytes injected in the
1239 * provided context since the last initialisation or reset call, is
1240 * computed and written in the buffer pointed to by `dst`. The hash
1241 * function to use is identified by `id` and must be one of the standard
1242 * hash functions. If that hash function was indeed configured in the
1243 * multi-hasher context, the corresponding hash value is written in
1244 * `dst` and its length (in bytes) is returned. If the hash function
1245 * was _not_ configured, then nothing is written in `dst` and 0 is
1246 * returned.
1247 *
1248 * The context itself is not modified, so extra bytes may be injected
1249 * afterwards to continue the hash computations.
1250 *
1251 * \param ctx pointer to the context structure.
1252 * \param id the hash function symbolic identifier.
1253 * \param dst destination buffer for the hash output.
1254 * \return the hash output length (in bytes), or 0.
1255 */
1256 size_t br_multihash_out(const br_multihash_context *ctx, int id, void *dst);
1257
1258 /**
1259 * \brief Type for a GHASH implementation.
1260 *
1261 * GHASH is a sort of keyed hash meant to be used to implement GCM in
1262 * combination with a block cipher (with 16-byte blocks).
1263 *
1264 * The `y` array has length 16 bytes and is used for input and output; in
1265 * a complete GHASH run, it starts with an all-zero value. `h` is a 16-byte
1266 * value that serves as key (it is derived from the encryption key in GCM,
1267 * using the block cipher). The data length (`len`) is expressed in bytes.
1268 * The `y` array is updated.
1269 *
1270 * If the data length is not a multiple of 16, then the data is implicitly
1271 * padded with zeros up to the next multiple of 16. Thus, when using GHASH
1272 * in GCM, this method may be called twice, for the associated data and
1273 * for the ciphertext, respectively; the zero-padding implements exactly
1274 * the GCM rules.
1275 *
1276 * \param y the array to update.
1277 * \param h the GHASH key.
1278 * \param data the input data (may be `NULL` if `len` is zero).
1279 * \param len the input data length (in bytes).
1280 */
1281 typedef void (*br_ghash)(void *y, const void *h, const void *data, size_t len);
1282
1283 /**
1284 * \brief GHASH implementation using multiplications (mixed 32-bit).
1285 *
1286 * This implementation uses multiplications of 32-bit values, with a
1287 * 64-bit result. It is constant-time (if multiplications are
1288 * constant-time).
1289 *
1290 * \param y the array to update.
1291 * \param h the GHASH key.
1292 * \param data the input data (may be `NULL` if `len` is zero).
1293 * \param len the input data length (in bytes).
1294 */
1295 void br_ghash_ctmul(void *y, const void *h, const void *data, size_t len);
1296
1297 /**
1298 * \brief GHASH implementation using multiplications (strict 32-bit).
1299 *
1300 * This implementation uses multiplications of 32-bit values, with a
1301 * 32-bit result. It is usually somewhat slower than `br_ghash_ctmul()`,
1302 * but it is expected to be faster on architectures for which the
1303 * 32-bit multiplication opcode does not yield the upper 32 bits of the
1304 * product. It is constant-time (if multiplications are constant-time).
1305 *
1306 * \param y the array to update.
1307 * \param h the GHASH key.
1308 * \param data the input data (may be `NULL` if `len` is zero).
1309 * \param len the input data length (in bytes).
1310 */
1311 void br_ghash_ctmul32(void *y, const void *h, const void *data, size_t len);
1312
1313 /**
1314 * \brief GHASH implementation using multiplications (64-bit).
1315 *
1316 * This implementation uses multiplications of 64-bit values, with a
1317 * 64-bit result. It is constant-time (if multiplications are
1318 * constant-time). It is substantially faster than `br_ghash_ctmul()`
1319 * and `br_ghash_ctmul32()` on most 64-bit architectures.
1320 *
1321 * \param y the array to update.
1322 * \param h the GHASH key.
1323 * \param data the input data (may be `NULL` if `len` is zero).
1324 * \param len the input data length (in bytes).
1325 */
1326 void br_ghash_ctmul64(void *y, const void *h, const void *data, size_t len);
1327
1328 #endif