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"cxx . . $ `<file>/lib/jitterentropy-gcd.h`/
23 #include "jitterentropy-internal.h"cxx . . $ `<file>/lib/jitterentropy-internal.h`/
24
25 /* The common divisor for all timestamp deltas */
26 static uint64_tcxx . . $ uint64_t# jent_common_timer_gcdcxx . . $ jent_common_timer_gcd. = 0;
27
28 static inline int jent_gcd_testedcxx . . $ jent_gcd_tested(b126dc7c1de90089).(void)
29 {
30 return (jent_common_timer_gcdcxx . . $ jent_common_timer_gcd. != 0);
31 }
32
33 /* A straight forward implementation of the Euclidean algorithm for GCD. */
34 static inline uint64_tcxx . . $ uint64_t# jent_gcd64cxx . . $ jent_gcd64(80bad76eed2d2579).(uint64_tcxx . . $ uint64_t# a, uint64_tcxx . . $ uint64_t# b)
35 {
36 /* Make a greater a than or equal b. */
37 if (a < b) {
38 uint64_tcxx . . $ 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_tcxx . . $ 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_internalcxx . . $ jent_gcd_analyze_internal(24b8a76e78df791c).(uint64_tcxx . . $ uint64_t# *delta_history, size_t nelem,
58 uint64_tcxx . . $ uint64_t# *running_gcd_out,
59 uint64_tcxx . . $ uint64_t# *delta_sum_out)
60 {
61 uint64_tcxx . . $ uint64_t# running_gcd, delta_sum = 0;
62 size_t i;
63
64 if (!delta_history)
65 return -EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!;
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_gcd64cxx . . $ jent_gcd64(80bad76eed2d2579).(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_analyzecxx . . $ jent_gcd_analyze(4b2cf5631264baec).(uint64_tcxx . . $ uint64_t# *delta_history, size_t nelem, size_t osr)
100 {
101 uint64_tcxx . . $ uint64_t# running_gcd, delta_sum;
102 int ret = jent_gcd_analyze_internalcxx . . $ jent_gcd_analyze_internal(24b8a76e78df791c).(delta_history, nelem, &running_gcd,
103 &delta_sum);
104
105 if (ret == -EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!)
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 = EMINVARVARcxx . . $ `jitterentropy.h:276:9`!;
116 goto out;
117 }
118
119 /* Set a sensible maximum value. */
120 if (running_gcd >= UINT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:124:10`! / 2) {
121 ret = ECOARSETIMEcxx . . $ `jitterentropy.h:271:9`!;
122 goto out;
123 }
124
125 /* Adjust all deltas by the observed (small) common factor. */
126 if (!jent_gcd_testedcxx . . $ jent_gcd_tested(b126dc7c1de90089).())
127 jent_common_timer_gcdcxx . . $ jent_common_timer_gcd. = running_gcd;
128
129 out:
130 return ret;
131 }
132
133 uint64_tcxx . . $ uint64_t# *jent_gcd_initcxx . . $ jent_gcd_init(9087c41a63d875fd).(size_t nelem)
134 {
135 uint64_tcxx . . $ uint64_t# *delta_history;
136
137 delta_history = jent_zalloccxx . . $ jent_zalloc(bfcfa4d6b7f7ef64).(nelem * sizeof(uint64_tcxx . . $ uint64_t#));
138 if (!delta_history)
139 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
140
141 return delta_history;
142 }
143
144 void jent_gcd_finicxx . . $ jent_gcd_fini(18fbe3e454f5ef99).(uint64_tcxx . . $ uint64_t# *delta_history, size_t nelem)
145 {
146 if (delta_history)
147 jent_zfreecxx . . $ jent_zfree(5500c4293b1bbef0).(delta_history, nelem * sizeof(uint64_tcxx . . $ uint64_t#));
148 }
149
150 int jent_gcd_getcxx . . $ jent_gcd_get(b8664ca4beff78c9).(uint64_tcxx . . $ uint64_t# *value)
151 {
152 if (!jent_gcd_testedcxx . . $ jent_gcd_tested(b126dc7c1de90089).())
153 return 1;
154
155 *value = jent_common_timer_gcdcxx . . $ jent_common_timer_gcd.;
156 return 0;
157 }
158
159 int jent_gcd_selftestcxx . . $ jent_gcd_selftest(b126dc7c1de90089).(void)
160 {
161 #define JENT_GCD_SELFTEST_ELEMcxx . . $ `lib/jitterentropy-gcd.c:161:9`! 10
162 #define JENT_GCD_SELFTEST_EXPcxx . . $ `lib/jitterentropy-gcd.c:162:9`! 3ULL
163 uint64_tcxx . . $ uint64_t# *gcd = jent_gcd_initcxx . . $ jent_gcd_init(9087c41a63d875fd).(JENT_GCD_SELFTEST_ELEMcxx . . $ `lib/jitterentropy-gcd.c:161:9`!);
164 uint64_tcxx . . $ uint64_t# running_gcd, delta_sum;
165 unsigned int i;
166 int ret = EGCDcxx . . $ `jitterentropy.h:283:9`!;
167
168 if (!gcd)
169 return EMEMcxx . . $ `jitterentropy.h:282:9`!;
170
171 for (i = 0; i < JENT_GCD_SELFTEST_ELEMcxx . . $ `lib/jitterentropy-gcd.c:161:9`!; i++)
172 jent_gcd_add_valuecxx . . $ `lib/jitterentropy-gcd.h:42:9`!(gcd, i * JENT_GCD_SELFTEST_EXPcxx . . $ `lib/jitterentropy-gcd.c:162:9`!, i);
173
174 if (jent_gcd_analyze_internalcxx . . $ jent_gcd_analyze_internal(24b8a76e78df791c).(gcd, JENT_GCD_SELFTEST_ELEMcxx . . $ `lib/jitterentropy-gcd.c:161:9`!,
175 &running_gcd, &delta_sum))
176 goto out;
177
178 if (running_gcd != JENT_GCD_SELFTEST_EXPcxx . . $ `lib/jitterentropy-gcd.c:162:9`!)
179 goto out;
180
181 ret = 0;
182
183 out:
184 jent_gcd_finicxx . . $ jent_gcd_fini(18fbe3e454f5ef99).(gcd, JENT_GCD_SELFTEST_ELEMcxx . . $ `lib/jitterentropy-gcd.c:161:9`!);
185 return ret;
186 }