|
@@ -0,0 +1,90 @@
|
|
1
|
+#ifndef _IPXE_ENTROPY_H
|
|
2
|
+#define _IPXE_ENTROPY_H
|
|
3
|
+
|
|
4
|
+/** @file
|
|
5
|
+ *
|
|
6
|
+ * Entropy source
|
|
7
|
+ *
|
|
8
|
+ */
|
|
9
|
+
|
|
10
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
11
|
+
|
|
12
|
+#include <stdint.h>
|
|
13
|
+#include <assert.h>
|
|
14
|
+
|
|
15
|
+/** min-entropy per entropy sample
|
|
16
|
+ *
|
|
17
|
+ * min-entropy is defined in ANS X9.82 Part 1-2006 Section 8.3 and in
|
|
18
|
+ * NIST SP 800-90 Appendix C.3 as
|
|
19
|
+ *
|
|
20
|
+ * H_min = -log2 ( p_max )
|
|
21
|
+ *
|
|
22
|
+ * where p_max is the probability of the most likely sample value.
|
|
23
|
+ */
|
|
24
|
+#define MIN_ENTROPY_PER_SAMPLE 0.16
|
|
25
|
+
|
|
26
|
+/** Length of each entropy sample (in bits) */
|
|
27
|
+#define ENTROPY_SAMPLE_LEN_BITS 12
|
|
28
|
+
|
|
29
|
+/**
|
|
30
|
+ * Calculate entropy buffer size
|
|
31
|
+ *
|
|
32
|
+ * @v entropy_bits Amount of entropy required, in bits
|
|
33
|
+ * @v min_len Minimum buffer size, in bytes
|
|
34
|
+ * @v max_len Maximum buffer size, in bytes
|
|
35
|
+ * @ret len Buffer size, in bytes
|
|
36
|
+ */
|
|
37
|
+static inline __attribute__ (( const, always_inline )) size_t
|
|
38
|
+entropy_bufsize ( unsigned int entropy_bits, size_t min_len, size_t max_len ) {
|
|
39
|
+ unsigned int min_len_bits;
|
|
40
|
+ double min_samples;
|
|
41
|
+ double samples;
|
|
42
|
+ unsigned int samples_int;
|
|
43
|
+ unsigned int len_bits;
|
|
44
|
+ size_t len;
|
|
45
|
+
|
|
46
|
+ /* Sanity check */
|
|
47
|
+ linker_assert ( MIN_ENTROPY_PER_SAMPLE <= ENTROPY_SAMPLE_LEN_BITS,
|
|
48
|
+ min_entropy_per_sample_is_impossibly_high );
|
|
49
|
+
|
|
50
|
+ /* Calculate number of samples required to contain sufficient entropy */
|
|
51
|
+ samples = ( ( entropy_bits * 1.0 ) / MIN_ENTROPY_PER_SAMPLE );
|
|
52
|
+
|
|
53
|
+ /* Increase to minimum length if necessary */
|
|
54
|
+ min_len_bits = ( min_len * 8 );
|
|
55
|
+ min_samples = ( ( min_len_bits * 1.0 ) / ENTROPY_SAMPLE_LEN_BITS );
|
|
56
|
+ if ( samples < min_samples )
|
|
57
|
+ samples = min_samples;
|
|
58
|
+
|
|
59
|
+ /* Round up to a whole number of samples. We don't have the
|
|
60
|
+ * ceil() function available, so do the rounding by hand.
|
|
61
|
+ */
|
|
62
|
+ samples_int = samples;
|
|
63
|
+ if ( samples_int < samples )
|
|
64
|
+ samples_int++;
|
|
65
|
+ assert ( samples_int >= samples );
|
|
66
|
+
|
|
67
|
+ /* Calculate buffer length in bits */
|
|
68
|
+ len_bits = ( samples_int * ENTROPY_SAMPLE_LEN_BITS );
|
|
69
|
+
|
|
70
|
+ /* Calculate buffer length in bytes (rounding up) */
|
|
71
|
+ len = ( ( len_bits + 7 ) / 8 );
|
|
72
|
+
|
|
73
|
+ /* Check that buffer is within allowed lengths */
|
|
74
|
+ linker_assert ( len >= min_len, entropy_bufsize_too_short );
|
|
75
|
+ linker_assert ( len <= max_len, entropy_bufsize_too_long );
|
|
76
|
+
|
|
77
|
+ /* Floating-point operations are not allowed in iPXE since we
|
|
78
|
+ * never set up a suitable environment. Abort the build
|
|
79
|
+ * unless the calculated length is a compile-time constant.
|
|
80
|
+ */
|
|
81
|
+ linker_assert ( __builtin_constant_p ( len ),
|
|
82
|
+ entropy_bufsize_not_constant );
|
|
83
|
+
|
|
84
|
+ return len;
|
|
85
|
+}
|
|
86
|
+
|
|
87
|
+extern int get_entropy_input ( unsigned int entropy_bits, void *data,
|
|
88
|
+ size_t min_len, size_t max_len );
|
|
89
|
+
|
|
90
|
+#endif /* _IPXE_ENTROPY_H */
|