Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

341 Zeilen
11 KiB

  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. int sig_len = i2d_ECDSA_SIG(ecdsa_sig, NULL);
  75. if (sig_len <= 0) {
  76. return false;
  77. }
  78. std::vector<uint8_t> signature(static_cast<size_t>(sig_len));
  79. uint8_t *sig_ptr = bssl::vector_data(&signature);
  80. sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr);
  81. if (sig_len <= 0) {
  82. return false;
  83. }
  84. actual_result = ECDSA_verify(0, digest, digest_len, bssl::vector_data(&signature),
  85. signature.size(), eckey);
  86. break;
  87. }
  88. case kRawApi:
  89. actual_result = ECDSA_do_verify(digest, digest_len, ecdsa_sig, eckey);
  90. break;
  91. default:
  92. return false;
  93. }
  94. return expected_result == actual_result;
  95. }
  96. // TestTamperedSig verifies that signature verification fails when a valid
  97. // signature is tampered with. |ecdsa_sig| must be a valid signature, which will
  98. // be modified. TestTamperedSig returns true on success, false on failure.
  99. static bool TestTamperedSig(FILE *out, Api api, const uint8_t *digest,
  100. size_t digest_len, ECDSA_SIG *ecdsa_sig,
  101. EC_KEY *eckey, const BIGNUM *order) {
  102. // Modify a single byte of the signature: to ensure we don't
  103. // garble the ASN1 structure, we read the raw signature and
  104. // modify a byte in one of the bignums directly.
  105. // Store the two BIGNUMs in raw_buf.
  106. size_t r_len = BN_num_bytes(ecdsa_sig->r);
  107. size_t s_len = BN_num_bytes(ecdsa_sig->s);
  108. size_t bn_len = BN_num_bytes(order);
  109. if (r_len > bn_len || s_len > bn_len) {
  110. return false;
  111. }
  112. size_t buf_len = 2 * bn_len;
  113. std::vector<uint8_t> raw_buf(buf_len);
  114. // Pad the bignums with leading zeroes.
  115. if (!BN_bn2bin_padded(bssl::vector_data(&raw_buf), bn_len, ecdsa_sig->r) ||
  116. !BN_bn2bin_padded(bssl::vector_data(&raw_buf) + bn_len, bn_len,
  117. ecdsa_sig->s)) {
  118. return false;
  119. }
  120. // Modify a single byte in the buffer.
  121. size_t offset = raw_buf[10] % buf_len;
  122. uint8_t dirt = raw_buf[11] ? raw_buf[11] : 1;
  123. raw_buf[offset] ^= dirt;
  124. // Now read the BIGNUMs back in from raw_buf.
  125. if (BN_bin2bn(bssl::vector_data(&raw_buf), bn_len, ecdsa_sig->r) == NULL ||
  126. BN_bin2bn(bssl::vector_data(&raw_buf) + bn_len, bn_len,
  127. ecdsa_sig->s) == NULL ||
  128. !VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 0)) {
  129. return false;
  130. }
  131. // Sanity check: Undo the modification and verify signature.
  132. raw_buf[offset] ^= dirt;
  133. if (BN_bin2bn(bssl::vector_data(&raw_buf), bn_len, ecdsa_sig->r) == NULL ||
  134. BN_bin2bn(bssl::vector_data(&raw_buf) + bn_len, bn_len,
  135. ecdsa_sig->s) == NULL ||
  136. !VerifyECDSASig(api, digest, digest_len, ecdsa_sig, eckey, 1)) {
  137. return false;
  138. }
  139. return true;
  140. }
  141. static bool TestBuiltin(FILE *out) {
  142. // Fill digest values with some random data.
  143. uint8_t digest[20], wrong_digest[20];
  144. if (!RAND_bytes(digest, 20) || !RAND_bytes(wrong_digest, 20)) {
  145. fprintf(out, "ERROR: unable to get random data\n");
  146. return false;
  147. }
  148. static const struct {
  149. int nid;
  150. const char *name;
  151. } kCurves[] = {
  152. { NID_secp224r1, "secp224r1" },
  153. { NID_X9_62_prime256v1, "secp256r1" },
  154. { NID_secp384r1, "secp384r1" },
  155. { NID_secp521r1, "secp521r1" },
  156. { NID_undef, NULL }
  157. };
  158. // Create and verify ECDSA signatures with every available curve.
  159. fputs("\ntesting ECDSA_sign(), ECDSA_verify(), ECDSA_do_sign(), and "
  160. "ECDSA_do_verify() with some internal curves:\n", out);
  161. for (size_t n = 0; kCurves[n].nid != NID_undef; n++) {
  162. fprintf(out, "%s: ", kCurves[n].name);
  163. int nid = kCurves[n].nid;
  164. ScopedEC_GROUP group(EC_GROUP_new_by_curve_name(nid));
  165. if (!group) {
  166. fprintf(out, " failed\n");
  167. return false;
  168. }
  169. ScopedBIGNUM order(BN_new());
  170. if (!order || !EC_GROUP_get_order(group.get(), order.get(), NULL)) {
  171. fprintf(out, " failed\n");
  172. return false;
  173. }
  174. if (BN_num_bits(order.get()) < 160) {
  175. // Too small to test.
  176. fprintf(out, " skipped\n");
  177. continue;
  178. }
  179. // Create a new ECDSA key.
  180. ScopedEC_KEY eckey(EC_KEY_new());
  181. if (!eckey || !EC_KEY_set_group(eckey.get(), group.get()) ||
  182. !EC_KEY_generate_key(eckey.get())) {
  183. fprintf(out, " failed\n");
  184. return false;
  185. }
  186. // Create a second key.
  187. ScopedEC_KEY wrong_eckey(EC_KEY_new());
  188. if (!wrong_eckey || !EC_KEY_set_group(wrong_eckey.get(), group.get()) ||
  189. !EC_KEY_generate_key(wrong_eckey.get())) {
  190. fprintf(out, " failed\n");
  191. return false;
  192. }
  193. fprintf(out, ".");
  194. fflush(out);
  195. // Check the key.
  196. if (!EC_KEY_check_key(eckey.get())) {
  197. fprintf(out, " failed\n");
  198. return false;
  199. }
  200. fprintf(out, ".");
  201. fflush(out);
  202. // Test ASN.1-encoded signatures.
  203. // Create a signature.
  204. unsigned sig_len = ECDSA_size(eckey.get());
  205. std::vector<uint8_t> signature(sig_len);
  206. if (!ECDSA_sign(0, digest, 20, bssl::vector_data(&signature), &sig_len,
  207. eckey.get())) {
  208. fprintf(out, " failed\n");
  209. return false;
  210. }
  211. signature.resize(sig_len);
  212. fprintf(out, ".");
  213. fflush(out);
  214. // Verify the signature.
  215. if (!ECDSA_verify(0, digest, 20, bssl::vector_data(&signature),
  216. signature.size(), eckey.get())) {
  217. fprintf(out, " failed\n");
  218. return false;
  219. }
  220. fprintf(out, ".");
  221. fflush(out);
  222. // Verify the signature with the wrong key.
  223. if (ECDSA_verify(0, digest, 20, bssl::vector_data(&signature),
  224. signature.size(), wrong_eckey.get())) {
  225. fprintf(out, " failed\n");
  226. return false;
  227. }
  228. fprintf(out, ".");
  229. fflush(out);
  230. // Verify the signature using the wrong digest.
  231. if (ECDSA_verify(0, wrong_digest, 20, bssl::vector_data(&signature),
  232. signature.size(), eckey.get())) {
  233. fprintf(out, " failed\n");
  234. return false;
  235. }
  236. fprintf(out, ".");
  237. fflush(out);
  238. // Verify a truncated signature.
  239. if (ECDSA_verify(0, digest, 20, bssl::vector_data(&signature),
  240. signature.size() - 1, eckey.get())) {
  241. fprintf(out, " failed\n");
  242. return false;
  243. }
  244. fprintf(out, ".");
  245. fflush(out);
  246. // Verify a tampered signature.
  247. const uint8_t *sig_ptr = bssl::vector_data(&signature);
  248. ScopedECDSA_SIG ecdsa_sig(d2i_ECDSA_SIG(NULL, &sig_ptr, signature.size()));
  249. if (!ecdsa_sig ||
  250. !TestTamperedSig(out, kEncodedApi, digest, 20, ecdsa_sig.get(),
  251. eckey.get(), order.get())) {
  252. fprintf(out, " failed\n");
  253. return false;
  254. }
  255. fprintf(out, ".");
  256. fflush(out);
  257. // Test ECDSA_SIG signing and verification.
  258. // Create a signature.
  259. ecdsa_sig.reset(ECDSA_do_sign(digest, 20, eckey.get()));
  260. if (!ecdsa_sig) {
  261. fprintf(out, " failed\n");
  262. return false;
  263. }
  264. fprintf(out, ".");
  265. fflush(out);
  266. // Verify the signature using the correct key.
  267. if (!ECDSA_do_verify(digest, 20, ecdsa_sig.get(), eckey.get())) {
  268. fprintf(out, " failed\n");
  269. return false;
  270. }
  271. fprintf(out, ".");
  272. fflush(out);
  273. // Verify the signature with the wrong key.
  274. if (ECDSA_do_verify(digest, 20, ecdsa_sig.get(), wrong_eckey.get())) {
  275. fprintf(out, " failed\n");
  276. return false;
  277. }
  278. fprintf(out, ".");
  279. fflush(out);
  280. // Verify the signature using the wrong digest.
  281. if (ECDSA_do_verify(wrong_digest, 20, ecdsa_sig.get(), eckey.get())) {
  282. fprintf(out, " failed\n");
  283. return false;
  284. }
  285. fprintf(out, ".");
  286. fflush(out);
  287. // Verify a tampered signature.
  288. if (!TestTamperedSig(out, kRawApi, digest, 20, ecdsa_sig.get(), eckey.get(),
  289. order.get())) {
  290. fprintf(out, " failed\n");
  291. return false;
  292. }
  293. fprintf(out, ".");
  294. fflush(out);
  295. fprintf(out, " ok\n");
  296. // Clear bogus errors.
  297. ERR_clear_error();
  298. }
  299. return true;
  300. }
  301. int main(void) {
  302. CRYPTO_library_init();
  303. ERR_load_crypto_strings();
  304. if (!TestBuiltin(stdout)) {
  305. printf("\nECDSA test failed\n");
  306. ERR_print_errors_fp(stdout);
  307. return 1;
  308. }
  309. printf("\nPASS\n");
  310. return 0;
  311. }