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