AFAICS lst1 stands for "lshift test" not "list".
[oweals/openssl.git] / crypto / rand / md_rand.c
index d727fff9243d6efc57b74994ab3fc5b458fe1690..7b8cde94012241467524596eadde681ca444aff4 100644 (file)
@@ -144,14 +144,16 @@ const char *RAND_version="RAND" OPENSSL_VERSION_PTEXT;
 
 static void ssleay_rand_cleanup(void);
 static void ssleay_rand_seed(const void *buf, int num);
-static void ssleay_rand_add(const void *buf, int num, int entropy);
+static void ssleay_rand_add(const void *buf, int num, int add_entropy);
 static int ssleay_rand_bytes(unsigned char *buf, int num);
+static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num);
 
 RAND_METHOD rand_ssleay_meth={
        ssleay_rand_seed,
        ssleay_rand_bytes,
        ssleay_rand_cleanup,
        ssleay_rand_add,
+       ssleay_rand_pseudo_bytes,
        }; 
 
 RAND_METHOD *RAND_SSLeay(void)
@@ -360,15 +362,13 @@ static int ssleay_rand_bytes(unsigned char *buf, int num)
                 */
                if ((fh = fopen(DEVRANDOM, "r")) != NULL)
                        {
-                       unsigned char tmpbuf[32];
+                       unsigned char tmpbuf[ENTROPY_NEEDED];
+                       int n;
 
-                       fread((unsigned char *)tmpbuf,1,32,fh);
-                       /* we don't care how many bytes we read,
-                        * we will just copy the 'stack' if there is
-                        * nothing else :-) */
+                       n=fread((unsigned char *)tmpbuf,1,ENTROPY_NEEDED,fh);
                        fclose(fh);
-                       RAND_seed(tmpbuf,32);
-                       memset(tmpbuf,0,32);
+                       RAND_add(tmpbuf,sizeof tmpbuf,n);
+                       memset(tmpbuf,0,n);
                        }
 #endif
 #ifdef PURIFY
@@ -451,6 +451,23 @@ static int ssleay_rand_bytes(unsigned char *buf, int num)
                }
        }
 
+/* pseudo-random bytes that are guaranteed to be unique but not
+   unpredictable */
+static int ssleay_rand_pseudo_bytes(unsigned char *buf, int num) 
+       {
+       int ret, err;
+
+       ret = RAND_bytes(buf, num);
+       if (ret == 0)
+               {
+               err = ERR_peek_error();
+               if (ERR_GET_LIB(err) == ERR_LIB_RAND &&
+                   ERR_GET_REASON(err) == RAND_R_PRNG_NOT_SEEDED)
+                       (void)ERR_get_error();
+               }
+       return (ret);
+       }
+
 #ifdef WINDOWS
 #include <windows.h>
 #include <openssl/rand.h>