Fixes for 'no-hw' combined with 'no-SOME_CIPHER'.
[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         /* i2d_ - d2i_ECDSAPublicKey() */
394
395         BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAPublicKey \n");
396         buf_len = i2d_ECDSAPublicKey(ecdsa, NULL);
397         if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
398         pp = buffer;
399         if (!i2d_ECDSAPublicKey(ecdsa, &pp)) goto err;
400         pp = buffer;
401         if ((ret_ecdsa = d2i_ECDSAPublicKey(&ret_ecdsa , (const unsigned char**)&pp, 
402                         buf_len)) == NULL) goto err;
403         ECDSA_print(bio_err, ret_ecdsa, 0);
404         if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
405         OPENSSL_free(buffer);
406         buffer = NULL;
407         ECDSA_free(ret_ecdsa);
408         ret_ecdsa = NULL;       
409         
410         /* X509_PUBKEY_set() &  X509_PUBKEY_get() */    
411
412         BIO_printf(bio_err, "\nTesting X509_PUBKEY_{get,set}            : ");
413         if ((pkey = EVP_PKEY_new()) == NULL) goto err;
414         EVP_PKEY_assign_ECDSA(pkey, ecdsa);
415         if ((x509_pubkey = X509_PUBKEY_new()) == NULL) goto err;
416         if (!X509_PUBKEY_set(&x509_pubkey, pkey)) goto err;
417
418         if ((ret_pkey = X509_PUBKEY_get(x509_pubkey)) == NULL) goto err;
419         ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
420         EVP_PKEY_free(ret_pkey);
421         ret_pkey = NULL;
422
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         X509_PUBKEY_free(x509_pubkey);
430         x509_pubkey = NULL;
431         ECDSA_free(ret_ecdsa);
432         ret_ecdsa = NULL;
433
434         /* Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY */
435         BIO_printf(bio_err, "Testing PKCS8_PRIV_KEY_INFO <-> EVP_PKEY : \n");
436         BIO_printf(bio_err, "PKCS8_OK              : ");
437         if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK)) == NULL) goto err;
438         if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
439         ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
440         if (ecdsa_cmp(ecdsa, ret_ecdsa))
441         {
442                 BIO_printf(bio_err, "TEST FAILED \n");
443                 goto err;
444         }
445         else BIO_printf(bio_err, "TEST OK \n");
446         EVP_PKEY_free(ret_pkey);
447         ret_pkey = NULL;
448         ECDSA_free(ret_ecdsa);
449         ret_ecdsa = NULL;
450         PKCS8_PRIV_KEY_INFO_free(pkcs8);
451         BIO_printf(bio_err, "PKCS8_NO_OCTET        : ");
452         if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NO_OCTET)) == NULL) goto err;
453         if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
454         ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
455         if (ecdsa_cmp(ecdsa, ret_ecdsa))
456         {
457                 BIO_printf(bio_err, "TEST FAILED \n");
458                 goto err;
459         }
460         else BIO_printf(bio_err, "TEST OK \n");
461         EVP_PKEY_free(ret_pkey);
462         ret_pkey = NULL;
463         ECDSA_free(ret_ecdsa);
464         ret_ecdsa = NULL;
465         PKCS8_PRIV_KEY_INFO_free(pkcs8);
466         BIO_printf(bio_err, "PKCS8_EMBEDDED_PARAM  : ");
467         if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_EMBEDDED_PARAM)) == NULL) goto err;
468         if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
469         ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
470         if (ecdsa_cmp(ecdsa, ret_ecdsa))
471         {
472                 BIO_printf(bio_err, "TEST FAILED \n");
473                 goto err;
474         }
475         else BIO_printf(bio_err, "TEST OK \n");
476         EVP_PKEY_free(ret_pkey);
477         ret_pkey = NULL;
478         ECDSA_free(ret_ecdsa);
479         ret_ecdsa = NULL;
480         PKCS8_PRIV_KEY_INFO_free(pkcs8);
481         BIO_printf(bio_err, "PKCS8_NS_DB           : ");
482         if ((pkcs8 = EVP_PKEY2PKCS8_broken(pkey, PKCS8_NS_DB)) == NULL) goto err;
483         if ((ret_pkey = EVP_PKCS82PKEY(pkcs8)) == NULL) goto err;
484         ret_ecdsa = EVP_PKEY_get1_ECDSA(ret_pkey);
485         if (ecdsa_cmp(ecdsa, ret_ecdsa))
486         {
487                 BIO_printf(bio_err, "TEST FAILED \n");
488                 goto err;
489         }
490         else BIO_printf(bio_err, "TEST OK \n");
491         EVP_PKEY_free(ret_pkey);
492         ret_pkey = NULL;
493         ECDSA_free(ret_ecdsa);
494         ret_ecdsa = NULL;
495         EVP_PKEY_free(pkey);
496         pkey  = NULL;
497         ecdsa = NULL;
498         PKCS8_PRIV_KEY_INFO_free(pkcs8);
499         pkcs8 = NULL;
500
501         /* sign and verify tests */
502         if ((d = BN_new()) == NULL) goto err;
503  
504         if (!BN_dec2bn(&d, "968236873715988614170569073515315707566766479517")) goto err;
505         dgst_len = BN_num_bytes(d);
506         if ((dgst = OPENSSL_malloc(dgst_len)) == NULL) goto err;
507         if (!BN_bn2bin(d, dgst)) goto err;
508
509         BIO_printf(bio_err, "Performing tests based on examples H.3.1 and H.3.2 of X9.62 \n");
510  
511         BIO_printf(bio_err, "PRIME_192_V1 : ");
512         if ((ecdsa = ECDSA_new()) == NULL) goto err;
513         if (!set_p192_param(ecdsa)) goto err;
514         if (!test_x962_sig_vrf(ecdsa, dgst, "6140507067065001063065065565667405560006161556565665656654",
515                                "3342403536405981729393488334694600415596881826869351677613",
516                                "5735822328888155254683894997897571951568553642892029982342"))
517                 goto err;
518         else
519                 BIO_printf(bio_err, "OK\n");
520         BIO_printf(bio_err, "PRIME_239_V1 : ");
521         if (!set_p239_param(ecdsa))
522                 goto err;
523         if (!test_x962_sig_vrf(ecdsa, dgst, "700000017569056646655505781757157107570501575775705779575555657156756655",
524                                "308636143175167811492622547300668018854959378758531778147462058306432176",
525                                "323813553209797357708078776831250505931891051755007842781978505179448783"))
526                 goto err;
527         else
528                 BIO_printf(bio_err, "OK\n");
529
530         ECDSA_free(ecdsa);
531         ecdsa = NULL;
532         OPENSSL_free(dgst);
533         dgst = NULL;
534
535
536         /* NIST PRIME CURVES TESTS */
537         /* EC_GROUP_NIST_PRIME_192 */
538         for (i=0; i<ECDSA_NIST_TESTS; i++)
539                 if (!RAND_bytes(digest[i], 20)) goto err;       
540
541         BIO_printf(bio_err, "\nTesting sign & verify with NIST Prime-Curve P-192 : \n");
542         ECDSA_free(ecdsa);
543         if ((ecdsa = ECDSA_new()) == NULL) goto err;
544         if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_192)) == NULL) goto err;
545         if (!ECDSA_generate_key(ecdsa)) goto err;
546         tim = clock();
547         for (i=0; i<ECDSA_NIST_TESTS; i++)
548                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
549         tim = clock() - tim;
550         tim_d = (double)tim / CLOCKS_PER_SEC;
551         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
552                 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
553         tim = clock();
554         for (i=0; i<ECDSA_NIST_TESTS; i++)
555                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
556         tim = clock() - tim;
557         tim_d = (double)tim / CLOCKS_PER_SEC;
558         BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
559                 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
560         for (i=0; i<ECDSA_NIST_TESTS; i++)
561         {
562                 ECDSA_SIG_free(signatures[i]);
563                 signatures[i] = NULL;
564         }
565
566         /* EC_GROUP_NIST_PRIME_224 */
567         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-224 : \n");
568         ECDSA_free(ecdsa);
569         if ((ecdsa = ECDSA_new()) == NULL) goto err;
570         if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_224)) == NULL) goto err;
571         if (!ECDSA_generate_key(ecdsa)) goto err;
572         tim = clock();
573         for (i=0; i<ECDSA_NIST_TESTS; i++)
574                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
575         tim = clock() - tim;
576         tim_d = (double)tim / CLOCKS_PER_SEC;
577         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
578                 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
579         tim = clock();
580         for (i=0; i<ECDSA_NIST_TESTS; i++)
581                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
582         tim = clock() - tim;
583         tim_d = (double)tim / CLOCKS_PER_SEC;
584         BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
585                 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
586         for (i=0; i<ECDSA_NIST_TESTS; i++)
587         {
588                 ECDSA_SIG_free(signatures[i]);
589                 signatures[i] = NULL;
590         }
591
592         /* EC_GROUP_NIST_PRIME_256 */
593         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-256 : \n");
594         ECDSA_free(ecdsa);
595         if ((ecdsa = ECDSA_new()) == NULL) goto err;
596         if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_256)) == NULL) goto err;
597         if (!ECDSA_generate_key(ecdsa)) goto err;
598         tim = clock();
599         for (i=0; i<ECDSA_NIST_TESTS; i++)
600                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
601         tim = clock() - tim;
602         tim_d = (double)tim / CLOCKS_PER_SEC;
603         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
604                 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
605         tim = clock();
606         for (i=0; i<ECDSA_NIST_TESTS; i++)
607                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
608         tim = clock() - tim;
609         tim_d = (double)tim / CLOCKS_PER_SEC;
610         BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
611                 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
612         for (i=0; i<ECDSA_NIST_TESTS; i++)
613         {
614                 ECDSA_SIG_free(signatures[i]);
615                 signatures[i] = NULL;
616         }
617
618         /* EC_GROUP_NIST_PRIME_384 */
619         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-384 : \n");
620         ECDSA_free(ecdsa);
621         if ((ecdsa = ECDSA_new()) == NULL) goto err;
622         if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_384)) == NULL) goto err;
623         if (!ECDSA_generate_key(ecdsa)) goto err;
624         tim = clock();
625         for (i=0; i<ECDSA_NIST_TESTS; i++)
626                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
627         tim = clock() - tim;
628         tim_d = (double)tim / CLOCKS_PER_SEC;
629         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
630                 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
631         tim = clock();
632         for (i=0; i<ECDSA_NIST_TESTS; i++)
633                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
634         tim = clock() - tim;
635         tim_d = (double)tim / CLOCKS_PER_SEC;
636         BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
637                 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
638         for (i=0; i<ECDSA_NIST_TESTS; i++)
639         {
640                 ECDSA_SIG_free(signatures[i]);
641                 signatures[i] = NULL;
642         }
643
644         /* EC_GROUP_NIST_PRIME_521 */
645         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-521 : \n");
646         ECDSA_free(ecdsa);
647         if ((ecdsa = ECDSA_new()) == NULL) goto err;
648         if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_521)) == NULL) goto err;
649         if (!ECDSA_generate_key(ecdsa)) goto err;
650         tim = clock();
651         for (i=0; i<ECDSA_NIST_TESTS; i++)
652                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
653         tim = clock() - tim;
654         tim_d = (double)tim / CLOCKS_PER_SEC;
655         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
656                 , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
657         tim = clock();
658         for (i=0; i<ECDSA_NIST_TESTS; i++)
659                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
660         tim = clock() - tim;
661         tim_d = (double)tim / CLOCKS_PER_SEC;
662         BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
663                 , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
664         ECDSA_free(ecdsa);
665         ecdsa = NULL;
666         for (i=0; i<ECDSA_NIST_TESTS; i++)
667         {
668                 ECDSA_SIG_free(signatures[i]);
669                 signatures[i] = NULL;
670         }
671
672         OPENSSL_free(buffer);
673         buffer = NULL;
674         EVP_PKEY_free(pkey);
675         pkey = NULL;
676         ecdsa = NULL;
677         
678         ret = 1;
679 err:    if (!ret)       
680                 BIO_printf(bio_err, "TEST FAILED \n");
681         else 
682                 BIO_printf(bio_err, "TEST PASSED \n");
683         if (!ret)
684                 ERR_print_errors(bio_err);
685         if (ecdsa)      ECDSA_free(ecdsa);
686         if (d)          BN_free(d);
687         if (dgst)       OPENSSL_free(dgst);
688         if (md_ctx)     EVP_MD_CTX_destroy(md_ctx);
689         CRYPTO_cleanup_all_ex_data();
690         ERR_remove_state(0);
691         ERR_free_strings();
692         CRYPTO_mem_leaks(bio_err);
693         if (bio_err != NULL)
694         {
695                 BIO_free(bio_err);
696                 bio_err = NULL;
697         }
698         return(0);
699 }       
700
701 #endif