Blame


1 f9ab77a8 2023-08-23 op /*
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>
5 f9ab77a8 2023-08-23 op *
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.
9 f9ab77a8 2023-08-23 op *
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.
17 f9ab77a8 2023-08-23 op */
18 f9ab77a8 2023-08-23 op
19 f9ab77a8 2023-08-23 op #include "../config.h"
20 f9ab77a8 2023-08-23 op
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 */
24 f9ab77a8 2023-08-23 op
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>
28 f9ab77a8 2023-08-23 op #endif
29 f9ab77a8 2023-08-23 op
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>
38 f9ab77a8 2023-08-23 op #endif
39 f9ab77a8 2023-08-23 op
40 f9ab77a8 2023-08-23 op #include "log.h"
41 f9ab77a8 2023-08-23 op
42 f9ab77a8 2023-08-23 op int _ssh_compat_getentropy(void *, size_t);
43 f9ab77a8 2023-08-23 op
44 f9ab77a8 2023-08-23 op static int
45 f9ab77a8 2023-08-23 op seed_from_prngd(unsigned char *buf, size_t bytes)
46 f9ab77a8 2023-08-23 op {
47 f9ab77a8 2023-08-23 op return -1;
48 f9ab77a8 2023-08-23 op }
49 f9ab77a8 2023-08-23 op
50 f9ab77a8 2023-08-23 op int
51 f9ab77a8 2023-08-23 op _ssh_compat_getentropy(void *s, size_t len)
52 f9ab77a8 2023-08-23 op {
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());
57 f9ab77a8 2023-08-23 op #else
58 f9ab77a8 2023-08-23 op int fd, save_errno;
59 f9ab77a8 2023-08-23 op ssize_t r;
60 f9ab77a8 2023-08-23 op size_t o = 0;
61 f9ab77a8 2023-08-23 op
62 f9ab77a8 2023-08-23 op #ifdef WITH_OPENSSL
63 f9ab77a8 2023-08-23 op if (RAND_bytes(s, len) == 1)
64 f9ab77a8 2023-08-23 op return 0;
65 f9ab77a8 2023-08-23 op #endif
66 f9ab77a8 2023-08-23 op #ifdef HAVE_GETENTROPY
67 f9ab77a8 2023-08-23 op if ((r = getentropy(s, len)) == 0)
68 f9ab77a8 2023-08-23 op return 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)
72 f9ab77a8 2023-08-23 op return 0;
73 f9ab77a8 2023-08-23 op #endif /* HAVE_GETRANDOM */
74 f9ab77a8 2023-08-23 op
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)
79 f9ab77a8 2023-08-23 op return 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));
82 f9ab77a8 2023-08-23 op }
83 f9ab77a8 2023-08-23 op while (o < len) {
84 f9ab77a8 2023-08-23 op r = read(fd, (u_char *)s + o, len - o);
85 f9ab77a8 2023-08-23 op if (r < 0) {
86 f9ab77a8 2023-08-23 op if (errno == EAGAIN || errno == EINTR ||
87 f9ab77a8 2023-08-23 op errno == EWOULDBLOCK)
88 f9ab77a8 2023-08-23 op continue;
89 f9ab77a8 2023-08-23 op fatal("read %s: %s", SSH_RANDOM_DEV, strerror(errno));
90 f9ab77a8 2023-08-23 op }
91 f9ab77a8 2023-08-23 op o += r;
92 f9ab77a8 2023-08-23 op }
93 f9ab77a8 2023-08-23 op close(fd);
94 f9ab77a8 2023-08-23 op #endif /* WITH_OPENSSL */
95 f9ab77a8 2023-08-23 op return 0;
96 f9ab77a8 2023-08-23 op }