Debian Codegraph @

jitterentropy-rngd.c

raw symbols SCIP
1 /*
2 * Non-physical true random number generator based on timing jitter.
3 *
4 * Copyright Stephan Mueller <[email protected]>, 2014 - 2026
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, and the entire permission notice in its entirety,
11 * including the disclaimer of warranties.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior
17 * written permission.
18 *
19 * ALTERNATIVELY, this product may be distributed under the terms of
20 * the GNU General Public License, in which case the provisions of the GPL are
21 * required INSTEAD OF the above restrictions. (This clause is
22 * necessary due to a potential bad interaction between the GPL and
23 * the restrictions contained in a BSD-style copyright.)
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
28 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
31 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
36 * DAMAGE.
37 */
38
39 #include <unistd.h>
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <sys/types.h>
43 #include <asm/types.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <sys/ioctl.h>
49 #include <sys/utsname.h>
50 #define _GNU_SOURCEcxx . . $ `jitterentropy-rngd.c:50:9`!
51 #include <getopt.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include <linux/random.h>
55 #include <linux/version.h>
56 #include <signal.h>
57
58 #include "jitterentropy.h"cxx . . $ `<file>/jitterentropy.h`/
59
60 #define MAJVERSIONcxx . . $ `jitterentropy-rngd.c:60:9`! 1 /* API / ABI incompatible changes, functional changes that
61 * require consumer to be updated (as long as this number
62 * is zero, the API is not considered stable and can
63 * change without a bump of the major version) */
64 #define MINVERSIONcxx . . $ `jitterentropy-rngd.c:64:9`! 3 /* API compatible, ABI may change, functional
65 * enhancements only, consumer can be left unchanged if
66 * enhancements are not considered */
67 #define PATCHLEVELcxx . . $ `jitterentropy-rngd.c:67:9`! 2 /* API / ABI compatible, no functional changes, no
68 * enhancements, bug fixes only */
69
70 static int Verbositycxx . . $ Verbosity. = 0;
71 static int force_sp80090bcxx . . $ force_sp80090b. = 0;
72 static int statuscxx . . $ status. = 0;
73
74 /*
75 * When set, the daemon will exit on any error it encounters. The goal is that
76 * a wrapping monitor will pick up the errors and handle it as it sees fit, such
77 * as creating audit logs and possibly restart the daemon.
78 *
79 * The following errors are returned:
80 *
81 * * EOPNOTSUPP - the Jitter RNG triggered a fatal health test error
82 * * Errors reported by IOCTLs of RNDADDENTROPY and RNDRESEEDCRNG to /dev/random
83 * * Errors triggered by system calls including select(2), open(2), truncate(2),
84 * write(2), fork(2), as well as errors from library functions including
85 * lockf(3).
86 */
87 static int exit_on_errorcxx . . $ exit_on_error. = 0;
88
89 struct kernel_rngcxx . . $ kernel_rng# {
90 int fdcxx . . $ kernel_rng#fd.;
91 struct rand_datacxx . . $ rand_data# *eccxx . . $ kernel_rng#ec.;
92 struct rand_pool_infocxx . . $ rand_pool_info# *rpicxx . . $ kernel_rng#rpi.;
93 const char *devcxx . . $ kernel_rng#dev.;
94 };
95
96 static struct kernel_rngcxx . . $ kernel_rng# Randomcxx . . $ Random. = {
97 /*.fd = */ -1,
98 /*.ec = */ NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
99 /*.rpi = */ NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!,
100 /*.dev = */ "/dev/random"
101 };
102
103 /*
104 * handler for /dev/urandom not needed as used IOCTL alters input_pool
105 static struct kernel_rng Urandom = {
106 .fd = 0,
107 .ec = NULL,
108 .rpi = NULL,
109 .dev = "/dev/urandom"
110 };
111 */
112
113 static int Pidfile_fdcxx . . $ Pidfile_fd. = -1;
114 /* "/var/run/jitterentropy-rngd.pid" */
115 static char *Pidfilecxx . . $ Pidfile. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
116
117 static int Entropy_avail_fdcxx . . $ Entropy_avail_fd. = -1;
118 static int Entropy_thresh_fdcxx . . $ Entropy_thresh_fd. = -1;
119 static unsigned int jent_flagscxx . . $ jent_flags. = 0;
120 static unsigned int jent_osrcxx . . $ jent_osr. = 1;
121
122 #define ENTROPYBYTEScxx . . $ `jitterentropy-rngd.c:122:9`! 32
123 #define OVERSAMPLINGFACTORcxx . . $ `jitterentropy-rngd.c:123:9`! 2
124 /*
125 * After (force reseed wakeups), the installed alarm handler will unconditionally
126 * trigger a reseed irrespective of the seed level in two phases. This ensures
127 * that new seed is added after every (force reseed wakeups) * (alarm period).
128 * PHASE1: 120(force reseed wakeups) * 5(alarm period) == 600s
129 * PHASE2: 12(force reseed wakeups) * 50(alarm period) == 600s
130 */
131 #define FORCE_RESEED_WAKEUPS_PHASE1cxx . . $ `jitterentropy-rngd.c:131:9`! 120
132 #define ALARM_PERIOD_PHASE1cxx . . $ `jitterentropy-rngd.c:132:9`! 5
133 #define FORCE_RESEED_WAKEUPS_PHASE2cxx . . $ `jitterentropy-rngd.c:133:9`! 12
134 #define ALARM_PERIOD_PHASE2cxx . . $ `jitterentropy-rngd.c:134:9`! 50
135 #define ENTROPYAVAILcxx . . $ `jitterentropy-rngd.c:135:9`! "/proc/sys/kernel/random/entropy_avail"
136 #define ENTROPYTHRESHcxx . . $ `jitterentropy-rngd.c:136:9`! "/proc/sys/kernel/random/write_wakeup_threshold"
137 #define LRNG_FILEcxx . . $ `jitterentropy-rngd.c:137:9`! "/proc/lrng_type"
138
139 static void install_alarmcxx . . $ install_alarm(cb2f890c2fdad230).(unsigned int secs);
140 static void dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).(void);
141 static int alloccxx . . $ alloc(b126dc7c1de90089).(void);
142 static void dealloc_rngcxx . . $ dealloc_rng(ed9240bb51f9de62).(struct kernel_rngcxx . . $ kernel_rng# *rng);
143
144 static unsigned long kern_majcxx . . $ kern_maj. = ULONG_MAXcxx . . $ `<external>/2419331716647297546/limits.h:135:9`!, kern_minorcxx . . $ kern_minor., kern_patchlevelcxx . . $ kern_patchlevel.;
145
146 static void jentrng_versionstringcxx . . $ jentrng_versionstring(a14cc78460091691).(char *buf, size_t buflen)
147 {
148 snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, buflen, "jitterentropy-rngd %d.%d.%d",
149 MAJVERSIONcxx . . $ `jitterentropy-rngd.c:60:9`!, MINVERSIONcxx . . $ `jitterentropy-rngd.c:64:9`!, PATCHLEVELcxx . . $ `jitterentropy-rngd.c:67:9`!);
150 }
151
152 /* Is the LRNG present instead of the legacy /dev/random? */
153 static int lrng_presentcxx . . $ lrng_present(b126dc7c1de90089).(void)
154 {
155 struct statcxx . . $ stat# buf;
156 static int lrng_present = -1;
157
158 if (lrng_present < 0) {
159 int ret = statcxx . . $ stat(a9cd1e23e96f423c).(LRNG_FILEcxx . . $ `jitterentropy-rngd.c:137:9`!, &buf);
160
161 if (ret == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!)
162 lrng_present = 0;
163 else
164 lrng_present = 1;
165 }
166
167 return lrng_present;
168 }
169
170 static int get_kernvercxx . . $ get_kernver(b126dc7c1de90089).(void)
171 {
172 struct utsnamecxx . . $ utsname# kernel;
173 char *saveptr = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
174 char *res = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
175
176 if (kern_majcxx . . $ kern_maj. != ULONG_MAXcxx . . $ `<external>/2419331716647297546/limits.h:135:9`!)
177 return 0;
178
179 if (unamecxx . . $ uname(c69a16225c55e184).(&kernel))
180 return -errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
181
182 /* 5.11.2 */
183 res = strtok_rcxx . . $ strtok_r(a42394c85d6397dd).(kernel.releasecxx . . $ utsname#release., ".", &saveptr);
184 if (!res) {
185 printfcxx . . $ printf(581fddd72a8db8fc).("Could not parse kernel version");
186 return -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
187 }
188 kern_majcxx . . $ kern_maj. = strtoulcxx . . $ strtoul(31746e8d6ec49860).(res, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 10);
189
190 res = strtok_rcxx . . $ strtok_r(a42394c85d6397dd).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, ".", &saveptr);
191 if (!res) {
192 printfcxx . . $ printf(581fddd72a8db8fc).("Could not parse kernel version");
193 return -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
194 }
195 kern_minorcxx . . $ kern_minor. = strtoulcxx . . $ strtoul(31746e8d6ec49860).(res, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 10);
196
197 res = strtok_rcxx . . $ strtok_r(a42394c85d6397dd).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, ".", &saveptr);
198 if (!res) {
199 printfcxx . . $ printf(581fddd72a8db8fc).("Could not parse kernel version");
200 return -EFAULTcxx . . $ `<external>/8987895939074593727/errno-base.h:18:9`!;
201 }
202 kern_patchlevelcxx . . $ kern_patchlevel. = strtoulcxx . . $ strtoul(31746e8d6ec49860).(res, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 10);
203
204 return 0;
205 }
206
207 /* return true if kernel is greater or equal to given values, otherwise false */
208 static int kernver_gecxx . . $ kernver_ge(64d4c609941711dd).(unsigned int maj, unsigned int minor,
209 unsigned int patchlevel)
210 {
211 if (get_kernvercxx . . $ get_kernver(b126dc7c1de90089).())
212 return 0;
213
214 if (maj < kern_majcxx . . $ kern_maj.)
215 return 1;
216 if (maj == kern_majcxx . . $ kern_maj.) {
217 if (minor < kern_minorcxx . . $ kern_minor.)
218 return 1;
219 if (minor == kern_minorcxx . . $ kern_minor.) {
220 if (patchlevel <= kern_patchlevelcxx . . $ kern_patchlevel.)
221 return 1;
222 }
223 }
224 return 0;
225 }
226
227 static void usagecxx . . $ usage(49f6e7a06ebc5aa8).(void)
228 {
229 unsigned int ver = jent_versioncxx . . $ jent_version(db52760e79bf9054).();
230 char version[30];
231
232 memsetcxx . . $ memset(d1de1e4fb215f1c6).(version, 0, 30);
233 jentrng_versionstringcxx . . $ jentrng_versionstring(a14cc78460091691).(version, sizeof(version));
234
235 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\njitterentropy rngd feeding entropy to input_pool of Linux RNG\n");
236 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Version %s\n\n", version);
237 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Reported numeric version number of jent library %u\n\n", ver);
238 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Usage:\n");
239 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t-h --help\tThis help information\n");
240 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t --version\tPrint version\n");
241 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t-v --verbose\tVerbose logging, multiple options increase verbosity\n");
242 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t\t\tVerbose logging implies running in foreground\n");
243 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t-p --pid\tWrite daemon PID to file\n");
244 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t-s --sp800-90b\tForce SP800-90B compliance\n");
245 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t-f --flags\tInteger with flags used to allocate Jitter RNG\n");
246 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t-o --osr\tInteger with OSR used to allocate Jitter RNG\n");
247 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t --status\tStatus information of the Jitter RNG - invoke with\n");
248 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t \tsame flags as used for runtime\n");
249 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t --exit-on-error\tCause the daemon to exit on errors\n");
250 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\nLRNG presence %sdetected\n",
251 lrng_presentcxx . . $ lrng_present(b126dc7c1de90089).() ? "" : "not ");
252 exitcxx . . $ exit(3ca292c338df9bb3).(1);
253 }
254
255 static void parse_optscxx . . $ parse_opts(c759356ac01a31e7).(int argc, char *argv[])
256 {
257 int c = 0;
258 char version[30];
259
260 while (1) {
261 int opt_index = 0;
262 static struct optioncxx . . $ option# opts[] = {
263 {"verbose", 0, 0, 0},
264 {"pid", 1, 0, 0},
265 {"help", 0, 0, 0},
266 {"version", 0, 0, 0},
267 {"sp800-90b", 0, 0, 0},
268 {"flags", 1, 0, 0},
269 {"osr", 1, 0, 0},
270 {"status", 0, 0, 0},
271 {"exit-on-error", 0, 0, 0},
272 {0, 0, 0, 0}
273 };
274 c = getopt_longcxx . . $ getopt_long(41f665c090816971).(argc, argv, "svp:hf:o:", opts, &opt_index);
275 if (-1 == c)
276 break;
277 switch (c) {
278 case 0:
279 switch (opt_index) {
280
281 /* verbose */
282 case 0:
283 Verbositycxx . . $ Verbosity.++;
284 break;
285
286 /* pid */
287 case 1:
288 Pidfilecxx . . $ Pidfile. = optargcxx . . $ optarg.;
289 break;
290
291 /* help */
292 case 2:
293 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
294 break;
295
296 /* version */
297 case 3:
298 jentrng_versionstringcxx . . $ jentrng_versionstring(a14cc78460091691).(version, sizeof(version));
299 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Version %s\n", version);
300 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Version Jitterentropy Core %u\n", jent_versioncxx . . $ jent_version(db52760e79bf9054).());
301 exitcxx . . $ exit(3ca292c338df9bb3).(0);
302 break;
303
304 /* sp800-90b */
305 case 4:
306 force_sp80090bcxx . . $ force_sp80090b. = 1;
307 break;
308
309 /* flags */
310 case 5:
311 {
312 unsigned long val = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 10);
313
314 if (val > UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!)
315 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
316 jent_flagscxx . . $ jent_flags. = (unsigned int)val;
317
318 break;
319 }
320
321 /* osr */
322 case 6:
323 {
324 unsigned long val = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 10);
325
326 if (val > UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!)
327 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
328 jent_osrcxx . . $ jent_osr. = (unsigned int)val;
329
330 break;
331 }
332
333 /* status */
334 case 7:
335 statuscxx . . $ status. = 1;
336 break;
337
338 /* exit-on-error */
339 case 8:
340 exit_on_errorcxx . . $ exit_on_error. = 1;
341 break;
342
343 default:
344 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
345 }
346 break;
347 case 'v':
348 Verbositycxx . . $ Verbosity.++;
349 break;
350 case 'p':
351 Pidfilecxx . . $ Pidfile. = optargcxx . . $ optarg.;
352 break;
353 case 'h':
354 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
355 break;
356 case 's':
357 force_sp80090bcxx . . $ force_sp80090b. = 1;
358 break;
359 case 'f':
360 {
361 unsigned long val = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 10);
362
363 if (val > UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!)
364 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
365 jent_flagscxx . . $ jent_flags. = (unsigned int)val;
366
367 break;
368 }
369 case 'o':
370 {
371 unsigned long val = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 10);
372
373 if (val > UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`!)
374 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
375 jent_osrcxx . . $ jent_osr. = (unsigned int)val;
376
377 break;
378 }
379 default:
380 usagecxx . . $ usage(49f6e7a06ebc5aa8).();
381 }
382 }
383 }
384
385 #define LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`! 3
386 #define LOG_VERBOSEcxx . . $ `jitterentropy-rngd.c:386:9`! 2
387 #define LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`! 1
388 #define LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`! 0
389 static void dologcxx . . $ dolog(afcfcee7634adf7d).(int severity, const char *fmt, ...)
390 {
391 va_listcxx . . $ va_list# args;
392 char msg[1024];
393 char sev[10];
394
395 if (severity <= Verbositycxx . . $ Verbosity.) {
396 va_startcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stdarg.h:51:9`!(args, fmt);
397 vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(msg, sizeof(msg), fmt, args);
398 va_endcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stdarg.h:53:9`!(args);
399
400 switch (severity) {
401 case LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!:
402 snprintfcxx . . $ snprintf(ea502d73e74c754c).(sev, sizeof(sev), "Debug");
403 break;
404 case LOG_VERBOSEcxx . . $ `jitterentropy-rngd.c:386:9`!:
405 snprintfcxx . . $ snprintf(ea502d73e74c754c).(sev, sizeof(sev), "Verbose");
406 break;
407 case LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`!:
408 snprintfcxx . . $ snprintf(ea502d73e74c754c).(sev, sizeof(sev), "Warning");
409 break;
410 case LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!:
411 snprintfcxx . . $ snprintf(ea502d73e74c754c).(sev, sizeof(sev), "Error");
412 break;
413 default:
414 snprintfcxx . . $ snprintf(ea502d73e74c754c).(sev, sizeof(sev), "Unknown");
415 }
416 printfcxx . . $ printf(581fddd72a8db8fc).("jitterentropy-rngd - %s: %s\n", sev, msg);
417 }
418
419 if (LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`! == severity) {
420 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
421 exitcxx . . $ exit(3ca292c338df9bb3).(1);
422 }
423 }
424
425 static inline void memset_securecxx . . $ memset_secure(e37d57925061d5c9).(void *s, int c, size_t n)
426 {
427 memsetcxx . . $ memset(d1de1e4fb215f1c6).(s, c, n);
428 __asm__ __volatile__("" : : "r" (s) : "memory");
429 }
430
431 /*******************************************************************
432 * entropy handler functions
433 *******************************************************************/
434
435 static ssize_tcxx . . $ ssize_t# write_randomcxx . . $ write_random(b984c3db3bd714de).(struct kernel_rngcxx . . $ kernel_rng# *rng, char *buf, size_t len,
436 size_t entropy_bytes, int force_reseed)
437 {
438 ssize_tcxx . . $ ssize_t# written = 0;
439 int ret;
440
441 if (len > SSIZE_MAXcxx . . $ `<external>/2534844627595019613/posix1_lim.h:169:11`!)
442 return -EOVERFLOWcxx . . $ `<external>/17922322188510340118/errno.h:59:9`!;
443
444 /* value is in bits */
445 rng->rpicxx . . $ kernel_rng#rpi.->entropy_countcxx . . $ rand_pool_info#entropy_count. = (entropy_bytes * 8);
446 rng->rpicxx . . $ kernel_rng#rpi.->buf_sizecxx . . $ rand_pool_info#buf_size. = len;
447 memcpycxx . . $ memcpy(cc817cb175f282d).(rng->rpicxx . . $ kernel_rng#rpi.->bufcxx . . $ rand_pool_info#buf., buf, len);
448
449 ret = ioctlcxx . . $ ioctl(67fa1c773b05068c).(rng->fdcxx . . $ kernel_rng#fd., RNDADDENTROPYcxx . . $ `<external>/10659480645472688339/random.h:30:9`!, rng->rpicxx . . $ kernel_rng#rpi.);
450 if (0 > ret) {
451 int errsv = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
452
453 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`!, "Error injecting entropy: %s", strerrorcxx . . $ strerror(ff224f080444ee45).(errsv));
454 return -errsv;
455 } else {
456 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Injected %u bytes with an entropy count of %u bytes of entropy",
457 len, entropy_bytes);
458 written = len;
459 }
460
461 rng->rpicxx . . $ kernel_rng#rpi.->entropy_countcxx . . $ rand_pool_info#entropy_count. = 0;
462 rng->rpicxx . . $ kernel_rng#rpi.->buf_sizecxx . . $ rand_pool_info#buf_size. = 0;
463 memsetcxx . . $ memset(d1de1e4fb215f1c6).(rng->rpicxx . . $ kernel_rng#rpi.->bufcxx . . $ rand_pool_info#buf., 0, len);
464
465 #if LINUX_VERSION_CODEcxx . . $ `<external>/6429733429949359714/version.h:1:9`! >= KERNEL_VERSIONcxx . . $ `<external>/6429733429949359714/version.h:2:9`!(4,17,0)
466 /*
467 * The LRNG does not require this IOCTL as the reseed is automatically
468 * triggered.
469 */
470 if (force_reseed && kernver_gecxx . . $ kernver_ge(64d4c609941711dd).(4, 17, 0) && !lrng_presentcxx . . $ lrng_present(b126dc7c1de90089).()) {
471 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(rng->fdcxx . . $ kernel_rng#fd., RNDRESEEDCRNGcxx . . $ `<external>/10659480645472688339/random.h:39:9`!) < 0) {
472 written = -errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
473 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!)
474 goto out;
475 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`!,
476 "Error triggering a reseed of the kernel DRNG: %s",
477 strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
478 } else {
479 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Reseeding of kernel DRNG triggered");
480 }
481 }
482 #endif
483
484 out:
485 return written;
486 }
487
488 static ssize_tcxx . . $ ssize_t# read_jentcxx . . $ read_jent(e59ca0fcea0ad1d9).(struct kernel_rngcxx . . $ kernel_rng# *rng, char *buf, size_t buflen)
489 {
490 ssize_tcxx . . $ ssize_t# ret;
491
492 /*
493 *jent_read_entropy_safe implies a changing H_submitter which is not
494 * allowed in SP800-90B.
495 */
496 if (force_sp80090bcxx . . $ force_sp80090b.)
497 ret = jent_read_entropycxx . . $ jent_read_entropy(d73b7c7d0a42315f).(rng->eccxx . . $ kernel_rng#ec., buf, buflen);
498 else
499 ret = jent_read_entropy_safecxx . . $ jent_read_entropy_safe(472376e7fa383f61).(&rng->eccxx . . $ kernel_rng#ec., buf, buflen);
500
501 if (ret >= 0)
502 return ret;
503
504 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`!, "Cannot read entropy");
505
506 return -EOPNOTSUPPcxx . . $ `<external>/17922322188510340118/errno.h:79:9`!;
507 }
508
509 static ssize_tcxx . . $ ssize_t# gather_entropycxx . . $ gather_entropy(fee6cd045d83b823).(struct kernel_rngcxx . . $ kernel_rng# *rng)
510 {
511 sigset_tcxx . . $ sigset_t# blocking_set, previous_set;
512 #define ENTBLOCKSIZEcxx . . $ `jitterentropy-rngd.c:512:9`! (ENTROPYBYTEScxx . . $ `jitterentropy-rngd.c:122:9`! * OVERSAMPLINGFACTORcxx . . $ `jitterentropy-rngd.c:123:9`!)
513 /*
514 * Maximum numbers of blocks is determined by numbers of reseed IOCTLs: if
515 * the reseed IOCTL is used, we call ceil(256 / 80) numbers of IOCTLs. As
516 * each IOCTL may drain the entropy pool by 256 bits, we need to ensure that
517 * after the numbers of IOCTLs, we finally inject more blocks than the numbers
518 * of IOCTLs into the input_pool. Otherwise the entropy estimator will never
519 * rise and we encounter an endless loop.
520 */
521 #define ENTBLOCKScxx . . $ `jitterentropy-rngd.c:521:9`! (4 + 2 + 1)
522 char buf[(ENTBLOCKSIZEcxx . . $ `jitterentropy-rngd.c:512:9`! * ENTBLOCKScxx . . $ `jitterentropy-rngd.c:521:9`!)];
523 ssize_tcxx . . $ ssize_t# buflen = ENTBLOCKSIZEcxx . . $ `jitterentropy-rngd.c:512:9`!;
524 ssize_tcxx . . $ ssize_t# ret = 0;
525
526 sigemptysetcxx . . $ sigemptyset(f9c066a8ff75ee9d).(&previous_set);
527 sigemptysetcxx . . $ sigemptyset(f9c066a8ff75ee9d).(&blocking_set);
528 sigaddsetcxx . . $ sigaddset(4917c6e3ecb7dbd5).(&blocking_set, SIGALRMcxx . . $ `<external>/11773118241360591791/signum-generic.h:61:9`!);
529
530 sigprocmaskcxx . . $ sigprocmask(7504382baa173b1).(SIG_BLOCKcxx . . $ `<external>/16635209305659446502/sigaction.h:79:9`!, &blocking_set, &previous_set);
531
532 if (lrng_presentcxx . . $ lrng_present(b126dc7c1de90089).()) {
533 /*
534 * The LRNG operates fully 90B compliant, no special handling
535 * is necessary.
536 */
537 ret = read_jentcxx . . $ read_jent(e59ca0fcea0ad1d9).(rng, buf, buflen);
538 if (ret < 0)
539 goto out;
540
541 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "LRNG: Inject %u bits of data with %u bits of entropy into Blake2S state",
542 buflen << 3, ret << 3);
543
544 /*
545 * Write the entropy, LRNG seeds automatically - the Jitter RNG
546 * provides full entropy so, we tell the Linux RNG the amount of
547 * entropy.
548 */
549 ret = write_randomcxx . . $ write_random(b984c3db3bd714de).(rng, buf, buflen, ret, 0);
550 } else {
551 if (!kernver_gecxx . . $ kernver_ge(64d4c609941711dd).(5, 18, 0)) {
552 static int reported = 0;
553
554 if (!reported) {
555 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`!, "Kernel older than 5.18 detected - DRT.1 status unclear");
556 reported = 1;
557 }
558 }
559
560 /*
561 * AIS 20/31 DRT.1, no special handling is necessary.
562 */
563 ret = read_jentcxx . . $ read_jent(e59ca0fcea0ad1d9).(rng, buf, buflen);
564 if (ret < 0)
565 goto out;
566
567 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Linux kernel >= 5.18: Inject %u bits of data with %u bits of entropy into Blake2S state",
568 buflen << 3, ret << 3);
569
570 /*
571 * Write the entropy and trigger reseed - the Jitter RNG provides
572 * full entropy so, we tell the Linux RNG the amount of entropy.
573 */
574 ret = write_randomcxx . . $ write_random(b984c3db3bd714de).(rng, buf, buflen, ret, 1);
575 }
576
577 if (ret >= 0 && buflen != ret) {
578 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`!, "Injected %lu bytes into %s, expected %d",
579 ret, rng->devcxx . . $ kernel_rng#dev., buflen);
580 ret = 0;
581 }
582
583 out:
584 memset_securecxx . . $ memset_secure(e37d57925061d5c9).(buf, 0, buflen);
585
586 if (exit_on_errorcxx . . $ exit_on_error. && ret < 0) {
587 /* We now exit as requested by caller */
588 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
589 exitcxx . . $ exit(3ca292c338df9bb3).(-ret);
590 }
591
592 sigprocmaskcxx . . $ sigprocmask(7504382baa173b1).(SIG_SETMASKcxx . . $ `<external>/16635209305659446502/sigaction.h:81:9`!, &previous_set, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
593
594 return ret;
595 }
596
597 static int read_entropy_valuecxx . . $ read_entropy_value(7864480464b09eea).(int fd)
598 {
599 ssize_tcxx . . $ ssize_t# data = 0;
600 char buf[5];
601 int entropy = 0;
602
603 data = readcxx . . $ read(7e34d0a36f355389).(fd, buf, sizeof(buf));
604 lseekcxx . . $ lseek(f1afe674ac17c82e).(fd, 0, SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!);
605
606 if (0 > data) {
607 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`!, "Error reading data from entropy fd: %s",
608 strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
609 return 0;
610 }
611 if (0 == data) {
612 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`!, "Could not read data from entropy fd");
613 return 0;
614 }
615
616 entropy = atoicxx . . $ atoi(5c4306d8553b3696).(buf);
617 if (0 > entropy || 4096 < entropy) {
618 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_WARNcxx . . $ `jitterentropy-rngd.c:387:9`!, "Entropy read from entropy fd (%d) is outsize of range", entropy);
619 return 0;
620 }
621
622 return entropy;
623 }
624
625 /*******************************************************************
626 * Signal handling functions
627 *******************************************************************/
628
629 /*
630 * Wakeup and check entropy_avail -- this covers the drain of entropy
631 * from the nonblocking_pool via get_random_bytes
632 */
633 static void sig_entropy_availcxx . . $ sig_entropy_avail(d4f767463ce0a6b3).(int sig)
634 {
635 int entropy = 0, thresh = 0;
636 ssize_tcxx . . $ ssize_t# written = 0;
637 static unsigned int force_reseed = FORCE_RESEED_WAKEUPS_PHASE1cxx . . $ `jitterentropy-rngd.c:131:9`!;
638 static unsigned int alarm_period = ALARM_PERIOD_PHASE1cxx . . $ `jitterentropy-rngd.c:132:9`!;
639
640 (void)sig;
641
642 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_VERBOSEcxx . . $ `jitterentropy-rngd.c:386:9`!, "Wakeup call for alarm on %s", ENTROPYAVAILcxx . . $ `jitterentropy-rngd.c:135:9`!);
643
644 if (--force_reseed == 0) {
645 force_reseed = FORCE_RESEED_WAKEUPS_PHASE2cxx . . $ `jitterentropy-rngd.c:133:9`!;
646 alarm_period = ALARM_PERIOD_PHASE2cxx . . $ `jitterentropy-rngd.c:134:9`!;
647 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Force reseed", entropy);
648 do {
649 if (written < 0) {
650 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Re-initializing rngd\n");
651 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
652 if (alloccxx . . $ alloc(b126dc7c1de90089).() < 0)
653 goto out;
654 }
655 written = gather_entropycxx . . $ gather_entropy(fee6cd045d83b823).(&Randomcxx . . $ Random.);
656 } while (written < 0);
657 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_VERBOSEcxx . . $ `jitterentropy-rngd.c:386:9`!, "%zd bytes written to /dev/random", written);
658 goto out;
659 }
660
661 entropy = read_entropy_valuecxx . . $ read_entropy_value(7864480464b09eea).(Entropy_avail_fdcxx . . $ Entropy_avail_fd.);
662 thresh = read_entropy_valuecxx . . $ read_entropy_value(7864480464b09eea).(Entropy_thresh_fdcxx . . $ Entropy_thresh_fd.);
663
664 if (0 == entropy || 0 == thresh)
665 goto out;
666 if (entropy >= thresh) {
667 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Sufficient entropy %d available", entropy);
668 goto out;
669 }
670 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Insufficient entropy %d available (threshold %d)",
671 entropy, thresh);
672 do {
673 if (written < 0) {
674 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Re-initializing rngd\n");
675 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
676 if (alloccxx . . $ alloc(b126dc7c1de90089).() < 0)
677 goto out;
678 }
679 written = gather_entropycxx . . $ gather_entropy(fee6cd045d83b823).(&Randomcxx . . $ Random.);
680 } while (written < 0);
681 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_VERBOSEcxx . . $ `jitterentropy-rngd.c:386:9`!, "%zd bytes written to /dev/random", written);
682 out:
683 install_alarmcxx . . $ install_alarm(cb2f890c2fdad230).(alarm_period);
684 return;
685 }
686
687 /* terminate the daemon cleanly */
688 static void sig_termcxx . . $ sig_term(d4f767463ce0a6b3).(int sig)
689 {
690 (void)sig;
691 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Shutting down cleanly\n");
692
693 /* Prevent the kernel from interfering with the shutdown */
694 signalcxx . . $ signal(c2d516773f91faf8).(SIGALRMcxx . . $ `<external>/11773118241360591791/signum-generic.h:61:9`!, SIG_IGNcxx . . $ `<external>/11773118241360591791/signum-generic.h:30:9`!);
695
696 /* If we got another termination signal, just get killed */
697 signalcxx . . $ signal(c2d516773f91faf8).(SIGHUPcxx . . $ `<external>/11773118241360591791/signum-generic.h:56:9`!, SIG_DFLcxx . . $ `<external>/11773118241360591791/signum-generic.h:29:9`!);
698 signalcxx . . $ signal(c2d516773f91faf8).(SIGINTcxx . . $ `<external>/11773118241360591791/signum-generic.h:48:9`!, SIG_DFLcxx . . $ `<external>/11773118241360591791/signum-generic.h:29:9`!);
699 signalcxx . . $ signal(c2d516773f91faf8).(SIGQUITcxx . . $ `<external>/11773118241360591791/signum-generic.h:57:9`!, SIG_DFLcxx . . $ `<external>/11773118241360591791/signum-generic.h:29:9`!);
700 signalcxx . . $ signal(c2d516773f91faf8).(SIGTERMcxx . . $ `<external>/11773118241360591791/signum-generic.h:53:9`!, SIG_DFLcxx . . $ `<external>/11773118241360591791/signum-generic.h:29:9`!);
701
702 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
703 exitcxx . . $ exit(3ca292c338df9bb3).(0);
704 }
705
706 /*
707 * Wakeup on insufficient entropy on /dev/random
708 */
709 static void select_fdcxx . . $ select_fd(49f6e7a06ebc5aa8).(void)
710 {
711 fd_setcxx . . $ fd_set# fds;
712 int ret = 0;
713 ssize_tcxx . . $ ssize_t# written = 0;
714
715 while (1) {
716 FD_ZEROcxx . . $ `<external>/946878812840035423/select.h:88:9`!(&fds);
717 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Polling /dev/random");
718 FD_SETcxx . . $ `<external>/946878812840035423/select.h:85:9`!(Random.fd, &fds);
719 /* only /dev/random implements polling */
720 ret = selectcxx . . $ select(6d24c7514c8a5b88).((Randomcxx . . $ Random..fdcxx . . $ kernel_rng#fd. + 1), NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, &fds, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
721
722 if (-1 == ret && EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`! != errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) {
723 if (exit_on_errorcxx . . $ exit_on_error.) {
724 int errsv = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
725
726 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
727 exitcxx . . $ exit(3ca292c338df9bb3).(errsv);
728 }
729 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Select returned with error %s", strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
730 }
731 if (0 <= ret) {
732 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_VERBOSEcxx . . $ `jitterentropy-rngd.c:386:9`!, "Wakeup call for select on /dev/random");
733 do {
734 if (written < 0) {
735 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!,
736 "Re-initializing rngd\n");
737 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
738 if (alloccxx . . $ alloc(b126dc7c1de90089).() < 0)
739 continue;
740 }
741 written = gather_entropycxx . . $ gather_entropy(fee6cd045d83b823).(&Randomcxx . . $ Random.);
742 } while (written < 0);
743 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_VERBOSEcxx . . $ `jitterentropy-rngd.c:386:9`!, "%zd bytes written to /dev/random",
744 written);
745 }
746 }
747 }
748
749 static void install_alarmcxx . . $ install_alarm(cb2f890c2fdad230).(unsigned int secs)
750 {
751 if (lrng_presentcxx . . $ lrng_present(b126dc7c1de90089).())
752 return;
753 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Install alarm signal handler");
754 signalcxx . . $ signal(c2d516773f91faf8).(SIGALRMcxx . . $ `<external>/11773118241360591791/signum-generic.h:61:9`!, sig_entropy_availcxx . . $ sig_entropy_avail(d4f767463ce0a6b3).);
755 alarmcxx . . $ alarm(2141b656ce2cc063).(secs);
756 }
757
758 static void install_termcxx . . $ install_term(49f6e7a06ebc5aa8).(void)
759 {
760 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_DEBUGcxx . . $ `jitterentropy-rngd.c:385:9`!, "Install termination signal handler");
761 signalcxx . . $ signal(c2d516773f91faf8).(SIGHUPcxx . . $ `<external>/11773118241360591791/signum-generic.h:56:9`!, sig_termcxx . . $ sig_term(d4f767463ce0a6b3).);
762 signalcxx . . $ signal(c2d516773f91faf8).(SIGINTcxx . . $ `<external>/11773118241360591791/signum-generic.h:48:9`!, sig_termcxx . . $ sig_term(d4f767463ce0a6b3).);
763 signalcxx . . $ signal(c2d516773f91faf8).(SIGQUITcxx . . $ `<external>/11773118241360591791/signum-generic.h:57:9`!, sig_termcxx . . $ sig_term(d4f767463ce0a6b3).);
764 signalcxx . . $ signal(c2d516773f91faf8).(SIGTERMcxx . . $ `<external>/11773118241360591791/signum-generic.h:53:9`!, sig_termcxx . . $ sig_term(d4f767463ce0a6b3).);
765 }
766
767 /*******************************************************************
768 * allocation functions
769 *******************************************************************/
770
771 static void dealloc_rngcxx . . $ dealloc_rng(ed9240bb51f9de62).(struct kernel_rngcxx . . $ kernel_rng# *rng)
772 {
773 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! != rng->eccxx . . $ kernel_rng#ec.) {
774 jent_entropy_collector_freecxx . . $ jent_entropy_collector_free(dd399101f0e3ec34).(rng->eccxx . . $ kernel_rng#ec.);
775 rng->eccxx . . $ kernel_rng#ec. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
776 }
777 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! != rng->rpicxx . . $ kernel_rng#rpi.) {
778 memsetcxx . . $ memset(d1de1e4fb215f1c6).(rng->rpicxx . . $ kernel_rng#rpi., 0,(sizeof(struct rand_pool_infocxx . . $ rand_pool_info#) +
779 (ENTROPYBYTEScxx . . $ `jitterentropy-rngd.c:122:9`! * OVERSAMPLINGFACTORcxx . . $ `jitterentropy-rngd.c:123:9`! *
780 sizeof(char))));
781 freecxx . . $ free(5d22bdacc48458e8).(rng->rpicxx . . $ kernel_rng#rpi.);
782 rng->rpicxx . . $ kernel_rng#rpi. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
783 }
784 if (-1 != rng->fdcxx . . $ kernel_rng#fd.) {
785 closecxx . . $ close(7864480464b09eea).(rng->fdcxx . . $ kernel_rng#fd.);
786 rng->fdcxx . . $ kernel_rng#fd. = -1;
787 }
788 }
789
790 static void dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).(void)
791 {
792 dealloc_rngcxx . . $ dealloc_rng(ed9240bb51f9de62).(&Randomcxx . . $ Random.);
793 if (-1 != Entropy_avail_fdcxx . . $ Entropy_avail_fd.) {
794 closecxx . . $ close(7864480464b09eea).(Entropy_avail_fdcxx . . $ Entropy_avail_fd.);
795 Entropy_avail_fdcxx . . $ Entropy_avail_fd. = -1;
796 }
797 if (-1 != Entropy_thresh_fdcxx . . $ Entropy_thresh_fd.) {
798 closecxx . . $ close(7864480464b09eea).(Entropy_thresh_fdcxx . . $ Entropy_thresh_fd.);
799 Entropy_thresh_fdcxx . . $ Entropy_thresh_fd. = -1;
800 }
801 if (-1 != Pidfile_fdcxx . . $ Pidfile_fd.) {
802 closecxx . . $ close(7864480464b09eea).(Pidfile_fdcxx . . $ Pidfile_fd.);
803 Pidfile_fdcxx . . $ Pidfile_fd. = -1;
804 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! != Pidfilecxx . . $ Pidfile.)
805 unlinkcxx . . $ unlink(5c4306d8553b3696).(Pidfilecxx . . $ Pidfile.);
806 }
807 }
808
809 static int alloc_rngcxx . . $ alloc_rng(adae6967ed6b4813).(struct kernel_rngcxx . . $ kernel_rng# *rng)
810 {
811 rng->eccxx . . $ kernel_rng#ec. = jent_entropy_collector_alloccxx . . $ jent_entropy_collector_alloc(5e5cd898694a5438).(jent_osrcxx . . $ jent_osr., jent_flagscxx . . $ jent_flags.);
812 if (!rng->eccxx . . $ kernel_rng#ec.) {
813 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Allocation of entropy collector failed");
814 return -EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!;
815 }
816
817 if (statuscxx . . $ status.) {
818 char buf[2500];
819 int ret = jent_statuscxx . . $ jent_status(b830aaa7046db909).(rng->eccxx . . $ kernel_rng#ec., buf, sizeof(buf));
820
821 if (ret)
822 return ret;
823
824 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s\n", buf);
825 return -EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!;
826 }
827
828 rng->rpicxx . . $ kernel_rng#rpi. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).((sizeof(struct rand_pool_infocxx . . $ rand_pool_info#) +
829 (ENTROPYBYTEScxx . . $ `jitterentropy-rngd.c:122:9`! * OVERSAMPLINGFACTORcxx . . $ `jitterentropy-rngd.c:123:9`! * sizeof(char))));
830 if (!rng->rpicxx . . $ kernel_rng#rpi.) {
831 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Cannot allocate memory for random bytes");
832 dealloc_rngcxx . . $ dealloc_rng(ed9240bb51f9de62).(rng);
833 return -ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!;
834 }
835
836 rng->fdcxx . . $ kernel_rng#fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(rng->devcxx . . $ kernel_rng#dev., O_WRONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:44:9`!);
837 if (-1 == rng->fdcxx . . $ kernel_rng#fd.) {
838 int errsv = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
839
840 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Open of %s failed: %s", rng->devcxx . . $ kernel_rng#dev., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
841 dealloc_rngcxx . . $ dealloc_rng(ed9240bb51f9de62).(rng);
842 return -errsv;
843 }
844
845 return 0;
846 }
847
848 static int alloccxx . . $ alloc(b126dc7c1de90089).(void)
849 {
850 int ret = 0;
851 ssize_tcxx . . $ ssize_t# written = 0;
852
853 ret = jent_entropy_init_excxx . . $ jent_entropy_init_ex(21cb2064190e2a90).(jent_osrcxx . . $ jent_osr., jent_flagscxx . . $ jent_flags.);
854 if (ret) {
855 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "The initialization of CPU Jitter RNG failed with error code %d\n", ret);
856 return ret;
857 }
858
859 ret = alloc_rngcxx . . $ alloc_rng(adae6967ed6b4813).(&Randomcxx . . $ Random.);
860 if (ret)
861 return ret;
862
863 Entropy_avail_fdcxx . . $ Entropy_avail_fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(ENTROPYAVAILcxx . . $ `jitterentropy-rngd.c:135:9`!, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
864 if (-1 == Entropy_avail_fdcxx . . $ Entropy_avail_fd.) {
865 int errsv = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
866
867 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Open of %s failed: %s", ENTROPYAVAILcxx . . $ `jitterentropy-rngd.c:135:9`!, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
868 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
869 return -errsv;
870 }
871
872 Entropy_thresh_fdcxx . . $ Entropy_thresh_fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(ENTROPYTHRESHcxx . . $ `jitterentropy-rngd.c:136:9`!, O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
873 if (-1 == Entropy_thresh_fdcxx . . $ Entropy_thresh_fd.) {
874 int errsv = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
875
876 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Open of %s failed: %s", ENTROPYTHRESHcxx . . $ `jitterentropy-rngd.c:136:9`!, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
877 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
878 return -errsv;
879 }
880
881 written = gather_entropycxx . . $ gather_entropy(fee6cd045d83b823).(&Randomcxx . . $ Random.);
882 if (written >= 0) {
883 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_VERBOSEcxx . . $ `jitterentropy-rngd.c:386:9`!, "%zd bytes to /dev/random", written);
884 } else {
885 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Cannot write to /dev/random, failure: %zd",
886 written);
887 /* We consider this as no error at this point */
888 }
889
890 return 0;
891 }
892
893 static void create_pid_filecxx . . $ create_pid_file(85c52e162fed56f9).(const char *pid_file)
894 {
895 char pid_str[12]; /* max. integer length + '\n' + null */
896
897 /* Ensure only one copy */
898 Pidfile_fdcxx . . $ Pidfile_fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(pid_file, O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!|O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!|O_EXCLcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:50:10`!, S_IRUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:105:10`!|S_IWUSRcxx . . $ `<external>/14710646456994434043/fcntl.h:106:10`!);
899 if (Pidfile_fdcxx . . $ Pidfile_fd. == -1)
900 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Cannot open pid file\n");
901
902 if (lockfcxx . . $ lockf(bfd589b6ba05a0de).(Pidfile_fdcxx . . $ Pidfile_fd., F_TLOCKcxx . . $ `<external>/12628407106812493757/unistd.h:1110:10`!, 0) == -1) {
903 if (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`! || errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EACCEScxx . . $ `<external>/8987895939074593727/errno-base.h:17:9`!) {
904 int errsv = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
905
906 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "PID file already locked\n");
907 exitcxx . . $ exit(3ca292c338df9bb3).(errsv);
908 } else
909 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Cannot lock pid file\n");
910 }
911
912 if (ftruncatecxx . . $ ftruncate(bba9d6e9fb34fad7).(Pidfile_fdcxx . . $ Pidfile_fd., 0) == -1) {
913 int errsv = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
914
915 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Cannot truncate pid file\n");
916 exitcxx . . $ exit(3ca292c338df9bb3).(errsv);
917 }
918
919 /* write our pid to the pid file */
920 snprintfcxx . . $ snprintf(ea502d73e74c754c).(pid_str, sizeof(pid_str), "%d\n", getpidcxx . . $ getpid(b126dc7c1de90089).());
921 if (writecxx . . $ write(41184a59d38b4f65).(Pidfile_fdcxx . . $ Pidfile_fd., pid_str, strlencxx . . $ strlen(751346f8406fd082).(pid_str)) !=
922 (ssize_tcxx . . $ ssize_t#)strlencxx . . $ strlen(751346f8406fd082).(pid_str)) {
923 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Cannot write to pid file\n");
924 exitcxx . . $ exit(3ca292c338df9bb3).(1);
925 }
926 }
927
928 static void daemonizecxx . . $ daemonize(49f6e7a06ebc5aa8).(void)
929 {
930 pid_tcxx . . $ pid_t# pid;
931
932 /* already a daemon */
933 if (1 == getppidcxx . . $ getppid(b126dc7c1de90089).())
934 return;
935
936 pid = forkcxx . . $ fork(b126dc7c1de90089).();
937 if (pid < 0)
938 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Cannot fork to daemonize\n");
939
940 /* the parent process exits -- nothing has been allocated, nothing
941 * needs to be freed */
942 if (0 < pid)
943 exitcxx . . $ exit(3ca292c338df9bb3).(0);
944
945 /* we are the child now */
946
947 /* new SID for the child process */
948 if (setsidcxx . . $ setsid(b126dc7c1de90089).() < 0)
949 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Cannot obtain new SID for child\n");
950
951 /* Change the current working directory. This prevents the current
952 * directory from being locked; hence not being able to remove it. */
953 if ((chdircxx . . $ chdir(5c4306d8553b3696).("/")) < 0)
954 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Cannot change directory\n");
955
956 if (Pidfilecxx . . $ Pidfile. && strlencxx . . $ strlen(751346f8406fd082).(Pidfilecxx . . $ Pidfile.))
957 create_pid_filecxx . . $ create_pid_file(85c52e162fed56f9).(Pidfilecxx . . $ Pidfile.);
958
959 /* Redirect standard files to /dev/null */
960 #pragma GCC diagnostic push
961 #pragma GCC diagnostic ignored "-Wunused-result"
962 freopencxx . . $ freopen(8add521aa6a46b24).( "/dev/null", "r", stdincxx . . $ `<external>/232824719582829767/stdio.h:157:9`!);
963 freopencxx . . $ freopen(8add521aa6a46b24).( "/dev/null", "w", stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!);
964 freopencxx . . $ freopen(8add521aa6a46b24).( "/dev/null", "w", stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!);
965 #pragma GCC diagnostic pop
966 }
967
968
969 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
970 {
971 int ret;
972
973 parse_optscxx . . $ parse_opts(c759356ac01a31e7).(argc, argv);
974
975 if (geteuidcxx . . $ geteuid(db52760e79bf9054).())
976 dologcxx . . $ dolog(afcfcee7634adf7d).(LOG_ERRcxx . . $ `jitterentropy-rngd.c:388:9`!, "Program must start as root!");
977
978 ret = alloccxx . . $ alloc(b126dc7c1de90089).();
979 if (ret)
980 goto out;
981
982 if (0 == Verbositycxx . . $ Verbosity.)
983 daemonizecxx . . $ daemonize(49f6e7a06ebc5aa8).();
984 install_termcxx . . $ install_term(49f6e7a06ebc5aa8).();
985 install_alarmcxx . . $ install_alarm(cb2f890c2fdad230).(ALARM_PERIOD_PHASE1cxx . . $ `jitterentropy-rngd.c:132:9`!);
986 select_fdcxx . . $ select_fd(49f6e7a06ebc5aa8).();
987 /* NOTREACHED */
988
989 out:
990 dealloccxx . . $ dealloc(49f6e7a06ebc5aa8).();
991 return -ret;
992 }