1 /* crypto/ecdsa/ecdsatest.c */
2 /* ====================================================================
3 * Copyright (c) 2000-2002 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
61 /* "To determine the time in seconds, the value returned
62 * by the clock function should be divided by the value
63 * of the macro CLOCKS_PER_SEC."
67 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
68 * -- cc on NeXTstep/OpenStep */
70 # define CLOCKS_PER_SEC 1
73 #ifdef OPENSSL_NO_ECDSA
74 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
77 #include <openssl/crypto.h>
78 #include <openssl/bio.h>
79 #include <openssl/evp.h>
80 #include <openssl/x509.h>
81 #include <openssl/ecdsa.h>
82 #include <openssl/engine.h>
83 #include <openssl/err.h>
85 static BIO *bio_err=NULL;
86 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
88 #define ECDSA_NIST_TESTS 10
89 ECDSA_SIG* signatures[ECDSA_NIST_TESTS];
90 unsigned char digest[ECDSA_NIST_TESTS][20];
92 void clear_ecdsa(ECDSA *ecdsa)
98 EC_GROUP_free(ecdsa->group);
103 EC_POINT_free(ecdsa->pub_key);
104 ecdsa->pub_key = NULL;
108 BN_free(ecdsa->priv_key);
109 ecdsa->priv_key = NULL;
113 int set_p192_param(ECDSA *ecdsa)
120 if ((ctx = BN_CTX_new()) == NULL) goto err;
123 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_192V1)) == NULL)
125 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_192_V1() failed \n");
128 if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
130 BIO_printf(bio_err,"EC_POINT_new failed \n");
134 if (!BN_dec2bn(&(ecdsa->priv_key), "651056770906015076056810763456358567190100156695615665659")) goto err;
135 if (!EC_POINT_mul(ecdsa->group,ecdsa->pub_key,ecdsa->priv_key,NULL,NULL,ctx))
137 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
142 err : if (ctx) BN_CTX_free(ctx);
146 int set_p239_param(ECDSA *ecdsa)
153 if ((ctx = BN_CTX_new()) == NULL) goto err;
156 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_239V1)) == NULL)
158 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_239_V1() failed \n");
161 if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
163 BIO_printf(bio_err,"EC_POINT_new failed \n");
167 if (!BN_dec2bn(&(ecdsa->priv_key), "876300101507107567501066130761671078357010671067781776716671676178726717")) goto err;
168 if (!EC_POINT_mul(ecdsa->group,ecdsa->pub_key,ecdsa->priv_key,NULL,NULL,ctx))
170 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
175 err : if (ctx) BN_CTX_free(ctx);
179 int test_sig_vrf(ECDSA *ecdsa, const unsigned char* dgst)
182 unsigned char *buffer=NULL;
183 unsigned int buf_len;
186 if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
188 if ((buf_len = ECDSA_size(ecdsa)) == 0)
190 BIO_printf(bio_err, "ECDSA_size() == 0 \n");
193 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
197 if (!ECDSA_sign(type, dgst , 20, buffer, &buf_len, ecdsa))
199 BIO_printf(bio_err, "ECDSA_sign() FAILED \n");
203 BIO_printf(bio_err, " [ ECDSA_sign() %.2f"UNIT, (double)tim/(CLOCKS_PER_SEC));
206 ret = ECDSA_verify(type, dgst, 20, buffer, buf_len, ecdsa);
209 BIO_printf(bio_err, "ECDSA_verify() FAILED \n");
213 BIO_printf(bio_err, " and ECDSA_verify() %.2f"UNIT" ] ", (double)tim/(CLOCKS_PER_SEC));
215 err: OPENSSL_free(buffer);
219 int test_x962_sig_vrf(ECDSA *ecdsa, const unsigned char *dgst,
220 const char *k_in, const char *r_in, const char *s_in)
224 EC_POINT *point=NULL;
225 BIGNUM *r=NULL,*s=NULL,*k=NULL,*x=NULL,*y=NULL,*m=NULL,*ord=NULL;
229 if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
231 if ((point = EC_POINT_new(ecdsa->group)) == NULL) goto err;
232 if ((r = BN_new()) == NULL || (s = BN_new()) == NULL || (k = BN_new()) == NULL ||
233 (x = BN_new()) == NULL || (y = BN_new()) == NULL || (m = BN_new()) == NULL ||
234 (ord = BN_new()) == NULL) goto err;
235 if ((ctx = BN_CTX_new()) == NULL) goto err;
236 if (!BN_bin2bn(dgst, 20, m)) goto err;
237 if (!BN_dec2bn(&k, k_in)) goto err;
238 if (!EC_POINT_mul(ecdsa->group, point, k, NULL, NULL, ctx)) goto err;
239 if (!EC_POINT_get_affine_coordinates_GFp(ecdsa->group, point, x, y, ctx)) goto err;
240 if (!EC_GROUP_get_order(ecdsa->group, ord, ctx)) goto err;
241 if ((ecdsa->r = BN_dup(x)) == NULL) goto err;
242 if ((ecdsa->kinv = BN_mod_inverse(NULL, k, ord, ctx)) == NULL) goto err;
244 if ((sig = ECDSA_do_sign(dgst, 20, ecdsa)) == NULL)
246 BIO_printf(bio_err,"ECDSA_do_sign() failed \n");
250 if (!BN_dec2bn(&r, r_in)) goto err;
251 if (!BN_dec2bn(&s, s_in)) goto err;
252 if (BN_cmp(sig->r,r) != 0 || BN_cmp(sig->s,s) != 0)
254 tmp_char = OPENSSL_malloc(128);
255 if (tmp_char == NULL) goto err;
256 tmp_char = BN_bn2dec(sig->r);
257 BIO_printf(bio_err,"unexpected signature \n");
258 BIO_printf(bio_err,"sig->r = %s\n",tmp_char);
259 tmp_char = BN_bn2dec(sig->s);
260 BIO_printf(bio_err,"sig->s = %s\n",tmp_char);
263 ret = ECDSA_do_verify(dgst, 20, sig, ecdsa);
266 BIO_printf(bio_err,"ECDSA_do_verify : signature verification failed \n");
271 err : if (r) BN_free(r);
277 if (ord) BN_free(ord);
278 if (sig) ECDSA_SIG_free(sig);
279 if (ctx) BN_CTX_free(ctx);
280 if (point) EC_POINT_free(point);
281 if (tmp_char) OPENSSL_free(tmp_char);
285 int ecdsa_cmp(const ECDSA *a, const ECDSA *b)
289 BIGNUM *tmp_a1=NULL, *tmp_a2=NULL, *tmp_a3=NULL;
290 BIGNUM *tmp_b1=NULL, *tmp_b2=NULL, *tmp_b3=NULL;
292 if ((ctx = BN_CTX_new()) == NULL) return 1;
293 if ((tmp_a1 = BN_new()) == NULL || (tmp_a2 = BN_new()) == NULL || (tmp_a3 = BN_new()) == NULL) goto err;
294 if ((tmp_b1 = BN_new()) == NULL || (tmp_b2 = BN_new()) == NULL || (tmp_b3 = BN_new()) == NULL) goto err;
296 if (a->pub_key && b->pub_key)
297 if (EC_POINT_cmp(a->group, a->pub_key, b->pub_key, ctx) != 0) goto err;
298 if (a->priv_key && b->priv_key)
299 if (BN_cmp(a->priv_key, b->priv_key) != 0) goto err;
300 if (!EC_GROUP_get_curve_GFp(a->group, tmp_a1, tmp_a2, tmp_a3, ctx)) goto err;
301 if (!EC_GROUP_get_curve_GFp(a->group, tmp_b1, tmp_b2, tmp_b3, ctx)) goto err;
302 if (BN_cmp(tmp_a1, tmp_b1) != 0) goto err;
303 if (BN_cmp(tmp_a2, tmp_b2) != 0) goto err;
304 if (BN_cmp(tmp_a3, tmp_b3) != 0) goto err;
307 err: if (tmp_a1) BN_free(tmp_a1);
308 if (tmp_a2) BN_free(tmp_a2);
309 if (tmp_a3) BN_free(tmp_a3);
310 if (tmp_b1) BN_free(tmp_b1);
311 if (tmp_b2) BN_free(tmp_b2);
312 if (tmp_b3) BN_free(tmp_b3);
313 if (ctx) BN_CTX_free(ctx);
319 ECDSA *ecdsa=NULL, *ret_ecdsa=NULL;
321 X509_PUBKEY *x509_pubkey=NULL;
322 PKCS8_PRIV_KEY_INFO *pkcs8=NULL;
323 EVP_PKEY *pkey=NULL, *ret_pkey=NULL;
325 unsigned char *dgst=NULL;
328 unsigned char *buffer=NULL;
332 EVP_MD_CTX *md_ctx=NULL;
334 /* enable memory leak checking unless explicitly disabled */
335 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
337 CRYPTO_malloc_debug_init();
338 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
342 /* OPENSSL_DEBUG_MEMORY=off */
343 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
345 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
347 ERR_load_crypto_strings();
350 bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
352 RAND_seed(rnd_seed, sizeof(rnd_seed));
354 if ((ecdsa = ECDSA_new()) == NULL) goto err;
356 set_p192_param(ecdsa);
357 ECDSA_print(bio_err, ecdsa, 0);
359 /* en- decode tests */
361 /* i2d_ - d2i_ECDSAParameters() */
362 BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAParameters \n");
363 buf_len = i2d_ECDSAParameters(ecdsa, NULL);
364 if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
366 if (!i2d_ECDSAParameters(ecdsa, &pp)) goto err;
368 if ((ret_ecdsa = d2i_ECDSAParameters(&ret_ecdsa, (const unsigned char **)&pp,
369 buf_len)) == NULL) goto err;
370 ECDSAParameters_print(bio_err, ret_ecdsa);
371 if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
372 OPENSSL_free(buffer);
374 ECDSA_free(ret_ecdsa);
377 /* i2d_ - d2i_ECDSAPrivateKey() */
378 BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAPrivateKey \n");
379 buf_len = i2d_ECDSAPrivateKey(ecdsa, NULL);
380 if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
382 if (!i2d_ECDSAPrivateKey(ecdsa, &pp)) goto err;
384 if ((ret_ecdsa = d2i_ECDSAPrivateKey(&ret_ecdsa, (const unsigned char**)&pp,
385 buf_len)) == NULL) goto err;
386 ECDSA_print(bio_err, ret_ecdsa, 0);
387 if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
388 ECDSA_free(ret_ecdsa);
390 OPENSSL_free(buffer);
393 /* X509_PUBKEY_set() & X509_PUBKEY_get() */
395 BIO_printf(bio_err, "\nTesting X509_PUBKEY_{get,set} : ");
396 if ((pkey = EVP_PKEY_new()) == NULL) goto err;
397 EVP_PKEY_assign_ECDSA(pkey, ecdsa);
398 if ((x509_pubkey = X509_PUBKEY_new()) == NULL) goto err;
399 if (!X509_PUBKEY_set(&x509_pubkey, pkey)) goto err;
401 if ((ret_pkey = X509_PUBKEY_get(x509_pubkey)) == NULL) goto err;
402 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
403 EVP_PKEY_free(ret_pkey);
406 if (ecdsa_cmp(ecdsa, ret_ecdsa))
408 BIO_printf(bio_err, "TEST FAILED \n");
411 else BIO_printf(bio_err, "TEST OK \n");
412 X509_PUBKEY_free(x509_pubkey);
414 ECDSA_free(ret_ecdsa);
417 /* Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY */
418 BIO_printf(bio_err, "Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY : \n");
419 BIO_printf(bio_err, "PKCS8_OK : ");
420 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK)) == NULL) goto err;
421 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
422 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
423 if (ecdsa_cmp(ecdsa, ret_ecdsa))
425 BIO_printf(bio_err, "TEST FAILED \n");
428 else BIO_printf(bio_err, "TEST OK \n");
429 EVP_PKEY_free(ret_pkey);
431 ECDSA_free(ret_ecdsa);
433 PKCS8_PRIV_KEY_INFO_free(pkcs8);
434 BIO_printf(bio_err, "PKCS8_NO_OCTET : ");
435 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NO_OCTET)) == NULL) goto err;
436 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
437 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
438 if (ecdsa_cmp(ecdsa, ret_ecdsa))
440 BIO_printf(bio_err, "TEST FAILED \n");
443 else BIO_printf(bio_err, "TEST OK \n");
444 EVP_PKEY_free(ret_pkey);
446 ECDSA_free(ret_ecdsa);
448 PKCS8_PRIV_KEY_INFO_free(pkcs8);
449 BIO_printf(bio_err, "PKCS8_EMBEDDED_PARAM : ");
450 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_EMBEDDED_PARAM)) == NULL) goto err;
451 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
452 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
453 if (ecdsa_cmp(ecdsa, ret_ecdsa))
455 BIO_printf(bio_err, "TEST FAILED \n");
458 else BIO_printf(bio_err, "TEST OK \n");
459 EVP_PKEY_free(ret_pkey);
461 ECDSA_free(ret_ecdsa);
463 PKCS8_PRIV_KEY_INFO_free(pkcs8);
464 BIO_printf(bio_err, "PKCS8_NS_DB : ");
465 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NS_DB)) == NULL) goto err;
466 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
467 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
468 if (ecdsa_cmp(ecdsa, ret_ecdsa))
470 BIO_printf(bio_err, "TEST FAILED \n");
473 else BIO_printf(bio_err, "TEST OK \n");
474 EVP_PKEY_free(ret_pkey);
476 ECDSA_free(ret_ecdsa);
481 PKCS8_PRIV_KEY_INFO_free(pkcs8);
484 /* sign and verify tests */
485 if ((d = BN_new()) == NULL) goto err;
487 if (!BN_dec2bn(&d, "968236873715988614170569073515315707566766479517")) goto err;
488 dgst_len = BN_num_bytes(d);
489 if ((dgst = OPENSSL_malloc(dgst_len)) == NULL) goto err;
490 if (!BN_bn2bin(d, dgst)) goto err;
492 BIO_printf(bio_err, "Performing tests based on examples H.3.1 and H.3.2 of X9.62 \n");
494 BIO_printf(bio_err, "PRIME_192_V1 : ");
495 if ((ecdsa = ECDSA_new()) == NULL) goto err;
496 if (!set_p192_param(ecdsa)) goto err;
497 if (!test_x962_sig_vrf(ecdsa, dgst, "6140507067065001063065065565667405560006161556565665656654",
498 "3342403536405981729393488334694600415596881826869351677613",
499 "5735822328888155254683894997897571951568553642892029982342"))
502 BIO_printf(bio_err, "OK\n");
503 BIO_printf(bio_err, "PRIME_239_V1 : ");
504 if (!set_p239_param(ecdsa))
506 if (!test_x962_sig_vrf(ecdsa, dgst, "700000017569056646655505781757157107570501575775705779575555657156756655",
507 "308636143175167811492622547300668018854959378758531778147462058306432176",
508 "323813553209797357708078776831250505931891051755007842781978505179448783"))
511 BIO_printf(bio_err, "OK\n");
519 /* NIST PRIME CURVES TESTS */
520 /* EC_GROUP_NIST_PRIME_192 */
521 for (i=0; i<ECDSA_NIST_TESTS; i++)
522 if (!RAND_bytes(digest[i], 20)) goto err;
524 BIO_printf(bio_err, "\nTesting sign & verify with NIST Prime-Curve P-192 : \n");
526 if ((ecdsa = ECDSA_new()) == NULL) goto err;
527 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_192)) == NULL) goto err;
528 if (!ECDSA_generate_key(ecdsa)) goto err;
530 for (i=0; i<ECDSA_NIST_TESTS; i++)
531 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
533 tim_d = (double)tim / CLOCKS_PER_SEC;
534 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
535 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
537 for (i=0; i<ECDSA_NIST_TESTS; i++)
538 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
540 tim_d = (double)tim / CLOCKS_PER_SEC;
541 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
542 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
543 for (i=0; i<ECDSA_NIST_TESTS; i++)
545 ECDSA_SIG_free(signatures[i]);
546 signatures[i] = NULL;
549 /* EC_GROUP_NIST_PRIME_224 */
550 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-224 : \n");
552 if ((ecdsa = ECDSA_new()) == NULL) goto err;
553 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_224)) == NULL) goto err;
554 if (!ECDSA_generate_key(ecdsa)) goto err;
556 for (i=0; i<ECDSA_NIST_TESTS; i++)
557 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
559 tim_d = (double)tim / CLOCKS_PER_SEC;
560 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
561 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
563 for (i=0; i<ECDSA_NIST_TESTS; i++)
564 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
566 tim_d = (double)tim / CLOCKS_PER_SEC;
567 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
568 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
569 for (i=0; i<ECDSA_NIST_TESTS; i++)
571 ECDSA_SIG_free(signatures[i]);
572 signatures[i] = NULL;
575 /* EC_GROUP_NIST_PRIME_256 */
576 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-256 : \n");
578 if ((ecdsa = ECDSA_new()) == NULL) goto err;
579 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_256)) == NULL) goto err;
580 if (!ECDSA_generate_key(ecdsa)) goto err;
582 for (i=0; i<ECDSA_NIST_TESTS; i++)
583 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
585 tim_d = (double)tim / CLOCKS_PER_SEC;
586 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
587 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
589 for (i=0; i<ECDSA_NIST_TESTS; i++)
590 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
592 tim_d = (double)tim / CLOCKS_PER_SEC;
593 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
594 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
595 for (i=0; i<ECDSA_NIST_TESTS; i++)
597 ECDSA_SIG_free(signatures[i]);
598 signatures[i] = NULL;
601 /* EC_GROUP_NIST_PRIME_384 */
602 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-384 : \n");
604 if ((ecdsa = ECDSA_new()) == NULL) goto err;
605 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_384)) == NULL) goto err;
606 if (!ECDSA_generate_key(ecdsa)) goto err;
608 for (i=0; i<ECDSA_NIST_TESTS; i++)
609 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
611 tim_d = (double)tim / CLOCKS_PER_SEC;
612 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
613 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
615 for (i=0; i<ECDSA_NIST_TESTS; i++)
616 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
618 tim_d = (double)tim / CLOCKS_PER_SEC;
619 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
620 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
621 for (i=0; i<ECDSA_NIST_TESTS; i++)
623 ECDSA_SIG_free(signatures[i]);
624 signatures[i] = NULL;
627 /* EC_GROUP_NIST_PRIME_521 */
628 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-521 : \n");
630 if ((ecdsa = ECDSA_new()) == NULL) goto err;
631 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_521)) == NULL) goto err;
632 if (!ECDSA_generate_key(ecdsa)) goto err;
634 for (i=0; i<ECDSA_NIST_TESTS; i++)
635 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
637 tim_d = (double)tim / CLOCKS_PER_SEC;
638 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
639 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
641 for (i=0; i<ECDSA_NIST_TESTS; i++)
642 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
644 tim_d = (double)tim / CLOCKS_PER_SEC;
645 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
646 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
649 for (i=0; i<ECDSA_NIST_TESTS; i++)
651 ECDSA_SIG_free(signatures[i]);
652 signatures[i] = NULL;
655 OPENSSL_free(buffer);
663 BIO_printf(bio_err, "TEST FAILED \n");
665 BIO_printf(bio_err, "TEST PASSED \n");
667 ERR_print_errors(bio_err);
668 if (ecdsa) ECDSA_free(ecdsa);
670 if (dgst) OPENSSL_free(dgst);
671 if (md_ctx) EVP_MD_CTX_destroy(md_ctx);
672 CRYPTO_cleanup_all_ex_data();
675 CRYPTO_mem_leaks(bio_err);