ecdsatest.c 13 KB

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