ecdhtest.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* ====================================================================
  10. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  11. *
  12. * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
  13. * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
  14. * to the OpenSSL project.
  15. *
  16. * The ECC Code is licensed pursuant to the OpenSSL open source
  17. * license provided below.
  18. *
  19. * The ECDH software is originally written by Douglas Stebila of
  20. * Sun Microsystems Laboratories.
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "../e_os.h"
  27. #include <openssl/opensslconf.h> /* for OPENSSL_NO_EC */
  28. #include <openssl/crypto.h>
  29. #include <openssl/bio.h>
  30. #include <openssl/bn.h>
  31. #include <openssl/objects.h>
  32. #include <openssl/rand.h>
  33. #include <openssl/sha.h>
  34. #include <openssl/err.h>
  35. #ifdef OPENSSL_NO_EC
  36. int main(int argc, char *argv[])
  37. {
  38. printf("No ECDH support\n");
  39. return (0);
  40. }
  41. #else
  42. # include <openssl/ec.h>
  43. static const char rnd_seed[] =
  44. "string to make the random number generator think it has entropy";
  45. static const int KDF1_SHA1_len = 20;
  46. static void *KDF1_SHA1(const void *in, size_t inlen, void *out,
  47. size_t *outlen)
  48. {
  49. if (*outlen < SHA_DIGEST_LENGTH)
  50. return NULL;
  51. *outlen = SHA_DIGEST_LENGTH;
  52. return SHA1(in, inlen, out);
  53. }
  54. static int test_ecdh_curve(int nid, BN_CTX *ctx, BIO *out)
  55. {
  56. EC_KEY *a = NULL;
  57. EC_KEY *b = NULL;
  58. BIGNUM *x_a = NULL, *y_a = NULL, *x_b = NULL, *y_b = NULL;
  59. char buf[12];
  60. unsigned char *abuf = NULL, *bbuf = NULL;
  61. int i, alen, blen, aout, bout, ret = 0;
  62. const EC_GROUP *group;
  63. a = EC_KEY_new_by_curve_name(nid);
  64. b = EC_KEY_new_by_curve_name(nid);
  65. if (a == NULL || b == NULL)
  66. goto err;
  67. group = EC_KEY_get0_group(a);
  68. if ((x_a = BN_new()) == NULL)
  69. goto err;
  70. if ((y_a = BN_new()) == NULL)
  71. goto err;
  72. if ((x_b = BN_new()) == NULL)
  73. goto err;
  74. if ((y_b = BN_new()) == NULL)
  75. goto err;
  76. BIO_puts(out, "Testing key generation with ");
  77. BIO_puts(out, OBJ_nid2sn(nid));
  78. # ifdef NOISY
  79. BIO_puts(out, "\n");
  80. # else
  81. (void)BIO_flush(out);
  82. # endif
  83. if (!EC_KEY_generate_key(a))
  84. goto err;
  85. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  86. NID_X9_62_prime_field) {
  87. if (!EC_POINT_get_affine_coordinates_GFp
  88. (group, EC_KEY_get0_public_key(a), x_a, y_a, ctx))
  89. goto err;
  90. }
  91. # ifndef OPENSSL_NO_EC2M
  92. else {
  93. if (!EC_POINT_get_affine_coordinates_GF2m(group,
  94. EC_KEY_get0_public_key(a),
  95. x_a, y_a, ctx))
  96. goto err;
  97. }
  98. # endif
  99. # ifdef NOISY
  100. BIO_puts(out, " pri 1=");
  101. BN_print(out, a->priv_key);
  102. BIO_puts(out, "\n pub 1=");
  103. BN_print(out, x_a);
  104. BIO_puts(out, ",");
  105. BN_print(out, y_a);
  106. BIO_puts(out, "\n");
  107. # else
  108. BIO_printf(out, " .");
  109. (void)BIO_flush(out);
  110. # endif
  111. if (!EC_KEY_generate_key(b))
  112. goto err;
  113. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  114. NID_X9_62_prime_field) {
  115. if (!EC_POINT_get_affine_coordinates_GFp
  116. (group, EC_KEY_get0_public_key(b), x_b, y_b, ctx))
  117. goto err;
  118. }
  119. # ifndef OPENSSL_NO_EC2M
  120. else {
  121. if (!EC_POINT_get_affine_coordinates_GF2m(group,
  122. EC_KEY_get0_public_key(b),
  123. x_b, y_b, ctx))
  124. goto err;
  125. }
  126. # endif
  127. # ifdef NOISY
  128. BIO_puts(out, " pri 2=");
  129. BN_print(out, b->priv_key);
  130. BIO_puts(out, "\n pub 2=");
  131. BN_print(out, x_b);
  132. BIO_puts(out, ",");
  133. BN_print(out, y_b);
  134. BIO_puts(out, "\n");
  135. # else
  136. BIO_printf(out, ".");
  137. (void)BIO_flush(out);
  138. # endif
  139. alen = KDF1_SHA1_len;
  140. abuf = OPENSSL_malloc(alen);
  141. aout =
  142. ECDH_compute_key(abuf, alen, EC_KEY_get0_public_key(b), a, KDF1_SHA1);
  143. # ifdef NOISY
  144. BIO_puts(out, " key1 =");
  145. for (i = 0; i < aout; i++) {
  146. sprintf(buf, "%02X", abuf[i]);
  147. BIO_puts(out, buf);
  148. }
  149. BIO_puts(out, "\n");
  150. # else
  151. BIO_printf(out, ".");
  152. (void)BIO_flush(out);
  153. # endif
  154. blen = KDF1_SHA1_len;
  155. bbuf = OPENSSL_malloc(blen);
  156. bout =
  157. ECDH_compute_key(bbuf, blen, EC_KEY_get0_public_key(a), b, KDF1_SHA1);
  158. # ifdef NOISY
  159. BIO_puts(out, " key2 =");
  160. for (i = 0; i < bout; i++) {
  161. sprintf(buf, "%02X", bbuf[i]);
  162. BIO_puts(out, buf);
  163. }
  164. BIO_puts(out, "\n");
  165. # else
  166. BIO_printf(out, ".");
  167. (void)BIO_flush(out);
  168. # endif
  169. if ((aout < 4) || (bout != aout) || (memcmp(abuf, bbuf, aout) != 0)) {
  170. # ifndef NOISY
  171. BIO_printf(out, " failed\n\n");
  172. BIO_printf(out, "key a:\n");
  173. BIO_printf(out, "private key: ");
  174. BN_print(out, EC_KEY_get0_private_key(a));
  175. BIO_printf(out, "\n");
  176. BIO_printf(out, "public key (x,y): ");
  177. BN_print(out, x_a);
  178. BIO_printf(out, ",");
  179. BN_print(out, y_a);
  180. BIO_printf(out, "\nkey b:\n");
  181. BIO_printf(out, "private key: ");
  182. BN_print(out, EC_KEY_get0_private_key(b));
  183. BIO_printf(out, "\n");
  184. BIO_printf(out, "public key (x,y): ");
  185. BN_print(out, x_b);
  186. BIO_printf(out, ",");
  187. BN_print(out, y_b);
  188. BIO_printf(out, "\n");
  189. BIO_printf(out, "generated key a: ");
  190. for (i = 0; i < bout; i++) {
  191. sprintf(buf, "%02X", bbuf[i]);
  192. BIO_puts(out, buf);
  193. }
  194. BIO_printf(out, "\n");
  195. BIO_printf(out, "generated key b: ");
  196. for (i = 0; i < aout; i++) {
  197. sprintf(buf, "%02X", abuf[i]);
  198. BIO_puts(out, buf);
  199. }
  200. BIO_printf(out, "\n");
  201. # endif
  202. fprintf(stderr, "Error in ECDH routines\n");
  203. ret = 0;
  204. } else {
  205. # ifndef NOISY
  206. BIO_printf(out, " ok\n");
  207. # endif
  208. ret = 1;
  209. }
  210. err:
  211. ERR_print_errors_fp(stderr);
  212. OPENSSL_free(abuf);
  213. OPENSSL_free(bbuf);
  214. BN_free(x_a);
  215. BN_free(y_a);
  216. BN_free(x_b);
  217. BN_free(y_b);
  218. EC_KEY_free(b);
  219. EC_KEY_free(a);
  220. return (ret);
  221. }
  222. typedef struct {
  223. const int nid;
  224. const char *da;
  225. const char *db;
  226. const char *Z;
  227. } ecdh_kat_t;
  228. static const ecdh_kat_t ecdh_kats[] = {
  229. /* Keys and shared secrets from RFC 5114 */
  230. { NID_X9_62_prime192v1,
  231. "323FA3169D8E9C6593F59476BC142000AB5BE0E249C43426",
  232. "631F95BB4A67632C9C476EEE9AB695AB240A0499307FCF62",
  233. "AD420182633F8526BFE954ACDA376F05E5FF4F837F54FEBE" },
  234. { NID_secp224r1,
  235. "B558EB6C288DA707BBB4F8FBAE2AB9E9CB62E3BC5C7573E22E26D37F",
  236. "AC3B1ADD3D9770E6F6A708EE9F3B8E0AB3B480E9F27F85C88B5E6D18",
  237. "52272F50F46F4EDC9151569092F46DF2D96ECC3B6DC1714A4EA949FA" },
  238. { NID_X9_62_prime256v1,
  239. "814264145F2F56F2E96A8E337A1284993FAF432A5ABCE59E867B7291D507A3AF",
  240. "2CE1788EC197E096DB95A200CC0AB26A19CE6BCCAD562B8EEE1B593761CF7F41",
  241. "DD0F5396219D1EA393310412D19A08F1F5811E9DC8EC8EEA7F80D21C820C2788" },
  242. { NID_secp384r1,
  243. "D27335EA71664AF244DD14E9FD1260715DFD8A7965571C48D709EE7A7962A156"
  244. "D706A90CBCB5DF2986F05FEADB9376F1",
  245. "52D1791FDB4B70F89C0F00D456C2F7023B6125262C36A7DF1F80231121CCE3D3"
  246. "9BE52E00C194A4132C4A6C768BCD94D2",
  247. "5EA1FC4AF7256D2055981B110575E0A8CAE53160137D904C59D926EB1B8456E4"
  248. "27AA8A4540884C37DE159A58028ABC0E" },
  249. { NID_secp521r1,
  250. "0113F82DA825735E3D97276683B2B74277BAD27335EA71664AF2430CC4F33459"
  251. "B9669EE78B3FFB9B8683015D344DCBFEF6FB9AF4C6C470BE254516CD3C1A1FB4"
  252. "7362",
  253. "00CEE3480D8645A17D249F2776D28BAE616952D1791FDB4B70F7C3378732AA1B"
  254. "22928448BCD1DC2496D435B01048066EBE4F72903C361B1A9DC1193DC2C9D089"
  255. "1B96",
  256. "00CDEA89621CFA46B132F9E4CFE2261CDE2D4368EB5656634C7CC98C7A00CDE5"
  257. "4ED1866A0DD3E6126C9D2F845DAFF82CEB1DA08F5D87521BB0EBECA77911169C"
  258. "20CC" },
  259. /* Keys and shared secrets from RFC 5903 */
  260. { NID_X9_62_prime256v1,
  261. "C88F01F510D9AC3F70A292DAA2316DE544E9AAB8AFE84049C62A9C57862D1433",
  262. "C6EF9C5D78AE012A011164ACB397CE2088685D8F06BF9BE0B283AB46476BEE53",
  263. "D6840F6B42F6EDAFD13116E0E12565202FEF8E9ECE7DCE03812464D04B9442DE" },
  264. { NID_secp384r1,
  265. "099F3C7034D4A2C699884D73A375A67F7624EF7C6B3C0F160647B67414DCE655"
  266. "E35B538041E649EE3FAEF896783AB194",
  267. "41CB0779B4BDB85D47846725FBEC3C9430FAB46CC8DC5060855CC9BDA0AA2942"
  268. "E0308312916B8ED2960E4BD55A7448FC",
  269. "11187331C279962D93D604243FD592CB9D0A926F422E47187521287E7156C5C4"
  270. "D603135569B9E9D09CF5D4A270F59746" },
  271. { NID_secp521r1,
  272. "0037ADE9319A89F4DABDB3EF411AACCCA5123C61ACAB57B5393DCE47608172A0"
  273. "95AA85A30FE1C2952C6771D937BA9777F5957B2639BAB072462F68C27A57382D"
  274. "4A52",
  275. "0145BA99A847AF43793FDD0E872E7CDFA16BE30FDC780F97BCCC3F078380201E"
  276. "9C677D600B343757A3BDBF2A3163E4C2F869CCA7458AA4A4EFFC311F5CB15168"
  277. "5EB9",
  278. "01144C7D79AE6956BC8EDB8E7C787C4521CB086FA64407F97894E5E6B2D79B04"
  279. "D1427E73CA4BAA240A34786859810C06B3C715A3A8CC3151F2BEE417996D19F3"
  280. "DDEA" },
  281. /* Keys and shared secrets from RFC 7027 */
  282. { NID_brainpoolP256r1,
  283. "81DB1EE100150FF2EA338D708271BE38300CB54241D79950F77B063039804F1D",
  284. "55E40BC41E37E3E2AD25C3C6654511FFA8474A91A0032087593852D3E7D76BD3",
  285. "89AFC39D41D3B327814B80940B042590F96556EC91E6AE7939BCE31F3A18BF2B" },
  286. { NID_brainpoolP384r1,
  287. "1E20F5E048A5886F1F157C74E91BDE2B98C8B52D58E5003D57053FC4B0BD65D6"
  288. "F15EB5D1EE1610DF870795143627D042",
  289. "032640BC6003C59260F7250C3DB58CE647F98E1260ACCE4ACDA3DD869F74E01F"
  290. "8BA5E0324309DB6A9831497ABAC96670",
  291. "0BD9D3A7EA0B3D519D09D8E48D0785FB744A6B355E6304BC51C229FBBCE239BB"
  292. "ADF6403715C35D4FB2A5444F575D4F42" },
  293. { NID_brainpoolP512r1,
  294. "16302FF0DBBB5A8D733DAB7141C1B45ACBC8715939677F6A56850A38BD87BD59"
  295. "B09E80279609FF333EB9D4C061231FB26F92EEB04982A5F1D1764CAD57665422",
  296. "230E18E1BCC88A362FA54E4EA3902009292F7F8033624FD471B5D8ACE49D12CF"
  297. "ABBC19963DAB8E2F1EBA00BFFB29E4D72D13F2224562F405CB80503666B25429",
  298. "A7927098655F1F9976FA50A9D566865DC530331846381C87256BAF3226244B76"
  299. "D36403C024D7BBF0AA0803EAFF405D3D24F11A9B5C0BEF679FE1454B21C4CD1F" }
  300. };
  301. /* Given private value and NID, create EC_KEY structure */
  302. static EC_KEY *mk_eckey(int nid, const char *str)
  303. {
  304. int ok = 0;
  305. EC_KEY *k = NULL;
  306. BIGNUM *priv = NULL;
  307. EC_POINT *pub = NULL;
  308. const EC_GROUP *grp;
  309. k = EC_KEY_new_by_curve_name(nid);
  310. if (!k)
  311. goto err;
  312. if(!BN_hex2bn(&priv, str))
  313. goto err;
  314. if (!priv)
  315. goto err;
  316. if (!EC_KEY_set_private_key(k, priv))
  317. goto err;
  318. grp = EC_KEY_get0_group(k);
  319. pub = EC_POINT_new(grp);
  320. if (!pub)
  321. goto err;
  322. if (!EC_POINT_mul(grp, pub, priv, NULL, NULL, NULL))
  323. goto err;
  324. if (!EC_KEY_set_public_key(k, pub))
  325. goto err;
  326. ok = 1;
  327. err:
  328. BN_clear_free(priv);
  329. EC_POINT_free(pub);
  330. if (ok)
  331. return k;
  332. EC_KEY_free(k);
  333. return NULL;
  334. }
  335. /*
  336. * Known answer test: compute shared secret and check it matches expected
  337. * value.
  338. */
  339. static int ecdh_kat(BIO *out, const ecdh_kat_t *kat)
  340. {
  341. int rv = 0;
  342. EC_KEY *key1 = NULL, *key2 = NULL;
  343. BIGNUM *bnz = NULL;
  344. unsigned char *Ztmp = NULL, *Z = NULL;
  345. size_t Ztmplen, Zlen;
  346. BIO_puts(out, "Testing ECDH shared secret with ");
  347. BIO_puts(out, OBJ_nid2sn(kat->nid));
  348. if(!BN_hex2bn(&bnz, kat->Z))
  349. goto err;
  350. key1 = mk_eckey(kat->nid, kat->da);
  351. key2 = mk_eckey(kat->nid, kat->db);
  352. if (!key1 || !key2)
  353. goto err;
  354. Ztmplen = (EC_GROUP_get_degree(EC_KEY_get0_group(key1)) + 7) / 8;
  355. Zlen = BN_num_bytes(bnz);
  356. if (Zlen > Ztmplen)
  357. goto err;
  358. if((Ztmp = OPENSSL_zalloc(Ztmplen)) == NULL)
  359. goto err;
  360. if((Z = OPENSSL_zalloc(Ztmplen)) == NULL)
  361. goto err;
  362. if(!BN_bn2binpad(bnz, Z, Ztmplen))
  363. goto err;
  364. if (!ECDH_compute_key(Ztmp, Ztmplen,
  365. EC_KEY_get0_public_key(key2), key1, 0))
  366. goto err;
  367. if (memcmp(Ztmp, Z, Ztmplen))
  368. goto err;
  369. memset(Ztmp, 0, Ztmplen);
  370. if (!ECDH_compute_key(Ztmp, Ztmplen,
  371. EC_KEY_get0_public_key(key1), key2, 0))
  372. goto err;
  373. if (memcmp(Ztmp, Z, Ztmplen))
  374. goto err;
  375. rv = 1;
  376. err:
  377. EC_KEY_free(key1);
  378. EC_KEY_free(key2);
  379. OPENSSL_free(Ztmp);
  380. OPENSSL_free(Z);
  381. BN_free(bnz);
  382. if (rv)
  383. BIO_puts(out, " ok\n");
  384. else {
  385. fprintf(stderr, "Error in ECDH routines\n");
  386. ERR_print_errors_fp(stderr);
  387. }
  388. return rv;
  389. }
  390. #include "ecdhtest_cavs.h"
  391. /*
  392. * NIST SP800-56A co-factor ECDH tests.
  393. * KATs taken from NIST documents with parameters:
  394. *
  395. * - (QCAVSx,QCAVSy) is the public key for CAVS.
  396. * - dIUT is the private key for IUT.
  397. * - (QIUTx,QIUTy) is the public key for IUT.
  398. * - ZIUT is the shared secret KAT.
  399. *
  400. * CAVS: Cryptographic Algorithm Validation System
  401. * IUT: Implementation Under Test
  402. *
  403. * This function tests two things:
  404. *
  405. * 1. dIUT * G = (QIUTx,QIUTy)
  406. * i.e. public key for IUT computes correctly.
  407. * 2. x-coord of cofactor * dIUT * (QCAVSx,QCAVSy) = ZIUT
  408. * i.e. co-factor ECDH key computes correctly.
  409. *
  410. * returns zero on failure or unsupported curve. One otherwise.
  411. */
  412. static int ecdh_cavs_kat(BIO *out, const ecdh_cavs_kat_t *kat)
  413. {
  414. int rv = 0, is_char_two = 0;
  415. EC_KEY *key1 = NULL;
  416. EC_POINT *pub = NULL;
  417. const EC_GROUP *group = NULL;
  418. BIGNUM *bnz = NULL, *x = NULL, *y = NULL;
  419. unsigned char *Ztmp = NULL, *Z = NULL;
  420. size_t Ztmplen, Zlen;
  421. BIO_puts(out, "Testing ECC CDH Primitive SP800-56A with ");
  422. BIO_puts(out, OBJ_nid2sn(kat->nid));
  423. /* dIUT is IUT's private key */
  424. if ((key1 = mk_eckey(kat->nid, kat->dIUT)) == NULL)
  425. goto err;
  426. /* these are cofactor ECDH KATs */
  427. EC_KEY_set_flags(key1, EC_FLAG_COFACTOR_ECDH);
  428. if ((group = EC_KEY_get0_group(key1)) == NULL)
  429. goto err;
  430. if ((pub = EC_POINT_new(group)) == NULL)
  431. goto err;
  432. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_characteristic_two_field)
  433. is_char_two = 1;
  434. /* (QIUTx, QIUTy) is IUT's public key */
  435. if(!BN_hex2bn(&x, kat->QIUTx))
  436. goto err;
  437. if(!BN_hex2bn(&y, kat->QIUTy))
  438. goto err;
  439. if (is_char_two) {
  440. #ifdef OPENSSL_NO_EC2M
  441. goto err;
  442. #else
  443. if (!EC_POINT_set_affine_coordinates_GF2m(group, pub, x, y, NULL))
  444. goto err;
  445. #endif
  446. }
  447. else {
  448. if (!EC_POINT_set_affine_coordinates_GFp(group, pub, x, y, NULL))
  449. goto err;
  450. }
  451. /* dIUT * G = (QIUTx, QIUTy) should hold */
  452. if (EC_POINT_cmp(group, EC_KEY_get0_public_key(key1), pub, NULL))
  453. goto err;
  454. /* (QCAVSx, QCAVSy) is CAVS's public key */
  455. if(!BN_hex2bn(&x, kat->QCAVSx))
  456. goto err;
  457. if(!BN_hex2bn(&y, kat->QCAVSy))
  458. goto err;
  459. if (is_char_two) {
  460. #ifdef OPENSSL_NO_EC2M
  461. goto err;
  462. #else
  463. if (!EC_POINT_set_affine_coordinates_GF2m(group, pub, x, y, NULL))
  464. goto err;
  465. #endif
  466. }
  467. else {
  468. if (!EC_POINT_set_affine_coordinates_GFp(group, pub, x, y, NULL))
  469. goto err;
  470. }
  471. /* ZIUT is the shared secret */
  472. if(!BN_hex2bn(&bnz, kat->ZIUT))
  473. goto err;
  474. Ztmplen = (EC_GROUP_get_degree(EC_KEY_get0_group(key1)) + 7) / 8;
  475. Zlen = BN_num_bytes(bnz);
  476. if (Zlen > Ztmplen)
  477. goto err;
  478. if((Ztmp = OPENSSL_zalloc(Ztmplen)) == NULL)
  479. goto err;
  480. if((Z = OPENSSL_zalloc(Ztmplen)) == NULL)
  481. goto err;
  482. if(!BN_bn2binpad(bnz, Z, Ztmplen))
  483. goto err;
  484. if (!ECDH_compute_key(Ztmp, Ztmplen, pub, key1, 0))
  485. goto err;
  486. /* shared secrets should be identical */
  487. if (memcmp(Ztmp, Z, Ztmplen))
  488. goto err;
  489. rv = 1;
  490. err:
  491. EC_KEY_free(key1);
  492. EC_POINT_free(pub);
  493. BN_free(bnz);
  494. BN_free(x);
  495. BN_free(y);
  496. OPENSSL_free(Ztmp);
  497. OPENSSL_free(Z);
  498. if (rv) {
  499. BIO_puts(out, " ok\n");
  500. }
  501. else {
  502. fprintf(stderr, "Error in ECC CDH routines\n");
  503. ERR_print_errors_fp(stderr);
  504. }
  505. return rv;
  506. }
  507. int main(int argc, char *argv[])
  508. {
  509. BN_CTX *ctx = NULL;
  510. int nid, ret = 1;
  511. EC_builtin_curve *curves = NULL;
  512. size_t crv_len = 0, n = 0;
  513. BIO *out;
  514. CRYPTO_set_mem_debug(1);
  515. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  516. RAND_seed(rnd_seed, sizeof rnd_seed);
  517. out = BIO_new(BIO_s_file());
  518. if (out == NULL)
  519. EXIT(1);
  520. BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
  521. if ((ctx = BN_CTX_new()) == NULL)
  522. goto err;
  523. /* get a list of all internal curves */
  524. crv_len = EC_get_builtin_curves(NULL, 0);
  525. curves = OPENSSL_malloc(sizeof(*curves) * crv_len);
  526. if (curves == NULL) goto err;
  527. if (!EC_get_builtin_curves(curves, crv_len)) goto err;
  528. /* NAMED CURVES TESTS */
  529. for (n = 0; n < crv_len; n++) {
  530. nid = curves[n].nid;
  531. /*
  532. * Skipped for X25519 because affine coordinate operations are not
  533. * supported for this curve.
  534. * Higher level ECDH tests are performed in evptests.txt instead.
  535. */
  536. if (nid == NID_X25519)
  537. continue;
  538. if (!test_ecdh_curve(nid, ctx, out)) goto err;
  539. }
  540. /* KATs */
  541. for (n = 0; n < (sizeof(ecdh_kats)/sizeof(ecdh_kat_t)); n++) {
  542. if (!ecdh_kat(out, &ecdh_kats[n]))
  543. goto err;
  544. }
  545. /* NIST SP800-56A co-factor ECDH KATs */
  546. for (n = 0; n < (sizeof(ecdh_cavs_kats)/sizeof(ecdh_cavs_kat_t)); n++) {
  547. if (!ecdh_cavs_kat(out, &ecdh_cavs_kats[n]))
  548. goto err;
  549. }
  550. ret = 0;
  551. err:
  552. ERR_print_errors_fp(stderr);
  553. OPENSSL_free(curves);
  554. BN_CTX_free(ctx);
  555. BIO_free(out);
  556. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  557. if (CRYPTO_mem_leaks_fp(stderr) <= 0)
  558. ret = 1;
  559. #endif
  560. EXIT(ret);
  561. }
  562. #endif