Tolerate a Certificate using a non-supported group on server side
[openssl.git] / test / ectest.c
index 37bffc65e1932c4f4e7e833571620cffad0b8edb..66d84a75cd789265bdc45074d47d73cf0eae3ad0 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright 2001-2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  *
  * Licensed under the OpenSSL license (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -7,21 +8,7 @@
  * https://www.openssl.org/source/license.html
  */
 
-/* ====================================================================
- * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
- *
- * Portions of the attached software ("Contribution") are developed by
- * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
- *
- * The Contribution is licensed pursuant to the OpenSSL open source
- * license provided above.
- *
- * The elliptic curve binary polynomial software is originally written by
- * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
- *
- */
-
-#include "e_os.h"
+#include "internal/nelem.h"
 #include "testutil.h"
 
 #ifndef OPENSSL_NO_EC
 # include <openssl/bn.h>
 # include <openssl/opensslconf.h>
 
-# if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
-/* suppress "too big too optimize" warning */
-#  pragma warning(disable:4959)
-# endif
-
 static size_t crv_len = 0;
 static EC_builtin_curve *curves = NULL;
 
@@ -250,7 +232,7 @@ static int prime_field_tests(void)
 
     len =
         EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
-                           sizeof buf, ctx);
+                           sizeof(buf), ctx);
     if (!TEST_size_t_ne(len, 0)
         || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
         || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
@@ -259,7 +241,7 @@ static int prime_field_tests(void)
                        buf, len);
 
     len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
-                             buf, sizeof buf, ctx);
+                             buf, sizeof(buf), ctx);
     if (!TEST_size_t_ne(len, 0)
         || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
         || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
@@ -268,7 +250,7 @@ static int prime_field_tests(void)
                        buf, len);
 
     len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID,
-                             buf, sizeof buf, ctx);
+                             buf, sizeof(buf), ctx);
     if (!TEST_size_t_ne(len, 0)
         || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
         || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
@@ -590,7 +572,7 @@ static int prime_field_tests(void)
         || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
         || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
         || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx))
-        || !TEST_true(BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
+        || !TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
         || !TEST_true(BN_add(z, z, y)))
         goto err;
     BN_set_negative(z, 1);
@@ -599,7 +581,7 @@ static int prime_field_tests(void)
 
     if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
         || !TEST_true(EC_POINT_is_at_infinity(group, P))
-        || !TEST_true(BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
+        || !TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
         || !TEST_true(BN_add(z, x, y)))
         goto err;
     BN_set_negative(z, 1);
@@ -934,7 +916,7 @@ static int char2_curve_test(int n)
             || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)))
             goto err;
 
-        if (!TEST_true(BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
+        if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
             || !TEST_true(BN_add(z, z, y)))
             goto err;
         BN_set_negative(z, 1);
@@ -945,7 +927,7 @@ static int char2_curve_test(int n)
             || !TEST_true(EC_POINT_is_at_infinity(group, P)))
             goto err;
 
-        if (!TEST_true(BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
+        if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
             || !TEST_true(BN_add(z, x, y)))
             goto err;
         BN_set_negative(z, 1);
@@ -1089,7 +1071,7 @@ static int char2_field_tests(void)
 /* Change test based on whether binary point compression is enabled or not. */
 #  ifdef OPENSSL_EC_BIN_PT_COMP
     len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED,
-                             buf, sizeof buf, ctx);
+                             buf, sizeof(buf), ctx);
     if (!TEST_size_t_ne(len, 0)
         || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
         || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
@@ -1099,7 +1081,7 @@ static int char2_field_tests(void)
 #  endif
 
     len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
-                             buf, sizeof buf, ctx);
+                             buf, sizeof(buf), ctx);
     if (!TEST_size_t_ne(len, 0)
         || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
         || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
@@ -1110,7 +1092,7 @@ static int char2_field_tests(void)
 /* Change test based on whether binary point compression is enabled or not. */
 #  ifdef OPENSSL_EC_BIN_PT_COMP
     len =
-        EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf,
+        EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof(buf),
                            ctx);
     if (!TEST_size_t_ne(len, 0)
         || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
@@ -1170,12 +1152,6 @@ static int internal_curve_test_method(int n)
     int r, nid = curves[n].nid;
     EC_GROUP *group;
 
-    /*
-     * Skip for X25519 because low level operations such as EC_POINT_mul()
-     * are not supported for this curve
-     */
-    if (nid == NID_X25519)
-        return 1;
     if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
         TEST_info("Curve %s failed\n", OBJ_nid2sn(nid));
         return 0;
@@ -1438,22 +1414,15 @@ static int parameter_test(void)
     ECPARAMETERS_free(ecparameters);
     return r;
 }
-
-static const char rnd_seed[] =
-    "string to make the random number generator think it has entropy";
 #endif
 
-int test_main(int argc, char *argv[])
+int setup_tests(void)
 {
-    int result = EXIT_SUCCESS;
 #ifndef OPENSSL_NO_EC
-
     crv_len = EC_get_builtin_curves(NULL, 0);
     if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
         || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
-        return EXIT_FAILURE;
-
-    RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
+        return 0;
 
     ADD_TEST(parameter_test);
     ADD_TEST(prime_field_tests);
@@ -1466,9 +1435,13 @@ int test_main(int argc, char *argv[])
 # endif
     ADD_ALL_TESTS(internal_curve_test, crv_len);
     ADD_ALL_TESTS(internal_curve_test_method, crv_len);
+#endif
+    return 1;
+}
 
-    result = run_tests(argv[0]);
+void cleanup_tests(void)
+{
+#ifndef OPENSSL_NO_EC
     OPENSSL_free(curves);
 #endif
-    return result;
 }