]> git.ipfire.org Git - thirdparty/rng-tools.git/blob - rngd_linux.c
rngd_rdrand: Actually perform the correct AES data reduction
[thirdparty/rng-tools.git] / rngd_linux.c
1 /*
2 * rngd_linux.c -- Entropy sink for the Linux Kernel (/dev/random)
3 *
4 * Copyright (C) 2001 Philipp Rumpf
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #define _GNU_SOURCE
22
23 #ifndef HAVE_CONFIG_H
24 #error Invalid or missing autoconf build environment
25 #endif
26
27 #include "rng-tools-config.h"
28
29 #include <unistd.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <syslog.h>
35 #include <sys/ioctl.h>
36 #include <sys/poll.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <sys/time.h>
41 #include <time.h>
42 #include <linux/types.h>
43 #include <linux/random.h>
44 #include <string.h>
45
46 #include "rngd.h"
47 #include "fips.h"
48 #include "exits.h"
49 #include "rngd_linux.h"
50
51 extern struct rng *rng_list;
52
53 /* Kernel output device */
54 static int random_fd;
55
56 /*
57 * Get the default watermark
58 */
59 int default_watermark(void)
60 {
61 char psbuf[64], *p;
62 unsigned long ps;
63 FILE *f;
64 size_t l;
65 unsigned int wm = 2048; /* Default guess */
66
67 f = fopen("/proc/sys/kernel/random/poolsize", "r");
68 if (!f)
69 goto err;
70 l = fread(psbuf, 1, sizeof psbuf, f);
71 if (ferror(f) || !feof(f) || l == 0)
72 goto err;
73 if (psbuf[l-1] != '\n')
74 goto err;
75 psbuf[l-1] = '\0';
76 ps = strtoul(psbuf, &p, 0);
77 if (*p)
78 goto err;
79
80 wm = ps*3/4;
81
82 err:
83 if (f)
84 fclose(f);
85 return wm;
86 }
87
88 /*
89 * Initialize the interface to the Linux Kernel
90 * entropy pool (through /dev/random)
91 *
92 * randomdev is the path to the random device
93 */
94 void init_kernel_rng(const char* randomdev)
95 {
96 FILE *f;
97
98 random_fd = open(randomdev, O_RDWR);
99 if (random_fd == -1) {
100 message(LOG_DAEMON|LOG_ERR, "can't open %s: %s",
101 randomdev, strerror(errno));
102 exit(EXIT_USAGE);
103 }
104
105 f = fopen("/proc/sys/kernel/random/write_wakeup_threshold", "w");
106 if (f) {
107 fprintf(f, "%u\n", arguments->fill_watermark);
108 fclose(f);
109 }
110 }
111
112 void random_add_entropy(void *buf, size_t size)
113 {
114 struct {
115 int ent_count;
116 int size;
117 unsigned char data[size];
118 } entropy;
119
120 entropy.ent_count = size * 8;
121 entropy.size = size;
122 memcpy(entropy.data, buf, size);
123
124 if (ioctl(random_fd, RNDADDENTROPY, &entropy) != 0) {
125 message(LOG_DAEMON|LOG_ERR, "RNDADDENTROPY failed: %s\n",
126 strerror(errno));
127 exit(1);
128 }
129 }
130
131 void random_sleep(void)
132 {
133 struct pollfd pfd = {
134 fd: random_fd,
135 events: POLLOUT,
136 };
137
138 poll(&pfd, 1, -1);
139 }
140
141 void src_list_add(struct rng *ent_src)
142 {
143 if (rng_list) {
144 struct rng *iter;
145
146 iter = rng_list;
147 while (iter->next) {
148 iter = iter->next;
149 }
150 iter->next = ent_src;
151 } else {
152 rng_list = ent_src;
153 }
154 }