fix output
[openssl.git] / crypto / ecdh / ecdhtest.c
index a9ee28caf90410f5ec0f52e614730d7e3633f1d4..32b6a9ab8980483667cf3bad674f69f2399ee7b0 100644 (file)
@@ -9,19 +9,6 @@
  * The ECC Code is licensed pursuant to the OpenSSL open source
  * license provided below.
  *
- * In addition, Sun covenants to all licensees who provide a reciprocal
- * covenant with respect to their own patents if any, not to sue under
- * current and future patent claims necessarily infringed by the making,
- * using, practicing, selling, offering for sale and/or otherwise
- * disposing of the ECC Code as delivered hereunder (or portions thereof),
- * provided that such covenant shall not apply:
- *  1) for code that a licensee deletes from the ECC Code;
- *  2) separates from the ECC Code; or
- *  3) for infringements caused by:
- *       i) the modification of the ECC Code or
- *      ii) the combination of the ECC Code with other software or
- *          devices where such combination causes the infringement.
- *
  * The ECDH software is originally written by Douglas Stebila of
  * Sun Microsystems Laboratories.
  *
@@ -109,7 +96,10 @@ int main(int argc, char *argv[])
 #define MS_CALLBACK
 #endif
 
+#if 0
 static void MS_CALLBACK cb(int p, int n, void *arg);
+#endif
+
 #ifdef OPENSSL_NO_STDIO
 #define APPS_WIN16
 #include "bss_file.c"
