'version' is not optional in the encoding
[openssl.git] / crypto / ecdsa / ecdsatest.c
index 1f2cef03319aacf2b2715e54d2c9a617838f706e..cffc1949283bc7c6022c13012c2d994a46710bda 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
-#include <openssl/crypto.h>
-#include <openssl/bio.h>
-#include <openssl/evp.h>
-#include <openssl/x509.h>
-#include <openssl/ecdsa.h>
-#include <openssl/engine.h>
-#include <openssl/err.h>
 
 #ifdef CLOCKS_PER_SEC
        /* "To determine the time in seconds, the value returned
 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
 #else
 
+#include <openssl/crypto.h>
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/x509.h>
+#include <openssl/ecdsa.h>
+#include <openssl/engine.h>
+#include <openssl/err.h>
+
 static BIO *bio_err=NULL;
 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
 
@@ -119,7 +120,7 @@ int set_p192_param(ECDSA *ecdsa)
        if ((ctx = BN_CTX_new()) == NULL) goto err;
        clear_ecdsa(ecdsa);
        
-       if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_X9_62_PRIME_192V1)) == NULL)
+       if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_192V1)) == NULL)
        {
                BIO_printf(bio_err,"ECDSA_SET_GROUP_P_192_V1() failed \n");
                goto err;
@@ -152,7 +153,7 @@ int set_p239_param(ECDSA *ecdsa)
        if ((ctx = BN_CTX_new()) == NULL) goto err;
        clear_ecdsa(ecdsa);
        
-       if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_X9_62_PRIME_239V1)) == NULL)
+       if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_X9_62_PRIME_239V1)) == NULL)
        {
                BIO_printf(bio_err,"ECDSA_SET_GROUP_P_239_V1() failed \n");
                goto err;
@@ -180,7 +181,7 @@ int test_sig_vrf(ECDSA *ecdsa, const unsigned char* dgst)
         int       ret=0,type=0;
         unsigned char *buffer=NULL;
         unsigned int  buf_len;
-        clock_t  time;
+        clock_t  tim;
  
         if (!ecdsa || !ecdsa->group || !ecdsa->pub_key || !ecdsa->priv_key)
                 return 0;
@@ -192,24 +193,24 @@ int test_sig_vrf(ECDSA *ecdsa, const unsigned char* dgst)
         if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
                 goto err;
  
-        time = clock();
+        tim = clock();
         if (!ECDSA_sign(type, dgst , 20, buffer, &buf_len, ecdsa))
         {
                 BIO_printf(bio_err, "ECDSA_sign() FAILED \n");
                 goto err;
         }
-        time = clock() - time;
-        BIO_printf(bio_err, " [ ECDSA_sign() %.2f"UNIT, (double)time/(CLOCKS_PER_SEC));
+        tim = clock() - tim;
+        BIO_printf(bio_err, " [ ECDSA_sign() %.2f"UNIT, (double)tim/(CLOCKS_PER_SEC));
  
-        time = clock();
+        tim = clock();
         ret = ECDSA_verify(type, dgst, 20, buffer, buf_len, ecdsa);
         if (ret != 1)
         {
                 BIO_printf(bio_err, "ECDSA_verify() FAILED \n");
                 goto err;
         }
-        time = clock() - time;
-        BIO_printf(bio_err, " and ECDSA_verify() %.2f"UNIT" ] ", (double)time/(CLOCKS_PER_SEC));
+        tim = clock() - tim;
+        BIO_printf(bio_err, " and ECDSA_verify() %.2f"UNIT" ] ", (double)tim/(CLOCKS_PER_SEC));
  
 err:    OPENSSL_free(buffer);
         return(ret == 1);
@@ -323,24 +324,32 @@ int main(void)
        int             dgst_len=0;
        unsigned char   *dgst=NULL;
        int             ret = 0, i=0;
-       clock_t         time;
+       clock_t         tim;
        unsigned char   *buffer=NULL;
        unsigned char   *pp;
        long            buf_len=0;
-       double          time_d;
+       double          tim_d;
        EVP_MD_CTX      *md_ctx=NULL;
        
+       /* enable memory leak checking unless explicitly disabled */
+       if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
+               {
+               CRYPTO_malloc_debug_init();
+               CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
+               }
+       else
+               {
+               /* OPENSSL_DEBUG_MEMORY=off */
+               CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
+               }
+       CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
 
