Make sure time() is properly declared.
[openssl.git] / crypto / rand / rand_unix.c
index c4aae38f1d998cb2798de1f4963a176c35f605f9..8e48561354e22fa038a460e9dc6fe97a712d266b 100644 (file)
 #include <sys/times.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <time.h>
 
 int RAND_poll(void)
 {
        unsigned long l;
        pid_t curr_pid = getpid();
-#ifdef DEVRANDOM
+#if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
        unsigned char tmpbuf[ENTROPY_NEEDED];
        int n = 0;
+#endif
+#ifdef DEVRANDOM
        static const char *randomfiles[] = { DEVRANDOM, NULL };
        const char **randomfile = NULL;
        int fd;
 #endif
+#ifdef DEVRANDOM_EGD
+       static const char *egdsockets[] = { DEVRANDOM_EGD, NULL };
+       const char **egdsocket = NULL;
+#endif
 
 #ifdef DEVRANDOM
        /* Use a random entropy pool device. Linux, FreeBSD and OpenBSD
@@ -183,12 +190,33 @@ int RAND_poll(void)
                                && t.tv_usec != 0 && n < ENTROPY_NEEDED);
 
                        close(fd);
-                       RAND_add(tmpbuf,sizeof tmpbuf,n);
-                       memset(tmpbuf,0,n);
                        }
                }
 #endif
 
+#ifdef DEVRANDOM_EGD
+       /* Use an EGD socket to read entropy from an EGD or PRNGD entropy
+        * collecting daemon. */
+
+       for (egdsocket = egdsockets; *egdsocket && n < ENTROPY_NEEDED; egdsocket++)
+               {
+               int r;
+
+               r = RAND_query_egd_bytes(*egdsocket, (unsigned char *)tmpbuf+n,
+                                        ENTROPY_NEEDED-n);
+               if (r > 0)
+                       n += r;
+               }
+#endif
+
+#if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
+       if (n > 0)
+               {
+               RAND_add(tmpbuf,sizeof tmpbuf,n);
+               memset(tmpbuf,0,n);
+               }
+#endif
+
        /* put in some default random data, we need more than just this */
        l=curr_pid;
        RAND_add(&l,sizeof(l),0);
@@ -198,7 +226,7 @@ int RAND_poll(void)
        l=time(NULL);
        RAND_add(&l,sizeof(l),0);
 
-#ifdef DEVRANDOM
+#if defined(DEVRANDOM) || defined(DEVRANDOM_EGD)
        return 1;
 #endif
        return 0;