-
Notifications
You must be signed in to change notification settings - Fork 0
/
urngd.c
244 lines (203 loc) · 5.8 KB
/
urngd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Non-physical true random number generator based on timing jitter.
*
* Copyright Stephan Mueller <[email protected]>, 2014
* Copyright Petr Štetiar <[email protected]>, 2019
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, and the entire permission notice in its entirety,
* including the disclaimer of warranties.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* ALTERNATIVELY, this product may be distributed under the terms of
* the GNU General Public License, in which case the provisions of the GPL are
* required INSTEAD OF the above restrictions. (This clause is
* necessary due to a potential bad interaction between the GPL and
* the restrictions contained in a BSD-style copyright.)
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/random.h>
#include <libubox/uloop.h>
#include "log.h"
#include "jitterentropy.h"
#define ENTROPYBYTES 32
#define ENTROPYTHRESH 1024
#define OVERSAMPLINGFACTOR 2
#define DEV_RANDOM "/dev/random"
#define ENTROPYAVAIL "/proc/sys/kernel/random/entropy_avail"
#define ENTROPYPOOLBYTES (sizeof(struct rand_pool_info) + \
(ENTROPYBYTES * OVERSAMPLINGFACTOR * sizeof(char)))
#ifdef URNGD_DEBUG
unsigned int debug;
#endif
struct urngd {
struct uloop_fd rnd_fd;
struct rand_data *ec;
struct rand_pool_info *rpi;
};
static struct urngd urngd_service;
static inline void memset_secure(void *s, int c, size_t n)
{
memset(s, c, n);
__asm__ __volatile__("" : : "r" (s) : "memory");
}
static size_t write_entropy(struct urngd *u, char *buf, size_t len,
size_t entropy_bytes)
{
int ret;
size_t written = 0;
/* value is in bits */
u->rpi->entropy_count = (entropy_bytes * 8);
u->rpi->buf_size = len;
memcpy(u->rpi->buf, buf, len);
memset(buf, 0, len);
ret = ioctl(u->rnd_fd.fd, RNDADDENTROPY, u->rpi);
if (0 > ret) {
ERROR("error injecting entropy: %s\n", strerror(errno));
} else {
DEBUG(1, "injected %zub (%zub of entropy)\n", len, entropy_bytes);
written = len;
}
u->rpi->entropy_count = 0;
u->rpi->buf_size = 0;
memset(u->rpi->buf, 0, len);
return written;
}
static size_t gather_entropy(struct urngd *u)
{
size_t ret = 0;
char buf[(ENTROPYBYTES * OVERSAMPLINGFACTOR)];
if (jent_read_entropy(u->ec, buf, sizeof(buf)) < 0) {
ERROR("cannot read entropy\n");
return 0;
}
ret = write_entropy(u, buf, sizeof(buf), ENTROPYBYTES);
if (sizeof(buf) != ret) {
ERROR("injected %zub of entropy, less then %zub expected\n",
ret, sizeof(buf));
} else {
ret = sizeof(buf);
}
memset_secure(buf, 0, sizeof(buf));
DEBUG(2, DEV_RANDOM " fed with %zub of entropy\n", ret);
return ret;
}
static void low_entropy_cb(struct uloop_fd *ufd, unsigned int events)
{
struct urngd *u = container_of(ufd, struct urngd, rnd_fd);
DEBUG(2, DEV_RANDOM " signals low entropy\n");
gather_entropy(u);
}
static void urngd_done(struct urngd *u)
{
if (u->ec) {
jent_entropy_collector_free(u->ec);
u->ec = NULL;
}
if (u->rpi) {
memset(u->rpi, 0, ENTROPYPOOLBYTES);
free(u->rpi);
u->rpi = NULL;
}
if (u->rnd_fd.fd) {
close(u->rnd_fd.fd);
u->rnd_fd.fd = 0;
}
}
static bool urngd_init(struct urngd *u)
{
int ret = jent_entropy_init();
if (ret) {
ERROR("jent-rng init failed, err: %d\n", ret);
return false;
}
u->ec = jent_entropy_collector_alloc(1, 0);
if (!u->ec) {
ERROR("jent-rng alloc failed\n");
return false;
}
u->rpi = malloc(ENTROPYPOOLBYTES);
if (!u->rpi) {
ERROR("rand pool alloc failed\n");
return false;
}
u->rnd_fd.cb = low_entropy_cb;
u->rnd_fd.fd = open(DEV_RANDOM, O_WRONLY);
if (u->rnd_fd.fd < 1) {
ERROR(DEV_RANDOM " open failed: %s\n", strerror(errno));
return false;
}
uloop_fd_add(&u->rnd_fd, ULOOP_READ);
return true;
}
static int usage(const char *prog)
{
fprintf(stderr, "Usage: %s [options]\n"
"Options:\n"
#ifdef URNGD_DEBUG
" -d <level> Enable debug messages\n"
#endif
" -S Print messages to stdout\n"
"\n", prog);
return 1;
}
int main(int argc, char **argv)
{
int ch;
int ulog_channels = ULOG_KMSG;
#ifdef URNGD_DEBUG
char *dbglvl = getenv("DBGLVL");
if (dbglvl) {
debug = atoi(dbglvl);
unsetenv("DBGLVL");
}
#endif
while ((ch = getopt(argc, argv, "d:S")) != -1) {
switch (ch) {
#ifdef URNGD_DEBUG
case 'd':
debug = atoi(optarg);
break;
#endif
case 'S':
ulog_channels = ULOG_STDIO;
break;
default:
return usage(argv[0]);
}
}
ulog_open(ulog_channels, LOG_DAEMON, "urngd");
if (!urngd_init(&urngd_service))
return -1;
LOG("v%s started.\n", URNGD_VERSION);
gather_entropy(&urngd_service);
uloop_init();
uloop_run();
uloop_done();
urngd_done(&urngd_service);
return 0;
}