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).
59 #include <openssl/crypto.h>
60 #include <openssl/bio.h>
61 #include <openssl/evp.h>
62 #include <openssl/x509.h>
63 #include <openssl/ecdsa.h>
64 #include <openssl/engine.h>
65 #include <openssl/err.h>
68 /* "To determine the time in seconds, the value returned
69 * by the clock function should be divided by the value
70 * of the macro CLOCKS_PER_SEC."
74 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
75 * -- cc on NeXTstep/OpenStep */
77 # define CLOCKS_PER_SEC 1
80 #ifdef OPENSSL_NO_ECDSA
81 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
84 static BIO *bio_err=NULL;
85 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
87 #define ECDSA_NIST_TESTS 10
88 ECDSA_SIG* signatures[ECDSA_NIST_TESTS];
89 unsigned char digest[ECDSA_NIST_TESTS][20];
91 void clear_ecdsa(ECDSA *ecdsa)
97 EC_GROUP_free(ecdsa->group);
102 EC_POINT_free(ecdsa->pub_key);
103 ecdsa->pub_key = NULL;
107 BN_free(ecdsa->priv_key);
108 ecdsa->priv_key = NULL;
112 int set_p192_param(ECDSA *ecdsa)
119 if ((ctx = BN_CTX_new()) == NULL) goto err;
122 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_192V1)) == NULL)
124 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_192_V1() failed \n");
127 if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
129 BIO_printf(bio_err,"EC_POINT_new failed \n");
133 if (!BN_dec2bn(&(ecdsa->priv_key), "651056770906015076056810763456358567190100156695615665659")) goto err;
134 if (!EC_POINT_mul(ecdsa->group,ecdsa->pub_key,ecdsa->priv_key,NULL,NULL,ctx))
136 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
141 err : if (ctx) BN_CTX_free(ctx);
145 int set_p239_param(ECDSA *ecdsa)
152 if ((ctx = BN_CTX_new()) == NULL) goto err;
155 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_239V1)) == NULL)
157 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_239_V1() failed \n");
160 if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
162 BIO_printf(bio_err,"EC_POINT_new failed \n");
166 if (!BN_dec2bn(&(ecdsa->priv_key), "876300101507107567501066130761671078357010671067781776716671676178726717")) goto err;
167 if (!EC_POINT_mul(ecdsa->group,ecdsa->pub_key,ecdsa->priv_key,NULL,NULL,ctx))
169 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
174 err : if (ctx) BN_CTX_free(ctx);
178 int test_sig_vrf(ECDSA *ecdsa, const unsigned char* dgst)
181 unsigned char *buffer=NULL;
182 unsigned int buf_len;
185 if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
187 if ((buf_len = ECDSA_size(ecdsa)) == 0)
189 BIO_printf(bio_err, "ECDSA_size() == 0 \n");
192 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
196 if (!ECDSA_sign(type, dgst , 20, buffer, &buf_len, ecdsa))
198 BIO_printf(bio_err, "ECDSA_sign() FAILED \n");
201 time = clock() - time;
202 BIO_printf(bio_err, " [ ECDSA_sign() %.2f"UNIT, (double)time/(CLOCKS_PER_SEC));
205 ret = ECDSA_verify(type, dgst, 20, buffer, buf_len, ecdsa);
208 BIO_printf(bio_err, "ECDSA_verify() FAILED \n");
211 time = clock() - time;
212 BIO_printf(bio_err, " and ECDSA_verify() %.2f"UNIT" ] ", (double)time/(CLOCKS_PER_SEC));
214 err: OPENSSL_free(buffer);
218 int test_x962_sig_vrf(ECDSA *ecdsa, const unsigned char *dgst,
219 const char *k_in, const char *r_in, const char *s_in)
223 EC_POINT *point=NULL;
224 BIGNUM *r=NULL,*s=NULL,*k=NULL,*x=NULL,*y=NULL,*m=NULL,*ord=NULL;
228 if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
230 if ((point = EC_POINT_new(ecdsa->group)) == NULL) goto err;
231 if ((r = BN_new()) == NULL || (s = BN_new()) == NULL || (k = BN_new()) == NULL ||
232 (x = BN_new()) == NULL || (y = BN_new()) == NULL || (m = BN_new()) == NULL ||
233 (ord = BN_new()) == NULL) goto err;
234 if ((ctx = BN_CTX_new()) == NULL) goto err;
235 if (!BN_bin2bn(dgst, 20, m)) goto err;
236 if (!BN_dec2bn(&k, k_in)) goto err;
237 if (!EC_POINT_mul(ecdsa->group, point, k, NULL, NULL, ctx)) goto err;
238 if (!EC_POINT_get_affine_coordinates_GFp(ecdsa->group, point, x, y, ctx)) goto err;
239 if (!EC_GROUP_get_order(ecdsa->group, ord, ctx)) goto err;
240 if ((ecdsa->r = BN_dup(x)) == NULL) goto err;
241 if ((ecdsa->kinv = BN_mod_inverse(NULL, k, ord, ctx)) == NULL) goto err;
243 if ((sig = ECDSA_do_sign(dgst, 20, ecdsa)) == NULL)
245 BIO_printf(bio_err,"ECDSA_do_sign() failed \n");
249 if (!BN_dec2bn(&r, r_in)) goto err;
250 if (!BN_dec2bn(&s, s_in)) goto err;
251 if (BN_cmp(sig->r,r) != 0 || BN_cmp(sig->s,s) != 0)
253 tmp_char = OPENSSL_malloc(128);
254 if (tmp_char == NULL) goto err;
255 tmp_char = BN_bn2dec(sig->r);
256 BIO_printf(bio_err,"unexpected signature \n");
257 BIO_printf(bio_err,"sig->r = %s\n",tmp_char);
258 tmp_char = BN_bn2dec(sig->s);
259 BIO_printf(bio_err,"sig->s = %s\n",tmp_char);
262 ret = ECDSA_do_verify(dgst, 20, sig, ecdsa);
265 BIO_printf(bio_err,"ECDSA_do_verify : signature verification failed \n");
270 err : if (r) BN_free(r);
276 if (ord) BN_free(ord);
277 if (sig) ECDSA_SIG_free(sig);
278 if (ctx) BN_CTX_free(ctx);
279 if (point) EC_POINT_free(point);
280 if (tmp_char) OPENSSL_free(tmp_char);
284 int ecdsa_cmp(const ECDSA *a, const ECDSA *b)
288 BIGNUM *tmp_a1=NULL, *tmp_a2=NULL, *tmp_a3=NULL;
289 BIGNUM *tmp_b1=NULL, *tmp_b2=NULL, *tmp_b3=NULL;
291 if ((ctx = BN_CTX_new()) == NULL) return 1;
292 if ((tmp_a1 = BN_new()) == NULL || (tmp_a2 = BN_new()) == NULL || (tmp_a3 = BN_new()) == NULL) goto err;
293 if ((tmp_b1 = BN_new()) == NULL || (tmp_b2 = BN_new()) == NULL || (tmp_b3 = BN_new()) == NULL) goto err;
295 if (a->pub_key && b->pub_key)
296 if (EC_POINT_cmp(a->group, a->pub_key, b->pub_key, ctx) != 0) goto err;
297 if (a->priv_key && b->priv_key)
298 if (BN_cmp(a->priv_key, b->priv_key) != 0) goto err;
299 if (!EC_GROUP_get_curve_GFp(a->group, tmp_a1, tmp_a2, tmp_a3, ctx)) goto err;
300 if (!EC_GROUP_get_curve_GFp(a->group, tmp_b1, tmp_b2, tmp_b3, ctx)) goto err;
301 if (BN_cmp(tmp_a1, tmp_b1) != 0) goto err;
302 if (BN_cmp(tmp_a2, tmp_b2) != 0) goto err;
303 if (BN_cmp(tmp_a3, tmp_b3) != 0) goto err;
306 err: if (tmp_a1) BN_free(tmp_a1);
307 if (tmp_a2) BN_free(tmp_a2);
308 if (tmp_a3) BN_free(tmp_a3);
309 if (tmp_b1) BN_free(tmp_b1);
310 if (tmp_b2) BN_free(tmp_b2);
311 if (tmp_b3) BN_free(tmp_b3);
312 if (ctx) BN_CTX_free(ctx);
318 ECDSA *ecdsa=NULL, *ret_ecdsa=NULL;
320 X509_PUBKEY *x509_pubkey=NULL;
321 PKCS8_PRIV_KEY_INFO *pkcs8=NULL;
322 EVP_PKEY *pkey=NULL, *ret_pkey=NULL;
324 unsigned char *dgst=NULL;
327 unsigned char *buffer=NULL;
331 EVP_MD_CTX *md_ctx=NULL;
333 /* enable memory leak checking unless explicitly disabled */
334 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
336 CRYPTO_malloc_debug_init();
337 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
341 /* OPENSSL_DEBUG_MEMORY=off */
342 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
344 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
346 ERR_load_crypto_strings();
349 bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
351 RAND_seed(rnd_seed, sizeof(rnd_seed));
353 if ((ecdsa = ECDSA_new()) == NULL) goto err;
355 set_p192_param(ecdsa);
356 ECDSA_print(bio_err, ecdsa, 0);
358 /* en- decode tests */
360 /* i2d_ - d2i_ECDSAParameters() */
361 BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAParameters \n");
362 buf_len = i2d_ECDSAParameters(ecdsa, NULL);
363 if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
365 if (!i2d_ECDSAParameters(ecdsa, &pp)) goto err;
367 if ((ret_ecdsa = d2i_ECDSAParameters(&ret_ecdsa, (const unsigned char **)&pp,
368 buf_len)) == NULL) goto err;
369 ECDSAParameters_print(bio_err, ret_ecdsa);
370 if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
371 OPENSSL_free(buffer);
373 ECDSA_free(ret_ecdsa);
376 /* i2d_ - d2i_ECDSAPrivateKey() */
377 BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAPrivateKey \n");
378 buf_len = i2d_ECDSAPrivateKey(ecdsa, NULL);
379 if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
381 if (!i2d_ECDSAPrivateKey(ecdsa, &pp)) goto err;
383 if ((ret_ecdsa = d2i_ECDSAPrivateKey(&ret_ecdsa, (const unsigned char**)&pp,
384 buf_len)) == NULL) goto err;
385 ECDSA_print(bio_err, ret_ecdsa, 0);
386 if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
387 ECDSA_free(ret_ecdsa);
389 OPENSSL_free(buffer);
392 /* i2d_ - d2i_ECDSAPublicKey() */
394 BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAPublicKey \n");
395 buf_len = i2d_ECDSAPublicKey(ecdsa, NULL);
396 if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
398 if (!i2d_ECDSAPublicKey(ecdsa, &pp)) goto err;
400 if ((ret_ecdsa = d2i_ECDSAPublicKey(&ret_ecdsa , (const unsigned char**)&pp,
401 buf_len)) == NULL) goto err;
402 ECDSA_print(bio_err, ret_ecdsa, 0);
403 if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
404 OPENSSL_free(buffer);
406 ECDSA_free(ret_ecdsa);
409 /* X509_PUBKEY_set() & X509_PUBKEY_get() */
411 BIO_printf(bio_err, "\nTesting X509_PUBKEY_{get,set} : ");
412 if ((pkey = EVP_PKEY_new()) == NULL) goto err;
413 EVP_PKEY_assign_ECDSA(pkey, ecdsa);
414 if ((x509_pubkey = X509_PUBKEY_new()) == NULL) goto err;
415 if (!X509_PUBKEY_set(&x509_pubkey, pkey)) goto err;
417 if ((ret_pkey = X509_PUBKEY_get(x509_pubkey)) == NULL) goto err;
418 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
419 EVP_PKEY_free(ret_pkey);
422 if (ecdsa_cmp(ecdsa, ret_ecdsa))
424 BIO_printf(bio_err, "TEST FAILED \n");
427 else BIO_printf(bio_err, "TEST OK \n");
428 X509_PUBKEY_free(x509_pubkey);
430 ECDSA_free(ret_ecdsa);
433 /* Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY */
434 BIO_printf(bio_err, "Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY : \n");
435 BIO_printf(bio_err, "PKCS8_OK : ");
436 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK)) == NULL) goto err;
437 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
438 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
439 if (ecdsa_cmp(ecdsa, ret_ecdsa))
441 BIO_printf(bio_err, "TEST FAILED \n");
444 else BIO_printf(bio_err, "TEST OK \n");
445 EVP_PKEY_free(ret_pkey);
447 ECDSA_free(ret_ecdsa);
449 PKCS8_PRIV_KEY_INFO_free(pkcs8);
450 BIO_printf(bio_err, "PKCS8_NO_OCTET : ");
451 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NO_OCTET)) == NULL) goto err;
452 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
453 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
454 if (ecdsa_cmp(ecdsa, ret_ecdsa))
456 BIO_printf(bio_err, "TEST FAILED \n");
459 else BIO_printf(bio_err, "TEST OK \n");
460 EVP_PKEY_free(ret_pkey);
462 ECDSA_free(ret_ecdsa);
464 PKCS8_PRIV_KEY_INFO_free(pkcs8);
465 BIO_printf(bio_err, "PKCS8_EMBEDDED_PARAM : ");
466 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_EMBEDDED_PARAM)) == NULL) goto err;
467 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
468 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
469 if (ecdsa_cmp(ecdsa, ret_ecdsa))
471 BIO_printf(bio_err, "TEST FAILED \n");
474 else BIO_printf(bio_err, "TEST OK \n");
475 EVP_PKEY_free(ret_pkey);
477 ECDSA_free(ret_ecdsa);
479 PKCS8_PRIV_KEY_INFO_free(pkcs8);
480 BIO_printf(bio_err, "PKCS8_NS_DB : ");
481 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NS_DB)) == NULL) goto err;
482 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
483 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
484 if (ecdsa_cmp(ecdsa, ret_ecdsa))
486 BIO_printf(bio_err, "TEST FAILED \n");
489 else BIO_printf(bio_err, "TEST OK \n");
490 EVP_PKEY_free(ret_pkey);
492 ECDSA_free(ret_ecdsa);
497 PKCS8_PRIV_KEY_INFO_free(pkcs8);
500 /* sign and verify tests */
501 if ((d = BN_new()) == NULL) goto err;
503 if (!BN_dec2bn(&d, "968236873715988614170569073515315707566766479517")) goto err;
504 dgst_len = BN_num_bytes(d);
505 if ((dgst = OPENSSL_malloc(dgst_len)) == NULL) goto err;
506 if (!BN_bn2bin(d, dgst)) goto err;
508 BIO_printf(bio_err, "Performing tests based on examples H.3.1 and H.3.2 of X9.62 \n");
510 BIO_printf(bio_err, "PRIME_192_V1 : ");
511 if ((ecdsa = ECDSA_new()) == NULL) goto err;
512 if (!set_p192_param(ecdsa)) goto err;
513 if (!test_x962_sig_vrf(ecdsa, dgst, "6140507067065001063065065565667405560006161556565665656654",
514 "3342403536405981729393488334694600415596881826869351677613",
515 "5735822328888155254683894997897571951568553642892029982342"))
518 BIO_printf(bio_err, "OK\n");
519 BIO_printf(bio_err, "PRIME_239_V1 : ");
520 if (!set_p239_param(ecdsa))
522 if (!test_x962_sig_vrf(ecdsa, dgst, "700000017569056646655505781757157107570501575775705779575555657156756655",
523 "308636143175167811492622547300668018854959378758531778147462058306432176",
524 "323813553209797357708078776831250505931891051755007842781978505179448783"))
527 BIO_printf(bio_err, "OK\n");
535 /* NIST PRIME CURVES TESTS */
536 /* EC_GROUP_NIST_PRIME_192 */
537 for (i=0; i<ECDSA_NIST_TESTS; i++)
538 if (!RAND_bytes(digest[i], 20)) goto err;
540 BIO_printf(bio_err, "\nTesting sign & verify with NIST Prime-Curve P-192 : \n");
542 if ((ecdsa = ECDSA_new()) == NULL) goto err;
543 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_192)) == NULL) goto err;
544 if (!ECDSA_generate_key(ecdsa)) goto err;
546 for (i=0; i<ECDSA_NIST_TESTS; i++)
547 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
548 time = clock() - time;
549 time_d = (double)time / CLOCKS_PER_SEC;
550 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
551 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
553 for (i=0; i<ECDSA_NIST_TESTS; i++)
554 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
555 time = clock() - time;
556 time_d = (double)time / CLOCKS_PER_SEC;
557 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
558 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
559 for (i=0; i<ECDSA_NIST_TESTS; i++)
561 ECDSA_SIG_free(signatures[i]);
562 signatures[i] = NULL;
565 /* EC_GROUP_NIST_PRIME_224 */
566 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-224 : \n");
568 if ((ecdsa = ECDSA_new()) == NULL) goto err;
569 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_224)) == NULL) goto err;
570 if (!ECDSA_generate_key(ecdsa)) goto err;
572 for (i=0; i<ECDSA_NIST_TESTS; i++)
573 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
574 time = clock() - time;
575 time_d = (double)time / CLOCKS_PER_SEC;
576 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
577 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
579 for (i=0; i<ECDSA_NIST_TESTS; i++)
580 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
581 time = clock() - time;
582 time_d = (double)time / CLOCKS_PER_SEC;
583 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
584 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
585 for (i=0; i<ECDSA_NIST_TESTS; i++)
587 ECDSA_SIG_free(signatures[i]);
588 signatures[i] = NULL;
591 /* EC_GROUP_NIST_PRIME_256 */
592 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-256 : \n");
594 if ((ecdsa = ECDSA_new()) == NULL) goto err;
595 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_256)) == NULL) goto err;
596 if (!ECDSA_generate_key(ecdsa)) goto err;
598 for (i=0; i<ECDSA_NIST_TESTS; i++)
599 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
600 time = clock() - time;
601 time_d = (double)time / CLOCKS_PER_SEC;
602 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
603 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
605 for (i=0; i<ECDSA_NIST_TESTS; i++)
606 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
607 time = clock() - time;
608 time_d = (double)time / CLOCKS_PER_SEC;
609 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
610 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
611 for (i=0; i<ECDSA_NIST_TESTS; i++)
613 ECDSA_SIG_free(signatures[i]);
614 signatures[i] = NULL;
617 /* EC_GROUP_NIST_PRIME_384 */
618 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-384 : \n");
620 if ((ecdsa = ECDSA_new()) == NULL) goto err;
621 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_384)) == NULL) goto err;
622 if (!ECDSA_generate_key(ecdsa)) goto err;
624 for (i=0; i<ECDSA_NIST_TESTS; i++)
625 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
626 time = clock() - time;
627 time_d = (double)time / CLOCKS_PER_SEC;
628 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
629 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
631 for (i=0; i<ECDSA_NIST_TESTS; i++)
632 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
633 time = clock() - time;
634 time_d = (double)time / CLOCKS_PER_SEC;
635 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
636 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
637 for (i=0; i<ECDSA_NIST_TESTS; i++)
639 ECDSA_SIG_free(signatures[i]);
640 signatures[i] = NULL;
643 /* EC_GROUP_NIST_PRIME_521 */
644 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-521 : \n");
646 if ((ecdsa = ECDSA_new()) == NULL) goto err;
647 if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_521)) == NULL) goto err;
648 if (!ECDSA_generate_key(ecdsa)) goto err;
650 for (i=0; i<ECDSA_NIST_TESTS; i++)
651 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
652 time = clock() - time;
653 time_d = (double)time / CLOCKS_PER_SEC;
654 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
655 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
657 for (i=0; i<ECDSA_NIST_TESTS; i++)
658 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
659 time = clock() - time;
660 time_d = (double)time / CLOCKS_PER_SEC;
661 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
662 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
665 for (i=0; i<ECDSA_NIST_TESTS; i++)
667 ECDSA_SIG_free(signatures[i]);
668 signatures[i] = NULL;
671 OPENSSL_free(buffer);
679 BIO_printf(bio_err, "TEST FAILED \n");
681 BIO_printf(bio_err, "TEST PASSED \n");
683 ERR_print_errors(bio_err);
684 if (ecdsa) ECDSA_free(ecdsa);
686 if (dgst) OPENSSL_free(dgst);
687 if (md_ctx) EVP_MD_CTX_destroy(md_ctx);
688 CRYPTO_cleanup_all_ex_data();
691 CRYPTO_mem_leaks(bio_err);