fix ECDSA handling
[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
60 #ifdef CLOCKS_PER_SEC
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."
64          *                                       -- ISO/IEC 9899 */
65 #       define UNIT "s"
66 #else
67         /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
68          *                            -- cc on NeXTstep/OpenStep */
69 #       define UNIT "units"
70 #       define CLOCKS_PER_SEC 1
71 #endif
72
73 #ifdef OPENSSL_NO_ECDSA
74 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
75 #else
76
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>
84
85 static BIO *bio_err=NULL;
86 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
87
88 #define ECDSA_NIST_TESTS        10
89 ECDSA_SIG*      signatures[ECDSA_NIST_TESTS];
90 unsigned char   digest[ECDSA_NIST_TESTS][20];
91
92 void clear_ecdsa(ECDSA *ecdsa)
93 {
94         if (!ecdsa)
95                 return;
96         if (ecdsa->group)
97         {
98                 EC_GROUP_free(ecdsa->group);
99                 ecdsa->group = NULL;
100         }
101         if (ecdsa->pub_key)
102         {
103                 EC_POINT_free(ecdsa->pub_key);
104                 ecdsa->pub_key = NULL;
105         }
106         if (ecdsa->priv_key)
107         {
108                 BN_free(ecdsa->priv_key);
109                 ecdsa->priv_key = NULL;
110         }
111 }
112
113 int set_p192_param(ECDSA *ecdsa)
114 {
115         BN_CTX   *ctx=NULL;
116         int      ret=0;
117
118         if (!ecdsa)
119                 return 0;
120         if ((ctx = BN_CTX_new()) == NULL) goto err;
121         clear_ecdsa(ecdsa);
122         
123         if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_192V1)) == NULL)
124         {
125                 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_192_V1() failed \n");
126                 goto err;
127         }
128         if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
129         {
130                 BIO_printf(bio_err,"EC_POINT_new failed \n");
131                 goto err;
132         }
133
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))
136         {
137                 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
138                 goto err;
139         }
140         ret = 1;
141
142 err :   if (ctx)        BN_CTX_free(ctx);
143         return ret;
144 }
145
146 int set_p239_param(ECDSA *ecdsa)
147 {
148         BN_CTX   *ctx=NULL;
149         int      ret=0;
150
151         if (!ecdsa)
152                 return 0;
153         if ((ctx = BN_CTX_new()) == NULL) goto err;
154         clear_ecdsa(ecdsa);
155         
156         if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_239V1)) == NULL)
157         {
158                 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_239_V1() failed \n");
159                 goto err;
160         }
161         if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
162         {
163                 BIO_printf(bio_err,"EC_POINT_new failed \n");
164                 goto err;
165         }
166
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))
169         {
170                 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
171                 goto err;
172         }
173         ret = 1;
174
175 err :   if (ctx)        BN_CTX_free(ctx);
176         return ret;
177 }
178
179 int test_sig_vrf(ECDSA *ecdsa, const unsigned char* dgst)
180 {
181         int       ret=0,type=0;
182         unsigned char *buffer=NULL;
183         unsigned int  buf_len;
184         clock_t  tim;
185  
186         if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
187                 return 0;
188         if ((buf_len = ECDSA_size(ecdsa)) == 0)
189         {
190                 BIO_printf(bio_err, "ECDSA_size() == 0 \n");
191                 goto err;
192         }
193         if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
194                 goto err;
195  
196         tim = clock();
197         if (!ECDSA_sign(type, dgst , 20, buffer, &buf_len, ecdsa))
198         {
199                 BIO_printf(bio_err, "ECDSA_sign() FAILED \n");
200                 goto err;
201         }
202         tim = clock() - tim;
203         BIO_printf(bio_err, " [ ECDSA_sign() %.2f"UNIT, (double)tim/(CLOCKS_PER_SEC));
204  
205         tim = clock();
206         ret = ECDSA_verify(type, dgst, 20, buffer, buf_len, ecdsa);
207         if (ret != 1)
208         {
209                 BIO_printf(bio_err, "ECDSA_verify() FAILED \n");
210                 goto err;
211         }
212         tim = clock() - tim;
213         BIO_printf(bio_err, " and ECDSA_verify() %.2f"UNIT" ] ", (double)tim/(CLOCKS_PER_SEC));
214  
215 err:    OPENSSL_free(buffer);
216         return(ret == 1);
217 }
218
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)
221 {
222         int       ret=0;
223         ECDSA_SIG *sig=NULL;
224         EC_POINT  *point=NULL;
225         BIGNUM    *r=NULL,*s=NULL,*k=NULL,*x=NULL,*y=NULL,*m=NULL,*ord=NULL;
226         BN_CTX    *ctx=NULL;
227         char      *tmp_char=NULL;
228  
229         if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
230                 return 0;
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;
243  
244         if ((sig = ECDSA_do_sign(dgst, 20, ecdsa)) == NULL)
245         {
246                 BIO_printf(bio_err,"ECDSA_do_sign() failed \n");
247                 goto err;
248         }
249  
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)
253         {
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);
261                 goto err;
262         }
263                 ret = ECDSA_do_verify(dgst, 20, sig, ecdsa);
264         if (ret != 1)
265         {
266                 BIO_printf(bio_err,"ECDSA_do_verify : signature verification failed \n");
267                 goto err;
268         }
269  
270         ret = 1;
271 err :   if (r)    BN_free(r);
272         if (s)    BN_free(s);
273         if (k)    BN_free(k);
274         if (x)    BN_free(x);
275         if (y)    BN_free(y);
276         if (m)    BN_free(m);
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);
282         return(ret == 1);
283 }
284
285 int ecdsa_cmp(const ECDSA *a, const ECDSA *b)
286 {
287         int     ret=1;
288         BN_CTX  *ctx=NULL;
289         BIGNUM  *tmp_a1=NULL, *tmp_a2=NULL, *tmp_a3=NULL;
290         BIGNUM  *tmp_b1=NULL, *tmp_b2=NULL, *tmp_b3=NULL;
291
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;
295
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;
305
306         ret = 0;
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);
314         return(ret);
315 }
316
317 int main(void)
318 {
319         ECDSA           *ecdsa=NULL, *ret_ecdsa=NULL;
320         BIGNUM          *d=NULL;
321         X509_PUBKEY     *x509_pubkey=NULL;
322         PKCS8_PRIV_KEY_INFO *pkcs8=NULL;
323         EVP_PKEY        *pkey=NULL, *ret_pkey=NULL;
324         int             dgst_len=0;
325         unsigned char   *dgst=NULL;
326         int             ret = 0, i=0;
327         clock_t         tim;
328         unsigned char   *buffer=NULL;
329         unsigned char   *pp;
330         long            buf_len=0;
331         double          tim_d;
332         EVP_MD_CTX      *md_ctx=NULL;
333         
334         /* enable memory leak checking unless explicitly disabled */
335         if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
336                 {
337                 CRYPTO_malloc_debug_init();
338                 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
339                 }
340         else
341                 {
342                 /* OPENSSL_DEBUG_MEMORY=off */
343                 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
344                 }
345         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
346
347         ERR_load_crypto_strings();
348
349         if (bio_err == NULL)
350                 bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
351
352         RAND_seed(rnd_seed, sizeof(rnd_seed));
353
354         if ((ecdsa = ECDSA_new()) == NULL)   goto err;
355
356         set_p192_param(ecdsa);
357         ECDSA_print(bio_err, ecdsa, 0);
358
359         /* en- decode tests */
360
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;
365         pp = buffer;
366         if (!i2d_ECDSAParameters(ecdsa, &pp)) goto err;
367         pp = buffer;
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);
373         buffer = NULL;
374         ECDSA_free(ret_ecdsa);
375         ret_ecdsa = NULL;
376
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;
381         pp = buffer;
382         if (!i2d_ECDSAPrivateKey(ecdsa, &pp)) goto err;
383         pp = buffer;
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);
389         ret_ecdsa = NULL;
390         OPENSSL_free(buffer);
391         buffer = NULL;
392
393         /* X509_PUBKEY_set() &  X509_PUBKEY_get() */    
394
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;
400
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);
404         ret_pkey = NULL;
405
406         if (ecdsa_cmp(ecdsa, ret_ecdsa)) 
407         {
408                 BIO_printf(bio_err, "TEST FAILED \n");
409                 goto err;
410         }
411         else BIO_printf(bio_err, "TEST OK \n");
412         X509_PUBKEY_free(x509_pubkey);
413         x509_pubkey = NULL;
414         ECDSA_free(ret_ecdsa);
415         ret_ecdsa = NULL;
416
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))
424         {
425                 BIO_printf(bio_err, "TEST FAILED \n");
426                 goto err;
427         }
428         else BIO_printf(bio_err, "TEST OK \n");
429         EVP_PKEY_free(ret_pkey);
430         ret_pkey = NULL;
431         ECDSA_free(ret_ecdsa);
432         ret_ecdsa = NULL;
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))
439         {
440                 BIO_printf(bio_err, "TEST FAILED \n");
441                 goto err;
442         }
443         else BIO_printf(bio_err, "TEST OK \n");
444         EVP_PKEY_free(ret_pkey);
445         ret_pkey = NULL;
446         ECDSA_free(ret_ecdsa);
447         ret_ecdsa = NULL;
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))
454         {
455                 BIO_printf(bio_err, "TEST FAILED \n");
456                 goto err;
457         }
458         else BIO_printf(bio_err, "TEST OK \n");
459         EVP_PKEY_free(ret_pkey);
460         ret_pkey = NULL;
461         ECDSA_free(ret_ecdsa);
462         ret_ecdsa = NULL;
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))
469         {
470                 BIO_printf(bio_err, "TEST FAILED \n");
471                 goto err;
472         }
473         else BIO_printf(bio_err, "TEST OK \n");
474         EVP_PKEY_free(ret_pkey);
475         ret_pkey = NULL;
476         ECDSA_free(ret_ecdsa);
477         ret_ecdsa = NULL;
478         EVP_PKEY_free(pkey);
479         pkey  = NULL;
480         ecdsa = NULL;
481         PKCS8_PRIV_KEY_INFO_free(pkcs8);
482         pkcs8 = NULL;
483
484         /* sign and verify tests */
485         if ((d = BN_new()) == NULL) goto err;
486  
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;
491
492         BIO_printf(bio_err, "Performing tests based on examples H.3.1 and H.3.2 of X9.62 \n");
493  
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"))
500                 goto err;
501         else
502                 BIO_printf(bio_err, "OK\n");
503         BIO_printf(bio_err, "PRIME_239_V1 : ");
504         if (!set_p239_param(ecdsa))
505                 goto err;
506         if (!test_x962_sig_vrf(ecdsa, dgst, "700000017569056646655505781757157107570501575775705779575555657156756655",
507                                "308636143175167811492622547300668018854959378758531778147462058306432176",
508                                "323813553209797357708078776831250505931891051755007842781978505179448783"))
509                 goto err;
510         else
511                 BIO_printf(bio_err, "OK\n");
512
513         ECDSA_free(ecdsa);
514         ecdsa = NULL;
515         OPENSSL_free(dgst);
516         dgst = NULL;
517
518
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;       
523
524         BIO_printf(bio_err, "\nTesting sign & verify with NIST Prime-Curve P-192 : \n");
525         ECDSA_free(ecdsa);
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;
529         tim = clock();
530         for (i=0; i<ECDSA_NIST_TESTS; i++)
531                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
532         tim = clock() - tim;
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);
536         tim = clock();
537         for (i=0; i<ECDSA_NIST_TESTS; i++)
538                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
539         tim = clock() - tim;
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++)
544         {
545                 ECDSA_SIG_free(signatures[i]);
546                 signatures[i] = NULL;
547         }
548
549         /* EC_GROUP_NIST_PRIME_224 */
550         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-224 : \n");
551         ECDSA_free(ecdsa);
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;
555         tim = clock();
556         for (i=0; i<ECDSA_NIST_TESTS; i++)
557                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
558         tim = clock() - tim;
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);
562         tim = clock();
563         for (i=0; i<ECDSA_NIST_TESTS; i++)
564                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
565         tim = clock() - tim;
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++)
570         {
571                 ECDSA_SIG_free(signatures[i]);
572                 signatures[i] = NULL;
573         }
574
575         /* EC_GROUP_NIST_PRIME_256 */
576         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-256 : \n");
577         ECDSA_free(ecdsa);
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;
581         tim = clock();
582         for (i=0; i<ECDSA_NIST_TESTS; i++)
583                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
584         tim = clock() - tim;
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);
588         tim = clock();
589         for (i=0; i<ECDSA_NIST_TESTS; i++)
590                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
591         tim = clock() - tim;
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++)
596         {
597                 ECDSA_SIG_free(signatures[i]);
598                 signatures[i] = NULL;
599         }
600
601         /* EC_GROUP_NIST_PRIME_384 */
602         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-384 : \n");
603         ECDSA_free(ecdsa);
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;
607         tim = clock();
608         for (i=0; i<ECDSA_NIST_TESTS; i++)
609                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
610         tim = clock() - tim;
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);
614         tim = clock();
615         for (i=0; i<ECDSA_NIST_TESTS; i++)
616                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
617         tim = clock() - tim;
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++)
622         {
623                 ECDSA_SIG_free(signatures[i]);
624                 signatures[i] = NULL;
625         }
626
627         /* EC_GROUP_NIST_PRIME_521 */
628         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-521 : \n");
629         ECDSA_free(ecdsa);
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;
633         tim = clock();
634         for (i=0; i<ECDSA_NIST_TESTS; i++)
635                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
636         tim = clock() - tim;
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);
640         tim = clock();
641         for (i=0; i<ECDSA_NIST_TESTS; i++)
642                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
643         tim = clock() - tim;
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);
647         ECDSA_free(ecdsa);
648         ecdsa = NULL;
649         for (i=0; i<ECDSA_NIST_TESTS; i++)
650         {
651                 ECDSA_SIG_free(signatures[i]);
652                 signatures[i] = NULL;
653         }
654
655         OPENSSL_free(buffer);
656         buffer = NULL;
657         EVP_PKEY_free(pkey);
658         pkey = NULL;
659         ecdsa = NULL;
660         
661         ret = 1;
662 err:    if (!ret)       
663                 BIO_printf(bio_err, "TEST FAILED \n");
664         else 
665                 BIO_printf(bio_err, "TEST PASSED \n");
666         if (!ret)
667                 ERR_print_errors(bio_err);
668         if (ecdsa)      ECDSA_free(ecdsa);
669         if (d)          BN_free(d);
670         if (dgst)       OPENSSL_free(dgst);
671         if (md_ctx)     EVP_MD_CTX_destroy(md_ctx);
672         CRYPTO_cleanup_all_ex_data();
673         ERR_remove_state(0);
674         ERR_free_strings();
675         CRYPTO_mem_leaks(bio_err);
676         if (bio_err != NULL)
677         {
678                 BIO_free(bio_err);
679                 bio_err = NULL;
680         }
681         return(0);
682 }       
683
684 #endif