forked from nhorman/rng-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rngd.h
231 lines (202 loc) · 4.46 KB
/
rngd.h
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
/*
* rngd.h -- rngd globals
*
* Copyright (C) 2001 Philipp Rumpf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*/
#ifndef RNGD__H
#define RNGD__H
#define _GNU_SOURCE
#include "rng-tools-config.h"
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <syslog.h>
#include "fips.h"
#define NSECS_IN_SECOND 1.0e9
#define MEGABITS 1048576
#define KILOBITS 1024
enum {
MAX_RNG_FAILURES = 25,
RNG_OK_CREDIT = 1000, /* ~1:1250 false positives */
};
/* Command line arguments and processing */
struct arguments {
char *random_name;
char *pid_file;
int random_step;
int fill_watermark;
bool debug;
bool daemon;
bool test;
bool list;
bool ignorefail;
bool enable_drng;
bool enable_tpm;
int entropy_count;
int force_reseed;
bool use_slow_sources;
bool drop_privs;
uid_t drop_uid;
gid_t drop_gid;
};
extern struct arguments *arguments;
/*
* DRNG (RDRAND) Options
*/
enum {
DRNG_OPT_AES = 0,
DRNG_OPT_MAX,
};
/*
* DARN Options
*/
enum {
DARN_OPT_AES = 0,
DARN_OPT_MAX,
};
/*
* JITTER options
*/
enum {
JITTER_OPT_THREADS = 0,
JITTER_OPT_BUF_SZ = 1,
JITTER_OPT_REFILL = 2,
JITTER_OPT_RETRY_COUNT = 3,
JITTER_OPT_RETRY_DELAY = 4,
JITTER_OPT_USE_AES = 5,
JITTER_OPT_FORCE_INT_TIMER = 6,
JITTER_OPT_TIMEOUT = 7,
JITTER_OPT_MAX,
};
/*
* PKCS11 options
*/
enum {
PKCS11_OPT_ENGINE = 0,
PKCS11_OPT_CHUNK = 1,
};
/*
* NIST options
*/
enum {
NIST_OPT_USE_AES = 0,
NIST_OPT_MAX,
};
/*
* RTLSDR options
*/
enum {
RTLSDR_OPT_DEVID = 0,
RTLSDR_OPT_FREQ_MIN = 1,
RTLSDR_OPT_FREQ_MAX = 2,
RTLSDR_OPT_SRATE_MIN = 3,
RTLSDR_OPT_SRATE_MAX = 4,
RTLSDR_OPT_MAX,
};
/*
* QRYPT options
*/
enum {
QRYPT_OPT_TOKEN_FILE = 0,
QRYPT_OPT_MAX_ERROR_DELAY = 1,
QRYPT_OPT_MAX,
};
/*
* NAMEDPIPE options
*/
enum {
NAMEDPIPE_OPT_PATH = 0,
NAMEDPIPE_OPT_TIMEOUT = 1,
NAMEDPIPE_OPT_MAX,
};
enum option_val_type {
VAL_INT = 0,
VAL_STRING = 1,
};
struct rng_option {
char *key;
enum option_val_type type;
union {
int int_val;
char *str_val;
};
};
/* structures to store rng information */
struct rng {
char *rng_name;
char *rng_sname;
char *rng_fname;
int rng_fd;
bool disabled;
bool failed_init;
int failures;
int success;
size_t ent_gathered;
struct flags {
/* Slow sources - takes a long time to produce entropy */
unsigned int slow_source : 1;
/* Intermittent sources - may sometimes fail to produce entropy */
unsigned int intermittent_source : 1;
} flags;
struct entropy_buf {
/* structure to store entropy from a source before mixing it with other sources */
unsigned char entropy[FIPS_RNG_BUFFER_SIZE];
bool valid;
int used_pos;
} entropy_buf;
int (*xread) (void *buf, size_t size, struct rng *ent_src);
int (*init) (struct rng *ent_src);
void (*close) (struct rng *end_src);
fips_ctx_t *fipsctx;
struct rng_option *rng_options;
};
/* Background/daemon mode */
extern bool am_daemon; /* True if we went daemon */
extern bool msg_squash;
extern bool quiet;
/*
* Routines and macros
*/
#define message(priority,fmt,args...) do { \
if (quiet) \
break;\
if (arguments->debug == false && LOG_PRI(priority) == LOG_DEBUG) \
break;\
if (am_daemon) { \
syslog((priority), fmt, ##args); \
} else if (!msg_squash) { \
fprintf(stderr, fmt, ##args); \
fflush(stderr); \
} \
} while (0)
#define message_entsrc(src, priority, fmt, args...) do { \
if (quiet) \
break; \
size_t ____neededpfx = snprintf(NULL, 0, "[%-6s]: ", src->rng_sname); \
size_t ____neededmsg = snprintf(NULL, 0, fmt, ##args) + 1; \
char *____buf = malloc(____neededpfx + ____neededmsg); \
if (____buf) { \
sprintf(____buf, "[%-6s]: " fmt, src->rng_sname, ##args); \
message(priority, "%s", ____buf); \
free(____buf); \
} \
} while (0)
extern bool do_reseed;
extern volatile bool server_running;
extern int write_pid_file(const char *pid_fn);
#endif /* RNGD__H */