Path: blob/main/contrib/bearssl/inc/bearssl_hash.h
39586 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#ifndef BR_BEARSSL_HASH_H__25#define BR_BEARSSL_HASH_H__2627#include <stddef.h>28#include <stdint.h>29#include <string.h>3031#ifdef __cplusplus32extern "C" {33#endif3435/** \file bearssl_hash.h36*37* # Hash Functions38*39* This file documents the API for hash functions.40*41*42* ## Procedural API43*44* For each implemented hash function, of name "`xxx`", the following45* elements are defined:46*47* - `br_xxx_vtable`48*49* An externally defined instance of `br_hash_class`.50*51* - `br_xxx_SIZE`52*53* A macro that evaluates to the output size (in bytes) of the54* hash function.55*56* - `br_xxx_ID`57*58* A macro that evaluates to a symbolic identifier for the hash59* function. Such identifiers are used with HMAC and signature60* algorithm implementations.61*62* NOTE: for the "standard" hash functions defined in [the TLS63* standard](https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1),64* the symbolic identifiers match the constants used in TLS, i.e.65* 1 to 6 for MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512,66* respectively.67*68* - `br_xxx_context`69*70* Context for an ongoing computation. It is allocated by the71* caller, and a pointer to it is passed to all functions. A72* context contains no interior pointer, so it can be moved around73* and cloned (with a simple `memcpy()` or equivalent) in order to74* capture the function state at some point. Computations that use75* distinct context structures are independent of each other. The76* first field of `br_xxx_context` is always a pointer to the77* `br_xxx_vtable` structure; `br_xxx_init()` sets that pointer.78*79* - `br_xxx_init(br_xxx_context *ctx)`80*81* Initialise the provided context. Previous contents of the structure82* are ignored. This calls resets the context to the start of a new83* hash computation; it also sets the first field of the context84* structure (called `vtable`) to a pointer to the statically85* allocated constant `br_xxx_vtable` structure.86*87* - `br_xxx_update(br_xxx_context *ctx, const void *data, size_t len)`88*89* Add some more bytes to the hash computation represented by the90* provided context.91*92* - `br_xxx_out(const br_xxx_context *ctx, void *out)`93*94* Complete the hash computation and write the result in the provided95* buffer. The output buffer MUST be large enough to accommodate the96* result. The context is NOT modified by this operation, so this97* function can be used to get a "partial hash" while still keeping98* the possibility of adding more bytes to the input.99*100* - `br_xxx_state(const br_xxx_context *ctx, void *out)`101*102* Get a copy of the "current state" for the computation so far. For103* MD functions (MD5, SHA-1, SHA-2 family), this is the running state104* resulting from the processing of the last complete input block.105* Returned value is the current input length (in bytes).106*107* - `br_xxx_set_state(br_xxx_context *ctx, const void *stb, uint64_t count)`108*109* Set the internal state to the provided values. The 'stb' and110* 'count' values shall match that which was obtained from111* `br_xxx_state()`. This restores the hash state only if the state112* values were at an appropriate block boundary. This does NOT set113* the `vtable` pointer in the context.114*115* Context structures can be discarded without any explicit deallocation.116* Hash function implementations are purely software and don't reserve117* any resources outside of the context structure itself.118*119*120* ## Object-Oriented API121*122* For each hash function that follows the procedural API described123* above, an object-oriented API is also provided. In that API, function124* pointers from the vtable (`br_xxx_vtable`) are used. The vtable125* incarnates object-oriented programming. An introduction on the OOP126* concept used here can be read on the BearSSL Web site:<br />127* [https://www.bearssl.org/oop.html](https://www.bearssl.org/oop.html)128*129* The vtable offers functions called `init()`, `update()`, `out()`,130* `set()` and `set_state()`, which are in fact the functions from131* the procedural API. That vtable also contains two informative fields:132*133* - `context_size`134*135* The size of the context structure (`br_xxx_context`), in bytes.136* This can be used by generic implementations to perform dynamic137* context allocation.138*139* - `desc`140*141* A "descriptor" field that encodes some information on the hash142* function: symbolic identifier, output size, state size,143* internal block size, details on the padding.144*145* Users of this object-oriented API (in particular generic HMAC146* implementations) may make the following assumptions:147*148* - Hash output size is no more than 64 bytes.149* - Hash internal state size is no more than 64 bytes.150* - Internal block size is a power of two, no less than 16 and no more151* than 256.152*153*154* ## Implemented Hash Functions155*156* Implemented hash functions are:157*158* | Function | Name | Output length | State length |159* | :-------- | :------ | :-----------: | :----------: |160* | MD5 | md5 | 16 | 16 |161* | SHA-1 | sha1 | 20 | 20 |162* | SHA-224 | sha224 | 28 | 32 |163* | SHA-256 | sha256 | 32 | 32 |164* | SHA-384 | sha384 | 48 | 64 |165* | SHA-512 | sha512 | 64 | 64 |166* | MD5+SHA-1 | md5sha1 | 36 | 36 |167*168* (MD5+SHA-1 is the concatenation of MD5 and SHA-1 computed over the169* same input; in the implementation, the internal data buffer is170* shared, thus making it more memory-efficient than separate MD5 and171* SHA-1. It can be useful in implementing SSL 3.0, TLS 1.0 and TLS172* 1.1.)173*174*175* ## Multi-Hasher176*177* An aggregate hasher is provided, that can compute several standard178* hash functions in parallel. It uses `br_multihash_context` and a179* procedural API. It is configured with the implementations (the vtables)180* that it should use; it will then compute all these hash functions in181* parallel, on the same input. It is meant to be used in cases when the182* hash of an object will be used, but the exact hash function is not183* known yet (typically, streamed processing on X.509 certificates).184*185* Only the standard hash functions (MD5, SHA-1, SHA-224, SHA-256, SHA-384186* and SHA-512) are supported by the multi-hasher.187*188*189* ## GHASH190*191* GHASH is not a generic hash function; it is a _universal_ hash function,192* which, as the name does not say, means that it CANNOT be used in most193* places where a hash function is needed. GHASH is used within the GCM194* encryption mode, to provide the checked integrity functionality.195*196* A GHASH implementation is basically a function that uses the type defined197* in this file under the name `br_ghash`:198*199* typedef void (*br_ghash)(void *y, const void *h, const void *data, size_t len);200*201* The `y` pointer refers to a 16-byte value which is used as input, and202* receives the output of the GHASH invocation. `h` is a 16-byte secret203* value (that serves as key). `data` and `len` define the input data.204*205* Three GHASH implementations are provided, all constant-time, based on206* the use of integer multiplications with appropriate masking to cancel207* carry propagation.208*/209210/**211* \brief Class type for hash function implementations.212*213* A `br_hash_class` instance references the methods implementing a hash214* function. Constant instances of this structure are defined for each215* implemented hash function. Such instances are also called "vtables".216*217* Vtables are used to support object-oriented programming, as218* described on [the BearSSL Web site](https://www.bearssl.org/oop.html).219*/220typedef struct br_hash_class_ br_hash_class;221struct br_hash_class_ {222/**223* \brief Size (in bytes) of the context structure appropriate for224* computing this hash function.225*/226size_t context_size;227228/**229* \brief Descriptor word that contains information about the hash230* function.231*232* For each word `xxx` described below, use `BR_HASHDESC_xxx_OFF`233* and `BR_HASHDESC_xxx_MASK` to access the specific value, as234* follows:235*236* (hf->desc >> BR_HASHDESC_xxx_OFF) & BR_HASHDESC_xxx_MASK237*238* The defined elements are:239*240* - `ID`: the symbolic identifier for the function, as defined241* in [TLS](https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1)242* (MD5 = 1, SHA-1 = 2,...).243*244* - `OUT`: hash output size, in bytes.245*246* - `STATE`: internal running state size, in bytes.247*248* - `LBLEN`: base-2 logarithm for the internal block size, as249* defined for HMAC processing (this is 6 for MD5, SHA-1, SHA-224250* and SHA-256, since these functions use 64-byte blocks; for251* SHA-384 and SHA-512, this is 7, corresponding to their252* 128-byte blocks).253*254* The descriptor may contain a few other flags.255*/256uint32_t desc;257258/**259* \brief Initialisation method.260*261* This method takes as parameter a pointer to a context area,262* that it initialises. The first field of the context is set263* to this vtable; other elements are initialised for a new hash264* computation.265*266* \param ctx pointer to (the first field of) the context.267*/268void (*init)(const br_hash_class **ctx);269270/**271* \brief Data injection method.272*273* The `len` bytes starting at address `data` are injected into274* the running hash computation incarnated by the specified275* context. The context is updated accordingly. It is allowed276* to have `len == 0`, in which case `data` is ignored (and could277* be `NULL`), and nothing happens.278* on the input data.279*280* \param ctx pointer to (the first field of) the context.281* \param data pointer to the first data byte to inject.282* \param len number of bytes to inject.283*/284void (*update)(const br_hash_class **ctx, const void *data, size_t len);285286/**287* \brief Produce hash output.288*289* The hash output corresponding to all data bytes injected in the290* context since the last `init()` call is computed, and written291* in the buffer pointed to by `dst`. The hash output size depends292* on the implemented hash function (e.g. 16 bytes for MD5).293* The context is _not_ modified by this call, so further bytes294* may be afterwards injected to continue the current computation.295*296* \param ctx pointer to (the first field of) the context.297* \param dst destination buffer for the hash output.298*/299void (*out)(const br_hash_class *const *ctx, void *dst);300301/**302* \brief Get running state.303*304* This method saves the current running state into the `dst`305* buffer. What constitutes the "running state" depends on the306* hash function; for Merkle-Damgård hash functions (like307* MD5 or SHA-1), this is the output obtained after processing308* each block. The number of bytes injected so far is returned.309* The context is not modified by this call.310*311* \param ctx pointer to (the first field of) the context.312* \param dst destination buffer for the state.313* \return the injected total byte length.314*/315uint64_t (*state)(const br_hash_class *const *ctx, void *dst);316317/**318* \brief Set running state.319*320* This methods replaces the running state for the function.321*322* \param ctx pointer to (the first field of) the context.323* \param stb source buffer for the state.324* \param count injected total byte length.325*/326void (*set_state)(const br_hash_class **ctx,327const void *stb, uint64_t count);328};329330#ifndef BR_DOXYGEN_IGNORE331#define BR_HASHDESC_ID(id) ((uint32_t)(id) << BR_HASHDESC_ID_OFF)332#define BR_HASHDESC_ID_OFF 0333#define BR_HASHDESC_ID_MASK 0xFF334335#define BR_HASHDESC_OUT(size) ((uint32_t)(size) << BR_HASHDESC_OUT_OFF)336#define BR_HASHDESC_OUT_OFF 8337#define BR_HASHDESC_OUT_MASK 0x7F338339#define BR_HASHDESC_STATE(size) ((uint32_t)(size) << BR_HASHDESC_STATE_OFF)340#define BR_HASHDESC_STATE_OFF 15341#define BR_HASHDESC_STATE_MASK 0xFF342343#define BR_HASHDESC_LBLEN(ls) ((uint32_t)(ls) << BR_HASHDESC_LBLEN_OFF)344#define BR_HASHDESC_LBLEN_OFF 23345#define BR_HASHDESC_LBLEN_MASK 0x0F346347#define BR_HASHDESC_MD_PADDING ((uint32_t)1 << 28)348#define BR_HASHDESC_MD_PADDING_128 ((uint32_t)1 << 29)349#define BR_HASHDESC_MD_PADDING_BE ((uint32_t)1 << 30)350#endif351352/*353* Specific hash functions.354*355* Rules for contexts:356* -- No interior pointer.357* -- No pointer to external dynamically allocated resources.358* -- First field is called 'vtable' and is a pointer to a359* const-qualified br_hash_class instance (pointer is set by init()).360* -- SHA-224 and SHA-256 contexts are identical.361* -- SHA-384 and SHA-512 contexts are identical.362*363* Thus, contexts can be moved and cloned to capture the hash function364* current state; and there is no need for any explicit "release" function.365*/366367/**368* \brief Symbolic identifier for MD5.369*/370#define br_md5_ID 1371372/**373* \brief MD5 output size (in bytes).374*/375#define br_md5_SIZE 16376377/**378* \brief Constant vtable for MD5.379*/380extern const br_hash_class br_md5_vtable;381382/**383* \brief MD5 context.384*385* First field is a pointer to the vtable; it is set by the initialisation386* function. Other fields are not supposed to be accessed by user code.387*/388typedef struct {389/**390* \brief Pointer to vtable for this context.391*/392const br_hash_class *vtable;393#ifndef BR_DOXYGEN_IGNORE394unsigned char buf[64];395uint64_t count;396uint32_t val[4];397#endif398} br_md5_context;399400/**401* \brief MD5 context initialisation.402*403* This function initialises or resets a context for a new MD5404* computation. It also sets the vtable pointer.405*406* \param ctx pointer to the context structure.407*/408void br_md5_init(br_md5_context *ctx);409410/**411* \brief Inject some data bytes in a running MD5 computation.412*413* The provided context is updated with some data bytes. If the number414* of bytes (`len`) is zero, then the data pointer (`data`) is ignored415* and may be `NULL`, and this function does nothing.416*417* \param ctx pointer to the context structure.418* \param data pointer to the injected data.419* \param len injected data length (in bytes).420*/421void br_md5_update(br_md5_context *ctx, const void *data, size_t len);422423/**424* \brief Compute MD5 output.425*426* The MD5 output for the concatenation of all bytes injected in the427* provided context since the last initialisation or reset call, is428* computed and written in the buffer pointed to by `out`. The context429* itself is not modified, so extra bytes may be injected afterwards430* to continue that computation.431*432* \param ctx pointer to the context structure.433* \param out destination buffer for the hash output.434*/435void br_md5_out(const br_md5_context *ctx, void *out);436437/**438* \brief Save MD5 running state.439*440* The running state for MD5 (output of the last internal block441* processing) is written in the buffer pointed to by `out`. The442* number of bytes injected since the last initialisation or reset443* call is returned. The context is not modified.444*445* \param ctx pointer to the context structure.446* \param out destination buffer for the running state.447* \return the injected total byte length.448*/449uint64_t br_md5_state(const br_md5_context *ctx, void *out);450451/**452* \brief Restore MD5 running state.453*454* The running state for MD5 is set to the provided values.455*456* \param ctx pointer to the context structure.457* \param stb source buffer for the running state.458* \param count the injected total byte length.459*/460void br_md5_set_state(br_md5_context *ctx, const void *stb, uint64_t count);461462/**463* \brief Symbolic identifier for SHA-1.464*/465#define br_sha1_ID 2466467/**468* \brief SHA-1 output size (in bytes).469*/470#define br_sha1_SIZE 20471472/**473* \brief Constant vtable for SHA-1.474*/475extern const br_hash_class br_sha1_vtable;476477/**478* \brief SHA-1 context.479*480* First field is a pointer to the vtable; it is set by the initialisation481* function. Other fields are not supposed to be accessed by user code.482*/483typedef struct {484/**485* \brief Pointer to vtable for this context.486*/487const br_hash_class *vtable;488#ifndef BR_DOXYGEN_IGNORE489unsigned char buf[64];490uint64_t count;491uint32_t val[5];492#endif493} br_sha1_context;494495/**496* \brief SHA-1 context initialisation.497*498* This function initialises or resets a context for a new SHA-1499* computation. It also sets the vtable pointer.500*501* \param ctx pointer to the context structure.502*/503void br_sha1_init(br_sha1_context *ctx);504505/**506* \brief Inject some data bytes in a running SHA-1 computation.507*508* The provided context is updated with some data bytes. If the number509* of bytes (`len`) is zero, then the data pointer (`data`) is ignored510* and may be `NULL`, and this function does nothing.511*512* \param ctx pointer to the context structure.513* \param data pointer to the injected data.514* \param len injected data length (in bytes).515*/516void br_sha1_update(br_sha1_context *ctx, const void *data, size_t len);517518/**519* \brief Compute SHA-1 output.520*521* The SHA-1 output for the concatenation of all bytes injected in the522* provided context since the last initialisation or reset call, is523* computed and written in the buffer pointed to by `out`. The context524* itself is not modified, so extra bytes may be injected afterwards525* to continue that computation.526*527* \param ctx pointer to the context structure.528* \param out destination buffer for the hash output.529*/530void br_sha1_out(const br_sha1_context *ctx, void *out);531532/**533* \brief Save SHA-1 running state.534*535* The running state for SHA-1 (output of the last internal block536* processing) is written in the buffer pointed to by `out`. The537* number of bytes injected since the last initialisation or reset538* call is returned. The context is not modified.539*540* \param ctx pointer to the context structure.541* \param out destination buffer for the running state.542* \return the injected total byte length.543*/544uint64_t br_sha1_state(const br_sha1_context *ctx, void *out);545546/**547* \brief Restore SHA-1 running state.548*549* The running state for SHA-1 is set to the provided values.550*551* \param ctx pointer to the context structure.552* \param stb source buffer for the running state.553* \param count the injected total byte length.554*/555void br_sha1_set_state(br_sha1_context *ctx, const void *stb, uint64_t count);556557/**558* \brief Symbolic identifier for SHA-224.559*/560#define br_sha224_ID 3561562/**563* \brief SHA-224 output size (in bytes).564*/565#define br_sha224_SIZE 28566567/**568* \brief Constant vtable for SHA-224.569*/570extern const br_hash_class br_sha224_vtable;571572/**573* \brief SHA-224 context.574*575* First field is a pointer to the vtable; it is set by the initialisation576* function. Other fields are not supposed to be accessed by user code.577*/578typedef struct {579/**580* \brief Pointer to vtable for this context.581*/582const br_hash_class *vtable;583#ifndef BR_DOXYGEN_IGNORE584unsigned char buf[64];585uint64_t count;586uint32_t val[8];587#endif588} br_sha224_context;589590/**591* \brief SHA-224 context initialisation.592*593* This function initialises or resets a context for a new SHA-224594* computation. It also sets the vtable pointer.595*596* \param ctx pointer to the context structure.597*/598void br_sha224_init(br_sha224_context *ctx);599600/**601* \brief Inject some data bytes in a running SHA-224 computation.602*603* The provided context is updated with some data bytes. If the number604* of bytes (`len`) is zero, then the data pointer (`data`) is ignored605* and may be `NULL`, and this function does nothing.606*607* \param ctx pointer to the context structure.608* \param data pointer to the injected data.609* \param len injected data length (in bytes).610*/611void br_sha224_update(br_sha224_context *ctx, const void *data, size_t len);612613/**614* \brief Compute SHA-224 output.615*616* The SHA-224 output for the concatenation of all bytes injected in the617* provided context since the last initialisation or reset call, is618* computed and written in the buffer pointed to by `out`. The context619* itself is not modified, so extra bytes may be injected afterwards620* to continue that computation.621*622* \param ctx pointer to the context structure.623* \param out destination buffer for the hash output.624*/625void br_sha224_out(const br_sha224_context *ctx, void *out);626627/**628* \brief Save SHA-224 running state.629*630* The running state for SHA-224 (output of the last internal block631* processing) is written in the buffer pointed to by `out`. The632* number of bytes injected since the last initialisation or reset633* call is returned. The context is not modified.634*635* \param ctx pointer to the context structure.636* \param out destination buffer for the running state.637* \return the injected total byte length.638*/639uint64_t br_sha224_state(const br_sha224_context *ctx, void *out);640641/**642* \brief Restore SHA-224 running state.643*644* The running state for SHA-224 is set to the provided values.645*646* \param ctx pointer to the context structure.647* \param stb source buffer for the running state.648* \param count the injected total byte length.649*/650void br_sha224_set_state(br_sha224_context *ctx,651const void *stb, uint64_t count);652653/**654* \brief Symbolic identifier for SHA-256.655*/656#define br_sha256_ID 4657658/**659* \brief SHA-256 output size (in bytes).660*/661#define br_sha256_SIZE 32662663/**664* \brief Constant vtable for SHA-256.665*/666extern const br_hash_class br_sha256_vtable;667668#ifdef BR_DOXYGEN_IGNORE669/**670* \brief SHA-256 context.671*672* First field is a pointer to the vtable; it is set by the initialisation673* function. Other fields are not supposed to be accessed by user code.674*/675typedef struct {676/**677* \brief Pointer to vtable for this context.678*/679const br_hash_class *vtable;680} br_sha256_context;681#else682typedef br_sha224_context br_sha256_context;683#endif684685/**686* \brief SHA-256 context initialisation.687*688* This function initialises or resets a context for a new SHA-256689* computation. It also sets the vtable pointer.690*691* \param ctx pointer to the context structure.692*/693void br_sha256_init(br_sha256_context *ctx);694695#ifdef BR_DOXYGEN_IGNORE696/**697* \brief Inject some data bytes in a running SHA-256 computation.698*699* The provided context is updated with some data bytes. If the number700* of bytes (`len`) is zero, then the data pointer (`data`) is ignored701* and may be `NULL`, and this function does nothing.702*703* \param ctx pointer to the context structure.704* \param data pointer to the injected data.705* \param len injected data length (in bytes).706*/707void br_sha256_update(br_sha256_context *ctx, const void *data, size_t len);708#else709#define br_sha256_update br_sha224_update710#endif711712/**713* \brief Compute SHA-256 output.714*715* The SHA-256 output for the concatenation of all bytes injected in the716* provided context since the last initialisation or reset call, is717* computed and written in the buffer pointed to by `out`. The context718* itself is not modified, so extra bytes may be injected afterwards719* to continue that computation.720*721* \param ctx pointer to the context structure.722* \param out destination buffer for the hash output.723*/724void br_sha256_out(const br_sha256_context *ctx, void *out);725726#ifdef BR_DOXYGEN_IGNORE727/**728* \brief Save SHA-256 running state.729*730* The running state for SHA-256 (output of the last internal block731* processing) is written in the buffer pointed to by `out`. The732* number of bytes injected since the last initialisation or reset733* call is returned. The context is not modified.734*735* \param ctx pointer to the context structure.736* \param out destination buffer for the running state.737* \return the injected total byte length.738*/739uint64_t br_sha256_state(const br_sha256_context *ctx, void *out);740#else741#define br_sha256_state br_sha224_state742#endif743744#ifdef BR_DOXYGEN_IGNORE745/**746* \brief Restore SHA-256 running state.747*748* The running state for SHA-256 is set to the provided values.749*750* \param ctx pointer to the context structure.751* \param stb source buffer for the running state.752* \param count the injected total byte length.753*/754void br_sha256_set_state(br_sha256_context *ctx,755const void *stb, uint64_t count);756#else757#define br_sha256_set_state br_sha224_set_state758#endif759760/**761* \brief Symbolic identifier for SHA-384.762*/763#define br_sha384_ID 5764765/**766* \brief SHA-384 output size (in bytes).767*/768#define br_sha384_SIZE 48769770/**771* \brief Constant vtable for SHA-384.772*/773extern const br_hash_class br_sha384_vtable;774775/**776* \brief SHA-384 context.777*778* First field is a pointer to the vtable; it is set by the initialisation779* function. Other fields are not supposed to be accessed by user code.780*/781typedef struct {782/**783* \brief Pointer to vtable for this context.784*/785const br_hash_class *vtable;786#ifndef BR_DOXYGEN_IGNORE787unsigned char buf[128];788uint64_t count;789uint64_t val[8];790#endif791} br_sha384_context;792793/**794* \brief SHA-384 context initialisation.795*796* This function initialises or resets a context for a new SHA-384797* computation. It also sets the vtable pointer.798*799* \param ctx pointer to the context structure.800*/801void br_sha384_init(br_sha384_context *ctx);802803/**804* \brief Inject some data bytes in a running SHA-384 computation.805*806* The provided context is updated with some data bytes. If the number807* of bytes (`len`) is zero, then the data pointer (`data`) is ignored808* and may be `NULL`, and this function does nothing.809*810* \param ctx pointer to the context structure.811* \param data pointer to the injected data.812* \param len injected data length (in bytes).813*/814void br_sha384_update(br_sha384_context *ctx, const void *data, size_t len);815816/**817* \brief Compute SHA-384 output.818*819* The SHA-384 output for the concatenation of all bytes injected in the820* provided context since the last initialisation or reset call, is821* computed and written in the buffer pointed to by `out`. The context822* itself is not modified, so extra bytes may be injected afterwards823* to continue that computation.824*825* \param ctx pointer to the context structure.826* \param out destination buffer for the hash output.827*/828void br_sha384_out(const br_sha384_context *ctx, void *out);829830/**831* \brief Save SHA-384 running state.832*833* The running state for SHA-384 (output of the last internal block834* processing) is written in the buffer pointed to by `out`. The835* number of bytes injected since the last initialisation or reset836* call is returned. The context is not modified.837*838* \param ctx pointer to the context structure.839* \param out destination buffer for the running state.840* \return the injected total byte length.841*/842uint64_t br_sha384_state(const br_sha384_context *ctx, void *out);843844/**845* \brief Restore SHA-384 running state.846*847* The running state for SHA-384 is set to the provided values.848*849* \param ctx pointer to the context structure.850* \param stb source buffer for the running state.851* \param count the injected total byte length.852*/853void br_sha384_set_state(br_sha384_context *ctx,854const void *stb, uint64_t count);855856/**857* \brief Symbolic identifier for SHA-512.858*/859#define br_sha512_ID 6860861/**862* \brief SHA-512 output size (in bytes).863*/864#define br_sha512_SIZE 64865866/**867* \brief Constant vtable for SHA-512.868*/869extern const br_hash_class br_sha512_vtable;870871#ifdef BR_DOXYGEN_IGNORE872/**873* \brief SHA-512 context.874*875* First field is a pointer to the vtable; it is set by the initialisation876* function. Other fields are not supposed to be accessed by user code.877*/878typedef struct {879/**880* \brief Pointer to vtable for this context.881*/882const br_hash_class *vtable;883} br_sha512_context;884#else885typedef br_sha384_context br_sha512_context;886#endif887888/**889* \brief SHA-512 context initialisation.890*891* This function initialises or resets a context for a new SHA-512892* computation. It also sets the vtable pointer.893*894* \param ctx pointer to the context structure.895*/896void br_sha512_init(br_sha512_context *ctx);897898#ifdef BR_DOXYGEN_IGNORE899/**900* \brief Inject some data bytes in a running SHA-512 computation.901*902* The provided context is updated with some data bytes. If the number903* of bytes (`len`) is zero, then the data pointer (`data`) is ignored904* and may be `NULL`, and this function does nothing.905*906* \param ctx pointer to the context structure.907* \param data pointer to the injected data.908* \param len injected data length (in bytes).909*/910void br_sha512_update(br_sha512_context *ctx, const void *data, size_t len);911#else912#define br_sha512_update br_sha384_update913#endif914915/**916* \brief Compute SHA-512 output.917*918* The SHA-512 output for the concatenation of all bytes injected in the919* provided context since the last initialisation or reset call, is920* computed and written in the buffer pointed to by `out`. The context921* itself is not modified, so extra bytes may be injected afterwards922* to continue that computation.923*924* \param ctx pointer to the context structure.925* \param out destination buffer for the hash output.926*/927void br_sha512_out(const br_sha512_context *ctx, void *out);928929#ifdef BR_DOXYGEN_IGNORE930/**931* \brief Save SHA-512 running state.932*933* The running state for SHA-512 (output of the last internal block934* processing) is written in the buffer pointed to by `out`. The935* number of bytes injected since the last initialisation or reset936* call is returned. The context is not modified.937*938* \param ctx pointer to the context structure.939* \param out destination buffer for the running state.940* \return the injected total byte length.941*/942uint64_t br_sha512_state(const br_sha512_context *ctx, void *out);943#else944#define br_sha512_state br_sha384_state945#endif946947#ifdef BR_DOXYGEN_IGNORE948/**949* \brief Restore SHA-512 running state.950*951* The running state for SHA-512 is set to the provided values.952*953* \param ctx pointer to the context structure.954* \param stb source buffer for the running state.955* \param count the injected total byte length.956*/957void br_sha512_set_state(br_sha512_context *ctx,958const void *stb, uint64_t count);959#else960#define br_sha512_set_state br_sha384_set_state961#endif962963/*964* "md5sha1" is a special hash function that computes both MD5 and SHA-1965* on the same input, and produces a 36-byte output (MD5 and SHA-1966* concatenation, in that order). State size is also 36 bytes.967*/968969/**970* \brief Symbolic identifier for MD5+SHA-1.971*972* MD5+SHA-1 is the concatenation of MD5 and SHA-1, computed over the973* same input. It is not one of the functions identified in TLS, so974* we give it a symbolic identifier of value 0.975*/976#define br_md5sha1_ID 0977978/**979* \brief MD5+SHA-1 output size (in bytes).980*/981#define br_md5sha1_SIZE 36982983/**984* \brief Constant vtable for MD5+SHA-1.985*/986extern const br_hash_class br_md5sha1_vtable;987988/**989* \brief MD5+SHA-1 context.990*991* First field is a pointer to the vtable; it is set by the initialisation992* function. Other fields are not supposed to be accessed by user code.993*/994typedef struct {995/**996* \brief Pointer to vtable for this context.997*/998const br_hash_class *vtable;999#ifndef BR_DOXYGEN_IGNORE1000unsigned char buf[64];1001uint64_t count;1002uint32_t val_md5[4];1003uint32_t val_sha1[5];1004#endif1005} br_md5sha1_context;10061007/**1008* \brief MD5+SHA-1 context initialisation.1009*1010* This function initialises or resets a context for a new SHA-5121011* computation. It also sets the vtable pointer.1012*1013* \param ctx pointer to the context structure.1014*/1015void br_md5sha1_init(br_md5sha1_context *ctx);10161017/**1018* \brief Inject some data bytes in a running MD5+SHA-1 computation.1019*1020* The provided context is updated with some data bytes. If the number1021* of bytes (`len`) is zero, then the data pointer (`data`) is ignored1022* and may be `NULL`, and this function does nothing.1023*1024* \param ctx pointer to the context structure.1025* \param data pointer to the injected data.1026* \param len injected data length (in bytes).1027*/1028void br_md5sha1_update(br_md5sha1_context *ctx, const void *data, size_t len);10291030/**1031* \brief Compute MD5+SHA-1 output.1032*1033* The MD5+SHA-1 output for the concatenation of all bytes injected in the1034* provided context since the last initialisation or reset call, is1035* computed and written in the buffer pointed to by `out`. The context1036* itself is not modified, so extra bytes may be injected afterwards1037* to continue that computation.1038*1039* \param ctx pointer to the context structure.1040* \param out destination buffer for the hash output.1041*/1042void br_md5sha1_out(const br_md5sha1_context *ctx, void *out);10431044/**1045* \brief Save MD5+SHA-1 running state.1046*1047* The running state for MD5+SHA-1 (output of the last internal block1048* processing) is written in the buffer pointed to by `out`. The1049* number of bytes injected since the last initialisation or reset1050* call is returned. The context is not modified.1051*1052* \param ctx pointer to the context structure.1053* \param out destination buffer for the running state.1054* \return the injected total byte length.1055*/1056uint64_t br_md5sha1_state(const br_md5sha1_context *ctx, void *out);10571058/**1059* \brief Restore MD5+SHA-1 running state.1060*1061* The running state for MD5+SHA-1 is set to the provided values.1062*1063* \param ctx pointer to the context structure.1064* \param stb source buffer for the running state.1065* \param count the injected total byte length.1066*/1067void br_md5sha1_set_state(br_md5sha1_context *ctx,1068const void *stb, uint64_t count);10691070/**1071* \brief Aggregate context for configurable hash function support.1072*1073* The `br_hash_compat_context` type is a type which is large enough to1074* serve as context for all standard hash functions defined above.1075*/1076typedef union {1077const br_hash_class *vtable;1078br_md5_context md5;1079br_sha1_context sha1;1080br_sha224_context sha224;1081br_sha256_context sha256;1082br_sha384_context sha384;1083br_sha512_context sha512;1084br_md5sha1_context md5sha1;1085} br_hash_compat_context;10861087/*1088* The multi-hasher is a construct that handles hashing of the same input1089* data with several hash functions, with a single shared input buffer.1090* It can handle MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-5121091* simultaneously, though which functions are activated depends on1092* the set implementation pointers.1093*/10941095/**1096* \brief Multi-hasher context structure.1097*1098* The multi-hasher runs up to six hash functions in the standard TLS list1099* (MD5, SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512) in parallel, over1100* the same input.1101*1102* The multi-hasher does _not_ follow the OOP structure with a vtable.1103* Instead, it is configured with the vtables of the hash functions it1104* should run. Structure fields are not supposed to be accessed directly.1105*/1106typedef struct {1107#ifndef BR_DOXYGEN_IGNORE1108unsigned char buf[128];1109uint64_t count;1110uint32_t val_32[25];1111uint64_t val_64[16];1112const br_hash_class *impl[6];1113#endif1114} br_multihash_context;11151116/**1117* \brief Clear a multi-hasher context.1118*1119* This should always be called once on a given context, _before_ setting1120* the implementation pointers.1121*1122* \param ctx the multi-hasher context.1123*/1124void br_multihash_zero(br_multihash_context *ctx);11251126/**1127* \brief Set a hash function implementation.1128*1129* Implementations shall be set _after_ clearing the context (with1130* `br_multihash_zero()`) but _before_ initialising the computation1131* (with `br_multihash_init()`). The hash function implementation1132* MUST be one of the standard hash functions (MD5, SHA-1, SHA-224,1133* SHA-256, SHA-384 or SHA-512); it may also be `NULL` to remove1134* an implementation from the multi-hasher.1135*1136* \param ctx the multi-hasher context.1137* \param id the hash function symbolic identifier.1138* \param impl the hash function vtable, or `NULL`.1139*/1140static inline void1141br_multihash_setimpl(br_multihash_context *ctx,1142int id, const br_hash_class *impl)1143{1144/*1145* This code relies on hash functions ID being values 1 to 6,1146* in the MD5 to SHA-512 order.1147*/1148ctx->impl[id - 1] = impl;1149}11501151/**1152* \brief Get a hash function implementation.1153*1154* This function returns the currently configured vtable for a given1155* hash function (by symbolic ID). If no such function was configured in1156* the provided multi-hasher context, then this function returns `NULL`.1157*1158* \param ctx the multi-hasher context.1159* \param id the hash function symbolic identifier.1160* \return the hash function vtable, or `NULL`.1161*/1162static inline const br_hash_class *1163br_multihash_getimpl(const br_multihash_context *ctx, int id)1164{1165return ctx->impl[id - 1];1166}11671168/**1169* \brief Reset a multi-hasher context.1170*1171* This function prepares the context for a new hashing computation,1172* for all implementations configured at that point.1173*1174* \param ctx the multi-hasher context.1175*/1176void br_multihash_init(br_multihash_context *ctx);11771178/**1179* \brief Inject some data bytes in a running multi-hashing computation.1180*1181* The provided context is updated with some data bytes. If the number1182* of bytes (`len`) is zero, then the data pointer (`data`) is ignored1183* and may be `NULL`, and this function does nothing.1184*1185* \param ctx pointer to the context structure.1186* \param data pointer to the injected data.1187* \param len injected data length (in bytes).1188*/1189void br_multihash_update(br_multihash_context *ctx,1190const void *data, size_t len);11911192/**1193* \brief Compute a hash output from a multi-hasher.1194*1195* The hash output for the concatenation of all bytes injected in the1196* provided context since the last initialisation or reset call, is1197* computed and written in the buffer pointed to by `dst`. The hash1198* function to use is identified by `id` and must be one of the standard1199* hash functions. If that hash function was indeed configured in the1200* multi-hasher context, the corresponding hash value is written in1201* `dst` and its length (in bytes) is returned. If the hash function1202* was _not_ configured, then nothing is written in `dst` and 0 is1203* returned.1204*1205* The context itself is not modified, so extra bytes may be injected1206* afterwards to continue the hash computations.1207*1208* \param ctx pointer to the context structure.1209* \param id the hash function symbolic identifier.1210* \param dst destination buffer for the hash output.1211* \return the hash output length (in bytes), or 0.1212*/1213size_t br_multihash_out(const br_multihash_context *ctx, int id, void *dst);12141215/**1216* \brief Type for a GHASH implementation.1217*1218* GHASH is a sort of keyed hash meant to be used to implement GCM in1219* combination with a block cipher (with 16-byte blocks).1220*1221* The `y` array has length 16 bytes and is used for input and output; in1222* a complete GHASH run, it starts with an all-zero value. `h` is a 16-byte1223* value that serves as key (it is derived from the encryption key in GCM,1224* using the block cipher). The data length (`len`) is expressed in bytes.1225* The `y` array is updated.1226*1227* If the data length is not a multiple of 16, then the data is implicitly1228* padded with zeros up to the next multiple of 16. Thus, when using GHASH1229* in GCM, this method may be called twice, for the associated data and1230* for the ciphertext, respectively; the zero-padding implements exactly1231* the GCM rules.1232*1233* \param y the array to update.1234* \param h the GHASH key.1235* \param data the input data (may be `NULL` if `len` is zero).1236* \param len the input data length (in bytes).1237*/1238typedef void (*br_ghash)(void *y, const void *h, const void *data, size_t len);12391240/**1241* \brief GHASH implementation using multiplications (mixed 32-bit).1242*1243* This implementation uses multiplications of 32-bit values, with a1244* 64-bit result. It is constant-time (if multiplications are1245* constant-time).1246*1247* \param y the array to update.1248* \param h the GHASH key.1249* \param data the input data (may be `NULL` if `len` is zero).1250* \param len the input data length (in bytes).1251*/1252void br_ghash_ctmul(void *y, const void *h, const void *data, size_t len);12531254/**1255* \brief GHASH implementation using multiplications (strict 32-bit).1256*1257* This implementation uses multiplications of 32-bit values, with a1258* 32-bit result. It is usually somewhat slower than `br_ghash_ctmul()`,1259* but it is expected to be faster on architectures for which the1260* 32-bit multiplication opcode does not yield the upper 32 bits of the1261* product. It is constant-time (if multiplications are constant-time).1262*1263* \param y the array to update.1264* \param h the GHASH key.1265* \param data the input data (may be `NULL` if `len` is zero).1266* \param len the input data length (in bytes).1267*/1268void br_ghash_ctmul32(void *y, const void *h, const void *data, size_t len);12691270/**1271* \brief GHASH implementation using multiplications (64-bit).1272*1273* This implementation uses multiplications of 64-bit values, with a1274* 64-bit result. It is constant-time (if multiplications are1275* constant-time). It is substantially faster than `br_ghash_ctmul()`1276* and `br_ghash_ctmul32()` on most 64-bit architectures.1277*1278* \param y the array to update.1279* \param h the GHASH key.1280* \param data the input data (may be `NULL` if `len` is zero).1281* \param len the input data length (in bytes).1282*/1283void br_ghash_ctmul64(void *y, const void *h, const void *data, size_t len);12841285/**1286* \brief GHASH implementation using the `pclmulqdq` opcode (part of the1287* AES-NI instructions).1288*1289* This implementation is available only on x86 platforms where the1290* compiler supports the relevant intrinsic functions. Even if the1291* compiler supports these functions, the local CPU might not support1292* the `pclmulqdq` opcode, meaning that a call will fail with an1293* illegal instruction exception. To safely obtain a pointer to this1294* function when supported (or 0 otherwise), use `br_ghash_pclmul_get()`.1295*1296* \param y the array to update.1297* \param h the GHASH key.1298* \param data the input data (may be `NULL` if `len` is zero).1299* \param len the input data length (in bytes).1300*/1301void br_ghash_pclmul(void *y, const void *h, const void *data, size_t len);13021303/**1304* \brief Obtain the `pclmul` GHASH implementation, if available.1305*1306* If the `pclmul` implementation was compiled in the library (depending1307* on the compiler abilities) _and_ the local CPU appears to support the1308* opcode, then this function will return a pointer to the1309* `br_ghash_pclmul()` function. Otherwise, it will return `0`.1310*1311* \return the `pclmul` GHASH implementation, or `0`.1312*/1313br_ghash br_ghash_pclmul_get(void);13141315/**1316* \brief GHASH implementation using the POWER8 opcodes.1317*1318* This implementation is available only on POWER8 platforms (and later).1319* To safely obtain a pointer to this function when supported (or 01320* otherwise), use `br_ghash_pwr8_get()`.1321*1322* \param y the array to update.1323* \param h the GHASH key.1324* \param data the input data (may be `NULL` if `len` is zero).1325* \param len the input data length (in bytes).1326*/1327void br_ghash_pwr8(void *y, const void *h, const void *data, size_t len);13281329/**1330* \brief Obtain the `pwr8` GHASH implementation, if available.1331*1332* If the `pwr8` implementation was compiled in the library (depending1333* on the compiler abilities) _and_ the local CPU appears to support the1334* opcode, then this function will return a pointer to the1335* `br_ghash_pwr8()` function. Otherwise, it will return `0`.1336*1337* \return the `pwr8` GHASH implementation, or `0`.1338*/1339br_ghash br_ghash_pwr8_get(void);13401341#ifdef __cplusplus1342}1343#endif13441345#endif134613471348