]> git.ipfire.org Git - thirdparty/rng-tools.git/blob - rngd.c
Disable entropy source, if facing continued failures.
[thirdparty/rng-tools.git] / rngd.c
1 /*
2 * rngd.c -- Random Number Generator daemon
3 *
4 * rngd reads data from a hardware random number generator, verifies it
5 * looks like random data, and adds it to /dev/random's entropy store.
6 *
7 * In theory, this should allow you to read very quickly from
8 * /dev/random; rngd also adds bytes to the entropy store periodically
9 * when it's full, which makes predicting the entropy store's contents
10 * harder.
11 *
12 * Copyright (C) 2001 Philipp Rumpf
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 */
28
29 #define _GNU_SOURCE
30
31 #ifndef HAVE_CONFIG_H
32 #error Invalid or missing autoconf build environment
33 #endif
34
35 #include "rng-tools-config.h"
36
37 #include <unistd.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <string.h>
46 #include <argp.h>
47 #include <syslog.h>
48
49 #include "rngd.h"
50 #include "fips.h"
51 #include "exits.h"
52 #include "rngd_entsource.h"
53 #include "rngd_linux.h"
54
55 /*
56 * Globals
57 */
58
59 /* Background/daemon mode */
60 int am_daemon; /* Nonzero if we went daemon */
61
62 /* Command line arguments and processing */
63 const char *argp_program_version =
64 "rngd " VERSION "\n"
65 "Copyright 2001-2004 Jeff Garzik\n"
66 "Copyright (c) 2001 by Philipp Rumpf\n"
67 "This is free software; see the source for copying conditions. There is NO "
68 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.";
69
70 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
71
72 static char doc[] =
73 "Check and feed random data from hardware device to kernel entropy pool.\n";
74
75 static struct argp_option options[] = {
76 { "foreground", 'f', 0, 0, "Do not fork and become a daemon" },
77
78 { "background", 'b', 0, 0, "Become a daemon (default)" },
79
80 { "random-device", 'o', "file", 0,
81 "Kernel device used for random number output (default: /dev/random)" },
82
83 { "rng-device", 'r', "file", 0,
84 "Kernel device used for random number input (default: /dev/hw_random)" },
85
86 { "random-step", 's', "nnn", 0,
87 "Number of bytes written to random-device at a time (default: 64)" },
88
89 { "fill-watermark", 'W', "n", 0,
90 "Do not stop feeding entropy to random-device until at least n bits of entropy are available in the pool (default: 2048), 0 <= n <= 4096" },
91
92 { "timeout", 't', "nnn", 0,
93 "Interval written to random-device when the entropy pool is full, in seconds (default: 60)" },
94 { "no-tpm", 'n', "1|0", 0,
95 "do not use tpm as a source of random number input (default: 0)" },
96
97 { 0 },
98 };
99
100 static struct arguments default_arguments = {
101 .random_name = "/dev/random",
102 .poll_timeout = 60,
103 .random_step = 64,
104 .fill_watermark = 2048,
105 .daemon = 1,
106 .enable_tpm = 1,
107 };
108 struct arguments *arguments = &default_arguments;
109
110 static struct rng rng_default = {
111 .rng_name = "/dev/hw_random",
112 .rng_fd = -1,
113 .xread = xread,
114 };
115
116 static struct rng rng_tpm = {
117 .rng_name = "/dev/tpm0",
118 .rng_fd = -1,
119 .xread = xread_tpm,
120 };
121
122 struct rng *rng_list;
123
124 /*
125 * command line processing
126 */
127 static error_t parse_opt (int key, char *arg, struct argp_state *state)
128 {
129 switch(key) {
130 case 'o':
131 arguments->random_name = arg;
132 break;
133 case 'r':
134 rng_default.rng_name = arg;
135 break;
136 case 't': {
137 float f;
138 if (sscanf(arg, "%f", &f) == 0)
139 argp_usage(state);
140 else
141 arguments->poll_timeout = f;
142 break;
143 }
144
145 case 'f':
146 arguments->daemon = 0;
147 break;
148 case 'b':
149 arguments->daemon = 1;
150 break;
151 case 's':
152 if (sscanf(arg, "%i", &arguments->random_step) == 0)
153 argp_usage(state);
154 break;
155 case 'W': {
156 int n;
157 if ((sscanf(arg, "%i", &n) == 0) || (n < 0) || (n > 4096))
158 argp_usage(state);
159 else
160 arguments->fill_watermark = n;
161 break;
162 }
163 case 'n': {
164 int n;
165 if ((sscanf(arg,"%i", &n) == 0) || ((n | 1)!=1))
166 argp_usage(state);
167 else
168 arguments->enable_tpm = 0;
169 break;
170 }
171
172 default:
173 return ARGP_ERR_UNKNOWN;
174 }
175
176 return 0;
177 }
178
179 static struct argp argp = { options, parse_opt, NULL, doc };
180
181
182 static int update_kernel_random(int random_step, double poll_timeout,
183 unsigned char *buf, fips_ctx_t *fipsctx)
184 {
185 unsigned char *p;
186 int fips;
187
188 fips = fips_run_rng_test(fipsctx, buf);
189 if (fips) {
190 message(LOG_DAEMON|LOG_ERR, "failed fips test\n");
191 return 1;
192 }
193
194 for (p = buf; p + random_step <= &buf[FIPS_RNG_BUFFER_SIZE];
195 p += random_step) {
196 random_add_entropy(p, random_step);
197 random_sleep(poll_timeout);
198 }
199 return 0;
200 }
201
202 static void do_loop(int random_step, double poll_timeout)
203 {
204 unsigned char buf[FIPS_RNG_BUFFER_SIZE];
205 int retval;
206 int no_work = 0;
207
208 while (no_work < 100) {
209 struct rng *iter;
210 bool work_done;
211
212 work_done = false;
213 for (iter = rng_list; iter; iter = iter->next)
214 {
215 int rc;
216
217 if (iter->disabled)
218 continue; /* failed, no work */
219
220 retval = iter->xread(buf, sizeof buf, iter);
221 if (retval)
222 continue; /* failed, no work */
223
224 work_done = true;
225
226 rc = update_kernel_random(random_step,
227 poll_timeout, buf,
228 iter->fipsctx);
229 if (rc == 0)
230 continue; /* succeeded, work done */
231
232 iter->failures++;
233 if (iter->failures == MAX_RNG_FAILURES) {
234 message(LOG_DAEMON|LOG_ERR,
235 "too many FIPS failures, disabling entropy source\n");
236 iter->disabled = true;
237 }
238 }
239
240 if (!work_done)
241 no_work++;
242 }
243
244 message(LOG_DAEMON|LOG_ERR,
245 "No entropy sources working, exiting rngd\n");
246 }
247
248 int main(int argc, char **argv)
249 {
250 int rc_rng = 1;
251 int rc_tpm = 1;
252
253 /* Parsing of commandline parameters */
254 argp_parse(&argp, argc, argv, 0, 0, arguments);
255
256 /* Init entropy sources, and open TRNG device */
257 rc_rng = init_entropy_source(&rng_default);
258 if (arguments->enable_tpm)
259 rc_tpm = init_tpm_entropy_source(&rng_tpm);
260
261 if (rc_rng && rc_tpm) {
262 message(LOG_DAEMON|LOG_ERR,
263 "can't open entropy source(tpm or intel/amd rng)");
264 message(LOG_DAEMON|LOG_ERR,
265 "Maybe RNG device modules are not loaded\n");
266 return 1;
267 }
268
269 /* Init entropy sink and open random device */
270 init_kernel_rng(arguments->random_name);
271
272 if (arguments->daemon) {
273 am_daemon = 1;
274
275 if (daemon(0, 0) < 0) {
276 fprintf(stderr, "can't daemonize: %s\n",
277 strerror(errno));
278 return 1;
279 }
280
281 openlog("rngd", 0, LOG_DAEMON);
282 }
283
284 do_loop(arguments->random_step,
285 arguments->poll_timeout ? : -1.0);
286
287 return 0;
288 }