Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / libcryptoutil / common / random.c
blobab071684098993aeefb12d1f5560bb0b513205d3
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <locale.h>
33 #include <stdarg.h>
34 #include <cryptoutil.h>
35 #include <pthread.h>
37 #pragma init(pkcs11_random_init)
39 static pthread_mutex_t random_mutex = PTHREAD_MUTEX_INITIALIZER;
40 static pthread_mutex_t urandom_mutex = PTHREAD_MUTEX_INITIALIZER;
42 static pthread_mutex_t random_seed_mutex = PTHREAD_MUTEX_INITIALIZER;
43 static pthread_mutex_t urandom_seed_mutex = PTHREAD_MUTEX_INITIALIZER;
45 #define RANDOM_DEVICE "/dev/random" /* random device name */
46 #define URANDOM_DEVICE "/dev/urandom" /* urandom device name */
48 static int random_fd = -1;
49 static int urandom_fd = -1;
51 static int random_seed_fd = -1;
52 static int urandom_seed_fd = -1;
56 * Equivalent of open(2) insulated from EINTR.
57 * Also sets close-on-exec.
59 int
60 open_nointr(const char *path, int oflag, ...)
62 int fd;
63 mode_t pmode;
64 va_list alist;
66 va_start(alist, oflag);
67 pmode = va_arg(alist, mode_t);
68 va_end(alist);
70 do {
71 if ((fd = open(path, oflag, pmode)) >= 0) {
72 (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
73 break;
75 /* errno definitely set by failed open() */
76 } while (errno == EINTR);
77 return (fd);
81 * Equivalent of read(2) insulated from EINTR.
83 ssize_t
84 readn_nointr(int fd, void *dbuf, size_t dlen)
86 char *marker = dbuf;
87 size_t left = dlen;
88 ssize_t nread = 0, err;
90 for (err = 0; left > 0 && nread != -1; marker += nread, left -= nread) {
91 if ((nread = read(fd, marker, left)) < 0) {
92 if (errno == EINTR) { /* keep trying */
93 nread = 0;
94 continue;
96 err = nread; /* hard error */
97 break;
98 } else if (nread == 0) {
99 break;
102 return (err != 0 ? err : dlen - left);
106 * Equivalent of write(2) insulated from EINTR.
108 ssize_t
109 writen_nointr(int fd, void *dbuf, size_t dlen)
111 char *marker = dbuf;
112 size_t left = dlen;
113 ssize_t nwrite = 0, err;
115 for (err = 0; left > 0 && nwrite != -1; marker += nwrite,
116 left -= nwrite) {
117 if ((nwrite = write(fd, marker, left)) < 0) {
118 if (errno == EINTR) { /* keep trying */
119 nwrite = 0;
120 continue;
122 err = nwrite; /* hard error */
123 break;
124 } else if (nwrite == 0) {
125 break;
128 return (err != 0 ? err : dlen - left);
132 * Opens the random number generator devices if not already open.
133 * Always returns the opened fd of the device, or error.
135 static int
136 pkcs11_open_common(int *fd, pthread_mutex_t *mtx, const char *dev, int oflag)
138 (void) pthread_mutex_lock(mtx);
139 if (*fd < 0)
140 *fd = open_nointr(dev, oflag);
141 (void) pthread_mutex_unlock(mtx);
143 return (*fd);
146 static int
147 pkcs11_open_random(void)
149 return (pkcs11_open_common(&random_fd, &random_mutex,
150 RANDOM_DEVICE, O_RDONLY));
153 static int
154 pkcs11_open_urandom(void)
156 return (pkcs11_open_common(&urandom_fd, &urandom_mutex,
157 URANDOM_DEVICE, O_RDONLY));
160 static int
161 pkcs11_open_random_seed(void)
163 return (pkcs11_open_common(&random_seed_fd, &random_seed_mutex,
164 RANDOM_DEVICE, O_WRONLY));
167 static int
168 pkcs11_open_urandom_seed(void)
170 return (pkcs11_open_common(&urandom_seed_fd, &urandom_seed_mutex,
171 URANDOM_DEVICE, O_WRONLY));
175 * Close the random number generator devices if already open.
177 static void
178 pkcs11_close_common(int *fd, pthread_mutex_t *mtx)
180 (void) pthread_mutex_lock(mtx);
181 (void) close(*fd);
182 *fd = -1;
183 (void) pthread_mutex_unlock(mtx);
186 static void
187 pkcs11_close_random(void)
189 pkcs11_close_common(&random_fd, &random_mutex);
192 static void
193 pkcs11_close_urandom(void)
195 pkcs11_close_common(&urandom_fd, &urandom_mutex);
198 static void
199 pkcs11_close_random_seed(void)
201 pkcs11_close_common(&random_seed_fd, &random_seed_mutex);
204 static void
205 pkcs11_close_urandom_seed(void)
207 pkcs11_close_common(&urandom_seed_fd, &urandom_seed_mutex);
211 * Read from the random number generator devices.
213 static size_t
214 pkcs11_read_common(int *fd, pthread_mutex_t *mtx, void *dbuf, size_t dlen)
216 size_t n;
218 (void) pthread_mutex_lock(mtx);
219 n = readn_nointr(*fd, dbuf, dlen);
220 (void) pthread_mutex_unlock(mtx);
222 return (n);
225 static size_t
226 pkcs11_read_random(void *dbuf, size_t dlen)
228 return (pkcs11_read_common(&random_fd, &random_mutex, dbuf, dlen));
231 static size_t
232 pkcs11_read_urandom(void *dbuf, size_t dlen)
234 return (pkcs11_read_common(&urandom_fd, &urandom_mutex, dbuf, dlen));
238 * Write to the random number generator devices.
240 static size_t
241 pkcs11_write_common(int *fd, pthread_mutex_t *mtx, void *dbuf, size_t dlen)
243 size_t n;
245 (void) pthread_mutex_lock(mtx);
246 n = writen_nointr(*fd, dbuf, dlen);
247 (void) pthread_mutex_unlock(mtx);
249 return (n);
252 static size_t
253 pkcs11_write_random_seed(void *dbuf, size_t dlen)
255 return (pkcs11_write_common(&random_seed_fd, &random_seed_mutex,
256 dbuf, dlen));
259 static size_t
260 pkcs11_write_urandom_seed(void *dbuf, size_t dlen)
262 return (pkcs11_write_common(&urandom_seed_fd, &urandom_seed_mutex,
263 dbuf, dlen));
267 * Seed /dev/random with the data in the buffer.
270 pkcs11_seed_random(void *sbuf, size_t slen)
272 int rv;
274 if (sbuf == NULL || slen == 0)
275 return (0);
277 /* Seeding error could mean it's not supported (errno = EACCES) */
278 if (pkcs11_open_random_seed() < 0)
279 return (-1);
281 rv = -1;
282 if (pkcs11_write_random_seed(sbuf, slen) == slen)
283 rv = 0;
285 pkcs11_close_random_seed();
286 return (rv);
290 * Seed /dev/urandom with the data in the buffer.
293 pkcs11_seed_urandom(void *sbuf, size_t slen)
295 int rv;
297 if (sbuf == NULL || slen == 0)
298 return (0);
300 /* Seeding error could mean it's not supported (errno = EACCES) */
301 if (pkcs11_open_urandom_seed() < 0)
302 return (-1);
304 rv = -1;
305 if (pkcs11_write_urandom_seed(sbuf, slen) == slen)
306 rv = 0;
308 pkcs11_close_urandom_seed();
309 return (rv);
313 * Put the requested amount of random data into a preallocated buffer.
314 * Good for token key data, persistent objects.
317 pkcs11_get_random(void *dbuf, size_t dlen)
319 if (dbuf == NULL || dlen == 0)
320 return (0);
322 /* Read random data directly from /dev/random */
323 if (pkcs11_open_random() < 0)
324 return (-1);
326 if (pkcs11_read_random(dbuf, dlen) == dlen)
327 return (0);
328 return (-1);
332 * Put the requested amount of random data into a preallocated buffer.
333 * Good for passphrase salts, initialization vectors.
336 pkcs11_get_urandom(void *dbuf, size_t dlen)
338 if (dbuf == NULL || dlen == 0)
339 return (0);
341 /* Read random data directly from /dev/urandom */
342 if (pkcs11_open_urandom() < 0)
343 return (-1);
345 if (pkcs11_read_urandom(dbuf, dlen) == dlen)
346 return (0);
347 return (-1);
351 * Same as pkcs11_get_urandom but ensures non zero data.
354 pkcs11_get_nzero_urandom(void *dbuf, size_t dlen)
356 char extrarand[32];
357 size_t bytesleft = 0;
358 size_t i = 0;
360 /* Start with some random data */
361 if (pkcs11_get_urandom(dbuf, dlen) < 0)
362 return (-1);
364 /* Walk through data replacing any 0 bytes with more random data */
365 while (i < dlen) {
366 if (((char *)dbuf)[i] != 0) {
367 i++;
368 continue;
371 if (bytesleft == 0) {
372 bytesleft = sizeof (extrarand);
373 if (pkcs11_get_urandom(extrarand, bytesleft) < 0)
374 return (-1);
376 bytesleft--;
378 ((char *)dbuf)[i] = extrarand[bytesleft];
380 return (0);
383 static void
384 pkcs11_random_prepare(void)
387 * NOTE - None of these are acquired more than one at a time.
388 * I can therefore acquire all four without fear of deadlock.
390 (void) pthread_mutex_lock(&random_mutex);
391 (void) pthread_mutex_lock(&urandom_mutex);
392 (void) pthread_mutex_lock(&random_seed_mutex);
393 (void) pthread_mutex_lock(&urandom_seed_mutex);
396 static void
397 pkcs11_random_parent_post(void)
399 /* Drop the mutexes and get back to work! */
400 (void) pthread_mutex_unlock(&urandom_seed_mutex);
401 (void) pthread_mutex_unlock(&random_seed_mutex);
402 (void) pthread_mutex_unlock(&urandom_mutex);
403 (void) pthread_mutex_unlock(&random_mutex);
406 static void
407 pkcs11_random_child_post(void)
409 pkcs11_random_parent_post();
411 /* Also, close the FDs, just in case. */
412 pkcs11_close_random();
413 pkcs11_close_urandom();
414 pkcs11_close_random_seed();
415 pkcs11_close_urandom_seed();
418 static void
419 pkcs11_random_init(void)
421 (void) pthread_atfork(pkcs11_random_prepare, pkcs11_random_parent_post,
422 pkcs11_random_child_post);