Debian Codegraph @

lib/jitterentropy-gcd.c

raw symbols SCIP
1 /* Jitter RNG: GCD health test
2 *
3 * Copyright (C) 2021 - 2026, Joshua E. Hill <[email protected]>
4 * Copyright (C) 2021 - 2026, Stephan Mueller <[email protected]>
5 *
6 * License: see LICENSE file in root directory
7 *
8 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
9 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
11 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
12 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
13 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
14 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
15 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
16 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
18 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
19 * DAMAGE.
20 */
21
22 #include "jitterentropy-gcd.h"
23 #include "jitterentropy-internal.h"
24
25 /* The common divisor for all timestamp deltas */
26 static uint64_t jent_common_timer_gcd = 0;
27
28 static inline int jent_gcd_tested(void)
29 {
30 return (jent_common_timer_gcd != 0);
31 }
32
33 /* A straight forward implementation of the Euclidean algorithm for GCD. */
34 static inline uint64_t jent_gcd64(uint64_t a, uint64_t b)
35 {
36 /* Make a greater a than or equal b. */
37 if (a < b) {
38 uint64_t c = a;
39
40 a = b;
41 b = c;
42 }
43
44 /* Now perform the standard inner-loop for this algorithm.*/
45 while (b != 0) {
46 uint64_t r;
47
48 r = a % b;
49
50 a = b;
51 b = r;
52 }
53
54 return a;
55 }
56
57 static int jent_gcd_analyze_internal(uint64_t *delta_history, size_t nelem,
58 uint64_t *running_gcd_out,
59 uint64_t *delta_sum_out)
60 {
61 uint64_t running_gcd, delta_sum = 0;
62 size_t i;
63
64 if (!delta_history)
65 return -EAGAIN;
66
67 running_gcd = delta_history[0];
68
69 /* Now perform the analysis on the accumulated delta data. */
70 for (i = 1; i < nelem; i++) {
71 /*
72 * ensure that we have a varying delta timer which is necessary
73 * for the calculation of entropy -- perform this check
74 * only after the first loop is executed as we need to prime
75 * the old_data value
76 */
77 if (delta_history[i] >= delta_history[i - 1])
78 delta_sum += delta_history[i] - delta_history[i - 1];
79 else
80 delta_sum += delta_history[i - 1] - delta_history[i];
81
82 /*
83 * This calculates the gcd of all the delta values. that is
84 * gcd(delta_1, delta_2, ..., delta_nelem)
85
86 * Some timers increment by a fixed (non-1) amount each step.
87 * This code checks for such increments, and allows the library
88 * to output the number of such changes have occurred.
89 */
90 running_gcd = jent_gcd64(delta_history[i], running_gcd);
91 }
92
93 *running_gcd_out = running_gcd;
94 *delta_sum_out = delta_sum;
95
96 return 0;
97 }
98
99 int jent_gcd_analyze(uint64_t *delta_history, size_t nelem, size_t osr)
100 {
101 uint64_t running_gcd, delta_sum;
102 int ret = jent_gcd_analyze_internal(delta_history, nelem, &running_gcd,
103 &delta_sum);
104
105 if (ret == -EAGAIN)
106 return 0;
107
108 /*
109 * We assume 1/osr bits of entropy per sample. On average, variations
110 * of deltas must be larger than 1 over osr cases; we do not capture
111 * fractions. Hence delta_sum < (nelem / osr) means we cannot satisfy the
112 * 1/osr bits of entropy per sample assumption.
113 */
114 if ((delta_sum * osr) < nelem) {
115 ret = EMINVARVAR;
116 goto out;
117 }
118
119 /* Set a sensible maximum value. */
120 if (running_gcd >= UINT32_MAX / 2) {
121 ret = ECOARSETIME;
122 goto out;
123 }
124
125 /* Adjust all deltas by the observed (small) common factor. */
126 if (!jent_gcd_tested())
127 jent_common_timer_gcd = running_gcd;
128
129 out:
130 return ret;
131 }
132
133 uint64_t *jent_gcd_init(size_t nelem)
134 {
135 uint64_t *delta_history;
136
137 delta_history = jent_zalloc(nelem * sizeof(uint64_t));
138 if (!delta_history)
139 return NULL;
140
141 return delta_history;
142 }
143
144 void jent_gcd_fini(uint64_t *delta_history, size_t nelem)
145 {
146 if (delta_history)
147 jent_zfree(delta_history, nelem * sizeof(uint64_t));
148 }
149
150 int jent_gcd_get(uint64_t *value)
151 {
152 if (!jent_gcd_tested())
153 return 1;
154
155 *value = jent_common_timer_gcd;
156 return 0;
157 }
158
159 int jent_gcd_selftest(void)
160 {
161 #define JENT_GCD_SELFTEST_ELEM 10
162 #define JENT_GCD_SELFTEST_EXP 3ULL
163 uint64_t *gcd = jent_gcd_init(JENT_GCD_SELFTEST_ELEM);
164 uint64_t running_gcd, delta_sum;
165 unsigned int i;
166 int ret = EGCD;
167
168 if (!gcd)
169 return EMEM;
170
171 for (i = 0; i < JENT_GCD_SELFTEST_ELEM; i++)
172 jent_gcd_add_value(gcd, i * JENT_GCD_SELFTEST_EXP, i);
173
174 if (jent_gcd_analyze_internal(gcd, JENT_GCD_SELFTEST_ELEM,
175 &running_gcd, &delta_sum))
176 goto out;
177
178 if (running_gcd != JENT_GCD_SELFTEST_EXP)
179 goto out;
180
181 ret = 0;
182
183 out:
184 jent_gcd_fini(gcd, JENT_GCD_SELFTEST_ELEM);
185 return ret;
186 }