-
Notifications
You must be signed in to change notification settings - Fork 10
/
patch
71 lines (67 loc) · 1.67 KB
/
patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
diff --git a/openbsd-compat/arc4random.c b/openbsd-compat/arc4random.c
index 80ff3c1..81b9151 100644
--- a/openbsd-compat/arc4random.c
+++ b/openbsd-compat/arc4random.c
@@ -83,56 +83,31 @@ _rs_init(u_char *buf, size_t n)
chacha_ivsetup(&rs, buf + KEYSZ);
}
-#ifndef WITH_OPENSSL
+#include <time.h>
# ifndef SSH_RANDOM_DEV
# define SSH_RANDOM_DEV "/dev/urandom"
# endif /* SSH_RANDOM_DEV */
static void
getrnd(u_char *s, size_t len)
{
- int fd, save_errno;
- ssize_t r;
- size_t o = 0;
-
-#ifdef HAVE_GETRANDOM
- if ((r = getrandom(s, len, 0)) > 0 && (size_t)r == len)
- return;
-#endif /* HAVE_GETRANDOM */
-
- if ((fd = open(SSH_RANDOM_DEV, O_RDONLY)) == -1) {
- save_errno = errno;
- /* Try egd/prngd before giving up. */
- if (seed_from_prngd(s, len) == 0)
- return;
- fatal("Couldn't open %s: %s", SSH_RANDOM_DEV,
- strerror(save_errno));
- }
- while (o < len) {
- r = read(fd, s + o, len - o);
- if (r < 0) {
- if (errno == EAGAIN || errno == EINTR ||
- errno == EWOULDBLOCK)
- continue;
- fatal("read %s: %s", SSH_RANDOM_DEV, strerror(errno));
- }
- o += r;
+ char state[32];
+ struct random_data rdata;
+ int32_t x;
+
+ rdata.state = NULL;
+ initstate_r(time(NULL) ^ getpid(), state, sizeof(state), &rdata);
+ for (int i = 0; i < len; i++) {
+ random_r(&rdata, &x);
+ ((unsigned char *)s)[i] = x & 0xff;
}
- close(fd);
}
-#endif /* WITH_OPENSSL */
static void
_rs_stir(void)
{
u_char rnd[KEYSZ + IVSZ];
-#ifdef WITH_OPENSSL
- if (RAND_bytes(rnd, sizeof(rnd)) <= 0)
- fatal("Couldn't obtain random bytes (error 0x%lx)",
- (unsigned long)ERR_get_error());
-#else
getrnd(rnd, sizeof(rnd));
-#endif
if (!rs_initialized) {
rs_initialized = 1;