138a52bd3SEd Maste /*
238a52bd3SEd Maste * Copyright (c) 1996, David Mazieres <dm@uun.org>
338a52bd3SEd Maste * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
438a52bd3SEd Maste * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
538a52bd3SEd Maste *
638a52bd3SEd Maste * Permission to use, copy, modify, and distribute this software for any
738a52bd3SEd Maste * purpose with or without fee is hereby granted, provided that the above
838a52bd3SEd Maste * copyright notice and this permission notice appear in all copies.
938a52bd3SEd Maste *
1038a52bd3SEd Maste * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1138a52bd3SEd Maste * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1238a52bd3SEd Maste * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1338a52bd3SEd Maste * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1438a52bd3SEd Maste * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1538a52bd3SEd Maste * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1638a52bd3SEd Maste * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1738a52bd3SEd Maste */
1838a52bd3SEd Maste
1938a52bd3SEd Maste #include "includes.h"
2038a52bd3SEd Maste
2138a52bd3SEd Maste #ifndef SSH_RANDOM_DEV
2238a52bd3SEd Maste # define SSH_RANDOM_DEV "/dev/urandom"
2338a52bd3SEd Maste #endif /* SSH_RANDOM_DEV */
2438a52bd3SEd Maste
2538a52bd3SEd Maste #include <sys/types.h>
2638a52bd3SEd Maste #ifdef HAVE_SYS_RANDOM_H
2738a52bd3SEd Maste # include <sys/random.h>
2838a52bd3SEd Maste #endif
2938a52bd3SEd Maste
3038a52bd3SEd Maste #include <fcntl.h>
3138a52bd3SEd Maste #include <stdlib.h>
3238a52bd3SEd Maste #include <string.h>
3338a52bd3SEd Maste #include <unistd.h>
3438a52bd3SEd Maste #ifdef WITH_OPENSSL
3538a52bd3SEd Maste #include <openssl/rand.h>
3638a52bd3SEd Maste #include <openssl/err.h>
3738a52bd3SEd Maste #endif
3838a52bd3SEd Maste
3938a52bd3SEd Maste #include "log.h"
4038a52bd3SEd Maste
4138a52bd3SEd Maste int
_ssh_compat_getentropy(void * s,size_t len)4238a52bd3SEd Maste _ssh_compat_getentropy(void *s, size_t len)
4338a52bd3SEd Maste {
44*535af610SEd Maste #if defined(WITH_OPENSSL) && defined(OPENSSL_PRNG_ONLY)
4538a52bd3SEd Maste if (RAND_bytes(s, len) <= 0)
4638a52bd3SEd Maste fatal("Couldn't obtain random bytes (error 0x%lx)",
4738a52bd3SEd Maste (unsigned long)ERR_get_error());
4838a52bd3SEd Maste #else
4938a52bd3SEd Maste int fd, save_errno;
5038a52bd3SEd Maste ssize_t r;
5138a52bd3SEd Maste size_t o = 0;
5238a52bd3SEd Maste
53*535af610SEd Maste #ifdef WITH_OPENSSL
54*535af610SEd Maste if (RAND_bytes(s, len) == 1)
55*535af610SEd Maste return 0;
56*535af610SEd Maste #endif
57f374ba41SEd Maste #ifdef HAVE_GETENTROPY
584d3fc8b0SEd Maste if ((r = getentropy(s, len)) == 0)
59f374ba41SEd Maste return 0;
60f374ba41SEd Maste #endif /* HAVE_GETENTROPY */
6138a52bd3SEd Maste #ifdef HAVE_GETRANDOM
6238a52bd3SEd Maste if ((r = getrandom(s, len, 0)) > 0 && (size_t)r == len)
6338a52bd3SEd Maste return 0;
6438a52bd3SEd Maste #endif /* HAVE_GETRANDOM */
6538a52bd3SEd Maste
6638a52bd3SEd Maste if ((fd = open(SSH_RANDOM_DEV, O_RDONLY)) == -1) {
6738a52bd3SEd Maste save_errno = errno;
6838a52bd3SEd Maste /* Try egd/prngd before giving up. */
6938a52bd3SEd Maste if (seed_from_prngd(s, len) == 0)
7038a52bd3SEd Maste return 0;
7138a52bd3SEd Maste fatal("Couldn't open %s: %s", SSH_RANDOM_DEV,
7238a52bd3SEd Maste strerror(save_errno));
7338a52bd3SEd Maste }
7438a52bd3SEd Maste while (o < len) {
7538a52bd3SEd Maste r = read(fd, (u_char *)s + o, len - o);
7638a52bd3SEd Maste if (r < 0) {
7738a52bd3SEd Maste if (errno == EAGAIN || errno == EINTR ||
7838a52bd3SEd Maste errno == EWOULDBLOCK)
7938a52bd3SEd Maste continue;
8038a52bd3SEd Maste fatal("read %s: %s", SSH_RANDOM_DEV, strerror(errno));
8138a52bd3SEd Maste }
8238a52bd3SEd Maste o += r;
8338a52bd3SEd Maste }
8438a52bd3SEd Maste close(fd);
8538a52bd3SEd Maste #endif /* WITH_OPENSSL */
8638a52bd3SEd Maste return 0;
8738a52bd3SEd Maste }
88