10Sstevel@tonic-gate /* crypto/rand/rand_unix.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate /* ====================================================================
590Sstevel@tonic-gate * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
600Sstevel@tonic-gate *
610Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate * are met:
640Sstevel@tonic-gate *
650Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate * the documentation and/or other materials provided with the
710Sstevel@tonic-gate * distribution.
720Sstevel@tonic-gate *
730Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate * software must display the following acknowledgment:
750Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate *
780Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate * endorse or promote products derived from this software without
800Sstevel@tonic-gate * prior written permission. For written permission, please contact
810Sstevel@tonic-gate * openssl-core@openssl.org.
820Sstevel@tonic-gate *
830Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate * permission of the OpenSSL Project.
860Sstevel@tonic-gate *
870Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate * acknowledgment:
890Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate *
920Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate * ====================================================================
1050Sstevel@tonic-gate *
1060Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
1080Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate *
1100Sstevel@tonic-gate */
111*2139Sjp161948
1120Sstevel@tonic-gate /*
113*2139Sjp161948 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
1140Sstevel@tonic-gate * Use is subject to license terms.
1150Sstevel@tonic-gate */
116*2139Sjp161948
1170Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
1180Sstevel@tonic-gate
119*2139Sjp161948 #include <stdio.h>
120*2139Sjp161948
1210Sstevel@tonic-gate #define USE_SOCKETS
1220Sstevel@tonic-gate #include "e_os.h"
1230Sstevel@tonic-gate #include "cryptlib.h"
1240Sstevel@tonic-gate #include <openssl/rand.h>
1250Sstevel@tonic-gate #include "rand_lcl.h"
1260Sstevel@tonic-gate
127*2139Sjp161948 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE))
1280Sstevel@tonic-gate
129*2139Sjp161948 #include <sys/types.h>
130*2139Sjp161948 #include <sys/time.h>
131*2139Sjp161948 #include <sys/times.h>
132*2139Sjp161948 #include <sys/stat.h>
1330Sstevel@tonic-gate #ifdef _BOOT
1340Sstevel@tonic-gate #include <sys/fcntl.h>
1350Sstevel@tonic-gate #else
1360Sstevel@tonic-gate #include <fcntl.h>
1370Sstevel@tonic-gate #endif
138*2139Sjp161948 #include <unistd.h>
1390Sstevel@tonic-gate #include <time.h>
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate #ifdef __OpenBSD__
RAND_poll(void)1420Sstevel@tonic-gate int RAND_poll(void)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate u_int32_t rnd = 0, i;
1450Sstevel@tonic-gate unsigned char buf[ENTROPY_NEEDED];
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate for (i = 0; i < sizeof(buf); i++) {
1480Sstevel@tonic-gate if (i % 4 == 0)
1490Sstevel@tonic-gate rnd = arc4random();
1500Sstevel@tonic-gate buf[i] = rnd;
1510Sstevel@tonic-gate rnd >>= 8;
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate RAND_add(buf, sizeof(buf), ENTROPY_NEEDED);
1540Sstevel@tonic-gate memset(buf, 0, sizeof(buf));
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate return 1;
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate #else
RAND_poll(void)1590Sstevel@tonic-gate int RAND_poll(void)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate unsigned long l;
1620Sstevel@tonic-gate pid_t curr_pid = getpid();
1630Sstevel@tonic-gate #if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
1640Sstevel@tonic-gate unsigned char tmpbuf[ENTROPY_NEEDED];
1650Sstevel@tonic-gate int n = 0;
1660Sstevel@tonic-gate #endif
1670Sstevel@tonic-gate #ifdef DEVRANDOM
168*2139Sjp161948 static const char *randomfiles[] = { DEVRANDOM };
169*2139Sjp161948 struct stat randomstats[sizeof(randomfiles)/sizeof(randomfiles[0])];
1700Sstevel@tonic-gate int fd;
171*2139Sjp161948 size_t i;
1720Sstevel@tonic-gate #endif
1730Sstevel@tonic-gate #ifdef DEVRANDOM_EGD
1740Sstevel@tonic-gate static const char *egdsockets[] = { DEVRANDOM_EGD, NULL };
1750Sstevel@tonic-gate const char **egdsocket = NULL;
1760Sstevel@tonic-gate #endif
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate #ifdef DEVRANDOM
179*2139Sjp161948 memset(randomstats,0,sizeof(randomstats));
1800Sstevel@tonic-gate /* Use a random entropy pool device. Linux, FreeBSD and OpenBSD
1810Sstevel@tonic-gate * have this. Use /dev/urandom if you can as /dev/random may block
1820Sstevel@tonic-gate * if it runs out of random entries. */
1830Sstevel@tonic-gate
184*2139Sjp161948 for (i=0; i<sizeof(randomfiles)/sizeof(randomfiles[0]) && n < ENTROPY_NEEDED; i++)
1850Sstevel@tonic-gate {
186*2139Sjp161948 if ((fd = open(randomfiles[i], O_RDONLY
187*2139Sjp161948 #ifdef O_NONBLOCK
188*2139Sjp161948 |O_NONBLOCK
189*2139Sjp161948 #endif
190*2139Sjp161948 #ifdef O_BINARY
191*2139Sjp161948 |O_BINARY
192*2139Sjp161948 #endif
1930Sstevel@tonic-gate #ifdef O_NOCTTY /* If it happens to be a TTY (god forbid), do not make it
1940Sstevel@tonic-gate our controlling tty */
195*2139Sjp161948 |O_NOCTTY
1960Sstevel@tonic-gate #endif
197*2139Sjp161948 )) >= 0)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate struct timeval t = { 0, 10*1000 }; /* Spend 10ms on
2000Sstevel@tonic-gate each file. */
2010Sstevel@tonic-gate int r;
202*2139Sjp161948 size_t j;
2030Sstevel@tonic-gate fd_set fset;
204*2139Sjp161948 struct stat *st=&randomstats[i];
205*2139Sjp161948
206*2139Sjp161948 /* Avoid using same input... Used to be O_NOFOLLOW
207*2139Sjp161948 * above, but it's not universally appropriate... */
208*2139Sjp161948 if (fstat(fd,st) != 0) { close(fd); continue; }
209*2139Sjp161948 for (j=0;j<i;j++)
210*2139Sjp161948 {
211*2139Sjp161948 if (randomstats[j].st_ino==st->st_ino &&
212*2139Sjp161948 randomstats[j].st_dev==st->st_dev)
213*2139Sjp161948 break;
214*2139Sjp161948 }
215*2139Sjp161948 if (j<i) { close(fd); continue; }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate do
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate #ifndef _BOOT
2200Sstevel@tonic-gate FD_ZERO(&fset);
2210Sstevel@tonic-gate FD_SET(fd, &fset);
2220Sstevel@tonic-gate r = -1;
223*2139Sjp161948
2240Sstevel@tonic-gate if (select(fd+1,&fset,NULL,NULL,&t) < 0)
2250Sstevel@tonic-gate t.tv_usec=0;
2260Sstevel@tonic-gate else if (FD_ISSET(fd, &fset))
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate #endif /* !_BOOT */
2290Sstevel@tonic-gate r=read(fd,(unsigned char *)tmpbuf+n,
2300Sstevel@tonic-gate ENTROPY_NEEDED-n);
2310Sstevel@tonic-gate if (r > 0)
2320Sstevel@tonic-gate n += r;
2330Sstevel@tonic-gate #ifndef _BOOT
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate /* Some Unixen will update t, some
2370Sstevel@tonic-gate won't. For those who won't, give
2380Sstevel@tonic-gate up here, otherwise, we will do
2390Sstevel@tonic-gate this once again for the remaining
2400Sstevel@tonic-gate time. */
2410Sstevel@tonic-gate if (t.tv_usec == 10*1000)
2420Sstevel@tonic-gate t.tv_usec=0;
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate while ((r > 0 || (errno == EINTR || errno == EAGAIN))
2450Sstevel@tonic-gate && t.tv_usec != 0 && n < ENTROPY_NEEDED);
2460Sstevel@tonic-gate #else
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate while ((r > 0 && n < ENTROPY_NEEDED));
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate #endif /* !_BOOT */
2510Sstevel@tonic-gate close(fd);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate #endif
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate #if defined(DEVRANDOM_EGD) && !defined(_BOOT)
2570Sstevel@tonic-gate /* Use an EGD socket to read entropy from an EGD or PRNGD entropy
2580Sstevel@tonic-gate * collecting daemon. */
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate for (egdsocket = egdsockets; *egdsocket && n < ENTROPY_NEEDED; egdsocket++)
2610Sstevel@tonic-gate {
2620Sstevel@tonic-gate int r;
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate r = RAND_query_egd_bytes(*egdsocket, (unsigned char *)tmpbuf+n,
2650Sstevel@tonic-gate ENTROPY_NEEDED-n);
2660Sstevel@tonic-gate if (r > 0)
2670Sstevel@tonic-gate n += r;
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate #endif
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate #if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
2720Sstevel@tonic-gate if (n > 0)
2730Sstevel@tonic-gate {
274*2139Sjp161948 RAND_add(tmpbuf,sizeof tmpbuf,(double)n);
2750Sstevel@tonic-gate OPENSSL_cleanse(tmpbuf,n);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate #endif
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate #ifndef _BOOT
2800Sstevel@tonic-gate /* put in some default random data, we need more than just this */
2810Sstevel@tonic-gate l=curr_pid;
282*2139Sjp161948 RAND_add(&l,sizeof(l),0.0);
2830Sstevel@tonic-gate l=getuid();
284*2139Sjp161948 RAND_add(&l,sizeof(l),0.0);
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate l=time(NULL);
287*2139Sjp161948 RAND_add(&l,sizeof(l),0.0);
2880Sstevel@tonic-gate #endif /* !_BOOT */
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate #if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
2910Sstevel@tonic-gate return 1;
2920Sstevel@tonic-gate #else
2930Sstevel@tonic-gate return 0;
2940Sstevel@tonic-gate #endif
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate #endif
2980Sstevel@tonic-gate #endif
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate #if defined(OPENSSL_SYS_VXWORKS)
RAND_poll(void)3010Sstevel@tonic-gate int RAND_poll(void)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate return 0;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate #endif
306