10957b409SSimon J. Gerraty /*
20957b409SSimon J. Gerraty * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
30957b409SSimon J. Gerraty *
40957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining
50957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the
60957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including
70957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish,
80957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to
90957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to
100957b409SSimon J. Gerraty * the following conditions:
110957b409SSimon J. Gerraty *
120957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be
130957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software.
140957b409SSimon J. Gerraty *
150957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
160957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
170957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
180957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
190957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
200957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
210957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
220957b409SSimon J. Gerraty * SOFTWARE.
230957b409SSimon J. Gerraty */
240957b409SSimon J. Gerraty
250957b409SSimon J. Gerraty #ifndef BR_BEARSSL_HASH_H__
260957b409SSimon J. Gerraty #define BR_BEARSSL_HASH_H__
270957b409SSimon J. Gerraty
280957b409SSimon J. Gerraty #include <stddef.h>
290957b409SSimon J. Gerraty #include <stdint.h>
300957b409SSimon J. Gerraty #include <string.h>
310957b409SSimon J. Gerraty
320957b409SSimon J. Gerraty #ifdef __cplusplus
330957b409SSimon J. Gerraty extern "C" {
340957b409SSimon J. Gerraty #endif
350957b409SSimon J. Gerraty
360957b409SSimon J. Gerraty /** \file bearssl_hash.h
370957b409SSimon J. Gerraty *
380957b409SSimon J. Gerraty * # Hash Functions
390957b409SSimon J. Gerraty *
400957b409SSimon J. Gerraty * This file documents the API for hash functions.
410957b409SSimon J. Gerraty *
420957b409SSimon J. Gerraty *
430957b409SSimon J. Gerraty * ## Procedural API
440957b409SSimon J. Gerraty *
450957b409SSimon J. Gerraty * For each implemented hash function, of name "`xxx`", the following
460957b409SSimon J. Gerraty * elements are defined:
470957b409SSimon J. Gerraty *
480957b409SSimon J. Gerraty * - `br_xxx_vtable`
490957b409SSimon J. Gerraty *
500957b409SSimon J. Gerraty * An externally defined instance of `br_hash_class`.
510957b409SSimon J. Gerraty *
520957b409SSimon J. Gerraty * - `br_xxx_SIZE`
530957b409SSimon J. Gerraty *
540957b409SSimon J. Gerraty * A macro that evaluates to the output size (in bytes) of the
550957b409SSimon J. Gerraty * hash function.
560957b409SSimon J. Gerraty *
570957b409SSimon J. Gerraty * - `br_xxx_ID`
580957b409SSimon J. Gerraty *
590957b409SSimon J. Gerraty * A macro that evaluates to a symbolic identifier for the hash
600957b409SSimon J. Gerraty * function. Such identifiers are used with HMAC and signature
610957b409SSimon J. Gerraty * algorithm implementations.
620957b409SSimon J. Gerraty *
630957b409SSimon J. Gerraty * NOTE: for the "standard" hash functions defined in [the TLS
640957b409SSimon J. Gerraty * standard](https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1),
650957b409SSimon J. Gerraty * the symbolic identifiers match the constants used in TLS, i.e.
660957b409SSimon J. Gerraty * 1 to 6 for MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512,
670957b409SSimon J. Gerraty * respectively.
680957b409SSimon J. Gerraty *
690957b409SSimon J. Gerraty * - `br_xxx_context`
700957b409SSimon J. Gerraty *
710957b409SSimon J. Gerraty * Context for an ongoing computation. It is allocated by the
720957b409SSimon J. Gerraty * caller, and a pointer to it is passed to all functions. A
730957b409SSimon J. Gerraty * context contains no interior pointer, so it can be moved around
740957b409SSimon J. Gerraty * and cloned (with a simple `memcpy()` or equivalent) in order to
750957b409SSimon J. Gerraty * capture the function state at some point. Computations that use
760957b409SSimon J. Gerraty * distinct context structures are independent of each other. The
770957b409SSimon J. Gerraty * first field of `br_xxx_context` is always a pointer to the
780957b409SSimon J. Gerraty * `br_xxx_vtable` structure; `br_xxx_init()` sets that pointer.
790957b409SSimon J. Gerraty *
800957b409SSimon J. Gerraty * - `br_xxx_init(br_xxx_context *ctx)`
810957b409SSimon J. Gerraty *
820957b409SSimon J. Gerraty * Initialise the provided context. Previous contents of the structure
830957b409SSimon J. Gerraty * are ignored. This calls resets the context to the start of a new
840957b409SSimon J. Gerraty * hash computation; it also sets the first field of the context
850957b409SSimon J. Gerraty * structure (called `vtable`) to a pointer to the statically
860957b409SSimon J. Gerraty * allocated constant `br_xxx_vtable` structure.
870957b409SSimon J. Gerraty *
880957b409SSimon J. Gerraty * - `br_xxx_update(br_xxx_context *ctx, const void *data, size_t len)`
890957b409SSimon J. Gerraty *
900957b409SSimon J. Gerraty * Add some more bytes to the hash computation represented by the
910957b409SSimon J. Gerraty * provided context.
920957b409SSimon J. Gerraty *
930957b409SSimon J. Gerraty * - `br_xxx_out(const br_xxx_context *ctx, void *out)`
940957b409SSimon J. Gerraty *
950957b409SSimon J. Gerraty * Complete the hash computation and write the result in the provided
960957b409SSimon J. Gerraty * buffer. The output buffer MUST be large enough to accommodate the
970957b409SSimon J. Gerraty * result. The context is NOT modified by this operation, so this
980957b409SSimon J. Gerraty * function can be used to get a "partial hash" while still keeping
990957b409SSimon J. Gerraty * the possibility of adding more bytes to the input.
1000957b409SSimon J. Gerraty *
1010957b409SSimon J. Gerraty * - `br_xxx_state(const br_xxx_context *ctx, void *out)`
1020957b409SSimon J. Gerraty *
1030957b409SSimon J. Gerraty * Get a copy of the "current state" for the computation so far. For
1040957b409SSimon J. Gerraty * MD functions (MD5, SHA-1, SHA-2 family), this is the running state
1050957b409SSimon J. Gerraty * resulting from the processing of the last complete input block.
1060957b409SSimon J. Gerraty * Returned value is the current input length (in bytes).
1070957b409SSimon J. Gerraty *
1080957b409SSimon J. Gerraty * - `br_xxx_set_state(br_xxx_context *ctx, const void *stb, uint64_t count)`
1090957b409SSimon J. Gerraty *
1100957b409SSimon J. Gerraty * Set the internal state to the provided values. The 'stb' and
1110957b409SSimon J. Gerraty * 'count' values shall match that which was obtained from
1120957b409SSimon J. Gerraty * `br_xxx_state()`. This restores the hash state only if the state
1130957b409SSimon J. Gerraty * values were at an appropriate block boundary. This does NOT set
1140957b409SSimon J. Gerraty * the `vtable` pointer in the context.
1150957b409SSimon J. Gerraty *
1160957b409SSimon J. Gerraty * Context structures can be discarded without any explicit deallocation.
1170957b409SSimon J. Gerraty * Hash function implementations are purely software and don't reserve
1180957b409SSimon J. Gerraty * any resources outside of the context structure itself.
1190957b409SSimon J. Gerraty *
1200957b409SSimon J. Gerraty *
1210957b409SSimon J. Gerraty * ## Object-Oriented API
1220957b409SSimon J. Gerraty *
1230957b409SSimon J. Gerraty * For each hash function that follows the procedural API described
1240957b409SSimon J. Gerraty * above, an object-oriented API is also provided. In that API, function
1250957b409SSimon J. Gerraty * pointers from the vtable (`br_xxx_vtable`) are used. The vtable
1260957b409SSimon J. Gerraty * incarnates object-oriented programming. An introduction on the OOP
1270957b409SSimon J. Gerraty * concept used here can be read on the BearSSL Web site:<br />
1280957b409SSimon J. Gerraty * [https://www.bearssl.org/oop.html](https://www.bearssl.org/oop.html)
1290957b409SSimon J. Gerraty *
1300957b409SSimon J. Gerraty * The vtable offers functions called `init()`, `update()`, `out()`,
1310957b409SSimon J. Gerraty * `set()` and `set_state()`, which are in fact the functions from
1320957b409SSimon J. Gerraty * the procedural API. That vtable also contains two informative fields:
1330957b409SSimon J. Gerraty *
1340957b409SSimon J. Gerraty * - `context_size`
1350957b409SSimon J. Gerraty *
1360957b409SSimon J. Gerraty * The size of the context structure (`br_xxx_context`), in bytes.
1370957b409SSimon J. Gerraty * This can be used by generic implementations to perform dynamic
1380957b409SSimon J. Gerraty * context allocation.
1390957b409SSimon J. Gerraty *
1400957b409SSimon J. Gerraty * - `desc`
1410957b409SSimon J. Gerraty *
1420957b409SSimon J. Gerraty * A "descriptor" field that encodes some information on the hash
1430957b409SSimon J. Gerraty * function: symbolic identifier, output size, state size,
1440957b409SSimon J. Gerraty * internal block size, details on the padding.
1450957b409SSimon J. Gerraty *
1460957b409SSimon J. Gerraty * Users of this object-oriented API (in particular generic HMAC
1470957b409SSimon J. Gerraty * implementations) may make the following assumptions:
1480957b409SSimon J. Gerraty *
1490957b409SSimon J. Gerraty * - Hash output size is no more than 64 bytes.
1500957b409SSimon J. Gerraty * - Hash internal state size is no more than 64 bytes.
1510957b409SSimon J. Gerraty * - Internal block size is a power of two, no less than 16 and no more
1520957b409SSimon J. Gerraty * than 256.
1530957b409SSimon J. Gerraty *
1540957b409SSimon J. Gerraty *
1550957b409SSimon J. Gerraty * ## Implemented Hash Functions
1560957b409SSimon J. Gerraty *
1570957b409SSimon J. Gerraty * Implemented hash functions are:
1580957b409SSimon J. Gerraty *
1590957b409SSimon J. Gerraty * | Function | Name | Output length | State length |
1600957b409SSimon J. Gerraty * | :-------- | :------ | :-----------: | :----------: |
1610957b409SSimon J. Gerraty * | MD5 | md5 | 16 | 16 |
1620957b409SSimon J. Gerraty * | SHA-1 | sha1 | 20 | 20 |
1630957b409SSimon J. Gerraty * | SHA-224 | sha224 | 28 | 32 |
1640957b409SSimon J. Gerraty * | SHA-256 | sha256 | 32 | 32 |
1650957b409SSimon J. Gerraty * | SHA-384 | sha384 | 48 | 64 |
1660957b409SSimon J. Gerraty * | SHA-512 | sha512 | 64 | 64 |
1670957b409SSimon J. Gerraty * | MD5+SHA-1 | md5sha1 | 36 | 36 |
1680957b409SSimon J. Gerraty *
1690957b409SSimon J. Gerraty * (MD5+SHA-1 is the concatenation of MD5 and SHA-1 computed over the
1700957b409SSimon J. Gerraty * same input; in the implementation, the internal data buffer is
1710957b409SSimon J. Gerraty * shared, thus making it more memory-efficient than separate MD5 and
1720957b409SSimon J. Gerraty * SHA-1. It can be useful in implementing SSL 3.0, TLS 1.0 and TLS
1730957b409SSimon J. Gerraty * 1.1.)
1740957b409SSimon J. Gerraty *
1750957b409SSimon J. Gerraty *
1760957b409SSimon J. Gerraty * ## Multi-Hasher
1770957b409SSimon J. Gerraty *
1780957b409SSimon J. Gerraty * An aggregate hasher is provided, that can compute several standard
1790957b409SSimon J. Gerraty * hash functions in parallel. It uses `br_multihash_context` and a
1800957b409SSimon J. Gerraty * procedural API. It is configured with the implementations (the vtables)
1810957b409SSimon J. Gerraty * that it should use; it will then compute all these hash functions in
1820957b409SSimon J. Gerraty * parallel, on the same input. It is meant to be used in cases when the
1830957b409SSimon J. Gerraty * hash of an object will be used, but the exact hash function is not
1840957b409SSimon J. Gerraty * known yet (typically, streamed processing on X.509 certificates).
1850957b409SSimon J. Gerraty *
1860957b409SSimon J. Gerraty * Only the standard hash functions (MD5, SHA-1, SHA-224, SHA-256, SHA-384
1870957b409SSimon J. Gerraty * and SHA-512) are supported by the multi-hasher.
1880957b409SSimon J. Gerraty *
1890957b409SSimon J. Gerraty *
1900957b409SSimon J. Gerraty * ## GHASH
1910957b409SSimon J. Gerraty *
1920957b409SSimon J. Gerraty * GHASH is not a generic hash function; it is a _universal_ hash function,
1930957b409SSimon J. Gerraty * which, as the name does not say, means that it CANNOT be used in most
1940957b409SSimon J. Gerraty * places where a hash function is needed. GHASH is used within the GCM
1950957b409SSimon J. Gerraty * encryption mode, to provide the checked integrity functionality.
1960957b409SSimon J. Gerraty *
1970957b409SSimon J. Gerraty * A GHASH implementation is basically a function that uses the type defined
1980957b409SSimon J. Gerraty * in this file under the name `br_ghash`:
1990957b409SSimon J. Gerraty *
2000957b409SSimon J. Gerraty * typedef void (*br_ghash)(void *y, const void *h, const void *data, size_t len);
2010957b409SSimon J. Gerraty *
2020957b409SSimon J. Gerraty * The `y` pointer refers to a 16-byte value which is used as input, and
2030957b409SSimon J. Gerraty * receives the output of the GHASH invocation. `h` is a 16-byte secret
2040957b409SSimon J. Gerraty * value (that serves as key). `data` and `len` define the input data.
2050957b409SSimon J. Gerraty *
2060957b409SSimon J. Gerraty * Three GHASH implementations are provided, all constant-time, based on
2070957b409SSimon J. Gerraty * the use of integer multiplications with appropriate masking to cancel
2080957b409SSimon J. Gerraty * carry propagation.
2090957b409SSimon J. Gerraty */
2100957b409SSimon J. Gerraty
2110957b409SSimon J. Gerraty /**
2120957b409SSimon J. Gerraty * \brief Class type for hash function implementations.
2130957b409SSimon J. Gerraty *
2140957b409SSimon J. Gerraty * A `br_hash_class` instance references the methods implementing a hash
2150957b409SSimon J. Gerraty * function. Constant instances of this structure are defined for each
2160957b409SSimon J. Gerraty * implemented hash function. Such instances are also called "vtables".
2170957b409SSimon J. Gerraty *
2180957b409SSimon J. Gerraty * Vtables are used to support object-oriented programming, as
2190957b409SSimon J. Gerraty * described on [the BearSSL Web site](https://www.bearssl.org/oop.html).
2200957b409SSimon J. Gerraty */
2210957b409SSimon J. Gerraty typedef struct br_hash_class_ br_hash_class;
2220957b409SSimon J. Gerraty struct br_hash_class_ {
2230957b409SSimon J. Gerraty /**
2240957b409SSimon J. Gerraty * \brief Size (in bytes) of the context structure appropriate for
2250957b409SSimon J. Gerraty * computing this hash function.
2260957b409SSimon J. Gerraty */
2270957b409SSimon J. Gerraty size_t context_size;
2280957b409SSimon J. Gerraty
2290957b409SSimon J. Gerraty /**
2300957b409SSimon J. Gerraty * \brief Descriptor word that contains information about the hash
2310957b409SSimon J. Gerraty * function.
2320957b409SSimon J. Gerraty *
2330957b409SSimon J. Gerraty * For each word `xxx` described below, use `BR_HASHDESC_xxx_OFF`
2340957b409SSimon J. Gerraty * and `BR_HASHDESC_xxx_MASK` to access the specific value, as
2350957b409SSimon J. Gerraty * follows:
2360957b409SSimon J. Gerraty *
2370957b409SSimon J. Gerraty * (hf->desc >> BR_HASHDESC_xxx_OFF) & BR_HASHDESC_xxx_MASK
2380957b409SSimon J. Gerraty *
2390957b409SSimon J. Gerraty * The defined elements are:
2400957b409SSimon J. Gerraty *
2410957b409SSimon J. Gerraty * - `ID`: the symbolic identifier for the function, as defined
2420957b409SSimon J. Gerraty * in [TLS](https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1)
2430957b409SSimon J. Gerraty * (MD5 = 1, SHA-1 = 2,...).
2440957b409SSimon J. Gerraty *
2450957b409SSimon J. Gerraty * - `OUT`: hash output size, in bytes.
2460957b409SSimon J. Gerraty *
2470957b409SSimon J. Gerraty * - `STATE`: internal running state size, in bytes.
2480957b409SSimon J. Gerraty *
2490957b409SSimon J. Gerraty * - `LBLEN`: base-2 logarithm for the internal block size, as
2500957b409SSimon J. Gerraty * defined for HMAC processing (this is 6 for MD5, SHA-1, SHA-224
2510957b409SSimon J. Gerraty * and SHA-256, since these functions use 64-byte blocks; for
2520957b409SSimon J. Gerraty * SHA-384 and SHA-512, this is 7, corresponding to their
2530957b409SSimon J. Gerraty * 128-byte blocks).
2540957b409SSimon J. Gerraty *
2550957b409SSimon J. Gerraty * The descriptor may contain a few other flags.
2560957b409SSimon J. Gerraty */
2570957b409SSimon J. Gerraty uint32_t desc;
2580957b409SSimon J. Gerraty
2590957b409SSimon J. Gerraty /**
2600957b409SSimon J. Gerraty * \brief Initialisation method.
2610957b409SSimon J. Gerraty *
2620957b409SSimon J. Gerraty * This method takes as parameter a pointer to a context area,
2630957b409SSimon J. Gerraty * that it initialises. The first field of the context is set
2640957b409SSimon J. Gerraty * to this vtable; other elements are initialised for a new hash
2650957b409SSimon J. Gerraty * computation.
2660957b409SSimon J. Gerraty *
2670957b409SSimon J. Gerraty * \param ctx pointer to (the first field of) the context.
2680957b409SSimon J. Gerraty */
2690957b409SSimon J. Gerraty void (*init)(const br_hash_class **ctx);
2700957b409SSimon J. Gerraty
2710957b409SSimon J. Gerraty /**
2720957b409SSimon J. Gerraty * \brief Data injection method.
2730957b409SSimon J. Gerraty *
2740957b409SSimon J. Gerraty * The `len` bytes starting at address `data` are injected into
2750957b409SSimon J. Gerraty * the running hash computation incarnated by the specified
2760957b409SSimon J. Gerraty * context. The context is updated accordingly. It is allowed
2770957b409SSimon J. Gerraty * to have `len == 0`, in which case `data` is ignored (and could
2780957b409SSimon J. Gerraty * be `NULL`), and nothing happens.
2790957b409SSimon J. Gerraty * on the input data.
2800957b409SSimon J. Gerraty *
2810957b409SSimon J. Gerraty * \param ctx pointer to (the first field of) the context.
2820957b409SSimon J. Gerraty * \param data pointer to the first data byte to inject.
2830957b409SSimon J. Gerraty * \param len number of bytes to inject.
2840957b409SSimon J. Gerraty */
2850957b409SSimon J. Gerraty void (*update)(const br_hash_class **ctx, const void *data, size_t len);
2860957b409SSimon J. Gerraty
2870957b409SSimon J. Gerraty /**
2880957b409SSimon J. Gerraty * \brief Produce hash output.
2890957b409SSimon J. Gerraty *
2900957b409SSimon J. Gerraty * The hash output corresponding to all data bytes injected in the
2910957b409SSimon J. Gerraty * context since the last `init()` call is computed, and written
2920957b409SSimon J. Gerraty * in the buffer pointed to by `dst`. The hash output size depends
2930957b409SSimon J. Gerraty * on the implemented hash function (e.g. 16 bytes for MD5).
2940957b409SSimon J. Gerraty * The context is _not_ modified by this call, so further bytes
2950957b409SSimon J. Gerraty * may be afterwards injected to continue the current computation.
2960957b409SSimon J. Gerraty *
2970957b409SSimon J. Gerraty * \param ctx pointer to (the first field of) the context.
2980957b409SSimon J. Gerraty * \param dst destination buffer for the hash output.
2990957b409SSimon J. Gerraty */
3000957b409SSimon J. Gerraty void (*out)(const br_hash_class *const *ctx, void *dst);
3010957b409SSimon J. Gerraty
3020957b409SSimon J. Gerraty /**
3030957b409SSimon J. Gerraty * \brief Get running state.
3040957b409SSimon J. Gerraty *
3050957b409SSimon J. Gerraty * This method saves the current running state into the `dst`
3060957b409SSimon J. Gerraty * buffer. What constitutes the "running state" depends on the
3070957b409SSimon J. Gerraty * hash function; for Merkle-Damgård hash functions (like
3080957b409SSimon J. Gerraty * MD5 or SHA-1), this is the output obtained after processing
3090957b409SSimon J. Gerraty * each block. The number of bytes injected so far is returned.
3100957b409SSimon J. Gerraty * The context is not modified by this call.
3110957b409SSimon J. Gerraty *
3120957b409SSimon J. Gerraty * \param ctx pointer to (the first field of) the context.
3130957b409SSimon J. Gerraty * \param dst destination buffer for the state.
3140957b409SSimon J. Gerraty * \return the injected total byte length.
3150957b409SSimon J. Gerraty */
3160957b409SSimon J. Gerraty uint64_t (*state)(const br_hash_class *const *ctx, void *dst);
3170957b409SSimon J. Gerraty
3180957b409SSimon J. Gerraty /**
3190957b409SSimon J. Gerraty * \brief Set running state.
3200957b409SSimon J. Gerraty *
3210957b409SSimon J. Gerraty * This methods replaces the running state for the function.
3220957b409SSimon J. Gerraty *
3230957b409SSimon J. Gerraty * \param ctx pointer to (the first field of) the context.
3240957b409SSimon J. Gerraty * \param stb source buffer for the state.
3250957b409SSimon J. Gerraty * \param count injected total byte length.
3260957b409SSimon J. Gerraty */
3270957b409SSimon J. Gerraty void (*set_state)(const br_hash_class **ctx,
3280957b409SSimon J. Gerraty const void *stb, uint64_t count);
3290957b409SSimon J. Gerraty };
3300957b409SSimon J. Gerraty
3310957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
3320957b409SSimon J. Gerraty #define BR_HASHDESC_ID(id) ((uint32_t)(id) << BR_HASHDESC_ID_OFF)
3330957b409SSimon J. Gerraty #define BR_HASHDESC_ID_OFF 0
3340957b409SSimon J. Gerraty #define BR_HASHDESC_ID_MASK 0xFF
3350957b409SSimon J. Gerraty
3360957b409SSimon J. Gerraty #define BR_HASHDESC_OUT(size) ((uint32_t)(size) << BR_HASHDESC_OUT_OFF)
3370957b409SSimon J. Gerraty #define BR_HASHDESC_OUT_OFF 8
3380957b409SSimon J. Gerraty #define BR_HASHDESC_OUT_MASK 0x7F
3390957b409SSimon J. Gerraty
3400957b409SSimon J. Gerraty #define BR_HASHDESC_STATE(size) ((uint32_t)(size) << BR_HASHDESC_STATE_OFF)
3410957b409SSimon J. Gerraty #define BR_HASHDESC_STATE_OFF 15
3420957b409SSimon J. Gerraty #define BR_HASHDESC_STATE_MASK 0xFF
3430957b409SSimon J. Gerraty
3440957b409SSimon J. Gerraty #define BR_HASHDESC_LBLEN(ls) ((uint32_t)(ls) << BR_HASHDESC_LBLEN_OFF)
3450957b409SSimon J. Gerraty #define BR_HASHDESC_LBLEN_OFF 23
3460957b409SSimon J. Gerraty #define BR_HASHDESC_LBLEN_MASK 0x0F
3470957b409SSimon J. Gerraty
3480957b409SSimon J. Gerraty #define BR_HASHDESC_MD_PADDING ((uint32_t)1 << 28)
3490957b409SSimon J. Gerraty #define BR_HASHDESC_MD_PADDING_128 ((uint32_t)1 << 29)
3500957b409SSimon J. Gerraty #define BR_HASHDESC_MD_PADDING_BE ((uint32_t)1 << 30)
3510957b409SSimon J. Gerraty #endif
3520957b409SSimon J. Gerraty
3530957b409SSimon J. Gerraty /*
3540957b409SSimon J. Gerraty * Specific hash functions.
3550957b409SSimon J. Gerraty *
3560957b409SSimon J. Gerraty * Rules for contexts:
3570957b409SSimon J. Gerraty * -- No interior pointer.
3580957b409SSimon J. Gerraty * -- No pointer to external dynamically allocated resources.
3590957b409SSimon J. Gerraty * -- First field is called 'vtable' and is a pointer to a
3600957b409SSimon J. Gerraty * const-qualified br_hash_class instance (pointer is set by init()).
3610957b409SSimon J. Gerraty * -- SHA-224 and SHA-256 contexts are identical.
3620957b409SSimon J. Gerraty * -- SHA-384 and SHA-512 contexts are identical.
3630957b409SSimon J. Gerraty *
3640957b409SSimon J. Gerraty * Thus, contexts can be moved and cloned to capture the hash function
3650957b409SSimon J. Gerraty * current state; and there is no need for any explicit "release" function.
3660957b409SSimon J. Gerraty */
3670957b409SSimon J. Gerraty
3680957b409SSimon J. Gerraty /**
3690957b409SSimon J. Gerraty * \brief Symbolic identifier for MD5.
3700957b409SSimon J. Gerraty */
3710957b409SSimon J. Gerraty #define br_md5_ID 1
3720957b409SSimon J. Gerraty
3730957b409SSimon J. Gerraty /**
3740957b409SSimon J. Gerraty * \brief MD5 output size (in bytes).
3750957b409SSimon J. Gerraty */
3760957b409SSimon J. Gerraty #define br_md5_SIZE 16
3770957b409SSimon J. Gerraty
3780957b409SSimon J. Gerraty /**
3790957b409SSimon J. Gerraty * \brief Constant vtable for MD5.
3800957b409SSimon J. Gerraty */
3810957b409SSimon J. Gerraty extern const br_hash_class br_md5_vtable;
3820957b409SSimon J. Gerraty
3830957b409SSimon J. Gerraty /**
3840957b409SSimon J. Gerraty * \brief MD5 context.
3850957b409SSimon J. Gerraty *
3860957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation
3870957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code.
3880957b409SSimon J. Gerraty */
3890957b409SSimon J. Gerraty typedef struct {
3900957b409SSimon J. Gerraty /**
3910957b409SSimon J. Gerraty * \brief Pointer to vtable for this context.
3920957b409SSimon J. Gerraty */
3930957b409SSimon J. Gerraty const br_hash_class *vtable;
3940957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
3950957b409SSimon J. Gerraty unsigned char buf[64];
3960957b409SSimon J. Gerraty uint64_t count;
3970957b409SSimon J. Gerraty uint32_t val[4];
3980957b409SSimon J. Gerraty #endif
3990957b409SSimon J. Gerraty } br_md5_context;
4000957b409SSimon J. Gerraty
4010957b409SSimon J. Gerraty /**
4020957b409SSimon J. Gerraty * \brief MD5 context initialisation.
4030957b409SSimon J. Gerraty *
4040957b409SSimon J. Gerraty * This function initialises or resets a context for a new MD5
4050957b409SSimon J. Gerraty * computation. It also sets the vtable pointer.
4060957b409SSimon J. Gerraty *
4070957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
4080957b409SSimon J. Gerraty */
4090957b409SSimon J. Gerraty void br_md5_init(br_md5_context *ctx);
4100957b409SSimon J. Gerraty
4110957b409SSimon J. Gerraty /**
4120957b409SSimon J. Gerraty * \brief Inject some data bytes in a running MD5 computation.
4130957b409SSimon J. Gerraty *
4140957b409SSimon J. Gerraty * The provided context is updated with some data bytes. If the number
4150957b409SSimon J. Gerraty * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
4160957b409SSimon J. Gerraty * and may be `NULL`, and this function does nothing.
4170957b409SSimon J. Gerraty *
4180957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
4190957b409SSimon J. Gerraty * \param data pointer to the injected data.
4200957b409SSimon J. Gerraty * \param len injected data length (in bytes).
4210957b409SSimon J. Gerraty */
4220957b409SSimon J. Gerraty void br_md5_update(br_md5_context *ctx, const void *data, size_t len);
4230957b409SSimon J. Gerraty
4240957b409SSimon J. Gerraty /**
4250957b409SSimon J. Gerraty * \brief Compute MD5 output.
4260957b409SSimon J. Gerraty *
4270957b409SSimon J. Gerraty * The MD5 output for the concatenation of all bytes injected in the
4280957b409SSimon J. Gerraty * provided context since the last initialisation or reset call, is
4290957b409SSimon J. Gerraty * computed and written in the buffer pointed to by `out`. The context
4300957b409SSimon J. Gerraty * itself is not modified, so extra bytes may be injected afterwards
4310957b409SSimon J. Gerraty * to continue that computation.
4320957b409SSimon J. Gerraty *
4330957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
4340957b409SSimon J. Gerraty * \param out destination buffer for the hash output.
4350957b409SSimon J. Gerraty */
4360957b409SSimon J. Gerraty void br_md5_out(const br_md5_context *ctx, void *out);
4370957b409SSimon J. Gerraty
4380957b409SSimon J. Gerraty /**
4390957b409SSimon J. Gerraty * \brief Save MD5 running state.
4400957b409SSimon J. Gerraty *
4410957b409SSimon J. Gerraty * The running state for MD5 (output of the last internal block
4420957b409SSimon J. Gerraty * processing) is written in the buffer pointed to by `out`. The
4430957b409SSimon J. Gerraty * number of bytes injected since the last initialisation or reset
4440957b409SSimon J. Gerraty * call is returned. The context is not modified.
4450957b409SSimon J. Gerraty *
4460957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
4470957b409SSimon J. Gerraty * \param out destination buffer for the running state.
4480957b409SSimon J. Gerraty * \return the injected total byte length.
4490957b409SSimon J. Gerraty */
4500957b409SSimon J. Gerraty uint64_t br_md5_state(const br_md5_context *ctx, void *out);
4510957b409SSimon J. Gerraty
4520957b409SSimon J. Gerraty /**
4530957b409SSimon J. Gerraty * \brief Restore MD5 running state.
4540957b409SSimon J. Gerraty *
4550957b409SSimon J. Gerraty * The running state for MD5 is set to the provided values.
4560957b409SSimon J. Gerraty *
4570957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
4580957b409SSimon J. Gerraty * \param stb source buffer for the running state.
4590957b409SSimon J. Gerraty * \param count the injected total byte length.
4600957b409SSimon J. Gerraty */
4610957b409SSimon J. Gerraty void br_md5_set_state(br_md5_context *ctx, const void *stb, uint64_t count);
4620957b409SSimon J. Gerraty
4630957b409SSimon J. Gerraty /**
4640957b409SSimon J. Gerraty * \brief Symbolic identifier for SHA-1.
4650957b409SSimon J. Gerraty */
4660957b409SSimon J. Gerraty #define br_sha1_ID 2
4670957b409SSimon J. Gerraty
4680957b409SSimon J. Gerraty /**
4690957b409SSimon J. Gerraty * \brief SHA-1 output size (in bytes).
4700957b409SSimon J. Gerraty */
4710957b409SSimon J. Gerraty #define br_sha1_SIZE 20
4720957b409SSimon J. Gerraty
4730957b409SSimon J. Gerraty /**
4740957b409SSimon J. Gerraty * \brief Constant vtable for SHA-1.
4750957b409SSimon J. Gerraty */
4760957b409SSimon J. Gerraty extern const br_hash_class br_sha1_vtable;
4770957b409SSimon J. Gerraty
4780957b409SSimon J. Gerraty /**
4790957b409SSimon J. Gerraty * \brief SHA-1 context.
4800957b409SSimon J. Gerraty *
4810957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation
4820957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code.
4830957b409SSimon J. Gerraty */
4840957b409SSimon J. Gerraty typedef struct {
4850957b409SSimon J. Gerraty /**
4860957b409SSimon J. Gerraty * \brief Pointer to vtable for this context.
4870957b409SSimon J. Gerraty */
4880957b409SSimon J. Gerraty const br_hash_class *vtable;
4890957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
4900957b409SSimon J. Gerraty unsigned char buf[64];
4910957b409SSimon J. Gerraty uint64_t count;
4920957b409SSimon J. Gerraty uint32_t val[5];
4930957b409SSimon J. Gerraty #endif
4940957b409SSimon J. Gerraty } br_sha1_context;
4950957b409SSimon J. Gerraty
4960957b409SSimon J. Gerraty /**
4970957b409SSimon J. Gerraty * \brief SHA-1 context initialisation.
4980957b409SSimon J. Gerraty *
4990957b409SSimon J. Gerraty * This function initialises or resets a context for a new SHA-1
5000957b409SSimon J. Gerraty * computation. It also sets the vtable pointer.
5010957b409SSimon J. Gerraty *
5020957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
5030957b409SSimon J. Gerraty */
5040957b409SSimon J. Gerraty void br_sha1_init(br_sha1_context *ctx);
5050957b409SSimon J. Gerraty
5060957b409SSimon J. Gerraty /**
5070957b409SSimon J. Gerraty * \brief Inject some data bytes in a running SHA-1 computation.
5080957b409SSimon J. Gerraty *
5090957b409SSimon J. Gerraty * The provided context is updated with some data bytes. If the number
5100957b409SSimon J. Gerraty * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
5110957b409SSimon J. Gerraty * and may be `NULL`, and this function does nothing.
5120957b409SSimon J. Gerraty *
5130957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
5140957b409SSimon J. Gerraty * \param data pointer to the injected data.
5150957b409SSimon J. Gerraty * \param len injected data length (in bytes).
5160957b409SSimon J. Gerraty */
5170957b409SSimon J. Gerraty void br_sha1_update(br_sha1_context *ctx, const void *data, size_t len);
5180957b409SSimon J. Gerraty
5190957b409SSimon J. Gerraty /**
5200957b409SSimon J. Gerraty * \brief Compute SHA-1 output.
5210957b409SSimon J. Gerraty *
5220957b409SSimon J. Gerraty * The SHA-1 output for the concatenation of all bytes injected in the
5230957b409SSimon J. Gerraty * provided context since the last initialisation or reset call, is
5240957b409SSimon J. Gerraty * computed and written in the buffer pointed to by `out`. The context
5250957b409SSimon J. Gerraty * itself is not modified, so extra bytes may be injected afterwards
5260957b409SSimon J. Gerraty * to continue that computation.
5270957b409SSimon J. Gerraty *
5280957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
5290957b409SSimon J. Gerraty * \param out destination buffer for the hash output.
5300957b409SSimon J. Gerraty */
5310957b409SSimon J. Gerraty void br_sha1_out(const br_sha1_context *ctx, void *out);
5320957b409SSimon J. Gerraty
5330957b409SSimon J. Gerraty /**
5340957b409SSimon J. Gerraty * \brief Save SHA-1 running state.
5350957b409SSimon J. Gerraty *
5360957b409SSimon J. Gerraty * The running state for SHA-1 (output of the last internal block
5370957b409SSimon J. Gerraty * processing) is written in the buffer pointed to by `out`. The
5380957b409SSimon J. Gerraty * number of bytes injected since the last initialisation or reset
5390957b409SSimon J. Gerraty * call is returned. The context is not modified.
5400957b409SSimon J. Gerraty *
5410957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
5420957b409SSimon J. Gerraty * \param out destination buffer for the running state.
5430957b409SSimon J. Gerraty * \return the injected total byte length.
5440957b409SSimon J. Gerraty */
5450957b409SSimon J. Gerraty uint64_t br_sha1_state(const br_sha1_context *ctx, void *out);
5460957b409SSimon J. Gerraty
5470957b409SSimon J. Gerraty /**
5480957b409SSimon J. Gerraty * \brief Restore SHA-1 running state.
5490957b409SSimon J. Gerraty *
5500957b409SSimon J. Gerraty * The running state for SHA-1 is set to the provided values.
5510957b409SSimon J. Gerraty *
5520957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
5530957b409SSimon J. Gerraty * \param stb source buffer for the running state.
5540957b409SSimon J. Gerraty * \param count the injected total byte length.
5550957b409SSimon J. Gerraty */
5560957b409SSimon J. Gerraty void br_sha1_set_state(br_sha1_context *ctx, const void *stb, uint64_t count);
5570957b409SSimon J. Gerraty
5580957b409SSimon J. Gerraty /**
5590957b409SSimon J. Gerraty * \brief Symbolic identifier for SHA-224.
5600957b409SSimon J. Gerraty */
5610957b409SSimon J. Gerraty #define br_sha224_ID 3
5620957b409SSimon J. Gerraty
5630957b409SSimon J. Gerraty /**
5640957b409SSimon J. Gerraty * \brief SHA-224 output size (in bytes).
5650957b409SSimon J. Gerraty */
5660957b409SSimon J. Gerraty #define br_sha224_SIZE 28
5670957b409SSimon J. Gerraty
5680957b409SSimon J. Gerraty /**
5690957b409SSimon J. Gerraty * \brief Constant vtable for SHA-224.
5700957b409SSimon J. Gerraty */
5710957b409SSimon J. Gerraty extern const br_hash_class br_sha224_vtable;
5720957b409SSimon J. Gerraty
5730957b409SSimon J. Gerraty /**
5740957b409SSimon J. Gerraty * \brief SHA-224 context.
5750957b409SSimon J. Gerraty *
5760957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation
5770957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code.
5780957b409SSimon J. Gerraty */
5790957b409SSimon J. Gerraty typedef struct {
5800957b409SSimon J. Gerraty /**
5810957b409SSimon J. Gerraty * \brief Pointer to vtable for this context.
5820957b409SSimon J. Gerraty */
5830957b409SSimon J. Gerraty const br_hash_class *vtable;
5840957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
5850957b409SSimon J. Gerraty unsigned char buf[64];
5860957b409SSimon J. Gerraty uint64_t count;
5870957b409SSimon J. Gerraty uint32_t val[8];
5880957b409SSimon J. Gerraty #endif
5890957b409SSimon J. Gerraty } br_sha224_context;
5900957b409SSimon J. Gerraty
5910957b409SSimon J. Gerraty /**
5920957b409SSimon J. Gerraty * \brief SHA-224 context initialisation.
5930957b409SSimon J. Gerraty *
5940957b409SSimon J. Gerraty * This function initialises or resets a context for a new SHA-224
5950957b409SSimon J. Gerraty * computation. It also sets the vtable pointer.
5960957b409SSimon J. Gerraty *
5970957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
5980957b409SSimon J. Gerraty */
5990957b409SSimon J. Gerraty void br_sha224_init(br_sha224_context *ctx);
6000957b409SSimon J. Gerraty
6010957b409SSimon J. Gerraty /**
6020957b409SSimon J. Gerraty * \brief Inject some data bytes in a running SHA-224 computation.
6030957b409SSimon J. Gerraty *
6040957b409SSimon J. Gerraty * The provided context is updated with some data bytes. If the number
6050957b409SSimon J. Gerraty * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
6060957b409SSimon J. Gerraty * and may be `NULL`, and this function does nothing.
6070957b409SSimon J. Gerraty *
6080957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
6090957b409SSimon J. Gerraty * \param data pointer to the injected data.
6100957b409SSimon J. Gerraty * \param len injected data length (in bytes).
6110957b409SSimon J. Gerraty */
6120957b409SSimon J. Gerraty void br_sha224_update(br_sha224_context *ctx, const void *data, size_t len);
6130957b409SSimon J. Gerraty
6140957b409SSimon J. Gerraty /**
6150957b409SSimon J. Gerraty * \brief Compute SHA-224 output.
6160957b409SSimon J. Gerraty *
6170957b409SSimon J. Gerraty * The SHA-224 output for the concatenation of all bytes injected in the
6180957b409SSimon J. Gerraty * provided context since the last initialisation or reset call, is
6190957b409SSimon J. Gerraty * computed and written in the buffer pointed to by `out`. The context
6200957b409SSimon J. Gerraty * itself is not modified, so extra bytes may be injected afterwards
6210957b409SSimon J. Gerraty * to continue that computation.
6220957b409SSimon J. Gerraty *
6230957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
6240957b409SSimon J. Gerraty * \param out destination buffer for the hash output.
6250957b409SSimon J. Gerraty */
6260957b409SSimon J. Gerraty void br_sha224_out(const br_sha224_context *ctx, void *out);
6270957b409SSimon J. Gerraty
6280957b409SSimon J. Gerraty /**
6290957b409SSimon J. Gerraty * \brief Save SHA-224 running state.
6300957b409SSimon J. Gerraty *
6310957b409SSimon J. Gerraty * The running state for SHA-224 (output of the last internal block
6320957b409SSimon J. Gerraty * processing) is written in the buffer pointed to by `out`. The
6330957b409SSimon J. Gerraty * number of bytes injected since the last initialisation or reset
6340957b409SSimon J. Gerraty * call is returned. The context is not modified.
6350957b409SSimon J. Gerraty *
6360957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
6370957b409SSimon J. Gerraty * \param out destination buffer for the running state.
6380957b409SSimon J. Gerraty * \return the injected total byte length.
6390957b409SSimon J. Gerraty */
6400957b409SSimon J. Gerraty uint64_t br_sha224_state(const br_sha224_context *ctx, void *out);
6410957b409SSimon J. Gerraty
6420957b409SSimon J. Gerraty /**
6430957b409SSimon J. Gerraty * \brief Restore SHA-224 running state.
6440957b409SSimon J. Gerraty *
6450957b409SSimon J. Gerraty * The running state for SHA-224 is set to the provided values.
6460957b409SSimon J. Gerraty *
6470957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
6480957b409SSimon J. Gerraty * \param stb source buffer for the running state.
6490957b409SSimon J. Gerraty * \param count the injected total byte length.
6500957b409SSimon J. Gerraty */
6510957b409SSimon J. Gerraty void br_sha224_set_state(br_sha224_context *ctx,
6520957b409SSimon J. Gerraty const void *stb, uint64_t count);
6530957b409SSimon J. Gerraty
6540957b409SSimon J. Gerraty /**
6550957b409SSimon J. Gerraty * \brief Symbolic identifier for SHA-256.
6560957b409SSimon J. Gerraty */
6570957b409SSimon J. Gerraty #define br_sha256_ID 4
6580957b409SSimon J. Gerraty
6590957b409SSimon J. Gerraty /**
6600957b409SSimon J. Gerraty * \brief SHA-256 output size (in bytes).
6610957b409SSimon J. Gerraty */
6620957b409SSimon J. Gerraty #define br_sha256_SIZE 32
6630957b409SSimon J. Gerraty
6640957b409SSimon J. Gerraty /**
6650957b409SSimon J. Gerraty * \brief Constant vtable for SHA-256.
6660957b409SSimon J. Gerraty */
6670957b409SSimon J. Gerraty extern const br_hash_class br_sha256_vtable;
6680957b409SSimon J. Gerraty
6690957b409SSimon J. Gerraty #ifdef BR_DOXYGEN_IGNORE
6700957b409SSimon J. Gerraty /**
6710957b409SSimon J. Gerraty * \brief SHA-256 context.
6720957b409SSimon J. Gerraty *
6730957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation
6740957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code.
6750957b409SSimon J. Gerraty */
6760957b409SSimon J. Gerraty typedef struct {
6770957b409SSimon J. Gerraty /**
6780957b409SSimon J. Gerraty * \brief Pointer to vtable for this context.
6790957b409SSimon J. Gerraty */
6800957b409SSimon J. Gerraty const br_hash_class *vtable;
6810957b409SSimon J. Gerraty } br_sha256_context;
6820957b409SSimon J. Gerraty #else
6830957b409SSimon J. Gerraty typedef br_sha224_context br_sha256_context;
6840957b409SSimon J. Gerraty #endif
6850957b409SSimon J. Gerraty
6860957b409SSimon J. Gerraty /**
6870957b409SSimon J. Gerraty * \brief SHA-256 context initialisation.
6880957b409SSimon J. Gerraty *
6890957b409SSimon J. Gerraty * This function initialises or resets a context for a new SHA-256
6900957b409SSimon J. Gerraty * computation. It also sets the vtable pointer.
6910957b409SSimon J. Gerraty *
6920957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
6930957b409SSimon J. Gerraty */
6940957b409SSimon J. Gerraty void br_sha256_init(br_sha256_context *ctx);
6950957b409SSimon J. Gerraty
6960957b409SSimon J. Gerraty #ifdef BR_DOXYGEN_IGNORE
6970957b409SSimon J. Gerraty /**
6980957b409SSimon J. Gerraty * \brief Inject some data bytes in a running SHA-256 computation.
6990957b409SSimon J. Gerraty *
7000957b409SSimon J. Gerraty * The provided context is updated with some data bytes. If the number
7010957b409SSimon J. Gerraty * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
7020957b409SSimon J. Gerraty * and may be `NULL`, and this function does nothing.
7030957b409SSimon J. Gerraty *
7040957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
7050957b409SSimon J. Gerraty * \param data pointer to the injected data.
7060957b409SSimon J. Gerraty * \param len injected data length (in bytes).
7070957b409SSimon J. Gerraty */
7080957b409SSimon J. Gerraty void br_sha256_update(br_sha256_context *ctx, const void *data, size_t len);
7090957b409SSimon J. Gerraty #else
7100957b409SSimon J. Gerraty #define br_sha256_update br_sha224_update
7110957b409SSimon J. Gerraty #endif
7120957b409SSimon J. Gerraty
7130957b409SSimon J. Gerraty /**
7140957b409SSimon J. Gerraty * \brief Compute SHA-256 output.
7150957b409SSimon J. Gerraty *
7160957b409SSimon J. Gerraty * The SHA-256 output for the concatenation of all bytes injected in the
7170957b409SSimon J. Gerraty * provided context since the last initialisation or reset call, is
7180957b409SSimon J. Gerraty * computed and written in the buffer pointed to by `out`. The context
7190957b409SSimon J. Gerraty * itself is not modified, so extra bytes may be injected afterwards
7200957b409SSimon J. Gerraty * to continue that computation.
7210957b409SSimon J. Gerraty *
7220957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
7230957b409SSimon J. Gerraty * \param out destination buffer for the hash output.
7240957b409SSimon J. Gerraty */
7250957b409SSimon J. Gerraty void br_sha256_out(const br_sha256_context *ctx, void *out);
7260957b409SSimon J. Gerraty
727*cc9e6590SSimon J. Gerraty #ifdef BR_DOXYGEN_IGNORE
7280957b409SSimon J. Gerraty /**
7290957b409SSimon J. Gerraty * \brief Save SHA-256 running state.
7300957b409SSimon J. Gerraty *
7310957b409SSimon J. Gerraty * The running state for SHA-256 (output of the last internal block
7320957b409SSimon J. Gerraty * processing) is written in the buffer pointed to by `out`. The
7330957b409SSimon J. Gerraty * number of bytes injected since the last initialisation or reset
7340957b409SSimon J. Gerraty * call is returned. The context is not modified.
7350957b409SSimon J. Gerraty *
7360957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
7370957b409SSimon J. Gerraty * \param out destination buffer for the running state.
7380957b409SSimon J. Gerraty * \return the injected total byte length.
7390957b409SSimon J. Gerraty */
7400957b409SSimon J. Gerraty uint64_t br_sha256_state(const br_sha256_context *ctx, void *out);
7410957b409SSimon J. Gerraty #else
7420957b409SSimon J. Gerraty #define br_sha256_state br_sha224_state
7430957b409SSimon J. Gerraty #endif
7440957b409SSimon J. Gerraty
745*cc9e6590SSimon J. Gerraty #ifdef BR_DOXYGEN_IGNORE
7460957b409SSimon J. Gerraty /**
7470957b409SSimon J. Gerraty * \brief Restore SHA-256 running state.
7480957b409SSimon J. Gerraty *
7490957b409SSimon J. Gerraty * The running state for SHA-256 is set to the provided values.
7500957b409SSimon J. Gerraty *
7510957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
7520957b409SSimon J. Gerraty * \param stb source buffer for the running state.
7530957b409SSimon J. Gerraty * \param count the injected total byte length.
7540957b409SSimon J. Gerraty */
7550957b409SSimon J. Gerraty void br_sha256_set_state(br_sha256_context *ctx,
7560957b409SSimon J. Gerraty const void *stb, uint64_t count);
7570957b409SSimon J. Gerraty #else
7580957b409SSimon J. Gerraty #define br_sha256_set_state br_sha224_set_state
7590957b409SSimon J. Gerraty #endif
7600957b409SSimon J. Gerraty
7610957b409SSimon J. Gerraty /**
7620957b409SSimon J. Gerraty * \brief Symbolic identifier for SHA-384.
7630957b409SSimon J. Gerraty */
7640957b409SSimon J. Gerraty #define br_sha384_ID 5
7650957b409SSimon J. Gerraty
7660957b409SSimon J. Gerraty /**
7670957b409SSimon J. Gerraty * \brief SHA-384 output size (in bytes).
7680957b409SSimon J. Gerraty */
7690957b409SSimon J. Gerraty #define br_sha384_SIZE 48
7700957b409SSimon J. Gerraty
7710957b409SSimon J. Gerraty /**
7720957b409SSimon J. Gerraty * \brief Constant vtable for SHA-384.
7730957b409SSimon J. Gerraty */
7740957b409SSimon J. Gerraty extern const br_hash_class br_sha384_vtable;
7750957b409SSimon J. Gerraty
7760957b409SSimon J. Gerraty /**
7770957b409SSimon J. Gerraty * \brief SHA-384 context.
7780957b409SSimon J. Gerraty *
7790957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation
7800957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code.
7810957b409SSimon J. Gerraty */
7820957b409SSimon J. Gerraty typedef struct {
7830957b409SSimon J. Gerraty /**
7840957b409SSimon J. Gerraty * \brief Pointer to vtable for this context.
7850957b409SSimon J. Gerraty */
7860957b409SSimon J. Gerraty const br_hash_class *vtable;
7870957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
7880957b409SSimon J. Gerraty unsigned char buf[128];
7890957b409SSimon J. Gerraty uint64_t count;
7900957b409SSimon J. Gerraty uint64_t val[8];
7910957b409SSimon J. Gerraty #endif
7920957b409SSimon J. Gerraty } br_sha384_context;
7930957b409SSimon J. Gerraty
7940957b409SSimon J. Gerraty /**
7950957b409SSimon J. Gerraty * \brief SHA-384 context initialisation.
7960957b409SSimon J. Gerraty *
7970957b409SSimon J. Gerraty * This function initialises or resets a context for a new SHA-384
7980957b409SSimon J. Gerraty * computation. It also sets the vtable pointer.
7990957b409SSimon J. Gerraty *
8000957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
8010957b409SSimon J. Gerraty */
8020957b409SSimon J. Gerraty void br_sha384_init(br_sha384_context *ctx);
8030957b409SSimon J. Gerraty
8040957b409SSimon J. Gerraty /**
8050957b409SSimon J. Gerraty * \brief Inject some data bytes in a running SHA-384 computation.
8060957b409SSimon J. Gerraty *
8070957b409SSimon J. Gerraty * The provided context is updated with some data bytes. If the number
8080957b409SSimon J. Gerraty * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
8090957b409SSimon J. Gerraty * and may be `NULL`, and this function does nothing.
8100957b409SSimon J. Gerraty *
8110957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
8120957b409SSimon J. Gerraty * \param data pointer to the injected data.
8130957b409SSimon J. Gerraty * \param len injected data length (in bytes).
8140957b409SSimon J. Gerraty */
8150957b409SSimon J. Gerraty void br_sha384_update(br_sha384_context *ctx, const void *data, size_t len);
8160957b409SSimon J. Gerraty
8170957b409SSimon J. Gerraty /**
8180957b409SSimon J. Gerraty * \brief Compute SHA-384 output.
8190957b409SSimon J. Gerraty *
8200957b409SSimon J. Gerraty * The SHA-384 output for the concatenation of all bytes injected in the
8210957b409SSimon J. Gerraty * provided context since the last initialisation or reset call, is
8220957b409SSimon J. Gerraty * computed and written in the buffer pointed to by `out`. The context
8230957b409SSimon J. Gerraty * itself is not modified, so extra bytes may be injected afterwards
8240957b409SSimon J. Gerraty * to continue that computation.
8250957b409SSimon J. Gerraty *
8260957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
8270957b409SSimon J. Gerraty * \param out destination buffer for the hash output.
8280957b409SSimon J. Gerraty */
8290957b409SSimon J. Gerraty void br_sha384_out(const br_sha384_context *ctx, void *out);
8300957b409SSimon J. Gerraty
8310957b409SSimon J. Gerraty /**
8320957b409SSimon J. Gerraty * \brief Save SHA-384 running state.
8330957b409SSimon J. Gerraty *
8340957b409SSimon J. Gerraty * The running state for SHA-384 (output of the last internal block
8350957b409SSimon J. Gerraty * processing) is written in the buffer pointed to by `out`. The
8360957b409SSimon J. Gerraty * number of bytes injected since the last initialisation or reset
8370957b409SSimon J. Gerraty * call is returned. The context is not modified.
8380957b409SSimon J. Gerraty *
8390957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
8400957b409SSimon J. Gerraty * \param out destination buffer for the running state.
8410957b409SSimon J. Gerraty * \return the injected total byte length.
8420957b409SSimon J. Gerraty */
8430957b409SSimon J. Gerraty uint64_t br_sha384_state(const br_sha384_context *ctx, void *out);
8440957b409SSimon J. Gerraty
8450957b409SSimon J. Gerraty /**
8460957b409SSimon J. Gerraty * \brief Restore SHA-384 running state.
8470957b409SSimon J. Gerraty *
8480957b409SSimon J. Gerraty * The running state for SHA-384 is set to the provided values.
8490957b409SSimon J. Gerraty *
8500957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
8510957b409SSimon J. Gerraty * \param stb source buffer for the running state.
8520957b409SSimon J. Gerraty * \param count the injected total byte length.
8530957b409SSimon J. Gerraty */
8540957b409SSimon J. Gerraty void br_sha384_set_state(br_sha384_context *ctx,
8550957b409SSimon J. Gerraty const void *stb, uint64_t count);
8560957b409SSimon J. Gerraty
8570957b409SSimon J. Gerraty /**
8580957b409SSimon J. Gerraty * \brief Symbolic identifier for SHA-512.
8590957b409SSimon J. Gerraty */
8600957b409SSimon J. Gerraty #define br_sha512_ID 6
8610957b409SSimon J. Gerraty
8620957b409SSimon J. Gerraty /**
8630957b409SSimon J. Gerraty * \brief SHA-512 output size (in bytes).
8640957b409SSimon J. Gerraty */
8650957b409SSimon J. Gerraty #define br_sha512_SIZE 64
8660957b409SSimon J. Gerraty
8670957b409SSimon J. Gerraty /**
8680957b409SSimon J. Gerraty * \brief Constant vtable for SHA-512.
8690957b409SSimon J. Gerraty */
8700957b409SSimon J. Gerraty extern const br_hash_class br_sha512_vtable;
8710957b409SSimon J. Gerraty
8720957b409SSimon J. Gerraty #ifdef BR_DOXYGEN_IGNORE
8730957b409SSimon J. Gerraty /**
8740957b409SSimon J. Gerraty * \brief SHA-512 context.
8750957b409SSimon J. Gerraty *
8760957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation
8770957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code.
8780957b409SSimon J. Gerraty */
8790957b409SSimon J. Gerraty typedef struct {
8800957b409SSimon J. Gerraty /**
8810957b409SSimon J. Gerraty * \brief Pointer to vtable for this context.
8820957b409SSimon J. Gerraty */
8830957b409SSimon J. Gerraty const br_hash_class *vtable;
8840957b409SSimon J. Gerraty } br_sha512_context;
8850957b409SSimon J. Gerraty #else
8860957b409SSimon J. Gerraty typedef br_sha384_context br_sha512_context;
8870957b409SSimon J. Gerraty #endif
8880957b409SSimon J. Gerraty
8890957b409SSimon J. Gerraty /**
8900957b409SSimon J. Gerraty * \brief SHA-512 context initialisation.
8910957b409SSimon J. Gerraty *
8920957b409SSimon J. Gerraty * This function initialises or resets a context for a new SHA-512
8930957b409SSimon J. Gerraty * computation. It also sets the vtable pointer.
8940957b409SSimon J. Gerraty *
8950957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
8960957b409SSimon J. Gerraty */
8970957b409SSimon J. Gerraty void br_sha512_init(br_sha512_context *ctx);
8980957b409SSimon J. Gerraty
8990957b409SSimon J. Gerraty #ifdef BR_DOXYGEN_IGNORE
9000957b409SSimon J. Gerraty /**
9010957b409SSimon J. Gerraty * \brief Inject some data bytes in a running SHA-512 computation.
9020957b409SSimon J. Gerraty *
9030957b409SSimon J. Gerraty * The provided context is updated with some data bytes. If the number
9040957b409SSimon J. Gerraty * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
9050957b409SSimon J. Gerraty * and may be `NULL`, and this function does nothing.
9060957b409SSimon J. Gerraty *
9070957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
9080957b409SSimon J. Gerraty * \param data pointer to the injected data.
9090957b409SSimon J. Gerraty * \param len injected data length (in bytes).
9100957b409SSimon J. Gerraty */
9110957b409SSimon J. Gerraty void br_sha512_update(br_sha512_context *ctx, const void *data, size_t len);
9120957b409SSimon J. Gerraty #else
9130957b409SSimon J. Gerraty #define br_sha512_update br_sha384_update
9140957b409SSimon J. Gerraty #endif
9150957b409SSimon J. Gerraty
9160957b409SSimon J. Gerraty /**
9170957b409SSimon J. Gerraty * \brief Compute SHA-512 output.
9180957b409SSimon J. Gerraty *
9190957b409SSimon J. Gerraty * The SHA-512 output for the concatenation of all bytes injected in the
9200957b409SSimon J. Gerraty * provided context since the last initialisation or reset call, is
9210957b409SSimon J. Gerraty * computed and written in the buffer pointed to by `out`. The context
9220957b409SSimon J. Gerraty * itself is not modified, so extra bytes may be injected afterwards
9230957b409SSimon J. Gerraty * to continue that computation.
9240957b409SSimon J. Gerraty *
9250957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
9260957b409SSimon J. Gerraty * \param out destination buffer for the hash output.
9270957b409SSimon J. Gerraty */
9280957b409SSimon J. Gerraty void br_sha512_out(const br_sha512_context *ctx, void *out);
9290957b409SSimon J. Gerraty
9300957b409SSimon J. Gerraty #ifdef BR_DOXYGEN_IGNORE
9310957b409SSimon J. Gerraty /**
9320957b409SSimon J. Gerraty * \brief Save SHA-512 running state.
9330957b409SSimon J. Gerraty *
9340957b409SSimon J. Gerraty * The running state for SHA-512 (output of the last internal block
9350957b409SSimon J. Gerraty * processing) is written in the buffer pointed to by `out`. The
9360957b409SSimon J. Gerraty * number of bytes injected since the last initialisation or reset
9370957b409SSimon J. Gerraty * call is returned. The context is not modified.
9380957b409SSimon J. Gerraty *
9390957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
9400957b409SSimon J. Gerraty * \param out destination buffer for the running state.
9410957b409SSimon J. Gerraty * \return the injected total byte length.
9420957b409SSimon J. Gerraty */
9430957b409SSimon J. Gerraty uint64_t br_sha512_state(const br_sha512_context *ctx, void *out);
9440957b409SSimon J. Gerraty #else
9450957b409SSimon J. Gerraty #define br_sha512_state br_sha384_state
9460957b409SSimon J. Gerraty #endif
9470957b409SSimon J. Gerraty
9480957b409SSimon J. Gerraty #ifdef BR_DOXYGEN_IGNORE
9490957b409SSimon J. Gerraty /**
9500957b409SSimon J. Gerraty * \brief Restore SHA-512 running state.
9510957b409SSimon J. Gerraty *
9520957b409SSimon J. Gerraty * The running state for SHA-512 is set to the provided values.
9530957b409SSimon J. Gerraty *
9540957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
9550957b409SSimon J. Gerraty * \param stb source buffer for the running state.
9560957b409SSimon J. Gerraty * \param count the injected total byte length.
9570957b409SSimon J. Gerraty */
9580957b409SSimon J. Gerraty void br_sha512_set_state(br_sha512_context *ctx,
9590957b409SSimon J. Gerraty const void *stb, uint64_t count);
9600957b409SSimon J. Gerraty #else
9610957b409SSimon J. Gerraty #define br_sha512_set_state br_sha384_set_state
9620957b409SSimon J. Gerraty #endif
9630957b409SSimon J. Gerraty
9640957b409SSimon J. Gerraty /*
9650957b409SSimon J. Gerraty * "md5sha1" is a special hash function that computes both MD5 and SHA-1
9660957b409SSimon J. Gerraty * on the same input, and produces a 36-byte output (MD5 and SHA-1
9670957b409SSimon J. Gerraty * concatenation, in that order). State size is also 36 bytes.
9680957b409SSimon J. Gerraty */
9690957b409SSimon J. Gerraty
9700957b409SSimon J. Gerraty /**
9710957b409SSimon J. Gerraty * \brief Symbolic identifier for MD5+SHA-1.
9720957b409SSimon J. Gerraty *
9730957b409SSimon J. Gerraty * MD5+SHA-1 is the concatenation of MD5 and SHA-1, computed over the
9740957b409SSimon J. Gerraty * same input. It is not one of the functions identified in TLS, so
9750957b409SSimon J. Gerraty * we give it a symbolic identifier of value 0.
9760957b409SSimon J. Gerraty */
9770957b409SSimon J. Gerraty #define br_md5sha1_ID 0
9780957b409SSimon J. Gerraty
9790957b409SSimon J. Gerraty /**
9800957b409SSimon J. Gerraty * \brief MD5+SHA-1 output size (in bytes).
9810957b409SSimon J. Gerraty */
9820957b409SSimon J. Gerraty #define br_md5sha1_SIZE 36
9830957b409SSimon J. Gerraty
9840957b409SSimon J. Gerraty /**
9850957b409SSimon J. Gerraty * \brief Constant vtable for MD5+SHA-1.
9860957b409SSimon J. Gerraty */
9870957b409SSimon J. Gerraty extern const br_hash_class br_md5sha1_vtable;
9880957b409SSimon J. Gerraty
9890957b409SSimon J. Gerraty /**
9900957b409SSimon J. Gerraty * \brief MD5+SHA-1 context.
9910957b409SSimon J. Gerraty *
9920957b409SSimon J. Gerraty * First field is a pointer to the vtable; it is set by the initialisation
9930957b409SSimon J. Gerraty * function. Other fields are not supposed to be accessed by user code.
9940957b409SSimon J. Gerraty */
9950957b409SSimon J. Gerraty typedef struct {
9960957b409SSimon J. Gerraty /**
9970957b409SSimon J. Gerraty * \brief Pointer to vtable for this context.
9980957b409SSimon J. Gerraty */
9990957b409SSimon J. Gerraty const br_hash_class *vtable;
10000957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
10010957b409SSimon J. Gerraty unsigned char buf[64];
10020957b409SSimon J. Gerraty uint64_t count;
10030957b409SSimon J. Gerraty uint32_t val_md5[4];
10040957b409SSimon J. Gerraty uint32_t val_sha1[5];
10050957b409SSimon J. Gerraty #endif
10060957b409SSimon J. Gerraty } br_md5sha1_context;
10070957b409SSimon J. Gerraty
10080957b409SSimon J. Gerraty /**
10090957b409SSimon J. Gerraty * \brief MD5+SHA-1 context initialisation.
10100957b409SSimon J. Gerraty *
10110957b409SSimon J. Gerraty * This function initialises or resets a context for a new SHA-512
10120957b409SSimon J. Gerraty * computation. It also sets the vtable pointer.
10130957b409SSimon J. Gerraty *
10140957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
10150957b409SSimon J. Gerraty */
10160957b409SSimon J. Gerraty void br_md5sha1_init(br_md5sha1_context *ctx);
10170957b409SSimon J. Gerraty
10180957b409SSimon J. Gerraty /**
10190957b409SSimon J. Gerraty * \brief Inject some data bytes in a running MD5+SHA-1 computation.
10200957b409SSimon J. Gerraty *
10210957b409SSimon J. Gerraty * The provided context is updated with some data bytes. If the number
10220957b409SSimon J. Gerraty * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
10230957b409SSimon J. Gerraty * and may be `NULL`, and this function does nothing.
10240957b409SSimon J. Gerraty *
10250957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
10260957b409SSimon J. Gerraty * \param data pointer to the injected data.
10270957b409SSimon J. Gerraty * \param len injected data length (in bytes).
10280957b409SSimon J. Gerraty */
10290957b409SSimon J. Gerraty void br_md5sha1_update(br_md5sha1_context *ctx, const void *data, size_t len);
10300957b409SSimon J. Gerraty
10310957b409SSimon J. Gerraty /**
10320957b409SSimon J. Gerraty * \brief Compute MD5+SHA-1 output.
10330957b409SSimon J. Gerraty *
10340957b409SSimon J. Gerraty * The MD5+SHA-1 output for the concatenation of all bytes injected in the
10350957b409SSimon J. Gerraty * provided context since the last initialisation or reset call, is
10360957b409SSimon J. Gerraty * computed and written in the buffer pointed to by `out`. The context
10370957b409SSimon J. Gerraty * itself is not modified, so extra bytes may be injected afterwards
10380957b409SSimon J. Gerraty * to continue that computation.
10390957b409SSimon J. Gerraty *
10400957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
10410957b409SSimon J. Gerraty * \param out destination buffer for the hash output.
10420957b409SSimon J. Gerraty */
10430957b409SSimon J. Gerraty void br_md5sha1_out(const br_md5sha1_context *ctx, void *out);
10440957b409SSimon J. Gerraty
10450957b409SSimon J. Gerraty /**
10460957b409SSimon J. Gerraty * \brief Save MD5+SHA-1 running state.
10470957b409SSimon J. Gerraty *
10480957b409SSimon J. Gerraty * The running state for MD5+SHA-1 (output of the last internal block
10490957b409SSimon J. Gerraty * processing) is written in the buffer pointed to by `out`. The
10500957b409SSimon J. Gerraty * number of bytes injected since the last initialisation or reset
10510957b409SSimon J. Gerraty * call is returned. The context is not modified.
10520957b409SSimon J. Gerraty *
10530957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
10540957b409SSimon J. Gerraty * \param out destination buffer for the running state.
10550957b409SSimon J. Gerraty * \return the injected total byte length.
10560957b409SSimon J. Gerraty */
10570957b409SSimon J. Gerraty uint64_t br_md5sha1_state(const br_md5sha1_context *ctx, void *out);
10580957b409SSimon J. Gerraty
10590957b409SSimon J. Gerraty /**
10600957b409SSimon J. Gerraty * \brief Restore MD5+SHA-1 running state.
10610957b409SSimon J. Gerraty *
10620957b409SSimon J. Gerraty * The running state for MD5+SHA-1 is set to the provided values.
10630957b409SSimon J. Gerraty *
10640957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
10650957b409SSimon J. Gerraty * \param stb source buffer for the running state.
10660957b409SSimon J. Gerraty * \param count the injected total byte length.
10670957b409SSimon J. Gerraty */
10680957b409SSimon J. Gerraty void br_md5sha1_set_state(br_md5sha1_context *ctx,
10690957b409SSimon J. Gerraty const void *stb, uint64_t count);
10700957b409SSimon J. Gerraty
10710957b409SSimon J. Gerraty /**
10720957b409SSimon J. Gerraty * \brief Aggregate context for configurable hash function support.
10730957b409SSimon J. Gerraty *
10740957b409SSimon J. Gerraty * The `br_hash_compat_context` type is a type which is large enough to
10750957b409SSimon J. Gerraty * serve as context for all standard hash functions defined above.
10760957b409SSimon J. Gerraty */
10770957b409SSimon J. Gerraty typedef union {
10780957b409SSimon J. Gerraty const br_hash_class *vtable;
10790957b409SSimon J. Gerraty br_md5_context md5;
10800957b409SSimon J. Gerraty br_sha1_context sha1;
10810957b409SSimon J. Gerraty br_sha224_context sha224;
10820957b409SSimon J. Gerraty br_sha256_context sha256;
10830957b409SSimon J. Gerraty br_sha384_context sha384;
10840957b409SSimon J. Gerraty br_sha512_context sha512;
10850957b409SSimon J. Gerraty br_md5sha1_context md5sha1;
10860957b409SSimon J. Gerraty } br_hash_compat_context;
10870957b409SSimon J. Gerraty
10880957b409SSimon J. Gerraty /*
10890957b409SSimon J. Gerraty * The multi-hasher is a construct that handles hashing of the same input
10900957b409SSimon J. Gerraty * data with several hash functions, with a single shared input buffer.
10910957b409SSimon J. Gerraty * It can handle MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512
10920957b409SSimon J. Gerraty * simultaneously, though which functions are activated depends on
10930957b409SSimon J. Gerraty * the set implementation pointers.
10940957b409SSimon J. Gerraty */
10950957b409SSimon J. Gerraty
10960957b409SSimon J. Gerraty /**
10970957b409SSimon J. Gerraty * \brief Multi-hasher context structure.
10980957b409SSimon J. Gerraty *
10990957b409SSimon J. Gerraty * The multi-hasher runs up to six hash functions in the standard TLS list
11000957b409SSimon J. Gerraty * (MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512) in parallel, over
11010957b409SSimon J. Gerraty * the same input.
11020957b409SSimon J. Gerraty *
11030957b409SSimon J. Gerraty * The multi-hasher does _not_ follow the OOP structure with a vtable.
11040957b409SSimon J. Gerraty * Instead, it is configured with the vtables of the hash functions it
11050957b409SSimon J. Gerraty * should run. Structure fields are not supposed to be accessed directly.
11060957b409SSimon J. Gerraty */
11070957b409SSimon J. Gerraty typedef struct {
11080957b409SSimon J. Gerraty #ifndef BR_DOXYGEN_IGNORE
11090957b409SSimon J. Gerraty unsigned char buf[128];
11100957b409SSimon J. Gerraty uint64_t count;
11110957b409SSimon J. Gerraty uint32_t val_32[25];
11120957b409SSimon J. Gerraty uint64_t val_64[16];
11130957b409SSimon J. Gerraty const br_hash_class *impl[6];
11140957b409SSimon J. Gerraty #endif
11150957b409SSimon J. Gerraty } br_multihash_context;
11160957b409SSimon J. Gerraty
11170957b409SSimon J. Gerraty /**
11180957b409SSimon J. Gerraty * \brief Clear a multi-hasher context.
11190957b409SSimon J. Gerraty *
11200957b409SSimon J. Gerraty * This should always be called once on a given context, _before_ setting
11210957b409SSimon J. Gerraty * the implementation pointers.
11220957b409SSimon J. Gerraty *
11230957b409SSimon J. Gerraty * \param ctx the multi-hasher context.
11240957b409SSimon J. Gerraty */
11250957b409SSimon J. Gerraty void br_multihash_zero(br_multihash_context *ctx);
11260957b409SSimon J. Gerraty
11270957b409SSimon J. Gerraty /**
11280957b409SSimon J. Gerraty * \brief Set a hash function implementation.
11290957b409SSimon J. Gerraty *
11300957b409SSimon J. Gerraty * Implementations shall be set _after_ clearing the context (with
11310957b409SSimon J. Gerraty * `br_multihash_zero()`) but _before_ initialising the computation
11320957b409SSimon J. Gerraty * (with `br_multihash_init()`). The hash function implementation
11330957b409SSimon J. Gerraty * MUST be one of the standard hash functions (MD5, SHA-1, SHA-224,
11340957b409SSimon J. Gerraty * SHA-256, SHA-384 or SHA-512); it may also be `NULL` to remove
11350957b409SSimon J. Gerraty * an implementation from the multi-hasher.
11360957b409SSimon J. Gerraty *
11370957b409SSimon J. Gerraty * \param ctx the multi-hasher context.
11380957b409SSimon J. Gerraty * \param id the hash function symbolic identifier.
11390957b409SSimon J. Gerraty * \param impl the hash function vtable, or `NULL`.
11400957b409SSimon J. Gerraty */
11410957b409SSimon J. Gerraty static inline void
br_multihash_setimpl(br_multihash_context * ctx,int id,const br_hash_class * impl)11420957b409SSimon J. Gerraty br_multihash_setimpl(br_multihash_context *ctx,
11430957b409SSimon J. Gerraty int id, const br_hash_class *impl)
11440957b409SSimon J. Gerraty {
11450957b409SSimon J. Gerraty /*
11460957b409SSimon J. Gerraty * This code relies on hash functions ID being values 1 to 6,
11470957b409SSimon J. Gerraty * in the MD5 to SHA-512 order.
11480957b409SSimon J. Gerraty */
11490957b409SSimon J. Gerraty ctx->impl[id - 1] = impl;
11500957b409SSimon J. Gerraty }
11510957b409SSimon J. Gerraty
11520957b409SSimon J. Gerraty /**
11530957b409SSimon J. Gerraty * \brief Get a hash function implementation.
11540957b409SSimon J. Gerraty *
11550957b409SSimon J. Gerraty * This function returns the currently configured vtable for a given
11560957b409SSimon J. Gerraty * hash function (by symbolic ID). If no such function was configured in
11570957b409SSimon J. Gerraty * the provided multi-hasher context, then this function returns `NULL`.
11580957b409SSimon J. Gerraty *
11590957b409SSimon J. Gerraty * \param ctx the multi-hasher context.
11600957b409SSimon J. Gerraty * \param id the hash function symbolic identifier.
11610957b409SSimon J. Gerraty * \return the hash function vtable, or `NULL`.
11620957b409SSimon J. Gerraty */
11630957b409SSimon J. Gerraty static inline const br_hash_class *
br_multihash_getimpl(const br_multihash_context * ctx,int id)11640957b409SSimon J. Gerraty br_multihash_getimpl(const br_multihash_context *ctx, int id)
11650957b409SSimon J. Gerraty {
11660957b409SSimon J. Gerraty return ctx->impl[id - 1];
11670957b409SSimon J. Gerraty }
11680957b409SSimon J. Gerraty
11690957b409SSimon J. Gerraty /**
11700957b409SSimon J. Gerraty * \brief Reset a multi-hasher context.
11710957b409SSimon J. Gerraty *
11720957b409SSimon J. Gerraty * This function prepares the context for a new hashing computation,
11730957b409SSimon J. Gerraty * for all implementations configured at that point.
11740957b409SSimon J. Gerraty *
11750957b409SSimon J. Gerraty * \param ctx the multi-hasher context.
11760957b409SSimon J. Gerraty */
11770957b409SSimon J. Gerraty void br_multihash_init(br_multihash_context *ctx);
11780957b409SSimon J. Gerraty
11790957b409SSimon J. Gerraty /**
11800957b409SSimon J. Gerraty * \brief Inject some data bytes in a running multi-hashing computation.
11810957b409SSimon J. Gerraty *
11820957b409SSimon J. Gerraty * The provided context is updated with some data bytes. If the number
11830957b409SSimon J. Gerraty * of bytes (`len`) is zero, then the data pointer (`data`) is ignored
11840957b409SSimon J. Gerraty * and may be `NULL`, and this function does nothing.
11850957b409SSimon J. Gerraty *
11860957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
11870957b409SSimon J. Gerraty * \param data pointer to the injected data.
11880957b409SSimon J. Gerraty * \param len injected data length (in bytes).
11890957b409SSimon J. Gerraty */
11900957b409SSimon J. Gerraty void br_multihash_update(br_multihash_context *ctx,
11910957b409SSimon J. Gerraty const void *data, size_t len);
11920957b409SSimon J. Gerraty
11930957b409SSimon J. Gerraty /**
11940957b409SSimon J. Gerraty * \brief Compute a hash output from a multi-hasher.
11950957b409SSimon J. Gerraty *
11960957b409SSimon J. Gerraty * The hash output for the concatenation of all bytes injected in the
11970957b409SSimon J. Gerraty * provided context since the last initialisation or reset call, is
11980957b409SSimon J. Gerraty * computed and written in the buffer pointed to by `dst`. The hash
11990957b409SSimon J. Gerraty * function to use is identified by `id` and must be one of the standard
12000957b409SSimon J. Gerraty * hash functions. If that hash function was indeed configured in the
12010957b409SSimon J. Gerraty * multi-hasher context, the corresponding hash value is written in
12020957b409SSimon J. Gerraty * `dst` and its length (in bytes) is returned. If the hash function
12030957b409SSimon J. Gerraty * was _not_ configured, then nothing is written in `dst` and 0 is
12040957b409SSimon J. Gerraty * returned.
12050957b409SSimon J. Gerraty *
12060957b409SSimon J. Gerraty * The context itself is not modified, so extra bytes may be injected
12070957b409SSimon J. Gerraty * afterwards to continue the hash computations.
12080957b409SSimon J. Gerraty *
12090957b409SSimon J. Gerraty * \param ctx pointer to the context structure.
12100957b409SSimon J. Gerraty * \param id the hash function symbolic identifier.
12110957b409SSimon J. Gerraty * \param dst destination buffer for the hash output.
12120957b409SSimon J. Gerraty * \return the hash output length (in bytes), or 0.
12130957b409SSimon J. Gerraty */
12140957b409SSimon J. Gerraty size_t br_multihash_out(const br_multihash_context *ctx, int id, void *dst);
12150957b409SSimon J. Gerraty
12160957b409SSimon J. Gerraty /**
12170957b409SSimon J. Gerraty * \brief Type for a GHASH implementation.
12180957b409SSimon J. Gerraty *
12190957b409SSimon J. Gerraty * GHASH is a sort of keyed hash meant to be used to implement GCM in
12200957b409SSimon J. Gerraty * combination with a block cipher (with 16-byte blocks).
12210957b409SSimon J. Gerraty *
12220957b409SSimon J. Gerraty * The `y` array has length 16 bytes and is used for input and output; in
12230957b409SSimon J. Gerraty * a complete GHASH run, it starts with an all-zero value. `h` is a 16-byte
12240957b409SSimon J. Gerraty * value that serves as key (it is derived from the encryption key in GCM,
12250957b409SSimon J. Gerraty * using the block cipher). The data length (`len`) is expressed in bytes.
12260957b409SSimon J. Gerraty * The `y` array is updated.
12270957b409SSimon J. Gerraty *
12280957b409SSimon J. Gerraty * If the data length is not a multiple of 16, then the data is implicitly
12290957b409SSimon J. Gerraty * padded with zeros up to the next multiple of 16. Thus, when using GHASH
12300957b409SSimon J. Gerraty * in GCM, this method may be called twice, for the associated data and
12310957b409SSimon J. Gerraty * for the ciphertext, respectively; the zero-padding implements exactly
12320957b409SSimon J. Gerraty * the GCM rules.
12330957b409SSimon J. Gerraty *
12340957b409SSimon J. Gerraty * \param y the array to update.
12350957b409SSimon J. Gerraty * \param h the GHASH key.
12360957b409SSimon J. Gerraty * \param data the input data (may be `NULL` if `len` is zero).
12370957b409SSimon J. Gerraty * \param len the input data length (in bytes).
12380957b409SSimon J. Gerraty */
12390957b409SSimon J. Gerraty typedef void (*br_ghash)(void *y, const void *h, const void *data, size_t len);
12400957b409SSimon J. Gerraty
12410957b409SSimon J. Gerraty /**
12420957b409SSimon J. Gerraty * \brief GHASH implementation using multiplications (mixed 32-bit).
12430957b409SSimon J. Gerraty *
12440957b409SSimon J. Gerraty * This implementation uses multiplications of 32-bit values, with a
12450957b409SSimon J. Gerraty * 64-bit result. It is constant-time (if multiplications are
12460957b409SSimon J. Gerraty * constant-time).
12470957b409SSimon J. Gerraty *
12480957b409SSimon J. Gerraty * \param y the array to update.
12490957b409SSimon J. Gerraty * \param h the GHASH key.
12500957b409SSimon J. Gerraty * \param data the input data (may be `NULL` if `len` is zero).
12510957b409SSimon J. Gerraty * \param len the input data length (in bytes).
12520957b409SSimon J. Gerraty */
12530957b409SSimon J. Gerraty void br_ghash_ctmul(void *y, const void *h, const void *data, size_t len);
12540957b409SSimon J. Gerraty
12550957b409SSimon J. Gerraty /**
12560957b409SSimon J. Gerraty * \brief GHASH implementation using multiplications (strict 32-bit).
12570957b409SSimon J. Gerraty *
12580957b409SSimon J. Gerraty * This implementation uses multiplications of 32-bit values, with a
12590957b409SSimon J. Gerraty * 32-bit result. It is usually somewhat slower than `br_ghash_ctmul()`,
12600957b409SSimon J. Gerraty * but it is expected to be faster on architectures for which the
12610957b409SSimon J. Gerraty * 32-bit multiplication opcode does not yield the upper 32 bits of the
12620957b409SSimon J. Gerraty * product. It is constant-time (if multiplications are constant-time).
12630957b409SSimon J. Gerraty *
12640957b409SSimon J. Gerraty * \param y the array to update.
12650957b409SSimon J. Gerraty * \param h the GHASH key.
12660957b409SSimon J. Gerraty * \param data the input data (may be `NULL` if `len` is zero).
12670957b409SSimon J. Gerraty * \param len the input data length (in bytes).
12680957b409SSimon J. Gerraty */
12690957b409SSimon J. Gerraty void br_ghash_ctmul32(void *y, const void *h, const void *data, size_t len);
12700957b409SSimon J. Gerraty
12710957b409SSimon J. Gerraty /**
12720957b409SSimon J. Gerraty * \brief GHASH implementation using multiplications (64-bit).
12730957b409SSimon J. Gerraty *
12740957b409SSimon J. Gerraty * This implementation uses multiplications of 64-bit values, with a
12750957b409SSimon J. Gerraty * 64-bit result. It is constant-time (if multiplications are
12760957b409SSimon J. Gerraty * constant-time). It is substantially faster than `br_ghash_ctmul()`
12770957b409SSimon J. Gerraty * and `br_ghash_ctmul32()` on most 64-bit architectures.
12780957b409SSimon J. Gerraty *
12790957b409SSimon J. Gerraty * \param y the array to update.
12800957b409SSimon J. Gerraty * \param h the GHASH key.
12810957b409SSimon J. Gerraty * \param data the input data (may be `NULL` if `len` is zero).
12820957b409SSimon J. Gerraty * \param len the input data length (in bytes).
12830957b409SSimon J. Gerraty */
12840957b409SSimon J. Gerraty void br_ghash_ctmul64(void *y, const void *h, const void *data, size_t len);
12850957b409SSimon J. Gerraty
12860957b409SSimon J. Gerraty /**
12870957b409SSimon J. Gerraty * \brief GHASH implementation using the `pclmulqdq` opcode (part of the
12880957b409SSimon J. Gerraty * AES-NI instructions).
12890957b409SSimon J. Gerraty *
12900957b409SSimon J. Gerraty * This implementation is available only on x86 platforms where the
12910957b409SSimon J. Gerraty * compiler supports the relevant intrinsic functions. Even if the
12920957b409SSimon J. Gerraty * compiler supports these functions, the local CPU might not support
12930957b409SSimon J. Gerraty * the `pclmulqdq` opcode, meaning that a call will fail with an
12940957b409SSimon J. Gerraty * illegal instruction exception. To safely obtain a pointer to this
12950957b409SSimon J. Gerraty * function when supported (or 0 otherwise), use `br_ghash_pclmul_get()`.
12960957b409SSimon J. Gerraty *
12970957b409SSimon J. Gerraty * \param y the array to update.
12980957b409SSimon J. Gerraty * \param h the GHASH key.
12990957b409SSimon J. Gerraty * \param data the input data (may be `NULL` if `len` is zero).
13000957b409SSimon J. Gerraty * \param len the input data length (in bytes).
13010957b409SSimon J. Gerraty */
13020957b409SSimon J. Gerraty void br_ghash_pclmul(void *y, const void *h, const void *data, size_t len);
13030957b409SSimon J. Gerraty
13040957b409SSimon J. Gerraty /**
13050957b409SSimon J. Gerraty * \brief Obtain the `pclmul` GHASH implementation, if available.
13060957b409SSimon J. Gerraty *
13070957b409SSimon J. Gerraty * If the `pclmul` implementation was compiled in the library (depending
13080957b409SSimon J. Gerraty * on the compiler abilities) _and_ the local CPU appears to support the
13090957b409SSimon J. Gerraty * opcode, then this function will return a pointer to the
13100957b409SSimon J. Gerraty * `br_ghash_pclmul()` function. Otherwise, it will return `0`.
13110957b409SSimon J. Gerraty *
13120957b409SSimon J. Gerraty * \return the `pclmul` GHASH implementation, or `0`.
13130957b409SSimon J. Gerraty */
13140957b409SSimon J. Gerraty br_ghash br_ghash_pclmul_get(void);
13150957b409SSimon J. Gerraty
13160957b409SSimon J. Gerraty /**
13170957b409SSimon J. Gerraty * \brief GHASH implementation using the POWER8 opcodes.
13180957b409SSimon J. Gerraty *
13190957b409SSimon J. Gerraty * This implementation is available only on POWER8 platforms (and later).
13200957b409SSimon J. Gerraty * To safely obtain a pointer to this function when supported (or 0
13210957b409SSimon J. Gerraty * otherwise), use `br_ghash_pwr8_get()`.
13220957b409SSimon J. Gerraty *
13230957b409SSimon J. Gerraty * \param y the array to update.
13240957b409SSimon J. Gerraty * \param h the GHASH key.
13250957b409SSimon J. Gerraty * \param data the input data (may be `NULL` if `len` is zero).
13260957b409SSimon J. Gerraty * \param len the input data length (in bytes).
13270957b409SSimon J. Gerraty */
13280957b409SSimon J. Gerraty void br_ghash_pwr8(void *y, const void *h, const void *data, size_t len);
13290957b409SSimon J. Gerraty
13300957b409SSimon J. Gerraty /**
13310957b409SSimon J. Gerraty * \brief Obtain the `pwr8` GHASH implementation, if available.
13320957b409SSimon J. Gerraty *
13330957b409SSimon J. Gerraty * If the `pwr8` implementation was compiled in the library (depending
13340957b409SSimon J. Gerraty * on the compiler abilities) _and_ the local CPU appears to support the
13350957b409SSimon J. Gerraty * opcode, then this function will return a pointer to the
13360957b409SSimon J. Gerraty * `br_ghash_pwr8()` function. Otherwise, it will return `0`.
13370957b409SSimon J. Gerraty *
13380957b409SSimon J. Gerraty * \return the `pwr8` GHASH implementation, or `0`.
13390957b409SSimon J. Gerraty */
13400957b409SSimon J. Gerraty br_ghash br_ghash_pwr8_get(void);
13410957b409SSimon J. Gerraty
13420957b409SSimon J. Gerraty #ifdef __cplusplus
13430957b409SSimon J. Gerraty }
13440957b409SSimon J. Gerraty #endif
13450957b409SSimon J. Gerraty
13460957b409SSimon J. Gerraty #endif
1347