do tests with all built-in curves
[openssl.git] / crypto / ec / ectest.c
index 9d46d150783211f0d3988d127b90f18224f22495..96ff87d7ec384e3ce074c6af1d6763cc23560a6e 100644 (file)
  * The Contribution is licensed pursuant to the OpenSSL open source
  * license provided above.
  *
- * 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 Contribution as delivered hereunder 
- * (or portions thereof), provided that such covenant shall not apply:
- *  1) for code that a licensee deletes from the Contribution;
- *  2) separates from the Contribution; or
- *  3) for infringements caused by:
- *       i) the modification of the Contribution or
- *      ii) the combination of the Contribution with other software or
- *          devices where such combination causes the infringement.
- *
  * The elliptic curve binary polynomial software is originally written by 
  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  *
@@ -97,6 +84,7 @@ int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); retur
 #include <openssl/engine.h>
 #include <openssl/err.h>
 #include <openssl/obj_mac.h>
+#include <openssl/objects.h>
 
 #define ABORT do { \
        fflush(stdout); \
@@ -105,6 +93,10 @@ int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); retur
        exit(1); \
 } while (0)
 
+void prime_field_tests(void);
+void char2_field_tests(void);
+void internal_curve_test(void);
+
 #if 0
 static void timings(EC_GROUP *group, int multi, BN_CTX *ctx)
        {
@@ -613,7 +605,7 @@ void prime_field_tests()
 
                if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
                if (!BN_add(z, z, y)) ABORT;
-               z->neg = 1;
+               BN_set_sign(z, 1);
                scalars[0] = y;
                scalars[1] = z; /* z = -(order + y) */
 
@@ -625,7 +617,7 @@ void prime_field_tests()
 
                if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
                if (!BN_add(z, x, y)) ABORT;
-               z->neg = 1;
+               BN_set_sign(z, 1);
                scalars[0] = x;
                scalars[1] = y;
                scalars[2] = z; /* z = -(x+y) */
@@ -752,8 +744,8 @@ void char2_field_tests()
        if (!BN_hex2bn(&a, "3")) ABORT;
        if (!BN_hex2bn(&b, "1")) ABORT;
        
-       group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GFp
-                                                    * so that the library gets to choose the EC_METHOD */
+       group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GF2m
+                                                       * so that the library gets to choose the EC_METHOD */
        if (!group) ABORT;
        if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT;
 
@@ -774,7 +766,7 @@ void char2_field_tests()
        BN_print_fp(stdout, a);
        fprintf(stdout, "\n     b = 0x");
        BN_print_fp(stdout, b);
-       fprintf(stdout, "\n");
+       fprintf(stdout, "\n(0x... means binary polynomial)\n");
 
        P = EC_POINT_new(group);
        Q = EC_POINT_new(group);
@@ -1079,7 +1071,7 @@ void char2_field_tests()
 
                if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
                if (!BN_add(z, z, y)) ABORT;
-               z->neg = 1;
+               BN_set_sign(z, 1);
                scalars[0] = y;
                scalars[1] = z; /* z = -(order + y) */
 
@@ -1091,7 +1083,7 @@ void char2_field_tests()
 
                if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
                if (!BN_add(z, x, y)) ABORT;
-               z->neg = 1;
+               BN_set_sign(z, 1);
                scalars[0] = x;
                scalars[1] = y;
                scalars[2] = z; /* z = -(x+y) */
@@ -1149,6 +1141,60 @@ void char2_field_tests()
 
        }
 
+void internal_curve_test(void)
+       {
+       EC_builtin_curve *curves = NULL;
+       size_t crv_len = 0, n = 0;
+       int    ok = 1;
+
+       crv_len = EC_get_builtin_curves(NULL, 0);
+
+       curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
+
+       if (curves == NULL)
+               return;
+
+       if (!EC_get_builtin_curves(curves, crv_len))
+               {
+               OPENSSL_free(curves);
+               return;
+               }
+
+       fprintf(stdout, "testing internal curves: ");
+               
+       for (n = 0; n < crv_len; n++)
+               {
+               EC_GROUP *group = NULL;
+               int nid = curves[n].nid;
+               if ((group = EC_GROUP_new_by_nid(nid)) == NULL)
+                       {
+                       ok = 0;
+                       fprintf(stdout, "\nEC_GROUP_new_by_nid() failed with"
+                               " curve %s\n", OBJ_nid2sn(nid));
+                       /* try next curve */
+                       continue;
+                       }
+               if (!EC_GROUP_check(group, NULL))
+                       {
+                       ok = 0;
+                       fprintf(stdout, "\nEC_GROUP_check() failed with"
+                               " curve %s\n", OBJ_nid2sn(nid));
+                       EC_GROUP_free(group);
+                       /* try the next curve */
+                       continue;
+                       }
+               fprintf(stdout, ".");
+               fflush(stdout);
+               EC_GROUP_free(group);
+               }
+       if (ok)
+               fprintf(stdout, " ok\n");
+       else
+               fprintf(stdout, " failed\n");
+       OPENSSL_free(curves);
+       return;
+       }
+
 static const char rnd_seed[] = "string to make the random number generator think it has entropy";
 
 int main(int argc, char *argv[])
@@ -1171,7 +1217,10 @@ int main(int argc, char *argv[])
        RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
 
        prime_field_tests();
+       puts("");
        char2_field_tests();
+       /* test the internal curves */
+       internal_curve_test();
 
        ENGINE_cleanup();
        CRYPTO_cleanup_all_ex_data();