32#if LC_SHA3_MAX_SIZE_BLOCK
33#define LC_SHA_MAX_SIZE_BLOCK LC_SHA3_MAX_SIZE_BLOCK
34#elif LC_SHA512_SIZE_BLOCK
35#define LC_SHA_MAX_SIZE_BLOCK LC_SHA512_SIZE_BLOCK
36#elif LC_SHA256_SIZE_BLOCK
37#define LC_SHA_MAX_SIZE_BLOCK LC_SHA256_SIZE_BLOCK
39#error "No known maximum block size defined - include sha3.h, sha512.h or sha256.h before hmac.h"
45 struct lc_hash_ctx hash_ctx;
48#define LC_HMAC_STATE_SIZE(x) \
49 (LC_HASH_STATE_SIZE(x) + 2 * LC_SHA_MAX_SIZE_BLOCK)
50#define LC_HMAC_CTX_SIZE(x) (LC_HMAC_STATE_SIZE(x) + sizeof(struct lc_hmac_ctx))
52#define _LC_HMAC_SET_CTX(name, hashname, ctx, offset) \
53 _LC_HASH_SET_CTX((&name->hash_ctx), hashname, ctx, offset); \
54 name->k_opad = (uint8_t *)((uint8_t *)ctx + offset + \
55 LC_HASH_STATE_SIZE(hashname)); \
56 name->k_ipad = (uint8_t *)((uint8_t *)ctx + offset + \
57 LC_HASH_STATE_SIZE(hashname) + \
58 LC_SHA_MAX_SIZE_BLOCK)
60#define LC_HMAC_SET_CTX(name, hashname) \
61 _LC_HMAC_SET_CTX(name, hashname, name, sizeof(struct lc_hmac_ctx))
150 struct lc_hash_ctx *hash_ctx = &hmac_ctx->hash_ctx;
151 const struct lc_hash *hash = hash_ctx->hash;
154 LC_HMAC_STATE_SIZE(hash));
165#define LC_HMAC_CTX_ON_STACK(name, hashname) \
166 _Pragma("GCC diagnostic push") \
167 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
168 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
169 LC_ALIGNED_BUFFER(name##_ctx_buf, \
170 LC_HMAC_CTX_SIZE(hashname), \
171 LC_HASH_COMMON_ALIGNMENT); \
172 struct lc_hmac_ctx *name = (struct lc_hmac_ctx *)name##_ctx_buf; \
173 LC_HMAC_SET_CTX(name, hashname); \
174 lc_hmac_zero(name); \
175 _Pragma("GCC diagnostic pop")
187 struct lc_hash_ctx *hash_ctx = &hmac_ctx->hash_ctx;
207 size_t keylen,
const uint8_t *in,
size_t inlen,
void lc_hmac_init(struct lc_hmac_ctx *hmac_ctx, const uint8_t *key, size_t keylen)
Initialize HMAC context.
void lc_hmac_reinit(struct lc_hmac_ctx *hmac_ctx)
Re-initialize HMAC context after a hmac_final operation.
#define LC_HMAC_CTX_ON_STACK(name, hashname)
Allocate stack memory for the HMAC context.
static size_t lc_hmac_macsize(struct lc_hmac_ctx *hmac_ctx)
Return the MAC size.
static void lc_hmac(const struct lc_hash *hash, const uint8_t *key, size_t keylen, const uint8_t *in, size_t inlen, uint8_t *mac)
Calculate HMAC - one-shot.
static void lc_hmac_zero(struct lc_hmac_ctx *hmac_ctx)
Zeroize HMAC context allocated with either HMAC_CTX_ON_STACK or hmac_alloc.
void lc_hmac_final(struct lc_hmac_ctx *hmac_ctx, uint8_t *mac)
Calculate HMAC mac.
void lc_hmac_update(struct lc_hmac_ctx *hmac_ctx, const uint8_t *in, size_t inlen)
Update HMAC.
int lc_hmac_alloc(const struct lc_hash *hash, struct lc_hmac_ctx **hmac_ctx)
Allocate HMAC context on heap.
void lc_hmac_zero_free(struct lc_hmac_ctx *hmac_ctx)
Zeroize and free HMAC context.
static size_t lc_hash_digestsize(struct lc_hash_ctx *hash_ctx)
Get the size of the message digest.
void lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
static void lc_memset_secure(void *s, int c, size_t n)