fips_ecdsavs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* fips/ecdsa/fips_ecdsavs.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #define OPENSSL_FIPSAPI
  54. #include <openssl/opensslconf.h>
  55. #include <stdio.h>
  56. #ifndef OPENSSL_FIPS
  57. int main(int argc, char **argv)
  58. {
  59. printf("No FIPS ECDSA support\n");
  60. return(0);
  61. }
  62. #else
  63. #include <string.h>
  64. #include <ctype.h>
  65. #include <openssl/err.h>
  66. #include <openssl/bn.h>
  67. #include <openssl/ecdsa.h>
  68. #include <openssl/evp.h>
  69. #include "fips_utl.h"
  70. #include <openssl/objects.h>
  71. static int lookup_curve(char *in, char *curve_name, const EVP_MD **pmd)
  72. {
  73. char *cname, *p;
  74. /* Copy buffer as we will change it */
  75. strcpy(curve_name, in);
  76. cname = curve_name + 1;
  77. p = strchr(cname, ']');
  78. if (!p)
  79. {
  80. fprintf(stderr, "Parse error: missing ]\n");
  81. return NID_undef;
  82. }
  83. *p = 0;
  84. p = strchr(cname, ',');
  85. if (p)
  86. {
  87. if (!pmd)
  88. {
  89. fprintf(stderr, "Parse error: unexpected digest\n");
  90. return NID_undef;
  91. }
  92. *p = 0;
  93. p++;
  94. if (!strcmp(p, "SHA-1"))
  95. *pmd = EVP_sha1();
  96. else if (!strcmp(p, "SHA-224"))
  97. *pmd = EVP_sha224();
  98. else if (!strcmp(p, "SHA-256"))
  99. *pmd = EVP_sha256();
  100. else if (!strcmp(p, "SHA-384"))
  101. *pmd = EVP_sha384();
  102. else if (!strcmp(p, "SHA-512"))
  103. *pmd = EVP_sha512();
  104. else
  105. {
  106. fprintf(stderr, "Unknown digest %s\n", p);
  107. return NID_undef;
  108. }
  109. }
  110. else if(pmd)
  111. *pmd = EVP_sha1();
  112. if (!strcmp(cname, "B-163"))
  113. return NID_sect163r2;
  114. if (!strcmp(cname, "B-233"))
  115. return NID_sect233r1;
  116. if (!strcmp(cname, "B-283"))
  117. return NID_sect283r1;
  118. if (!strcmp(cname, "B-409"))
  119. return NID_sect409r1;
  120. if (!strcmp(cname, "B-571"))
  121. return NID_sect571r1;
  122. if (!strcmp(cname, "K-163"))
  123. return NID_sect163k1;
  124. if (!strcmp(cname, "K-233"))
  125. return NID_sect233k1;
  126. if (!strcmp(cname, "K-283"))
  127. return NID_sect283k1;
  128. if (!strcmp(cname, "K-409"))
  129. return NID_sect409k1;
  130. if (!strcmp(cname, "K-571"))
  131. return NID_sect571k1;
  132. if (!strcmp(cname, "P-192"))
  133. return NID_X9_62_prime192v1;
  134. if (!strcmp(cname, "P-224"))
  135. return NID_secp224r1;
  136. if (!strcmp(cname, "P-256"))
  137. return NID_X9_62_prime256v1;
  138. if (!strcmp(cname, "P-384"))
  139. return NID_secp384r1;
  140. if (!strcmp(cname, "P-521"))
  141. return NID_secp521r1;
  142. fprintf(stderr, "Unknown Curve name %s\n", cname);
  143. return NID_undef;
  144. }
  145. static int ec_get_pubkey(EC_KEY *key, BIGNUM *x, BIGNUM *y)
  146. {
  147. const EC_POINT *pt;
  148. const EC_GROUP *grp;
  149. const EC_METHOD *meth;
  150. int rv;
  151. BN_CTX *ctx;
  152. ctx = BN_CTX_new();
  153. if (!ctx)
  154. return 0;
  155. grp = EC_KEY_get0_group(key);
  156. pt = EC_KEY_get0_public_key(key);
  157. meth = EC_GROUP_method_of(grp);
  158. if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
  159. rv = EC_POINT_get_affine_coordinates_GFp(grp, pt, x, y, ctx);
  160. else
  161. #ifdef OPENSSL_NO_EC2M
  162. {
  163. fprintf(stderr, "ERROR: GF2m not supported\n");
  164. exit(1);
  165. }
  166. #else
  167. rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, x, y, ctx);
  168. #endif
  169. BN_CTX_free(ctx);
  170. return rv;
  171. }
  172. static int KeyPair(FILE *in, FILE *out)
  173. {
  174. char buf[2048], lbuf[2048];
  175. char *keyword, *value;
  176. int curve_nid = NID_undef;
  177. int i, count;
  178. BIGNUM *Qx = NULL, *Qy = NULL;
  179. const BIGNUM *d = NULL;
  180. EC_KEY *key = NULL;
  181. Qx = BN_new();
  182. Qy = BN_new();
  183. while(fgets(buf, sizeof buf, in) != NULL)
  184. {
  185. if (*buf == '[' && buf[2] == '-')
  186. {
  187. if (buf[2] == '-')
  188. curve_nid = lookup_curve(buf, lbuf, NULL);
  189. fputs(buf, out);
  190. continue;
  191. }
  192. if (!parse_line(&keyword, &value, lbuf, buf))
  193. {
  194. fputs(buf, out);
  195. continue;
  196. }
  197. if (!strcmp(keyword, "N"))
  198. {
  199. count = atoi(value);
  200. for (i = 0; i < count; i++)
  201. {
  202. key = EC_KEY_new_by_curve_name(curve_nid);
  203. if (!EC_KEY_generate_key(key))
  204. {
  205. fprintf(stderr, "Error generating key\n");
  206. return 0;
  207. }
  208. if (!ec_get_pubkey(key, Qx, Qy))
  209. {
  210. fprintf(stderr, "Error getting public key\n");
  211. return 0;
  212. }
  213. d = EC_KEY_get0_private_key(key);
  214. do_bn_print_name(out, "d", d);
  215. do_bn_print_name(out, "Qx", Qx);
  216. do_bn_print_name(out, "Qy", Qy);
  217. fputs("\n", out);
  218. EC_KEY_free(key);
  219. }
  220. }
  221. }
  222. BN_free(Qx);
  223. BN_free(Qy);
  224. return 1;
  225. }
  226. static int PKV(FILE *in, FILE *out)
  227. {
  228. char buf[2048], lbuf[2048];
  229. char *keyword, *value;
  230. int curve_nid = NID_undef;
  231. BIGNUM *Qx = NULL, *Qy = NULL;
  232. EC_KEY *key = NULL;
  233. while(fgets(buf, sizeof buf, in) != NULL)
  234. {
  235. fputs(buf, out);
  236. if (*buf == '[' && buf[2] == '-')
  237. {
  238. curve_nid = lookup_curve(buf, lbuf, NULL);
  239. if (curve_nid == NID_undef)
  240. return 0;
  241. }
  242. if (!parse_line(&keyword, &value, lbuf, buf))
  243. continue;
  244. if (!strcmp(keyword, "Qx"))
  245. {
  246. if (!do_hex2bn(&Qx, value))
  247. {
  248. fprintf(stderr, "Invalid Qx value\n");
  249. return 0;
  250. }
  251. }
  252. if (!strcmp(keyword, "Qy"))
  253. {
  254. int rv;
  255. if (!do_hex2bn(&Qy, value))
  256. {
  257. fprintf(stderr, "Invalid Qy value\n");
  258. return 0;
  259. }
  260. key = EC_KEY_new_by_curve_name(curve_nid);
  261. rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
  262. fprintf(out, "Result = %s\n", rv ? "P":"F");
  263. }
  264. }
  265. return 1;
  266. }
  267. static int SigGen(FILE *in, FILE *out)
  268. {
  269. char buf[2048], lbuf[2048];
  270. char *keyword, *value;
  271. unsigned char *msg;
  272. int curve_nid = NID_undef;
  273. long mlen;
  274. BIGNUM *Qx = NULL, *Qy = NULL;
  275. EC_KEY *key = NULL;
  276. ECDSA_SIG *sig = NULL;
  277. const EVP_MD *digest = NULL;
  278. EVP_MD_CTX mctx;
  279. EVP_MD_CTX_init(&mctx);
  280. Qx = BN_new();
  281. Qy = BN_new();
  282. while(fgets(buf, sizeof buf, in) != NULL)
  283. {
  284. fputs(buf, out);
  285. if (*buf == '[')
  286. {
  287. curve_nid = lookup_curve(buf, lbuf, &digest);
  288. if (curve_nid == NID_undef)
  289. return 0;
  290. }
  291. if (!parse_line(&keyword, &value, lbuf, buf))
  292. continue;
  293. if (!strcmp(keyword, "Msg"))
  294. {
  295. msg = hex2bin_m(value, &mlen);
  296. if (!msg)
  297. {
  298. fprintf(stderr, "Invalid Message\n");
  299. return 0;
  300. }
  301. key = EC_KEY_new_by_curve_name(curve_nid);
  302. if (!EC_KEY_generate_key(key))
  303. {
  304. fprintf(stderr, "Error generating key\n");
  305. return 0;
  306. }
  307. if (!ec_get_pubkey(key, Qx, Qy))
  308. {
  309. fprintf(stderr, "Error getting public key\n");
  310. return 0;
  311. }
  312. FIPS_digestinit(&mctx, digest);
  313. FIPS_digestupdate(&mctx, msg, mlen);
  314. sig = FIPS_ecdsa_sign_ctx(key, &mctx);
  315. if (!sig)
  316. {
  317. fprintf(stderr, "Error signing message\n");
  318. return 0;
  319. }
  320. do_bn_print_name(out, "Qx", Qx);
  321. do_bn_print_name(out, "Qy", Qy);
  322. do_bn_print_name(out, "R", sig->r);
  323. do_bn_print_name(out, "S", sig->s);
  324. EC_KEY_free(key);
  325. FIPS_ecdsa_sig_free(sig);
  326. }
  327. }
  328. BN_free(Qx);
  329. BN_free(Qy);
  330. FIPS_md_ctx_cleanup(&mctx);
  331. return 1;
  332. }
  333. static int SigVer(FILE *in, FILE *out)
  334. {
  335. char buf[2048], lbuf[2048];
  336. char *keyword, *value;
  337. unsigned char *msg = NULL;
  338. int curve_nid = NID_undef;
  339. long mlen;
  340. BIGNUM *Qx = NULL, *Qy = NULL;
  341. EC_KEY *key = NULL;
  342. ECDSA_SIG sg, *sig = &sg;
  343. const EVP_MD *digest = NULL;
  344. EVP_MD_CTX mctx;
  345. EVP_MD_CTX_init(&mctx);
  346. sig->r = NULL;
  347. sig->s = NULL;
  348. while(fgets(buf, sizeof buf, in) != NULL)
  349. {
  350. fputs(buf, out);
  351. if (*buf == '[')
  352. {
  353. curve_nid = lookup_curve(buf, lbuf, &digest);
  354. if (curve_nid == NID_undef)
  355. return 0;
  356. }
  357. if (!parse_line(&keyword, &value, lbuf, buf))
  358. continue;
  359. if (!strcmp(keyword, "Msg"))
  360. {
  361. msg = hex2bin_m(value, &mlen);
  362. if (!msg)
  363. {
  364. fprintf(stderr, "Invalid Message\n");
  365. return 0;
  366. }
  367. }
  368. if (!strcmp(keyword, "Qx"))
  369. {
  370. if (!do_hex2bn(&Qx, value))
  371. {
  372. fprintf(stderr, "Invalid Qx value\n");
  373. return 0;
  374. }
  375. }
  376. if (!strcmp(keyword, "Qy"))
  377. {
  378. if (!do_hex2bn(&Qy, value))
  379. {
  380. fprintf(stderr, "Invalid Qy value\n");
  381. return 0;
  382. }
  383. }
  384. if (!strcmp(keyword, "R"))
  385. {
  386. if (!do_hex2bn(&sig->r, value))
  387. {
  388. fprintf(stderr, "Invalid R value\n");
  389. return 0;
  390. }
  391. }
  392. if (!strcmp(keyword, "S"))
  393. {
  394. int rv;
  395. if (!do_hex2bn(&sig->s, value))
  396. {
  397. fprintf(stderr, "Invalid S value\n");
  398. return 0;
  399. }
  400. key = EC_KEY_new_by_curve_name(curve_nid);
  401. rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
  402. if (rv != 1)
  403. {
  404. fprintf(stderr, "Error setting public key\n");
  405. return 0;
  406. }
  407. FIPS_digestinit(&mctx, digest);
  408. FIPS_digestupdate(&mctx, msg, mlen);
  409. no_err = 1;
  410. rv = FIPS_ecdsa_verify_ctx(key, &mctx, sig);
  411. no_err = 0;
  412. fprintf(out, "Result = %s\n", rv ? "P":"F");
  413. }
  414. }
  415. return 1;
  416. }
  417. int main(int argc, char **argv)
  418. {
  419. FILE *in = NULL, *out = NULL;
  420. const char *cmd = argv[1];
  421. int rv = 0;
  422. fips_algtest_init();
  423. if (argc == 4)
  424. {
  425. in = fopen(argv[2], "r");
  426. if (!in)
  427. {
  428. fprintf(stderr, "Error opening input file\n");
  429. exit(1);
  430. }
  431. out = fopen(argv[3], "w");
  432. if (!out)
  433. {
  434. fprintf(stderr, "Error opening output file\n");
  435. exit(1);
  436. }
  437. }
  438. else if (argc == 2)
  439. {
  440. in = stdin;
  441. out = stdout;
  442. }
  443. if (!cmd)
  444. {
  445. fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
  446. return 1;
  447. }
  448. if (!strcmp(cmd, "KeyPair"))
  449. rv = KeyPair(in, out);
  450. else if (!strcmp(cmd, "PKV"))
  451. rv = PKV(in, out);
  452. else if (!strcmp(cmd, "SigVer"))
  453. rv = SigVer(in, out);
  454. else if (!strcmp(cmd, "SigGen"))
  455. rv = SigGen(in, out);
  456. else
  457. {
  458. fprintf(stderr, "Unknown command %s\n", cmd);
  459. return 1;
  460. }
  461. if (argc == 4)
  462. {
  463. fclose(in);
  464. fclose(out);
  465. }
  466. if (rv <= 0)
  467. {
  468. fprintf(stderr, "Error running %s\n", cmd);
  469. return 1;
  470. }
  471. return 0;
  472. }
  473. #endif