34 uint8_t final_called : 1;
35 uint8_t rng_initialized : 1;
37 struct lc_hash_ctx hash_ctx;
40#define LC_KMAC_STATE_SIZE(x) (LC_HASH_STATE_SIZE(x))
41#define LC_KMAC_STATE_SIZE_REINIT(x) (2 * LC_HASH_STATE_SIZE(x))
42#define LC_KMAC_CTX_SIZE(x) (LC_KMAC_STATE_SIZE(x) + sizeof(struct lc_kmac_ctx))
43#define LC_KMAC_CTX_SIZE_REINIT(x) \
44 (LC_KMAC_STATE_SIZE_REINIT(x) + sizeof(struct lc_kmac_ctx))
46#define _LC_KMAC_SET_CTX(name, hashname, ctx, offset) \
47 _LC_HASH_SET_CTX((&name->hash_ctx), hashname, ctx, offset); \
48 name->shadow_ctx = NULL
50#define LC_KMAC_SET_CTX(name, hashname) \
51 _LC_KMAC_SET_CTX(name, hashname, name, sizeof(struct lc_kmac_ctx))
53#define _LC_KMAC_SET_CTX_REINIT(name, hashname, ctx, offset) \
54 _LC_HASH_SET_CTX((&name->hash_ctx), hashname, ctx, offset); \
55 name->shadow_ctx = (uint8_t *)((uint8_t *)ctx + offset + \
56 LC_HASH_STATE_SIZE(hashname))
58#define LC_KMAC_SET_CTX_REINIT(name, hashname) \
59 _LC_KMAC_SET_CTX_REINIT(name, hashname, name, \
60 sizeof(struct lc_kmac_ctx))
76void lc_kmac_init(
struct lc_kmac_ctx *kmac_ctx,
const uint8_t *key,
size_t klen,
77 const uint8_t *s,
size_t slen);
113void lc_kmac_final(
struct lc_kmac_ctx *kmac_ctx, uint8_t *mac,
size_t maclen);
170#define LC_KMAC_FLAGS_SUPPORT_REINIT (1 << 0)
187 struct lc_hash_ctx *hash_ctx;
192 hash_ctx = &kmac_ctx->hash_ctx;
193 hash = hash_ctx->hash;
195 kmac_ctx->final_called = 0;
196 kmac_ctx->rng_initialized = 0;
199 kmac_ctx->shadow_ctx ?
200 LC_KMAC_STATE_SIZE_REINIT(hash) :
201 LC_KMAC_STATE_SIZE(hash));
214#define LC_KMAC_CTX_ON_STACK(name, hashname) \
215 _Pragma("GCC diagnostic push") \
216 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
217 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
218 LC_ALIGNED_BUFFER(name##_ctx_buf, \
219 LC_KMAC_CTX_SIZE(hashname), \
220 LC_HASH_COMMON_ALIGNMENT); \
221 struct lc_kmac_ctx *name = (struct lc_kmac_ctx *)name##_ctx_buf; \
222 LC_KMAC_SET_CTX(name, hashname); \
223 lc_kmac_zero(name); \
224 _Pragma("GCC diagnostic pop")
237#define LC_KMAC_CTX_ON_STACK_REINIT(name, hashname) \
238 _Pragma("GCC diagnostic push") \
239 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
240 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
241 LC_ALIGNED_BUFFER(name##_ctx_buf, \
242 LC_KMAC_CTX_SIZE_REINIT(hashname), \
243 LC_HASH_COMMON_ALIGNMENT); \
244 struct lc_kmac_ctx *name = (struct lc_kmac_ctx *)name##_ctx_buf; \
245 LC_KMAC_SET_CTX_REINIT(name, hashname); \
246 lc_kmac_zero(name); \
247 _Pragma("GCC diagnostic pop")
258 struct lc_hash_ctx *hash_ctx;
263 hash_ctx = &kmac_ctx->hash_ctx;
284 size_t keylen,
const uint8_t *s,
size_t slen,
285 const uint8_t *in,
size_t inlen, uint8_t *mac,
314 size_t keylen,
const uint8_t *s,
size_t slen,
315 const uint8_t *in,
size_t inlen, uint8_t *mac,
343#define LC_KMAC_KDF_DRNG_CTX_SIZE(hashname) \
344 (sizeof(struct lc_rng_ctx) + LC_KMAC_CTX_SIZE(hashname))
346#define LC_KMAC_KDF_DRNG_SET_CTX(name, hashname) LC_KMAC_SET_CTX(name, hashname)
348#define LC_KMAC_KDF_RNG_CTX(name, hashname) \
349 LC_RNG_CTX(name, lc_kmac_rng); \
350 LC_KMAC_KDF_DRNG_SET_CTX(((struct lc_kmac_ctx *)(name->rng_state)), \
361#define LC_KMAC_KDF_DRNG_CTX_ON_STACK(name, hashname) \
362 _Pragma("GCC diagnostic push") \
363 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
364 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
365 LC_ALIGNED_BUFFER(name##_ctx_buf, \
366 LC_KMAC_KDF_DRNG_CTX_SIZE(hashname), \
367 LC_HASH_COMMON_ALIGNMENT); \
368 struct lc_rng_ctx *name = (struct lc_rng_ctx *)name##_ctx_buf; \
369 LC_KMAC_KDF_RNG_CTX(name, hashname); \
370 _Pragma("GCC diagnostic pop")
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.
const struct lc_rng * lc_kmac_rng
void lc_kmac_final(struct lc_kmac_ctx *kmac_ctx, uint8_t *mac, size_t maclen)
Calculate KMAC MAC.
#define LC_KMAC_CTX_ON_STACK(name, hashname)
Allocate stack memory for the KMAC context.
int lc_kmac_alloc(const struct lc_hash *hash, struct lc_kmac_ctx **kmac_ctx, uint32_t flags)
Allocate KMAC context on heap.
static void lc_kmac_zero(struct lc_kmac_ctx *kmac_ctx)
Zeroize KMAC context allocated with either LC_KMAC_CTX_ON_STACK or lc_kmac_alloc.
static size_t lc_kmac_macsize(struct lc_kmac_ctx *kmac_ctx)
Return the MAC size.
static void lc_kmac(const struct lc_hash *hash, const uint8_t *key, size_t keylen, const uint8_t *s, size_t slen, const uint8_t *in, size_t inlen, uint8_t *mac, size_t maclen)
Calculate KMAC - one-shot.
void lc_kmac_final_xof(struct lc_kmac_ctx *kmac_ctx, uint8_t *mac, size_t maclen)
Calculate KMAC MAC in XOF mode.
void lc_kmac_update(struct lc_kmac_ctx *kmac_ctx, const uint8_t *in, size_t inlen)
Update KMAC.
static void lc_kmac_xof(const struct lc_hash *hash, const uint8_t *key, size_t keylen, const uint8_t *s, size_t slen, const uint8_t *in, size_t inlen, uint8_t *mac, size_t maclen)
Calculate KMAC in XOF mode - one-shot.
int lc_kmac_rng_alloc(struct lc_rng_ctx **state, const struct lc_hash *hash)
Allocation of a KMAC DRNG context.
void lc_kmac_zero_free(struct lc_kmac_ctx *kmac_ctx)
Zeroize and free KMAC context.
void lc_kmac_init(struct lc_kmac_ctx *kmac_ctx, const uint8_t *key, size_t klen, const uint8_t *s, size_t slen)
Initialize KMAC context.
void lc_kmac_reinit(struct lc_kmac_ctx *kmac_ctx)
Re-initialize KMAC context after a kmac_final operation.
static void lc_memset_secure(void *s, int c, size_t n)