Leancrypto 1.7.2
Post-Quantum Cryptographic Library
lc_hmac_drbg.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_HMAC_DRBG_H
21#define LC_HMAC_DRBG_H
22
23#include "lc_drbg.h"
24#include "lc_hmac.h"
25#include "lc_rng.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#if !defined(LC_DRBG_HMAC_STATELEN) || !defined(LC_DRBG_HMAC_BLOCKLEN) || \
32 !defined(LC_DRBG_HMAC_CORE)
33#error "Do not include this header file directly! Use lc_hmac_drbg_<hashtype>.h"
34#endif
35
37struct lc_drbg_hmac_state {
38 struct lc_hmac_ctx hmac_ctx; /* Cipher handle - HMAC_MAX_STATE_SIZE */
39 uint8_t *V; /* internal state 10.1.1.1 1a) - DRBG_STATELEN */
40 uint8_t *C; /* static value 10.1.1.1 1b) - DRBG_STATELEN */
41 unsigned int seeded : 1;
42};
43
44#define LC_DRBG_HMAC_STATE_SIZE (2 * LC_DRBG_HMAC_STATELEN + LC_HMAC_STATE_SIZE)
45#define LC_DRBG_HMAC_CTX_SIZE \
46 (LC_DRBG_HMAC_STATE_SIZE + sizeof(struct lc_drbg_hmac_state) + \
47 sizeof(struct lc_rng))
48
49#define _LC_DRBG_HMAC_SET_CTX(name, ctx, offset) \
50 _LC_HMAC_SET_CTX((&(name)->hmac_ctx), LC_DRBG_HMAC_CORE, ctx, offset); \
51 (name)->V = (uint8_t *)((uint8_t *)ctx + offset + LC_HMAC_STATE_SIZE); \
52 (name)->C = (uint8_t *)((uint8_t *)ctx + offset + LC_HMAC_STATE_SIZE + \
53 LC_DRBG_HMAC_STATELEN); \
54 (name)->seeded = 0
55
56#define LC_DRBG_HMAC_SET_CTX(name) \
57 _LC_DRBG_HMAC_SET_CTX(name, name, sizeof(struct lc_drbg_hmac_state))
58
59extern const struct lc_rng *lc_hmac_drbg;
60
61#define LC_DRBG_HMAC_RNG_CTX(name) \
62 LC_RNG_CTX(name, lc_hmac_drbg); \
63 LC_DRBG_HMAC_SET_CTX((struct lc_drbg_hmac_state *)name->rng_state); \
64 lc_hmac_drbg->zero(name->rng_state)
66
74#define LC_DRBG_HMAC_CTX_ON_STACK(name) \
75 _Pragma("GCC diagnostic push") _Pragma( \
76 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
77 LC_ALIGNED_BUFFER(name##_ctx_buf, LC_DRBG_HMAC_CTX_SIZE, \
78 LC_HASH_COMMON_ALIGNMENT); \
79 struct lc_rng_ctx *name = (struct lc_rng_ctx *)name##_ctx_buf; \
80 LC_DRBG_HMAC_RNG_CTX(name); \
81 _Pragma("GCC diagnostic pop")
82
92int lc_drbg_hmac_alloc(struct lc_rng_ctx **drbg);
93
110int lc_drbg_hmac_healthcheck_sanity(struct lc_rng_ctx *drbg);
111
112#ifdef __cplusplus
113}
114#endif
115
116#endif /* LC_HMAC_DRBG_H */
int lc_drbg_hmac_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_hmac_alloc(struct lc_rng_ctx **drbg)
Allocate HMAC DRBG context on heap.