More Win32 fixes and upsdate INSTALL.W32 documentation.
[openssl.git] / crypto / rand / md_rand.c
index 6c8e65a05cc81c94e57c4eb40d5b76aab60b0150..bef42459109c713cf8aaef4de6a5e2efd9921eb0 100644 (file)
@@ -1,5 +1,5 @@
 /* crypto/rand/md_rand.c */
-/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
+/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * This package is an SSL implementation written
  */
 
 #include <stdio.h>
-#include "cryptlib.h"
 #include <sys/types.h>
 #include <time.h>
-
+#include <string.h>
+#include "e_os.h"
+#include "crypto.h"
 
 #if !defined(USE_MD5_RAND) && !defined(USE_SHA1_RAND) && !defined(USE_MDC2_RAND) && !defined(USE_MD2_RAND)
 #ifndef NO_MD5
@@ -89,6 +90,7 @@ We need a message digest of some type
 #define MD_Init(a)             MD5_Init(a)
 #define MD_Update(a,b,c)       MD5_Update(a,b,c)
 #define        MD_Final(a,b)           MD5_Final(a,b)
+#define        MD(a,b,c)               MD5(a,b,c)
 #elif defined(USE_SHA1_RAND)
 #include "sha.h"
 #define MD_DIGEST_LENGTH       SHA_DIGEST_LENGTH
@@ -96,6 +98,7 @@ We need a message digest of some type
 #define MD_Init(a)             SHA1_Init(a)
 #define MD_Update(a,b,c)       SHA1_Update(a,b,c)
 #define        MD_Final(a,b)           SHA1_Final(a,b)
+#define        MD(a,b,c)               SHA1(a,b,c)
 #elif defined(USE_MDC2_RAND)
 #include "mdc2.h"
 #define MD_DIGEST_LENGTH       MDC2_DIGEST_LENGTH
@@ -103,6 +106,7 @@ We need a message digest of some type
 #define MD_Init(a)             MDC2_Init(a)
 #define MD_Update(a,b,c)       MDC2_Update(a,b,c)
 #define        MD_Final(a,b)           MDC2_Final(a,b)
+#define        MD(a,b,c)               MDC2(a,b,c)
 #elif defined(USE_MD2_RAND)
 #include "md2.h"
 #define MD_DIGEST_LENGTH       MD2_DIGEST_LENGTH
@@ -110,32 +114,49 @@ We need a message digest of some type
 #define MD_Init(a)             MD2_Init(a)
 #define MD_Update(a,b,c)       MD2_Update(a,b,c)
 #define        MD_Final(a,b)           MD2_Final(a,b)
+#define        MD(a,b,c)               MD2(a,b,c)
 #endif
 
 #include "rand.h"
 
-/*#define NORAND       1 */
-/*#define PREDICT      1 */
+/* #define NORAND      1 */
+/* #define PREDICT     1 */
 
 #define STATE_SIZE     1023
 static int state_num=0,state_index=0;
-static unsigned char state[STATE_SIZE];
+static unsigned char state[STATE_SIZE+MD_DIGEST_LENGTH];
 static unsigned char md[MD_DIGEST_LENGTH];
-static int count=0;
+static long md_count[2]={0,0};
+
+char *RAND_version="RAND part of OpenSSL 0.9.2 31-Dec-1998";
+
+static void ssleay_rand_cleanup(void);
+static void ssleay_rand_seed(const void *buf, int num);
+static void ssleay_rand_bytes(unsigned char *buf, int num);
 
-char *RAND_version="RAND part of SSLeay 0.8.1b 29-Jun-1998";
+RAND_METHOD rand_ssleay_meth={
+       ssleay_rand_seed,
+       ssleay_rand_bytes,
+       ssleay_rand_cleanup,
+       }; 
 
-void RAND_cleanup()
+RAND_METHOD *RAND_SSLeay()
        {
-       memset(state,0,STATE_SIZE);
+       return(&rand_ssleay_meth);
+       }
+
+static void ssleay_rand_cleanup()
+       {
+       memset(state,0,sizeof(state));
        state_num=0;
        state_index=0;
        memset(md,0,MD_DIGEST_LENGTH);
-       count=0;
+       md_count[0]=0;
+       md_count[1]=0;
        }
 
