23#include "ext_headers.h"
34 void (*init)(
void *state);
35 void (*update)(
void *state,
const uint8_t *in,
size_t inlen);
36 void (*
final)(
void *state, uint8_t *digest);
37 void (*set_digestsize)(
void *state,
size_t digestsize);
38 size_t (*get_digestsize)(
void *state);
41 void (*sponge_permutation)(
void *state,
unsigned int rounds);
42 void (*sponge_add_bytes)(
void *state,
const uint8_t *data,
43 size_t offset,
size_t length);
44 void (*sponge_extract_bytes)(
const void *state, uint8_t *data,
45 size_t offset,
size_t length);
46 void (*sponge_newstate)(
void *state,
const uint8_t *newstate,
47 size_t offset,
size_t length);
50 unsigned short statesize;
67#ifndef LC_HASH_COMMON_ALIGNMENT
69#define LC_HASH_COMMON_ALIGNMENT 64
72#define LC_ALIGN_HASH_MASK(p) \
73 LC_ALIGN_PTR_64(p, LC_ALIGNMENT_MASK(LC_HASH_COMMON_ALIGNMENT))
75#define LC_SHA_MAX_SIZE_DIGEST 64
94#define LC_HASH_STATE_SIZE_NONALIGNED(x) ((unsigned long)(x->statesize))
95#define LC_HASH_STATE_SIZE(x) \
96 (LC_HASH_STATE_SIZE_NONALIGNED(x) + LC_HASH_COMMON_ALIGNMENT)
97#define LC_HASH_CTX_SIZE(x) (sizeof(struct lc_hash_ctx) + LC_HASH_STATE_SIZE(x))
99#define _LC_HASH_SET_CTX(name, hashname, ctx, offset) \
100 name->hash_state = LC_ALIGN_HASH_MASK(((uint8_t *)(ctx)) + (offset)); \
101 name->hash = hashname
103#define LC_HASH_SET_CTX(name, hashname) \
104 _LC_HASH_SET_CTX(name, hashname, name, sizeof(struct lc_hash_ctx))
137 hash = hash_ctx->hash;
138 hash->init(hash_ctx->hash_state);
151 const uint8_t *in,
size_t inlen)
158 hash = hash_ctx->hash;
159 hash->update(hash_ctx->hash_state, in, inlen);
192static inline void lc_hash_final(
struct lc_hash_ctx *hash_ctx, uint8_t *digest)
196 if (!hash_ctx || !digest)
199 hash = hash_ctx->hash;
200 hash->final(hash_ctx->hash_state, digest);
219 hash = hash_ctx->hash;
220 if (hash->set_digestsize)
221 hash->set_digestsize(hash_ctx->hash_state, digestsize);
238 hash = hash_ctx->hash;
239 return hash->get_digestsize(hash_ctx->hash_state);
257 hash = hash_ctx->hash;
258 return hash->sponge_rate;
275 hash = hash_ctx->hash;
276 return hash->statesize;
293 hash = hash_ctx->hash;
307#define LC_HASH_CTX_ON_STACK(name, hashname) \
308 _Pragma("GCC diagnostic push") \
309 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
310 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
311 LC_ALIGNED_BUFFER(name##_ctx_buf, \
312 LC_HASH_CTX_SIZE(hashname), \
313 LC_HASH_COMMON_ALIGNMENT); \
314 struct lc_hash_ctx *name = (struct lc_hash_ctx *)name##_ctx_buf; \
315 LC_HASH_SET_CTX(name, hashname); \
316 lc_hash_zero(name); \
317 _Pragma("GCC diagnostic pop")
370 uint8_t *digest,
size_t digestlen);
393 if (!state || !hash || !hash->sponge_permutation)
396 hash->sponge_permutation(state, rounds);
426 const uint8_t *data,
size_t offset,
429 if (!state || !hash || !hash->sponge_add_bytes)
432 hash->sponge_add_bytes(state, data, offset, length);
460 const void *state, uint8_t *data,
461 size_t offset,
size_t length)
463 if (!state || !hash || !hash->sponge_extract_bytes)
466 hash->sponge_extract_bytes(state, data, offset, length);
489 const uint8_t *data,
size_t offset,
492 if (!state || !hash || !hash->sponge_newstate)
495 hash->sponge_newstate(state, data, offset, length);
static int lc_sponge_add_bytes(const struct lc_hash *hash, void *state, const uint8_t *data, size_t offset, size_t length)
Function to add (in GF(2), using bitwise exclusive-or) data given as bytes into the sponge state.
static size_t lc_hash_digestsize(struct lc_hash_ctx *hash_ctx)
Get the size of the message digest.
static void lc_hash_set_digestsize(struct lc_hash_ctx *hash_ctx, size_t digestsize)
Set the size of the message digest - this call is intended for SHAKE.
static int lc_sponge(const struct lc_hash *hash, void *state, unsigned int rounds)
Perform Sponge permutation on buffer.
static int lc_sponge_extract_bytes(const struct lc_hash *hash, const void *state, uint8_t *data, size_t offset, size_t length)
Function to retrieve data from the state. The bit positions that are retrieved by this function are f...
int lc_hash_alloc(const struct lc_hash *hash, struct lc_hash_ctx **hash_ctx)
Allocate Hash context on heap.
void lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
void lc_xof(const struct lc_hash *xof, const uint8_t *in, size_t inlen, uint8_t *digest, size_t digestlen)
Calculate message digest for an XOF - one-shot.
static unsigned int lc_hash_ctxsize(struct lc_hash_ctx *hash_ctx)
Get the context size of the message digest implementation.
static unsigned int lc_hash_blocksize(struct lc_hash_ctx *hash_ctx)
Get the block size of the message digest (or the "rate" in terms of Sponge-based algorithms)
static void lc_hash_zero(struct lc_hash_ctx *hash_ctx)
Zeroize Hash context allocated with either LC_HASH_CTX_ON_STACK or lc_hmac_alloc.
static void lc_hash_final(struct lc_hash_ctx *hash_ctx, uint8_t *digest)
Calculate message digest.
static void lc_hash_update(struct lc_hash_ctx *hash_ctx, const uint8_t *in, size_t inlen)
Update hash.
void lc_hash_zero_free(struct lc_hash_ctx *hash_ctx)
Zeroize and free hash context.
static void lc_hash_init(struct lc_hash_ctx *hash_ctx)
Initialize hash context.
static int lc_sponge_newstate(const struct lc_hash *hash, void *state, const uint8_t *data, size_t offset, size_t length)
Function to insert a complete new sponge state.
static void lc_memset_secure(void *s, int c, size_t n)