fips_ecdhvs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* fips/ecdh/fips_ecdhvs.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. #ifndef OPENSSL_FIPS
  56. #include <stdio.h>
  57. int main(int argc, char **argv)
  58. {
  59. printf("No FIPS ECDH support\n");
  60. return(0);
  61. }
  62. #else
  63. #include <openssl/crypto.h>
  64. #include <openssl/bn.h>
  65. #include <openssl/ecdh.h>
  66. #include <openssl/fips.h>
  67. #include <openssl/err.h>
  68. #include <openssl/evp.h>
  69. #include <string.h>
  70. #include <ctype.h>
  71. #include "fips_utl.h"
  72. static const EVP_MD *parse_md(char *line)
  73. {
  74. char *p;
  75. if (line[0] != '[' || line[1] != 'E')
  76. return NULL;
  77. p = strchr(line, '-');
  78. if (!p)
  79. return NULL;
  80. line = p + 1;
  81. p = strchr(line, ']');
  82. if (!p)
  83. return NULL;
  84. *p = 0;
  85. p = line;
  86. while(isspace(*p))
  87. p++;
  88. if (!strcmp(p, "SHA1"))
  89. return EVP_sha1();
  90. else if (!strcmp(p, "SHA224"))
  91. return EVP_sha224();
  92. else if (!strcmp(p, "SHA256"))
  93. return EVP_sha256();
  94. else if (!strcmp(p, "SHA384"))
  95. return EVP_sha384();
  96. else if (!strcmp(p, "SHA512"))
  97. return EVP_sha512();
  98. else
  99. return NULL;
  100. }
  101. static int lookup_curve(char *cname)
  102. {
  103. char *p;
  104. p = strchr(cname, ':');
  105. if (!p)
  106. {
  107. fprintf(stderr, "Parse error: missing :\n");
  108. return NID_undef;
  109. }
  110. cname = p + 1;
  111. while(isspace(*cname))
  112. cname++;
  113. p = strchr(cname, ']');
  114. if (!p)
  115. {
  116. fprintf(stderr, "Parse error: missing ]\n");
  117. return NID_undef;
  118. }
  119. *p = 0;
  120. if (!strcmp(cname, "B-163"))
  121. return NID_sect163r2;
  122. if (!strcmp(cname, "B-233"))
  123. return NID_sect233r1;
  124. if (!strcmp(cname, "B-283"))
  125. return NID_sect283r1;
  126. if (!strcmp(cname, "B-409"))
  127. return NID_sect409r1;
  128. if (!strcmp(cname, "B-571"))
  129. return NID_sect571r1;
  130. if (!strcmp(cname, "K-163"))
  131. return NID_sect163k1;
  132. if (!strcmp(cname, "K-233"))
  133. return NID_sect233k1;
  134. if (!strcmp(cname, "K-283"))
  135. return NID_sect283k1;
  136. if (!strcmp(cname, "K-409"))
  137. return NID_sect409k1;
  138. if (!strcmp(cname, "K-571"))
  139. return NID_sect571k1;
  140. if (!strcmp(cname, "P-192"))
  141. return NID_X9_62_prime192v1;
  142. if (!strcmp(cname, "P-224"))
  143. return NID_secp224r1;
  144. if (!strcmp(cname, "P-256"))
  145. return NID_X9_62_prime256v1;
  146. if (!strcmp(cname, "P-384"))
  147. return NID_secp384r1;
  148. if (!strcmp(cname, "P-521"))
  149. return NID_secp521r1;
  150. fprintf(stderr, "Unknown Curve name %s\n", cname);
  151. return NID_undef;
  152. }
  153. static EC_POINT *make_peer(EC_GROUP *group, BIGNUM *x, BIGNUM *y)
  154. {
  155. EC_POINT *peer;
  156. int rv;
  157. BN_CTX *c;
  158. peer = EC_POINT_new(group);
  159. if (!peer)
  160. return NULL;
  161. c = BN_CTX_new();
  162. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group))
  163. == NID_X9_62_prime_field)
  164. rv = EC_POINT_set_affine_coordinates_GFp(group, peer, x, y, c);
  165. else
  166. #ifdef OPENSSL_NO_EC2M
  167. {
  168. fprintf(stderr, "ERROR: GF2m not supported\n");
  169. exit(1);
  170. }
  171. #else
  172. rv = EC_POINT_set_affine_coordinates_GF2m(group, peer, x, y, c);
  173. #endif
  174. BN_CTX_free(c);
  175. if (rv)
  176. return peer;
  177. EC_POINT_free(peer);
  178. return NULL;
  179. }
  180. static int ec_print_pubkey(FILE *out, EC_KEY *key)
  181. {
  182. const EC_POINT *pt;
  183. const EC_GROUP *grp;
  184. const EC_METHOD *meth;
  185. int rv;
  186. BIGNUM *tx, *ty;
  187. BN_CTX *ctx;
  188. ctx = BN_CTX_new();
  189. if (!ctx)
  190. return 0;
  191. tx = BN_CTX_get(ctx);
  192. ty = BN_CTX_get(ctx);
  193. if (!tx || !ty)
  194. return 0;
  195. grp = EC_KEY_get0_group(key);
  196. pt = EC_KEY_get0_public_key(key);
  197. meth = EC_GROUP_method_of(grp);
  198. if (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field)
  199. rv = EC_POINT_get_affine_coordinates_GFp(grp, pt, tx, ty, ctx);
  200. else
  201. #ifdef OPENSSL_NO_EC2M
  202. {
  203. fprintf(stderr, "ERROR: GF2m not supported\n");
  204. exit(1);
  205. }
  206. #else
  207. rv = EC_POINT_get_affine_coordinates_GF2m(grp, pt, tx, ty, ctx);
  208. #endif
  209. do_bn_print_name(out, "QeIUTx", tx);
  210. do_bn_print_name(out, "QeIUTy", ty);
  211. BN_CTX_free(ctx);
  212. return rv;
  213. }
  214. static void ec_output_Zhash(FILE *out, int exout, EC_GROUP *group,
  215. BIGNUM *ix, BIGNUM *iy, BIGNUM *id, BIGNUM *cx,
  216. BIGNUM *cy, const EVP_MD *md,
  217. unsigned char *rhash, size_t rhashlen)
  218. {
  219. EC_KEY *ec = NULL;
  220. EC_POINT *peerkey = NULL;
  221. unsigned char *Z;
  222. unsigned char chash[EVP_MAX_MD_SIZE];
  223. int Zlen;
  224. ec = EC_KEY_new();
  225. EC_KEY_set_group(ec, group);
  226. peerkey = make_peer(group, cx, cy);
  227. if (rhash == NULL)
  228. {
  229. rhashlen = M_EVP_MD_size(md);
  230. EC_KEY_generate_key(ec);
  231. ec_print_pubkey(out, ec);
  232. }
  233. else
  234. {
  235. EC_KEY_set_public_key_affine_coordinates(ec, ix, iy);
  236. EC_KEY_set_private_key(ec, id);
  237. }
  238. Zlen = (EC_GROUP_get_degree(group) + 7)/8;
  239. Z = OPENSSL_malloc(Zlen);
  240. if (!Z)
  241. exit(1);
  242. ECDH_compute_key(Z, Zlen, peerkey, ec, 0);
  243. if (exout)
  244. OutputValue("Z", Z, Zlen, out, 0);
  245. FIPS_digest(Z, Zlen, chash, NULL, md);
  246. OutputValue(rhash ? "IUTHashZZ" : "HashZZ", chash, rhashlen, out, 0);
  247. if (rhash)
  248. {
  249. fprintf(out, "Result = %s\n",
  250. memcmp(chash, rhash, rhashlen) ? "F" : "P");
  251. }
  252. OPENSSL_cleanse(Z, Zlen);
  253. OPENSSL_free(Z);
  254. EC_KEY_free(ec);
  255. EC_POINT_free(peerkey);
  256. }
  257. int main(int argc,char **argv)
  258. {
  259. char **args = argv + 1;
  260. int argn = argc - 1;
  261. FILE *in, *out;
  262. char buf[2048], lbuf[2048];
  263. unsigned char *rhash = NULL;
  264. long rhashlen;
  265. BIGNUM *cx = NULL, *cy = NULL;
  266. BIGNUM *id = NULL, *ix = NULL, *iy = NULL;
  267. const EVP_MD *md = NULL;
  268. EC_GROUP *group = NULL;
  269. char *keyword = NULL, *value = NULL;
  270. int do_verify = -1, exout = 0;
  271. int curve_nids[5] = {0,0,0,0,0};
  272. int param_set = -1;
  273. fips_algtest_init();
  274. if (argn && !strcmp(*args, "ecdhver"))
  275. {
  276. do_verify = 1;
  277. args++;
  278. argn--;
  279. }
  280. else if (argn && !strcmp(*args, "ecdhgen"))
  281. {
  282. do_verify = 0;
  283. args++;
  284. argn--;
  285. }
  286. if (argn && !strcmp(*args, "-exout"))
  287. {
  288. exout = 1;
  289. args++;
  290. argn--;
  291. }
  292. if (do_verify == -1)
  293. {
  294. fprintf(stderr,"%s [ecdhver|ecdhgen|] [-exout] (infile outfile)\n",argv[0]);
  295. exit(1);
  296. }
  297. if (argn == 2)
  298. {
  299. in = fopen(*args, "r");
  300. if (!in)
  301. {
  302. fprintf(stderr, "Error opening input file\n");
  303. exit(1);
  304. }
  305. out = fopen(args[1], "w");
  306. if (!out)
  307. {
  308. fprintf(stderr, "Error opening output file\n");
  309. exit(1);
  310. }
  311. }
  312. else if (argn == 0)
  313. {
  314. in = stdin;
  315. out = stdout;
  316. }
  317. else
  318. {
  319. fprintf(stderr,"%s [dhver|dhgen|] [-exout] (infile outfile)\n",argv[0]);
  320. exit(1);
  321. }
  322. while (fgets(buf, sizeof(buf), in) != NULL)
  323. {
  324. fputs(buf, out);
  325. if (buf[0] == '[' && buf[1] == 'E')
  326. {
  327. int c = buf[2];
  328. if (c < 'A' || c > 'E')
  329. goto parse_error;
  330. param_set = c - 'A';
  331. /* If just [E?] then initial paramset */
  332. if (buf[3] == ']')
  333. continue;
  334. if (group)
  335. EC_GROUP_free(group);
  336. group = EC_GROUP_new_by_curve_name(curve_nids[c - 'A']);
  337. }
  338. if (strlen(buf) > 10 && !strncmp(buf, "[Curve", 6))
  339. {
  340. int nid;
  341. if (param_set == -1)
  342. goto parse_error;
  343. nid = lookup_curve(buf);
  344. if (nid == NID_undef)
  345. goto parse_error;
  346. curve_nids[param_set] = nid;
  347. }
  348. if (strlen(buf) > 6 && !strncmp(buf, "[E", 2))
  349. {
  350. md = parse_md(buf);
  351. if (md == NULL)
  352. goto parse_error;
  353. continue;
  354. }
  355. if (!parse_line(&keyword, &value, lbuf, buf))
  356. continue;
  357. if (!strcmp(keyword, "QeCAVSx"))
  358. {
  359. if (!do_hex2bn(&cx, value))
  360. goto parse_error;
  361. }
  362. else if (!strcmp(keyword, "QeCAVSy"))
  363. {
  364. if (!do_hex2bn(&cy, value))
  365. goto parse_error;
  366. if (do_verify == 0)
  367. ec_output_Zhash(out, exout, group,
  368. NULL, NULL, NULL,
  369. cx, cy, md, rhash, rhashlen);
  370. }
  371. else if (!strcmp(keyword, "deIUT"))
  372. {
  373. if (!do_hex2bn(&id, value))
  374. goto parse_error;
  375. }
  376. else if (!strcmp(keyword, "QeIUTx"))
  377. {
  378. if (!do_hex2bn(&ix, value))
  379. goto parse_error;
  380. }
  381. else if (!strcmp(keyword, "QeIUTy"))
  382. {
  383. if (!do_hex2bn(&iy, value))
  384. goto parse_error;
  385. }
  386. else if (!strcmp(keyword, "CAVSHashZZ"))
  387. {
  388. if (!md)
  389. goto parse_error;
  390. rhash = hex2bin_m(value, &rhashlen);
  391. if (!rhash || rhashlen != M_EVP_MD_size(md))
  392. goto parse_error;
  393. ec_output_Zhash(out, exout, group, ix, iy, id, cx, cy,
  394. md, rhash, rhashlen);
  395. }
  396. }
  397. return 0;
  398. parse_error:
  399. fprintf(stderr, "Error Parsing request file\n");
  400. exit(1);
  401. }
  402. #endif