ecdsatest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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, use_fake = 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. int fbytes(unsigned char *buf, int num)
  143. {
  144. int ret;
  145. BIGNUM *tmp = NULL;
  146. if (use_fake == 0)
  147. return old_rand->bytes(buf, num);
  148. use_fake = 0;
  149. if (fbytes_counter >= 8)
  150. return 0;
  151. tmp = BN_new();
  152. if (!tmp)
  153. return 0;
  154. if (!BN_dec2bn(&tmp, numbers[fbytes_counter]))
  155. {
  156. BN_free(tmp);
  157. return 0;
  158. }
  159. fbytes_counter ++;
  160. if (num != BN_num_bytes(tmp) || !BN_bn2bin(tmp, buf))
  161. ret = 0;
  162. else
  163. ret = 1;
  164. if (tmp)
  165. BN_free(tmp);
  166. return ret;
  167. }
  168. /* some tests from the X9.62 draft */
  169. int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
  170. {
  171. int ret = 0;
  172. const char message[] = "abc";
  173. unsigned char digest[20];
  174. unsigned int dgst_len = 0;
  175. EVP_MD_CTX md_ctx;
  176. EC_KEY *key = NULL;
  177. ECDSA_SIG *signature = NULL;
  178. BIGNUM *r = NULL, *s = NULL;
  179. EVP_MD_CTX_init(&md_ctx);
  180. /* get the message digest */
  181. EVP_DigestInit(&md_ctx, EVP_ecdsa());
  182. EVP_DigestUpdate(&md_ctx, (const void*)message, 3);
  183. EVP_DigestFinal(&md_ctx, digest, &dgst_len);
  184. BIO_printf(out, "testing %s: ", OBJ_nid2sn(nid));
  185. /* create the key */
  186. if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
  187. goto x962_int_err;
  188. use_fake = 1;
  189. if (!EC_KEY_generate_key(key))
  190. goto x962_int_err;
  191. BIO_printf(out, ".");
  192. (void)BIO_flush(out);
  193. /* create the signature */
  194. use_fake = 1;
  195. signature = ECDSA_do_sign(digest, 20, key);
  196. if (signature == NULL)
  197. goto x962_int_err;
  198. BIO_printf(out, ".");
  199. (void)BIO_flush(out);
  200. /* compare the created signature with the expected signature */
  201. if ((r = BN_new()) == NULL || (s = BN_new()) == NULL)
  202. goto x962_int_err;
  203. if (!BN_dec2bn(&r, r_in) ||
  204. !BN_dec2bn(&s, s_in))
  205. goto x962_int_err;
  206. if (BN_cmp(signature->r ,r) || BN_cmp(signature->s, s))
  207. goto x962_int_err;
  208. BIO_printf(out, ".");
  209. (void)BIO_flush(out);
  210. /* verify the signature */
  211. if (ECDSA_do_verify(digest, 20, signature, key) != 1)
  212. goto x962_int_err;
  213. BIO_printf(out, ".");
  214. (void)BIO_flush(out);
  215. BIO_printf(out, " ok\n");
  216. ret = 1;
  217. x962_int_err:
  218. if (!ret)
  219. BIO_printf(out, " failed\n");
  220. if (key)
  221. EC_KEY_free(key);
  222. if (signature)
  223. ECDSA_SIG_free(signature);
  224. if (r)
  225. BN_free(r);
  226. if (s)
  227. BN_free(s);
  228. EVP_MD_CTX_cleanup(&md_ctx);
  229. return ret;
  230. }
  231. int x9_62_tests(BIO *out)
  232. {
  233. int ret = 0;
  234. BIO_printf(out, "some tests from X9.62:\n");
  235. /* set own rand method */
  236. if (!change_rand())
  237. goto x962_err;
  238. if (!x9_62_test_internal(out, NID_X9_62_prime192v1,
  239. "3342403536405981729393488334694600415596881826869351677613",
  240. "5735822328888155254683894997897571951568553642892029982342"))
  241. goto x962_err;
  242. if (!x9_62_test_internal(out, NID_X9_62_prime239v1,
  243. "3086361431751678114926225473006680188549593787585317781474"
  244. "62058306432176",
  245. "3238135532097973577080787768312505059318910517550078427819"
  246. "78505179448783"))
  247. goto x962_err;
  248. #ifndef OPENSSL_NO_EC2M
  249. if (!x9_62_test_internal(out, NID_X9_62_c2tnb191v1,
  250. "87194383164871543355722284926904419997237591535066528048",
  251. "308992691965804947361541664549085895292153777025772063598"))
  252. goto x962_err;
  253. if (!x9_62_test_internal(out, NID_X9_62_c2tnb239v1,
  254. "2159633321041961198501834003903461262881815148684178964245"
  255. "5876922391552",
  256. "1970303740007316867383349976549972270528498040721988191026"
  257. "49413465737174"))
  258. goto x962_err;
  259. #endif
  260. ret = 1;
  261. x962_err:
  262. if (!restore_rand())
  263. ret = 0;
  264. return ret;
  265. }
  266. int test_builtin(BIO *out)
  267. {
  268. EC_builtin_curve *curves = NULL;
  269. size_t crv_len = 0, n = 0;
  270. EC_KEY *eckey = NULL, *wrong_eckey = NULL;
  271. EC_GROUP *group;
  272. unsigned char digest[20], wrong_digest[20];
  273. unsigned char *signature = NULL;
  274. unsigned int sig_len;
  275. int nid, ret = 0;
  276. /* fill digest values with some random data */
  277. if (!RAND_pseudo_bytes(digest, 20) ||
  278. !RAND_pseudo_bytes(wrong_digest, 20))
  279. {
  280. BIO_printf(out, "ERROR: unable to get random data\n");
  281. goto builtin_err;
  282. }
  283. /* create and verify a ecdsa signature with every availble curve
  284. * (with ) */
  285. BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() "
  286. "with some internal curves:\n");
  287. /* get a list of all internal curves */
  288. crv_len = EC_get_builtin_curves(NULL, 0);
  289. curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
  290. if (curves == NULL)
  291. {
  292. BIO_printf(out, "malloc error\n");
  293. goto builtin_err;
  294. }
  295. if (!EC_get_builtin_curves(curves, crv_len))
  296. {
  297. BIO_printf(out, "unable to get internal curves\n");
  298. goto builtin_err;
  299. }
  300. /* now create and verify a signature for every curve */
  301. for (n = 0; n < crv_len; n++)
  302. {
  303. unsigned char dirt, offset;
  304. nid = curves[n].nid;
  305. if (nid == NID_ipsec4)
  306. continue;
  307. /* create new ecdsa key (== EC_KEY) */
  308. if ((eckey = EC_KEY_new()) == NULL)
  309. goto builtin_err;
  310. group = EC_GROUP_new_by_curve_name(nid);
  311. if (group == NULL)
  312. goto builtin_err;
  313. if (EC_KEY_set_group(eckey, group) == 0)
  314. goto builtin_err;
  315. EC_GROUP_free(group);
  316. if (EC_GROUP_get_degree(EC_KEY_get0_group(eckey)) < 160)
  317. /* drop the curve */
  318. {
  319. EC_KEY_free(eckey);
  320. eckey = NULL;
  321. continue;
  322. }
  323. BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
  324. /* create key */
  325. if (!EC_KEY_generate_key(eckey))
  326. {
  327. BIO_printf(out, " failed\n");
  328. goto builtin_err;
  329. }
  330. /* create second key */
  331. if ((wrong_eckey = EC_KEY_new()) == NULL)
  332. goto builtin_err;
  333. group = EC_GROUP_new_by_curve_name(nid);
  334. if (group == NULL)
  335. goto builtin_err;
  336. if (EC_KEY_set_group(wrong_eckey, group) == 0)
  337. goto builtin_err;
  338. EC_GROUP_free(group);
  339. if (!EC_KEY_generate_key(wrong_eckey))
  340. {
  341. BIO_printf(out, " failed\n");
  342. goto builtin_err;
  343. }
  344. BIO_printf(out, ".");
  345. (void)BIO_flush(out);
  346. /* check key */
  347. if (!EC_KEY_check_key(eckey))
  348. {
  349. BIO_printf(out, " failed\n");
  350. goto builtin_err;
  351. }
  352. BIO_printf(out, ".");
  353. (void)BIO_flush(out);
  354. /* create signature */
  355. sig_len = ECDSA_size(eckey);
  356. if ((signature = OPENSSL_malloc(sig_len)) == NULL)
  357. goto builtin_err;
  358. if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey))
  359. {
  360. BIO_printf(out, " failed\n");
  361. goto builtin_err;
  362. }
  363. BIO_printf(out, ".");
  364. (void)BIO_flush(out);
  365. /* verify signature */
  366. if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
  367. {
  368. BIO_printf(out, " failed\n");
  369. goto builtin_err;
  370. }
  371. BIO_printf(out, ".");
  372. (void)BIO_flush(out);
  373. /* verify signature with the wrong key */
  374. if (ECDSA_verify(0, digest, 20, signature, sig_len,
  375. wrong_eckey) == 1)
  376. {
  377. BIO_printf(out, " failed\n");
  378. goto builtin_err;
  379. }
  380. BIO_printf(out, ".");
  381. (void)BIO_flush(out);
  382. /* wrong digest */
  383. if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len,
  384. eckey) == 1)
  385. {
  386. BIO_printf(out, " failed\n");
  387. goto builtin_err;
  388. }
  389. BIO_printf(out, ".");
  390. (void)BIO_flush(out);
  391. /* modify a single byte of the signature */
  392. offset = signature[10] % sig_len;
  393. dirt = signature[11];
  394. signature[offset] ^= dirt ? dirt : 1;
  395. if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1)
  396. {
  397. BIO_printf(out, " failed\n");
  398. goto builtin_err;
  399. }
  400. BIO_printf(out, ".");
  401. (void)BIO_flush(out);
  402. BIO_printf(out, " ok\n");
  403. /* cleanup */
  404. OPENSSL_free(signature);
  405. signature = NULL;
  406. EC_KEY_free(eckey);
  407. eckey = NULL;
  408. EC_KEY_free(wrong_eckey);
  409. wrong_eckey = NULL;
  410. }
  411. ret = 1;
  412. builtin_err:
  413. if (eckey)
  414. EC_KEY_free(eckey);
  415. if (wrong_eckey)
  416. EC_KEY_free(wrong_eckey);
  417. if (signature)
  418. OPENSSL_free(signature);
  419. if (curves)
  420. OPENSSL_free(curves);
  421. return ret;
  422. }
  423. int main(void)
  424. {
  425. int ret = 1;
  426. BIO *out;
  427. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  428. /* enable memory leak checking unless explicitly disabled */
  429. if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) &&
  430. (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
  431. {
  432. CRYPTO_malloc_debug_init();
  433. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  434. }
  435. else
  436. {
  437. /* OPENSSL_DEBUG_MEMORY=off */
  438. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  439. }
  440. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  441. ERR_load_crypto_strings();
  442. /* initialize the prng */
  443. RAND_seed(rnd_seed, sizeof(rnd_seed));
  444. /* the tests */
  445. if (!x9_62_tests(out)) goto err;
  446. if (!test_builtin(out)) goto err;
  447. ret = 0;
  448. err:
  449. if (ret)
  450. BIO_printf(out, "\nECDSA test failed\n");
  451. else
  452. BIO_printf(out, "\nECDSA test passed\n");
  453. if (ret)
  454. ERR_print_errors(out);
  455. CRYPTO_cleanup_all_ex_data();
  456. ERR_remove_thread_state(NULL);
  457. ERR_free_strings();
  458. CRYPTO_mem_leaks(out);
  459. if (out != NULL)
  460. BIO_free(out);
  461. return ret;
  462. }
  463. #endif