Path: blob/main/contrib/bearssl/inc/bearssl_hmac.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_HMAC_H__25#define BR_BEARSSL_HMAC_H__2627#include <stddef.h>28#include <stdint.h>2930#include "bearssl_hash.h"3132#ifdef __cplusplus33extern "C" {34#endif3536/** \file bearssl_hmac.h37*38* # HMAC39*40* HMAC is initialized with a key and an underlying hash function; it41* then fills a "key context". That context contains the processed42* key.43*44* With the key context, a HMAC context can be initialized to process45* the input bytes and obtain the MAC output. The key context is not46* modified during that process, and can be reused.47*48* IMPORTANT: HMAC shall be used only with functions that have the49* following properties:50*51* - hash output size does not exceed 64 bytes;52* - hash internal state size does not exceed 64 bytes;53* - internal block length is a power of 2 between 16 and 256 bytes.54*/5556/**57* \brief HMAC key context.58*59* The HMAC key context is initialised with a hash function implementation60* and a secret key. Contents are opaque (callers should not access them61* directly). The caller is responsible for allocating the context where62* appropriate. Context initialisation and usage incurs no dynamic63* allocation, so there is no release function.64*/65typedef struct {66#ifndef BR_DOXYGEN_IGNORE67const br_hash_class *dig_vtable;68unsigned char ksi[64], kso[64];69#endif70} br_hmac_key_context;7172/**73* \brief HMAC key context initialisation.74*75* Initialise the key context with the provided key, using the hash function76* identified by `digest_vtable`. This supports arbitrary key lengths.77*78* \param kc HMAC key context to initialise.79* \param digest_vtable pointer to the hash function implementation vtable.80* \param key pointer to the HMAC secret key.81* \param key_len HMAC secret key length (in bytes).82*/83void br_hmac_key_init(br_hmac_key_context *kc,84const br_hash_class *digest_vtable, const void *key, size_t key_len);8586/*87* \brief Get the underlying hash function.88*89* This function returns a pointer to the implementation vtable of the90* hash function used for this HMAC key context.91*92* \param kc HMAC key context.93* \return the hash function implementation.94*/95static inline const br_hash_class *br_hmac_key_get_digest(96const br_hmac_key_context *kc)97{98return kc->dig_vtable;99}100101/**102* \brief HMAC computation context.103*104* The HMAC computation context maintains the state for a single HMAC105* computation. It is modified as input bytes are injected. The context106* is caller-allocated and has no release function since it does not107* dynamically allocate external resources. Its contents are opaque.108*/109typedef struct {110#ifndef BR_DOXYGEN_IGNORE111br_hash_compat_context dig;112unsigned char kso[64];113size_t out_len;114#endif115} br_hmac_context;116117/**118* \brief HMAC computation initialisation.119*120* Initialise a HMAC context with a key context. The key context is121* unmodified. Relevant data from the key context is immediately copied;122* the key context can thus be independently reused, modified or released123* without impacting this HMAC computation.124*125* An explicit output length can be specified; the actual output length126* will be the minimum of that value and the natural HMAC output length.127* If `out_len` is 0, then the natural HMAC output length is selected. The128* "natural output length" is the output length of the underlying hash129* function.130*131* \param ctx HMAC context to initialise.132* \param kc HMAC key context (already initialised with the key).133* \param out_len HMAC output length (0 to select "natural length").134*/135void br_hmac_init(br_hmac_context *ctx,136const br_hmac_key_context *kc, size_t out_len);137138/**139* \brief Get the HMAC output size.140*141* The HMAC output size is the number of bytes that will actually be142* produced with `br_hmac_out()` with the provided context. This function143* MUST NOT be called on a non-initialised HMAC computation context.144* The returned value is the minimum of the HMAC natural length (output145* size of the underlying hash function) and the `out_len` parameter which146* was used with the last `br_hmac_init()` call on that context (if the147* initialisation `out_len` parameter was 0, then this function will148* return the HMAC natural length).149*150* \param ctx the (already initialised) HMAC computation context.151* \return the HMAC actual output size.152*/153static inline size_t154br_hmac_size(br_hmac_context *ctx)155{156return ctx->out_len;157}158159/*160* \brief Get the underlying hash function.161*162* This function returns a pointer to the implementation vtable of the163* hash function used for this HMAC context.164*165* \param hc HMAC context.166* \return the hash function implementation.167*/168static inline const br_hash_class *br_hmac_get_digest(169const br_hmac_context *hc)170{171return hc->dig.vtable;172}173174/**175* \brief Inject some bytes in HMAC.176*177* The provided `len` bytes are injected as extra input in the HMAC178* computation incarnated by the `ctx` HMAC context. It is acceptable179* that `len` is zero, in which case `data` is ignored (and may be180* `NULL`) and this function does nothing.181*/182void br_hmac_update(br_hmac_context *ctx, const void *data, size_t len);183184/**185* \brief Compute the HMAC output.186*187* The destination buffer MUST be large enough to accommodate the result;188* its length is at most the "natural length" of HMAC (i.e. the output189* length of the underlying hash function). The context is NOT modified;190* further bytes may be processed. Thus, "partial HMAC" values can be191* efficiently obtained.192*193* Returned value is the output length (in bytes).194*195* \param ctx HMAC computation context.196* \param out destination buffer for the HMAC output.197* \return the produced value length (in bytes).198*/199size_t br_hmac_out(const br_hmac_context *ctx, void *out);200201/**202* \brief Constant-time HMAC computation.203*204* This function compute the HMAC output in constant time. Some extra205* input bytes are processed, then the output is computed. The extra206* input consists in the `len` bytes pointed to by `data`. The `len`207* parameter must lie between `min_len` and `max_len` (inclusive);208* `max_len` bytes are actually read from `data`. Computing time (and209* memory access pattern) will not depend upon the data byte contents or210* the value of `len`.211*212* The output is written in the `out` buffer, that MUST be large enough213* to receive it.214*215* The difference `max_len - min_len` MUST be less than 2<sup>30</sup>216* (i.e. about one gigabyte).217*218* This function computes the output properly only if the underlying219* hash function uses MD padding (i.e. MD5, SHA-1, SHA-224, SHA-256,220* SHA-384 or SHA-512).221*222* The provided context is NOT modified.223*224* \param ctx the (already initialised) HMAC computation context.225* \param data the extra input bytes.226* \param len the extra input length (in bytes).227* \param min_len minimum extra input length (in bytes).228* \param max_len maximum extra input length (in bytes).229* \param out destination buffer for the HMAC output.230* \return the produced value length (in bytes).231*/232size_t br_hmac_outCT(const br_hmac_context *ctx,233const void *data, size_t len, size_t min_len, size_t max_len,234void *out);235236#ifdef __cplusplus237}238#endif239240#endif241242243