fips_dssvs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. #define OPENSSL_FIPSAPI
  2. #include <openssl/opensslconf.h>
  3. #ifndef OPENSSL_FIPS
  4. #include <stdio.h>
  5. int main(int argc, char **argv)
  6. {
  7. printf("No FIPS DSA support\n");
  8. return(0);
  9. }
  10. #else
  11. #include <openssl/bn.h>
  12. #include <openssl/dsa.h>
  13. #include <openssl/fips.h>
  14. #include <openssl/err.h>
  15. #include <openssl/evp.h>
  16. #include <string.h>
  17. #include <ctype.h>
  18. #include "fips_utl.h"
  19. static int parse_mod(char *line, int *pdsa2, int *pL, int *pN,
  20. const EVP_MD **pmd)
  21. {
  22. char lbuf[10240];
  23. char *keyword, *value;
  24. char *p;
  25. p = strchr(line, ',');
  26. if (!p)
  27. {
  28. *pL = atoi(line);
  29. *pdsa2 = 0;
  30. *pN = 160;
  31. if (pmd)
  32. *pmd = EVP_sha1();
  33. return 1;
  34. }
  35. *pdsa2 = 1;
  36. *p = 0;
  37. if (!parse_line2(&keyword, &value, lbuf, line, 0))
  38. return 0;
  39. if (strcmp(keyword, "L"))
  40. return 0;
  41. *pL = atoi(value);
  42. strcpy(line, p + 1);
  43. if (pmd)
  44. p = strchr(line, ',');
  45. else
  46. p = strchr(line, ']');
  47. if (!p)
  48. return 0;
  49. *p = 0;
  50. if (!parse_line2(&keyword, &value, lbuf, line, 0))
  51. return 0;
  52. if (strcmp(keyword, "N"))
  53. return 0;
  54. *pN = atoi(value);
  55. if (!pmd)
  56. return 1;
  57. strcpy(line, p + 1);
  58. p = strchr(line, ']');
  59. if (!p)
  60. return 0;
  61. *p = 0;
  62. p = line;
  63. while(isspace(*p))
  64. p++;
  65. if (!strcmp(p, "SHA-1"))
  66. *pmd = EVP_sha1();
  67. else if (!strcmp(p, "SHA-224"))
  68. *pmd = EVP_sha224();
  69. else if (!strcmp(p, "SHA-256"))
  70. *pmd = EVP_sha256();
  71. else if (!strcmp(p, "SHA-384"))
  72. *pmd = EVP_sha384();
  73. else if (!strcmp(p, "SHA-512"))
  74. *pmd = EVP_sha512();
  75. else
  76. return 0;
  77. return 1;
  78. }
  79. static void primes(FILE *in, FILE *out)
  80. {
  81. char buf[10240];
  82. char lbuf[10240];
  83. char *keyword, *value;
  84. while(fgets(buf,sizeof buf,in) != NULL)
  85. {
  86. fputs(buf,out);
  87. if (!parse_line(&keyword, &value, lbuf, buf))
  88. continue;
  89. if(!strcmp(keyword,"Prime"))
  90. {
  91. BIGNUM *pp;
  92. pp=BN_new();
  93. do_hex2bn(&pp,value);
  94. fprintf(out, "result= %c" RESP_EOL,
  95. BN_is_prime_ex(pp,20,NULL,NULL) ? 'P' : 'F');
  96. }
  97. }
  98. }
  99. int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
  100. const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
  101. unsigned char *seed_out,
  102. int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
  103. int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
  104. const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
  105. int idx, unsigned char *seed_out,
  106. int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
  107. int dsa_paramgen_check_g(DSA *dsa);
  108. static void pqg(FILE *in, FILE *out)
  109. {
  110. char buf[1024];
  111. char lbuf[1024];
  112. char *keyword, *value;
  113. int dsa2, L, N;
  114. const EVP_MD *md = NULL;
  115. BIGNUM *p = NULL, *q = NULL;
  116. enum pqtype { PQG_NONE, PQG_PQ, PQG_G, PQG_GCANON}
  117. pqg_type = PQG_NONE;
  118. int seedlen=-1, idxlen, idx = -1;
  119. unsigned char seed[1024], idtmp[1024];
  120. while(fgets(buf,sizeof buf,in) != NULL)
  121. {
  122. if (buf[0] == '[')
  123. {
  124. if (strstr(buf, "Probable"))
  125. pqg_type = PQG_PQ;
  126. else if (strstr(buf, "Unverifiable"))
  127. pqg_type = PQG_G;
  128. else if (strstr(buf, "Canonical"))
  129. pqg_type = PQG_GCANON;
  130. }
  131. if (!parse_line(&keyword, &value, lbuf, buf))
  132. {
  133. fputs(buf,out);
  134. continue;
  135. }
  136. if (strcmp(keyword, "Num"))
  137. fputs(buf,out);
  138. if(!strcmp(keyword,"[mod"))
  139. {
  140. if (!parse_mod(value, &dsa2, &L, &N, &md))
  141. {
  142. fprintf(stderr, "Mod Parse Error\n");
  143. exit (1);
  144. }
  145. }
  146. else if(!strcmp(keyword,"N")
  147. || (!strcmp(keyword, "Num") && pqg_type == PQG_PQ))
  148. {
  149. int n=atoi(value);
  150. while(n--)
  151. {
  152. DSA *dsa;
  153. int counter;
  154. unsigned long h;
  155. dsa = FIPS_dsa_new();
  156. if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md,
  157. NULL, 0, seed,
  158. &counter, &h, NULL))
  159. {
  160. fprintf(stderr, "Parameter Generation error\n");
  161. exit(1);
  162. }
  163. if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md,
  164. NULL, 0, -1, seed,
  165. &counter, &h, NULL) <= 0)
  166. {
  167. fprintf(stderr, "Parameter Generation error\n");
  168. exit(1);
  169. }
  170. do_bn_print_name(out, "P",dsa->p);
  171. do_bn_print_name(out, "Q",dsa->q);
  172. if (!dsa2)
  173. do_bn_print_name(out, "G",dsa->g);
  174. OutputValue(dsa2 ? "domain_parameter_seed" : "Seed",
  175. seed, M_EVP_MD_size(md), out, 0);
  176. if (!dsa2)
  177. {
  178. fprintf(out, "c = %d" RESP_EOL, counter);
  179. fprintf(out, "H = %lx" RESP_EOL RESP_EOL,h);
  180. }
  181. else
  182. {
  183. fprintf(out, "counter = %d" RESP_EOL RESP_EOL, counter);
  184. }
  185. FIPS_dsa_free(dsa);
  186. }
  187. }
  188. else if(!strcmp(keyword,"P"))
  189. p=hex2bn(value);
  190. else if(!strcmp(keyword,"Q"))
  191. q=hex2bn(value);
  192. else if(!strcmp(keyword,"domain_parameter_seed"))
  193. seedlen = hex2bin(value, seed);
  194. else if(!strcmp(keyword,"firstseed"))
  195. seedlen = hex2bin(value, seed);
  196. else if(!strcmp(keyword,"pseed"))
  197. seedlen += hex2bin(value, seed + seedlen);
  198. else if(!strcmp(keyword,"qseed"))
  199. seedlen += hex2bin(value, seed + seedlen);
  200. else if(!strcmp(keyword,"index"))
  201. {
  202. idxlen = hex2bin(value, idtmp);
  203. if (idxlen != 1)
  204. {
  205. fprintf(stderr, "Index value error\n");
  206. exit (1);
  207. }
  208. idx = idtmp[0];
  209. }
  210. if ((idx >= 0 && pqg_type == PQG_GCANON) || (q && pqg_type == PQG_G))
  211. {
  212. DSA *dsa;
  213. dsa = FIPS_dsa_new();
  214. dsa->p = p;
  215. dsa->q = q;
  216. p = q = NULL;
  217. if (dsa_builtin_paramgen2(dsa, L, N, md,
  218. seed, seedlen, idx, NULL,
  219. NULL, NULL, NULL) <= 0)
  220. {
  221. fprintf(stderr, "Parameter Generation error\n");
  222. exit(1);
  223. }
  224. do_bn_print_name(out, "G",dsa->g);
  225. FIPS_dsa_free(dsa);
  226. idx = -1;
  227. }
  228. }
  229. }
  230. static void pqgver(FILE *in, FILE *out)
  231. {
  232. char buf[1024];
  233. char lbuf[1024];
  234. char *keyword, *value;
  235. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  236. int counter=-1, counter2;
  237. unsigned long h=0, h2;
  238. DSA *dsa=NULL;
  239. int dsa2, L, N, part_test = 0;
  240. const EVP_MD *md = NULL;
  241. int seedlen=-1, idxlen, idx = -1;
  242. unsigned char seed[1024], idtmp[1024];
  243. while(fgets(buf,sizeof buf,in) != NULL)
  244. {
  245. if (!parse_line(&keyword, &value, lbuf, buf))
  246. {
  247. if (p && q)
  248. {
  249. part_test = 1;
  250. goto partial;
  251. }
  252. fputs(buf,out);
  253. continue;
  254. }
  255. fputs(buf, out);
  256. if(!strcmp(keyword,"[mod"))
  257. {
  258. if (!parse_mod(value, &dsa2, &L, &N, &md))
  259. {
  260. fprintf(stderr, "Mod Parse Error\n");
  261. exit (1);
  262. }
  263. }
  264. else if(!strcmp(keyword,"P"))
  265. p=hex2bn(value);
  266. else if(!strcmp(keyword,"Q"))
  267. q=hex2bn(value);
  268. else if(!strcmp(keyword,"G"))
  269. g=hex2bn(value);
  270. else if(!strcmp(keyword,"firstseed"))
  271. seedlen = hex2bin(value, seed);
  272. else if(!strcmp(keyword,"pseed"))
  273. seedlen += hex2bin(value, seed + seedlen);
  274. else if(!strcmp(keyword,"qseed"))
  275. seedlen += hex2bin(value, seed + seedlen);
  276. else if(!strcmp(keyword,"Seed")
  277. || !strcmp(keyword,"domain_parameter_seed"))
  278. {
  279. seedlen = hex2bin(value, seed);
  280. if (!dsa2 && seedlen != 20)
  281. {
  282. fprintf(stderr, "Seed parse length error\n");
  283. exit (1);
  284. }
  285. if (idx > 0)
  286. part_test = 1;
  287. }
  288. else if(!strcmp(keyword,"index"))
  289. {
  290. idxlen = hex2bin(value, idtmp);
  291. if (idxlen != 1)
  292. {
  293. fprintf(stderr, "Index value error\n");
  294. exit (1);
  295. }
  296. idx = idtmp[0];
  297. }
  298. else if(!strcmp(keyword,"c"))
  299. counter = atoi(buf+4);
  300. partial:
  301. if (part_test && idx < 0 && h == 0 && g)
  302. {
  303. dsa = FIPS_dsa_new();
  304. dsa->p = BN_dup(p);
  305. dsa->q = BN_dup(q);
  306. dsa->g = BN_dup(g);
  307. if (dsa_paramgen_check_g(dsa))
  308. fprintf(out, "Result = P" RESP_EOL);
  309. else
  310. fprintf(out, "Result = F" RESP_EOL);
  311. BN_free(p);
  312. BN_free(q);
  313. BN_free(g);
  314. p = NULL;
  315. q = NULL;
  316. g = NULL;
  317. FIPS_dsa_free(dsa);
  318. dsa = NULL;
  319. part_test = 0;
  320. }
  321. else if(!strcmp(keyword,"H") || part_test)
  322. {
  323. if (!part_test)
  324. h = atoi(value);
  325. if (!p || !q || (!g && !part_test))
  326. {
  327. fprintf(stderr, "Parse Error\n");
  328. exit (1);
  329. }
  330. dsa = FIPS_dsa_new();
  331. if (idx >= 0)
  332. {
  333. dsa->p = BN_dup(p);
  334. dsa->q = BN_dup(q);
  335. }
  336. no_err = 1;
  337. if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md,
  338. seed, seedlen, NULL,
  339. &counter2, &h2, NULL))
  340. {
  341. fprintf(stderr, "Parameter Generation error\n");
  342. exit(1);
  343. }
  344. if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md,
  345. seed, seedlen, idx, NULL,
  346. &counter2, &h2, NULL) < 0)
  347. {
  348. fprintf(stderr, "Parameter Generation error\n");
  349. exit(1);
  350. }
  351. no_err = 0;
  352. if (idx >= 0)
  353. {
  354. if (BN_cmp(dsa->g, g))
  355. fprintf(out, "Result = F" RESP_EOL);
  356. else
  357. fprintf(out, "Result = P" RESP_EOL);
  358. }
  359. else if (BN_cmp(dsa->p, p) || BN_cmp(dsa->q, q) ||
  360. (!part_test &&
  361. ((BN_cmp(dsa->g, g) || (counter != counter2) || (h != h2)))))
  362. fprintf(out, "Result = F" RESP_EOL);
  363. else
  364. fprintf(out, "Result = P" RESP_EOL);
  365. BN_free(p);
  366. BN_free(q);
  367. BN_free(g);
  368. p = NULL;
  369. q = NULL;
  370. g = NULL;
  371. FIPS_dsa_free(dsa);
  372. dsa = NULL;
  373. if (part_test)
  374. {
  375. if (idx == -1)
  376. fputs(buf,out);
  377. part_test = 0;
  378. }
  379. idx = -1;
  380. }
  381. }
  382. }
  383. /* Keypair verification routine. NB: this isn't part of the standard FIPS140-2
  384. * algorithm tests. It is an additional test to perform sanity checks on the
  385. * output of the KeyPair test.
  386. */
  387. static int dss_paramcheck(int L, int N, BIGNUM *p, BIGNUM *q, BIGNUM *g,
  388. BN_CTX *ctx)
  389. {
  390. BIGNUM *rem = NULL;
  391. if (BN_num_bits(p) != L)
  392. return 0;
  393. if (BN_num_bits(q) != N)
  394. return 0;
  395. if (BN_is_prime_ex(p, BN_prime_checks, ctx, NULL) != 1)
  396. return 0;
  397. if (BN_is_prime_ex(q, BN_prime_checks, ctx, NULL) != 1)
  398. return 0;
  399. rem = BN_new();
  400. if (!BN_mod(rem, p, q, ctx) || !BN_is_one(rem)
  401. || (BN_cmp(g, BN_value_one()) <= 0)
  402. || !BN_mod_exp(rem, g, q, p, ctx) || !BN_is_one(rem))
  403. {
  404. BN_free(rem);
  405. return 0;
  406. }
  407. /* Todo: check g */
  408. BN_free(rem);
  409. return 1;
  410. }
  411. static void keyver(FILE *in, FILE *out)
  412. {
  413. char buf[1024];
  414. char lbuf[1024];
  415. char *keyword, *value;
  416. BIGNUM *p = NULL, *q = NULL, *g = NULL, *X = NULL, *Y = NULL;
  417. BIGNUM *Y2;
  418. BN_CTX *ctx = NULL;
  419. int dsa2, L, N;
  420. int paramcheck = 0;
  421. ctx = BN_CTX_new();
  422. Y2 = BN_new();
  423. while(fgets(buf,sizeof buf,in) != NULL)
  424. {
  425. if (!parse_line(&keyword, &value, lbuf, buf))
  426. {
  427. fputs(buf,out);
  428. continue;
  429. }
  430. if(!strcmp(keyword,"[mod"))
  431. {
  432. if (p)
  433. BN_free(p);
  434. p = NULL;
  435. if (q)
  436. BN_free(q);
  437. q = NULL;
  438. if (g)
  439. BN_free(g);
  440. g = NULL;
  441. paramcheck = 0;
  442. if (!parse_mod(value, &dsa2, &L, &N, NULL))
  443. {
  444. fprintf(stderr, "Mod Parse Error\n");
  445. exit (1);
  446. }
  447. }
  448. else if(!strcmp(keyword,"P"))
  449. p=hex2bn(value);
  450. else if(!strcmp(keyword,"Q"))
  451. q=hex2bn(value);
  452. else if(!strcmp(keyword,"G"))
  453. g=hex2bn(value);
  454. else if(!strcmp(keyword,"X"))
  455. X=hex2bn(value);
  456. else if(!strcmp(keyword,"Y"))
  457. {
  458. Y=hex2bn(value);
  459. if (!p || !q || !g || !X || !Y)
  460. {
  461. fprintf(stderr, "Parse Error\n");
  462. exit (1);
  463. }
  464. do_bn_print_name(out, "P",p);
  465. do_bn_print_name(out, "Q",q);
  466. do_bn_print_name(out, "G",g);
  467. do_bn_print_name(out, "X",X);
  468. do_bn_print_name(out, "Y",Y);
  469. if (!paramcheck)
  470. {
  471. if (dss_paramcheck(L, N, p, q, g, ctx))
  472. paramcheck = 1;
  473. else
  474. paramcheck = -1;
  475. }
  476. if (paramcheck != 1)
  477. fprintf(out, "Result = F" RESP_EOL);
  478. else
  479. {
  480. if (!BN_mod_exp(Y2, g, X, p, ctx) || BN_cmp(Y2, Y))
  481. fprintf(out, "Result = F" RESP_EOL);
  482. else
  483. fprintf(out, "Result = P" RESP_EOL);
  484. }
  485. BN_free(X);
  486. BN_free(Y);
  487. X = NULL;
  488. Y = NULL;
  489. }
  490. }
  491. if (p)
  492. BN_free(p);
  493. if (q)
  494. BN_free(q);
  495. if (g)
  496. BN_free(g);
  497. if (Y2)
  498. BN_free(Y2);
  499. if (ctx)
  500. BN_CTX_free(ctx);
  501. }
  502. static void keypair(FILE *in, FILE *out)
  503. {
  504. char buf[1024];
  505. char lbuf[1024];
  506. char *keyword, *value;
  507. int dsa2, L, N;
  508. while(fgets(buf,sizeof buf,in) != NULL)
  509. {
  510. if (!parse_line(&keyword, &value, lbuf, buf))
  511. {
  512. continue;
  513. }
  514. if(!strcmp(keyword,"[mod"))
  515. {
  516. if (!parse_mod(value, &dsa2, &L, &N, NULL))
  517. {
  518. fprintf(stderr, "Mod Parse Error\n");
  519. exit (1);
  520. }
  521. fputs(buf,out);
  522. }
  523. else if(!strcmp(keyword,"N"))
  524. {
  525. DSA *dsa;
  526. int n=atoi(value);
  527. dsa = FIPS_dsa_new();
  528. if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, NULL, NULL, 0,
  529. NULL, NULL, NULL, NULL))
  530. {
  531. fprintf(stderr, "Parameter Generation error\n");
  532. exit(1);
  533. }
  534. if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, NULL, NULL, 0, -1,
  535. NULL, NULL, NULL, NULL) <= 0)
  536. {
  537. fprintf(stderr, "Parameter Generation error\n");
  538. exit(1);
  539. }
  540. do_bn_print_name(out, "P",dsa->p);
  541. do_bn_print_name(out, "Q",dsa->q);
  542. do_bn_print_name(out, "G",dsa->g);
  543. fputs(RESP_EOL, out);
  544. while(n--)
  545. {
  546. if (!DSA_generate_key(dsa))
  547. exit(1);
  548. do_bn_print_name(out, "X",dsa->priv_key);
  549. do_bn_print_name(out, "Y",dsa->pub_key);
  550. fputs(RESP_EOL, out);
  551. }
  552. if (dsa)
  553. FIPS_dsa_free(dsa);
  554. }
  555. }
  556. }
  557. static void siggen(FILE *in, FILE *out)
  558. {
  559. char buf[1024];
  560. char lbuf[1024];
  561. char *keyword, *value;
  562. int dsa2, L, N;
  563. const EVP_MD *md = NULL;
  564. DSA *dsa=NULL;
  565. while(fgets(buf,sizeof buf,in) != NULL)
  566. {
  567. if (!parse_line(&keyword, &value, lbuf, buf))
  568. {
  569. fputs(buf,out);
  570. continue;
  571. }
  572. fputs(buf,out);
  573. if(!strcmp(keyword,"[mod"))
  574. {
  575. if (!parse_mod(value, &dsa2, &L, &N, &md))
  576. {
  577. fprintf(stderr, "Mod Parse Error\n");
  578. exit (1);
  579. }
  580. if (dsa)
  581. FIPS_dsa_free(dsa);
  582. dsa = FIPS_dsa_new();
  583. if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md, NULL, 0,
  584. NULL, NULL, NULL, NULL))
  585. {
  586. fprintf(stderr, "Parameter Generation error\n");
  587. exit(1);
  588. }
  589. if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md, NULL, 0, -1,
  590. NULL, NULL, NULL, NULL) <= 0)
  591. {
  592. fprintf(stderr, "Parameter Generation error\n");
  593. exit(1);
  594. }
  595. do_bn_print_name(out, "P",dsa->p);
  596. do_bn_print_name(out, "Q",dsa->q);
  597. do_bn_print_name(out, "G",dsa->g);
  598. fputs(RESP_EOL, out);
  599. }
  600. else if(!strcmp(keyword,"Msg"))
  601. {
  602. unsigned char msg[1024];
  603. int n;
  604. DSA_SIG *sig;
  605. n=hex2bin(value,msg);
  606. if (!DSA_generate_key(dsa))
  607. exit(1);
  608. do_bn_print_name(out, "Y",dsa->pub_key);
  609. sig = FIPS_dsa_sign(dsa, msg, n, md);
  610. do_bn_print_name(out, "R",sig->r);
  611. do_bn_print_name(out, "S",sig->s);
  612. fputs(RESP_EOL, out);
  613. FIPS_dsa_sig_free(sig);
  614. }
  615. }
  616. if (dsa)
  617. FIPS_dsa_free(dsa);
  618. }
  619. static void sigver(FILE *in, FILE *out)
  620. {
  621. DSA *dsa=NULL;
  622. char buf[1024];
  623. char lbuf[1024];
  624. unsigned char msg[1024];
  625. char *keyword, *value;
  626. int n=0;
  627. int dsa2, L, N;
  628. const EVP_MD *md = NULL;
  629. DSA_SIG sg, *sig = &sg;
  630. sig->r = NULL;
  631. sig->s = NULL;
  632. while(fgets(buf,sizeof buf,in) != NULL)
  633. {
  634. if (!parse_line(&keyword, &value, lbuf, buf))
  635. {
  636. fputs(buf,out);
  637. continue;
  638. }
  639. fputs(buf,out);
  640. if(!strcmp(keyword,"[mod"))
  641. {
  642. if (!parse_mod(value, &dsa2, &L, &N, &md))
  643. {
  644. fprintf(stderr, "Mod Parse Error\n");
  645. exit (1);
  646. }
  647. if (dsa)
  648. FIPS_dsa_free(dsa);
  649. dsa = FIPS_dsa_new();
  650. }
  651. else if(!strcmp(keyword,"P"))
  652. do_hex2bn(&dsa->p, value);
  653. else if(!strcmp(keyword,"Q"))
  654. do_hex2bn(&dsa->q, value);
  655. else if(!strcmp(keyword,"G"))
  656. do_hex2bn(&dsa->g, value);
  657. else if(!strcmp(keyword,"Msg"))
  658. n=hex2bin(value,msg);
  659. else if(!strcmp(keyword,"Y"))
  660. do_hex2bn(&dsa->pub_key, value);
  661. else if(!strcmp(keyword,"R"))
  662. sig->r=hex2bn(value);
  663. else if(!strcmp(keyword,"S"))
  664. {
  665. int r;
  666. sig->s=hex2bn(value);
  667. no_err = 1;
  668. r = FIPS_dsa_verify(dsa, msg, n, md, sig);
  669. no_err = 0;
  670. if (sig->s)
  671. {
  672. BN_free(sig->s);
  673. sig->s = NULL;
  674. }
  675. if (sig->r)
  676. {
  677. BN_free(sig->r);
  678. sig->r = NULL;
  679. }
  680. fprintf(out, "Result = %c" RESP_EOL RESP_EOL, r == 1 ? 'P' : 'F');
  681. }
  682. }
  683. if (dsa)
  684. FIPS_dsa_free(dsa);
  685. }
  686. #ifdef FIPS_ALGVS
  687. int fips_dssvs_main(int argc, char **argv)
  688. #else
  689. int main(int argc, char **argv)
  690. #endif
  691. {
  692. FILE *in, *out;
  693. if (argc == 4)
  694. {
  695. in = fopen(argv[2], "r");
  696. if (!in)
  697. {
  698. fprintf(stderr, "Error opening input file\n");
  699. exit(1);
  700. }
  701. out = fopen(argv[3], "w");
  702. if (!out)
  703. {
  704. fprintf(stderr, "Error opening output file\n");
  705. exit(1);
  706. }
  707. }
  708. else if (argc == 2)
  709. {
  710. in = stdin;
  711. out = stdout;
  712. }
  713. else
  714. {
  715. fprintf(stderr,"%s [prime|pqg|pqgver|keypair|keyver|siggen|sigver]\n",argv[0]);
  716. exit(1);
  717. }
  718. fips_algtest_init();
  719. if(!strcmp(argv[1],"prime"))
  720. primes(in, out);
  721. else if(!strcmp(argv[1],"pqg"))
  722. pqg(in, out);
  723. else if(!strcmp(argv[1],"pqgver"))
  724. pqgver(in, out);
  725. else if(!strcmp(argv[1],"keypair"))
  726. keypair(in, out);
  727. else if(!strcmp(argv[1],"keyver"))
  728. keyver(in, out);
  729. else if(!strcmp(argv[1],"siggen"))
  730. siggen(in, out);
  731. else if(!strcmp(argv[1],"sigver"))
  732. sigver(in, out);
  733. else
  734. {
  735. fprintf(stderr,"Don't know how to %s.\n",argv[1]);
  736. exit(1);
  737. }
  738. if (argc == 4)
  739. {
  740. fclose(in);
  741. fclose(out);
  742. }
  743. return 0;
  744. }
  745. #endif