]>
Commit | Line | Data |
---|---|---|
61af3de3 JG |
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. | |
d1b4c504 | 10 | * |
61af3de3 JG |
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 | |
d32709d1 | 18 | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
61af3de3 JG |
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 | ||
d1b4c504 | 51 | extern struct rng *rng_list; |
61af3de3 JG |
52 | |
53 | /* Kernel output device */ | |
54 | static int random_fd; | |
55 | ||
870f0d5d PA |
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 | } | |
61af3de3 JG |
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 | { | |
870f0d5d | 96 | FILE *f; |
31a7953f | 97 | int err; |
870f0d5d | 98 | |
61af3de3 JG |
99 | random_fd = open(randomdev, O_RDWR); |
100 | if (random_fd == -1) { | |
101 | message(LOG_DAEMON|LOG_ERR, "can't open %s: %s", | |
102 | randomdev, strerror(errno)); | |
103 | exit(EXIT_USAGE); | |
104 | } | |
870f0d5d PA |
105 | |
106 | f = fopen("/proc/sys/kernel/random/write_wakeup_threshold", "w"); | |
31a7953f PA |
107 | if (!f) { |
108 | err = 1; | |
109 | } else { | |
870f0d5d | 110 | fprintf(f, "%u\n", arguments->fill_watermark); |
31a7953f PA |
111 | /* Note | not || here... we always want to close the file */ |
112 | err = ferror(f) | fclose(f); | |
113 | } | |
114 | if (err) { | |
115 | message(LOG_DAEMON|LOG_WARNING, | |
116 | "unable to adjust write_wakeup_threshold: %s", | |
117 | strerror(errno)); | |
870f0d5d | 118 | } |
61af3de3 JG |
119 | } |
120 | ||
121 | void random_add_entropy(void *buf, size_t size) | |
122 | { | |
123 | struct { | |
124 | int ent_count; | |
125 | int size; | |
126 | unsigned char data[size]; | |
127 | } entropy; | |
128 | ||
129 | entropy.ent_count = size * 8; | |
130 | entropy.size = size; | |
131 | memcpy(entropy.data, buf, size); | |
d1b4c504 | 132 | |
61af3de3 JG |
133 | if (ioctl(random_fd, RNDADDENTROPY, &entropy) != 0) { |
134 | message(LOG_DAEMON|LOG_ERR, "RNDADDENTROPY failed: %s\n", | |
135 | strerror(errno)); | |
136 | exit(1); | |
137 | } | |
138 | } | |
139 | ||
593a9302 | 140 | void random_sleep(void) |
61af3de3 | 141 | { |
61af3de3 JG |
142 | struct pollfd pfd = { |
143 | fd: random_fd, | |
144 | events: POLLOUT, | |
145 | }; | |
146 | ||
a9b9bb1d | 147 | poll(&pfd, 1, -1); |
61af3de3 JG |
148 | } |
149 | ||
a26d20cf | 150 | void src_list_add(struct rng *ent_src) |
d1b4c504 JG |
151 | { |
152 | if (rng_list) { | |
153 | struct rng *iter; | |
154 | ||
155 | iter = rng_list; | |
156 | while (iter->next) { | |
157 | iter = iter->next; | |
158 | } | |
159 | iter->next = ent_src; | |
160 | } else { | |
161 | rng_list = ent_src; | |
162 | } | |
163 | } |