ecdsatest.c 11 KB

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