summaryrefslogtreecommitdiff
path: root/bsd-misc.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2000-01-29 20:40:22 +1100
committerDamien Miller <djm@mindrot.org>2000-01-29 20:40:22 +1100
commitf07390e90da683fecbf55849a3cee6dc9b79a3e3 (patch)
treec9c7ad28557e08ff024da1e9a5302fc78d4de4f7 /bsd-misc.c
parent4e61b79d5bcb3c5ac3014fe55be55214e23b2927 (diff)
- Seed OpenSSL's random number generator before generating RSA keypairs
- Split random collector into seperate file
Diffstat (limited to 'bsd-misc.c')
-rw-r--r--bsd-misc.c62
1 files changed, 2 insertions, 60 deletions
diff --git a/bsd-misc.c b/bsd-misc.c
index b00c793c0..99fe29816 100644
--- a/bsd-misc.c
+++ b/bsd-misc.c
@@ -44,8 +44,6 @@
44 44
45#include <sys/types.h> 45#include <sys/types.h>
46#include <sys/stat.h> 46#include <sys/stat.h>
47#include <sys/socket.h>
48#include <sys/un.h>
49#include <fcntl.h> 47#include <fcntl.h>
50#ifdef HAVE_STDDEF_H 48#ifdef HAVE_STDDEF_H
51#include <stddef.h> 49#include <stddef.h>
@@ -54,10 +52,7 @@
54#include "xmalloc.h" 52#include "xmalloc.h"
55#include "ssh.h" 53#include "ssh.h"
56#include "bsd-misc.h" 54#include "bsd-misc.h"
57 55#include "random.h"
58#ifndef offsetof
59#define offsetof(type, member) ((size_t) &((type *)0)->member)
60#endif
61 56
62#ifndef HAVE_ARC4RANDOM 57#ifndef HAVE_ARC4RANDOM
63 58
@@ -68,7 +63,6 @@ typedef struct
68 int j; 63 int j;
69} rc4_t; 64} rc4_t;
70 65
71void get_random_bytes(unsigned char *buf, int len);
72void rc4_key(rc4_t *r, unsigned char *key, int len); 66void rc4_key(rc4_t *r, unsigned char *key, int len);
73void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len); 67void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len);
74 68
@@ -134,59 +128,7 @@ void arc4random_stir(void)
134 128
135 get_random_bytes(rand_buf, sizeof(rand_buf)); 129 get_random_bytes(rand_buf, sizeof(rand_buf));
136 rc4_key(rc4, rand_buf, sizeof(rand_buf)); 130 rc4_key(rc4, rand_buf, sizeof(rand_buf));
137} 131 memset(rand_buf, 0, sizeof(rand_buf));
138
139void get_random_bytes(unsigned char *buf, int len)
140{
141 static int random_pool;
142 int c;
143#ifdef HAVE_EGD
144 char egd_message[2] = { 0x02, 0x00 };
145 struct sockaddr_un addr;
146 int addr_len;
147
148 memset(&addr, '\0', sizeof(addr));
149 addr.sun_family = AF_UNIX;
150
151 /* FIXME: compile time check? */
152 if (sizeof(RANDOM_POOL) > sizeof(addr.sun_path))
153 fatal("Random pool path is too long");
154
155 strcpy(addr.sun_path, RANDOM_POOL);
156
157 addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(RANDOM_POOL);
158
159 random_pool = socket(AF_UNIX, SOCK_STREAM, 0);
160
161 if (random_pool == -1)
162 fatal("Couldn't create AF_UNIX socket: %s", strerror(errno));
163
164 if (connect(random_pool, (struct sockaddr*)&addr, addr_len) == -1)
165 fatal("Couldn't connect to EGD socket \"%s\": %s", addr.sun_path, strerror(errno));
166
167 if (len > 255)
168 fatal("Too many bytes to read from EGD");
169
170 /* Send blocking read request to EGD */
171 egd_message[1] = len;
172
173 c = atomicio(write, random_pool, egd_message, sizeof(egd_message));
174 if (c == -1)
175 fatal("Couldn't write to EGD socket \"%s\": %s", RANDOM_POOL, strerror(errno));
176
177#else /* HAVE_EGD */
178
179 random_pool = open(RANDOM_POOL, O_RDONLY);
180 if (random_pool == -1)
181 fatal("Couldn't open random pool \"%s\": %s", RANDOM_POOL, strerror(errno));
182
183#endif /* HAVE_EGD */
184
185 c = atomicio(read, random_pool, buf, len);
186 if (c <= 0)
187 fatal("Couldn't read from random pool \"%s\": %s", RANDOM_POOL, strerror(errno));
188
189 close(random_pool);
190} 132}
191#endif /* !HAVE_ARC4RANDOM */ 133#endif /* !HAVE_ARC4RANDOM */
192 134