]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ecdsa/ecdsatest.c
ECDSA support
[thirdparty/openssl.git] / crypto / ecdsa / ecdsatest.c
1 /* crypto/ecdsa/ecdsatest.c */
2 /* ====================================================================
3 * Copyright (c) 2000-2002 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
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
15 * distribution.
16 *
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/)"
21 *
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.
26 *
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.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
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 * ====================================================================
49 *
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).
53 *
54 */
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <time.h>
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>
66
67 #ifdef CLOCKS_PER_SEC
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."
71 * -- ISO/IEC 9899 */
72 # define UNIT "s"
73 #else
74 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
75 * -- cc on NeXTstep/OpenStep */
76 # define UNIT "units"
77 # define CLOCKS_PER_SEC 1
78 #endif
79
80 #ifdef OPENSSL_NO_ECDSA
81 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
82 #else
83
84 static BIO *bio_err=NULL;
85 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
86
87 #define ECDSA_NIST_TESTS 10
88 ECDSA_SIG* signatures[ECDSA_NIST_TESTS];
89 unsigned char digest[ECDSA_NIST_TESTS][20];
90
91 void clear_ecdsa(ECDSA *ecdsa)
92 {
93 if (!ecdsa)
94 return;
95 if (ecdsa->group)
96 {
97 EC_GROUP_free(ecdsa->group);
98 ecdsa->group = NULL;
99 }
100 if (ecdsa->pub_key)
101 {
102 EC_POINT_free(ecdsa->pub_key);
103 ecdsa->pub_key = NULL;
104 }
105 if (ecdsa->priv_key)
106 {
107 BN_free(ecdsa->priv_key);
108 ecdsa->priv_key = NULL;
109 }
110 }
111
112 int set_p192_param(ECDSA *ecdsa)
113 {
114 BN_CTX *ctx=NULL;
115 int ret=0;
116
117 if (!ecdsa)
118 return 0;
119 if ((ctx = BN_CTX_new()) == NULL) goto err;
120 clear_ecdsa(ecdsa);
121
122 if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_X9_62_PRIME_192V1)) == NULL)
123 {
124 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_192_V1() failed \n");
125 goto err;
126 }
127 if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
128 {
129 BIO_printf(bio_err,"EC_POINT_new failed \n");
130 goto err;
131 }
132
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))
135 {
136 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
137 goto err;
138 }
139 ret = 1;
140
141 err : if (ctx) BN_CTX_free(ctx);
142 return ret;
143 }
144
145 int set_p239_param(ECDSA *ecdsa)
146 {
147 BN_CTX *ctx=NULL;
148 int ret=0;
149
150 if (!ecdsa)
151 return 0;
152 if ((ctx = BN_CTX_new()) == NULL) goto err;
153 clear_ecdsa(ecdsa);
154
155 if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_X9_62_PRIME_239V1)) == NULL)
156 {
157 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_239_V1() failed \n");
158 goto err;
159 }
160 if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
161 {
162 BIO_printf(bio_err,"EC_POINT_new failed \n");
163 goto err;
164 }
165
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))
168 {
169 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
170 goto err;
171 }
172 ret = 1;
173
174 err : if (ctx) BN_CTX_free(ctx);
175 return ret;
176 }
177
178 int test_sig_vrf(ECDSA *ecdsa, const unsigned char* dgst)
179 {
180 int ret=0,type=0;
181 unsigned char *buffer=NULL;
182 unsigned int buf_len;
183 clock_t time;
184
185 if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
186 return 0;
187 if ((buf_len = ECDSA_size(ecdsa)) == 0)
188 {
189 BIO_printf(bio_err, "ECDSA_size() == 0 \n");
190 goto err;
191 }
192 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
193 goto err;
194
195 time = clock();
196 if (!ECDSA_sign(type, dgst , 20, buffer, &buf_len, ecdsa))
197 {
198 BIO_printf(bio_err, "ECDSA_sign() FAILED \n");
199 goto err;
200 }
201 time = clock() - time;
202 BIO_printf(bio_err, " [ ECDSA_sign() %.2f"UNIT, (double)time/(CLOCKS_PER_SEC));
203
204 time = clock();
205 ret = ECDSA_verify(type, dgst, 20, buffer, buf_len, ecdsa);
206 if (ret != 1)
207 {
208 BIO_printf(bio_err, "ECDSA_verify() FAILED \n");
209 goto err;
210 }
211 time = clock() - time;
212 BIO_printf(bio_err, " and ECDSA_verify() %.2f"UNIT" ] ", (double)time/(CLOCKS_PER_SEC));
213
214 err: OPENSSL_free(buffer);
215 return(ret == 1);
216 }
217
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)
220 {
221 int ret=0;
222 ECDSA_SIG *sig=NULL;
223 EC_POINT *point=NULL;
224 BIGNUM *r=NULL,*s=NULL,*k=NULL,*x=NULL,*y=NULL,*m=NULL,*ord=NULL;
225 BN_CTX *ctx=NULL;
226 char *tmp_char=NULL;
227
228 if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
229 return 0;
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;
242
243 if ((sig = ECDSA_do_sign(dgst, 20, ecdsa)) == NULL)
244 {
245 BIO_printf(bio_err,"ECDSA_do_sign() failed \n");
246 goto err;
247 }
248
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)
252 {
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);
260 goto err;
261 }
262 ret = ECDSA_do_verify(dgst, 20, sig, ecdsa);
263 if (ret != 1)
264 {
265 BIO_printf(bio_err,"ECDSA_do_verify : signature verification failed \n");
266 goto err;
267 }
268
269 ret = 1;
270 err : if (r) BN_free(r);
271 if (s) BN_free(s);
272 if (k) BN_free(k);
273 if (x) BN_free(x);
274 if (y) BN_free(y);
275 if (m) BN_free(m);
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);
281 return(ret == 1);
282 }
283
284 int ecdsa_cmp(const ECDSA *a, const ECDSA *b)
285 {
286 int ret=1;
287 BN_CTX *ctx=NULL;
288 BIGNUM *tmp_a1=NULL, *tmp_a2=NULL, *tmp_a3=NULL;
289 BIGNUM *tmp_b1=NULL, *tmp_b2=NULL, *tmp_b3=NULL;
290
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;
294
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;
304
305 ret = 0;
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);
313 return(ret);
314 }
315
316 int main(void)
317 {
318 ECDSA *ecdsa=NULL, *ret_ecdsa=NULL;
319 BIGNUM *d=NULL;
320 X509_PUBKEY *x509_pubkey=NULL;
321 PKCS8_PRIV_KEY_INFO *pkcs8=NULL;
322 EVP_PKEY *pkey=NULL, *ret_pkey=NULL;
323 int dgst_len=0;
324 unsigned char *dgst=NULL;
325 int ret = 0, i=0;
326 clock_t time;
327 unsigned char *buffer=NULL;
328 unsigned char *pp;
329 long buf_len=0;
330 double time_d;
331 EVP_MD_CTX *md_ctx=NULL;
332
333
334 RAND_seed(rnd_seed, sizeof(rnd_seed));
335
336 if (bio_err == NULL)
337 bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
338
339 CRYPTO_malloc_debug_init();
340 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
341 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
342
343 ERR_load_crypto_strings();
344
345 if ((ecdsa = ECDSA_new()) == NULL) goto err;
346
347 set_p192_param(ecdsa);
348 ECDSA_print(bio_err, ecdsa, 0);
349
350 /* en- decode tests */
351
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;
356 pp = buffer;
357 if (!i2d_ECDSAParameters(ecdsa, &pp)) goto err;
358 pp = buffer;
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);
364 buffer = NULL;
365 ECDSA_free(ret_ecdsa);
366 ret_ecdsa = NULL;
367
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;
372 pp = buffer;
373 if (!i2d_ECDSAPrivateKey(ecdsa, &pp)) goto err;
374 pp = buffer;
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);
380 ret_ecdsa = NULL;
381 OPENSSL_free(buffer);
382 buffer = NULL;
383
384 /* i2d_ - d2i_ECDSAPublicKey() */
385
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;
389 pp = buffer;
390 if (!i2d_ECDSAPublicKey(ecdsa, &pp)) goto err;
391 pp = buffer;
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);
397 buffer = NULL;
398 ECDSA_free(ret_ecdsa);
399 ret_ecdsa = NULL;
400
401 /* X509_PUBKEY_set() & X509_PUBKEY_get() */
402
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;
408
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);
412 ret_pkey = NULL;
413
414 if (ecdsa_cmp(ecdsa, ret_ecdsa))
415 {
416 BIO_printf(bio_err, "TEST FAILED \n");
417 goto err;
418 }
419 else BIO_printf(bio_err, "TEST OK \n");
420 X509_PUBKEY_free(x509_pubkey);
421 x509_pubkey = NULL;
422 ECDSA_free(ret_ecdsa);
423 ret_ecdsa = NULL;
424
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))
432 {
433 BIO_printf(bio_err, "TEST FAILED \n");
434 goto err;
435 }
436 else BIO_printf(bio_err, "TEST OK \n");
437 EVP_PKEY_free(ret_pkey);
438 ret_pkey = NULL;
439 ECDSA_free(ret_ecdsa);
440 ret_ecdsa = NULL;
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))
447 {
448 BIO_printf(bio_err, "TEST FAILED \n");
449 goto err;
450 }
451 else BIO_printf(bio_err, "TEST OK \n");
452 EVP_PKEY_free(ret_pkey);
453 ret_pkey = NULL;
454 ECDSA_free(ret_ecdsa);
455 ret_ecdsa = NULL;
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))
462 {
463 BIO_printf(bio_err, "TEST FAILED \n");
464 goto err;
465 }
466 else BIO_printf(bio_err, "TEST OK \n");
467 EVP_PKEY_free(ret_pkey);
468 ret_pkey = NULL;
469 ECDSA_free(ret_ecdsa);
470 ret_ecdsa = NULL;
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))
477 {
478 BIO_printf(bio_err, "TEST FAILED \n");
479 goto err;
480 }
481 else BIO_printf(bio_err, "TEST OK \n");
482 EVP_PKEY_free(ret_pkey);
483 ret_pkey = NULL;
484 ECDSA_free(ret_ecdsa);
485 ret_ecdsa = NULL;
486 EVP_PKEY_free(pkey);
487 pkey = NULL;
488 ecdsa = NULL;
489 PKCS8_PRIV_KEY_INFO_free(pkcs8);
490 pkcs8 = NULL;
491
492 /* sign and verify tests */
493 if ((d = BN_new()) == NULL) goto err;
494
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;
499
500 BIO_printf(bio_err, "Performing tests based on examples H.3.1 and H.3.2 of X9.62 \n");
501
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"))
508 goto err;
509 else
510 BIO_printf(bio_err, "OK\n");
511 BIO_printf(bio_err, "PRIME_239_V1 : ");
512 if (!set_p239_param(ecdsa))
513 goto err;
514 if (!test_x962_sig_vrf(ecdsa, dgst, "700000017569056646655505781757157107570501575775705779575555657156756655",
515 "308636143175167811492622547300668018854959378758531778147462058306432176",
516 "323813553209797357708078776831250505931891051755007842781978505179448783"))
517 goto err;
518 else
519 BIO_printf(bio_err, "OK\n");
520
521 ECDSA_free(ecdsa);
522 ecdsa = NULL;
523 OPENSSL_free(dgst);
524 dgst = NULL;
525
526
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;
531
532 BIO_printf(bio_err, "\nTesting sign & verify with NIST Prime-Curve P-192 : \n");
533 ECDSA_free(ecdsa);
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;
537 time = clock();
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);
544 time = clock();
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++)
552 {
553 ECDSA_SIG_free(signatures[i]);
554 signatures[i] = NULL;
555 }
556
557 /* EC_GROUP_NIST_PRIME_224 */
558 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-224 : \n");
559 ECDSA_free(ecdsa);
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;
563 time = clock();
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);
570 time = clock();
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++)
578 {
579 ECDSA_SIG_free(signatures[i]);
580 signatures[i] = NULL;
581 }
582
583 /* EC_GROUP_NIST_PRIME_256 */
584 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-256 : \n");
585 ECDSA_free(ecdsa);
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;
589 time = clock();
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);
596 time = clock();
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++)
604 {
605 ECDSA_SIG_free(signatures[i]);
606 signatures[i] = NULL;
607 }
608
609 /* EC_GROUP_NIST_PRIME_384 */
610 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-384 : \n");
611 ECDSA_free(ecdsa);
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;
615 time = clock();
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);
622 time = clock();
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++)
630 {
631 ECDSA_SIG_free(signatures[i]);
632 signatures[i] = NULL;
633 }
634
635 /* EC_GROUP_NIST_PRIME_521 */
636 BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-521 : \n");
637 ECDSA_free(ecdsa);
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;
641 time = clock();
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);
648 time = clock();
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);
655 ECDSA_free(ecdsa);
656 ecdsa = NULL;
657 for (i=0; i<ECDSA_NIST_TESTS; i++)
658 {
659 ECDSA_SIG_free(signatures[i]);
660 signatures[i] = NULL;
661 }
662
663 OPENSSL_free(buffer);
664 buffer = NULL;
665 EVP_PKEY_free(pkey);
666 pkey = NULL;
667 ecdsa = NULL;
668
669 ret = 1;
670 err: if (!ret)
671 BIO_printf(bio_err, "TEST FAILED \n");
672 else
673 BIO_printf(bio_err, "TEST PASSED \n");
674 if (!ret)
675 ERR_print_errors(bio_err);
676 if (ecdsa) ECDSA_free(ecdsa);
677 if (d) BN_free(d);
678 if (dgst) OPENSSL_free(dgst);
679 if (md_ctx) EVP_MD_CTX_destroy(md_ctx);
680 CRYPTO_cleanup_all_ex_data();
681 ERR_remove_state(0);
682 ERR_free_strings();
683 CRYPTO_mem_leaks(bio_err);
684 if (bio_err != NULL)
685 {
686 BIO_free(bio_err);
687 bio_err = NULL;
688 }
689 return(0);
690 }
691
692 #endif