2 f9ab77a8 2023-08-23 op * Copyright (c) 1996, David Mazieres <dm@uun.org>
3 f9ab77a8 2023-08-23 op * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
4 f9ab77a8 2023-08-23 op * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
6 f9ab77a8 2023-08-23 op * Permission to use, copy, modify, and distribute this software for any
7 f9ab77a8 2023-08-23 op * purpose with or without fee is hereby granted, provided that the above
8 f9ab77a8 2023-08-23 op * copyright notice and this permission notice appear in all copies.
10 f9ab77a8 2023-08-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 f9ab77a8 2023-08-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 f9ab77a8 2023-08-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 f9ab77a8 2023-08-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 f9ab77a8 2023-08-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 f9ab77a8 2023-08-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 f9ab77a8 2023-08-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 f9ab77a8 2023-08-23 op #include "../config.h"
21 f9ab77a8 2023-08-23 op #ifndef SSH_RANDOM_DEV
22 f9ab77a8 2023-08-23 op # define SSH_RANDOM_DEV "/dev/urandom"
23 f9ab77a8 2023-08-23 op #endif /* SSH_RANDOM_DEV */
25 f9ab77a8 2023-08-23 op #include <sys/types.h>
26 f9ab77a8 2023-08-23 op #ifdef HAVE_SYS_RANDOM_H
27 f9ab77a8 2023-08-23 op # include <sys/random.h>
30 f9ab77a8 2023-08-23 op #include <errno.h>
31 f9ab77a8 2023-08-23 op #include <fcntl.h>
32 f9ab77a8 2023-08-23 op #include <stdlib.h>
33 f9ab77a8 2023-08-23 op #include <string.h>
34 f9ab77a8 2023-08-23 op #include <unistd.h>
35 f9ab77a8 2023-08-23 op #ifdef WITH_OPENSSL
36 f9ab77a8 2023-08-23 op #include <openssl/rand.h>
37 f9ab77a8 2023-08-23 op #include <openssl/err.h>
40 f9ab77a8 2023-08-23 op #include "log.h"
42 f9ab77a8 2023-08-23 op int _ssh_compat_getentropy(void *, size_t);
45 f9ab77a8 2023-08-23 op seed_from_prngd(unsigned char *buf, size_t bytes)
51 f9ab77a8 2023-08-23 op _ssh_compat_getentropy(void *s, size_t len)
53 f9ab77a8 2023-08-23 op #if defined(WITH_OPENSSL) && defined(OPENSSL_PRNG_ONLY)
54 f9ab77a8 2023-08-23 op if (RAND_bytes(s, len) <= 0)
55 f9ab77a8 2023-08-23 op fatal("Couldn't obtain random bytes (error 0x%lx)",
56 f9ab77a8 2023-08-23 op (unsigned long)ERR_get_error());
58 f9ab77a8 2023-08-23 op int fd, save_errno;
62 f9ab77a8 2023-08-23 op #ifdef WITH_OPENSSL
63 f9ab77a8 2023-08-23 op if (RAND_bytes(s, len) == 1)
66 f9ab77a8 2023-08-23 op #ifdef HAVE_GETENTROPY
67 f9ab77a8 2023-08-23 op if ((r = getentropy(s, len)) == 0)
69 f9ab77a8 2023-08-23 op #endif /* HAVE_GETENTROPY */
70 f9ab77a8 2023-08-23 op #ifdef HAVE_GETRANDOM
71 f9ab77a8 2023-08-23 op if ((r = getrandom(s, len, 0)) > 0 && (size_t)r == len)
73 f9ab77a8 2023-08-23 op #endif /* HAVE_GETRANDOM */
75 f9ab77a8 2023-08-23 op if ((fd = open(SSH_RANDOM_DEV, O_RDONLY)) == -1) {
76 f9ab77a8 2023-08-23 op save_errno = errno;
77 f9ab77a8 2023-08-23 op /* Try egd/prngd before giving up. */
78 f9ab77a8 2023-08-23 op if (seed_from_prngd(s, len) == 0)
80 f9ab77a8 2023-08-23 op fatal("Couldn't open %s: %s", SSH_RANDOM_DEV,
81 f9ab77a8 2023-08-23 op strerror(save_errno));
83 f9ab77a8 2023-08-23 op while (o < len) {
84 f9ab77a8 2023-08-23 op r = read(fd, (u_char *)s + o, len - o);
86 f9ab77a8 2023-08-23 op if (errno == EAGAIN || errno == EINTR ||
87 f9ab77a8 2023-08-23 op errno == EWOULDBLOCK)
89 f9ab77a8 2023-08-23 op fatal("read %s: %s", SSH_RANDOM_DEV, strerror(errno));
94 f9ab77a8 2023-08-23 op #endif /* WITH_OPENSSL */