Leancrypto 1.6.0
Post-Quantum Cryptographic Library
Loading...
Searching...
No Matches
lc_rng.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_RNG_H
21#define LC_RNG_H
22
23#include "lc_memory_support.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
30struct lc_rng {
31 int (*generate)(void *state, const uint8_t *addtl_input,
32 size_t addtl_input_len, uint8_t *out, size_t outlen);
33 int (*seed)(void *state, const uint8_t *seed, size_t seedlen,
34 const uint8_t *persbuf, size_t perslen);
35 void (*zero)(void *state);
36 uint64_t algorithm_type;
37};
38
39struct lc_rng_ctx {
40 const struct lc_rng *rng;
41 void *rng_state;
42};
43
44#define LC_RNG_CTX(name, cb) \
45 name->rng = cb; \
46 name->rng_state = (uint8_t *)(name) + sizeof(struct lc_rng_ctx)
48
62
72extern struct lc_rng_ctx *lc_seeded_rng;
73
83void lc_rng_check(struct lc_rng_ctx **ctx);
84
91void lc_rng_zero(struct lc_rng_ctx *ctx);
92
99void lc_rng_zero_free(struct lc_rng_ctx *ctx);
100
116int lc_rng_generate(struct lc_rng_ctx *ctx, const uint8_t *addtl_input,
117 size_t addtl_input_len, uint8_t *out, size_t outlen);
118
135int lc_rng_seed(struct lc_rng_ctx *ctx, const uint8_t *seed, size_t seedlen,
136 const uint8_t *persbuf, size_t perslen);
137
157int lc_rng_set_seeded(struct lc_rng_ctx *new_ctx);
158
167uint64_t lc_rng_algorithm_type(const struct lc_rng *rng);
168
177uint64_t lc_rng_ctx_algorithm_type(const struct lc_rng_ctx *ctx);
178
179#ifdef __cplusplus
180}
181#endif
182
183#endif /* LC_RNG_H */
uint64_t lc_rng_ctx_algorithm_type(const struct lc_rng_ctx *ctx)
Obtain algorithm type usable with lc_alg_status.
void lc_rng_zero(struct lc_rng_ctx *ctx)
Zeroize RNG context.
int lc_rng_set_seeded(struct lc_rng_ctx *new_ctx)
Set an externally defined RNG as the seeded RNG.
struct lc_rng_ctx * lc_seeded_rng
One common instance of a seeded DRNG. The caller does not need to consider the seeding and reseeding ...
int lc_rng_generate(struct lc_rng_ctx *ctx, const uint8_t *addtl_input, size_t addtl_input_len, uint8_t *out, size_t outlen)
Obtain random numbers.
void lc_rng_check(struct lc_rng_ctx **ctx)
Get the default leancrypto RNG.
uint64_t lc_rng_algorithm_type(const struct lc_rng *rng)
Obtain algorithm type usable with lc_alg_status.
int lc_rng_seed(struct lc_rng_ctx *ctx, const uint8_t *seed, size_t seedlen, const uint8_t *persbuf, size_t perslen)
(Re)Seed the RNG
void lc_rng_zero_free(struct lc_rng_ctx *ctx)
Zeroize and free RNG context.