entropy_source.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2020 HAW Hamburg
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include <stddef.h>
25 #include <inttypes.h>
26 #include <assert.h>
27 
31 typedef enum {
41 
45 typedef struct {
46  uint8_t old_sample;
47  uint16_t cnt_rep;
48  uint8_t c_rep;
50 
54 typedef struct {
55  uint8_t old_sample;
56  uint16_t cnt_prop;
57  uint16_t cnt_window;
58  uint16_t c_prop;
60 
66 #define ENTROPY_SOURCE_HMIN_SCALE(x) ((x * (1UL << 16)))
67 
72 #define ENTROPY_SOURCE_HMIN_SCALE_BACK(x) ((float)x / (1UL << 16))
73 
89 #ifndef CONFIG_ENTROPY_SOURCE_TESTS_WIN
90 #define CONFIG_ENTROPY_SOURCE_TESTS_WIN (512)
91 #endif
92 
99 #ifndef CONFIG_ENTROPY_SOURCE_NEUMANN_ABORT
100 #define CONFIG_ENTROPY_SOURCE_NEUMANN_ABORT (5)
101 #endif
116 typedef int (*entropy_source_sample_func_t)(uint8_t *sample);
117 
137  uint8_t *out, size_t len);
138 
157 static inline uint32_t entropy_source_test_rep_cutoff(uint32_t entropy_per_sample)
158 {
159  return (1 + ((20 * 65536) / entropy_per_sample));
160 }
161 
173 static inline int entropy_source_test_prop_cutoff(uint32_t entropy_per_sample)
174 {
175  int ret;
176 
177  if (entropy_per_sample < 49152UL) { /* 0.75 bit/sample */
178  ret = 410;
179  }
180  else if (entropy_per_sample < 98304UL) { /* 1.5 bit/sample */
181  ret = 311;
182  }
183  else if (entropy_per_sample < 196608UL) { /* 3 bit/sample */
184  ret = 177;
185  }
186  else if (entropy_per_sample < 393216UL) { /* 6 bit/sample */
187  ret = 62;
188  }
189  else if (entropy_per_sample <= 524288UL) { /* 8 bit/sample */
190  ret = 13;
191  }
192  else {
194  }
195 
196  return ret;
197 }
198 
206 static inline void entropy_source_test_rep_init(
207  entropy_source_tests_rep_t *state, uint16_t c_rep)
208 {
209  assert(state != NULL);
210 
211  state->old_sample = 0;
212  state->cnt_rep = 0;
213  state->c_rep = c_rep;
214 }
215 
223 static inline void entropy_source_test_prop_init(
224  entropy_source_tests_prop_t *state, uint16_t c_prop)
225 {
226  assert(state != NULL);
227 
228  state->old_sample = 0;
229  state->cnt_prop = 0;
231  state->c_prop = c_prop;
232 }
233 
246 
259  uint8_t sample);
260 
276 static inline int entropy_source_test(entropy_source_tests_rep_t *state_rep,
277  entropy_source_tests_prop_t *state_prop,
278  uint8_t sample)
279 {
280  int ret = ENTROPY_SOURCE_OK;
281 
282  if (entropy_source_test_rep(state_rep, sample) < 0) {
284  }
285  if (entropy_source_test_prop(state_prop, sample) < 0) {
286  /* If repetition count failed before, indicate that both tests failed */
287  if (ret == ENTROPY_SOURCE_ERR_TEST_REP) {
289  }
290  else {
292  }
293  }
294  return ret;
295 }
296 
297 #ifdef __cplusplus
298 }
299 #endif
300 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
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.