fips_ecdsavs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 elookup_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 = elookup_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(RESP_EOL, 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 = elookup_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. no_err = 1;
  262. rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
  263. no_err = 0;
  264. EC_KEY_free(key);
  265. fprintf(out, "Result = %s" RESP_EOL, rv ? "P":"F");
  266. }
  267. }
  268. BN_free(Qx);
  269. BN_free(Qy);
  270. return 1;
  271. }
  272. static int SigGen(FILE *in, FILE *out)
  273. {
  274. char buf[2048], lbuf[2048];
  275. char *keyword, *value;
  276. unsigned char *msg;
  277. int curve_nid = NID_undef;
  278. long mlen;
  279. BIGNUM *Qx = NULL, *Qy = NULL;
  280. EC_KEY *key = NULL;
  281. ECDSA_SIG *sig = NULL;
  282. const EVP_MD *digest = NULL;
  283. Qx = BN_new();
  284. Qy = BN_new();
  285. while(fgets(buf, sizeof buf, in) != NULL)
  286. {
  287. fputs(buf, out);
  288. if (*buf == '[')
  289. {
  290. curve_nid = elookup_curve(buf, lbuf, &digest);
  291. if (curve_nid == NID_undef)
  292. return 0;
  293. }
  294. if (!parse_line(&keyword, &value, lbuf, buf))
  295. continue;
  296. if (!strcmp(keyword, "Msg"))
  297. {
  298. msg = hex2bin_m(value, &mlen);
  299. if (!msg)
  300. {
  301. fprintf(stderr, "Invalid Message\n");
  302. return 0;
  303. }
  304. key = EC_KEY_new_by_curve_name(curve_nid);
  305. if (!EC_KEY_generate_key(key))
  306. {
  307. fprintf(stderr, "Error generating key\n");
  308. return 0;
  309. }
  310. if (!ec_get_pubkey(key, Qx, Qy))
  311. {
  312. fprintf(stderr, "Error getting public key\n");
  313. return 0;
  314. }
  315. sig = FIPS_ecdsa_sign(key, msg, mlen, digest);
  316. if (!sig)
  317. {
  318. fprintf(stderr, "Error signing message\n");
  319. return 0;
  320. }
  321. do_bn_print_name(out, "Qx", Qx);
  322. do_bn_print_name(out, "Qy", Qy);
  323. do_bn_print_name(out, "R", sig->r);
  324. do_bn_print_name(out, "S", sig->s);
  325. EC_KEY_free(key);
  326. OPENSSL_free(msg);
  327. FIPS_ecdsa_sig_free(sig);
  328. }
  329. }
  330. BN_free(Qx);
  331. BN_free(Qy);
  332. return 1;
  333. }
  334. static int SigVer(FILE *in, FILE *out)
  335. {
  336. char buf[2048], lbuf[2048];
  337. char *keyword, *value;
  338. unsigned char *msg = NULL;
  339. int curve_nid = NID_undef;
  340. long mlen;
  341. BIGNUM *Qx = NULL, *Qy = NULL;
  342. EC_KEY *key = NULL;
  343. ECDSA_SIG sg, *sig = &sg;
  344. const EVP_MD *digest = NULL;
  345. sig->r = NULL;
  346. sig->s = NULL;
  347. while(fgets(buf, sizeof buf, in) != NULL)
  348. {
  349. fputs(buf, out);
  350. if (*buf == '[')
  351. {
  352. curve_nid = elookup_curve(buf, lbuf, &digest);
  353. if (curve_nid == NID_undef)
  354. return 0;
  355. }
  356. if (!parse_line(&keyword, &value, lbuf, buf))
  357. continue;
  358. if (!strcmp(keyword, "Msg"))
  359. {
  360. msg = hex2bin_m(value, &mlen);
  361. if (!msg)
  362. {
  363. fprintf(stderr, "Invalid Message\n");
  364. return 0;
  365. }
  366. }
  367. if (!strcmp(keyword, "Qx"))
  368. {
  369. if (!do_hex2bn(&Qx, value))
  370. {
  371. fprintf(stderr, "Invalid Qx value\n");
  372. return 0;
  373. }
  374. }
  375. if (!strcmp(keyword, "Qy"))
  376. {
  377. if (!do_hex2bn(&Qy, value))
  378. {
  379. fprintf(stderr, "Invalid Qy value\n");
  380. return 0;
  381. }
  382. }
  383. if (!strcmp(keyword, "R"))
  384. {
  385. if (!do_hex2bn(&sig->r, value))
  386. {
  387. fprintf(stderr, "Invalid R value\n");
  388. return 0;
  389. }
  390. }
  391. if (!strcmp(keyword, "S"))
  392. {
  393. int rv;
  394. if (!do_hex2bn(&sig->s, value))
  395. {
  396. fprintf(stderr, "Invalid S value\n");
  397. return 0;
  398. }
  399. key = EC_KEY_new_by_curve_name(curve_nid);
  400. rv = EC_KEY_set_public_key_affine_coordinates(key, Qx, Qy);
  401. if (rv != 1)
  402. {
  403. fprintf(stderr, "Error setting public key\n");
  404. return 0;
  405. }
  406. no_err = 1;
  407. rv = FIPS_ecdsa_verify(key, msg, mlen, digest, sig);
  408. EC_KEY_free(key);
  409. if (msg)
  410. OPENSSL_free(msg);
  411. no_err = 0;
  412. fprintf(out, "Result = %s" RESP_EOL, rv ? "P":"F");
  413. }
  414. }
  415. if (sig->r)
  416. BN_free(sig->r);
  417. if (sig->s)
  418. BN_free(sig->s);
  419. if (Qx)
  420. BN_free(Qx);
  421. if (Qy)
  422. BN_free(Qy);
  423. return 1;
  424. }
  425. #ifdef FIPS_ALGVS
  426. int fips_ecdsavs_main(int argc, char **argv)
  427. #else
  428. int main(int argc, char **argv)
  429. #endif
  430. {
  431. FILE *in = NULL, *out = NULL;
  432. const char *cmd = argv[1];
  433. int rv = 0;
  434. fips_algtest_init();
  435. if (argc == 4)
  436. {
  437. in = fopen(argv[2], "r");
  438. if (!in)
  439. {
  440. fprintf(stderr, "Error opening input file\n");
  441. exit(1);
  442. }
  443. out = fopen(argv[3], "w");
  444. if (!out)
  445. {
  446. fprintf(stderr, "Error opening output file\n");
  447. exit(1);
  448. }
  449. }
  450. else if (argc == 2)
  451. {
  452. in = stdin;
  453. out = stdout;
  454. }
  455. if (!cmd)
  456. {
  457. fprintf(stderr, "fips_ecdsavs [KeyPair|PKV|SigGen|SigVer]\n");
  458. return 1;
  459. }
  460. if (!strcmp(cmd, "KeyPair"))
  461. rv = KeyPair(in, out);
  462. else if (!strcmp(cmd, "PKV"))
  463. rv = PKV(in, out);
  464. else if (!strcmp(cmd, "SigVer"))
  465. rv = SigVer(in, out);
  466. else if (!strcmp(cmd, "SigGen"))
  467. rv = SigGen(in, out);
  468. else
  469. {
  470. fprintf(stderr, "Unknown command %s\n", cmd);
  471. return 1;
  472. }
  473. if (argc == 4)
  474. {
  475. fclose(in);
  476. fclose(out);
  477. }
  478. if (rv <= 0)
  479. {
  480. fprintf(stderr, "Error running %s\n", cmd);
  481. return 1;
  482. }
  483. return 0;
  484. }
  485. #endif