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.
 
 
 
 
 
 

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