Harden ASN.1 BIO handling of large amounts of data.
[openssl.git] / crypto / ecdh / ecdhtest.c
index 212a87efa4ef1d3da5db9a9b1b6d2542d6eb6c79..996321d1ee430ba9e9895417077d99319d5b4ab1 100644 (file)
@@ -21,7 +21,7 @@
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer. 
+ *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
@@ -73,7 +73,7 @@
 
 #include "../e_os.h"
 
-#include <openssl/opensslconf.h>       /* for OPENSSL_NO_ECDH */
+#include <openssl/opensslconf.h> /* for OPENSSL_NO_ECDH */
 #include <openssl/crypto.h>
 #include <openssl/bio.h>
 #include <openssl/bn.h>
 int main(int argc, char *argv[])
 {
     printf("No ECDH support\n");
-    return(0);
+    return (0);
 }
 #else
-#include <openssl/ec.h>
-#include <openssl/ecdh.h>
+# include <openssl/ec.h>
+# include <openssl/ecdh.h>
 
-#ifdef OPENSSL_SYS_WIN16
-#define MS_CALLBACK    _far _loadds
-#else
-#define MS_CALLBACK
-#endif
+# ifdef OPENSSL_SYS_WIN16
+#  define MS_CALLBACK     _far _loadds
+# else
+#  define MS_CALLBACK
+# endif
 
-#if 0
+# if 0
 static void MS_CALLBACK cb(int p, int n, void *arg);
-#endif
-
-static const char rnd_seed[] = "string to make the random number generator think it has entropy";
+# endif
 
+static const char rnd_seed[] =
+    "string to make the random number generator think it has entropy";
 
 static const int KDF1_SHA1_len = 20;
-static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
-       {
-#ifndef OPENSSL_NO_SHA
-       if (*outlen < SHA_DIGEST_LENGTH)
-               return NULL;
-       else
-               *outlen = SHA_DIGEST_LENGTH;
-       return SHA1(in, inlen, out);
-#else
-       return NULL;
-#endif
-       }
-
+static void *KDF1_SHA1(const void *in, size_t inlen, void *out,
+                       size_t *outlen)
+{
+# ifndef OPENSSL_NO_SHA
+    if (*outlen < SHA_DIGEST_LENGTH)
+        return NULL;
+    else
+        *outlen = SHA_DIGEST_LENGTH;
+    return SHA1(in, inlen, out);
+# else
+    return NULL;
+# endif
+}
 
 static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