-void RAND_seed(buf,num)
-unsigned char *buf;
+static void ssleay_rand_seed(buf,num)
+const void *buf;
 int num;
        {
        int i,j,k,st_idx,st_num;
@@ -150,7 +171,7 @@ int num;
        st_num=state_num;
 
        state_index=(state_index+num);
-       if (state_index > STATE_SIZE)
+       if (state_index >= STATE_SIZE)
                {
                state_index%=STATE_SIZE;
                state_num=STATE_SIZE;
@@ -179,9 +200,11 @@ int num;
                        MD_Update(&m,&(state[st_idx]),j);
                        
                MD_Update(&m,buf,j);
+               MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));
                MD_Final(md,&m);
+               md_count[1]++;
 
-               buf+=j;
+               buf=(char *)buf + j;
 
                for (k=0; k<j; k++)
                        {
@@ -196,7 +219,7 @@ int num;
        memset((char *)&m,0,sizeof(m));
        }
 
-void RAND_bytes(buf,num)
+static void ssleay_rand_bytes(buf,num)
 unsigned char *buf;
 int num;
        {
@@ -226,17 +249,17 @@ int num;
                CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
                /* put in some default random data, we need more than
                 * just this */
-               RAND_seed((unsigned char *)&m,sizeof(m));
+               RAND_seed(&m,sizeof(m));
 #ifndef MSDOS
                l=getpid();
-               RAND_seed((unsigned char *)&l,sizeof(l));
+               RAND_seed(&l,sizeof(l));
                l=getuid();
-               RAND_seed((unsigned char *)&l,sizeof(l));
+               RAND_seed(&l,sizeof(l));
 #endif
                l=time(NULL);
-               RAND_seed((unsigned char *)&l,sizeof(l));
+               RAND_seed(&l,sizeof(l));
 
-#ifdef DEVRANDOM
+/* #ifdef DEVRANDOM */
                /* 
                 * Use a random entropy pool device.
                 * Linux 1.3.x and FreeBSD-Current has 
@@ -246,17 +269,17 @@ int num;
                 */
                if ((fh = fopen(DEVRANDOM, "r")) != NULL)
                        {
-                       unsigned char buf[32];
+                       unsigned char tmpbuf[32];
 
-                       fread((unsigned char *)buf,1,32,fh);
+                       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 :-) */
                        fclose(fh);
-                       RAND_seed(buf,32);
-                       memset(buf,0,32);
+                       RAND_seed(tmpbuf,32);
+                       memset(tmpbuf,0,32);
                        }
-#endif
+/* #endif */
 #ifdef PURIFY
                memset(state,0,STATE_SIZE);
                memset(md,0,MD_DIGEST_LENGTH);
@@ -278,6 +301,7 @@ int num;
                num-=j;
                MD_Init(&m);
                MD_Update(&m,&(md[MD_DIGEST_LENGTH/2]),MD_DIGEST_LENGTH/2);
+               MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));
 #ifndef PURIFY
                MD_Update(&m,buf,j); /* purify complains */
 #endif
@@ -301,7 +325,8 @@ int num;
                }
 
        MD_Init(&m);
-       MD_Update(&m,(unsigned char *)&count,sizeof(count)); count++;
+       MD_Update(&m,(unsigned char *)&(md_count[0]),sizeof(md_count));
+       md_count[0]++;
        MD_Update(&m,md,MD_DIGEST_LENGTH);
        MD_Final(md,&m);
        memset(&m,0,sizeof(m));
@@ -386,7 +411,7 @@ void RAND_screen(void)
        GetBitmapBits(hBitmap, size, bmbits);
 
        /* Get the MD5 of the bitmap */
-       MD5(bmbits,size,md);
+       MD(bmbits,size,md);
 
        /* Seed the random generator with the MD5 digest */
        RAND_seed(md, MD_DIGEST_LENGTH);