fix warnings
[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 /* ====================================================================
56  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57  *
58  * Portions of the attached software ("Contribution") are developed by 
59  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60  *
61  * The Contribution is licensed pursuant to the OpenSSL open source
62  * license provided above.
63  *
64  * In addition, Sun covenants to all licensees who provide a reciprocal
65  * covenant with respect to their own patents if any, not to sue under
66  * current and future patent claims necessarily infringed by the making,
67  * using, practicing, selling, offering for sale and/or otherwise
68  * disposing of the Contribution as delivered hereunder 
69  * (or portions thereof), provided that such covenant shall not apply:
70  *  1) for code that a licensee deletes from the Contribution;
71  *  2) separates from the Contribution; or
72  *  3) for infringements caused by:
73  *       i) the modification of the Contribution or
74  *      ii) the combination of the Contribution with other software or
75  *          devices where such combination causes the infringement.
76  *
77  * The elliptic curve binary polynomial software is originally written by 
78  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
79  *
80  */
81
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <time.h>
86
87 #ifdef CLOCKS_PER_SEC
88         /* "To determine the time in seconds, the value returned
89          * by the clock function should be divided by the value
90          * of the macro CLOCKS_PER_SEC."
91          *                                       -- ISO/IEC 9899 */
92 #       define UNIT "s"
93 #else
94         /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
95          *                            -- cc on NeXTstep/OpenStep */
96 #       define UNIT "units"
97 #       define CLOCKS_PER_SEC 1
98 #endif
99
100 #ifdef OPENSSL_NO_ECDSA
101 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
102 #else
103
104 #include <openssl/crypto.h>
105 #include <openssl/bio.h>
106 #include <openssl/evp.h>
107 #include <openssl/x509.h>
108 #include <openssl/ecdsa.h>
109 #include <openssl/engine.h>
110 #include <openssl/err.h>
111
112 static BIO *bio_err=NULL;
113 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
114
115 #define ECDSA_NIST_TESTS        10
116 ECDSA_SIG*      signatures[ECDSA_NIST_TESTS];
117 unsigned char   digest[ECDSA_NIST_TESTS][20];
118
119 /* some declarations */
120 void clear_ecdsa(EC_KEY *);
121 int set_p192_param(EC_KEY *);
122 int set_p239_param(EC_KEY *);
123 int test_sig_vrf(EC_KEY *, const unsigned char *);
124 int test_x962_sig_vrf(EC_KEY *, const unsigned char *,
125                            const char *, const char *, const char *);
126 int ecdsa_cmp(const EC_KEY *, const EC_KEY *);
127
128 void clear_ecdsa(EC_KEY *ecdsa)
129 {
130         if (!ecdsa)
131                 return;
132         if (ecdsa->group)
133         {
134                 EC_GROUP_free(ecdsa->group);
135                 ecdsa->group = NULL;
136         }
137         if (ecdsa->pub_key)
138         {
139                 EC_POINT_free(ecdsa->pub_key);
140                 ecdsa->pub_key = NULL;
141         }
142         if (ecdsa->priv_key)
143         {
144                 BN_free(ecdsa->priv_key);
145                 ecdsa->priv_key = NULL;
146         }
147 }
148
149 int set_p192_param(EC_KEY *ecdsa)
150 {
151         BN_CTX   *ctx=NULL;
152         int      ret=0;
153
154         if (!ecdsa)
155                 return 0;
156         if ((ctx = BN_CTX_new()) == NULL) goto err;
157         clear_ecdsa(ecdsa);
158         
159         if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_192V1)) == NULL)
160         {
161                 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_192_V1() failed \n");
162                 goto err;
163         }
164         if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
165         {
166                 BIO_printf(bio_err,"EC_POINT_new failed \n");
167                 goto err;
168         }
169
170         if (!BN_dec2bn(&(ecdsa->priv_key), "651056770906015076056810763456358567190100156695615665659"))        goto err;
171         if (!EC_POINT_mul(ecdsa->group,ecdsa->pub_key,ecdsa->priv_key,NULL,NULL,ctx))
172         {
173                 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
174                 goto err;
175         }
176         ret = 1;
177
178 err :   if (ctx)        BN_CTX_free(ctx);
179         return ret;
180 }
181
182 int set_p239_param(EC_KEY *ecdsa)
183 {
184         BN_CTX   *ctx=NULL;
185         int      ret=0;
186
187         if (!ecdsa)
188                 return 0;
189         if ((ctx = BN_CTX_new()) == NULL) goto err;
190         clear_ecdsa(ecdsa);
191         
192         if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_239V1)) == NULL)
193         {
194                 BIO_printf(bio_err,"ECDSA_SET_GROUP_P_239_V1() failed \n");
195                 goto err;
196         }
197         if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
198         {
199                 BIO_printf(bio_err,"EC_POINT_new failed \n");
200                 goto err;
201         }
202
203         if (!BN_dec2bn(&(ecdsa->priv_key), "876300101507107567501066130761671078357010671067781776716671676178726717")) goto err;
204         if (!EC_POINT_mul(ecdsa->group,ecdsa->pub_key,ecdsa->priv_key,NULL,NULL,ctx))
205         {
206                 BIO_printf(bio_err,"EC_POINT_mul() failed \n");
207                 goto err;
208         }
209         ret = 1;
210
211 err :   if (ctx)        BN_CTX_free(ctx);
212         return ret;
213 }
214
215 int test_sig_vrf(EC_KEY *ecdsa, const unsigned char* dgst)
216 {
217         int       ret=0,type=0;
218         unsigned char *buffer=NULL;
219         unsigned int  buf_len;
220         clock_t  tim;
221  
222         if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
223                 return 0;
224         if ((buf_len = ECDSA_size(ecdsa)) == 0)
225         {
226                 BIO_printf(bio_err, "ECDSA_size() == 0 \n");
227                 goto err;
228         }
229         if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
230                 goto err;
231  
232         tim = clock();
233         if (!ECDSA_sign(type, dgst , 20, buffer, &buf_len, ecdsa))
234         {
235                 BIO_printf(bio_err, "ECDSA_sign() FAILED \n");
236                 goto err;
237         }
238         tim = clock() - tim;
239         BIO_printf(bio_err, " [ ECDSA_sign() %.2f"UNIT, (double)tim/(CLOCKS_PER_SEC));
240  
241         tim = clock();
242         ret = ECDSA_verify(type, dgst, 20, buffer, buf_len, ecdsa);
243         if (ret != 1)
244         {
245                 BIO_printf(bio_err, "ECDSA_verify() FAILED \n");
246                 goto err;
247         }
248         tim = clock() - tim;
249         BIO_printf(bio_err, " and ECDSA_verify() %.2f"UNIT" ] ", (double)tim/(CLOCKS_PER_SEC));
250  
251 err:    OPENSSL_free(buffer);
252         return(ret == 1);
253 }
254
255 int test_x962_sig_vrf(EC_KEY *eckey, const unsigned char *dgst,
256                            const char *k_in, const char *r_in, const char *s_in)
257 {
258         int       ret=0;
259         ECDSA_SIG *sig=NULL;
260         EC_POINT  *point=NULL;
261         BIGNUM    *r=NULL,*s=NULL,*k=NULL,*x=NULL,*y=NULL,*m=NULL,*ord=NULL;
262         BN_CTX    *ctx=NULL;
263         char      *tmp_char=NULL;
264         ECDSA_DATA *ecdsa = ecdsa_check(eckey);;
265         
266         if (!eckey || !eckey->group || !eckey->pub_key || !eckey->priv_key
267                 || !ecdsa)
268                 return 0;
269         if ((point = EC_POINT_new(eckey->group)) == NULL) goto err;
270         if ((r = BN_new()) == NULL || (s = BN_new()) == NULL 
271                 || (k = BN_new()) == NULL || (x = BN_new()) == NULL || 
272                 (y = BN_new()) == NULL || (m = BN_new()) == NULL ||
273                 (ord = BN_new()) == NULL) goto err;
274         if ((ctx = BN_CTX_new()) == NULL) goto err;
275         if (!BN_bin2bn(dgst, 20, m)) goto err;
276         if (!BN_dec2bn(&k, k_in)) goto err;
277         if (!EC_POINT_mul(eckey->group, point, k, NULL, NULL, ctx)) goto err;
278         if (!EC_POINT_get_affine_coordinates_GFp(eckey->group, point, x, y,
279                 ctx)) goto err;
280         if (!EC_GROUP_get_order(eckey->group, ord, ctx)) goto err;
281         if ((ecdsa->r = BN_dup(x)) == NULL) goto err;
282         if ((ecdsa->kinv = BN_mod_inverse(NULL, k, ord, ctx)) == NULL)
283                 goto err;
284  
285         if ((sig = ECDSA_do_sign(dgst, 20, eckey)) == NULL)
286         {
287                 BIO_printf(bio_err,"ECDSA_do_sign() failed \n");
288                 goto err;
289         }
290  
291         if (!BN_dec2bn(&r, r_in)) goto err;
292         if (!BN_dec2bn(&s, s_in)) goto err;
293         if (BN_cmp(sig->r,r) != 0 || BN_cmp(sig->s,s) != 0)
294         {
295                 tmp_char = OPENSSL_malloc(128);
296                 if (tmp_char == NULL) goto err;
297                 tmp_char = BN_bn2dec(sig->r);
298                 BIO_printf(bio_err,"unexpected signature \n");
299                 BIO_printf(bio_err,"sig->r = %s\n",tmp_char);
300                 tmp_char = BN_bn2dec(sig->s);
301                 BIO_printf(bio_err,"sig->s = %s\n",tmp_char);
302                 goto err;
303         }
304                 ret = ECDSA_do_verify(dgst, 20, sig, eckey);
305         if (ret != 1)
306         {
307                 BIO_printf(bio_err,"ECDSA_do_verify : signature verification failed \n");
308                 goto err;
309         }
310  
311         ret = 1;
312 err :   if (r)    BN_free(r);
313         if (s)    BN_free(s);
314         if (k)    BN_free(k);
315         if (x)    BN_free(x);
316         if (y)    BN_free(y);
317         if (m)    BN_free(m);
318         if (ord)  BN_free(ord);
319         if (sig)  ECDSA_SIG_free(sig);
320         if (ctx)  BN_CTX_free(ctx);
321         if (point) EC_POINT_free(point);
322         if (tmp_char) OPENSSL_free(tmp_char);
323         return(ret == 1);
324 }
325
326 int ecdsa_cmp(const EC_KEY *a, const EC_KEY *b)
327 {
328         int     ret=1;
329         BN_CTX  *ctx=NULL;
330         BIGNUM  *tmp_a1=NULL, *tmp_a2=NULL, *tmp_a3=NULL;
331         BIGNUM  *tmp_b1=NULL, *tmp_b2=NULL, *tmp_b3=NULL;
332
333         if ((ctx = BN_CTX_new()) == NULL) return 1;
334         if ((tmp_a1 = BN_new()) == NULL || (tmp_a2 = BN_new()) == NULL || (tmp_a3 = BN_new()) == NULL) goto err;
335         if ((tmp_b1 = BN_new()) == NULL || (tmp_b2 = BN_new()) == NULL || (tmp_b3 = BN_new()) == NULL) goto err;
336
337         if (a->pub_key && b->pub_key)
338                 if (EC_POINT_cmp(a->group, a->pub_key, b->pub_key, ctx) != 0) goto err;
339         if (a->priv_key && b->priv_key)
340                 if (BN_cmp(a->priv_key, b->priv_key) != 0) goto err;
341         if (!EC_GROUP_get_curve_GFp(a->group, tmp_a1, tmp_a2, tmp_a3, ctx)) goto err;
342         if (!EC_GROUP_get_curve_GFp(a->group, tmp_b1, tmp_b2, tmp_b3, ctx)) goto err;
343         if (BN_cmp(tmp_a1, tmp_b1) != 0) goto err;
344         if (BN_cmp(tmp_a2, tmp_b2) != 0) goto err;
345         if (BN_cmp(tmp_a3, tmp_b3) != 0) goto err;
346
347         ret = 0;
348 err:    if (tmp_a1) BN_free(tmp_a1);
349         if (tmp_a2) BN_free(tmp_a2);
350         if (tmp_a3) BN_free(tmp_a3);
351         if (tmp_b1) BN_free(tmp_b1);
352         if (tmp_b2) BN_free(tmp_b2);
353         if (tmp_b3) BN_free(tmp_b3);
354         if (ctx) BN_CTX_free(ctx);
355         return(ret);
356 }
357
358 int main(void)
359 {
360         EC_KEY          *ecdsa=NULL, *ret_ecdsa=NULL;
361         BIGNUM          *d=NULL;
362         X509_PUBKEY     *x509_pubkey=NULL;
363         PKCS8_PRIV_KEY_INFO *pkcs8=NULL;
364         EVP_PKEY        *pkey=NULL, *ret_pkey=NULL;
365         int             dgst_len=0;
366         unsigned char   *dgst=NULL;
367         int             ret = 0, i=0;
368         clock_t         tim;
369         unsigned char   *buffer=NULL;
370         unsigned char   *pp;
371         long            buf_len=0;
372         double          tim_d;
373         EVP_MD_CTX      *md_ctx=NULL;
374         
375         /* enable memory leak checking unless explicitly disabled */
376         if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
377                 {
378                 CRYPTO_malloc_debug_init();
379                 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
380                 }
381         else
382                 {
383                 /* OPENSSL_DEBUG_MEMORY=off */
384                 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
385                 }
386         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
387
388         ERR_load_crypto_strings();
389
390         if (bio_err == NULL)
391                 bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
392
393         RAND_seed(rnd_seed, sizeof(rnd_seed));
394
395         if ((ecdsa = EC_KEY_new()) == NULL)   goto err;
396
397         set_p192_param(ecdsa);
398         EC_KEY_print(bio_err, ecdsa, 0);
399
400         /* en- decode tests */
401
402         /* i2d_ - d2i_ECParameters() */
403         BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAParameters \n");
404         buf_len = i2d_ECParameters(ecdsa, NULL);
405         if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
406         pp = buffer;
407         if (!i2d_ECParameters(ecdsa, &pp)) goto err;
408         pp = buffer;
409         if ((ret_ecdsa = d2i_ECParameters(&ret_ecdsa, (const unsigned char **)&pp, 
410                         buf_len)) == NULL) goto err;
411         ECParameters_print(bio_err, ret_ecdsa);
412         if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
413         OPENSSL_free(buffer);
414         buffer = NULL;
415         EC_KEY_free(ret_ecdsa);
416         ret_ecdsa = NULL;
417
418         /* i2d_ - d2i_ECPrivateKey() */
419         BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAPrivateKey \n");
420         buf_len = i2d_ECPrivateKey(ecdsa, NULL);
421         if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
422         pp = buffer;
423         if (!i2d_ECPrivateKey(ecdsa, &pp)) goto err;
424         pp = buffer;
425         if ((ret_ecdsa = d2i_ECPrivateKey(&ret_ecdsa, (const unsigned char**)&pp, 
426                         buf_len)) == NULL) goto err;
427         EC_KEY_print(bio_err, ret_ecdsa, 0);
428         if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
429         EC_KEY_free(ret_ecdsa);
430         ret_ecdsa = NULL;
431         OPENSSL_free(buffer);
432         buffer = NULL;
433
434         /* X509_PUBKEY_set() &  X509_PUBKEY_get() */    
435
436         BIO_printf(bio_err, "\nTesting X509_PUBKEY_{get,set}            : ");
437         if ((pkey = EVP_PKEY_new()) == NULL) goto err;
438         EVP_PKEY_assign_EC_KEY(pkey, ecdsa);
439         if ((x509_pubkey = X509_PUBKEY_new()) == NULL) goto err;
440         if (!X509_PUBKEY_set(&x509_pubkey, pkey)) goto err;
441
442         if ((ret_pkey = X509_PUBKEY_get(x509_pubkey)) == NULL) goto err;
443         ret_ecdsa = EVP_PKEY_get1_EC_KEY(ret_pkey);
444         EVP_PKEY_free(ret_pkey);
445         ret_pkey = NULL;
446
447         if (ecdsa_cmp(ecdsa, ret_ecdsa)) 
448         {
449                 BIO_printf(bio_err, "TEST FAILED \n");
450                 goto err;
451         }
452         else BIO_printf(bio_err, "TEST OK \n");
453         X509_PUBKEY_free(x509_pubkey);
454         x509_pubkey = NULL;
455         EC_KEY_free(ret_ecdsa);
456         ret_ecdsa = NULL;
457
458         /* Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY */
459         BIO_printf(bio_err, "Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY : \n");
460         BIO_printf(bio_err, "PKCS8_OK              : ");
461         if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK)) == NULL) goto err;
462         if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
463         ret_ecdsa = EVP_PKEY_get1_EC_KEY(ret_pkey);
464         if (ecdsa_cmp(ecdsa, ret_ecdsa))
465         {
466                 BIO_printf(bio_err, "TEST FAILED \n");
467                 goto err;
468         }
469         else BIO_printf(bio_err, "TEST OK \n");
470         EVP_PKEY_free(ret_pkey);
471         ret_pkey = NULL;
472         EC_KEY_free(ret_ecdsa);
473         ret_ecdsa = NULL;
474         PKCS8_PRIV_KEY_INFO_free(pkcs8);
475         BIO_printf(bio_err, "PKCS8_NO_OCTET        : ");
476         if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NO_OCTET)) == NULL) goto err;
477         if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
478         ret_ecdsa = EVP_PKEY_get1_EC_KEY(ret_pkey);
479         if (ecdsa_cmp(ecdsa, ret_ecdsa))
480         {
481                 BIO_printf(bio_err, "TEST FAILED \n");
482                 goto err;
483         }
484         else BIO_printf(bio_err, "TEST OK \n");
485         EVP_PKEY_free(ret_pkey);
486         ret_pkey = NULL;
487         EC_KEY_free(ret_ecdsa);
488         ret_ecdsa = NULL;
489         PKCS8_PRIV_KEY_INFO_free(pkcs8);
490         BIO_printf(bio_err, "PKCS8_EMBEDDED_PARAM  : ");
491         if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_EMBEDDED_PARAM)) == NULL) goto err;
492         if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
493         ret_ecdsa = EVP_PKEY_get1_EC_KEY(ret_pkey);
494         if (ecdsa_cmp(ecdsa, ret_ecdsa))
495         {
496                 BIO_printf(bio_err, "TEST FAILED \n");
497                 goto err;
498         }
499         else BIO_printf(bio_err, "TEST OK \n");
500         EVP_PKEY_free(ret_pkey);
501         ret_pkey = NULL;
502         EC_KEY_free(ret_ecdsa);
503         ret_ecdsa = NULL;
504         PKCS8_PRIV_KEY_INFO_free(pkcs8);
505         BIO_printf(bio_err, "PKCS8_NS_DB           : ");
506         if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NS_DB)) == NULL) goto err;
507         if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
508         ret_ecdsa = EVP_PKEY_get1_EC_KEY(ret_pkey);
509         if (ecdsa_cmp(ecdsa, ret_ecdsa))
510         {
511                 BIO_printf(bio_err, "TEST FAILED \n");
512                 goto err;
513         }
514         else BIO_printf(bio_err, "TEST OK \n");
515         EVP_PKEY_free(ret_pkey);
516         ret_pkey = NULL;
517         EC_KEY_free(ret_ecdsa);
518         ret_ecdsa = NULL;
519         EVP_PKEY_free(pkey);
520         pkey  = NULL;
521         ecdsa = NULL;
522         PKCS8_PRIV_KEY_INFO_free(pkcs8);
523         pkcs8 = NULL;
524
525         /* sign and verify tests */
526         if ((d = BN_new()) == NULL) goto err;
527  
528         if (!BN_dec2bn(&d, "968236873715988614170569073515315707566766479517")) goto err;
529         dgst_len = BN_num_bytes(d);
530         if ((dgst = OPENSSL_malloc(dgst_len)) == NULL) goto err;
531         if (!BN_bn2bin(d, dgst)) goto err;
532
533         BIO_printf(bio_err, "Performing tests based on examples H.3.1 and H.3.2 of X9.62 \n");
534  
535         BIO_printf(bio_err, "PRIME_192_V1 : ");
536         if ((ecdsa = EC_KEY_new()) == NULL) goto err;
537         if (!set_p192_param(ecdsa)) goto err;
538         if (!test_x962_sig_vrf(ecdsa, dgst, "6140507067065001063065065565667405560006161556565665656654",
539                                "3342403536405981729393488334694600415596881826869351677613",
540                                "5735822328888155254683894997897571951568553642892029982342"))
541                 goto err;
542         else
543                 BIO_printf(bio_err, "OK\n");
544         BIO_printf(bio_err, "PRIME_239_V1 : ");
545         if (!set_p239_param(ecdsa))
546                 goto err;
547         if (!test_x962_sig_vrf(ecdsa, dgst, "700000017569056646655505781757157107570501575775705779575555657156756655",
548                                "308636143175167811492622547300668018854959378758531778147462058306432176",
549                                "323813553209797357708078776831250505931891051755007842781978505179448783"))
550                 goto err;
551         else
552                 BIO_printf(bio_err, "OK\n");
553
554         EC_KEY_free(ecdsa);
555         ecdsa = NULL;
556         OPENSSL_free(dgst);
557         dgst = NULL;
558
559         for (i=0; i<ECDSA_NIST_TESTS; i++)
560                 if (!RAND_bytes(digest[i], 20)) goto err;
561
562         BIO_printf(bio_err, "\n");
563
564 /* Macro for each test */
565 #define ECDSA_GROUP_TEST(text, curve) \
566         BIO_printf(bio_err, "Testing sign & verify with %s : \n", text); \
567         EC_KEY_free(ecdsa); \
568         if ((ecdsa = EC_KEY_new()) == NULL) goto err; \
569         if ((ecdsa->group = EC_GROUP_new_by_name(curve)) == NULL) goto err; \
570         if (!EC_KEY_generate_key(ecdsa)) goto err; \
571         tim = 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         tim = clock() - tim; \
575         tim_d = (double)tim / 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, tim_d, tim_d / ECDSA_NIST_TESTS); \
578         tim = clock(); \
579         for (i=0; i<ECDSA_NIST_TESTS; i++) \
580                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err; \
581         tim = clock() - tim; \
582         tim_d = (double)tim / 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, tim_d, tim_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         /* NIST PRIME CURVES TESTS */
592         ECDSA_GROUP_TEST("NIST Prime-Curve P-192", EC_GROUP_NIST_PRIME_192);
593         ECDSA_GROUP_TEST("NIST Prime-Curve P-224", EC_GROUP_NIST_PRIME_224);
594         ECDSA_GROUP_TEST("NIST Prime-Curve P-256", EC_GROUP_NIST_PRIME_256);
595         ECDSA_GROUP_TEST("NIST Prime-Curve P-384", EC_GROUP_NIST_PRIME_384);
596         ECDSA_GROUP_TEST("NIST Prime-Curve P-521", EC_GROUP_NIST_PRIME_521);
597         /* NIST BINARY CURVES TESTS */
598         ECDSA_GROUP_TEST("NIST Binary-Curve K-163", EC_GROUP_NIST_CHAR2_K163);
599         ECDSA_GROUP_TEST("NIST Binary-Curve B-163", EC_GROUP_NIST_CHAR2_B163);
600         ECDSA_GROUP_TEST("NIST Binary-Curve K-233", EC_GROUP_NIST_CHAR2_K233);
601         ECDSA_GROUP_TEST("NIST Binary-Curve B-233", EC_GROUP_NIST_CHAR2_B233);
602         ECDSA_GROUP_TEST("NIST Binary-Curve K-283", EC_GROUP_NIST_CHAR2_K283);
603         ECDSA_GROUP_TEST("NIST Binary-Curve B-283", EC_GROUP_NIST_CHAR2_B283);
604         ECDSA_GROUP_TEST("NIST Binary-Curve K-409", EC_GROUP_NIST_CHAR2_K409);
605         ECDSA_GROUP_TEST("NIST Binary-Curve B-409", EC_GROUP_NIST_CHAR2_B409);
606         ECDSA_GROUP_TEST("NIST Binary-Curve K-571", EC_GROUP_NIST_CHAR2_K571);
607         ECDSA_GROUP_TEST("NIST Binary-Curve B-571", EC_GROUP_NIST_CHAR2_B571);
608 #undef ECDSA_GROUP_TEST
609
610         EC_KEY_free(ecdsa);
611         ecdsa = NULL;
612         OPENSSL_free(buffer);
613         buffer = NULL;
614         EVP_PKEY_free(pkey);
615         pkey = NULL;
616         
617         ret = 1;
618 err:    if (!ret)       
619                 BIO_printf(bio_err, "TEST FAILED \n");
620         else 
621                 BIO_printf(bio_err, "TEST PASSED \n");
622         if (!ret)
623                 ERR_print_errors(bio_err);
624         if (ecdsa)      EC_KEY_free(ecdsa);
625         if (d)          BN_free(d);
626         if (dgst)       OPENSSL_free(dgst);
627         if (md_ctx)     EVP_MD_CTX_destroy(md_ctx);
628         if (pkey)       EVP_PKEY_free(pkey);
629         CRYPTO_cleanup_all_ex_data();
630         ERR_remove_state(0);
631         ERR_free_strings();
632         CRYPTO_mem_leaks(bio_err);
633         if (bio_err != NULL)
634         {
635                 BIO_free(bio_err);
636                 bio_err = NULL;
637         }
638         return(0);
639 }       
640
641 #endif