Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_cshake.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_CSHAKE_H
21#define LC_CSHAKE_H
22
23#include "lc_hash.h"
24#include "lc_sha3.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
62int lc_cshake_init(struct lc_hash_ctx *ctx, const uint8_t *n, size_t nlen,
63 const uint8_t *s, size_t slen);
64
78static inline int lc_cshake_final(struct lc_hash_ctx *ctx, uint8_t *out,
79 size_t outlen)
80{
81 int ret;
82
83 ret = lc_hash_set_digestsize(ctx, outlen);
84 if (ret)
85 return ret;
86 lc_hash_final(ctx, out);
87
88 return 0;
89}
90
91/*
92 * Separate cSHAKE API with re-initialization support
93 *
94 * Re-initialization means that any state created by the init operation can be
95 * re-established during re-init.
96 */
98struct lc_cshake_ctx {
99 uint8_t *shadow_ctx;
100 struct lc_hash_ctx hash_ctx;
101};
102
103#define LC_CSHAKE_STATE_SIZE (LC_SHA3_224_STATE_SIZE)
104#define LC_CSHAKE_STATE_SIZE_REINIT (2 * LC_SHA3_224_STATE_SIZE)
105#define LC_CSHAKE_CTX_SIZE (LC_CSHAKE_STATE_SIZE + sizeof(struct lc_cshake_ctx))
106#define LC_CSHAKE_CTX_SIZE_REINIT \
107 (LC_CSHAKE_STATE_SIZE_REINIT + sizeof(struct lc_cshake_ctx))
108
109#define _LC_CSHAKE_SET_CTX(name, hashname, ctx, offset) \
110 _LC_HASH_SET_CTX((&name->hash_ctx), hashname, ctx, offset); \
111 name->shadow_ctx = NULL
112
113#define LC_CSHAKE_SET_CTX(name, hashname) \
114 _LC_CSHAKE_SET_CTX(name, hashname, name, sizeof(struct lc_cshake_ctx))
115
116#define _LC_CSHAKE_SET_CTX_REINIT(name, hashname, ctx, offset) \
117 _LC_HASH_SET_CTX((&name->hash_ctx), hashname, ctx, offset); \
118 name->shadow_ctx = (uint8_t *)((uint8_t *)ctx + offset + \
119 LC_HASH_STATE_SIZE(hashname))
120
121#define LC_CSHAKE_SET_CTX_REINIT(name, hashname) \
122 _LC_CSHAKE_SET_CTX_REINIT(name, hashname, name, \
123 sizeof(struct lc_cshake_ctx))
125
142int lc_cshake_ctx_init(struct lc_cshake_ctx *cshake_ctx, const uint8_t *n,
143 size_t nlen, const uint8_t *s, size_t slen);
144
154void lc_cshake_ctx_reinit(struct lc_cshake_ctx *cshake_ctx);
155
164void lc_cshake_ctx_update(struct lc_cshake_ctx *cshake_ctx, const uint8_t *in,
165 size_t inlen);
166
178void lc_cshake_ctx_final(struct lc_cshake_ctx *cshake_ctx, uint8_t *out,
179 size_t outlen);
180
193int lc_cshake_ctx_alloc(const struct lc_hash *hash,
194 struct lc_cshake_ctx **cshake_ctx, uint32_t flags);
195
196/*
197 * Support re-initialization of state. You set a key during cshake_init,
198 * perform a full CSHAKE operation. After a cshake_final, the re-initialization
199 * support allows you to call cshake_reinit and reuse the state for a new
200 * operation without providing the keys again. If in doubt, initialize
201 * the context with re-init support. The difference is that more memory is
202 * allocated to retain the initialized state.
203 */
204#define LC_CSHAKE_FLAGS_SUPPORT_REINIT (1 << 0)
205
211void lc_cshake_ctx_zero_free(struct lc_cshake_ctx *cshake_ctx);
212
219static inline void lc_cshake_ctx_zero(struct lc_cshake_ctx *cshake_ctx)
220{
221 if (!cshake_ctx)
222 return;
223
224 lc_memset_secure((uint8_t *)cshake_ctx + sizeof(struct lc_cshake_ctx),
225 0,
226 cshake_ctx->shadow_ctx ? LC_CSHAKE_STATE_SIZE_REINIT :
227 LC_CSHAKE_STATE_SIZE);
228}
229
240#define LC_CSHAKE_CTX_ON_STACK(name, hashname) \
241 _Pragma("GCC diagnostic push") _Pragma( \
242 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
243 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_CSHAKE_CTX_SIZE, \
244 LC_HASH_COMMON_ALIGNMENT); \
245 struct lc_cshake_ctx *name = (struct lc_cshake_ctx *)name##_ctx_buf; \
246 LC_CSHAKE_SET_CTX(name, hashname); \
247 lc_cshake_ctx_zero(name); \
248 _Pragma("GCC diagnostic pop")
249
261#define LC_CSHAKE_CTX_ON_STACK_REINIT(name, hashname) \
262 _Pragma("GCC diagnostic push") _Pragma( \
263 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
264 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_CSHAKE_CTX_SIZE_REINIT, \
265 LC_HASH_COMMON_ALIGNMENT); \
266 struct lc_cshake_ctx *name = (struct lc_cshake_ctx *)name##_ctx_buf; \
267 LC_CSHAKE_SET_CTX_REINIT(name, hashname); \
268 lc_cshake_ctx_zero(name); \
269 _Pragma("GCC diagnostic pop")
270
271#ifdef __cplusplus
272}
273#endif
274
275#endif /* LC_CSHAKE_H */
int lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
void lc_hash_final(struct lc_hash_ctx *hash_ctx, uint8_t *digest)
Calculate message digest.
int 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.
int lc_cshake_ctx_init(struct lc_cshake_ctx *cshake_ctx, const uint8_t *n, size_t nlen, const uint8_t *s, size_t slen)
Initialize the hash state with re-init support following the cSHAKE specification.
static int lc_cshake_final(struct lc_hash_ctx *ctx, uint8_t *out, size_t outlen)
Generate a cSHAKE message digest from a given state.
Definition: lc_cshake.h:78
void lc_cshake_ctx_update(struct lc_cshake_ctx *cshake_ctx, const uint8_t *in, size_t inlen)
Update CSHAKE.
int lc_cshake_ctx_alloc(const struct lc_hash *hash, struct lc_cshake_ctx **cshake_ctx, uint32_t flags)
Allocate CSHAKE context on heap.
int lc_cshake_init(struct lc_hash_ctx *ctx, const uint8_t *n, size_t nlen, const uint8_t *s, size_t slen)
Initialize the hash state following the cSHAKE specification.
void lc_cshake_ctx_final(struct lc_cshake_ctx *cshake_ctx, uint8_t *out, size_t outlen)
Generate a cSHAKE message digest from a given state.
void lc_cshake_ctx_zero_free(struct lc_cshake_ctx *cshake_ctx)
Zeroize and free CSHAKE context.
static void lc_cshake_ctx_zero(struct lc_cshake_ctx *cshake_ctx)
Zeroize CSHAKE context allocated with either LC_CSHAKE_CTX_ON_STACK or lc_cshake_alloc.
Definition: lc_cshake.h:219
void lc_cshake_ctx_reinit(struct lc_cshake_ctx *cshake_ctx)
Re-initialize CSHAKE context after a cshake_final operation.
static void lc_memset_secure(void *s, int c, size_t n)