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 
9 #pragma once
10 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #include <stddef.h>
28 #include <inttypes.h>
29 #include <assert.h>
30 
34 typedef enum {
44 
48 typedef struct {
49  uint8_t old_sample;
50  uint16_t cnt_rep;
51  uint8_t c_rep;
53 
57 typedef struct {
58  uint8_t old_sample;
59  uint16_t cnt_prop;
60  uint16_t cnt_window;
61  uint16_t c_prop;
63 
69 #define ENTROPY_SOURCE_HMIN_SCALE(x) ((x * (1UL << 16)))
70 
75 #define ENTROPY_SOURCE_HMIN_SCALE_BACK(x) ((float)x / (1UL << 16))
76 
92 #ifndef CONFIG_ENTROPY_SOURCE_TESTS_WIN
93 #define CONFIG_ENTROPY_SOURCE_TESTS_WIN (512)
94 #endif
95 
102 #ifndef CONFIG_ENTROPY_SOURCE_NEUMANN_ABORT
103 #define CONFIG_ENTROPY_SOURCE_NEUMANN_ABORT (5)
104 #endif
119 typedef int (*entropy_source_sample_func_t)(uint8_t *sample);
120 
140  uint8_t *out, size_t len);
141 
160 static inline uint32_t entropy_source_test_rep_cutoff(uint32_t entropy_per_sample)
161 {
162  return (1 + ((20 * 65536) / entropy_per_sample));
163 }
164 
176 static inline int entropy_source_test_prop_cutoff(uint32_t entropy_per_sample)
177 {
178  int ret;
179 
180  if (entropy_per_sample < 49152UL) { /* 0.75 bit/sample */
181  ret = 410;
182  }
183  else if (entropy_per_sample < 98304UL) { /* 1.5 bit/sample */
184  ret = 311;
185  }
186  else if (entropy_per_sample < 196608UL) { /* 3 bit/sample */
187  ret = 177;
188  }
189  else if (entropy_per_sample < 393216UL) { /* 6 bit/sample */
190  ret = 62;
191  }
192  else if (entropy_per_sample <= 524288UL) { /* 8 bit/sample */
193  ret = 13;
194  }
195  else {
197  }
198 
199  return ret;
200 }
201 
209 static inline void entropy_source_test_rep_init(
210  entropy_source_tests_rep_t *state, uint16_t c_rep)
211 {
212  assert(state != NULL);
213 
214  state->old_sample = 0;
215  state->cnt_rep = 0;
216  state->c_rep = c_rep;
217 }
218 
226 static inline void entropy_source_test_prop_init(
227  entropy_source_tests_prop_t *state, uint16_t c_prop)
228 {
229  assert(state != NULL);
230 
231  state->old_sample = 0;
232  state->cnt_prop = 0;
234  state->c_prop = c_prop;
235 }
236 
249 
262  uint8_t sample);
263 
279 static inline int entropy_source_test(entropy_source_tests_rep_t *state_rep,
280  entropy_source_tests_prop_t *state_prop,
281  uint8_t sample)
282 {
283  int ret = ENTROPY_SOURCE_OK;
284 
285  if (entropy_source_test_rep(state_rep, sample) < 0) {
287  }
288  if (entropy_source_test_prop(state_prop, sample) < 0) {
289  /* If repetition count failed before, indicate that both tests failed */
290  if (ret == ENTROPY_SOURCE_ERR_TEST_REP) {
292  }
293  else {
295  }
296  }
297  return ret;
298 }
299 
300 #ifdef __cplusplus
301 }
302 #endif
303 
POSIX.1-2008 compliant version of the assert macro.
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:135
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.