-       RAND_seed(rnd_seed, sizeof(rnd_seed));
+       ERR_load_crypto_strings();
 
        if (bio_err == NULL)
                bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
 
-       CRYPTO_malloc_debug_init();
-       CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
-       CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
-
-       ERR_load_crypto_strings();
+       RAND_seed(rnd_seed, sizeof(rnd_seed));
 
        if ((ecdsa = ECDSA_new()) == NULL)   goto err;
 
@@ -381,23 +390,6 @@ int main(void)
        OPENSSL_free(buffer);
        buffer = NULL;
 
-       /* i2d_ - d2i_ECDSAPublicKey() */
-
-       BIO_printf(bio_err, "\nTesting i2d_ - d2i_ECDSAPublicKey \n");
-       buf_len = i2d_ECDSAPublicKey(ecdsa, NULL);
-       if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) goto err;
-       pp = buffer;
-       if (!i2d_ECDSAPublicKey(ecdsa, &pp)) goto err;
-       pp = buffer;
-       if ((ret_ecdsa = d2i_ECDSAPublicKey(&ret_ecdsa , (const unsigned char**)&pp, 
-                       buf_len)) == NULL) goto err;
-       ECDSA_print(bio_err, ret_ecdsa, 0);
-       if (ecdsa_cmp(ecdsa, ret_ecdsa)) goto err;
-       OPENSSL_free(buffer);
-       buffer = NULL;
-       ECDSA_free(ret_ecdsa);
-       ret_ecdsa = NULL;       
-       
        /* X509_PUBKEY_set() &  X509_PUBKEY_get() */    
 
        BIO_printf(bio_err, "\nTesting X509_PUBKEY_{get,set}            : ");
@@ -532,22 +524,22 @@ int main(void)
        BIO_printf(bio_err, "\nTesting sign & verify with NIST Prime-Curve P-192 : \n");
        ECDSA_free(ecdsa);
        if ((ecdsa = ECDSA_new()) == NULL) goto err;
-       if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_192)) == NULL) goto err;
+       if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_192)) == NULL) goto err;
        if (!ECDSA_generate_key(ecdsa)) goto err;
-        time = clock();
+        tim = clock();
         for (i=0; i<ECDSA_NIST_TESTS; i++)
                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
-        time = clock() - time;
-       time_d = (double)time / CLOCKS_PER_SEC;
+        tim = clock() - tim;
+       tim_d = (double)tim / CLOCKS_PER_SEC;
         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
-               , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
-       time = clock();
+               , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
+       tim = clock();
        for (i=0; i<ECDSA_NIST_TESTS; i++)
                if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
-       time = clock() - time;
-       time_d = (double)time / CLOCKS_PER_SEC;
+       tim = clock() - tim;
+       tim_d = (double)tim / CLOCKS_PER_SEC;
        BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
-                , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
+                , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
        for (i=0; i<ECDSA_NIST_TESTS; i++)
        {
                ECDSA_SIG_free(signatures[i]);
@@ -558,22 +550,22 @@ int main(void)
        BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-224 : \n");
         ECDSA_free(ecdsa);
         if ((ecdsa = ECDSA_new()) == NULL) goto err;
-        if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_224)) == NULL) goto err;
+        if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_224)) == NULL) goto err;
         if (!ECDSA_generate_key(ecdsa)) goto err;
-        time = clock();
+        tim = clock();
         for (i=0; i<ECDSA_NIST_TESTS; i++)
                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
-        time = clock() - time;
-        time_d = (double)time / CLOCKS_PER_SEC;
+        tim = clock() - tim;
+        tim_d = (double)tim / CLOCKS_PER_SEC;
         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
-                , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
-        time = clock();
+                , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
+        tim = clock();
         for (i=0; i<ECDSA_NIST_TESTS; i++)
                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
-        time = clock() - time;
-        time_d = (double)time / CLOCKS_PER_SEC;
+        tim = clock() - tim;
+        tim_d = (double)tim / CLOCKS_PER_SEC;
         BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
-                , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
+                , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
        for (i=0; i<ECDSA_NIST_TESTS; i++)
        {
                ECDSA_SIG_free(signatures[i]);
@@ -584,22 +576,22 @@ int main(void)
         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-256 : \n");
         ECDSA_free(ecdsa);
         if ((ecdsa = ECDSA_new()) == NULL) goto err;
-        if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_256)) == NULL) goto err;
+        if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_256)) == NULL) goto err;
         if (!ECDSA_generate_key(ecdsa)) goto err;
