Leancrypto 0.12.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_hmac_drbg.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_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(x) \
45 (2 * LC_DRBG_HMAC_STATELEN + LC_HMAC_STATE_SIZE(x))
46#define LC_DRBG_HMAC_CTX_SIZE(x) \
47 (LC_DRBG_HMAC_STATE_SIZE(x) + sizeof(struct lc_drbg_hmac_state) + \
48 sizeof(struct lc_rng))
49
50#define _LC_DRBG_HMAC_SET_CTX(name, ctx, offset) \
51 _LC_HMAC_SET_CTX((&(name)->hmac_ctx), LC_DRBG_HMAC_CORE, ctx, offset); \
52 (name)->V = (uint8_t *)((uint8_t *)ctx + offset + \
53 LC_HMAC_STATE_SIZE(LC_DRBG_HMAC_CORE)); \
54 (name)->C = (uint8_t *)((uint8_t *)ctx + offset + \
55 LC_HMAC_STATE_SIZE(LC_DRBG_HMAC_CORE) + \
56 LC_DRBG_HMAC_STATELEN); \
57 (name)->seeded = 0
58
59#define LC_DRBG_HMAC_SET_CTX(name) \
60 _LC_DRBG_HMAC_SET_CTX(name, name, sizeof(struct lc_drbg_hmac_state))
61
62extern const struct lc_rng *lc_hmac_drbg;
63
64#define LC_DRBG_HMAC_RNG_CTX(name) \
65 LC_RNG_CTX(name, lc_hmac_drbg); \
66 LC_DRBG_HMAC_SET_CTX((struct lc_drbg_hmac_state *)name->rng_state); \
67 lc_hmac_drbg->zero(name->rng_state)
69
77#define LC_DRBG_HMAC_CTX_ON_STACK(name) \
78 _Pragma("GCC diagnostic push") \
79 _Pragma("GCC diagnostic ignored \"-Wvla\"") _Pragma( \
80 "GCC diagnostic ignored \"-Wdeclaration-after-statement\"") \
81 LC_ALIGNED_BUFFER( \
82 name##_ctx_buf, \
83 LC_DRBG_HMAC_CTX_SIZE(LC_DRBG_HMAC_CORE), \
84 LC_HASH_COMMON_ALIGNMENT); \
85 struct lc_rng_ctx *name = (struct lc_rng_ctx *)name##_ctx_buf; \
86 LC_DRBG_HMAC_RNG_CTX(name); \
87 _Pragma("GCC diagnostic pop")
88
98int lc_drbg_hmac_alloc(struct lc_rng_ctx **drbg);
99
116int lc_drbg_hmac_healthcheck_sanity(struct lc_rng_ctx *drbg);
117
118#ifdef __cplusplus
119}
120#endif
121
122#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.