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))
160#define LC_HMAC_CTX_ON_STACK(name, hashname) \
161 _Pragma("GCC diagnostic push") \
162 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
163 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
164 LC_ALIGNED_BUFFER(name##_ctx_buf, \
165 LC_HMAC_CTX_SIZE(hashname), \
166 LC_HASH_COMMON_ALIGNMENT); \
167 struct lc_hmac_ctx *name = (struct lc_hmac_ctx *)name##_ctx_buf; \
168 LC_HMAC_SET_CTX(name, hashname); \
169 lc_hmac_zero(name); \
170 _Pragma("GCC diagnostic pop")
199 const uint8_t *in,
size_t inlen, uint8_t *mac);
void lc_hmac_reinit(struct lc_hmac_ctx *hmac_ctx)
Re-initialize HMAC context after a hmac_final operation.
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_init(struct lc_hmac_ctx *hmac_ctx, const uint8_t *key, size_t keylen)
Initialize HMAC context.
int lc_hmac_alloc(const struct lc_hash *hash, struct lc_hmac_ctx **hmac_ctx)
Allocate HMAC context on heap.
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_zero_free(struct lc_hmac_ctx *hmac_ctx)
Zeroize and free HMAC context.
size_t lc_hmac_macsize(struct lc_hmac_ctx *hmac_ctx)
Return the MAC size.
int 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.
int lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.