Leancrypto 1.6.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_hash_drbg.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 - 2025, 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_HASH_DRBG_H
21#define LC_HASH_DRBG_H
22
23#include "lc_drbg.h"
24#include "lc_rng.h"
25#include "lc_sha512.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
32#define LC_DRBG_HASH_STATELEN 111
33#define LC_DRBG_HASH_BLOCKLEN 64
34
35struct lc_drbg_hash_state {
36 struct lc_hash_ctx hash_ctx; /* Cipher handle */
37 uint8_t hash_state[LC_SHA512_STATE_SIZE + LC_HASH_COMMON_ALIGNMENT];
38 uint8_t V[LC_DRBG_HASH_STATELEN]; /* internal state 10.1.1.1 1a) */
39 uint8_t C[LC_DRBG_HASH_STATELEN]; /* static value 10.1.1.1 1b) */
40 uint8_t scratchpad[LC_DRBG_HASH_STATELEN + LC_DRBG_HASH_BLOCKLEN];
41 /* working mem */
42
43 /* Number of RNG requests since last reseed -- 10.1.1.1 1c) */
44 size_t reseed_ctr;
45 unsigned int seeded : 1;
46};
47
48#define LC_DRBG_HASH_STATE_SIZE (sizeof(struct lc_drbg_hash_state))
49#define LC_DRBG_HASH_CTX_SIZE \
50 ((unsigned long)(LC_DRBG_HASH_STATE_SIZE + sizeof(struct lc_rng)))
51
52#define _LC_DRBG_HASH_SET_CTX(name, ctx, offset) \
53 LC_SHA512_CTX((&(name)->hash_ctx)); \
54 (name)->reseed_ctr = 0; \
55 (name)->seeded = 0
56
57#define LC_DRBG_HASH_SET_CTX(name) \
58 _LC_DRBG_HASH_SET_CTX(name, name, sizeof(struct lc_drbg_hash_state))
59
60extern const struct lc_rng *lc_hash_drbg;
61
62#define LC_DRBG_HASH_RNG_CTX(name) \
63 LC_RNG_CTX((name), lc_hash_drbg); \
64 LC_DRBG_HASH_SET_CTX((struct lc_drbg_hash_state *)name->rng_state); \
65 lc_rng_zero(name)
67
75#define LC_DRBG_HASH_CTX_ON_STACK(name) \
76 _Pragma("GCC diagnostic push") _Pragma( \
77 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
78 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_DRBG_HASH_CTX_SIZE, \
79 LC_HASH_COMMON_ALIGNMENT); \
80 struct lc_rng_ctx *name = (struct lc_rng_ctx *)name##_ctx_buf; \
81 LC_DRBG_HASH_RNG_CTX(name); \
82 _Pragma("GCC diagnostic pop")
83
93int lc_drbg_hash_alloc(struct lc_rng_ctx **drbg);
94
111int lc_drbg_hash_healthcheck_sanity(struct lc_rng_ctx *drbg);
112
113#ifdef __cplusplus
114}
115#endif
116
117#endif /* LC_HASH_DRBG_H */
int lc_drbg_hash_healthcheck_sanity(struct lc_rng_ctx *drbg)
Tests as defined in 11.3.2 in addition to the cipher tests: testing of the error handling.
int lc_drbg_hash_alloc(struct lc_rng_ctx **drbg)
Allocate Hash DRBG context on heap.