Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_kmac.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2026, Stephan Mueller <smueller@chronox.de>
3 *
4 * License: see LICENSE file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20#ifndef LC_KMAC_H
21#define LC_KMAC_H
22
23#include "lc_hash.h"
24#include "lc_rng.h"
25#include "lc_sha3.h"
26#include "lc_status.h"
27#include "lc_memset_secure.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
34struct lc_kmac_ctx {
35 uint8_t final_called : 1;
36 uint8_t rng_initialized : 1;
37 uint8_t *shadow_ctx;
38 struct lc_hash_ctx hash_ctx;
39};
40
41#define LC_KMAC_STATE_SIZE (LC_SHA3_224_STATE_SIZE)
42#define LC_KMAC_STATE_SIZE_REINIT (2 * LC_SHA3_224_STATE_SIZE)
43#define LC_KMAC_CTX_SIZE (LC_KMAC_STATE_SIZE + sizeof(struct lc_kmac_ctx))
44#define LC_KMAC_CTX_SIZE_REINIT \
45 (LC_KMAC_STATE_SIZE_REINIT + sizeof(struct lc_kmac_ctx))
46
47#define _LC_KMAC_SET_CTX(name, hashname, ctx, offset) \
48 _LC_HASH_SET_CTX((&name->hash_ctx), hashname, ctx, offset); \
49 name->shadow_ctx = NULL
50
51#define LC_KMAC_SET_CTX(name, hashname) \
52 _LC_KMAC_SET_CTX(name, hashname, name, sizeof(struct lc_kmac_ctx))
53
54#define _LC_KMAC_SET_CTX_REINIT(name, hashname, ctx, offset) \
55 _LC_HASH_SET_CTX((&name->hash_ctx), hashname, ctx, offset); \
56 name->shadow_ctx = (uint8_t *)((uint8_t *)ctx + offset + \
57 LC_HASH_STATE_SIZE(hashname))
58
59#define LC_KMAC_SET_CTX_REINIT(name, hashname) \
60 _LC_KMAC_SET_CTX_REINIT(name, hashname, name, \
61 sizeof(struct lc_kmac_ctx))
63
84int lc_kmac_init(struct lc_kmac_ctx *kmac_ctx, const uint8_t *key, size_t klen,
85 const uint8_t *s, size_t slen);
86
97void lc_kmac_reinit(struct lc_kmac_ctx *kmac_ctx);
98
108void lc_kmac_update(struct lc_kmac_ctx *kmac_ctx, const uint8_t *in,
109 size_t inlen);
110
126int lc_kmac_final(struct lc_kmac_ctx *kmac_ctx, uint8_t *mac, size_t maclen);
127
158void lc_kmac_final_xof(struct lc_kmac_ctx *kmac_ctx, uint8_t *mac,
159 size_t maclen);
160
174int lc_kmac_alloc(const struct lc_hash *hash, struct lc_kmac_ctx **kmac_ctx,
175 uint32_t flags);
176
177/*
178 * Support re-initialization of state. You set a key during kmac_init,
179 * perform a full KMAC operation. After a kmac_final, the re-initialization
180 * support allows you to call kmac_reinit and reuse the state for a new
181 * operation without providing the keys again. If in doubt, initialize
182 * the context with re-init support. The difference is that more memory is
183 * allocated to retain the initialized state.
184 */
185#define LC_KMAC_FLAGS_SUPPORT_REINIT (1 << 0)
186
193void lc_kmac_zero_free(struct lc_kmac_ctx *kmac_ctx);
194
202void lc_kmac_zero(struct lc_kmac_ctx *kmac_ctx);
203
213
222enum lc_alg_status_val lc_kmac_ctx_alg_status(const struct lc_hash_ctx *ctx);
223
235#define LC_KMAC_CTX_ON_STACK(name, hashname) \
236 _Pragma("GCC diagnostic push") _Pragma( \
237 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
238 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_KMAC_CTX_SIZE, \
239 LC_HASH_COMMON_ALIGNMENT); \
240 struct lc_kmac_ctx *name = (struct lc_kmac_ctx *)name##_ctx_buf; \
241 LC_KMAC_SET_CTX(name, hashname); \
242 lc_kmac_zero(name); \
243 _Pragma("GCC diagnostic pop")
244
257#define LC_KMAC_CTX_ON_STACK_REINIT(name, hashname) \
258 _Pragma("GCC diagnostic push") _Pragma( \
259 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
260 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_KMAC_CTX_SIZE_REINIT, \
261 LC_HASH_COMMON_ALIGNMENT); \
262 struct lc_kmac_ctx *name = (struct lc_kmac_ctx *)name##_ctx_buf; \
263 LC_KMAC_SET_CTX_REINIT(name, hashname); \
264 lc_kmac_zero(name); \
265 _Pragma("GCC diagnostic pop")
266
275size_t lc_kmac_macsize(struct lc_kmac_ctx *kmac_ctx);
276
297int lc_kmac(const struct lc_hash *hash, const uint8_t *key, size_t keylen,
298 const uint8_t *s, size_t slen, const uint8_t *in, size_t inlen,
299 uint8_t *mac, size_t maclen);
300
321int lc_kmac_xof(const struct lc_hash *hash, const uint8_t *key, size_t keylen,
322 const uint8_t *s, size_t slen, const uint8_t *in, size_t inlen,
323 uint8_t *mac, size_t maclen);
324
325/******************************** KMAC as RNG *********************************/
326
339/* KMAC DRNG implementation */
340extern const struct lc_rng *lc_kmac_rng;
341
343#define LC_KMAC_KDF_DRNG_CTX_SIZE (sizeof(struct lc_rng_ctx) + LC_KMAC_CTX_SIZE)
344
345#define LC_KMAC_KDF_DRNG_SET_CTX(name, hashname) LC_KMAC_SET_CTX(name, hashname)
346
347#define LC_KMAC_KDF_RNG_CTX(name, hashname) \
348 LC_RNG_CTX(name, lc_kmac_rng); \
349 LC_KMAC_KDF_DRNG_SET_CTX(((struct lc_kmac_ctx *)(name->rng_state)), \
350 hashname); \
351 lc_rng_zero(name)
353
364#define LC_KMAC_KDF_DRNG_CTX_ON_STACK(name, hashname) \
365 _Pragma("GCC diagnostic push") _Pragma( \
366 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
367 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_KMAC_KDF_DRNG_CTX_SIZE, \
368 LC_HASH_COMMON_ALIGNMENT); \
369 struct lc_rng_ctx *name = (struct lc_rng_ctx *)name##_ctx_buf; \
370 LC_KMAC_KDF_RNG_CTX(name, hashname); \
371 _Pragma("GCC diagnostic pop")
372
390int lc_kmac_rng_alloc(struct lc_rng_ctx **state, const struct lc_hash *hash);
391
392#ifdef __cplusplus
393}
394#endif
395
396#endif /* LC_KMAC_H */
int 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
int lc_kmac_rng_alloc(struct lc_rng_ctx **state, const struct lc_hash *hash)
Allocation of a KMAC DRNG context.
int 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_init(struct lc_kmac_ctx *kmac_ctx, const uint8_t *key, size_t klen, const uint8_t *s, size_t slen)
Initialize KMAC context.
int lc_kmac_final(struct lc_kmac_ctx *kmac_ctx, uint8_t *mac, size_t maclen)
Calculate KMAC MAC.
int lc_kmac_alloc(const struct lc_hash *hash, struct lc_kmac_ctx **kmac_ctx, uint32_t flags)
Allocate KMAC context on heap.
enum lc_alg_status_val lc_kmac_alg_status(const struct lc_hash *hash)
Obtain algorithm status.
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.
void lc_kmac_final_xof(struct lc_kmac_ctx *kmac_ctx, uint8_t *mac, size_t maclen)
Calculate KMAC MAC in XOF mode.
int 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_update(struct lc_kmac_ctx *kmac_ctx, const uint8_t *in, size_t inlen)
Update KMAC.
size_t lc_kmac_macsize(struct lc_kmac_ctx *kmac_ctx)
Return the MAC size.
void lc_kmac_zero_free(struct lc_kmac_ctx *kmac_ctx)
Zeroize and free KMAC context.
enum lc_alg_status_val lc_kmac_ctx_alg_status(const struct lc_hash_ctx *ctx)
Obtain algorithm status.
void lc_kmac_reinit(struct lc_kmac_ctx *kmac_ctx)
Re-initialize KMAC context after a kmac_final operation.
lc_alg_status_val
Definition: lc_status.h:63