Fix output format for DSA2 parameter generation.
[openssl.git] / fips / dsa / fips_dssvs.c
index ce1bc18f5304ea6f64063e3d83c5e6ea5396d8a1..3feb0ffe7c96142b1a035fa03a05805bbeafffa8 100644 (file)
@@ -123,30 +123,45 @@ static void pqg(FILE *in, FILE *out)
     char *keyword, *value;
     int dsa2, L, N;
     const EVP_MD *md = NULL;
+    BIGNUM *p = NULL, *q = NULL;
+    enum pqtype { PQG_NONE, PQG_PQ, PQG_G, PQG_GCANON}
+               pqg_type = PQG_NONE;
+    int seedlen=-1, idxlen, idx = -1;
+    unsigned char seed[1024], idtmp[1024];
 
     while(fgets(buf,sizeof buf,in) != NULL)
        {
+       if (buf[0] == '[')
+               {
+               if (strstr(buf, "Probable"))
+                       pqg_type = PQG_PQ;
+               else if (strstr(buf, "Unverifiable"))
+                       pqg_type = PQG_G;
+               else if (strstr(buf, "Canonical"))
+                       pqg_type = PQG_GCANON;
+               }
        if (!parse_line(&keyword, &value, lbuf, buf))
                {
                fputs(buf,out);
                continue;
                }
+       if (strcmp(keyword, "Num") || pqg_type != PQG_PQ)
+               fputs(buf,out);
        if(!strcmp(keyword,"[mod"))
            {
-           fputs(buf,out);
            if (!parse_mod(value, &dsa2, &L, &N, &md))
                {
                fprintf(stderr, "Mod Parse Error\n");
                exit (1);
                }
            }
-       else if(!strcmp(keyword,"N"))
+       else if(!strcmp(keyword,"N") 
+               || (!strcmp(keyword, "Num") && pqg_type == PQG_PQ))
            {
            int n=atoi(value);
 
            while(n--)
                {
-               unsigned char seed[EVP_MAX_MD_SIZE];
                DSA *dsa;
                int counter;
                unsigned long h;
@@ -169,14 +184,62 @@ static void pqg(FILE *in, FILE *out)
  
                do_bn_print_name(out, "P",dsa->p);
                do_bn_print_name(out, "Q",dsa->q);
-               do_bn_print_name(out, "G",dsa->g);
-               OutputValue("Seed",seed, M_EVP_MD_size(md), out, 0);
-               fprintf(out, "c = %d\n",counter);
-               fprintf(out, "H = %lx\n\n",h);
+               if (!dsa2)
+                       do_bn_print_name(out, "G",dsa->g);
+               OutputValue(dsa2 ? "domain_parameter_seed" : "Seed",
+                               seed, M_EVP_MD_size(md), out, 0);
+               if (!dsa2)
+                       {
+                       fprintf(out, "c = %d\n",counter);
+                       fprintf(out, "H = %lx\n\n",h);
+                       }
+               else
+                       {
+                       fprintf(out, "counter = %d\n",counter);
+                       fputs("\n", out);
+                       }
                }
            }
-       else
-           fputs(buf,out);
+       else if(!strcmp(keyword,"P"))
+           p=hex2bn(value);
+       else if(!strcmp(keyword,"Q"))
+           q=hex2bn(value);
+       else if(!strcmp(keyword,"domain_parameter_seed"))
+           seedlen = hex2bin(value, seed);
+       else if(!strcmp(keyword,"firstseed"))
+           seedlen = hex2bin(value, seed);
+       else if(!strcmp(keyword,"pseed"))
+           seedlen += hex2bin(value, seed + seedlen);
+       else if(!strcmp(keyword,"qseed"))
+           seedlen += hex2bin(value, seed + seedlen);
+       else if(!strcmp(keyword,"index"))
+           {
+           idxlen = hex2bin(value, idtmp);
+            if (idxlen != 1)
+               {
+               fprintf(stderr, "Index value error\n");
+               exit (1);
+               }
+           idx = idtmp[0];
+           }
+       if ((idx >= 0 && pqg_type == PQG_GCANON) || (q && pqg_type == PQG_G))
+               {
+               DSA *dsa;
+               dsa = FIPS_dsa_new();
+               dsa->p = p;
+               dsa->q = q;
+               p = q = NULL;
+               if (dsa_builtin_paramgen2(dsa, L, N, md,
+                                               seed, seedlen, idx, NULL,
+                                               NULL, NULL, NULL) <= 0)
+                       {
+                       fprintf(stderr, "Parameter Generation error\n");
+                       exit(1);
+                       }
+               do_bn_print_name(out, "G",dsa->g);
+               FIPS_dsa_free(dsa);
+               idx = -1;
+               }
        }
     }
 
@@ -261,6 +324,7 @@ static void pqgver(FILE *in, FILE *out)
                dsa->p = BN_dup(p);
                dsa->q = BN_dup(q);
                }
+           no_err = 1;
            if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md,
                                        seed, seedlen, NULL,
                                        &counter2, &h2, NULL))
@@ -275,6 +339,7 @@ static void pqgver(FILE *in, FILE *out)
                        fprintf(stderr, "Parameter Generation error\n");
                        exit(1);
                        }
+           no_err = 0;
            if (idx >= 0)
                {
                if (BN_cmp(dsa->g, g))