Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ecdsa_test.c 9.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <openssl/bio.h>
  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. int test_builtin(BIO *out) {
  62. size_t n = 0;
  63. EC_KEY *eckey = NULL, *wrong_eckey = NULL;
  64. EC_GROUP *group;
  65. ECDSA_SIG *ecdsa_sig = NULL;
  66. unsigned char digest[20], wrong_digest[20];
  67. unsigned char *signature = NULL;
  68. const unsigned char *sig_ptr;
  69. unsigned char *sig_ptr2;
  70. unsigned char *raw_buf = NULL;
  71. unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
  72. int nid, ret = 0;
  73. /* fill digest values with some random data */
  74. if (!RAND_pseudo_bytes(digest, 20) || !RAND_pseudo_bytes(wrong_digest, 20)) {
  75. BIO_printf(out, "ERROR: unable to get random data\n");
  76. goto builtin_err;
  77. }
  78. /* create and verify a ecdsa signature with every availble curve
  79. * (with ) */
  80. BIO_printf(out,
  81. "\ntesting ECDSA_sign() and ECDSA_verify() "
  82. "with some internal curves:\n");
  83. static const int kCurveNIDs[] = {NID_secp224r1, NID_X9_62_prime256v1,
  84. NID_secp384r1, NID_secp521r1, NID_undef};
  85. /* now create and verify a signature for every curve */
  86. for (n = 0; kCurveNIDs[n] != NID_undef; n++) {
  87. unsigned char dirt, offset;
  88. nid = kCurveNIDs[n];
  89. /* create new ecdsa key (== EC_KEY) */
  90. eckey = EC_KEY_new();
  91. if (eckey == NULL) {
  92. goto builtin_err;
  93. }
  94. group = EC_GROUP_new_by_curve_name(nid);
  95. if (group == NULL) {
  96. goto builtin_err;
  97. }
  98. if (!EC_KEY_set_group(eckey, group)) {
  99. goto builtin_err;
  100. }
  101. EC_GROUP_free(group);
  102. degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
  103. if (degree < 160) {
  104. /* Too small to test. */
  105. EC_KEY_free(eckey);
  106. eckey = NULL;
  107. continue;
  108. }
  109. BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
  110. /* create key */
  111. if (!EC_KEY_generate_key(eckey)) {
  112. BIO_printf(out, " failed\n");
  113. goto builtin_err;
  114. }
  115. /* create second key */
  116. wrong_eckey = EC_KEY_new();
  117. if (wrong_eckey == NULL) {
  118. goto builtin_err;
  119. }
  120. group = EC_GROUP_new_by_curve_name(nid);
  121. if (group == NULL) {
  122. goto builtin_err;
  123. }
  124. if (EC_KEY_set_group(wrong_eckey, group) == 0) {
  125. goto builtin_err;
  126. }
  127. EC_GROUP_free(group);
  128. if (!EC_KEY_generate_key(wrong_eckey)) {
  129. BIO_printf(out, " failed\n");
  130. goto builtin_err;
  131. }
  132. BIO_printf(out, ".");
  133. (void)BIO_flush(out);
  134. /* check key */
  135. if (!EC_KEY_check_key(eckey)) {
  136. BIO_printf(out, " failed\n");
  137. goto builtin_err;
  138. }
  139. BIO_printf(out, ".");
  140. (void)BIO_flush(out);
  141. /* create signature */
  142. sig_len = ECDSA_size(eckey);
  143. signature = OPENSSL_malloc(sig_len);
  144. if (signature == NULL) {
  145. goto builtin_err;
  146. }
  147. if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey)) {
  148. BIO_printf(out, " failed\n");
  149. goto builtin_err;
  150. }
  151. BIO_printf(out, ".");
  152. (void)BIO_flush(out);
  153. /* verify signature */
  154. if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1) {
  155. BIO_printf(out, " failed\n");
  156. goto builtin_err;
  157. }
  158. BIO_printf(out, ".");
  159. (void)BIO_flush(out);
  160. /* verify signature with the wrong key */
  161. if (ECDSA_verify(0, digest, 20, signature, sig_len, wrong_eckey) == 1) {
  162. BIO_printf(out, " failed\n");
  163. goto builtin_err;
  164. }
  165. BIO_printf(out, ".");
  166. (void)BIO_flush(out);
  167. /* wrong digest */
  168. if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len, eckey) == 1) {
  169. BIO_printf(out, " failed\n");
  170. goto builtin_err;
  171. }
  172. BIO_printf(out, ".");
  173. (void)BIO_flush(out);
  174. /* wrong length */
  175. if (ECDSA_verify(0, digest, 20, signature, sig_len - 1, eckey) == 1) {
  176. BIO_printf(out, " failed\n");
  177. goto builtin_err;
  178. }
  179. BIO_printf(out, ".");
  180. (void)BIO_flush(out);
  181. /* Modify a single byte of the signature: to ensure we don't
  182. * garble the ASN1 structure, we read the raw signature and
  183. * modify a byte in one of the bignums directly. */
  184. sig_ptr = signature;
  185. ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len);
  186. if (ecdsa_sig == NULL) {
  187. BIO_printf(out, " failed\n");
  188. goto builtin_err;
  189. }
  190. /* Store the two BIGNUMs in raw_buf. */
  191. r_len = BN_num_bytes(ecdsa_sig->r);
  192. s_len = BN_num_bytes(ecdsa_sig->s);
  193. bn_len = (degree + 7) / 8;
  194. if (r_len > bn_len || s_len > bn_len) {
  195. BIO_printf(out, " failed\n");
  196. goto builtin_err;
  197. }
  198. buf_len = 2 * bn_len;
  199. raw_buf = OPENSSL_malloc(buf_len);
  200. if (raw_buf == NULL) {
  201. goto builtin_err;
  202. }
  203. /* Pad the bignums with leading zeroes. */
  204. memset(raw_buf, 0, buf_len);
  205. BN_bn2bin(ecdsa_sig->r, raw_buf + bn_len - r_len);
  206. BN_bn2bin(ecdsa_sig->s, raw_buf + buf_len - s_len);
  207. /* Modify a single byte in the buffer. */
  208. offset = raw_buf[10] % buf_len;
  209. dirt = raw_buf[11] ? raw_buf[11] : 1;
  210. raw_buf[offset] ^= dirt;
  211. /* Now read the BIGNUMs back in from raw_buf. */
  212. if (BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL ||
  213. BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL) {
  214. goto builtin_err;
  215. }
  216. sig_ptr2 = signature;
  217. sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
  218. if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1) {
  219. BIO_printf(out, " failed\n");
  220. goto builtin_err;
  221. }
  222. /* Sanity check: undo the modification and verify signature. */
  223. raw_buf[offset] ^= dirt;
  224. if (BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL ||
  225. BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL) {
  226. goto builtin_err;
  227. }
  228. sig_ptr2 = signature;
  229. sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
  230. if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1) {
  231. BIO_printf(out, " failed\n");
  232. goto builtin_err;
  233. }
  234. BIO_printf(out, ".");
  235. (void)BIO_flush(out);
  236. BIO_printf(out, " ok\n");
  237. /* cleanup */
  238. /* clean bogus errors */
  239. ERR_clear_error();
  240. OPENSSL_free(signature);
  241. signature = NULL;
  242. EC_KEY_free(eckey);
  243. eckey = NULL;
  244. EC_KEY_free(wrong_eckey);
  245. wrong_eckey = NULL;
  246. ECDSA_SIG_free(ecdsa_sig);
  247. ecdsa_sig = NULL;
  248. OPENSSL_free(raw_buf);
  249. raw_buf = NULL;
  250. }
  251. ret = 1;
  252. builtin_err:
  253. if (eckey)
  254. EC_KEY_free(eckey);
  255. if (wrong_eckey)
  256. EC_KEY_free(wrong_eckey);
  257. if (ecdsa_sig)
  258. ECDSA_SIG_free(ecdsa_sig);
  259. if (signature)
  260. OPENSSL_free(signature);
  261. if (raw_buf)
  262. OPENSSL_free(raw_buf);
  263. return ret;
  264. }
  265. int main(void) {
  266. int ret = 1;
  267. BIO *out;
  268. CRYPTO_library_init();
  269. ERR_load_crypto_strings();
  270. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  271. if (!test_builtin(out))
  272. goto err;
  273. ret = 0;
  274. err:
  275. if (ret)
  276. BIO_printf(out, "\nECDSA test failed\n");
  277. else
  278. BIO_printf(out, "\nPASS\n");
  279. if (ret)
  280. BIO_print_errors(out);
  281. if (out != NULL)
  282. BIO_free(out);
  283. return ret;
  284. }