-       {
-       EC_KEY *a=NULL;
-       EC_KEY *b=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;
-       const EC_GROUP *group;
-
-       a = EC_KEY_new_by_curve_name(nid);
-       b = EC_KEY_new_by_curve_name(nid);
-       if (a == NULL || b == NULL)
-               goto err;
-
-       group = EC_KEY_get0_group(a);
-
-       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
-       (void)BIO_flush(out);
-#endif
-
-       if (!EC_KEY_generate_key(a)) goto err;
-       
-       if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) 
-               {
-               if (!EC_POINT_get_affine_coordinates_GFp(group,
-                       EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
-               }
-       else
-               {
-               if (!EC_POINT_get_affine_coordinates_GF2m(group,
-                       EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
-               }
-#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_a);
-       BIO_puts(out,"\n");
-#else
-       BIO_printf(out," .");
-       (void)BIO_flush(out);
-#endif
-
-       if (!EC_KEY_generate_key(b)) goto err;
-
-       if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) 
-               {
-               if (!EC_POINT_get_affine_coordinates_GFp(group, 
-                       EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
-               }
-       else
-               {
-               if (!EC_POINT_get_affine_coordinates_GF2m(group, 
-                       EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
-               }
-
-#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_b);
-       BIO_puts(out,"\n");
-#else
-       BIO_printf(out,".");
-       (void)BIO_flush(out);
-#endif
-
-       alen=KDF1_SHA1_len;
-       abuf=(unsigned char *)OPENSSL_malloc(alen);
-       aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1);
-
-#ifdef NOISY
-       BIO_puts(out,"  key1 =");
-       for (i=0; i<aout; i++)
-               {
-               sprintf(buf,"%02X",abuf[i]);
-               BIO_puts(out,buf);
-               }
-       BIO_puts(out,"\n");
-#else
-       BIO_printf(out,".");
-       (void)BIO_flush(out);
-#endif
-
-       blen=KDF1_SHA1_len;
-       bbuf=(unsigned char *)OPENSSL_malloc(blen);
-       bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1);
-
-#ifdef NOISY
-       BIO_puts(out,"  key2 =");
-       for (i=0; i<bout; i++)
-               {
-               sprintf(buf,"%02X",bbuf[i]);
-               BIO_puts(out,buf);
-               }
-       BIO_puts(out,"\n");
-#else
-       BIO_printf(out,".");
-       (void)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, EC_KEY_get0_private_key(a));
-               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, EC_KEY_get0_private_key(b));
-               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_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 (b) EC_KEY_free(b);
-       if (a) EC_KEY_free(a);
-       return(ret);
-       }
+{
+    EC_KEY *a = NULL;
+    EC_KEY *b = 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;
+    const EC_GROUP *group;
+
+    a = EC_KEY_new_by_curve_name(nid);
+    b = EC_KEY_new_by_curve_name(nid);
+    if (a == NULL || b == NULL)
+        goto err;
+
+    group = EC_KEY_get0_group(a);
+
+    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
+    (void)BIO_flush(out);
+# endif
+
+    if (!EC_KEY_generate_key(a))
+        goto err;
+
+    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
+        NID_X9_62_prime_field) {
+        if (!EC_POINT_get_affine_coordinates_GFp
+            (group, EC_KEY_get0_public_key(a), x_a, y_a, ctx))
+            goto err;
+    }
+# ifndef OPENSSL_NO_EC2M
+    else {
+        if (!EC_POINT_get_affine_coordinates_GF2m(group,
+                                                  EC_KEY_get0_public_key(a),
+                                                  x_a, y_a, ctx))
+            goto err;
+    }
+# endif
+# 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_a);
+    BIO_puts(out, "\n");
+# else
+    BIO_printf(out, " .");
+    (void)BIO_flush(out);
+# endif
+
+    if (!EC_KEY_generate_key(b))
+        goto err;
+
+    if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
+        NID_X9_62_prime_field) {
+        if (!EC_POINT_get_affine_coordinates_GFp
+            (group, EC_KEY_get0_public_key(b), x_b, y_b, ctx))
+            goto err;
+    }
+# ifndef OPENSSL_NO_EC2M
+    else {
+        if (!EC_POINT_get_affine_coordinates_GF2m(group,
+                                                  EC_KEY_get0_public_key(b),
+                                                  x_b, y_b, ctx))
+            goto err;
+    }
+# endif
+
+# 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_b);
+    BIO_puts(out, "\n");
+# else
+    BIO_printf(out, ".");
+    (void)BIO_flush(out);
+# endif
+
+    alen = KDF1_SHA1_len;
+    abuf = (unsigned char *)OPENSSL_malloc(alen);
+    aout =
+        ECDH_compute_key(abuf, alen, EC_KEY_get0_public_key(b), a, KDF1_SHA1);
+
+# ifdef NOISY
+    BIO_puts(out, "  key1 =");
+    for (i = 0; i < aout; i++) {
+        sprintf(buf, "%02X", abuf[i]);
+        BIO_puts(out, buf);
+    }
+    BIO_puts(out, "\n");
+# else
+    BIO_printf(out, ".");
+    (void)BIO_flush(out);
+# endif
+
+    blen = KDF1_SHA1_len;
+    bbuf = (unsigned char *)OPENSSL_malloc(blen);
+    bout =
+        ECDH_compute_key(bbuf, blen, EC_KEY_get0_public_key(a), b, KDF1_SHA1);
+
+# ifdef NOISY
+    BIO_puts(out, "  key2 =");
+    for (i = 0; i < bout; i++) {
+        sprintf(buf, "%02X", bbuf[i]);
+        BIO_puts(out, buf);
+    }
+    BIO_puts(out, "\n");
+# else
+    BIO_printf(out, ".");
+    (void)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, EC_KEY_get0_private_key(a));
+        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, EC_KEY_get0_private_key(b));
+        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_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 (b)
+        EC_KEY_free(b);
+    if (a)
+        EC_KEY_free(a);
+    return (ret);
+}
 
 int main(int argc, char *argv[])
