Check for uninitialised DRBG_CTX and don't free up default DRBG_CTX.
[openssl.git] / fips / rand / fips_rngvs.c
index 972acce6d1eb75c460e5fdf7c78d184fc2bab58e..ac0a526573d447534af576905432f75d5b62ca1a 100644 (file)
@@ -28,13 +28,12 @@ int main(int argc, char **argv)
 #include <openssl/err.h>
 #include <openssl/rand.h>
 #include <openssl/fips_rand.h>
-#include <openssl/x509v3.h>
 #include <string.h>
 #include <ctype.h>
 
 #include "fips_utl.h"
 
-static void vst()
+static void vst(FILE *in, FILE *out)
     {
     unsigned char *key = NULL;
     unsigned char *v = NULL;
@@ -47,9 +46,9 @@ static void vst()
 
     keylen = 0;
 
-    while(fgets(buf,sizeof buf,stdin) != NULL)
+    while(fgets(buf,sizeof buf,in) != NULL)
        {
-       fputs(buf,stdout);
+       fputs(buf,out);
        if(!strncmp(buf,"[AES 128-Key]", 13))
                keylen = 16;
        else if(!strncmp(buf,"[AES 192-Key]", 13))
@@ -91,16 +90,16 @@ static void vst()
                return;
                }
 
-           FIPS_rand_set_key(key, keylen);
-           FIPS_rand_seed(v,16);
-           FIPS_rand_set_dt(dt);
-           if (FIPS_rand_bytes(ret,16) <= 0)
+           FIPS_x931_set_key(key, keylen);
+           FIPS_x931_seed(v,16);
+           FIPS_x931_set_dt(dt);
+           if (FIPS_x931_bytes(ret,16) <= 0)
                {
                fprintf(stderr, "Error getting PRNG value\n");
                return;
                }
 
-           pv("R",ret,16);
+           OutputValue("R", ret, 16, out, 0);
            OPENSSL_free(key);
            key = NULL;
            OPENSSL_free(dt);
@@ -111,7 +110,7 @@ static void vst()
        }
     }
 
-static void mct()
+static void mct(FILE *in, FILE *out)
     {
     unsigned char *key = NULL;
     unsigned char *v = NULL;
@@ -125,9 +124,9 @@ static void mct()
 
     keylen = 0;
 
-    while(fgets(buf,sizeof buf,stdin) != NULL)
+    while(fgets(buf,sizeof buf,in) != NULL)
        {
-       fputs(buf,stdout);
+       fputs(buf,out);
        if(!strncmp(buf,"[AES 128-Key]", 13))
                keylen = 16;
        else if(!strncmp(buf,"[AES 192-Key]", 13))
@@ -169,12 +168,12 @@ static void mct()
                return;
                }
 
-           FIPS_rand_set_key(key, keylen);
-           FIPS_rand_seed(v,16);
+           FIPS_x931_set_key(key, keylen);
+           FIPS_x931_seed(v,16);
            for (i = 0; i < 10000; i++)
                {
-                   FIPS_rand_set_dt(dt);
-                   if (FIPS_rand_bytes(ret,16) <= 0)
+                   FIPS_x931_set_dt(dt);
+                   if (FIPS_x931_bytes(ret,16) <= 0)
                        {
                        fprintf(stderr, "Error getting PRNG value\n");
                        return;
@@ -188,7 +187,7 @@ static void mct()
                        }
                }
 
-           pv("R",ret,16);
+           OutputValue("R", ret, 16, out, 0);
            OPENSSL_free(key);
            key = NULL;
            OPENSSL_free(dt);
@@ -201,30 +200,55 @@ static void mct()
 
 int main(int argc,char **argv)
     {
-    if(argc != 2)
+    FILE *in, *out;
+    if (argc == 4)
+       {
+       in = fopen(argv[2], "r");
+       if (!in)
+               {
+               fprintf(stderr, "Error opening input file\n");
+               exit(1);
+               }
+       out = fopen(argv[3], "w");
+       if (!out)
+               {
+               fprintf(stderr, "Error opening output file\n");
+               exit(1);
+               }
+       }
+    else if (argc == 2)
+       {
+       in = stdin;
+       out = stdout;
+       }
+    else
        {
        fprintf(stderr,"%s [mct|vst]\n",argv[0]);
        exit(1);
        }
-    fips_set_error_print();
-    if(!FIPS_mode_set(1))
-       exit(1);
-    FIPS_rand_reset();
-    if (!FIPS_rand_test_mode())
+    fips_algtest_init();
+    FIPS_x931_reset();
+    if (!FIPS_x931_test_mode())
        {
        fprintf(stderr, "Error setting PRNG test mode\n");
        exit(1);
        }
     if(!strcmp(argv[1],"mct"))
-       mct();
+       mct(in, out);
     else if(!strcmp(argv[1],"vst"))
-       vst();
+       vst(in, out);
     else
        {
        fprintf(stderr,"Don't know how to %s.\n",argv[1]);
        exit(1);
        }
 
+    if (argc == 4)
+       {
+       fclose(in);
+       fclose(out);
+       }
+
     return 0;
     }
 #endif