Loading...
Searching...
No Matches
entropy_source.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 HAW Hamburg
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
21#ifndef ENTROPY_SOURCE_H
22#define ENTROPY_SOURCE_H
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#include <stddef.h>
29#include <inttypes.h>
30#include <assert.h>
31
45
49typedef struct {
50 uint8_t old_sample;
51 uint16_t cnt_rep;
52 uint8_t c_rep;
54
58typedef struct {
59 uint8_t old_sample;
60 uint16_t cnt_prop;
61 uint16_t cnt_window;
62 uint16_t c_prop;
64
70#define ENTROPY_SOURCE_HMIN_SCALE(x) ((x * (1UL << 16)))
71
76#define ENTROPY_SOURCE_HMIN_SCALE_BACK(x) ((float)x / (1UL << 16))
77
93#ifndef CONFIG_ENTROPY_SOURCE_TESTS_WIN
94#define CONFIG_ENTROPY_SOURCE_TESTS_WIN (512)
95#endif
96
103#ifndef CONFIG_ENTROPY_SOURCE_NEUMANN_ABORT
104#define CONFIG_ENTROPY_SOURCE_NEUMANN_ABORT (5)
105#endif
120typedef int (*entropy_source_sample_func_t)(uint8_t *sample);
121
141 uint8_t *out, size_t len);
142
161static inline uint32_t entropy_source_test_rep_cutoff(uint32_t entropy_per_sample)
162{
163 return (1 + ((20 * 65536) / entropy_per_sample));
164}
165
177static inline int entropy_source_test_prop_cutoff(uint32_t entropy_per_sample)
178{
179 int ret;
180
181 if (entropy_per_sample < 49152UL) { /* 0.75 bit/sample */
182 ret = 410;
183 }
184 else if (entropy_per_sample < 98304UL) { /* 1.5 bit/sample */
185 ret = 311;
186 }
187 else if (entropy_per_sample < 196608UL) { /* 3 bit/sample */
188 ret = 177;
189 }
190 else if (entropy_per_sample < 393216UL) { /* 6 bit/sample */
191 ret = 62;
192 }
193 else if (entropy_per_sample <= 524288UL) { /* 8 bit/sample */
194 ret = 13;
195 }
196 else {
198 }
199
200 return ret;
201}
202
211 entropy_source_tests_rep_t *state, uint16_t c_rep)
212{
213 assert(state != NULL);
214
215 state->old_sample = 0;
216 state->cnt_rep = 0;
217 state->c_rep = c_rep;
218}
219
228 entropy_source_tests_prop_t *state, uint16_t c_prop)
229{
230 assert(state != NULL);
231
232 state->old_sample = 0;
233 state->cnt_prop = 0;
235 state->c_prop = c_prop;
236}
237
250
263 uint8_t sample);
264
281 entropy_source_tests_prop_t *state_prop,
282 uint8_t sample)
283{
284 int ret = ENTROPY_SOURCE_OK;
285
286 if (entropy_source_test_rep(state_rep, sample) < 0) {
288 }
289 if (entropy_source_test_prop(state_prop, sample) < 0) {
290 /* If repetition count failed before, indicate that both tests failed */
291 if (ret == ENTROPY_SOURCE_ERR_TEST_REP) {
293 }
294 else {
296 }
297 }
298 return ret;
299}
300
301#ifdef __cplusplus
302}
303#endif
304
305#endif /* ENTROPY_SOURCE_H */
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition assert.h:136
static void entropy_source_test_prop_init(entropy_source_tests_prop_t *state, uint16_t c_prop)
Initialize structure for Adaptive Proportion Test.
static uint32_t entropy_source_test_rep_cutoff(uint32_t entropy_per_sample)
Calculate cutoff value for Repetition Count Test (NIST SP 800-90B 4.4.1)
entropy_source_error_t
Entropy source error codes.
int entropy_source_test_rep(entropy_source_tests_rep_t *state, uint8_t sample)
Performs Repetition Count Test (NIST SP 800-90B 4.4.1).
static int entropy_source_test(entropy_source_tests_rep_t *state_rep, entropy_source_tests_prop_t *state_prop, uint8_t sample)
Convenience function to perform entropy_source_test_rep and entropy_source_test_prop.
int entropy_source_test_prop(entropy_source_tests_prop_t *state, uint8_t sample)
Performs Adaptive Proportion Test (NIST SP 800-90B 4.4.2).
int(* entropy_source_sample_func_t)(uint8_t *sample)
Get one sample of the entropy source.
int entropy_source_neumann_unbias(entropy_source_sample_func_t func, uint8_t *out, size_t len)
Applies von Neumann unbiasing.
static int entropy_source_test_prop_cutoff(uint32_t entropy_per_sample)
Calculate cutoff value for Adaptive Proportion Test (NIST SP 800-90B 4.4.2)
static void entropy_source_test_rep_init(entropy_source_tests_rep_t *state, uint16_t c_rep)
Initialize structure for Repetition Count Test.
@ ENTROPY_SOURCE_ERR_COND
Conditioning error.
@ ENTROPY_SOURCE_ERR_CONFIG
Source configuration error.
@ ENTROPY_SOURCE_ERR_TEST_BOTH
Repetition count and Adaptive proportion test error.
@ ENTROPY_SOURCE_ERR_TEST_PROP
Adaptive proportion test error.
@ ENTROPY_SOURCE_ERR_TEST_REP
Repetition count test error.
@ ENTROPY_SOURCE_ERR_INIT
Source initialization error.
@ ENTROPY_SOURCE_OK
Success.
#define CONFIG_ENTROPY_SOURCE_TESTS_WIN
Window size for Adaptive Proportion Test (NIST SP 800-90B 4.4.2).
Adds include for missing inttype definitions.
Data structure for Adaptive Proportion Test (NIST SP 800-90B 4.4.2).
uint16_t cnt_window
Counter to count window size.
uint16_t c_prop
Cutoff threshold.
uint16_t cnt_prop
Counter to count proportion.
uint8_t old_sample
Preceding sample to compare for repetition.
Data structure for Repetition Count Test (NIST SP 800-90B 4.4.1).
uint16_t cnt_rep
Counter to count repetition.
uint8_t old_sample
Preceding sample to compare for repetition.
uint8_t c_rep
Cutoff threshold.