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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <locale.h>
#include <stdarg.h>
#include <cryptoutil.h>
#include <pthread.h>
static pthread_mutex_t random_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t urandom_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t random_seed_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t urandom_seed_mutex = PTHREAD_MUTEX_INITIALIZER;
#define RANDOM_DEVICE "/dev/random" /* random device name */
#define URANDOM_DEVICE "/dev/urandom" /* urandom device name */
static int random_fd = -1;
static int urandom_fd = -1;
static int random_seed_fd = -1;
static int urandom_seed_fd = -1;
/*
* Equivalent of open(2) insulated from EINTR.
* Also sets close-on-exec.
*/
int
open_nointr(const char *path, int oflag, ...)
{
int fd;
mode_t pmode;
va_list alist;
va_start(alist, oflag);
pmode = va_arg(alist, mode_t);
va_end(alist);
do {
if ((fd = open(path, oflag, pmode)) >= 0) {
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
break;
}
/* errno definitely set by failed open() */
} while (errno == EINTR);
return (fd);
}
/*
* Equivalent of read(2) insulated from EINTR.
*/
ssize_t
readn_nointr(int fd, void *dbuf, size_t dlen)
{
char *marker = dbuf;
size_t left = dlen;
ssize_t nread = 0, err;
for (err = 0; left > 0 && nread != -1; marker += nread, left -= nread) {
if ((nread = read(fd, marker, left)) < 0) {
if (errno == EINTR) { /* keep trying */
nread = 0;
continue;
}
err = nread; /* hard error */
break;
} else if (nread == 0) {
break;
}
}
return (err != 0 ? err : dlen - left);
}
/*
* Equivalent of write(2) insulated from EINTR.
*/
ssize_t
writen_nointr(int fd, void *dbuf, size_t dlen)
{
char *marker = dbuf;
size_t left = dlen;
ssize_t nwrite = 0, err;
for (err = 0; left > 0 && nwrite != -1; marker += nwrite,
left -= nwrite) {
if ((nwrite = write(fd, marker, left)) < 0) {
if (errno == EINTR) { /* keep trying */
nwrite = 0;
continue;
}
err = nwrite; /* hard error */
break;
} else if (nwrite == 0) {
break;
}
}
return (err != 0 ? err : dlen - left);
}
/*
* Opens the random number generator devices if not already open.
* Always returns the opened fd of the device, or error.
*/
static int
pkcs11_open_common(int *fd, pthread_mutex_t *mtx, const char *dev, int oflag)
{
(void) pthread_mutex_lock(mtx);
if (*fd < 0)
*fd = open_nointr(dev, oflag);
(void) pthread_mutex_unlock(mtx);
return (*fd);
}
static int
pkcs11_open_random(void)
{
return (pkcs11_open_common(&random_fd, &random_mutex,
RANDOM_DEVICE, O_RDONLY));
}
static int
pkcs11_open_urandom(void)
{
return (pkcs11_open_common(&urandom_fd, &urandom_mutex,
URANDOM_DEVICE, O_RDONLY));
}
static int
pkcs11_open_random_seed(void)
{
return (pkcs11_open_common(&random_seed_fd, &random_seed_mutex,
RANDOM_DEVICE, O_WRONLY));
}
static int
pkcs11_open_urandom_seed(void)
{
return (pkcs11_open_common(&urandom_seed_fd, &urandom_seed_mutex,
URANDOM_DEVICE, O_WRONLY));
}
/*
* Close the random number generator devices if already open.
*/
static void
pkcs11_close_common(int *fd, pthread_mutex_t *mtx)
{
(void) pthread_mutex_lock(mtx);
(void) close(*fd);
*fd = -1;
(void) pthread_mutex_unlock(mtx);
}
void
pkcs11_close_random(void)
{
pkcs11_close_common(&random_fd, &random_mutex);
}
void
pkcs11_close_urandom(void)
{
pkcs11_close_common(&urandom_fd, &urandom_mutex);
}
static void
pkcs11_close_random_seed(void)
{
pkcs11_close_common(&random_seed_fd, &random_seed_mutex);
}
void
pkcs11_close_urandom_seed(void)
{
pkcs11_close_common(&urandom_seed_fd, &urandom_seed_mutex);
}
/*
* Read from the random number generator devices.
*/
static size_t
pkcs11_read_common(int *fd, pthread_mutex_t *mtx, void *dbuf, size_t dlen)
{
size_t n;
(void) pthread_mutex_lock(mtx);
n = readn_nointr(*fd, dbuf, dlen);
(void) pthread_mutex_unlock(mtx);
return (n);
}
static size_t
pkcs11_read_random(void *dbuf, size_t dlen)
{
return (pkcs11_read_common(&random_fd, &random_mutex, dbuf, dlen));
}
static size_t
pkcs11_read_urandom(void *dbuf, size_t dlen)
{
return (pkcs11_read_common(&urandom_fd, &urandom_mutex, dbuf, dlen));
}
/*
* Write to the random number generator devices.
*/
static size_t
pkcs11_write_common(int *fd, pthread_mutex_t *mtx, void *dbuf, size_t dlen)
{
size_t n;
(void) pthread_mutex_lock(mtx);
n = writen_nointr(*fd, dbuf, dlen);
(void) pthread_mutex_unlock(mtx);
return (n);
}
static size_t
pkcs11_write_random_seed(void *dbuf, size_t dlen)
{
return (pkcs11_write_common(&random_seed_fd, &random_seed_mutex,
dbuf, dlen));
}
static size_t
pkcs11_write_urandom_seed(void *dbuf, size_t dlen)
{
return (pkcs11_write_common(&urandom_seed_fd, &urandom_seed_mutex,
dbuf, dlen));
}
/*
* Seed /dev/random with the data in the buffer.
*/
int
pkcs11_seed_random(void *sbuf, size_t slen)
{
int rv;
if (sbuf == NULL || slen == 0)
return (0);
/* Seeding error could mean it's not supported (errno = EACCES) */
if (pkcs11_open_random_seed() < 0)
return (-1);
rv = -1;
if (pkcs11_write_random_seed(sbuf, slen) == slen)
rv = 0;
pkcs11_close_random_seed();
return (rv);
}
/*
* Seed /dev/urandom with the data in the buffer.
*/
int
pkcs11_seed_urandom(void *sbuf, size_t slen)
{
int rv;
if (sbuf == NULL || slen == 0)
return (0);
/* Seeding error could mean it's not supported (errno = EACCES) */
if (pkcs11_open_urandom_seed() < 0)
return (-1);
rv = -1;
if (pkcs11_write_urandom_seed(sbuf, slen) == slen)
rv = 0;
pkcs11_close_urandom_seed();
return (rv);
}
/*
* Put the requested amount of random data into a preallocated buffer.
* Good for token key data, persistent objects.
*/
int
pkcs11_get_random(void *dbuf, size_t dlen)
{
if (dbuf == NULL || dlen == 0)
return (0);
/* Read random data directly from /dev/random */
if (pkcs11_open_random() < 0)
return (-1);
if (pkcs11_read_random(dbuf, dlen) == dlen)
return (0);
return (-1);
}
/*
* Put the requested amount of random data into a preallocated buffer.
* Good for passphrase salts, initialization vectors.
*/
int
pkcs11_get_urandom(void *dbuf, size_t dlen)
{
if (dbuf == NULL || dlen == 0)
return (0);
/* Read random data directly from /dev/urandom */
if (pkcs11_open_urandom() < 0)
return (-1);
if (pkcs11_read_urandom(dbuf, dlen) == dlen)
return (0);
return (-1);
}
/*
* Same as pkcs11_get_urandom but ensures non zero data.
*/
int
pkcs11_get_nzero_urandom(void *dbuf, size_t dlen)
{
char extrarand[32];
size_t bytesleft = 0;
size_t i = 0;
/* Start with some random data */
if (pkcs11_get_urandom(dbuf, dlen) < 0)
return (-1);
/* Walk through data replacing any 0 bytes with more random data */
while (i < dlen) {
if (((char *)dbuf)[i] != 0) {
i++;
continue;
}
if (bytesleft == 0) {
bytesleft = sizeof (extrarand);
if (pkcs11_get_urandom(extrarand, bytesleft) < 0)
return (-1);
}
bytesleft--;
((char *)dbuf)[i] = extrarand[bytesleft];
}
return (0);
}
|