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_get_group_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_get_group_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;
334 RAND_seed(rnd_seed, sizeof(rnd_seed));
337 bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
339 CRYPTO_malloc_debug_init();
340 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
341 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
343 ERR_load_crypto_strings();
345 if ((ecdsa = ECDSA_new()) == NULL) goto err;
347 set_p192_param(ecdsa);
348 ECDSA_print(bio_err, ecdsa, 0);
350 /* en- decode tests */
352 /* i2d_ - d2i_ECDSAParameters() */
353 BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAParameters \n");
354 buf_len = i2d_ECDSAParameters(ecdsa, NULL);
355 if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
357 if (!i2d_ECDSAParameters(ecdsa, &pp)) goto err;
359 if ((ret_ecdsa = d2i_ECDSAParameters(&ret_ecdsa, (const unsigned char **)&pp,
360 buf_len)) == NULL) goto err;
361 ECDSAParameters_print(bio_err, ret_ecdsa);
362 if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
363 OPENSSL_free(buffer);
365 ECDSA_free(ret_ecdsa);
368 /* i2d_ - d2i_ECDSAPrivateKey() */
369 BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAPrivateKey \n");
370 buf_len = i2d_ECDSAPrivateKey(ecdsa, NULL);
371 if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
373 if (!i2d_ECDSAPrivateKey(ecdsa, &pp)) goto err;
375 if ((ret_ecdsa = d2i_ECDSAPrivateKey(&ret_ecdsa, (const unsigned char**)&pp,
376 buf_len)) == NULL) goto err;
377 ECDSA_print(bio_err, ret_ecdsa, 0);
378 if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
379 ECDSA_free(ret_ecdsa);
381 OPENSSL_free(buffer);
384 /* i2d_ - d2i_ECDSAPublicKey() */
386 BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAPublicKey \n");
387 buf_len = i2d_ECDSAPublicKey(ecdsa, NULL);
388 if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
390 if (!i2d_ECDSAPublicKey(ecdsa, &pp)) goto err;
392 if ((ret_ecdsa = d2i_ECDSAPublicKey(&ret_ecdsa , (const unsigned char**)&pp,
393 buf_len)) == NULL) goto err;
394 ECDSA_print(bio_err, ret_ecdsa, 0);
395 if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
396 OPENSSL_free(buffer);
398 ECDSA_free(ret_ecdsa);
401 /* X509_PUBKEY_set() & X509_PUBKEY_get() */
403 BIO_printf(bio_err, "\nTesting X509_PUBKEY_{get,set} : ");
404 if ((pkey = EVP_PKEY_new()) == NULL) goto err;
405 EVP_PKEY_assign_ECDSA(pkey, ecdsa);
406 if ((x509_pubkey = X509_PUBKEY_new()) == NULL) goto err;
407 if (!X509_PUBKEY_set(&x509_pubkey, pkey)) goto err;
409 if ((ret_pkey = X509_PUBKEY_get(x509_pubkey)) == NULL) goto err;
410 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
411 EVP_PKEY_free(ret_pkey);
414 if (ecdsa_cmp(ecdsa, ret_ecdsa))
416 BIO_printf(bio_err, "TEST FAILED \n");
419 else BIO_printf(bio_err, "TEST OK \n");
420 X509_PUBKEY_free(x509_pubkey);
422 ECDSA_free(ret_ecdsa);
425 /* Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY */
426 BIO_printf(bio_err, "Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY : \n");
427 BIO_printf(bio_err, "PKCS8_OK : ");
428 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK)) == NULL) goto err;
429 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
430 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
431 if (ecdsa_cmp(ecdsa, ret_ecdsa))
433 BIO_printf(bio_err, "TEST FAILED \n");
436 else BIO_printf(bio_err, "TEST OK \n");
437 EVP_PKEY_free(ret_pkey);
439 ECDSA_free(ret_ecdsa);
441 PKCS8_PRIV_KEY_INFO_free(pkcs8);
442 BIO_printf(bio_err, "PKCS8_NO_OCTET : ");
443 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NO_OCTET)) == NULL) goto err;
444 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
445 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
446 if (ecdsa_cmp(ecdsa, ret_ecdsa))
448 BIO_printf(bio_err, "TEST FAILED \n");
451 else BIO_printf(bio_err, "TEST OK \n");
452 EVP_PKEY_free(ret_pkey);
454 ECDSA_free(ret_ecdsa);
456 PKCS8_PRIV_KEY_INFO_free(pkcs8);
457 BIO_printf(bio_err, "PKCS8_EMBEDDED_PARAM : ");
458 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_EMBEDDED_PARAM)) == NULL) goto err;
459 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
460 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
461 if (ecdsa_cmp(ecdsa, ret_ecdsa))
463 BIO_printf(bio_err, "TEST FAILED \n");
466 else BIO_printf(bio_err, "TEST OK \n");
467 EVP_PKEY_free(ret_pkey);
469 ECDSA_free(ret_ecdsa);
471 PKCS8_PRIV_KEY_INFO_free(pkcs8);
472 BIO_printf(bio_err, "PKCS8_NS_DB : ");
473 if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NS_DB)) == NULL) goto err;
474 if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
475 ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
476 if (ecdsa_cmp(ecdsa, ret_ecdsa))
478 BIO_printf(bio_err, "TEST FAILED \n");
481 else BIO_printf(bio_err, "TEST OK \n");
482 EVP_PKEY_free(ret_pkey);
484 ECDSA_free(ret_ecdsa);
489 PKCS8_PRIV_KEY_INFO_free(pkcs8);
492 /* sign and verify tests */
493 if ((d = BN_new()) == NULL) goto err;
495 if (!BN_dec2bn(&d, "968236873715988614170569073515315707566766479517")) goto err;
496 dgst_len = BN_num_bytes(d);
497 if ((dgst = OPENSSL_malloc(dgst_len)) == NULL) goto err;
498 if (!BN_bn2bin(d, dgst)) goto err;
500 BIO_printf(bio_err, "Performing tests based on examples H.3.1 and H.3.2 of X9.62 \n");
502 BIO_printf(bio_err, "PRIME_192_V1 : ");
503 if ((ecdsa = ECDSA_new()) == NULL) goto err;
504 if (!set_p192_param(ecdsa)) goto err;
505 if (!test_x962_sig_vrf(ecdsa, dgst, "6140507067065001063065065565667405560006161556565665656654",
506 "3342403536405981729393488334694600415596881826869351677613",
507 "5735822328888155254683894997897571951568553642892029982342"))
510 BIO_printf(bio_err, "OK\n");
511 BIO_printf(bio_err, "PRIME_239_V1 : ");
512 if (!set_p239_param(ecdsa))
514 if (!test_x962_sig_vrf(ecdsa, dgst, "700000017569056646655505781757157107570501575775705779575555657156756655",
515 "308636143175167811492622547300668018854959378758531778147462058306432176",
516 "323813553209797357708078776831250505931891051755007842781978505179448783"))
519 BIO_printf(bio_err, "OK\n");
527 /* NIST PRIME CURVES TESTS */
528 /* EC_GROUP_NIST_PRIME_192 */
529 for (i=0; i<ECDSA_NIST_TESTS; i++)
530 if (!RAND_bytes(digest[i], 20)) goto err;
532 BIO_printf(bio_err, "\nTesting sign & verify with NIST Prime-Curve P-192 : \n");
534 if ((ecdsa = ECDSA_new()) == NULL) goto err;
535 if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_192)) == NULL) goto err;
536 if (!ECDSA_generate_key(ecdsa)) goto err;
538 for (i=0; i<ECDSA_NIST_TESTS; i++)
539 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
540 time = clock() - time;
541 time_d = (double)time / CLOCKS_PER_SEC;
542 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
543 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
545 for (i=0; i<ECDSA_NIST_TESTS; i++)
546 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
547 time = clock() - time;
548 time_d = (double)time / CLOCKS_PER_SEC;
549 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
550 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
551 for (i=0; i<ECDSA_NIST_TESTS; i++)
553 ECDSA_SIG_free(signatures[i]);
554 signatures[i] = NULL;
557 /* EC_GROUP_NIST_PRIME_224 */
558 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-224 : \n");
560 if ((ecdsa = ECDSA_new()) == NULL) goto err;
561 if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_224)) == NULL) goto err;
562 if (!ECDSA_generate_key(ecdsa)) goto err;
564 for (i=0; i<ECDSA_NIST_TESTS; i++)
565 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
566 time = clock() - time;
567 time_d = (double)time / CLOCKS_PER_SEC;
568 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
569 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
571 for (i=0; i<ECDSA_NIST_TESTS; i++)
572 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
573 time = clock() - time;
574 time_d = (double)time / CLOCKS_PER_SEC;
575 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
576 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
577 for (i=0; i<ECDSA_NIST_TESTS; i++)
579 ECDSA_SIG_free(signatures[i]);
580 signatures[i] = NULL;
583 /* EC_GROUP_NIST_PRIME_256 */
584 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-256 : \n");
586 if ((ecdsa = ECDSA_new()) == NULL) goto err;
587 if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_256)) == NULL) goto err;
588 if (!ECDSA_generate_key(ecdsa)) goto err;
590 for (i=0; i<ECDSA_NIST_TESTS; i++)
591 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
592 time = clock() - time;
593 time_d = (double)time / CLOCKS_PER_SEC;
594 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
595 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
597 for (i=0; i<ECDSA_NIST_TESTS; i++)
598 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
599 time = clock() - time;
600 time_d = (double)time / CLOCKS_PER_SEC;
601 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
602 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
603 for (i=0; i<ECDSA_NIST_TESTS; i++)
605 ECDSA_SIG_free(signatures[i]);
606 signatures[i] = NULL;
609 /* EC_GROUP_NIST_PRIME_384 */
610 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-384 : \n");
612 if ((ecdsa = ECDSA_new()) == NULL) goto err;
613 if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_384)) == NULL) goto err;
614 if (!ECDSA_generate_key(ecdsa)) goto err;
616 for (i=0; i<ECDSA_NIST_TESTS; i++)
617 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
618 time = clock() - time;
619 time_d = (double)time / CLOCKS_PER_SEC;
620 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
621 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
623 for (i=0; i<ECDSA_NIST_TESTS; i++)
624 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
625 time = clock() - time;
626 time_d = (double)time / CLOCKS_PER_SEC;
627 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
628 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
629 for (i=0; i<ECDSA_NIST_TESTS; i++)
631 ECDSA_SIG_free(signatures[i]);
632 signatures[i] = NULL;
635 /* EC_GROUP_NIST_PRIME_521 */
636 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-521 : \n");
638 if ((ecdsa = ECDSA_new()) == NULL) goto err;
639 if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_521)) == NULL) goto err;
640 if (!ECDSA_generate_key(ecdsa)) goto err;
642 for (i=0; i<ECDSA_NIST_TESTS; i++)
643 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
644 time = clock() - time;
645 time_d = (double)time / CLOCKS_PER_SEC;
646 BIO_printf(bio_err, "%d x ECDSA_do_sign() in %.2f"UNIT" => average time for ECDSA_do_sign() %.4f"UNIT"\n"
647 , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
649 for (i=0; i<ECDSA_NIST_TESTS; i++)
650 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
651 time = clock() - time;
652 time_d = (double)time / CLOCKS_PER_SEC;
653 BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
654 , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
657 for (i=0; i<ECDSA_NIST_TESTS; i++)
659 ECDSA_SIG_free(signatures[i]);
660 signatures[i] = NULL;
663 OPENSSL_free(buffer);
671 BIO_printf(bio_err, "TEST FAILED \n");
673 BIO_printf(bio_err, "TEST PASSED \n");
675 ERR_print_errors(bio_err);
676 if (ecdsa) ECDSA_free(ecdsa);
678 if (dgst) OPENSSL_free(dgst);
679 if (md_ctx) EVP_MD_CTX_destroy(md_ctx);
680 CRYPTO_cleanup_all_ex_data();
683 CRYPTO_mem_leaks(bio_err);