]>
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 | |
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 | ||
d1b4c504 | 51 | extern struct rng *rng_list; |
61af3de3 JG |
52 | |
53 | /* Kernel output device */ | |
54 | static int random_fd; | |
55 | ||
56 | ||
57 | /* | |
58 | * Initialize the interface to the Linux Kernel | |
59 | * entropy pool (through /dev/random) | |
60 | * | |
61 | * randomdev is the path to the random device | |
62 | */ | |
63 | void init_kernel_rng(const char* randomdev) | |
64 | { | |
65 | random_fd = open(randomdev, O_RDWR); | |
66 | if (random_fd == -1) { | |
67 | message(LOG_DAEMON|LOG_ERR, "can't open %s: %s", | |
68 | randomdev, strerror(errno)); | |
69 | exit(EXIT_USAGE); | |
70 | } | |
71 | } | |
72 | ||
73 | void random_add_entropy(void *buf, size_t size) | |
74 | { | |
75 | struct { | |
76 | int ent_count; | |
77 | int size; | |
78 | unsigned char data[size]; | |
79 | } entropy; | |
80 | ||
81 | entropy.ent_count = size * 8; | |
82 | entropy.size = size; | |
83 | memcpy(entropy.data, buf, size); | |
d1b4c504 | 84 | |
61af3de3 JG |
85 | if (ioctl(random_fd, RNDADDENTROPY, &entropy) != 0) { |
86 | message(LOG_DAEMON|LOG_ERR, "RNDADDENTROPY failed: %s\n", | |
87 | strerror(errno)); | |
88 | exit(1); | |
89 | } | |
90 | } | |
91 | ||
92 | void random_sleep(double poll_timeout) | |
93 | { | |
94 | int ent_count; | |
95 | struct pollfd pfd = { | |
96 | fd: random_fd, | |
97 | events: POLLOUT, | |
98 | }; | |
99 | ||
100 | if (ioctl(random_fd, RNDGETENTCNT, &ent_count) == 0 && | |
101 | ent_count < arguments->fill_watermark) | |
102 | return; | |
d1b4c504 | 103 | |
61af3de3 JG |
104 | poll(&pfd, 1, 1000.0 * poll_timeout); |
105 | } | |
106 | ||
a26d20cf | 107 | void src_list_add(struct rng *ent_src) |
d1b4c504 JG |
108 | { |
109 | if (rng_list) { | |
110 | struct rng *iter; | |
111 | ||
112 | iter = rng_list; | |
113 | while (iter->next) { | |
114 | iter = iter->next; | |
115 | } | |
116 | iter->next = ent_src; | |
117 | } else { | |
118 | rng_list = ent_src; | |
119 | } | |
120 | } |