@@ -123,62 +113,83 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
        {
        EC_KEY *a=NULL;
        EC_KEY *b=NULL;
-       BIGNUM *x=NULL, *y=NULL;
+       BIGNUM *x_a=NULL, *y_a=NULL,
+              *x_b=NULL, *y_b=NULL;
        char buf[12];
        unsigned char *abuf=NULL,*bbuf=NULL;
        int i,alen,blen,aout,bout,ret=0;
 
        if ((a=EC_KEY_new()) == NULL) goto err;
-       if ((a->group=EC_GROUP_new_by_name(nid)) == NULL) goto err;
+       if ((a->group=EC_GROUP_new_by_nid(nid)) == NULL) goto err;
 
        if ((b=EC_KEY_new()) == NULL) goto err;
        b->group = a->group;
 
-       if ((x=BN_new()) == NULL) goto err;
-       if ((y=BN_new()) == NULL) goto err;
+       if ((x_a=BN_new()) == NULL) goto err;
+       if ((y_a=BN_new()) == NULL) goto err;
+       if ((x_b=BN_new()) == NULL) goto err;
+       if ((y_b=BN_new()) == NULL) goto err;
 
        BIO_puts(out,"Testing key generation with ");
        BIO_puts(out,text);
+#ifdef NOISY
        BIO_puts(out,"\n");
+#else
+       BIO_flush(out);
+#endif
 
        if (!EC_KEY_generate_key(a)) goto err;
-       BIO_puts(out,"  pri 1=");
-       BN_print(out,a->priv_key);
-       BIO_puts(out,"\n  pub 1=");
+       
        if (EC_METHOD_get_field_type(EC_GROUP_method_of(a->group)) == NID_X9_62_prime_field) 
                {
-               if (!EC_POINT_get_affine_coordinates_GFp(a->group, a->pub_key, x, y, ctx)) goto err;
+               if (!EC_POINT_get_affine_coordinates_GFp(a->group, a->pub_key, x_a, y_a, ctx)) goto err;
                }
        else
                {
-               if (!EC_POINT_get_affine_coordinates_GF2m(a->group, a->pub_key, x, y, ctx)) goto err;
+               if (!EC_POINT_get_affine_coordinates_GF2m(a->group, a->pub_key, x_a, y_a, ctx)) goto err;
                }
-       BN_print(out,x);
+#ifdef NOISY
+       BIO_puts(out,"  pri 1=");
+       BN_print(out,a->priv_key);
+       BIO_puts(out,"\n  pub 1=");
+       BN_print(out,x_a);
        BIO_puts(out,",");
-       BN_print(out,y);
+       BN_print(out,y_a);
        BIO_puts(out,"\n");
+#else
+       BIO_printf(out," .");
+       BIO_flush(out);
+#endif
 
        if (!EC_KEY_generate_key(b)) goto err;
-       BIO_puts(out,"  pri 2=");
-       BN_print(out,b->priv_key);
-       BIO_puts(out,"\n  pub 2=");
+
        if (EC_METHOD_get_field_type(EC_GROUP_method_of(b->group)) == NID_X9_62_prime_field) 
                {
-               if (!EC_POINT_get_affine_coordinates_GFp(b->group, b->pub_key, x, y, ctx)) goto err;
+               if (!EC_POINT_get_affine_coordinates_GFp(b->group, b->pub_key, x_b, y_b, ctx)) goto err;
                }
        else
                {
-               if (!EC_POINT_get_affine_coordinates_GF2m(a->group, b->pub_key, x, y, ctx)) goto err;
+               if (!EC_POINT_get_affine_coordinates_GF2m(a->group, b->pub_key, x_b, y_b, ctx)) goto err;
                }
-       BN_print(out,x);
+
+#ifdef NOISY
+       BIO_puts(out,"  pri 2=");
+       BN_print(out,b->priv_key);
+       BIO_puts(out,"\n  pub 2=");
+       BN_print(out,x_b);
        BIO_puts(out,",");
-       BN_print(out,y);
+       BN_print(out,y_b);
        BIO_puts(out,"\n");
+#else
+       BIO_printf(out,".");
+       BIO_flush(out);
+#endif
 
        alen=ECDH_size(a);
        abuf=(unsigned char *)OPENSSL_malloc(alen);
        aout=ECDH_compute_key(abuf,b->pub_key,a);
 
+#ifdef NOISY
        BIO_puts(out,"  key1 =");
        for (i=0; i<aout; i++)
                {
@@ -186,11 +197,16 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
                BIO_puts(out,buf);
                }
        BIO_puts(out,"\n");
+#else
+       BIO_printf(out,".");
+       BIO_flush(out);
+#endif
 
        blen=ECDH_size(b);
        bbuf=(unsigned char *)OPENSSL_malloc(blen);
        bout=ECDH_compute_key(bbuf,a->pub_key,b);
 
+#ifdef NOISY
        BIO_puts(out,"  key2 =");
        for (i=0; i<bout; i++)
                {
@@ -198,20 +214,66 @@ int test_ecdh_curve(int nid, char *text, BN_CTX *ctx, BIO *out)
                BIO_puts(out,buf);
                }
        BIO_puts(out,"\n");
+#else
+       BIO_printf(out,".");
+       BIO_flush(out);
+#endif
+
        if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
                {
+#ifndef NOISY
+               BIO_printf(out, " failed\n\n");
+               BIO_printf(out, "key a:\n");
+               BIO_printf(out, "private key: ");
+               BN_print(out, a->priv_key);
+               BIO_printf(out, "\n");
+               BIO_printf(out, "public key (x,y): ");
+               BN_print(out, x_a);
+               BIO_printf(out, ",");
+               BN_print(out, y_a);
+               BIO_printf(out, "\nkey b:\n");
+               BIO_printf(out, "private key: ");
+               BN_print(out, b->priv_key);
+               BIO_printf(out, "\n");
+               BIO_printf(out, "public key (x,y): ");
+               BN_print(out, x_b);
+               BIO_printf(out, ",");
+               BN_print(out, y_b);
+               BIO_printf(out, "\n");
+               BIO_printf(out, "generated key a: ");
+               for (i=0; i<bout; i++)
+                       {
+                       sprintf(buf, "%02X", bbuf[i]);
+                       BIO_puts(out, buf);
+                       }
+               BIO_printf(out, "\n");
+               BIO_printf(out, "generated key b: ");
+               for (i=0; i<aout; i++)
+                       {
+                       sprintf(buf, "%02X", abuf[i]);
+                       BIO_puts(out,buf);
+                       }
+               BIO_printf(out, "\n");
+#endif
                fprintf(stderr,"Error in ECDH routines\n");
                ret=0;
                }
        else
+               {
+#ifndef NOISY
+               BIO_printf(out, " ok\n");
+#endif
                ret=1;
+               }
 err:
        ERR_print_errors_fp(stderr);
 
        if (abuf != NULL) OPENSSL_free(abuf);
        if (bbuf != NULL) OPENSSL_free(bbuf);
-       if (x) BN_free(x);
-       if (y) BN_free(y);
+       if (x_a) BN_free(x_a);
+       if (y_a) BN_free(y_a);
+       if (x_b) BN_free(x_b);
+       if (y_b) BN_free(y_b);
        if (a->group) EC_GROUP_free(a->group);
        a->group = b->group = NULL;
        if (b) EC_KEY_free(b);
@@ -242,22 +304,22 @@ int main(int argc, char *argv[])
        if ((ctx=BN_CTX_new()) == NULL) goto err;
 
        /* NIST PRIME CURVES TESTS */
-       if (!test_ecdh_curve(EC_GROUP_NIST_PRIME_192, "NIST Prime-Curve P-192", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_PRIME_224, "NIST Prime-Curve P-224", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_PRIME_256, "NIST Prime-Curve P-256", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_PRIME_384, "NIST Prime-Curve P-384", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_PRIME_521, "NIST Prime-Curve P-521", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out)) goto err;
        /* NIST BINARY CURVES TESTS */
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_K163, "NIST Binary-Curve K-163", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_B163, "NIST Binary-Curve B-163", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_K233, "NIST Binary-Curve K-233", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_B233, "NIST Binary-Curve B-233", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_K283, "NIST Binary-Curve K-283", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_B283, "NIST Binary-Curve B-283", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_K409, "NIST Binary-Curve K-409", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_B409, "NIST Binary-Curve B-409", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_K571, "NIST Binary-Curve K-571", ctx, out)) goto err;
-       if (!test_ecdh_curve(EC_GROUP_NIST_CHAR2_B571, "NIST Binary-Curve B-571", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect163k1, "NIST Binary-Curve K-163", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect163r2, "NIST Binary-Curve B-163", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect233k1, "NIST Binary-Curve K-233", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect233r1, "NIST Binary-Curve B-233", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect283k1, "NIST Binary-Curve K-283", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect283r1, "NIST Binary-Curve B-283", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect409k1, "NIST Binary-Curve K-409", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect409r1, "NIST Binary-Curve B-409", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect571k1, "NIST Binary-Curve K-571", ctx, out)) goto err;
+       if (!test_ecdh_curve(NID_sect571r1, "NIST Binary-Curve B-571", ctx, out)) goto err;
 
        ret = 0;
 
@@ -272,6 +334,7 @@ err:
        return(ret);
        }
 
+#if 0
 static void MS_CALLBACK cb(int p, int n, void *arg)
        {
        char c='*';
@@ -287,3 +350,4 @@ static void MS_CALLBACK cb(int p, int n, void *arg)
 #endif
        }
 #endif
+#endif