use OPENSSL_SYS_MSDOS rather than __DJGPP__ to disable egd, this is not
[openssl.git] / crypto / rand / rand_egd.c
1 /* crypto/rand/rand_egd.c */
2 /* Written by Ulf Moeller and Lutz Jaenicke for the OpenSSL project. */
3 /* ====================================================================
4  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * 3. All advertising materials mentioning features or use of this
19  *    software must display the following acknowledgment:
20  *    "This product includes software developed by the OpenSSL Project
21  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22  *
23  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24  *    endorse or promote products derived from this software without
25  *    prior written permission. For written permission, please contact
26  *    openssl-core@openssl.org.
27  *
28  * 5. Products derived from this software may not be called "OpenSSL"
29  *    nor may "OpenSSL" appear in their names without prior written
30  *    permission of the OpenSSL Project.
31  *
32  * 6. Redistributions of any form whatsoever must retain the following
33  *    acknowledgment:
34  *    "This product includes software developed by the OpenSSL Project
35  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48  * OF THE POSSIBILITY OF SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This product includes cryptographic software written by Eric Young
52  * (eay@cryptsoft.com).  This product includes software written by Tim
53  * Hudson (tjh@cryptsoft.com).
54  *
55  */
56
57 #include <openssl/e_os2.h>
58 #include <openssl/rand.h>
59
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.
95  */
96
97 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS)
98 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
99         {
100         return(-1);
101         }
102 int RAND_egd(const char *path)
103         {
104         return(-1);
105         }
106
107 int RAND_egd_bytes(const char *path,int bytes)
108         {
109         return(-1);
110         }
111 #else
112 #include <openssl/opensslconf.h>
113 #include OPENSSL_UNISTD
114 #include <sys/types.h>
115 #include <sys/socket.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
123 struct  sockaddr_un {
124         short   sun_family;             /* AF_UNIX */
125         char    sun_path[108];          /* path name (gag) */
126 };
127 #endif /* NO_SYS_UN_H */
128 #include <string.h>
129 #include <errno.h>
130
131 #ifndef offsetof
132 #  define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
133 #endif
134
135 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
136         {
137         int ret = 0;
138         struct sockaddr_un addr;
139         int len, num, numbytes;
140         int fd = -1;
141         int success;
142         unsigned char egdbuf[2], tempbuf[255], *retrievebuf;
143
144         memset(&addr, 0, sizeof(addr));
145         addr.sun_family = AF_UNIX;
146         if (strlen(path) > sizeof(addr.sun_path))
147                 return (-1);
148         strcpy(addr.sun_path,path);
149         len = offsetof(struct sockaddr_un, sun_path) + strlen(path);
150         fd = socket(AF_UNIX, SOCK_STREAM, 0);
151         if (fd == -1) return (-1);
152         success = 0;
153         while (!success)
154             {
155             if (connect(fd, (struct sockaddr *)&addr, len) == 0)
156                success = 1;
157             else
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
279 int RAND_egd_bytes(const char *path, int bytes)
280         {
281         int num, ret = 0;
282
283         num = RAND_query_egd_bytes(path, NULL, bytes);
284         if (num < 1) goto err;
285         if (RAND_status() == 1)
286             ret = num;
287  err:
288         return(ret);
289         }
290
291
292 int RAND_egd(const char *path)
293         {
294         return (RAND_egd_bytes(path, 255));
295         }
296
297
298 #endif