2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
15 # include <openssl/crypto.h>
16 # include <openssl/e_os2.h>
17 # include <openssl/rand.h>
20 * Query the EGD <URL: http://www.lothar.com/tech/crypto/>.
22 * This module supplies three routines:
24 * RAND_query_egd_bytes(path, buf, bytes)
25 * will actually query "bytes" bytes of entropy form the egd-socket located
26 * at path and will write them to buf (if supplied) or will directly feed
27 * it to RAND_seed() if buf==NULL.
28 * The number of bytes is not limited by the maximum chunk size of EGD,
29 * which is 255 bytes. If more than 255 bytes are wanted, several chunks
30 * of entropy bytes are requested. The connection is left open until the
32 * RAND_query_egd_bytes() returns with
33 * -1 if an error occurred during connection or communication.
34 * num the number of bytes read from the EGD socket. This number is either
35 * the number of bytes requested or smaller, if the EGD pool is
36 * drained and the daemon signals that the pool is empty.
37 * This routine does not touch any RAND_status(). This is necessary, since
38 * PRNG functions may call it during initialization.
40 * RAND_egd_bytes(path, bytes) will query "bytes" bytes and have them
41 * used to seed the PRNG.
42 * RAND_egd_bytes() is a wrapper for RAND_query_egd_bytes() with buf=NULL.
43 * Unlike RAND_query_egd_bytes(), RAND_status() is used to test the
44 * seed status so that the return value can reflect the seed state:
45 * -1 if an error occurred during connection or communication _or_
46 * if the PRNG has still not received the required seeding.
47 * num the number of bytes read from the EGD socket. This number is either
48 * the number of bytes requested or smaller, if the EGD pool is
49 * drained and the daemon signals that the pool is empty.
51 * RAND_egd(path) will query 255 bytes and use the bytes retrieved to seed
53 * RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255.
56 # if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_VOS) || defined(OPENSSL_SYS_UEFI)
57 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
62 int RAND_egd(const char *path)
67 int RAND_egd_bytes(const char *path, int bytes)
72 # include <openssl/opensslconf.h>
73 # include OPENSSL_UNISTD
75 # include <sys/types.h>
76 # include <sys/socket.h>
78 # ifdef OPENSSL_SYS_VXWORKS
79 # include <streams/un.h>
85 short sun_family; /* AF_UNIX */
86 char sun_path[108]; /* path name (gag) */
88 # endif /* NO_SYS_UN_H */
92 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
95 struct sockaddr_un addr;
96 int len, num, numbytes;
99 unsigned char egdbuf[2], tempbuf[255], *retrievebuf;
101 memset(&addr, 0, sizeof(addr));
102 addr.sun_family = AF_UNIX;
103 if (strlen(path) >= sizeof(addr.sun_path))
105 OPENSSL_strlcpy(addr.sun_path, path, sizeof addr.sun_path);
106 len = offsetof(struct sockaddr_un, sun_path) + strlen(path);
107 fd = socket(AF_UNIX, SOCK_STREAM, 0);
112 if (connect(fd, (struct sockaddr *)&addr, len) == 0)
128 /* No error, try again */
137 goto err; /* failure */
144 egdbuf[1] = bytes < 255 ? bytes : 255;
146 while (numbytes != 2) {
147 num = write(fd, egdbuf + numbytes, 2 - numbytes);
158 /* No error, try again */
162 goto err; /* failure */
167 while (numbytes != 1) {
168 num = read(fd, egdbuf, 1);
170 goto err; /* descriptor closed */
181 /* No error, try again */
185 goto err; /* failure */
192 retrievebuf = buf + ret;
194 retrievebuf = tempbuf;
196 while (numbytes != egdbuf[0]) {
197 num = read(fd, retrievebuf + numbytes, egdbuf[0] - numbytes);
199 goto err; /* descriptor closed */
210 /* No error, try again */
214 goto err; /* failure */
221 RAND_seed(tempbuf, egdbuf[0]);
229 int RAND_egd_bytes(const char *path, int bytes)
233 num = RAND_query_egd_bytes(path, NULL, bytes);
234 if (num < 1 || RAND_status() == 1)
240 int RAND_egd(const char *path)
242 return (RAND_egd_bytes(path, 255));