ecdsatest.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* crypto/ecdsa/ecdsatest.c */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000-2005 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. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. *
  61. * Portions of the attached software ("Contribution") are developed by
  62. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  63. *
  64. * The Contribution is licensed pursuant to the OpenSSL open source
  65. * license provided above.
  66. *
  67. * The elliptic curve binary polynomial software is originally written by
  68. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  69. *
  70. */
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_ECDSA is defined */
  75. #ifdef OPENSSL_NO_ECDSA
  76. int main(int argc, char *argv[])
  77. {
  78. puts("Elliptic curves are disabled.");
  79. return 0;
  80. }
  81. #else
  82. # include <openssl/crypto.h>
  83. # include <openssl/bio.h>
  84. # include <openssl/evp.h>
  85. # include <openssl/bn.h>
  86. # include <openssl/ecdsa.h>
  87. # ifndef OPENSSL_NO_ENGINE
  88. # include <openssl/engine.h>
  89. # endif
  90. # include <openssl/err.h>
  91. # include <openssl/rand.h>
  92. static const char rnd_seed[] = "string to make the random number generator "
  93. "think it has entropy";
  94. /* declaration of the test functions */
  95. int x9_62_tests(BIO *);
  96. int x9_62_test_internal(BIO *out, int nid, const char *r, const char *s);
  97. int test_builtin(BIO *);
  98. /* functions to change the RAND_METHOD */
  99. int change_rand(void);
  100. int restore_rand(void);
  101. int fbytes(unsigned char *buf, int num);
  102. RAND_METHOD fake_rand;
  103. const RAND_METHOD *old_rand;
  104. int change_rand(void)
  105. {
  106. /* save old rand method */
  107. if ((old_rand = RAND_get_rand_method()) == NULL)
  108. return 0;
  109. fake_rand.seed = old_rand->seed;
  110. fake_rand.cleanup = old_rand->cleanup;
  111. fake_rand.add = old_rand->add;
  112. fake_rand.status = old_rand->status;
  113. /* use own random function */
  114. fake_rand.bytes = fbytes;
  115. fake_rand.pseudorand = old_rand->bytes;
  116. /* set new RAND_METHOD */
  117. if (!RAND_set_rand_method(&fake_rand))
  118. return 0;
  119. return 1;
  120. }
  121. int restore_rand(void)
  122. {
  123. if (!RAND_set_rand_method(old_rand))
  124. return 0;
  125. else
  126. return 1;
  127. }
  128. static int fbytes_counter = 0;
  129. static const char *numbers[8] = {
  130. "651056770906015076056810763456358567190100156695615665659",
  131. "6140507067065001063065065565667405560006161556565665656654",
  132. "8763001015071075675010661307616710783570106710677817767166"
  133. "71676178726717",
  134. "7000000175690566466555057817571571075705015757757057795755"
  135. "55657156756655",
  136. "1275552191113212300012030439187146164646146646466749494799",
  137. "1542725565216523985789236956265265265235675811949404040041",
  138. "1456427555219115346513212300075341203043918714616464614664"
  139. "64667494947990",
  140. "1712787255652165239672857892369562652652652356758119494040"
  141. "40041670216363"
  142. };
  143. int fbytes(unsigned char *buf, int num)
  144. {
  145. int ret;
  146. BIGNUM *tmp = NULL;
  147. if (fbytes_counter >= 8)
  148. return 0;
  149. tmp = BN_new();
  150. if (!tmp)
  151. return 0;
  152. if (!BN_dec2bn(&tmp, numbers[fbytes_counter])) {
  153. BN_free(tmp);
  154. return 0;
  155. }
  156. fbytes_counter++;
  157. if (num != BN_num_bytes(tmp) || !BN_bn2bin(tmp, buf))
  158. ret = 0;
  159. else
  160. ret = 1;
  161. if (tmp)
  162. BN_free(tmp);
  163. return ret;
  164. }
  165. /* some tests from the X9.62 draft */
  166. int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
  167. {
  168. int ret = 0;
  169. const char message[] = "abc";
  170. unsigned char digest[20];
  171. unsigned int dgst_len = 0;
  172. EVP_MD_CTX md_ctx;
  173. EC_KEY *key = NULL;
  174. ECDSA_SIG *signature = NULL;
  175. BIGNUM *r = NULL, *s = NULL;
  176. EVP_MD_CTX_init(&md_ctx);
  177. /* get the message digest */
  178. EVP_DigestInit(&md_ctx, EVP_ecdsa());
  179. EVP_DigestUpdate(&md_ctx, (const void *)message, 3);
  180. EVP_DigestFinal(&md_ctx, digest, &dgst_len);
  181. BIO_printf(out, "testing %s: ", OBJ_nid2sn(nid));
  182. /* create the key */
  183. if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
  184. goto x962_int_err;
  185. if (!EC_KEY_generate_key(key))
  186. goto x962_int_err;
  187. BIO_printf(out, ".");
  188. (void)BIO_flush(out);
  189. /* create the signature */
  190. signature = ECDSA_do_sign(digest, 20, key);
  191. if (signature == NULL)
  192. goto x962_int_err;
  193. BIO_printf(out, ".");
  194. (void)BIO_flush(out);
  195. /* compare the created signature with the expected signature */
  196. if ((r = BN_new()) == NULL || (s = BN_new()) == NULL)
  197. goto x962_int_err;
  198. if (!BN_dec2bn(&r, r_in) || !BN_dec2bn(&s, s_in))
  199. goto x962_int_err;
  200. if (BN_cmp(signature->r, r) || BN_cmp(signature->s, s))
  201. goto x962_int_err;
  202. BIO_printf(out, ".");
  203. (void)BIO_flush(out);
  204. /* verify the signature */
  205. if (ECDSA_do_verify(digest, 20, signature, key) != 1)
  206. goto x962_int_err;
  207. BIO_printf(out, ".");
  208. (void)BIO_flush(out);
  209. BIO_printf(out, " ok\n");
  210. ret = 1;
  211. x962_int_err:
  212. if (!ret)
  213. BIO_printf(out, " failed\n");
  214. if (key)
  215. EC_KEY_free(key);
  216. if (signature)
  217. ECDSA_SIG_free(signature);
  218. if (r)
  219. BN_free(r);
  220. if (s)
  221. BN_free(s);
  222. EVP_MD_CTX_cleanup(&md_ctx);
  223. return ret;
  224. }
  225. int x9_62_tests(BIO *out)
  226. {
  227. int ret = 0;
  228. BIO_printf(out, "some tests from X9.62:\n");
  229. /* set own rand method */
  230. if (!change_rand())
  231. goto x962_err;
  232. if (!x9_62_test_internal(out, NID_X9_62_prime192v1,
  233. "3342403536405981729393488334694600415596881826869351677613",
  234. "5735822328888155254683894997897571951568553642892029982342"))
  235. goto x962_err;
  236. if (!x9_62_test_internal(out, NID_X9_62_prime239v1,
  237. "3086361431751678114926225473006680188549593787585317781474"
  238. "62058306432176",
  239. "3238135532097973577080787768312505059318910517550078427819"
  240. "78505179448783"))
  241. goto x962_err;
  242. # ifndef OPENSSL_NO_EC2M
  243. if (!x9_62_test_internal(out, NID_X9_62_c2tnb191v1,
  244. "87194383164871543355722284926904419997237591535066528048",
  245. "308992691965804947361541664549085895292153777025772063598"))
  246. goto x962_err;
  247. if (!x9_62_test_internal(out, NID_X9_62_c2tnb239v1,
  248. "2159633321041961198501834003903461262881815148684178964245"
  249. "5876922391552",
  250. "1970303740007316867383349976549972270528498040721988191026"
  251. "49413465737174"))
  252. goto x962_err;
  253. # endif
  254. ret = 1;
  255. x962_err:
  256. if (!restore_rand())
  257. ret = 0;
  258. return ret;
  259. }
  260. int test_builtin(BIO *out)
  261. {
  262. EC_builtin_curve *curves = NULL;
  263. size_t crv_len = 0, n = 0;
  264. EC_KEY *eckey = NULL, *wrong_eckey = NULL;
  265. EC_GROUP *group;
  266. ECDSA_SIG *ecdsa_sig = NULL;
  267. unsigned char digest[20], wrong_digest[20];
  268. unsigned char *signature = NULL;
  269. const unsigned char *sig_ptr;
  270. unsigned char *sig_ptr2;
  271. unsigned char *raw_buf = NULL;
  272. unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
  273. int nid, ret = 0;
  274. /* fill digest values with some random data */
  275. if (RAND_pseudo_bytes(digest, 20) <= 0 ||
  276. RAND_pseudo_bytes(wrong_digest, 20) <= 0) {
  277. BIO_printf(out, "ERROR: unable to get random data\n");
  278. goto builtin_err;
  279. }
  280. /*
  281. * create and verify a ecdsa signature with every availble curve (with )
  282. */
  283. BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() "
  284. "with some internal curves:\n");
  285. /* get a list of all internal curves */
  286. crv_len = EC_get_builtin_curves(NULL, 0);
  287. curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
  288. if (curves == NULL) {
  289. BIO_printf(out, "malloc error\n");
  290. goto builtin_err;
  291. }
  292. if (!EC_get_builtin_curves(curves, crv_len)) {
  293. BIO_printf(out, "unable to get internal curves\n");
  294. goto builtin_err;
  295. }
  296. /* now create and verify a signature for every curve */
  297. for (n = 0; n < crv_len; n++) {
  298. unsigned char dirt, offset;
  299. nid = curves[n].nid;
  300. if (nid == NID_ipsec4)
  301. continue;
  302. /* create new ecdsa key (== EC_KEY) */
  303. if ((eckey = EC_KEY_new()) == NULL)
  304. goto builtin_err;
  305. group = EC_GROUP_new_by_curve_name(nid);
  306. if (group == NULL)
  307. goto builtin_err;
  308. if (EC_KEY_set_group(eckey, group) == 0)
  309. goto builtin_err;
  310. EC_GROUP_free(group);
  311. degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
  312. if (degree < 160)
  313. /* drop the curve */
  314. {
  315. EC_KEY_free(eckey);
  316. eckey = NULL;
  317. continue;
  318. }
  319. BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
  320. /* create key */
  321. if (!EC_KEY_generate_key(eckey)) {
  322. BIO_printf(out, " failed\n");
  323. goto builtin_err;
  324. }
  325. /* create second key */
  326. if ((wrong_eckey = EC_KEY_new()) == NULL)
  327. goto builtin_err;
  328. group = EC_GROUP_new_by_curve_name(nid);
  329. if (group == NULL)
  330. goto builtin_err;
  331. if (EC_KEY_set_group(wrong_eckey, group) == 0)
  332. goto builtin_err;
  333. EC_GROUP_free(group);
  334. if (!EC_KEY_generate_key(wrong_eckey)) {
  335. BIO_printf(out, " failed\n");
  336. goto builtin_err;
  337. }
  338. BIO_printf(out, ".");
  339. (void)BIO_flush(out);
  340. /* check key */
  341. if (!EC_KEY_check_key(eckey)) {
  342. BIO_printf(out, " failed\n");
  343. goto builtin_err;
  344. }
  345. BIO_printf(out, ".");
  346. (void)BIO_flush(out);
  347. /* create signature */
  348. sig_len = ECDSA_size(eckey);
  349. if ((signature = OPENSSL_malloc(sig_len)) == NULL)
  350. goto builtin_err;
  351. if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey)) {
  352. BIO_printf(out, " failed\n");
  353. goto builtin_err;
  354. }
  355. BIO_printf(out, ".");
  356. (void)BIO_flush(out);
  357. /* verify signature */
  358. if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1) {
  359. BIO_printf(out, " failed\n");
  360. goto builtin_err;
  361. }
  362. BIO_printf(out, ".");
  363. (void)BIO_flush(out);
  364. /* verify signature with the wrong key */
  365. if (ECDSA_verify(0, digest, 20, signature, sig_len, wrong_eckey) == 1) {
  366. BIO_printf(out, " failed\n");
  367. goto builtin_err;
  368. }
  369. BIO_printf(out, ".");
  370. (void)BIO_flush(out);
  371. /* wrong digest */
  372. if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len, eckey) == 1) {
  373. BIO_printf(out, " failed\n");
  374. goto builtin_err;
  375. }
  376. BIO_printf(out, ".");
  377. (void)BIO_flush(out);
  378. /* wrong length */
  379. if (ECDSA_verify(0, digest, 20, signature, sig_len - 1, eckey) == 1) {
  380. BIO_printf(out, " failed\n");
  381. goto builtin_err;
  382. }
  383. BIO_printf(out, ".");
  384. (void)BIO_flush(out);
  385. /*
  386. * Modify a single byte of the signature: to ensure we don't garble
  387. * the ASN1 structure, we read the raw signature and modify a byte in
  388. * one of the bignums directly.
  389. */
  390. sig_ptr = signature;
  391. if ((ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)) == NULL) {
  392. BIO_printf(out, " failed\n");
  393. goto builtin_err;
  394. }
  395. /* Store the two BIGNUMs in raw_buf. */
  396. r_len = BN_num_bytes(ecdsa_sig->r);
  397. s_len = BN_num_bytes(ecdsa_sig->s);
  398. bn_len = (degree + 7) / 8;
  399. if ((r_len > bn_len) || (s_len > bn_len)) {
  400. BIO_printf(out, " failed\n");
  401. goto builtin_err;
  402. }
  403. buf_len = 2 * bn_len;
  404. if ((raw_buf = OPENSSL_malloc(buf_len)) == NULL)
  405. goto builtin_err;
  406. /* Pad the bignums with leading zeroes. */
  407. memset(raw_buf, 0, buf_len);
  408. BN_bn2bin(ecdsa_sig->r, raw_buf + bn_len - r_len);
  409. BN_bn2bin(ecdsa_sig->s, raw_buf + buf_len - s_len);
  410. /* Modify a single byte in the buffer. */
  411. offset = raw_buf[10] % buf_len;
  412. dirt = raw_buf[11] ? raw_buf[11] : 1;
  413. raw_buf[offset] ^= dirt;
  414. /* Now read the BIGNUMs back in from raw_buf. */
  415. if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
  416. (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
  417. goto builtin_err;
  418. sig_ptr2 = signature;
  419. sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
  420. if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1) {
  421. BIO_printf(out, " failed\n");
  422. goto builtin_err;
  423. }
  424. /*
  425. * Sanity check: undo the modification and verify signature.
  426. */
  427. raw_buf[offset] ^= dirt;
  428. if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
  429. (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
  430. goto builtin_err;
  431. sig_ptr2 = signature;
  432. sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
  433. if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1) {
  434. BIO_printf(out, " failed\n");
  435. goto builtin_err;
  436. }
  437. BIO_printf(out, ".");
  438. (void)BIO_flush(out);
  439. BIO_printf(out, " ok\n");
  440. /* cleanup */
  441. /* clean bogus errors */
  442. ERR_clear_error();
  443. OPENSSL_free(signature);
  444. signature = NULL;
  445. EC_KEY_free(eckey);
  446. eckey = NULL;
  447. EC_KEY_free(wrong_eckey);
  448. wrong_eckey = NULL;
  449. ECDSA_SIG_free(ecdsa_sig);
  450. ecdsa_sig = NULL;
  451. OPENSSL_free(raw_buf);
  452. raw_buf = NULL;
  453. }
  454. ret = 1;
  455. builtin_err:
  456. if (eckey)
  457. EC_KEY_free(eckey);
  458. if (wrong_eckey)
  459. EC_KEY_free(wrong_eckey);
  460. if (ecdsa_sig)
  461. ECDSA_SIG_free(ecdsa_sig);
  462. if (signature)
  463. OPENSSL_free(signature);
  464. if (raw_buf)
  465. OPENSSL_free(raw_buf);
  466. if (curves)
  467. OPENSSL_free(curves);
  468. return ret;
  469. }
  470. int main(void)
  471. {
  472. int ret = 1;
  473. BIO *out;
  474. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  475. /* enable memory leak checking unless explicitly disabled */
  476. if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) &&
  477. (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) {
  478. CRYPTO_malloc_debug_init();
  479. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  480. } else {
  481. /* OPENSSL_DEBUG_MEMORY=off */
  482. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  483. }
  484. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  485. ERR_load_crypto_strings();
  486. /* initialize the prng */
  487. RAND_seed(rnd_seed, sizeof(rnd_seed));
  488. /* the tests */
  489. if (!x9_62_tests(out))
  490. goto err;
  491. if (!test_builtin(out))
  492. goto err;
  493. ret = 0;
  494. err:
  495. if (ret)
  496. BIO_printf(out, "\nECDSA test failed\n");
  497. else
  498. BIO_printf(out, "\nECDSA test passed\n");
  499. if (ret)
  500. ERR_print_errors(out);
  501. CRYPTO_cleanup_all_ex_data();
  502. ERR_remove_thread_state(NULL);
  503. ERR_free_strings();
  504. CRYPTO_mem_leaks(out);
  505. if (out != NULL)
  506. BIO_free(out);
  507. return ret;
  508. }
  509. #endif