-        time = clock();
+        tim = clock();
         for (i=0; i<ECDSA_NIST_TESTS; i++)
                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
-        time = clock() - time;
-        time_d = (double)time / CLOCKS_PER_SEC;
+        tim = clock() - tim;
+        tim_d = (double)tim / CLOCKS_PER_SEC;
         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
-                , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
-        time = clock();
+                , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
+        tim = clock();
         for (i=0; i<ECDSA_NIST_TESTS; i++)
                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
-        time = clock() - time;
-        time_d = (double)time / CLOCKS_PER_SEC;
+        tim = clock() - tim;
+        tim_d = (double)tim / CLOCKS_PER_SEC;
         BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
-                , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
+                , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
        for (i=0; i<ECDSA_NIST_TESTS; i++)
        {
                ECDSA_SIG_free(signatures[i]);
@@ -610,22 +602,22 @@ int main(void)
         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-384 : \n");
         ECDSA_free(ecdsa);
         if ((ecdsa = ECDSA_new()) == NULL) goto err;
-        if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_384)) == NULL) goto err;
+        if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_384)) == NULL) goto err;
         if (!ECDSA_generate_key(ecdsa)) goto err;
-        time = clock();
+        tim = clock();
         for (i=0; i<ECDSA_NIST_TESTS; i++)
                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
-        time = clock() - time;
-        time_d = (double)time / CLOCKS_PER_SEC;
+        tim = clock() - tim;
+        tim_d = (double)tim / CLOCKS_PER_SEC;
         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
-                , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
-        time = clock();
+                , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
+        tim = clock();
         for (i=0; i<ECDSA_NIST_TESTS; i++)
                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
-        time = clock() - time;
-        time_d = (double)time / CLOCKS_PER_SEC;
+        tim = clock() - tim;
+        tim_d = (double)tim / CLOCKS_PER_SEC;
         BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
-                , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
+                , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
        for (i=0; i<ECDSA_NIST_TESTS; i++)
        {
                ECDSA_SIG_free(signatures[i]);
@@ -636,22 +628,22 @@ int main(void)
         BIO_printf(bio_err, "Testing sign & verify with NIST Prime-Curve P-521 : \n");
         ECDSA_free(ecdsa);
         if ((ecdsa = ECDSA_new()) == NULL) goto err;
-        if ((ecdsa->group = EC_GROUP_get_group_by_name(EC_GROUP_NIST_PRIME_521)) == NULL) goto err;
+        if ((ecdsa->group = EC_GROUP_new_by_name(EC_GROUP_NIST_PRIME_521)) == NULL) goto err;
         if (!ECDSA_generate_key(ecdsa)) goto err;
-        time = clock();
+        tim = clock();
         for (i=0; i<ECDSA_NIST_TESTS; i++)
                 if ((signatures[i] = ECDSA_do_sign(digest[i], 20, ecdsa)) == NULL) goto err;
-        time = clock() - time;
-        time_d = (double)time / CLOCKS_PER_SEC;
+        tim = clock() - tim;
+        tim_d = (double)tim / CLOCKS_PER_SEC;
         BIO_printf(bio_err, "%d x ECDSA_do_sign()   in %.2f"UNIT" => average time for ECDSA_do_sign()   %.4f"UNIT"\n"
-                , ECDSA_NIST_TESTS, time_d, time_d / ECDSA_NIST_TESTS);
-        time = clock();
+                , ECDSA_NIST_TESTS, tim_d, tim_d / ECDSA_NIST_TESTS);
+        tim = clock();
         for (i=0; i<ECDSA_NIST_TESTS; i++)
                 if (!ECDSA_do_verify(digest[i], 20, signatures[i], ecdsa)) goto err;
-        time = clock() - time;
-        time_d = (double)time / CLOCKS_PER_SEC;
+        tim = clock() - tim;
+        tim_d = (double)tim / CLOCKS_PER_SEC;
         BIO_printf(bio_err, "%d x ECDSA_do_verify() in %.2f"UNIT" => average time for ECDSA_do_verify() %.4f"UNIT"\n"
-                , ECDSA_NIST_TESTS, time_d, time_d/ECDSA_NIST_TESTS);
+                , ECDSA_NIST_TESTS, tim_d, tim_d/ECDSA_NIST_TESTS);
        ECDSA_free(ecdsa);
        ecdsa = NULL;
        for (i=0; i<ECDSA_NIST_TESTS; i++)