Various randomness handling bugfixes and improvements --
[openssl.git] / apps / genrsa.c
index 1cba9f59256c396fc52e14a741443d16e6dafbd3..96df5703c482e4d1b5b4f7f282286d957cb58973 100644 (file)
@@ -63,7 +63,6 @@
 #include <sys/stat.h>
 #include "apps.h"
 #include <openssl/bio.h>
 #include <sys/stat.h>
 #include "apps.h"
 #include <openssl/bio.h>
-#include <openssl/rand.h>
 #include <openssl/err.h>
 #include <openssl/bn.h>
 #include <openssl/rsa.h>
 #include <openssl/err.h>
 #include <openssl/bn.h>
 #include <openssl/rsa.h>
 #undef PROG
 #define PROG genrsa_main
 
 #undef PROG
 #define PROG genrsa_main
 
-static void MS_CALLBACK genrsa_cb(int p, int n, char *arg);
-static long gr_load_rand(char *names);
+static void MS_CALLBACK genrsa_cb(int p, int n, void *arg);
 int MAIN(int argc, char **argv)
        {
        int ret=1;
 int MAIN(int argc, char **argv)
        {
        int ret=1;
-       char buffer[200];
        RSA *rsa=NULL;
        int i,num=DEFBITS;
        RSA *rsa=NULL;
        int i,num=DEFBITS;
-       long rnum=0,l;
+       long l;
        EVP_CIPHER *enc=NULL;
        unsigned long f4=RSA_F4;
        char *outfile=NULL;
        EVP_CIPHER *enc=NULL;
        unsigned long f4=RSA_F4;
        char *outfile=NULL;
-       char *inrand=NULL,*randfile;
+       char *inrand=NULL;
        BIO *out=NULL;
 
        apps_startup();
        BIO *out=NULL;
 
        apps_startup();
@@ -165,41 +162,19 @@ bad:
                        }
                }
 
                        }
                }
 
-#ifdef WINDOWS
-       BIO_printf(bio_err,"Loading 'screen' into random state -");
-       BIO_flush(bio_err);
-       RAND_screen();
-       BIO_printf(bio_err," done\n");
-#endif
-       randfile=RAND_file_name(buffer,200);
-       if ((randfile == NULL) ||
-                !(rnum=(long)RAND_load_file(randfile,1024L*1024L)))
-               {
-               BIO_printf(bio_err,"unable to load 'random state'\n");
-               }
-
-       if (inrand == NULL)
-               {
-               if (rnum == 0)
-                       {
-                       BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
-                       }
-               }
-       else
+       if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL)
                {
                {
-               rnum+=gr_load_rand(inrand);
+               BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n");
                }
                }
-       if (rnum != 0)
-               BIO_printf(bio_err,"%ld semi-random bytes loaded\n",rnum);
+       if (inrand != NULL)
+               BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
+                       app_RAND_load_files(inrand));
 
        BIO_printf(bio_err,"Generating RSA private key, %d bit long modulus\n",
                num);
 
        BIO_printf(bio_err,"Generating RSA private key, %d bit long modulus\n",
                num);
-       rsa=RSA_generate_key(num,f4,genrsa_cb,(char *)bio_err);
+       rsa=RSA_generate_key(num,f4,genrsa_cb,bio_err);
                
                
-       if (randfile == NULL)
-               BIO_printf(bio_err,"unable to write 'random state'\n");
-       else
-               RAND_write_file(randfile);
+       app_RAND_write_file(NULL, bio_err);
 
        if (rsa == NULL) goto err;
        
 
        if (rsa == NULL) goto err;
        
@@ -215,7 +190,7 @@ bad:
                l+=rsa->e->d[i];
                }
        BIO_printf(bio_err,"e is %ld (0x%lX)\n",l,l);
                l+=rsa->e->d[i];
                }
        BIO_printf(bio_err,"e is %ld (0x%lX)\n",l,l);
-       if (!PEM_write_bio_RSAPrivateKey(out,rsa,enc,NULL,0,NULL))
+       if (!PEM_write_bio_RSAPrivateKey(out,rsa,enc,NULL,0,NULL,NULL))
                goto err;
 
        ret=0;
                goto err;
 
        ret=0;
@@ -227,7 +202,7 @@ err:
        EXIT(ret);
        }
 
        EXIT(ret);
        }
 
-static void MS_CALLBACK genrsa_cb(int p, int n, char *arg)
+static void MS_CALLBACK genrsa_cb(int p, int n, void *arg)
        {
        char c='*';
 
        {
        char c='*';
 
@@ -236,31 +211,9 @@ static void MS_CALLBACK genrsa_cb(int p, int n, char *arg)
        if (p == 2) c='*';
        if (p == 3) c='\n';
        BIO_write((BIO *)arg,&c,1);
        if (p == 2) c='*';
        if (p == 3) c='\n';
        BIO_write((BIO *)arg,&c,1);
-       BIO_flush((BIO *)arg);
+       (void)BIO_flush((BIO *)arg);
 #ifdef LINT
        p=n;
 #endif
        }
 #ifdef LINT
        p=n;
 #endif
        }
-
-static long gr_load_rand(char *name)
-       {
-       char *p,*n;
-       int last;
-       long tot=0;
-
-       for (;;)
-               {
-               last=0;
-               for (p=name; ((*p != '\0') && (*p != LIST_SEPARATOR_CHAR)); p++);
-               if (*p == '\0') last=1;
-               *p='\0';
-               n=name;
-               name=p+1;
-               if (*n == '\0') break;
-
-               tot+=RAND_load_file(n,1024L*1024L);
-               if (last) break;
-               }
-       return(tot);
-       }
 #endif
 #endif