fips_dssvs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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_line(&keyword, &value, lbuf, line))
  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_line(&keyword, &value, lbuf, line))
  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\n",
  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 bits, size_t qbits,
  104. const EVP_MD *evpmd, const unsigned char *seed_in, size_t seed_len,
  105. unsigned char *seed_out,
  106. int *counter_ret, unsigned long *h_ret, BN_GENCB *cb);
  107. static void pqg(FILE *in, FILE *out)
  108. {
  109. char buf[1024];
  110. char lbuf[1024];
  111. char *keyword, *value;
  112. int dsa2, L, N;
  113. const EVP_MD *md = NULL;
  114. while(fgets(buf,sizeof buf,in) != NULL)
  115. {
  116. if (!parse_line(&keyword, &value, lbuf, buf))
  117. {
  118. fputs(buf,out);
  119. continue;
  120. }
  121. if(!strcmp(keyword,"[mod"))
  122. {
  123. fputs(buf,out);
  124. if (!parse_mod(value, &dsa2, &L, &N, &md))
  125. {
  126. fprintf(stderr, "Mod Parse Error\n");
  127. exit (1);
  128. }
  129. }
  130. else if(!strcmp(keyword,"N"))
  131. {
  132. int n=atoi(value);
  133. while(n--)
  134. {
  135. unsigned char seed[EVP_MAX_MD_SIZE];
  136. DSA *dsa;
  137. int counter;
  138. unsigned long h;
  139. dsa = FIPS_dsa_new();
  140. if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md,
  141. NULL, 0, seed,
  142. &counter, &h, NULL))
  143. {
  144. fprintf(stderr, "Parameter Generation error\n");
  145. exit(1);
  146. }
  147. if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md,
  148. NULL, 0, seed,
  149. &counter, &h, NULL) <= 0)
  150. {
  151. fprintf(stderr, "Parameter Generation error\n");
  152. exit(1);
  153. }
  154. do_bn_print_name(out, "P",dsa->p);
  155. do_bn_print_name(out, "Q",dsa->q);
  156. do_bn_print_name(out, "G",dsa->g);
  157. OutputValue("Seed",seed, M_EVP_MD_size(md), out, 0);
  158. fprintf(out, "c = %d\n",counter);
  159. fprintf(out, "H = %lx\n\n",h);
  160. }
  161. }
  162. else
  163. fputs(buf,out);
  164. }
  165. }
  166. static void pqgver(FILE *in, FILE *out)
  167. {
  168. char buf[1024];
  169. char lbuf[1024];
  170. char *keyword, *value;
  171. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  172. int counter=-1, counter2;
  173. unsigned long h=-1, h2;
  174. DSA *dsa=NULL;
  175. int dsa2, L, N, part_test = 0;
  176. const EVP_MD *md = NULL;
  177. int seedlen=-1;
  178. unsigned char seed[1024];
  179. while(fgets(buf,sizeof buf,in) != NULL)
  180. {
  181. if (!parse_line(&keyword, &value, lbuf, buf))
  182. {
  183. if (p && q)
  184. {
  185. part_test = 1;
  186. goto partial;
  187. }
  188. fputs(buf,out);
  189. continue;
  190. }
  191. fputs(buf, out);
  192. if(!strcmp(keyword,"[mod"))
  193. {
  194. if (!parse_mod(value, &dsa2, &L, &N, &md))
  195. {
  196. fprintf(stderr, "Mod Parse Error\n");
  197. exit (1);
  198. }
  199. }
  200. else if(!strcmp(keyword,"P"))
  201. p=hex2bn(value);
  202. else if(!strcmp(keyword,"Q"))
  203. q=hex2bn(value);
  204. else if(!strcmp(keyword,"G"))
  205. g=hex2bn(value);
  206. else if(!strcmp(keyword,"Seed"))
  207. {
  208. seedlen = hex2bin(value, seed);
  209. if (!dsa2 && seedlen != 20)
  210. {
  211. fprintf(stderr, "Seed parse length error\n");
  212. exit (1);
  213. }
  214. }
  215. else if(!strcmp(keyword,"c"))
  216. counter =atoi(buf+4);
  217. partial:
  218. if(!strcmp(keyword,"H") || part_test)
  219. {
  220. if (!part_test)
  221. h = atoi(value);
  222. if (!p || !q || (!g && !part_test))
  223. {
  224. fprintf(stderr, "Parse Error\n");
  225. exit (1);
  226. }
  227. dsa = FIPS_dsa_new();
  228. if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md,
  229. seed, seedlen, NULL,
  230. &counter2, &h2, NULL))
  231. {
  232. fprintf(stderr, "Parameter Generation error\n");
  233. exit(1);
  234. }
  235. if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md,
  236. seed, seedlen, NULL,
  237. &counter2, &h2, NULL) < 0)
  238. {
  239. fprintf(stderr, "Parameter Generation error\n");
  240. exit(1);
  241. }
  242. if (BN_cmp(dsa->p, p) || BN_cmp(dsa->q, q) ||
  243. (!part_test &&
  244. ((BN_cmp(dsa->g, g) || (counter != counter2) || (h != h2)))))
  245. fprintf(out, "Result = F\n");
  246. else
  247. fprintf(out, "Result = P\n");
  248. BN_free(p);
  249. BN_free(q);
  250. BN_free(g);
  251. p = NULL;
  252. q = NULL;
  253. g = NULL;
  254. FIPS_dsa_free(dsa);
  255. dsa = NULL;
  256. if (part_test)
  257. {
  258. fputs(buf,out);
  259. part_test = 0;
  260. }
  261. }
  262. }
  263. }
  264. /* Keypair verification routine. NB: this isn't part of the standard FIPS140-2
  265. * algorithm tests. It is an additional test to perform sanity checks on the
  266. * output of the KeyPair test.
  267. */
  268. static int dss_paramcheck(int L, int N, BIGNUM *p, BIGNUM *q, BIGNUM *g,
  269. BN_CTX *ctx)
  270. {
  271. BIGNUM *rem = NULL;
  272. if (BN_num_bits(p) != L)
  273. return 0;
  274. if (BN_num_bits(q) != N)
  275. return 0;
  276. if (BN_is_prime_ex(p, BN_prime_checks, ctx, NULL) != 1)
  277. return 0;
  278. if (BN_is_prime_ex(q, BN_prime_checks, ctx, NULL) != 1)
  279. return 0;
  280. rem = BN_new();
  281. if (!BN_mod(rem, p, q, ctx) || !BN_is_one(rem)
  282. || (BN_cmp(g, BN_value_one()) <= 0)
  283. || !BN_mod_exp(rem, g, q, p, ctx) || !BN_is_one(rem))
  284. {
  285. BN_free(rem);
  286. return 0;
  287. }
  288. /* Todo: check g */
  289. BN_free(rem);
  290. return 1;
  291. }
  292. static void keyver(FILE *in, FILE *out)
  293. {
  294. char buf[1024];
  295. char lbuf[1024];
  296. char *keyword, *value;
  297. BIGNUM *p = NULL, *q = NULL, *g = NULL, *X = NULL, *Y = NULL;
  298. BIGNUM *Y2;
  299. BN_CTX *ctx = NULL;
  300. int dsa2, L, N;
  301. int paramcheck = 0;
  302. ctx = BN_CTX_new();
  303. Y2 = BN_new();
  304. while(fgets(buf,sizeof buf,in) != NULL)
  305. {
  306. if (!parse_line(&keyword, &value, lbuf, buf))
  307. {
  308. fputs(buf,out);
  309. continue;
  310. }
  311. if(!strcmp(keyword,"[mod"))
  312. {
  313. if (p)
  314. BN_free(p);
  315. p = NULL;
  316. if (q)
  317. BN_free(q);
  318. q = NULL;
  319. if (g)
  320. BN_free(g);
  321. g = NULL;
  322. paramcheck = 0;
  323. if (!parse_mod(value, &dsa2, &L, &N, NULL))
  324. {
  325. fprintf(stderr, "Mod Parse Error\n");
  326. exit (1);
  327. }
  328. }
  329. else if(!strcmp(keyword,"P"))
  330. p=hex2bn(value);
  331. else if(!strcmp(keyword,"Q"))
  332. q=hex2bn(value);
  333. else if(!strcmp(keyword,"G"))
  334. g=hex2bn(value);
  335. else if(!strcmp(keyword,"X"))
  336. X=hex2bn(value);
  337. else if(!strcmp(keyword,"Y"))
  338. {
  339. Y=hex2bn(value);
  340. if (!p || !q || !g || !X || !Y)
  341. {
  342. fprintf(stderr, "Parse Error\n");
  343. exit (1);
  344. }
  345. do_bn_print_name(out, "P",p);
  346. do_bn_print_name(out, "Q",q);
  347. do_bn_print_name(out, "G",g);
  348. do_bn_print_name(out, "X",X);
  349. do_bn_print_name(out, "Y",Y);
  350. if (!paramcheck)
  351. {
  352. if (dss_paramcheck(L, N, p, q, g, ctx))
  353. paramcheck = 1;
  354. else
  355. paramcheck = -1;
  356. }
  357. if (paramcheck != 1)
  358. fprintf(out, "Result = F\n");
  359. else
  360. {
  361. if (!BN_mod_exp(Y2, g, X, p, ctx) || BN_cmp(Y2, Y))
  362. fprintf(out, "Result = F\n");
  363. else
  364. fprintf(out, "Result = P\n");
  365. }
  366. BN_free(X);
  367. BN_free(Y);
  368. X = NULL;
  369. Y = NULL;
  370. }
  371. }
  372. if (p)
  373. BN_free(p);
  374. if (q)
  375. BN_free(q);
  376. if (g)
  377. BN_free(g);
  378. if (Y2)
  379. BN_free(Y2);
  380. }
  381. static void keypair(FILE *in, FILE *out)
  382. {
  383. char buf[1024];
  384. char lbuf[1024];
  385. char *keyword, *value;
  386. int dsa2, L, N;
  387. while(fgets(buf,sizeof buf,in) != NULL)
  388. {
  389. if (!parse_line(&keyword, &value, lbuf, buf))
  390. {
  391. continue;
  392. }
  393. if(!strcmp(keyword,"[mod"))
  394. {
  395. if (!parse_mod(value, &dsa2, &L, &N, NULL))
  396. {
  397. fprintf(stderr, "Mod Parse Error\n");
  398. exit (1);
  399. }
  400. fputs(buf,out);
  401. }
  402. else if(!strcmp(keyword,"N"))
  403. {
  404. DSA *dsa;
  405. int n=atoi(value);
  406. dsa = FIPS_dsa_new();
  407. if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, NULL, NULL, 0,
  408. NULL, NULL, NULL, NULL))
  409. {
  410. fprintf(stderr, "Parameter Generation error\n");
  411. exit(1);
  412. }
  413. if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, NULL, NULL, 0,
  414. NULL, NULL, NULL, NULL) <= 0)
  415. {
  416. fprintf(stderr, "Parameter Generation error\n");
  417. exit(1);
  418. }
  419. do_bn_print_name(out, "P",dsa->p);
  420. do_bn_print_name(out, "Q",dsa->q);
  421. do_bn_print_name(out, "G",dsa->g);
  422. fputs("\n", out);
  423. while(n--)
  424. {
  425. if (!DSA_generate_key(dsa))
  426. exit(1);
  427. do_bn_print_name(out, "X",dsa->priv_key);
  428. do_bn_print_name(out, "Y",dsa->pub_key);
  429. fputs("\n", out);
  430. }
  431. }
  432. }
  433. }
  434. static void siggen(FILE *in, FILE *out)
  435. {
  436. char buf[1024];
  437. char lbuf[1024];
  438. char *keyword, *value;
  439. int dsa2, L, N;
  440. const EVP_MD *md = NULL;
  441. DSA *dsa=NULL;
  442. while(fgets(buf,sizeof buf,in) != NULL)
  443. {
  444. if (!parse_line(&keyword, &value, lbuf, buf))
  445. {
  446. fputs(buf,out);
  447. continue;
  448. }
  449. fputs(buf,out);
  450. if(!strcmp(keyword,"[mod"))
  451. {
  452. if (!parse_mod(value, &dsa2, &L, &N, &md))
  453. {
  454. fprintf(stderr, "Mod Parse Error\n");
  455. exit (1);
  456. }
  457. if (dsa)
  458. FIPS_dsa_free(dsa);
  459. dsa = FIPS_dsa_new();
  460. if (!dsa2 && !dsa_builtin_paramgen(dsa, L, N, md, NULL, 0,
  461. NULL, NULL, NULL, NULL))
  462. {
  463. fprintf(stderr, "Parameter Generation error\n");
  464. exit(1);
  465. }
  466. if (dsa2 && dsa_builtin_paramgen2(dsa, L, N, md, NULL, 0,
  467. NULL, NULL, NULL, NULL) <= 0)
  468. {
  469. fprintf(stderr, "Parameter Generation error\n");
  470. exit(1);
  471. }
  472. do_bn_print_name(out, "P",dsa->p);
  473. do_bn_print_name(out, "Q",dsa->q);
  474. do_bn_print_name(out, "G",dsa->g);
  475. fputs("\n", out);
  476. }
  477. else if(!strcmp(keyword,"Msg"))
  478. {
  479. unsigned char msg[1024];
  480. int n;
  481. EVP_MD_CTX mctx;
  482. DSA_SIG *sig;
  483. FIPS_md_ctx_init(&mctx);
  484. n=hex2bin(value,msg);
  485. if (!DSA_generate_key(dsa))
  486. exit(1);
  487. do_bn_print_name(out, "Y",dsa->pub_key);
  488. FIPS_digestinit(&mctx, md);
  489. FIPS_digestupdate(&mctx, msg, n);
  490. sig = FIPS_dsa_sign_ctx(dsa, &mctx);
  491. do_bn_print_name(out, "R",sig->r);
  492. do_bn_print_name(out, "S",sig->s);
  493. fputs("\n", out);
  494. FIPS_dsa_sig_free(sig);
  495. FIPS_md_ctx_cleanup(&mctx);
  496. }
  497. }
  498. if (dsa)
  499. FIPS_dsa_free(dsa);
  500. }
  501. static void sigver(FILE *in, FILE *out)
  502. {
  503. DSA *dsa=NULL;
  504. char buf[1024];
  505. char lbuf[1024];
  506. unsigned char msg[1024];
  507. char *keyword, *value;
  508. int n=0;
  509. int dsa2, L, N;
  510. const EVP_MD *md = NULL;
  511. DSA_SIG sg, *sig = &sg;
  512. sig->r = NULL;
  513. sig->s = NULL;
  514. while(fgets(buf,sizeof buf,in) != NULL)
  515. {
  516. if (!parse_line(&keyword, &value, lbuf, buf))
  517. {
  518. fputs(buf,out);
  519. continue;
  520. }
  521. fputs(buf,out);
  522. if(!strcmp(keyword,"[mod"))
  523. {
  524. if (!parse_mod(value, &dsa2, &L, &N, &md))
  525. {
  526. fprintf(stderr, "Mod Parse Error\n");
  527. exit (1);
  528. }
  529. if (dsa)
  530. FIPS_dsa_free(dsa);
  531. dsa = FIPS_dsa_new();
  532. }
  533. else if(!strcmp(keyword,"P"))
  534. dsa->p=hex2bn(value);
  535. else if(!strcmp(keyword,"Q"))
  536. dsa->q=hex2bn(value);
  537. else if(!strcmp(keyword,"G"))
  538. dsa->g=hex2bn(value);
  539. else if(!strcmp(keyword,"Msg"))
  540. n=hex2bin(value,msg);
  541. else if(!strcmp(keyword,"Y"))
  542. dsa->pub_key=hex2bn(value);
  543. else if(!strcmp(keyword,"R"))
  544. sig->r=hex2bn(value);
  545. else if(!strcmp(keyword,"S"))
  546. {
  547. EVP_MD_CTX mctx;
  548. int r;
  549. FIPS_md_ctx_init(&mctx);
  550. sig->s=hex2bn(value);
  551. FIPS_digestinit(&mctx, md);
  552. FIPS_digestupdate(&mctx, msg, n);
  553. no_err = 1;
  554. r = FIPS_dsa_verify_ctx(dsa, &mctx, sig);
  555. no_err = 0;
  556. FIPS_md_ctx_cleanup(&mctx);
  557. fprintf(out, "Result = %c\n\n", r == 1 ? 'P' : 'F');
  558. }
  559. }
  560. }
  561. int main(int argc,char **argv)
  562. {
  563. FILE *in, *out;
  564. if (argc == 4)
  565. {
  566. in = fopen(argv[2], "r");
  567. if (!in)
  568. {
  569. fprintf(stderr, "Error opening input file\n");
  570. exit(1);
  571. }
  572. out = fopen(argv[3], "w");
  573. if (!out)
  574. {
  575. fprintf(stderr, "Error opening output file\n");
  576. exit(1);
  577. }
  578. }
  579. else if (argc == 2)
  580. {
  581. in = stdin;
  582. out = stdout;
  583. }
  584. else
  585. {
  586. fprintf(stderr,"%s [prime|pqg|pqgver|keypair|keyver|siggen|sigver]\n",argv[0]);
  587. exit(1);
  588. }
  589. fips_algtest_init();
  590. if(!strcmp(argv[1],"prime"))
  591. primes(in, out);
  592. else if(!strcmp(argv[1],"pqg"))
  593. pqg(in, out);
  594. else if(!strcmp(argv[1],"pqgver"))
  595. pqgver(in, out);
  596. else if(!strcmp(argv[1],"keypair"))
  597. keypair(in, out);
  598. else if(!strcmp(argv[1],"keyver"))
  599. keyver(in, out);
  600. else if(!strcmp(argv[1],"siggen"))
  601. siggen(in, out);
  602. else if(!strcmp(argv[1],"sigver"))
  603. sigver(in, out);
  604. else
  605. {
  606. fprintf(stderr,"Don't know how to %s.\n",argv[1]);
  607. exit(1);
  608. }
  609. if (argc == 4)
  610. {
  611. fclose(in);
  612. fclose(out);
  613. }
  614. return 0;
  615. }
  616. #endif