2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
|
|
|
#if !defined(OPENSSL_WINDOWS)
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <openssl/thread.h>
|
|
|
|
#include <openssl/mem.h>
|
|
|
|
|
2015-04-13 19:04:21 +01:00
|
|
|
#include "internal.h"
|
|
|
|
#include "../internal.h"
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
/* This file implements a PRNG by reading from /dev/urandom, optionally with a
|
2015-09-03 22:18:08 +01:00
|
|
|
* buffer, which is unsafe across |fork|. */
|
|
|
|
|
|
|
|
#define BUF_SIZE 4096
|
|
|
|
|
|
|
|
/* rand_buffer contains unused, random bytes, some of which may have been
|
|
|
|
* consumed already. */
|
2014-06-20 20:00:00 +01:00
|
|
|
struct rand_buffer {
|
2015-09-03 22:18:08 +01:00
|
|
|
size_t used;
|
|
|
|
uint8_t rand[BUF_SIZE];
|
2014-06-20 20:00:00 +01:00
|
|
|
};
|
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
/* requested_lock is used to protect the |*_requested| variables. */
|
|
|
|
static struct CRYPTO_STATIC_MUTEX requested_lock = CRYPTO_STATIC_MUTEX_INIT;
|
2015-04-13 19:04:21 +01:00
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
/* urandom_fd_requested is set by |RAND_set_urandom_fd|. It's protected by
|
|
|
|
* |requested_lock|. */
|
|
|
|
static int urandom_fd_requested = -2;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
/* urandom_fd is a file descriptor to /dev/urandom. It's protected by |once|. */
|
2014-06-20 20:00:00 +01:00
|
|
|
static int urandom_fd = -2;
|
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
/* urandom_buffering_requested is set by |RAND_enable_fork_unsafe_buffering|.
|
|
|
|
* It's protected by |requested_lock|. */
|
|
|
|
static int urandom_buffering_requested = 0;
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
/* urandom_buffering controls whether buffering is enabled (1) or not (0). This
|
2015-09-03 22:18:08 +01:00
|
|
|
* is protected by |once|. */
|
2014-06-20 20:00:00 +01:00
|
|
|
static int urandom_buffering = 0;
|
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
static CRYPTO_once_t once = CRYPTO_ONCE_INIT;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
/* init_once initializes the state of this module to values previously
|
|
|
|
* requested. This is the only function that modifies |urandom_fd| and
|
|
|
|
* |urandom_buffering|, whose values may be read safely after calling the
|
|
|
|
* once. */
|
|
|
|
static void init_once(void) {
|
|
|
|
CRYPTO_STATIC_MUTEX_lock_read(&requested_lock);
|
|
|
|
urandom_buffering = urandom_buffering_requested;
|
|
|
|
int fd = urandom_fd_requested;
|
|
|
|
CRYPTO_STATIC_MUTEX_unlock(&requested_lock);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
if (fd == -2) {
|
|
|
|
do {
|
|
|
|
fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
} while (fd == -1 && errno == EINTR);
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
abort();
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-09-03 22:18:08 +01:00
|
|
|
|
|
|
|
int flags = fcntl(fd, F_GETFD);
|
|
|
|
if (flags == -1) {
|
2015-12-14 23:54:19 +00:00
|
|
|
/* Native Client doesn't implement |fcntl|. */
|
|
|
|
if (errno != ENOSYS) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
if (fcntl(fd, F_SETFD, flags) == -1) {
|
|
|
|
abort();
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-09-03 22:18:08 +01:00
|
|
|
urandom_fd = fd;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
void RAND_cleanup(void) {}
|
|
|
|
|
2015-06-30 18:52:59 +01:00
|
|
|
void RAND_set_urandom_fd(int fd) {
|
2015-09-03 22:18:08 +01:00
|
|
|
fd = dup(fd);
|
|
|
|
if (fd < 0) {
|
2015-06-30 18:52:59 +01:00
|
|
|
abort();
|
|
|
|
}
|
2015-09-03 22:18:08 +01:00
|
|
|
|
|
|
|
CRYPTO_STATIC_MUTEX_lock_write(&requested_lock);
|
|
|
|
urandom_fd_requested = fd;
|
|
|
|
CRYPTO_STATIC_MUTEX_unlock(&requested_lock);
|
|
|
|
|
|
|
|
CRYPTO_once(&once, init_once);
|
|
|
|
if (urandom_fd != fd) {
|
|
|
|
abort(); // Already initialized.
|
2015-06-30 18:52:59 +01:00
|
|
|
}
|
2015-09-03 22:18:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RAND_enable_fork_unsafe_buffering(int fd) {
|
|
|
|
if (fd >= 0) {
|
|
|
|
fd = dup(fd);
|
|
|
|
if (fd < 0) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fd = -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
CRYPTO_STATIC_MUTEX_lock_write(&requested_lock);
|
|
|
|
urandom_buffering_requested = 1;
|
|
|
|
urandom_fd_requested = fd;
|
|
|
|
CRYPTO_STATIC_MUTEX_unlock(&requested_lock);
|
|
|
|
|
|
|
|
CRYPTO_once(&once, init_once);
|
|
|
|
if (urandom_buffering != 1 || (fd >= 0 && urandom_fd != fd)) {
|
|
|
|
abort(); // Already initialized.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rand_buffer *get_thread_local_buffer(void) {
|
|
|
|
struct rand_buffer *buf =
|
|
|
|
CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF);
|
|
|
|
if (buf != NULL) {
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = OPENSSL_malloc(sizeof(struct rand_buffer));
|
|
|
|
if (buf == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buf->used = BUF_SIZE; /* To trigger a |read_full| on first use. */
|
|
|
|
if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_URANDOM_BUF, buf,
|
|
|
|
OPENSSL_free)) {
|
|
|
|
OPENSSL_free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
2015-06-30 18:52:59 +01:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
/* read_full reads exactly |len| bytes from |fd| into |out| and returns 1. In
|
|
|
|
* the case of an error it returns 0. */
|
|
|
|
static char read_full(int fd, uint8_t *out, size_t len) {
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
do {
|
|
|
|
r = read(fd, out, len);
|
|
|
|
} while (r == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (r <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
out += r;
|
|
|
|
len -= r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
/* read_from_buffer reads |requested| random bytes from the buffer into |out|,
|
|
|
|
* refilling it if necessary to satisfy the request. */
|
|
|
|
static void read_from_buffer(struct rand_buffer *buf,
|
|
|
|
uint8_t *out, size_t requested) {
|
|
|
|
size_t remaining = BUF_SIZE - buf->used;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
while (requested > remaining) {
|
|
|
|
memcpy(out, &buf->rand[buf->used], remaining);
|
|
|
|
buf->used += remaining;
|
|
|
|
out += remaining;
|
|
|
|
requested -= remaining;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
if (!read_full(urandom_fd, buf->rand, BUF_SIZE)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
abort();
|
2015-04-13 19:04:21 +01:00
|
|
|
return;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-09-03 22:18:08 +01:00
|
|
|
buf->used = 0;
|
|
|
|
remaining = BUF_SIZE;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
memcpy(out, &buf->rand[buf->used], requested);
|
|
|
|
buf->used += requested;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
/* CRYPTO_sysrand puts |requested| random bytes into |out|. */
|
|
|
|
void CRYPTO_sysrand(uint8_t *out, size_t requested) {
|
|
|
|
if (requested == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
CRYPTO_once(&once, init_once);
|
|
|
|
if (urandom_buffering && requested < BUF_SIZE) {
|
|
|
|
struct rand_buffer *buf = get_thread_local_buffer();
|
|
|
|
if (buf != NULL) {
|
|
|
|
read_from_buffer(buf, out, requested);
|
2015-04-13 19:04:21 +01:00
|
|
|
return;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 22:18:08 +01:00
|
|
|
if (!read_full(urandom_fd, out, requested)) {
|
|
|
|
abort();
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !OPENSSL_WINDOWS */
|