kernel - Reduce stalls, refactor lwkt_switch() core.
[dragonfly.git] / crypto / openssh / entropy.c
blob1e9d52ac4605cdec061ff61cd3b4e19aa4c77bcc
1 /*
2 * Copyright (c) 2001 Damien Miller. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include "includes.h"
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #ifdef HAVE_SYS_UN_H
30 # include <sys/un.h>
31 #endif
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stddef.h> /* for offsetof */
42 #include <openssl/rand.h>
43 #include <openssl/crypto.h>
44 #include <openssl/err.h>
46 #include "openbsd-compat/openssl-compat.h"
48 #include "ssh.h"
49 #include "misc.h"
50 #include "xmalloc.h"
51 #include "atomicio.h"
52 #include "pathnames.h"
53 #include "log.h"
54 #include "buffer.h"
57 * Portable OpenSSH PRNG seeding:
58 * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
59 * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
60 * PRNGd.
62 #ifndef OPENSSL_PRNG_ONLY
64 #define RANDOM_SEED_SIZE 48
67 * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
68 * listening either on 'tcp_port', or via Unix domain socket at *
69 * 'socket_path'.
70 * Either a non-zero tcp_port or a non-null socket_path must be
71 * supplied.
72 * Returns 0 on success, -1 on error
74 int
75 get_random_bytes_prngd(unsigned char *buf, int len,
76 unsigned short tcp_port, char *socket_path)
78 int fd, addr_len, rval, errors;
79 u_char msg[2];
80 struct sockaddr_storage addr;
81 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
82 struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
83 mysig_t old_sigpipe;
85 /* Sanity checks */
86 if (socket_path == NULL && tcp_port == 0)
87 fatal("You must specify a port or a socket");
88 if (socket_path != NULL &&
89 strlen(socket_path) >= sizeof(addr_un->sun_path))
90 fatal("Random pool path is too long");
91 if (len <= 0 || len > 255)
92 fatal("Too many bytes (%d) to read from PRNGD", len);
94 memset(&addr, '\0', sizeof(addr));
96 if (tcp_port != 0) {
97 addr_in->sin_family = AF_INET;
98 addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
99 addr_in->sin_port = htons(tcp_port);
100 addr_len = sizeof(*addr_in);
101 } else {
102 addr_un->sun_family = AF_UNIX;
103 strlcpy(addr_un->sun_path, socket_path,
104 sizeof(addr_un->sun_path));
105 addr_len = offsetof(struct sockaddr_un, sun_path) +
106 strlen(socket_path) + 1;
109 old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
111 errors = 0;
112 rval = -1;
113 reopen:
114 fd = socket(addr.ss_family, SOCK_STREAM, 0);
115 if (fd == -1) {
116 error("Couldn't create socket: %s", strerror(errno));
117 goto done;
120 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
121 if (tcp_port != 0) {
122 error("Couldn't connect to PRNGD port %d: %s",
123 tcp_port, strerror(errno));
124 } else {
125 error("Couldn't connect to PRNGD socket \"%s\": %s",
126 addr_un->sun_path, strerror(errno));
128 goto done;
131 /* Send blocking read request to PRNGD */
132 msg[0] = 0x02;
133 msg[1] = len;
135 if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
136 if (errno == EPIPE && errors < 10) {
137 close(fd);
138 errors++;
139 goto reopen;
141 error("Couldn't write to PRNGD socket: %s",
142 strerror(errno));
143 goto done;
146 if (atomicio(read, fd, buf, len) != (size_t)len) {
147 if (errno == EPIPE && errors < 10) {
148 close(fd);
149 errors++;
150 goto reopen;
152 error("Couldn't read from PRNGD socket: %s",
153 strerror(errno));
154 goto done;
157 rval = 0;
158 done:
159 mysignal(SIGPIPE, old_sigpipe);
160 if (fd != -1)
161 close(fd);
162 return rval;
165 static int
166 seed_from_prngd(unsigned char *buf, size_t bytes)
168 #ifdef PRNGD_PORT
169 debug("trying egd/prngd port %d", PRNGD_PORT);
170 if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
171 return 0;
172 #endif
173 #ifdef PRNGD_SOCKET
174 debug("trying egd/prngd socket %s", PRNGD_SOCKET);
175 if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
176 return 0;
177 #endif
178 return -1;
181 void
182 rexec_send_rng_seed(Buffer *m)
184 u_char buf[RANDOM_SEED_SIZE];
186 if (RAND_bytes(buf, sizeof(buf)) <= 0) {
187 error("Couldn't obtain random bytes (error %ld)",
188 ERR_get_error());
189 buffer_put_string(m, "", 0);
190 } else
191 buffer_put_string(m, buf, sizeof(buf));
194 void
195 rexec_recv_rng_seed(Buffer *m)
197 u_char *buf;
198 u_int len;
200 buf = buffer_get_string_ret(m, &len);
201 if (buf != NULL) {
202 debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len);
203 RAND_add(buf, len, len);
206 #endif /* OPENSSL_PRNG_ONLY */
208 void
209 seed_rng(void)
211 #ifndef OPENSSL_PRNG_ONLY
212 unsigned char buf[RANDOM_SEED_SIZE];
213 #endif
214 if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER, SSLeay()))
215 fatal("OpenSSL version mismatch. Built against %lx, you "
216 "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
218 #ifndef OPENSSL_PRNG_ONLY
219 if (RAND_status() == 1) {
220 debug3("RNG is ready, skipping seeding");
221 return;
224 if (seed_from_prngd(buf, sizeof(buf)) == -1)
225 fatal("Could not obtain seed from PRNGd");
226 RAND_add(buf, sizeof(buf), sizeof(buf));
227 memset(buf, '\0', sizeof(buf));
229 #endif /* OPENSSL_PRNG_ONLY */
230 if (RAND_status() != 1)
231 fatal("PRNG is not seeded");