ecdsatest.c 13 KB

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