25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ecdsa_test.cc 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* ====================================================================
  2. * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com). */
  52. #include <openssl/ecdsa.h>
  53. #include <vector>
  54. #include <openssl/bn.h>
  55. #include <openssl/crypto.h>
  56. #include <openssl/ec.h>
  57. #include <openssl/err.h>
  58. #include <openssl/mem.h>
  59. #include <openssl/obj.h>
  60. #include <openssl/rand.h>
  61. #include "../test/scoped_types.h"
  62. #include "../test/stl_compat.h"
  63. enum Api {
  64. kEncodedApi,
  65. kRawApi,
  66. };
  67. // VerifyECDSASig returns true on success, false on failure.
  68. static bool VerifyECDSASig(Api api, const uint8_t *digest,
  69. size_t digest_len, const ECDSA_SIG *ecdsa_sig,
  70. EC_KEY *eckey, int expected_result) {
  71. int actual_result;
  72. switch (api) {
  73. case kEncodedApi: {
  74. uint8_t *der;
  75. size_t der_len;
  76. if (!ECDSA_SIG_to_bytes(&der, &der_len, ecdsa_sig)) {
  77. return false;
  78. }
  79. ScopedOpenSSLBytes delete_der(der);
  80. actual_result = ECDSA_verify(0, digest, digest_len, der, der_len, eckey);
  81. break;
  82. }
  83. case kRawApi:
  84. actual_result = ECDSA_do_verify(digest, digest_len, ecdsa_sig, eckey);
  85. break;
  86. default:
  87. return false;
  88. }
  89. return expected_result == actual_result;
  90. }
  91. // TestTamperedSig verifies that signature verification fails when a valid
  92. // signature is tampered with. |ecdsa_sig| must be a valid signature, which will
  93. // be modified. TestTamperedSig returns true on success, false on failure.
  94. static bool TestTamperedSig(FILE *out, Api api, const uint8_t *digest,
  95. size_t digest_len, ECDSA_SIG *ecdsa_sig,
  96. EC_KEY *eckey, const BIGNUM *order) {
  97. // Modify a single byte of the signature: to ensure we don't
  98. // garble the ASN1 structure, we read the raw signature and
  99. // modify a byte in one of the bignums directly.
  100. // Store the two BIGNUMs in raw_buf.
  101. size_t r_len = BN_num_bytes(ecdsa_sig->r);
  102. size_t s_len = BN_num_bytes(ecdsa_sig->s);
  103. size_t bn_len = BN_num_bytes(order);
  104. if (r_len > bn_len || s_len > bn_len) {
  105. return false;
  106. }
  107. size_t buf_len = 2 * bn_len;
  108. std::vector<uint8_t> raw_buf(buf_len);
  109. // Pad the bignums with leading zeroes.
  110. if (!BN_bn2bin_padded(bssl::vector_data(&raw_buf), bn_len, ecdsa_sig->r) ||
  111. !BN_bn2bin_padded(bssl::vector_data(&raw_buf) + bn_len, bn_len,
  112. ecdsa_sig->s)) {
  113. return false;
  114. }
  115. // Modify a single byte in the buffer.
  116. size_t offset = raw_buf[10] % buf_len;
  117. uint8_t dirt = raw_buf[11] ? raw_buf[11] : 1;
  118. raw_buf[offset] ^= dirt;
  119. // Now read the BIGNUMs back in from raw_buf.
  120. if (BN_bin2bn(bssl::vector_data(&raw_buf), bn_len, ecdsa_sig->r) == NULL ||
  121. BN_bin2bn(bssl::vector_data(&raw_buf) + bn_len, bn_len,
  122. ecdsa_sig->s) == NULL ||
  123. !VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 0)) {
  124. return false;
  125. }
  126. // Sanity check: Undo the modification and verify signature.
  127. raw_buf[offset] ^= dirt;
  128. if (BN_bin2bn(bssl::vector_data(&raw_buf), bn_len, ecdsa_sig->r) == NULL ||
  129. BN_bin2bn(bssl::vector_data(&raw_buf) + bn_len, bn_len,
  130. ecdsa_sig->s) == NULL ||
  131. !VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 1)) {
  132. return false;
  133. }
  134. return true;
  135. }
  136. static bool TestBuiltin(FILE *out) {
  137. // Fill digest values with some random data.
  138. uint8_t digest[20], wrong_digest[20];
  139. if (!RAND_bytes(digest, 20) || !RAND_bytes(wrong_digest, 20)) {
  140. fprintf(out, "ERROR: unable to get random data\n");
  141. return false;
  142. }
  143. static const struct {
  144. int nid;
  145. const char *name;
  146. } kCurves[] = {
  147. { NID_secp224r1, "secp224r1" },
  148. { NID_X9_62_prime256v1, "secp256r1" },
  149. { NID_secp384r1, "secp384r1" },
  150. { NID_secp521r1, "secp521r1" },
  151. { NID_undef, NULL }
  152. };
  153. // Create and verify ECDSA signatures with every available curve.
  154. fputs("\ntesting ECDSA_sign(), ECDSA_verify(), ECDSA_do_sign(), and "
  155. "ECDSA_do_verify() with some internal curves:\n", out);
  156. for (size_t n = 0; kCurves[n].nid != NID_undef; n++) {
  157. fprintf(out, "%s: ", kCurves[n].name);
  158. int nid = kCurves[n].nid;
  159. ScopedEC_GROUP group(EC_GROUP_new_by_curve_name(nid));
  160. if (!group) {
  161. fprintf(out, " failed\n");
  162. return false;
  163. }
  164. ScopedBIGNUM order(BN_new());
  165. if (!order || !EC_GROUP_get_order(group.get(), order.get(), NULL)) {
  166. fprintf(out, " failed\n");
  167. return false;
  168. }
  169. if (BN_num_bits(order.get()) < 160) {
  170. // Too small to test.
  171. fprintf(out, " skipped\n");
  172. continue;
  173. }
  174. // Create a new ECDSA key.
  175. ScopedEC_KEY eckey(EC_KEY_new());
  176. if (!eckey || !EC_KEY_set_group(eckey.get(), group.get()) ||
  177. !EC_KEY_generate_key(eckey.get())) {
  178. fprintf(out, " failed\n");
  179. return false;
  180. }
  181. // Create a second key.
  182. ScopedEC_KEY wrong_eckey(EC_KEY_new());
  183. if (!wrong_eckey || !EC_KEY_set_group(wrong_eckey.get(), group.get()) ||
  184. !EC_KEY_generate_key(wrong_eckey.get())) {
  185. fprintf(out, " failed\n");
  186. return false;
  187. }
  188. fprintf(out, ".");
  189. fflush(out);
  190. // Check the key.
  191. if (!EC_KEY_check_key(eckey.get())) {
  192. fprintf(out, " failed\n");
  193. return false;
  194. }
  195. fprintf(out, ".");
  196. fflush(out);
  197. // Test ASN.1-encoded signatures.
  198. // Create a signature.
  199. unsigned sig_len = ECDSA_size(eckey.get());
  200. std::vector<uint8_t> signature(sig_len);
  201. if (!ECDSA_sign(0, digest, 20, bssl::vector_data(&signature), &sig_len,
  202. eckey.get())) {
  203. fprintf(out, " failed\n");
  204. return false;
  205. }
  206. signature.resize(sig_len);
  207. fprintf(out, ".");
  208. fflush(out);
  209. // Verify the signature.
  210. if (!ECDSA_verify(0, digest, 20, bssl::vector_data(&signature),
  211. signature.size(), eckey.get())) {
  212. fprintf(out, " failed\n");
  213. return false;
  214. }
  215. fprintf(out, ".");
  216. fflush(out);
  217. // Verify the signature with the wrong key.
  218. if (ECDSA_verify(0, digest, 20, bssl::vector_data(&signature),
  219. signature.size(), wrong_eckey.get())) {
  220. fprintf(out, " failed\n");
  221. return false;
  222. }
  223. fprintf(out, ".");
  224. fflush(out);
  225. // Verify the signature using the wrong digest.
  226. if (ECDSA_verify(0, wrong_digest, 20, bssl::vector_data(&signature),
  227. signature.size(), eckey.get())) {
  228. fprintf(out, " failed\n");
  229. return false;
  230. }
  231. fprintf(out, ".");
  232. fflush(out);
  233. // Verify a truncated signature.
  234. if (ECDSA_verify(0, digest, 20, bssl::vector_data(&signature),
  235. signature.size() - 1, eckey.get())) {
  236. fprintf(out, " failed\n");
  237. return false;
  238. }
  239. fprintf(out, ".");
  240. fflush(out);
  241. // Verify a tampered signature.
  242. ScopedECDSA_SIG ecdsa_sig(ECDSA_SIG_from_bytes(
  243. bssl::vector_data(&signature), signature.size()));
  244. if (!ecdsa_sig ||
  245. !TestTamperedSig(out, kEncodedApi, digest, 20, ecdsa_sig.get(),
  246. eckey.get(), order.get())) {
  247. fprintf(out, " failed\n");
  248. return false;
  249. }
  250. fprintf(out, ".");
  251. fflush(out);
  252. // Test ECDSA_SIG signing and verification.
  253. // Create a signature.
  254. ecdsa_sig.reset(ECDSA_do_sign(digest, 20, eckey.get()));
  255. if (!ecdsa_sig) {
  256. fprintf(out, " failed\n");
  257. return false;
  258. }
  259. fprintf(out, ".");
  260. fflush(out);
  261. // Verify the signature using the correct key.
  262. if (!ECDSA_do_verify(digest, 20, ecdsa_sig.get(), eckey.get())) {
  263. fprintf(out, " failed\n");
  264. return false;
  265. }
  266. fprintf(out, ".");
  267. fflush(out);
  268. // Verify the signature with the wrong key.
  269. if (ECDSA_do_verify(digest, 20, ecdsa_sig.get(), wrong_eckey.get())) {
  270. fprintf(out, " failed\n");
  271. return false;
  272. }
  273. fprintf(out, ".");
  274. fflush(out);
  275. // Verify the signature using the wrong digest.
  276. if (ECDSA_do_verify(wrong_digest, 20, ecdsa_sig.get(), eckey.get())) {
  277. fprintf(out, " failed\n");
  278. return false;
  279. }
  280. fprintf(out, ".");
  281. fflush(out);
  282. // Verify a tampered signature.
  283. if (!TestTamperedSig(out, kRawApi, digest, 20, ecdsa_sig.get(), eckey.get(),
  284. order.get())) {
  285. fprintf(out, " failed\n");
  286. return false;
  287. }
  288. fprintf(out, ".");
  289. fflush(out);
  290. fprintf(out, " ok\n");
  291. // Clear bogus errors.
  292. ERR_clear_error();
  293. }
  294. return true;
  295. }
  296. static bool TestECDSA_SIG_max_len(size_t order_len) {
  297. /* Create the largest possible |ECDSA_SIG| of the given constraints. */
  298. ScopedECDSA_SIG sig(ECDSA_SIG_new());
  299. if (!sig) {
  300. return false;
  301. }
  302. std::vector<uint8_t> bytes(order_len, 0xff);
  303. if (!BN_bin2bn(bssl::vector_data(&bytes), bytes.size(), sig->r) ||
  304. !BN_bin2bn(bssl::vector_data(&bytes), bytes.size(), sig->s)) {
  305. return false;
  306. }
  307. /* Serialize it. */
  308. uint8_t *der;
  309. size_t der_len;
  310. if (!ECDSA_SIG_to_bytes(&der, &der_len, sig.get())) {
  311. return false;
  312. }
  313. ScopedOpenSSLBytes delete_der(der);
  314. size_t max_len = ECDSA_SIG_max_len(order_len);
  315. if (max_len != der_len) {
  316. fprintf(stderr, "ECDSA_SIG_max_len(%u) returned %u, wanted %u\n",
  317. static_cast<unsigned>(order_len), static_cast<unsigned>(max_len),
  318. static_cast<unsigned>(der_len));
  319. return false;
  320. }
  321. return true;
  322. }
  323. int main(void) {
  324. CRYPTO_library_init();
  325. ERR_load_crypto_strings();
  326. if (!TestBuiltin(stdout) ||
  327. !TestECDSA_SIG_max_len(224/8) ||
  328. !TestECDSA_SIG_max_len(256/8) ||
  329. !TestECDSA_SIG_max_len(384/8) ||
  330. !TestECDSA_SIG_max_len(512/8) ||
  331. !TestECDSA_SIG_max_len(10000)) {
  332. printf("\nECDSA test failed\n");
  333. ERR_print_errors_fp(stdout);
  334. return 1;
  335. }
  336. printf("\nPASS\n");
  337. return 0;
  338. }