ecdsatest.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (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. /*
  11. * Low level APIs are deprecated for public use, but still ok for internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_EC is defined */
  15. #include "testutil.h"
  16. #ifndef OPENSSL_NO_EC
  17. # include <openssl/evp.h>
  18. # include <openssl/bn.h>
  19. # include <openssl/ec.h>
  20. # include <openssl/rand.h>
  21. # include "internal/nelem.h"
  22. # include "ecdsatest.h"
  23. /* functions to change the RAND_METHOD */
  24. static int fbytes(unsigned char *buf, int num);
  25. static RAND_METHOD fake_rand;
  26. static const RAND_METHOD *old_rand;
  27. static int use_fake = 0;
  28. static const char *numbers[2];
  29. static size_t crv_len = 0;
  30. static EC_builtin_curve *curves = NULL;
  31. static int change_rand(void)
  32. {
  33. /* save old rand method */
  34. if (!TEST_ptr(old_rand = RAND_get_rand_method()))
  35. return 0;
  36. fake_rand = *old_rand;
  37. /* use own random function */
  38. fake_rand.bytes = fbytes;
  39. /* set new RAND_METHOD */
  40. if (!TEST_true(RAND_set_rand_method(&fake_rand)))
  41. return 0;
  42. return 1;
  43. }
  44. static int restore_rand(void)
  45. {
  46. if (!TEST_true(RAND_set_rand_method(old_rand)))
  47. return 0;
  48. return 1;
  49. }
  50. static int fbytes(unsigned char *buf, int num)
  51. {
  52. int ret = 0;
  53. static int fbytes_counter = 0;
  54. BIGNUM *tmp = NULL;
  55. if (use_fake == 0)
  56. return old_rand->bytes(buf, num);
  57. use_fake = 0;
  58. if (!TEST_ptr(tmp = BN_new())
  59. || !TEST_int_lt(fbytes_counter, OSSL_NELEM(numbers))
  60. || !TEST_true(BN_hex2bn(&tmp, numbers[fbytes_counter]))
  61. /* tmp might need leading zeros so pad it out */
  62. || !TEST_int_le(BN_num_bytes(tmp), num)
  63. || !TEST_true(BN_bn2binpad(tmp, buf, num)))
  64. goto err;
  65. fbytes_counter = (fbytes_counter + 1) % OSSL_NELEM(numbers);
  66. ret = 1;
  67. err:
  68. BN_free(tmp);
  69. return ret;
  70. }
  71. /*-
  72. * This function hijacks the RNG to feed it the chosen ECDSA key and nonce.
  73. * The ECDSA KATs are from:
  74. * - the X9.62 draft (4)
  75. * - NIST CAVP (720)
  76. *
  77. * It uses the low-level ECDSA_sign_setup instead of EVP to control the RNG.
  78. * NB: This is not how applications should use ECDSA; this is only for testing.
  79. *
  80. * Tests the library can successfully:
  81. * - generate public keys that matches those KATs
  82. * - create ECDSA signatures that match those KATs
  83. * - accept those signatures as valid
  84. */
  85. static int x9_62_tests(int n)
  86. {
  87. int nid, md_nid, ret = 0;
  88. const char *r_in = NULL, *s_in = NULL, *tbs = NULL;
  89. unsigned char *pbuf = NULL, *qbuf = NULL, *message = NULL;
  90. unsigned char digest[EVP_MAX_MD_SIZE];
  91. unsigned int dgst_len = 0;
  92. long q_len, msg_len = 0;
  93. size_t p_len;
  94. EVP_MD_CTX *mctx = NULL;
  95. EC_KEY *key = NULL;
  96. ECDSA_SIG *signature = NULL;
  97. BIGNUM *r = NULL, *s = NULL;
  98. BIGNUM *kinv = NULL, *rp = NULL;
  99. const BIGNUM *sig_r = NULL, *sig_s = NULL;
  100. nid = ecdsa_cavs_kats[n].nid;
  101. md_nid = ecdsa_cavs_kats[n].md_nid;
  102. r_in = ecdsa_cavs_kats[n].r;
  103. s_in = ecdsa_cavs_kats[n].s;
  104. tbs = ecdsa_cavs_kats[n].msg;
  105. numbers[0] = ecdsa_cavs_kats[n].d;
  106. numbers[1] = ecdsa_cavs_kats[n].k;
  107. TEST_info("ECDSA KATs for curve %s", OBJ_nid2sn(nid));
  108. #ifdef FIPS_MODULE
  109. if (EC_curve_nid2nist(nid) == NULL)
  110. return TEST_skip("skip non approved curves");
  111. #endif /* FIPS_MODULE */
  112. if (!TEST_ptr(mctx = EVP_MD_CTX_new())
  113. /* get the message digest */
  114. || !TEST_ptr(message = OPENSSL_hexstr2buf(tbs, &msg_len))
  115. || !TEST_true(EVP_DigestInit_ex(mctx, EVP_get_digestbynid(md_nid), NULL))
  116. || !TEST_true(EVP_DigestUpdate(mctx, message, msg_len))
  117. || !TEST_true(EVP_DigestFinal_ex(mctx, digest, &dgst_len))
  118. /* create the key */
  119. || !TEST_ptr(key = EC_KEY_new_by_curve_name(nid))
  120. /* load KAT variables */
  121. || !TEST_ptr(r = BN_new())
  122. || !TEST_ptr(s = BN_new())
  123. || !TEST_true(BN_hex2bn(&r, r_in))
  124. || !TEST_true(BN_hex2bn(&s, s_in))
  125. /* swap the RNG source */
  126. || !TEST_true(change_rand()))
  127. goto err;
  128. /* public key must match KAT */
  129. use_fake = 1;
  130. if (!TEST_true(EC_KEY_generate_key(key))
  131. || !TEST_true(p_len = EC_KEY_key2buf(key, POINT_CONVERSION_UNCOMPRESSED,
  132. &pbuf, NULL))
  133. || !TEST_ptr(qbuf = OPENSSL_hexstr2buf(ecdsa_cavs_kats[n].Q, &q_len))
  134. || !TEST_int_eq(q_len, p_len)
  135. || !TEST_mem_eq(qbuf, q_len, pbuf, p_len))
  136. goto err;
  137. /* create the signature via ECDSA_sign_setup to avoid use of ECDSA nonces */
  138. use_fake = 1;
  139. if (!TEST_true(ECDSA_sign_setup(key, NULL, &kinv, &rp))
  140. || !TEST_ptr(signature = ECDSA_do_sign_ex(digest, dgst_len,
  141. kinv, rp, key))
  142. /* verify the signature */
  143. || !TEST_int_eq(ECDSA_do_verify(digest, dgst_len, signature, key), 1))
  144. goto err;
  145. /* compare the created signature with the expected signature */
  146. ECDSA_SIG_get0(signature, &sig_r, &sig_s);
  147. if (!TEST_BN_eq(sig_r, r)
  148. || !TEST_BN_eq(sig_s, s))
  149. goto err;
  150. ret = 1;
  151. err:
  152. /* restore the RNG source */
  153. if (!TEST_true(restore_rand()))
  154. ret = 0;
  155. OPENSSL_free(message);
  156. OPENSSL_free(pbuf);
  157. OPENSSL_free(qbuf);
  158. EC_KEY_free(key);
  159. ECDSA_SIG_free(signature);
  160. BN_free(r);
  161. BN_free(s);
  162. EVP_MD_CTX_free(mctx);
  163. BN_clear_free(kinv);
  164. BN_clear_free(rp);
  165. return ret;
  166. }
  167. /*-
  168. * Positive and negative ECDSA testing through EVP interface:
  169. * - EVP_DigestSign (this is the one-shot version)
  170. * - EVP_DigestVerify
  171. *
  172. * Tests the library can successfully:
  173. * - create a key
  174. * - create a signature
  175. * - accept that signature
  176. * - reject that signature with a different public key
  177. * - reject that signature if its length is not correct
  178. * - reject that signature after modifying the message
  179. * - accept that signature after un-modifying the message
  180. * - reject that signature after modifying the signature
  181. * - accept that signature after un-modifying the signature
  182. */
  183. static int set_sm2_id(EVP_MD_CTX *mctx, EVP_PKEY *pkey)
  184. {
  185. /* With the SM2 key type, the SM2 ID is mandatory */
  186. static const char sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' };
  187. EVP_PKEY_CTX *pctx;
  188. if (!TEST_ptr(pctx = EVP_MD_CTX_pkey_ctx(mctx))
  189. || !TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, sm2_id, sizeof(sm2_id)), 0))
  190. return 0;
  191. return 1;
  192. }
  193. static int test_builtin(int n, int as)
  194. {
  195. EC_KEY *eckey_neg = NULL, *eckey = NULL;
  196. unsigned char dirt, offset, tbs[128];
  197. unsigned char *sig = NULL;
  198. EVP_PKEY *pkey_neg = NULL, *pkey = NULL;
  199. EVP_MD_CTX *mctx = NULL;
  200. size_t sig_len;
  201. int nid, ret = 0;
  202. int temp;
  203. nid = curves[n].nid;
  204. /* skip built-in curves where ord(G) is not prime */
  205. if (nid == NID_ipsec4 || nid == NID_ipsec3) {
  206. TEST_info("skipped: ECDSA unsupported for curve %s", OBJ_nid2sn(nid));
  207. return 1;
  208. }
  209. /*
  210. * skip SM2 curve if 'as' is equal to EVP_PKEY_EC or, skip all curves
  211. * except SM2 curve if 'as' is equal to EVP_PKEY_SM2
  212. */
  213. if (nid == NID_sm2 && as == EVP_PKEY_EC) {
  214. TEST_info("skipped: EC key type unsupported for curve %s",
  215. OBJ_nid2sn(nid));
  216. return 1;
  217. } else if (nid != NID_sm2 && as == EVP_PKEY_SM2) {
  218. TEST_info("skipped: SM2 key type unsupported for curve %s",
  219. OBJ_nid2sn(nid));
  220. return 1;
  221. }
  222. TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid),
  223. as == EVP_PKEY_EC ? "EC" : "SM2");
  224. if (!TEST_ptr(mctx = EVP_MD_CTX_new())
  225. /* get some random message data */
  226. || !TEST_true(RAND_bytes(tbs, sizeof(tbs)))
  227. /* real key */
  228. || !TEST_ptr(eckey = EC_KEY_new_by_curve_name(nid))
  229. || !TEST_true(EC_KEY_generate_key(eckey))
  230. || !TEST_ptr(pkey = EVP_PKEY_new())
  231. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))
  232. /* fake key for negative testing */
  233. || !TEST_ptr(eckey_neg = EC_KEY_new_by_curve_name(nid))
  234. || !TEST_true(EC_KEY_generate_key(eckey_neg))
  235. || !TEST_ptr(pkey_neg = EVP_PKEY_new())
  236. || !TEST_false(EVP_PKEY_assign_EC_KEY(pkey_neg, NULL))
  237. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg, eckey_neg)))
  238. goto err;
  239. temp = ECDSA_size(eckey);
  240. /*
  241. * |as| indicates how we want to treat the key, i.e. what sort of
  242. * computation we want to do with it. The two choices are the key
  243. * types EVP_PKEY_EC and EVP_PKEY_SM2. It's perfectly possible to
  244. * switch back and forth between those two key types, regardless of
  245. * curve, even though the default is to have EVP_PKEY_SM2 for the
  246. * SM2 curve and EVP_PKEY_EC for all other curves.
  247. */
  248. if (!TEST_true(EVP_PKEY_set_alias_type(pkey, as))
  249. || !TEST_true(EVP_PKEY_set_alias_type(pkey_neg, as)))
  250. goto err;
  251. if (!TEST_int_ge(temp, 0)
  252. || !TEST_ptr(sig = OPENSSL_malloc(sig_len = (size_t)temp))
  253. /* create a signature */
  254. || !TEST_true(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey))
  255. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  256. || !TEST_true(EVP_DigestSign(mctx, sig, &sig_len, tbs, sizeof(tbs)))
  257. || !TEST_int_le(sig_len, ECDSA_size(eckey))
  258. || !TEST_true(EVP_MD_CTX_reset(mctx))
  259. /* negative test, verify with wrong key, 0 return */
  260. || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey_neg))
  261. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey_neg))
  262. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
  263. || !TEST_true(EVP_MD_CTX_reset(mctx))
  264. /* negative test, verify with wrong signature length, -1 return */
  265. || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  266. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  267. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len - 1, tbs, sizeof(tbs)), -1)
  268. || !TEST_true(EVP_MD_CTX_reset(mctx))
  269. /* positive test, verify with correct key, 1 return */
  270. || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  271. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  272. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
  273. || !TEST_true(EVP_MD_CTX_reset(mctx)))
  274. goto err;
  275. /* muck with the message, test it fails with 0 return */
  276. tbs[0] ^= 1;
  277. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  278. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  279. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
  280. || !TEST_true(EVP_MD_CTX_reset(mctx)))
  281. goto err;
  282. /* un-muck and test it verifies */
  283. tbs[0] ^= 1;
  284. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  285. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  286. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
  287. || !TEST_true(EVP_MD_CTX_reset(mctx)))
  288. goto err;
  289. /*-
  290. * Muck with the ECDSA signature. The DER encoding is one of:
  291. * - 30 LL 02 ..
  292. * - 30 81 LL 02 ..
  293. *
  294. * - Sometimes this mucks with the high level DER sequence wrapper:
  295. * in that case, DER-parsing of the whole signature should fail.
  296. *
  297. * - Sometimes this mucks with the DER-encoding of ECDSA.r:
  298. * in that case, DER-parsing of ECDSA.r should fail.
  299. *
  300. * - Sometimes this mucks with the DER-encoding of ECDSA.s:
  301. * in that case, DER-parsing of ECDSA.s should fail.
  302. *
  303. * - Sometimes this mucks with ECDSA.r:
  304. * in that case, the signature verification should fail.
  305. *
  306. * - Sometimes this mucks with ECDSA.s:
  307. * in that case, the signature verification should fail.
  308. *
  309. * The usual case is changing the integer value of ECDSA.r or ECDSA.s.
  310. * Because the ratio of DER overhead to signature bytes is small.
  311. * So most of the time it will be one of the last two cases.
  312. *
  313. * In any case, EVP_PKEY_verify should not return 1 for valid.
  314. */
  315. offset = tbs[0] % sig_len;
  316. dirt = tbs[1] ? tbs[1] : 1;
  317. sig[offset] ^= dirt;
  318. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  319. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  320. || !TEST_int_ne(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
  321. || !TEST_true(EVP_MD_CTX_reset(mctx)))
  322. goto err;
  323. /* un-muck and test it verifies */
  324. sig[offset] ^= dirt;
  325. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  326. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  327. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
  328. || !TEST_true(EVP_MD_CTX_reset(mctx)))
  329. goto err;
  330. ret = 1;
  331. err:
  332. EVP_PKEY_free(pkey);
  333. EVP_PKEY_free(pkey_neg);
  334. EVP_MD_CTX_free(mctx);
  335. OPENSSL_free(sig);
  336. return ret;
  337. }
  338. static int test_builtin_as_ec(int n)
  339. {
  340. return test_builtin(n, EVP_PKEY_EC);
  341. }
  342. # ifndef OPENSSL_NO_SM2
  343. static int test_builtin_as_sm2(int n)
  344. {
  345. return test_builtin(n, EVP_PKEY_SM2);
  346. }
  347. # endif
  348. #endif /* OPENSSL_NO_EC */
  349. int setup_tests(void)
  350. {
  351. #ifdef OPENSSL_NO_EC
  352. TEST_note("Elliptic curves are disabled.");
  353. #else
  354. /* get a list of all internal curves */
  355. crv_len = EC_get_builtin_curves(NULL, 0);
  356. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  357. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  358. return 0;
  359. ADD_ALL_TESTS(test_builtin_as_ec, crv_len);
  360. # ifndef OPENSSL_NO_SM2
  361. ADD_ALL_TESTS(test_builtin_as_sm2, crv_len);
  362. # endif
  363. ADD_ALL_TESTS(x9_62_tests, OSSL_NELEM(ecdsa_cavs_kats));
  364. #endif
  365. return 1;
  366. }
  367. void cleanup_tests(void)
  368. {
  369. #ifndef OPENSSL_NO_EC
  370. OPENSSL_free(curves);
  371. #endif
  372. }