2 #define OPENSSL_FIPSAPI
3 #include <openssl/opensslconf.h>
8 int main(int argc, char **argv)
10 printf("No FIPS DSA support\n");
15 #include <openssl/bn.h>
16 #include <openssl/dsa.h>
17 #include <openssl/fips.h>
18 #include <openssl/err.h>
19 #include <openssl/evp.h>
25 static int parse_mod(char *line, int *pdsa2, int *pL, int *pN,
29 char *keyword, *value;
32 p = strchr(line, ',');
44 if (!parse_line2(&keyword, &value, lbuf, line, 0))
46 if (strcmp(keyword, "L"))
51 p = strchr(line, ',');
53 p = strchr(line, ']');
57 if (!parse_line2(&keyword, &value, lbuf, line, 0))
59 if (strcmp(keyword, "N"))
65 p = strchr(line, ']');
72 if (!strcmp(p, "SHA-1"))
74 else if (!strcmp(p, "SHA-224"))
76 else if (!strcmp(p, "SHA-256"))
78 else if (!strcmp(p, "SHA-384"))
80 else if (!strcmp(p, "SHA-512"))
87 static void primes(FILE *in, FILE *out)
91 char *keyword, *value;
93 while(fgets(buf,sizeof buf,in) != NULL)
96 if (!parse_line(&keyword, &value, lbuf, buf))
98 if(!strcmp(keyword,"Prime"))
103 do_hex2bn(&pp,value);
104 fprintf(out, "result= %c\n",
105 BN_is_prime_ex(pp,20,NULL,NULL) ? 'P' : 'F');
110 int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
111 const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
112 unsigned char *seed_out,
113 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
114 int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
115 const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
116 int idx, unsigned char *seed_out,
117 int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
119 static void pqg(FILE *in, FILE *out)
123 char *keyword, *value;
125 const EVP_MD *md = NULL;
126 BIGNUM *p = NULL, *q = NULL;
127 enum pqtype { PQG_NONE, PQG_PQ, PQG_G, PQG_GCANON}
129 int seedlen=-1, idxlen, idx = -1;
130 unsigned char seed[1024], idtmp[1024];
132 while(fgets(buf,sizeof buf,in) != NULL)
136 if (strstr(buf, "Probable"))
138 else if (strstr(buf, "Unverifiable"))
140 else if (strstr(buf, "Canonical"))
141 pqg_type = PQG_GCANON;
143 if (!parse_line(&keyword, &value, lbuf, buf))
148 if (strcmp(keyword, "Num"))
150 if(!strcmp(keyword,"[mod"))
152 if (!parse_mod(value, &dsa2, &L, &N, &md))
154 fprintf(stderr, "Mod Parse Error\n");
158 else if(!strcmp(keyword,"N")
159 || (!strcmp(keyword, "Num") && pqg_type == PQG_PQ))
168 dsa = FIPS_dsa_new();
170 if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md,
174 fprintf(stderr, "Parameter Generation error\n");
177 if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md,
179 &counter, &h, NULL) <= 0)
181 fprintf(stderr, "Parameter Generation error\n");
185 do_bn_print_name(out, "P",dsa->p);
186 do_bn_print_name(out, "Q",dsa->q);
188 do_bn_print_name(out, "G",dsa->g);
189 OutputValue(dsa2 ? "domain_parameter_seed" : "Seed",
190 seed, M_EVP_MD_size(md), out, 0);
193 fprintf(out, "c = %d\n",counter);
194 fprintf(out, "H = %lx\n\n",h);
198 fprintf(out, "counter = %d\n",counter);
203 else if(!strcmp(keyword,"P"))
205 else if(!strcmp(keyword,"Q"))
207 else if(!strcmp(keyword,"domain_parameter_seed"))
208 seedlen = hex2bin(value, seed);
209 else if(!strcmp(keyword,"firstseed"))
210 seedlen = hex2bin(value, seed);
211 else if(!strcmp(keyword,"pseed"))
212 seedlen += hex2bin(value, seed + seedlen);
213 else if(!strcmp(keyword,"qseed"))
214 seedlen += hex2bin(value, seed + seedlen);
215 else if(!strcmp(keyword,"index"))
217 idxlen = hex2bin(value, idtmp);
220 fprintf(stderr, "Index value error\n");
225 if ((idx >= 0 && pqg_type == PQG_GCANON) || (q && pqg_type == PQG_G))
228 dsa = FIPS_dsa_new();
232 if (dsa_builtin_paramgen2(dsa, L, N, md,
233 seed, seedlen, idx, NULL,
234 NULL, NULL, NULL) <= 0)
236 fprintf(stderr, "Parameter Generation error\n");
239 do_bn_print_name(out, "G",dsa->g);
246 static void pqgver(FILE *in, FILE *out)
250 char *keyword, *value;
251 BIGNUM *p = NULL, *q = NULL, *g = NULL;
252 int counter=-1, counter2;
253 unsigned long h=0, h2;
255 int dsa2, L, N, part_test = 0;
256 const EVP_MD *md = NULL;
257 int seedlen=-1, idxlen, idx = -1;
258 unsigned char seed[1024], idtmp[1024];
260 while(fgets(buf,sizeof buf,in) != NULL)
262 if (!parse_line(&keyword, &value, lbuf, buf))
273 if(!strcmp(keyword,"[mod"))
275 if (!parse_mod(value, &dsa2, &L, &N, &md))
277 fprintf(stderr, "Mod Parse Error\n");
281 else if(!strcmp(keyword,"P"))
283 else if(!strcmp(keyword,"Q"))
285 else if(!strcmp(keyword,"G"))
287 else if(!strcmp(keyword,"Seed")
288 || !strcmp(keyword,"domain_parameter_seed"))
290 seedlen = hex2bin(value, seed);
291 if (!dsa2 && seedlen != 20)
293 fprintf(stderr, "Seed parse length error\n");
299 else if(!strcmp(keyword,"index"))
301 idxlen = hex2bin(value, idtmp);
304 fprintf(stderr, "Index value error\n");
309 else if(!strcmp(keyword,"c"))
310 counter = atoi(buf+4);
312 if(!strcmp(keyword,"H") || part_test)
316 if (!p || !q || (!g && !part_test))
318 fprintf(stderr, "Parse Error\n");
321 dsa = FIPS_dsa_new();
328 if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md,
330 &counter2, &h2, NULL))
332 fprintf(stderr, "Parameter Generation error\n");
335 if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md,
336 seed, seedlen, idx, NULL,
337 &counter2, &h2, NULL) < 0)
339 fprintf(stderr, "Parameter Generation error\n");
345 if (BN_cmp(dsa->g, g))
346 fprintf(out, "Result = F\n");
348 fprintf(out, "Result = P\n");
350 else if (BN_cmp(dsa->p, p) || BN_cmp(dsa->q, q) ||
352 ((BN_cmp(dsa->g, g) || (counter != counter2) || (h != h2)))))
353 fprintf(out, "Result = F\n");
355 fprintf(out, "Result = P\n");
375 /* Keypair verification routine. NB: this isn't part of the standard FIPS140-2
376 * algorithm tests. It is an additional test to perform sanity checks on the
377 * output of the KeyPair test.
380 static int dss_paramcheck(int L, int N, BIGNUM *p, BIGNUM *q, BIGNUM *g,
384 if (BN_num_bits(p) != L)
386 if (BN_num_bits(q) != N)
388 if (BN_is_prime_ex(p, BN_prime_checks, ctx, NULL) != 1)
390 if (BN_is_prime_ex(q, BN_prime_checks, ctx, NULL) != 1)
393 if (!BN_mod(rem, p, q, ctx) || !BN_is_one(rem)
394 || (BN_cmp(g, BN_value_one()) <= 0)
395 || !BN_mod_exp(rem, g, q, p, ctx) || !BN_is_one(rem))
405 static void keyver(FILE *in, FILE *out)
409 char *keyword, *value;
410 BIGNUM *p = NULL, *q = NULL, *g = NULL, *X = NULL, *Y = NULL;
419 while(fgets(buf,sizeof buf,in) != NULL)
421 if (!parse_line(&keyword, &value, lbuf, buf))
426 if(!strcmp(keyword,"[mod"))
438 if (!parse_mod(value, &dsa2, &L, &N, NULL))
440 fprintf(stderr, "Mod Parse Error\n");
444 else if(!strcmp(keyword,"P"))
446 else if(!strcmp(keyword,"Q"))
448 else if(!strcmp(keyword,"G"))
450 else if(!strcmp(keyword,"X"))
452 else if(!strcmp(keyword,"Y"))
455 if (!p || !q || !g || !X || !Y)
457 fprintf(stderr, "Parse Error\n");
460 do_bn_print_name(out, "P",p);
461 do_bn_print_name(out, "Q",q);
462 do_bn_print_name(out, "G",g);
463 do_bn_print_name(out, "X",X);
464 do_bn_print_name(out, "Y",Y);
467 if (dss_paramcheck(L, N, p, q, g, ctx))
473 fprintf(out, "Result = F\n");
476 if (!BN_mod_exp(Y2, g, X, p, ctx) || BN_cmp(Y2, Y))
477 fprintf(out, "Result = F\n");
479 fprintf(out, "Result = P\n");
497 static void keypair(FILE *in, FILE *out)
501 char *keyword, *value;
504 while(fgets(buf,sizeof buf,in) != NULL)
506 if (!parse_line(&keyword, &value, lbuf, buf))
510 if(!strcmp(keyword,"[mod"))
512 if (!parse_mod(value, &dsa2, &L, &N, NULL))
514 fprintf(stderr, "Mod Parse Error\n");
519 else if(!strcmp(keyword,"N"))
524 dsa = FIPS_dsa_new();
525 if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, NULL, NULL, 0,
526 NULL, NULL, NULL, NULL))
528 fprintf(stderr, "Parameter Generation error\n");
531 if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, NULL, NULL, 0, -1,
532 NULL, NULL, NULL, NULL) <= 0)
534 fprintf(stderr, "Parameter Generation error\n");
537 do_bn_print_name(out, "P",dsa->p);
538 do_bn_print_name(out, "Q",dsa->q);
539 do_bn_print_name(out, "G",dsa->g);
544 if (!DSA_generate_key(dsa))
547 do_bn_print_name(out, "X",dsa->priv_key);
548 do_bn_print_name(out, "Y",dsa->pub_key);
555 static void siggen(FILE *in, FILE *out)
559 char *keyword, *value;
561 const EVP_MD *md = NULL;
564 while(fgets(buf,sizeof buf,in) != NULL)
566 if (!parse_line(&keyword, &value, lbuf, buf))
572 if(!strcmp(keyword,"[mod"))
574 if (!parse_mod(value, &dsa2, &L, &N, &md))
576 fprintf(stderr, "Mod Parse Error\n");
581 dsa = FIPS_dsa_new();
582 if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md, NULL, 0,
583 NULL, NULL, NULL, NULL))
585 fprintf(stderr, "Parameter Generation error\n");
588 if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md, NULL, 0, -1,
589 NULL, NULL, NULL, NULL) <= 0)
591 fprintf(stderr, "Parameter Generation error\n");
594 do_bn_print_name(out, "P",dsa->p);
595 do_bn_print_name(out, "Q",dsa->q);
596 do_bn_print_name(out, "G",dsa->g);
599 else if(!strcmp(keyword,"Msg"))
601 unsigned char msg[1024];
605 FIPS_md_ctx_init(&mctx);
607 n=hex2bin(value,msg);
609 if (!DSA_generate_key(dsa))
611 do_bn_print_name(out, "Y",dsa->pub_key);
613 FIPS_digestinit(&mctx, md);
614 FIPS_digestupdate(&mctx, msg, n);
615 sig = FIPS_dsa_sign_ctx(dsa, &mctx);
617 do_bn_print_name(out, "R",sig->r);
618 do_bn_print_name(out, "S",sig->s);
620 FIPS_dsa_sig_free(sig);
621 FIPS_md_ctx_cleanup(&mctx);
628 static void sigver(FILE *in, FILE *out)
633 unsigned char msg[1024];
634 char *keyword, *value;
637 const EVP_MD *md = NULL;
638 DSA_SIG sg, *sig = &sg;
643 while(fgets(buf,sizeof buf,in) != NULL)
645 if (!parse_line(&keyword, &value, lbuf, buf))
651 if(!strcmp(keyword,"[mod"))
653 if (!parse_mod(value, &dsa2, &L, &N, &md))
655 fprintf(stderr, "Mod Parse Error\n");
660 dsa = FIPS_dsa_new();
662 else if(!strcmp(keyword,"P"))
663 dsa->p=hex2bn(value);
664 else if(!strcmp(keyword,"Q"))
665 dsa->q=hex2bn(value);
666 else if(!strcmp(keyword,"G"))
667 dsa->g=hex2bn(value);
668 else if(!strcmp(keyword,"Msg"))
669 n=hex2bin(value,msg);
670 else if(!strcmp(keyword,"Y"))
671 dsa->pub_key=hex2bn(value);
672 else if(!strcmp(keyword,"R"))
673 sig->r=hex2bn(value);
674 else if(!strcmp(keyword,"S"))
678 FIPS_md_ctx_init(&mctx);
679 sig->s=hex2bn(value);
681 FIPS_digestinit(&mctx, md);
682 FIPS_digestupdate(&mctx, msg, n);
684 r = FIPS_dsa_verify_ctx(dsa, &mctx, sig);
686 FIPS_md_ctx_cleanup(&mctx);
688 fprintf(out, "Result = %c\n\n", r == 1 ? 'P' : 'F');
693 int main(int argc,char **argv)
698 in = fopen(argv[2], "r");
701 fprintf(stderr, "Error opening input file\n");
704 out = fopen(argv[3], "w");
707 fprintf(stderr, "Error opening output file\n");
718 fprintf(stderr,"%s [prime|pqg|pqgver|keypair|keyver|siggen|sigver]\n",argv[0]);
722 if(!strcmp(argv[1],"prime"))
724 else if(!strcmp(argv[1],"pqg"))
726 else if(!strcmp(argv[1],"pqgver"))
728 else if(!strcmp(argv[1],"keypair"))
730 else if(!strcmp(argv[1],"keyver"))
732 else if(!strcmp(argv[1],"siggen"))
734 else if(!strcmp(argv[1],"sigver"))
738 fprintf(stderr,"Don't know how to %s.\n",argv[1]);