-       {
-       BN_CTX *ctx=NULL;
-       int ret=1;
-       BIO *out;
-
-       CRYPTO_malloc_debug_init();
-       CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
-       CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
-
-#ifdef OPENSSL_SYS_WIN32
-       CRYPTO_malloc_init();
-#endif
+{
+    BN_CTX *ctx = NULL;
+    int ret = 1;
+    BIO *out;
+
+    CRYPTO_malloc_debug_init();
+    CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
+    CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
+
+# ifdef OPENSSL_SYS_WIN32
+    CRYPTO_malloc_init();
+# endif
+
+    RAND_seed(rnd_seed, sizeof rnd_seed);
+
+    out = BIO_new(BIO_s_file());
+    if (out == NULL)
+        EXIT(1);
+    BIO_set_fp(out, stdout, BIO_NOCLOSE);
+
+    if ((ctx = BN_CTX_new()) == NULL)
+        goto err;
+
+    /* NIST PRIME CURVES TESTS */
+    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;
+# ifndef OPENSSL_NO_EC2M
+    /* NIST BINARY CURVES TESTS */
+    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;
+# endif
+
+    ret = 0;
+
+ err:
+    ERR_print_errors_fp(stderr);
+    if (ctx)
+        BN_CTX_free(ctx);
+    BIO_free(out);
+    CRYPTO_cleanup_all_ex_data();
+    ERR_remove_thread_state(NULL);
+    CRYPTO_mem_leaks_fp(stderr);
+    EXIT(ret);
+    return (ret);
+}
 
-       RAND_seed(rnd_seed, sizeof rnd_seed);
-
-       out=BIO_new(BIO_s_file());
-       if (out == NULL) EXIT(1);
-       BIO_set_fp(out,stdout,BIO_NOCLOSE);
-
-       if ((ctx=BN_CTX_new()) == NULL) goto err;
-
-       /* NIST PRIME CURVES TESTS */
-       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(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;
-
-err:
-       ERR_print_errors_fp(stderr);
-       if (ctx) BN_CTX_free(ctx);
-       BIO_free(out);
-       CRYPTO_cleanup_all_ex_data();
-       ERR_remove_thread_state(NULL);
-       CRYPTO_mem_leaks_fp(stderr);
-       EXIT(ret);
-       return(ret);
-       }
-
-#if 0
+# if 0
 static void MS_CALLBACK cb(int p, int n, void *arg)
-       {
-       char c='*';
-
-       if (p == 0) c='.';
-       if (p == 1) c='+';
-       if (p == 2) c='*';
-       if (p == 3) c='\n';
-       BIO_write((BIO *)arg,&c,1);
-       (void)BIO_flush((BIO *)arg);
-#ifdef LINT
-       p=n;
-#endif
-       }
-#endif
+{
+    char c = '*';
+
+    if (p == 0)
+        c = '.';
+    if (p == 1)
+        c = '+';
+    if (p == 2)
+        c = '*';
+    if (p == 3)
+        c = '\n';
+    BIO_write((BIO *)arg, &c, 1);
+    (void)BIO_flush((BIO *)arg);
+#  ifdef LINT
+    p = n;
+#  endif
+}
+# endif
 #endif