summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rand/rand_egd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/rand/rand_egd.c')
-rw-r--r--src/lib/libcrypto/rand/rand_egd.c224
1 files changed, 206 insertions, 18 deletions
diff --git a/src/lib/libcrypto/rand/rand_egd.c b/src/lib/libcrypto/rand/rand_egd.c
index d834408bd4..97ed12cf67 100644
--- a/src/lib/libcrypto/rand/rand_egd.c
+++ b/src/lib/libcrypto/rand/rand_egd.c
@@ -1,5 +1,5 @@
1/* crypto/rand/rand_egd.c */ 1/* crypto/rand/rand_egd.c */
2/* Written by Ulf Moeller for the OpenSSL project. */ 2/* Written by Ulf Moeller and Lutz Jaenicke for the OpenSSL project. */
3/* ==================================================================== 3/* ====================================================================
4 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 4 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
5 * 5 *
@@ -54,35 +54,92 @@
54 * 54 *
55 */ 55 */
56 56
57#include <openssl/e_os2.h>
57#include <openssl/rand.h> 58#include <openssl/rand.h>
58 59
59/* Query the EGD <URL: http://www.lothar.com/tech/crypto/>. 60/*
61 * Query the EGD <URL: http://www.lothar.com/tech/crypto/>.
62 *
63 * This module supplies three routines:
64 *
65 * RAND_query_egd_bytes(path, buf, bytes)
66 * will actually query "bytes" bytes of entropy form the egd-socket located
67 * at path and will write them to buf (if supplied) or will directly feed
68 * it to RAND_seed() if buf==NULL.
69 * The number of bytes is not limited by the maximum chunk size of EGD,
70 * which is 255 bytes. If more than 255 bytes are wanted, several chunks
71 * of entropy bytes are requested. The connection is left open until the
72 * query is competed.
73 * RAND_query_egd_bytes() returns with
74 * -1 if an error occured during connection or communication.
75 * num the number of bytes read from the EGD socket. This number is either
76 * the number of bytes requested or smaller, if the EGD pool is
77 * drained and the daemon signals that the pool is empty.
78 * This routine does not touch any RAND_status(). This is necessary, since
79 * PRNG functions may call it during initialization.
80 *
81 * RAND_egd_bytes(path, bytes) will query "bytes" bytes and have them
82 * used to seed the PRNG.
83 * RAND_egd_bytes() is a wrapper for RAND_query_egd_bytes() with buf=NULL.
84 * Unlike RAND_query_egd_bytes(), RAND_status() is used to test the
85 * seed status so that the return value can reflect the seed state:
86 * -1 if an error occured during connection or communication _or_
87 * if the PRNG has still not received the required seeding.
88 * num the number of bytes read from the EGD socket. This number is either
89 * the number of bytes requested or smaller, if the EGD pool is
90 * drained and the daemon signals that the pool is empty.
91 *
92 * RAND_egd(path) will query 255 bytes and use the bytes retreived to seed
93 * the PRNG.
94 * RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255.
60 */ 95 */
61 96
62#if defined(WIN32) || defined(VMS) || defined(__VMS) 97#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS)
98int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
99 {
100 return(-1);
101 }
63int RAND_egd(const char *path) 102int RAND_egd(const char *path)
64 { 103 {
65 return(-1); 104 return(-1);
66 } 105 }
106
107int RAND_egd_bytes(const char *path,int bytes)
108 {
109 return(-1);
110 }
67#else 111#else
68#include <openssl/opensslconf.h> 112#include <openssl/opensslconf.h>
69#include OPENSSL_UNISTD 113#include OPENSSL_UNISTD
70#include <sys/types.h> 114#include <sys/types.h>
71#include <sys/socket.h> 115#include <sys/socket.h>
72#include <sys/un.h> 116#ifndef NO_SYS_UN_H
117# ifdef OPENSSL_SYS_VSWORKS
118# include <streams/un.h>
119# else
120# include <sys/un.h>
121# endif
122#else
123struct sockaddr_un {
124 short sun_family; /* AF_UNIX */
125 char sun_path[108]; /* path name (gag) */
126};
127#endif /* NO_SYS_UN_H */
73#include <string.h> 128#include <string.h>
129#include <errno.h>
74 130
75#ifndef offsetof 131#ifndef offsetof
76# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 132# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
77#endif 133#endif
78 134
79int RAND_egd(const char *path) 135int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
80 { 136 {
81 int ret = -1; 137 int ret = 0;
82 struct sockaddr_un addr; 138 struct sockaddr_un addr;
83 int len, num; 139 int len, num, numbytes;
84 int fd = -1; 140 int fd = -1;
85 unsigned char buf[256]; 141 int success;
142 unsigned char egdbuf[2], tempbuf[255], *retrievebuf;
86 143
87 memset(&addr, 0, sizeof(addr)); 144 memset(&addr, 0, sizeof(addr));
88 addr.sun_family = AF_UNIX; 145 addr.sun_family = AF_UNIX;
@@ -92,19 +149,150 @@ int RAND_egd(const char *path)
92 len = offsetof(struct sockaddr_un, sun_path) + strlen(path); 149 len = offsetof(struct sockaddr_un, sun_path) + strlen(path);
93 fd = socket(AF_UNIX, SOCK_STREAM, 0); 150 fd = socket(AF_UNIX, SOCK_STREAM, 0);
94 if (fd == -1) return (-1); 151 if (fd == -1) return (-1);
95 if (connect(fd, (struct sockaddr *)&addr, len) == -1) goto err; 152 success = 0;
96 buf[0] = 1; 153 while (!success)
97 buf[1] = 255; 154 {
98 write(fd, buf, 2); 155 if (connect(fd, (struct sockaddr *)&addr, len) == 0)
99 if (read(fd, buf, 1) != 1) goto err; 156 success = 1;
100 if (buf[0] == 0) goto err; 157 else
101 num = read(fd, buf, 255); 158 {
159 switch (errno)
160 {
161#ifdef EINTR
162 case EINTR:
163#endif
164#ifdef EAGAIN
165 case EAGAIN:
166#endif
167#ifdef EINPROGRESS
168 case EINPROGRESS:
169#endif
170#ifdef EALREADY
171 case EALREADY:
172#endif
173 /* No error, try again */
174 break;
175#ifdef EISCONN
176 case EISCONN:
177 success = 1;
178 break;
179#endif
180 default:
181 goto err; /* failure */
182 }
183 }
184 }
185
186 while(bytes > 0)
187 {
188 egdbuf[0] = 1;
189 egdbuf[1] = bytes < 255 ? bytes : 255;
190 numbytes = 0;
191 while (numbytes != 2)
192 {
193 num = write(fd, egdbuf + numbytes, 2 - numbytes);
194 if (num >= 0)
195 numbytes += num;
196 else
197 {
198 switch (errno)
199 {
200#ifdef EINTR
201 case EINTR:
202#endif
203#ifdef EAGAIN
204 case EAGAIN:
205#endif
206 /* No error, try again */
207 break;
208 default:
209 ret = -1;
210 goto err; /* failure */
211 }
212 }
213 }
214 numbytes = 0;
215 while (numbytes != 1)
216 {
217 num = read(fd, egdbuf, 1);
218 if (num >= 0)
219 numbytes += num;
220 else
221 {
222 switch (errno)
223 {
224#ifdef EINTR
225 case EINTR:
226#endif
227#ifdef EAGAIN
228 case EAGAIN:
229#endif
230 /* No error, try again */
231 break;
232 default:
233 ret = -1;
234 goto err; /* failure */
235 }
236 }
237 }
238 if(egdbuf[0] == 0)
239 goto err;
240 if (buf)
241 retrievebuf = buf + ret;
242 else
243 retrievebuf = tempbuf;
244 numbytes = 0;
245 while (numbytes != egdbuf[0])
246 {
247 num = read(fd, retrievebuf + numbytes, egdbuf[0] - numbytes);
248 if (num >= 0)
249 numbytes += num;
250 else
251 {
252 switch (errno)
253 {
254#ifdef EINTR
255 case EINTR:
256#endif
257#ifdef EAGAIN
258 case EAGAIN:
259#endif
260 /* No error, try again */
261 break;
262 default:
263 ret = -1;
264 goto err; /* failure */
265 }
266 }
267 }
268 ret += egdbuf[0];
269 bytes -= egdbuf[0];
270 if (!buf)
271 RAND_seed(tempbuf, egdbuf[0]);
272 }
273 err:
274 if (fd != -1) close(fd);
275 return(ret);
276 }
277
278
279int RAND_egd_bytes(const char *path, int bytes)
280 {
281 int num, ret = 0;
282
283 num = RAND_query_egd_bytes(path, NULL, bytes);
102 if (num < 1) goto err; 284 if (num < 1) goto err;
103 RAND_seed(buf, num);
104 if (RAND_status() == 1) 285 if (RAND_status() == 1)
105 ret = num; 286 ret = num;
106 err: 287 err:
107 if (fd != -1) close(fd);
108 return(ret); 288 return(ret);
109 } 289 }
290
291
292int RAND_egd(const char *path)
293 {
294 return (RAND_egd_bytes(path, 255));
295 }
296
297
110#endif 298#endif