Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_xdrbg.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2024, 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_XDRBG256_DRNG_H
21#define LC_XDRBG256_DRNG_H
22
23#include "lc_ascon_hash.h"
24#include "lc_sha3.h"
25#include "lc_rng.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
32#define LC_XDRBG_DRNG_INITIALLY_SEEDED 0x80
33#define LC_XDRBG_DRNG_KEYSIZE_MASK 0x7F
34
35struct lc_xdrbg_drng_state {
36 uint16_t chunksize;
37 uint8_t status;
38 /*
39 * NOTE: keep the lc_hash pointer before the v variable, because
40 * lc_hash is aligned to 8 bytes and has a size of 8 bytes. This ensures
41 * that v is aligned to 8 bytes as well.
42 */
43 const struct lc_hash *xof;
44 uint8_t v[];
45};
46
47/* XDRBG-based DRNG */
48extern const struct lc_rng *lc_xdrbg_drng;
49
50/* Helper, do not call directly */
51#define LC_XDRBG_DRNG_CTX_ON_STACK(name) \
52 _Pragma("GCC diagnostic push") _Pragma( \
53 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
54 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_XDRBG256_DRNG_CTX_SIZE, \
55 LC_HASH_COMMON_ALIGNMENT); \
56 struct lc_rng_ctx *name = (struct lc_rng_ctx *)name##_ctx_buf; \
57 _Pragma("GCC diagnostic pop")
58
59#define LC_XDRBG256_DRNG_KEYSIZE 64
60/*
61 * For streamlining the access requests, the max chunk size plus the key size
62 * should be a full multiple of the SHAKE rate. As the key size is not
63 * exactly the rate size, the chunk size needs to consider it
64 */
65#define LC_XDRBG256_DRNG_MAX_CHUNK \
66 (LC_SHAKE_256_SIZE_BLOCK * 2 - LC_XDRBG256_DRNG_KEYSIZE)
67#define LC_XDRBG256_DRNG_STATE_SIZE \
68 (sizeof(struct lc_xdrbg_drng_state) + LC_XDRBG256_DRNG_KEYSIZE)
69#define LC_XDRBG256_DRNG_CTX_SIZE \
70 (sizeof(struct lc_rng) + LC_XDRBG256_DRNG_STATE_SIZE)
71
72#define LC_XDRBG256_RNG_CTX(name) \
73 LC_RNG_CTX(name, lc_xdrbg_drng); \
74 struct lc_xdrbg_drng_state *__name = name->rng_state; \
75 __name->status = LC_XDRBG256_DRNG_KEYSIZE; \
76 __name->xof = lc_shake256; \
77 __name->chunksize = LC_XDRBG256_DRNG_MAX_CHUNK; \
78 lc_xdrbg_drng->zero(name->rng_state);
80
96#define LC_XDRBG256_DRNG_CTX_ON_STACK(name) \
97 LC_XDRBG_DRNG_CTX_ON_STACK(name); \
98 LC_XDRBG256_RNG_CTX(name)
99
113int lc_xdrbg256_drng_alloc(struct lc_rng_ctx **state);
114
116#define LC_XDRBG128_DRNG_KEYSIZE 32
117
118/*
119 * For streamlining the access requests, the max chunk size plus the key size
120 * should be a full multiple of the Ascon rate. As the key size is already
121 * exactly the rate size, the chunk size does not need to consider it.
122 */
123#define LC_XDRBG128_DRNG_MAX_CHUNK (LC_ASCON_HASH_RATE * 32)
124#define LC_XDRBG128_DRNG_STATE_SIZE \
125 (sizeof(struct lc_xdrbg_drng_state) + LC_XDRBG128_DRNG_KEYSIZE)
126#define LC_XDRBG128_DRNG_CTX_SIZE \
127 (sizeof(struct lc_rng) + LC_XDRBG128_DRNG_STATE_SIZE)
128
129#define LC_XDRBG128_RNG_CTX(name) \
130 LC_RNG_CTX(name, lc_xdrbg_drng); \
131 struct lc_xdrbg_drng_state *__name = name->rng_state; \
132 __name->status = LC_XDRBG128_DRNG_KEYSIZE; \
133 __name->xof = lc_ascon_xof; \
134 __name->chunksize = LC_XDRBG128_DRNG_MAX_CHUNK; \
135 lc_xdrbg_drng->zero(name->rng_state);
137
154#define LC_XDRBG128_DRNG_CTX_ON_STACK(name) \
155 LC_XDRBG_DRNG_CTX_ON_STACK(name); \
156 LC_XDRBG128_RNG_CTX(name)
157
171int lc_xdrbg128_drng_alloc(struct lc_rng_ctx **state);
172
173#ifdef __cplusplus
174}
175#endif
176
177#endif /* LC_XDRBG256_DRNG_H */
void lc_hash(const struct lc_hash *hash, const uint8_t *in, size_t inlen, uint8_t *digest)
Calculate message digest - one-shot.
int lc_xdrbg128_drng_alloc(struct lc_rng_ctx **state)
Allocation of a XDRBG128 DRNG context using Ascon-XOF.
int lc_xdrbg256_drng_alloc(struct lc_rng_ctx **state)
Allocation of a XDRBG256 DRNG context using SHAKE-256.