ecdsatest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
  14. # include "testutil.h"
  15. #ifndef OPENSSL_NO_EC
  16. # include <openssl/crypto.h>
  17. # include <openssl/bio.h>
  18. # include <openssl/evp.h>
  19. # include <openssl/bn.h>
  20. # include <openssl/ec.h>
  21. # ifndef OPENSSL_NO_ENGINE
  22. # include <openssl/engine.h>
  23. # endif
  24. # include <openssl/err.h>
  25. # include <openssl/rand.h>
  26. /* functions to change the RAND_METHOD */
  27. static int fbytes(unsigned char *buf, int num);
  28. static RAND_METHOD fake_rand;
  29. static const RAND_METHOD *old_rand;
  30. static int change_rand(void)
  31. {
  32. /* save old rand method */
  33. if (!TEST_ptr(old_rand = RAND_get_rand_method()))
  34. return 0;
  35. fake_rand = *old_rand;
  36. /* use own random function */
  37. fake_rand.bytes = fbytes;
  38. /* set new RAND_METHOD */
  39. if (!TEST_true(RAND_set_rand_method(&fake_rand)))
  40. return 0;
  41. return 1;
  42. }
  43. static int restore_rand(void)
  44. {
  45. if (!TEST_true(RAND_set_rand_method(old_rand)))
  46. return 0;
  47. return 1;
  48. }
  49. static int fbytes_counter = 0, use_fake = 0;
  50. static const char *numbers[8] = {
  51. "651056770906015076056810763456358567190100156695615665659",
  52. "6140507067065001063065065565667405560006161556565665656654",
  53. "8763001015071075675010661307616710783570106710677817767166"
  54. "71676178726717",
  55. "7000000175690566466555057817571571075705015757757057795755"
  56. "55657156756655",
  57. "1275552191113212300012030439187146164646146646466749494799",
  58. "1542725565216523985789236956265265265235675811949404040041",
  59. "1456427555219115346513212300075341203043918714616464614664"
  60. "64667494947990",
  61. "1712787255652165239672857892369562652652652356758119494040"
  62. "40041670216363"
  63. };
  64. static int fbytes(unsigned char *buf, int num)
  65. {
  66. int ret = 0;
  67. BIGNUM *tmp = NULL;
  68. if (use_fake == 0)
  69. return old_rand->bytes(buf, num);
  70. use_fake = 0;
  71. if (fbytes_counter >= 8)
  72. return 0;
  73. if (!TEST_ptr(tmp = BN_new()))
  74. return 0;
  75. if (!TEST_true(BN_dec2bn(&tmp, numbers[fbytes_counter]))) {
  76. BN_free(tmp);
  77. return 0;
  78. }
  79. fbytes_counter++;
  80. if (TEST_int_eq(BN_num_bytes(tmp), num)
  81. && TEST_true(BN_bn2bin(tmp, buf)))
  82. ret = 1;
  83. BN_free(tmp);
  84. return ret;
  85. }
  86. /* some tests from the X9.62 draft */
  87. static int x9_62_test_internal(int nid, const char *r_in, const char *s_in)
  88. {
  89. int ret = 0;
  90. const char message[] = "abc";
  91. unsigned char digest[20];
  92. unsigned int dgst_len = 0;
  93. EVP_MD_CTX *md_ctx;
  94. EC_KEY *key = NULL;
  95. ECDSA_SIG *signature = NULL;
  96. BIGNUM *r = NULL, *s = NULL;
  97. BIGNUM *kinv = NULL, *rp = NULL;
  98. const BIGNUM *sig_r, *sig_s;
  99. if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
  100. goto x962_int_err;
  101. /* get the message digest */
  102. if (!TEST_true(EVP_DigestInit(md_ctx, EVP_sha1()))
  103. || !TEST_true(EVP_DigestUpdate(md_ctx, (const void *)message, 3))
  104. || !TEST_true(EVP_DigestFinal(md_ctx, digest, &dgst_len)))
  105. goto x962_int_err;
  106. TEST_info("testing %s", OBJ_nid2sn(nid));
  107. /* create the key */
  108. if (!TEST_ptr(key = EC_KEY_new_by_curve_name(nid)))
  109. goto x962_int_err;
  110. use_fake = 1;
  111. if (!TEST_true(EC_KEY_generate_key(key)))
  112. goto x962_int_err;
  113. /* create the signature */
  114. use_fake = 1;
  115. /* Use ECDSA_sign_setup to avoid use of ECDSA nonces */
  116. if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp)))
  117. goto x962_int_err;
  118. if (!TEST_ptr(signature = ECDSA_do_sign_ex(digest, 20, kinv, rp, key)))
  119. goto x962_int_err;
  120. /* compare the created signature with the expected signature */
  121. if (!TEST_ptr(r = BN_new()) || !TEST_ptr(s = BN_new()))
  122. goto x962_int_err;
  123. if (!TEST_true(BN_dec2bn(&r, r_in)) || !TEST_true(BN_dec2bn(&s, s_in)))
  124. goto x962_int_err;
  125. ECDSA_SIG_get0(signature, &sig_r, &sig_s);
  126. if (!TEST_BN_eq(sig_r, r)
  127. || !TEST_BN_eq(sig_s, s))
  128. goto x962_int_err;
  129. /* verify the signature */
  130. if (!TEST_int_eq(ECDSA_do_verify(digest, 20, signature, key), 1))
  131. goto x962_int_err;
  132. ret = 1;
  133. x962_int_err:
  134. EC_KEY_free(key);
  135. ECDSA_SIG_free(signature);
  136. BN_free(r);
  137. BN_free(s);
  138. EVP_MD_CTX_free(md_ctx);
  139. BN_clear_free(kinv);
  140. BN_clear_free(rp);
  141. return ret;
  142. }
  143. static int x9_62_tests(void)
  144. {
  145. int ret = 0;
  146. /* set own rand method */
  147. if (!change_rand())
  148. goto x962_err;
  149. if (!TEST_true(x9_62_test_internal(NID_X9_62_prime192v1,
  150. "3342403536405981729393488334694600415596881826869351677613",
  151. "5735822328888155254683894997897571951568553642892029982342")))
  152. goto x962_err;
  153. if (!TEST_true(x9_62_test_internal(NID_X9_62_prime239v1,
  154. "3086361431751678114926225473006680188549593787585317781474"
  155. "62058306432176",
  156. "3238135532097973577080787768312505059318910517550078427819"
  157. "78505179448783")))
  158. goto x962_err;
  159. # ifndef OPENSSL_NO_EC2M
  160. if (!TEST_true(x9_62_test_internal(NID_X9_62_c2tnb191v1,
  161. "87194383164871543355722284926904419997237591535066528048",
  162. "308992691965804947361541664549085895292153777025772063598")))
  163. goto x962_err;
  164. if (!TEST_true(x9_62_test_internal(NID_X9_62_c2tnb239v1,
  165. "2159633321041961198501834003903461262881815148684178964245"
  166. "5876922391552",
  167. "1970303740007316867383349976549972270528498040721988191026"
  168. "49413465737174")))
  169. goto x962_err;
  170. # endif
  171. ret = 1;
  172. x962_err:
  173. if (!TEST_true(restore_rand()))
  174. ret = 0;
  175. return ret;
  176. }
  177. static int test_builtin(void)
  178. {
  179. EC_builtin_curve *curves = NULL;
  180. size_t crv_len = 0, n = 0;
  181. EC_KEY *eckey = NULL, *wrong_eckey = NULL;
  182. EC_GROUP *group;
  183. ECDSA_SIG *ecdsa_sig = NULL, *modified_sig = NULL;
  184. unsigned char digest[20], wrong_digest[20];
  185. unsigned char *signature = NULL;
  186. const unsigned char *sig_ptr;
  187. unsigned char *sig_ptr2;
  188. unsigned char *raw_buf = NULL;
  189. const BIGNUM *sig_r, *sig_s;
  190. BIGNUM *modified_r = NULL, *modified_s = NULL;
  191. BIGNUM *unmodified_r = NULL, *unmodified_s = NULL;
  192. unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
  193. int nid, ret = 0;
  194. /* fill digest values with some random data */
  195. if (!TEST_true(RAND_bytes(digest, 20))
  196. || !TEST_true(RAND_bytes(wrong_digest, 20)))
  197. goto builtin_err;
  198. /* create and verify a ecdsa signature with every available curve */
  199. /* get a list of all internal curves */
  200. crv_len = EC_get_builtin_curves(NULL, 0);
  201. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  202. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  203. goto builtin_err;
  204. /* now create and verify a signature for every curve */
  205. for (n = 0; n < crv_len; n++) {
  206. unsigned char dirt, offset;
  207. nid = curves[n].nid;
  208. if (nid == NID_ipsec4)
  209. continue;
  210. /* create new ecdsa key (== EC_KEY) */
  211. if (!TEST_ptr(eckey = EC_KEY_new())
  212. || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
  213. || !TEST_true(EC_KEY_set_group(eckey, group)))
  214. goto builtin_err;
  215. EC_GROUP_free(group);
  216. degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
  217. if (degree < 160) {
  218. /* drop the curve */
  219. EC_KEY_free(eckey);
  220. eckey = NULL;
  221. continue;
  222. }
  223. TEST_info("testing %s", OBJ_nid2sn(nid));
  224. /* create key */
  225. if (!TEST_true(EC_KEY_generate_key(eckey)))
  226. goto builtin_err;
  227. /* create second key */
  228. if (!TEST_ptr(wrong_eckey = EC_KEY_new())
  229. || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
  230. || !TEST_true(EC_KEY_set_group(wrong_eckey, group)))
  231. goto builtin_err;
  232. EC_GROUP_free(group);
  233. if (!TEST_true(EC_KEY_generate_key(wrong_eckey)))
  234. goto builtin_err;
  235. /* check key */
  236. if (!TEST_true(EC_KEY_check_key(eckey)))
  237. goto builtin_err;
  238. /* create signature */
  239. sig_len = ECDSA_size(eckey);
  240. if (!TEST_ptr(signature = OPENSSL_malloc(sig_len))
  241. || !TEST_true(ECDSA_sign(0, digest, 20, signature, &sig_len,
  242. eckey)))
  243. goto builtin_err;
  244. /* verify signature */
  245. if (!TEST_int_eq(ECDSA_verify(0, digest, 20, signature, sig_len,
  246. eckey), 1))
  247. goto builtin_err;
  248. /* verify signature with the wrong key */
  249. if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature, sig_len,
  250. wrong_eckey), 1))
  251. goto builtin_err;
  252. /* wrong digest */
  253. if (!TEST_int_ne(ECDSA_verify(0, wrong_digest, 20, signature,
  254. sig_len, eckey), 1))
  255. goto builtin_err;
  256. /* wrong length */
  257. if (!TEST_int_ne(ECDSA_verify(0, digest, 20, signature,
  258. sig_len - 1, eckey), 1))
  259. goto builtin_err;
  260. /*
  261. * Modify a single byte of the signature: to ensure we don't garble
  262. * the ASN1 structure, we read the raw signature and modify a byte in
  263. * one of the bignums directly.
  264. */
  265. sig_ptr = signature;
  266. if (!TEST_ptr(ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)))
  267. goto builtin_err;
  268. ECDSA_SIG_get0(ecdsa_sig, &sig_r, &sig_s);
  269. /* Store the two BIGNUMs in raw_buf. */
  270. r_len = BN_num_bytes(sig_r);
  271. s_len = BN_num_bytes(sig_s);
  272. bn_len = (degree + 7) / 8;
  273. if (!TEST_false(r_len > bn_len)
  274. || !TEST_false(s_len > bn_len))
  275. goto builtin_err;
  276. buf_len = 2 * bn_len;
  277. if (!TEST_ptr(raw_buf = OPENSSL_zalloc(buf_len)))
  278. goto builtin_err;
  279. BN_bn2bin(sig_r, raw_buf + bn_len - r_len);
  280. BN_bn2bin(sig_s, raw_buf + buf_len - s_len);
  281. /* Modify a single byte in the buffer. */
  282. offset = raw_buf[10] % buf_len;
  283. dirt = raw_buf[11] ? raw_buf[11] : 1;
  284. raw_buf[offset] ^= dirt;
  285. /* Now read the BIGNUMs back in from raw_buf. */
  286. if (!TEST_ptr(modified_sig = ECDSA_SIG_new()))
  287. goto builtin_err;
  288. if (!TEST_ptr(modified_r = BN_bin2bn(raw_buf, bn_len, NULL))
  289. || !TEST_ptr(modified_s = BN_bin2bn(raw_buf + bn_len,
  290. bn_len, NULL))
  291. || !TEST_true(ECDSA_SIG_set0(modified_sig,
  292. modified_r, modified_s))) {
  293. BN_free(modified_r);
  294. BN_free(modified_s);
  295. goto builtin_err;
  296. }
  297. sig_ptr2 = signature;
  298. sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);
  299. if (!TEST_false(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))
  300. goto builtin_err;
  301. /* Sanity check: undo the modification and verify signature. */
  302. raw_buf[offset] ^= dirt;
  303. if (!TEST_ptr(unmodified_r = BN_bin2bn(raw_buf, bn_len, NULL))
  304. || !TEST_ptr(unmodified_s = BN_bin2bn(raw_buf + bn_len,
  305. bn_len, NULL))
  306. || !TEST_true(ECDSA_SIG_set0(modified_sig, unmodified_r,
  307. unmodified_s))) {
  308. BN_free(unmodified_r);
  309. BN_free(unmodified_s);
  310. goto builtin_err;
  311. }
  312. sig_ptr2 = signature;
  313. sig_len = i2d_ECDSA_SIG(modified_sig, &sig_ptr2);
  314. if (!TEST_true(ECDSA_verify(0, digest, 20, signature, sig_len, eckey)))
  315. goto builtin_err;
  316. /* cleanup */
  317. ERR_clear_error();
  318. OPENSSL_free(signature);
  319. signature = NULL;
  320. EC_KEY_free(eckey);
  321. eckey = NULL;
  322. EC_KEY_free(wrong_eckey);
  323. wrong_eckey = NULL;
  324. ECDSA_SIG_free(ecdsa_sig);
  325. ecdsa_sig = NULL;
  326. ECDSA_SIG_free(modified_sig);
  327. modified_sig = NULL;
  328. OPENSSL_free(raw_buf);
  329. raw_buf = NULL;
  330. }
  331. ret = 1;
  332. builtin_err:
  333. EC_KEY_free(eckey);
  334. EC_KEY_free(wrong_eckey);
  335. ECDSA_SIG_free(ecdsa_sig);
  336. ECDSA_SIG_free(modified_sig);
  337. OPENSSL_free(signature);
  338. OPENSSL_free(raw_buf);
  339. OPENSSL_free(curves);
  340. return ret;
  341. }
  342. #endif
  343. int setup_tests(void)
  344. {
  345. #ifdef OPENSSL_NO_EC
  346. TEST_note("Elliptic curves are disabled.");
  347. #else
  348. ADD_TEST(x9_62_tests);
  349. ADD_TEST(test_builtin);
  350. #endif
  351. return 1;
  352. }