10Sstevel@tonic-gate /* crypto/rand/rand_egd.c */
20Sstevel@tonic-gate /* Written by Ulf Moeller and Lutz Jaenicke for the OpenSSL project. */
30Sstevel@tonic-gate /* ====================================================================
40Sstevel@tonic-gate * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
50Sstevel@tonic-gate *
60Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
70Sstevel@tonic-gate * modification, are permitted provided that the following conditions
80Sstevel@tonic-gate * are met:
90Sstevel@tonic-gate *
100Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
110Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
140Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
150Sstevel@tonic-gate * the documentation and/or other materials provided with the
160Sstevel@tonic-gate * distribution.
170Sstevel@tonic-gate *
180Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
190Sstevel@tonic-gate * software must display the following acknowledgment:
200Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
210Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
240Sstevel@tonic-gate * endorse or promote products derived from this software without
250Sstevel@tonic-gate * prior written permission. For written permission, please contact
260Sstevel@tonic-gate * openssl-core@openssl.org.
270Sstevel@tonic-gate *
280Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
290Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
300Sstevel@tonic-gate * permission of the OpenSSL Project.
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
330Sstevel@tonic-gate * acknowledgment:
340Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
350Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
380Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
390Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
400Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
410Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
420Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
430Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
440Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
450Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
460Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
470Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
480Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
490Sstevel@tonic-gate * ====================================================================
500Sstevel@tonic-gate *
510Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
520Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
530Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
540Sstevel@tonic-gate *
550Sstevel@tonic-gate */
560Sstevel@tonic-gate
570Sstevel@tonic-gate #include <openssl/e_os2.h>
580Sstevel@tonic-gate #include <openssl/rand.h>
590Sstevel@tonic-gate #include <openssl/buffer.h>
600Sstevel@tonic-gate
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate * Query the EGD <URL: http://www.lothar.com/tech/crypto/>.
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * This module supplies three routines:
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * RAND_query_egd_bytes(path, buf, bytes)
670Sstevel@tonic-gate * will actually query "bytes" bytes of entropy form the egd-socket located
680Sstevel@tonic-gate * at path and will write them to buf (if supplied) or will directly feed
690Sstevel@tonic-gate * it to RAND_seed() if buf==NULL.
700Sstevel@tonic-gate * The number of bytes is not limited by the maximum chunk size of EGD,
710Sstevel@tonic-gate * which is 255 bytes. If more than 255 bytes are wanted, several chunks
720Sstevel@tonic-gate * of entropy bytes are requested. The connection is left open until the
730Sstevel@tonic-gate * query is competed.
740Sstevel@tonic-gate * RAND_query_egd_bytes() returns with
750Sstevel@tonic-gate * -1 if an error occured during connection or communication.
760Sstevel@tonic-gate * num the number of bytes read from the EGD socket. This number is either
770Sstevel@tonic-gate * the number of bytes requested or smaller, if the EGD pool is
780Sstevel@tonic-gate * drained and the daemon signals that the pool is empty.
790Sstevel@tonic-gate * This routine does not touch any RAND_status(). This is necessary, since
800Sstevel@tonic-gate * PRNG functions may call it during initialization.
810Sstevel@tonic-gate *
820Sstevel@tonic-gate * RAND_egd_bytes(path, bytes) will query "bytes" bytes and have them
830Sstevel@tonic-gate * used to seed the PRNG.
840Sstevel@tonic-gate * RAND_egd_bytes() is a wrapper for RAND_query_egd_bytes() with buf=NULL.
850Sstevel@tonic-gate * Unlike RAND_query_egd_bytes(), RAND_status() is used to test the
860Sstevel@tonic-gate * seed status so that the return value can reflect the seed state:
870Sstevel@tonic-gate * -1 if an error occured during connection or communication _or_
880Sstevel@tonic-gate * if the PRNG has still not received the required seeding.
890Sstevel@tonic-gate * num the number of bytes read from the EGD socket. This number is either
900Sstevel@tonic-gate * the number of bytes requested or smaller, if the EGD pool is
910Sstevel@tonic-gate * drained and the daemon signals that the pool is empty.
920Sstevel@tonic-gate *
930Sstevel@tonic-gate * RAND_egd(path) will query 255 bytes and use the bytes retreived to seed
940Sstevel@tonic-gate * the PRNG.
950Sstevel@tonic-gate * RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255.
960Sstevel@tonic-gate */
970Sstevel@tonic-gate
98*2139Sjp161948 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_VOS)
RAND_query_egd_bytes(const char * path,unsigned char * buf,int bytes)990Sstevel@tonic-gate int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate return(-1);
1020Sstevel@tonic-gate }
RAND_egd(const char * path)1030Sstevel@tonic-gate int RAND_egd(const char *path)
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate return(-1);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
RAND_egd_bytes(const char * path,int bytes)1080Sstevel@tonic-gate int RAND_egd_bytes(const char *path,int bytes)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate return(-1);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate #else
1130Sstevel@tonic-gate #include <openssl/opensslconf.h>
1140Sstevel@tonic-gate #include OPENSSL_UNISTD
1150Sstevel@tonic-gate #include <sys/types.h>
1160Sstevel@tonic-gate #include <sys/socket.h>
1170Sstevel@tonic-gate #ifndef NO_SYS_UN_H
1180Sstevel@tonic-gate # ifdef OPENSSL_SYS_VXWORKS
1190Sstevel@tonic-gate # include <streams/un.h>
1200Sstevel@tonic-gate # else
1210Sstevel@tonic-gate # include <sys/un.h>
1220Sstevel@tonic-gate # endif
1230Sstevel@tonic-gate #else
1240Sstevel@tonic-gate struct sockaddr_un {
1250Sstevel@tonic-gate short sun_family; /* AF_UNIX */
1260Sstevel@tonic-gate char sun_path[108]; /* path name (gag) */
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate #endif /* NO_SYS_UN_H */
1290Sstevel@tonic-gate #include <string.h>
1300Sstevel@tonic-gate #include <errno.h>
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate #ifndef offsetof
1330Sstevel@tonic-gate # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
1340Sstevel@tonic-gate #endif
1350Sstevel@tonic-gate
RAND_query_egd_bytes(const char * path,unsigned char * buf,int bytes)1360Sstevel@tonic-gate int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate int ret = 0;
1390Sstevel@tonic-gate struct sockaddr_un addr;
1400Sstevel@tonic-gate int len, num, numbytes;
1410Sstevel@tonic-gate int fd = -1;
1420Sstevel@tonic-gate int success;
1430Sstevel@tonic-gate unsigned char egdbuf[2], tempbuf[255], *retrievebuf;
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate memset(&addr, 0, sizeof(addr));
1460Sstevel@tonic-gate addr.sun_family = AF_UNIX;
1470Sstevel@tonic-gate if (strlen(path) >= sizeof(addr.sun_path))
1480Sstevel@tonic-gate return (-1);
1490Sstevel@tonic-gate BUF_strlcpy(addr.sun_path,path,sizeof addr.sun_path);
1500Sstevel@tonic-gate len = offsetof(struct sockaddr_un, sun_path) + strlen(path);
1510Sstevel@tonic-gate fd = socket(AF_UNIX, SOCK_STREAM, 0);
1520Sstevel@tonic-gate if (fd == -1) return (-1);
1530Sstevel@tonic-gate success = 0;
1540Sstevel@tonic-gate while (!success)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate if (connect(fd, (struct sockaddr *)&addr, len) == 0)
1570Sstevel@tonic-gate success = 1;
1580Sstevel@tonic-gate else
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate switch (errno)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate #ifdef EINTR
1630Sstevel@tonic-gate case EINTR:
1640Sstevel@tonic-gate #endif
1650Sstevel@tonic-gate #ifdef EAGAIN
1660Sstevel@tonic-gate case EAGAIN:
1670Sstevel@tonic-gate #endif
1680Sstevel@tonic-gate #ifdef EINPROGRESS
1690Sstevel@tonic-gate case EINPROGRESS:
1700Sstevel@tonic-gate #endif
1710Sstevel@tonic-gate #ifdef EALREADY
1720Sstevel@tonic-gate case EALREADY:
1730Sstevel@tonic-gate #endif
1740Sstevel@tonic-gate /* No error, try again */
1750Sstevel@tonic-gate break;
1760Sstevel@tonic-gate #ifdef EISCONN
1770Sstevel@tonic-gate case EISCONN:
1780Sstevel@tonic-gate success = 1;
1790Sstevel@tonic-gate break;
1800Sstevel@tonic-gate #endif
1810Sstevel@tonic-gate default:
1820Sstevel@tonic-gate goto err; /* failure */
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate while(bytes > 0)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate egdbuf[0] = 1;
1900Sstevel@tonic-gate egdbuf[1] = bytes < 255 ? bytes : 255;
1910Sstevel@tonic-gate numbytes = 0;
1920Sstevel@tonic-gate while (numbytes != 2)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate num = write(fd, egdbuf + numbytes, 2 - numbytes);
1950Sstevel@tonic-gate if (num >= 0)
1960Sstevel@tonic-gate numbytes += num;
1970Sstevel@tonic-gate else
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate switch (errno)
2000Sstevel@tonic-gate {
2010Sstevel@tonic-gate #ifdef EINTR
2020Sstevel@tonic-gate case EINTR:
2030Sstevel@tonic-gate #endif
2040Sstevel@tonic-gate #ifdef EAGAIN
2050Sstevel@tonic-gate case EAGAIN:
2060Sstevel@tonic-gate #endif
2070Sstevel@tonic-gate /* No error, try again */
2080Sstevel@tonic-gate break;
2090Sstevel@tonic-gate default:
2100Sstevel@tonic-gate ret = -1;
2110Sstevel@tonic-gate goto err; /* failure */
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate numbytes = 0;
2160Sstevel@tonic-gate while (numbytes != 1)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate num = read(fd, egdbuf, 1);
219*2139Sjp161948 if (num == 0)
220*2139Sjp161948 goto err; /* descriptor closed */
221*2139Sjp161948 else if (num > 0)
2220Sstevel@tonic-gate numbytes += num;
2230Sstevel@tonic-gate else
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate switch (errno)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate #ifdef EINTR
2280Sstevel@tonic-gate case EINTR:
2290Sstevel@tonic-gate #endif
2300Sstevel@tonic-gate #ifdef EAGAIN
2310Sstevel@tonic-gate case EAGAIN:
2320Sstevel@tonic-gate #endif
2330Sstevel@tonic-gate /* No error, try again */
2340Sstevel@tonic-gate break;
2350Sstevel@tonic-gate default:
2360Sstevel@tonic-gate ret = -1;
2370Sstevel@tonic-gate goto err; /* failure */
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate if(egdbuf[0] == 0)
2420Sstevel@tonic-gate goto err;
2430Sstevel@tonic-gate if (buf)
2440Sstevel@tonic-gate retrievebuf = buf + ret;
2450Sstevel@tonic-gate else
2460Sstevel@tonic-gate retrievebuf = tempbuf;
2470Sstevel@tonic-gate numbytes = 0;
2480Sstevel@tonic-gate while (numbytes != egdbuf[0])
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate num = read(fd, retrievebuf + numbytes, egdbuf[0] - numbytes);
251*2139Sjp161948 if (num == 0)
252*2139Sjp161948 goto err; /* descriptor closed */
253*2139Sjp161948 else if (num > 0)
2540Sstevel@tonic-gate numbytes += num;
2550Sstevel@tonic-gate else
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate switch (errno)
2580Sstevel@tonic-gate {
2590Sstevel@tonic-gate #ifdef EINTR
2600Sstevel@tonic-gate case EINTR:
2610Sstevel@tonic-gate #endif
2620Sstevel@tonic-gate #ifdef EAGAIN
2630Sstevel@tonic-gate case EAGAIN:
2640Sstevel@tonic-gate #endif
2650Sstevel@tonic-gate /* No error, try again */
2660Sstevel@tonic-gate break;
2670Sstevel@tonic-gate default:
2680Sstevel@tonic-gate ret = -1;
2690Sstevel@tonic-gate goto err; /* failure */
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate ret += egdbuf[0];
2740Sstevel@tonic-gate bytes -= egdbuf[0];
2750Sstevel@tonic-gate if (!buf)
2760Sstevel@tonic-gate RAND_seed(tempbuf, egdbuf[0]);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate err:
2790Sstevel@tonic-gate if (fd != -1) close(fd);
2800Sstevel@tonic-gate return(ret);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate
RAND_egd_bytes(const char * path,int bytes)2840Sstevel@tonic-gate int RAND_egd_bytes(const char *path, int bytes)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate int num, ret = 0;
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate num = RAND_query_egd_bytes(path, NULL, bytes);
2890Sstevel@tonic-gate if (num < 1) goto err;
2900Sstevel@tonic-gate if (RAND_status() == 1)
2910Sstevel@tonic-gate ret = num;
2920Sstevel@tonic-gate err:
2930Sstevel@tonic-gate return(ret);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate
RAND_egd(const char * path)2970Sstevel@tonic-gate int RAND_egd(const char *path)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate return (RAND_egd_bytes(path, 255));
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate #endif
304