/*
* Copyright 2001-2017 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
* 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;
const EC_POINT *points[4];
const BIGNUM *scalars[4];
unsigned char buf[100];
- size_t i, len, r = 0;
+ size_t len, r = 0;
int k;
if (!TEST_ptr(ctx = BN_CTX_new())
if (!TEST_true(EC_GROUP_get_curve_GFp(group, p, a, b, ctx)))
goto err;
- BIO_printf(bio_out,
- "Curve defined by Weierstrass equation\n"
- " y^2 = x^3 + a*x + b (mod 0x");
- BN_print(bio_out, p);
- BIO_printf(bio_out, ")\n a = 0x");
- BN_print(bio_out, a);
- BIO_printf(bio_out, "\n b = 0x");
- BN_print(bio_out, b);
- BIO_printf(bio_out, "\n");
+ TEST_info("Curve defined by Weierstrass equation");
+ TEST_note(" y^2 = x^3 + a*x + b (mod p)");
+ test_output_bignum("a", a);
+ test_output_bignum("b", b);
+ test_output_bignum("p", p);
buf[0] = 0;
if (!TEST_ptr(P = EC_POINT_new(group))
if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, Q, x, y,
ctx)))
goto err;
- BIO_printf(bio_err, "Point is not on curve: x = 0x");
- BN_print_fp(stderr, x);
- BIO_printf(bio_err, ", y = 0x");
- BN_print_fp(stderr, y);
- BIO_printf(bio_err, "\n");
+ TEST_info("Point is not on curve");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
goto err;
}
- BIO_printf(bio_out, "A cyclic subgroup:\n");
+ TEST_note("A cyclic subgroup:");
k = 100;
do {
if (!TEST_int_ne(k--, 0))
goto err;
if (EC_POINT_is_at_infinity(group, P)) {
- BIO_printf(bio_out, " point at infinity\n");
+ TEST_note(" point at infinity");
} else {
if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y,
ctx)))
goto err;
- BIO_printf(bio_out, " x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, ", y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
}
if (!TEST_true(EC_POINT_copy(R, P))
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)))
goto err;
- BIO_printf(bio_out, "Generator as octet string, compressed form:\n ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, compressed form:",
+ 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)))
goto err;
- BIO_printf(bio_out, "\nGenerator as octet string, uncompressed form:\n"
- " ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, uncompressed form:",
+ 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)))
goto err;
- BIO_printf(bio_out, "\nGenerator as octet string, hybrid form:\n ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, hybrid form:",
+ buf, len);
if (!TEST_true(EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z,
ctx)))
goto err;
- BIO_printf(bio_out,
- "\nA representation of the inverse of that generator in\n"
- "Jacobian projective coordinates:\n"
- " X = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, ", Y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, ", Z = 0x");
- BN_print(bio_out, z);
- BIO_printf(bio_out, "\n");
+ TEST_info("A representation of the inverse of that generator in");
+ TEST_note("Jacobian projective coordinates");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
+ test_output_bignum("z", z);
if (!TEST_true(EC_POINT_invert(group, P, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
|| !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("SEC2 curve secp160r1 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "23a62855"
"3168947d59dcc912042351377ac5fb32"))
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-192 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-192 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78"
"631011ED6B24CDD573F977A11E794811"))
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-224 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-224 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6"
"CD4375A05A07476444D5819985007E34"))
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-256 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-256 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
"2BCE33576B315ECECBB6406837BF51F5"))
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-384 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-384 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"
"F8F41DBD289A147CE9DA3113B5F0B8C0"
|| !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
goto err;
- BIO_printf(bio_out, "\nNIST curve P-521 -- Generator:\n x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("NIST curve P-521 -- Generator");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, "118"
"39296A789A3BC0045C8A5FB42C7D1BD9"
scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
scalars[1] = y;
- BIO_printf(bio_out, "combined multiplication ...");
+ TEST_note("combined multiplication ...");
/* z is still the group order */
if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
|| !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);
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);
|| !TEST_true(EC_POINT_is_at_infinity(group, P)))
goto err;
- BIO_printf(bio_out, " ok\n\n");
+ TEST_note(" ok\n");
r = 1;
|| !TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,
ctx)))
goto err;
- BIO_printf(bio_out, "\n%s -- Generator:\n x = 0x", test->name);
- BN_print(bio_out, x);
- BIO_printf(bio_out, "\n y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ TEST_info("%s -- Generator", test->name);
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
/* G_y value taken from the standard: */
if (!TEST_true(BN_hex2bn(&z, test->y))
|| !TEST_BN_eq(y, z))
|| !TEST_true(BN_hex2bn(&cof, test->cof))
|| !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))
goto err;
- BIO_printf(bio_out, "\n%s -- Generator:\n x = 0x", test->name); \
- BN_print(bio_out, x); \
- BIO_printf(bio_out, "\n y = 0x"); \
- BN_print(bio_out, y); \
- BIO_printf(bio_out, "\n");
+ TEST_info("%s -- Generator:", test->name);
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
# endif
if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)
scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
scalars[1] = y;
- BIO_printf(bio_out, "combined multiplication ...");
+ TEST_note("combined multiplication ...");
/* z is still the group order */
if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
|| !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);
|| !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);
EC_POINT *P = NULL, *Q = NULL, *R = NULL;
BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
unsigned char buf[100];
- size_t i, len;
+ size_t len;
int k, r = 0;
if (!TEST_ptr(ctx = BN_CTX_new())
if (!TEST_true(EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)))
goto err;
- BIO_printf(bio_out,
- "Curve defined by Weierstrass equation\n"
- " y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
- BN_print(bio_out, p);
- BIO_printf(bio_out, ")\n a = 0x");
- BN_print(bio_out, a);
- BIO_printf(bio_out, "\n b = 0x");
- BN_print(bio_out, b);
- BIO_printf(bio_out, "\n(0x... means binary polynomial)\n");
+ TEST_info("Curve defined by Weierstrass equation");
+ TEST_note(" y^2 + x*y = x^3 + a*x^2 + b (mod p)");
+ test_output_bignum("a", a);
+ test_output_bignum("b", b);
+ test_output_bignum("p", p);
if (!TEST_ptr(P = EC_POINT_new(group))
|| !TEST_ptr(Q = EC_POINT_new(group))
ctx)))
goto err;
# endif
- BIO_printf(bio_err, "Point is not on curve: x = 0x");
- BN_print_fp(stderr, x);
- BIO_printf(bio_err, ", y = 0x");
- BN_print_fp(stderr, y);
- BIO_printf(bio_err, "\n");
+ TEST_info("Point is not on curve");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
goto err;
}
- BIO_printf(bio_out, "A cyclic subgroup:\n");
+ TEST_note("A cyclic subgroup:");
k = 100;
do {
if (!TEST_int_ne(k--, 0))
goto err;
if (EC_POINT_is_at_infinity(group, P))
- BIO_printf(bio_out, " point at infinity\n");
+ TEST_note(" point at infinity");
else {
if (!TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,
ctx)))
goto err;
- BIO_printf(bio_out, " x = 0x");
- BN_print(bio_out, x);
- BIO_printf(bio_out, ", y = 0x");
- BN_print(bio_out, y);
- BIO_printf(bio_out, "\n");
+ test_output_bignum("x", x);
+ test_output_bignum("y", y);
}
if (!TEST_true(EC_POINT_copy(R, P))
/* 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)))
goto err;
- BIO_printf(bio_out, "Generator as octet string, compressed form:\n ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, compressed form:",
+ buf, len);
# 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)))
goto err;
- BIO_printf(bio_out, "\nGenerator as octet string, uncompressed form:\n"
- " ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, uncompressed form:",
+ buf, len);
/* 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))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
goto err;
- BIO_printf(bio_out, "\nGenerator as octet string, hybrid form:\n ");
- for (i = 0; i < len; i++)
- BIO_printf(bio_out, "%02X", buf[i]);
+ test_output_memory("Generator as octet string, hybrid form:",
+ buf, len);
# endif
- BIO_printf(bio_out, "\n");
if (!TEST_true(EC_POINT_invert(group, P, ctx))
|| !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)))
goto err;
-
-#if 0
-#endif
- BIO_printf(bio_out, "\n\n");
+ TEST_note("\n");
r = 1;
err:
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;
EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL;
int r = 0;
- BIO_printf(bio_out, "\nNIST curve P-%d (optimised implementation):\n",
- test->degree);
+ TEST_note("NIST curve P-%d (optimised implementation):",
+ test->degree);
if (!TEST_ptr(ctx = BN_CTX_new())
|| !TEST_ptr(p = BN_new())
|| !TEST_ptr(a = BN_new())
|| !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree))
goto err;
- BIO_printf(bio_out, "NIST test vectors ... ");
+ TEST_note("NIST test vectors ... ");
if (!TEST_true(BN_hex2bn(&n, test->d)))
goto err;
/* fixed point multiplication */
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);
# 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;
}