2
0

ecdsatest.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. TEST_info("testing ECDSA for curve %s as %s key type", OBJ_nid2sn(nid),
  210. as == EVP_PKEY_EC ? "EC" : "SM2");
  211. if (!TEST_ptr(mctx = EVP_MD_CTX_new())
  212. /* get some random message data */
  213. || !TEST_true(RAND_bytes(tbs, sizeof(tbs)))
  214. /* real key */
  215. || !TEST_ptr(eckey = EC_KEY_new_by_curve_name(nid))
  216. || !TEST_true(EC_KEY_generate_key(eckey))
  217. || !TEST_ptr(pkey = EVP_PKEY_new())
  218. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))
  219. /* fake key for negative testing */
  220. || !TEST_ptr(eckey_neg = EC_KEY_new_by_curve_name(nid))
  221. || !TEST_true(EC_KEY_generate_key(eckey_neg))
  222. || !TEST_ptr(pkey_neg = EVP_PKEY_new())
  223. || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey_neg, eckey_neg)))
  224. goto err;
  225. temp = ECDSA_size(eckey);
  226. /*
  227. * |as| indicates how we want to treat the key, i.e. what sort of
  228. * computation we want to do with it. The two choices are the key
  229. * types EVP_PKEY_EC and EVP_PKEY_SM2. It's perfectly possible to
  230. * switch back and forth between those two key types, regardless of
  231. * curve, even though the default is to have EVP_PKEY_SM2 for the
  232. * SM2 curve and EVP_PKEY_EC for all other curves.
  233. */
  234. if (!TEST_true(EVP_PKEY_set_alias_type(pkey, as))
  235. || !TEST_true(EVP_PKEY_set_alias_type(pkey_neg, as)))
  236. goto err;
  237. if (!TEST_int_ge(temp, 0)
  238. || !TEST_ptr(sig = OPENSSL_malloc(sig_len = (size_t)temp))
  239. /* create a signature */
  240. || !TEST_true(EVP_DigestSignInit(mctx, NULL, NULL, NULL, pkey))
  241. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  242. || !TEST_true(EVP_DigestSign(mctx, sig, &sig_len, tbs, sizeof(tbs)))
  243. || !TEST_int_le(sig_len, ECDSA_size(eckey))
  244. || !TEST_true(EVP_MD_CTX_reset(mctx))
  245. /* negative test, verify with wrong key, 0 return */
  246. || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey_neg))
  247. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey_neg))
  248. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
  249. || !TEST_true(EVP_MD_CTX_reset(mctx))
  250. /* negative test, verify with wrong signature length, -1 return */
  251. || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  252. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  253. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len - 1, tbs, sizeof(tbs)), -1)
  254. || !TEST_true(EVP_MD_CTX_reset(mctx))
  255. /* positive test, verify with correct key, 1 return */
  256. || !TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  257. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  258. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
  259. || !TEST_true(EVP_MD_CTX_reset(mctx)))
  260. goto err;
  261. /* muck with the message, test it fails with 0 return */
  262. tbs[0] ^= 1;
  263. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  264. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  265. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 0)
  266. || !TEST_true(EVP_MD_CTX_reset(mctx)))
  267. goto err;
  268. /* un-muck and test it verifies */
  269. tbs[0] ^= 1;
  270. if (!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. /*-
  276. * Muck with the ECDSA signature. The DER encoding is one of:
  277. * - 30 LL 02 ..
  278. * - 30 81 LL 02 ..
  279. *
  280. * - Sometimes this mucks with the high level DER sequence wrapper:
  281. * in that case, DER-parsing of the whole signature should fail.
  282. *
  283. * - Sometimes this mucks with the DER-encoding of ECDSA.r:
  284. * in that case, DER-parsing of ECDSA.r should fail.
  285. *
  286. * - Sometimes this mucks with the DER-encoding of ECDSA.s:
  287. * in that case, DER-parsing of ECDSA.s should fail.
  288. *
  289. * - Sometimes this mucks with ECDSA.r:
  290. * in that case, the signature verification should fail.
  291. *
  292. * - Sometimes this mucks with ECDSA.s:
  293. * in that case, the signature verification should fail.
  294. *
  295. * The usual case is changing the integer value of ECDSA.r or ECDSA.s.
  296. * Because the ratio of DER overhead to signature bytes is small.
  297. * So most of the time it will be one of the last two cases.
  298. *
  299. * In any case, EVP_PKEY_verify should not return 1 for valid.
  300. */
  301. offset = tbs[0] % sig_len;
  302. dirt = tbs[1] ? tbs[1] : 1;
  303. sig[offset] ^= dirt;
  304. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  305. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  306. || !TEST_int_ne(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
  307. || !TEST_true(EVP_MD_CTX_reset(mctx)))
  308. goto err;
  309. /* un-muck and test it verifies */
  310. sig[offset] ^= dirt;
  311. if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, NULL, NULL, pkey))
  312. || (as == EVP_PKEY_SM2 && !set_sm2_id(mctx, pkey))
  313. || !TEST_int_eq(EVP_DigestVerify(mctx, sig, sig_len, tbs, sizeof(tbs)), 1)
  314. || !TEST_true(EVP_MD_CTX_reset(mctx)))
  315. goto err;
  316. ret = 1;
  317. err:
  318. EVP_PKEY_free(pkey);
  319. EVP_PKEY_free(pkey_neg);
  320. EVP_MD_CTX_free(mctx);
  321. OPENSSL_free(sig);
  322. return ret;
  323. }
  324. static int test_builtin_as_ec(int n)
  325. {
  326. return test_builtin(n, EVP_PKEY_EC);
  327. }
  328. # ifndef OPENSSL_NO_SM2
  329. static int test_builtin_as_sm2(int n)
  330. {
  331. return test_builtin(n, EVP_PKEY_SM2);
  332. }
  333. # endif
  334. #endif /* OPENSSL_NO_EC */
  335. int setup_tests(void)
  336. {
  337. #ifdef OPENSSL_NO_EC
  338. TEST_note("Elliptic curves are disabled.");
  339. #else
  340. /* get a list of all internal curves */
  341. crv_len = EC_get_builtin_curves(NULL, 0);
  342. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  343. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  344. return 0;
  345. ADD_ALL_TESTS(test_builtin_as_ec, crv_len);
  346. # ifndef OPENSSL_NO_SM2
  347. ADD_ALL_TESTS(test_builtin_as_sm2, crv_len);
  348. # endif
  349. ADD_ALL_TESTS(x9_62_tests, OSSL_NELEM(ecdsa_cavs_kats));
  350. #endif
  351. return 1;
  352. }
  353. void cleanup_tests(void)
  354. {
  355. #ifndef OPENSSL_NO_EC
  356. OPENSSL_free(curves);
  357. #endif
  358. }