Software APIs
randomness_quality.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_RANDOMNESS_QUALITY_H_
6 #define OPENTITAN_SW_DEVICE_LIB_TESTING_RANDOMNESS_QUALITY_H_
7 
8 #include <stdint.h>
9 
10 #include "sw/device/lib/base/status.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif // __cplusplus
15 
16 /**
17  * Test statistical significance setting.
18  *
19  * The statistical tests in this file will always fail with some probability,
20  * even on truly random data. Use these settings to toggle the threshold
21  * values. In the Handbook of Applied Cryptography, this setting is denoted
22  * with the Greek letter alpha.
23  */
24 typedef enum randomness_quality_significance {
25  /**
26  * Set thresholds for a 5% failure rate on truly random data (alpha = 0.05).
27  */
28  kRandomnessQualitySignificanceFivePercent,
29  /**
30  * Set thresholds for a 1% failure rate on truly random data (alpha = 0.01).
31  */
32  kRandomnessQualitySignificanceOnePercent,
33 } randomness_quality_significance_t;
34 
35 /**
36  * Monobit test from section 5.4.4 of the Handbook of Applied Cryptography.
37  *
38  * This test counts the number of zero and one bits in the data and expects the
39  * number to be roughly similar.
40  *
41  * @param data Random data to check.
42  * @param len Length of data.
43  * @param significance Test statistical significance setting.
44  * @return OK if the test passes, a failure code otherwise.
45  */
46 status_t randomness_quality_monobit_test(
47  uint8_t *data, size_t len, randomness_quality_significance_t significance);
48 
49 #ifdef __cplusplus
50 } // extern "C"
51 #endif // __cplusplus
52 
53 #endif // OPENTITAN_SW_DEVICE_LIB_TESTING_RANDOMNESS_QUALITY_H_