ff9338c4c98d3a49155e6db6d6e5781f620788e3
[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_new_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_new_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         /* enable memory leak checking unless explicitly disabled */
334         if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
335                 {
336                 CRYPTO_malloc_debug_init();
337                 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
338                 }
339         else
340                 {
341                 /* OPENSSL_DEBUG_MEMORY=off */
342                 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
343                 }
344         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
345
346         ERR_load_crypto_strings();
347
348         if (bio_err == NULL)
349                 bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
350
351         RAND_seed(rnd_seed, sizeof(rnd_seed));
352
353         if ((ecdsa = ECDSA_new()) == NULL)   goto err;
354
355         set_p192_param(ecdsa);
356         ECDSA_print(bio_err, ecdsa, 0);
357
358         /* en- decode tests */
359
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;
364         pp = buffer;
365         if (!i2d_ECDSAParameters(ecdsa, &pp)) goto err;
366         pp = buffer;
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);
372         buffer = NULL;
373         ECDSA_free(ret_ecdsa);
374         ret_ecdsa = NULL;
375
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;
380         pp = buffer;
381         if (!i2d_ECDSAPrivateKey(ecdsa, &pp)) goto err;
382         pp = buffer;
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);
388         ret_ecdsa = NULL;
389         OPENSSL_free(buffer);
390         buffer = NULL;
391
392         /* i2d_ - d2i_ECDSAPublicKey() */
393
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;
397         pp = buffer;
398         if (!i2d_ECDSAPublicKey(ecdsa, &pp)) goto err;
399         pp = buffer;
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);
405         buffer = NULL;
406         ECDSA_free(ret_ecdsa);
407         ret_ecdsa = NULL;       
408         
409         /* X509_PUBKEY_set() &  X509_PUBKEY_get() */    
410
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;
416
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);
420         ret_pkey = NULL;
421
422         if (ecdsa_cmp(ecdsa, ret_ecdsa)) 
423         {
424                 BIO_printf(bio_err, "TEST FAILED \n");
425                 goto err;
426         }
427         else BIO_printf(bio_err, "TEST OK \n");
428         X509_PUBKEY_free(x509_pubkey);
429         x509_pubkey = NULL;
430         ECDSA_free(ret_ecdsa);
431         ret_ecdsa = NULL;
432
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))
440         {
441                 BIO_printf(bio_err, "TEST FAILED \n");
442                 goto err;
443         }
444         else BIO_printf(bio_err, "TEST OK \n");
445         EVP_PKEY_free(ret_pkey);
446         ret_pkey = NULL;
447         ECDSA_free(ret_ecdsa);
448         ret_ecdsa = NULL;
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))
455         {
456                 BIO_printf(bio_err, "TEST FAILED \n");
457                 goto err;
458         }
459         else BIO_printf(bio_err, "TEST OK \n");
460         EVP_PKEY_free(ret_pkey);
461         ret_pkey = NULL;
462         ECDSA_free(ret_ecdsa);
463         ret_ecdsa = NULL;
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))
470         {
471                 BIO_printf(bio_err, "TEST FAILED \n");
472                 goto err;
473         }
474         else BIO_printf(bio_err, "TEST OK \n");
475         EVP_PKEY_free(ret_pkey);
476         ret_pkey = NULL;
477         ECDSA_free(ret_ecdsa);
478         ret_ecdsa = NULL;
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))
485         {
486                 BIO_printf(bio_err, "TEST FAILED \n");
487                 goto err;
488         }
489         else BIO_printf(bio_err, "TEST OK \n");
490         EVP_PKEY_free(ret_pkey);
491         ret_pkey = NULL;
492         ECDSA_free(ret_ecdsa);
493         ret_ecdsa = NULL;
494         EVP_PKEY_free(pkey);
495         pkey  = NULL;
496         ecdsa = NULL;
497         PKCS8_PRIV_KEY_INFO_free(pkcs8);
498         pkcs8 = NULL;
499
500         /* sign and verify tests */
501         if ((d = BN_new()) == NULL) goto err;
502  
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;
507
508         BIO_printf(bio_err, "Performing tests based on examples H.3.1 and H.3.2 of X9.62 \n");
509  
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"))
516                 goto err;
517         else
518                 BIO_printf(bio_err, "OK\n");
519         BIO_printf(bio_err, "PRIME_239_V1 : ");
520         if (!set_p239_param(ecdsa))
521                 goto err;
522         if (!test_x962_sig_vrf(ecdsa, dgst, "700000017569056646655505781757157107570501575775705779575555657156756655",
523                                "308636143175167811492622547300668018854959378758531778147462058306432176",
524                                "323813553209797357708078776831250505931891051755007842781978505179448783"))
525                 goto err;
526         else
527                 BIO_printf(bio_err, "OK\n");
528
529         ECDSA_free(ecdsa);
530         ecdsa = NULL;
531         OPENSSL_free(dgst);
532         dgst = NULL;
533
534
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;       
539
540         BIO_printf(bio_err, "\nTesting sign & verify with NIST Prime-Curve P-192 : \n");
541         ECDSA_free(ecdsa);
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;
545         time = clock();
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);
552         time = clock();
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++)
560         {
561                 ECDSA_SIG_free(signatures[i]);
562                 signatures[i] = NULL;
563         }
564
565         /* EC_GROUP_NIST_PRIME_224 */
566         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-224 : \n");
567         ECDSA_free(ecdsa);
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;
571         time = clock();
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);
578         time = clock();
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++)
586         {
587                 ECDSA_SIG_free(signatures[i]);
588                 signatures[i] = NULL;
589         }
590
591         /* EC_GROUP_NIST_PRIME_256 */
592         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-256 : \n");
593         ECDSA_free(ecdsa);
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;
597         time = clock();
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);
604         time = clock();
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++)
612         {
613                 ECDSA_SIG_free(signatures[i]);
614                 signatures[i] = NULL;
615         }
616
617         /* EC_GROUP_NIST_PRIME_384 */
618         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-384 : \n");
619         ECDSA_free(ecdsa);
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;
623         time = clock();
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);
630         time = clock();
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++)
638         {
639                 ECDSA_SIG_free(signatures[i]);
640                 signatures[i] = NULL;
641         }
642
643         /* EC_GROUP_NIST_PRIME_521 */
644         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-521 : \n");
645         ECDSA_free(ecdsa);
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;
649         time = clock();
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);
656         time = clock();
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);
663         ECDSA_free(ecdsa);
664         ecdsa = NULL;
665         for (i=0; i<ECDSA_NIST_TESTS; i++)
666         {
667                 ECDSA_SIG_free(signatures[i]);
668                 signatures[i] = NULL;
669         }
670
671         OPENSSL_free(buffer);
672         buffer = NULL;
673         EVP_PKEY_free(pkey);
674         pkey = NULL;
675         ecdsa = NULL;
676         
677         ret = 1;
678 err:    if (!ret)       
679                 BIO_printf(bio_err, "TEST FAILED \n");
680         else 
681                 BIO_printf(bio_err, "TEST PASSED \n");
682         if (!ret)
683                 ERR_print_errors(bio_err);
684         if (ecdsa)      ECDSA_free(ecdsa);
685         if (d)          BN_free(d);
686         if (dgst)       OPENSSL_free(dgst);
687         if (md_ctx)     EVP_MD_CTX_destroy(md_ctx);
688         CRYPTO_cleanup_all_ex_data();
689         ERR_remove_state(0);
690         ERR_free_strings();
691         CRYPTO_mem_leaks(bio_err);
692         if (bio_err != NULL)
693         {
694                 BIO_free(bio_err);
695                 bio_err = NULL;
696         }
697         return(0);
698 }       
699
700 #endif