Cleanup OPENSSL_NO_xxx, part 1
[oweals/openssl.git] / crypto / ec / ectest.c
index 102eaa9b2326f94102eb68de7f6fce013172682f..16cf43f0edcd94f655564ce9a8c36503d05cb306 100644 (file)
@@ -164,8 +164,6 @@ static void timings(EC_GROUP *group, int type, BN_CTX *ctx)
         *                                       -- ISO/IEC 9899 */
 #      define UNIT "s"
 #else
-       /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
-        *                            -- cc on NeXTstep/OpenStep */
 #      define UNIT "units"
 #      define CLOCKS_PER_SEC 1
 #endif
@@ -199,6 +197,7 @@ static void group_order_tests(EC_GROUP *group)
        EC_POINT *P = EC_POINT_new(group);
        EC_POINT *Q = EC_POINT_new(group);
        BN_CTX *ctx = BN_CTX_new();
+       int i;
 
        n1 = BN_new(); n2 = BN_new(); order = BN_new();
        fprintf(stdout, "verify group order ...");
@@ -212,21 +211,56 @@ static void group_order_tests(EC_GROUP *group)
        if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT;
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, " ok\n");
-       fprintf(stdout, "long/negative scalar tests ... ");
-       if (!BN_one(n1)) ABORT;
-       /* n1 = 1 - order */
-       if (!BN_sub(n1, n1, order)) ABORT;
-       if(!EC_POINT_mul(group, Q, NULL, P, n1, ctx)) ABORT;
-       if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
-       /* n2 = 1 + order */
-       if (!BN_add(n2, order, BN_value_one())) ABORT;
-       if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
-       if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
-       /* n2 = (1 - order) * (1 + order) */
-       if (!BN_mul(n2, n1, n2, ctx)) ABORT;
-       if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
-       if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
+       fprintf(stdout, "long/negative scalar tests ");
+        for (i = 1; i <= 2; i++)
+               {
+               const BIGNUM *scalars[6];
+               const EC_POINT *points[6];
+
+               fprintf(stdout, i == 1 ?
+                       "allowing precomputation ... " :
+                       "without precomputation ... ");
+               if (!BN_set_word(n1, i)) ABORT;
+               /* If i == 1, P will be the predefined generator for which
+                * EC_GROUP_precompute_mult has set up precomputation. */
+               if (!EC_POINT_mul(group, P, n1, NULL, NULL, ctx)) ABORT;
+
+               if (!BN_one(n1)) ABORT;
+               /* n1 = 1 - order */
+               if (!BN_sub(n1, n1, order)) ABORT;
+               if (!EC_POINT_mul(group, Q, NULL, P, n1, ctx)) ABORT;
+               if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
+
+               /* n2 = 1 + order */
+               if (!BN_add(n2, order, BN_value_one())) ABORT;
+               if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
+               if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
+
+               /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
+               if (!BN_mul(n2, n1, n2, ctx)) ABORT;
+               if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
+               if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
+
+               /* n2 = order^2 - 1 */
+               BN_set_negative(n2, 0);
+               if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
+               /* Add P to verify the result. */
+               if (!EC_POINT_add(group, Q, Q, P, ctx)) ABORT;
+               if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
+
+               /* Exercise EC_POINTs_mul, including corner cases. */
+               if (EC_POINT_is_at_infinity(group, P)) ABORT;
+               scalars[0] = n1; points[0] = Q; /* => infinity */
+               scalars[1] = n2; points[1] = P; /* => -P */
+               scalars[2] = n1; points[2] = Q; /* => infinity */
+               scalars[3] = n2; points[3] = Q; /* => infinity */
+               scalars[4] = n1; points[4] = P; /* => P */
+               scalars[5] = n2; points[5] = Q; /* => infinity */
+               if (!EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx)) ABORT;
+               if (!EC_POINT_is_at_infinity(group, P)) ABORT;
+               }
        fprintf(stdout, "ok\n");
+
        EC_POINT_free(P);
        EC_POINT_free(Q);
        BN_free(n1);
@@ -631,7 +665,7 @@ static void prime_field_tests(void)
        {
                const EC_POINT *points[4];
                const BIGNUM *scalars[4];
-               BIGNUM scalar3;
+               BIGNUM *scalar3;
        
                if (EC_POINT_is_at_infinity(group, Q)) ABORT;
                points[0] = Q;
@@ -677,16 +711,17 @@ static void prime_field_tests(void)
                scalars[1] = y;
                scalars[2] = z; /* z = -(x+y) */
 
-               BN_init(&scalar3);
-               BN_zero(&scalar3);
-               scalars[3] = &scalar3;
+               scalar3 = BN_new();
+               if(!scalar3) ABORT;
+               BN_zero(scalar3);
+               scalars[3] = scalar3;
 
                if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx)) ABORT;
                if (!EC_POINT_is_at_infinity(group, P)) ABORT;
 
                fprintf(stdout, " ok\n\n");
 
-               BN_free(&scalar3);
+               BN_free(scalar3);
        }
 
 
@@ -1330,7 +1365,7 @@ static const struct nistp_test_params nistp_tests_params[] =
                },
        };
 
-void nistp_single_test(const struct nistp_test_params *test)
+static void nistp_single_test(const struct nistp_test_params *test)
        {
        BN_CTX *ctx;
        BIGNUM *p, *a, *b, *x, *y, *n, *m, *order;
@@ -1433,7 +1468,7 @@ void nistp_single_test(const struct nistp_test_params *test)
        BN_CTX_free(ctx);
        }
 
-void nistp_tests()
+static void nistp_tests()
        {
        unsigned i;