2017-06-17 04:55:19 +08:00
|
|
|
/*
|
2018-09-11 20:22:14 +08:00
|
|
|
* Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2017-06-17 04:55:19 +08:00
|
|
|
*
|
2018-12-06 20:05:25 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License");
|
2017-06-17 04:55:19 +08:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
* or in the file LICENSE in the source distribution.
|
|
|
|
*/
|
|
|
|
|
2017-08-22 20:35:43 +08:00
|
|
|
#include "internal/nelem.h"
|
2017-06-17 04:55:19 +08:00
|
|
|
#include "testutil.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#define NUM_REPEATS "1000000"
|
|
|
|
|
2018-08-16 10:36:01 +08:00
|
|
|
static intmax_t num_repeats;
|
2017-06-17 04:55:19 +08:00
|
|
|
static int print_mode = 0;
|
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
# include <openssl/ec.h>
|
|
|
|
# include <openssl/err.h>
|
|
|
|
# include <openssl/obj_mac.h>
|
|
|
|
# include <openssl/objects.h>
|
|
|
|
# include <openssl/rand.h>
|
|
|
|
# include <openssl/bn.h>
|
|
|
|
# include <openssl/opensslconf.h>
|
|
|
|
|
|
|
|
static const char *kP256DefaultResult =
|
|
|
|
"A1E24B223B8E81BC1FFF99BAFB909EDB895FACDE7D6DA5EF5E7B3255FB378E0F";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a deterministic walk on the curve, by starting from |point| and
|
|
|
|
* using the X-coordinate of the previous point as the next scalar for
|
|
|
|
* point multiplication.
|
|
|
|
* Returns the X-coordinate of the end result or NULL on error.
|
|
|
|
*/
|
2018-08-16 10:36:01 +08:00
|
|
|
static BIGNUM *walk_curve(const EC_GROUP *group, EC_POINT *point, intmax_t num)
|
2017-06-17 04:55:19 +08:00
|
|
|
{
|
|
|
|
BIGNUM *scalar = NULL;
|
2018-08-16 10:36:01 +08:00
|
|
|
intmax_t i;
|
2017-06-17 04:55:19 +08:00
|
|
|
|
|
|
|
if (!TEST_ptr(scalar = BN_new())
|
2018-07-30 23:40:18 +08:00
|
|
|
|| !TEST_true(EC_POINT_get_affine_coordinates(group, point, scalar,
|
|
|
|
NULL, NULL)))
|
2017-06-17 04:55:19 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if (!TEST_true(EC_POINT_mul(group, point, NULL, point, scalar, NULL))
|
2018-07-30 23:40:18 +08:00
|
|
|
|| !TEST_true(EC_POINT_get_affine_coordinates(group, point,
|
|
|
|
scalar,
|
|
|
|
NULL, NULL)))
|
2017-06-17 04:55:19 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
return scalar;
|
|
|
|
|
|
|
|
err:
|
|
|
|
BN_free(scalar);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-16 05:39:03 +08:00
|
|
|
static int test_curve(void)
|
2017-06-17 04:55:19 +08:00
|
|
|
{
|
|
|
|
EC_GROUP *group = NULL;
|
|
|
|
EC_POINT *point = NULL;
|
|
|
|
BIGNUM *result = NULL, *expected_result = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We currently hard-code P-256, though adaptation to other curves.
|
|
|
|
* would be straightforward.
|
|
|
|
*/
|
|
|
|
if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1))
|
|
|
|
|| !TEST_ptr(point = EC_POINT_dup(EC_GROUP_get0_generator(group),
|
|
|
|
group))
|
|
|
|
|| !TEST_ptr(result = walk_curve(group, point, num_repeats)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (print_mode) {
|
|
|
|
BN_print(bio_out, result);
|
|
|
|
BIO_printf(bio_out, "\n");
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
if (!TEST_true(BN_hex2bn(&expected_result, kP256DefaultResult))
|
|
|
|
|| !TEST_ptr(expected_result)
|
|
|
|
|| !TEST_BN_eq(result, expected_result))
|
|
|
|
goto err;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
EC_GROUP_free(group);
|
|
|
|
EC_POINT_free(point);
|
|
|
|
BN_free(result);
|
|
|
|
BN_free(expected_result);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-08-16 10:36:01 +08:00
|
|
|
typedef enum OPTION_choice {
|
|
|
|
OPT_ERR = -1,
|
|
|
|
OPT_EOF = 0,
|
|
|
|
OPT_NUM_REPEATS,
|
|
|
|
OPT_TEST_ENUM
|
|
|
|
} OPTION_CHOICE;
|
2017-06-17 04:55:19 +08:00
|
|
|
|
2018-08-16 10:36:01 +08:00
|
|
|
const OPTIONS *test_get_options(void)
|
|
|
|
{
|
|
|
|
static const OPTIONS test_options[] = {
|
|
|
|
OPT_TEST_OPTIONS_DEFAULT_USAGE,
|
|
|
|
{ "num", OPT_NUM_REPEATS, 'M', "Number of repeats" },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
return test_options;
|
2017-06-17 04:55:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stress test the curve. If the '-num' argument is given, runs the loop
|
|
|
|
* |num| times and prints the resulting X-coordinate. Otherwise runs the test
|
|
|
|
* the default number of times and compares against the expected result.
|
|
|
|
*/
|
2017-07-18 09:48:27 +08:00
|
|
|
int setup_tests(void)
|
2017-06-17 04:55:19 +08:00
|
|
|
{
|
2018-08-16 10:36:01 +08:00
|
|
|
OPTION_CHOICE o;
|
2017-06-17 04:55:19 +08:00
|
|
|
|
2018-08-16 10:36:01 +08:00
|
|
|
if (!opt_imax(NUM_REPEATS, &num_repeats)) {
|
2017-06-17 04:55:19 +08:00
|
|
|
TEST_error("Cannot parse " NUM_REPEATS);
|
2017-07-18 09:48:27 +08:00
|
|
|
return 0;
|
2017-06-17 04:55:19 +08:00
|
|
|
}
|
2018-08-16 10:36:01 +08:00
|
|
|
|
|
|
|
while ((o = opt_next()) != OPT_EOF) {
|
|
|
|
switch (o) {
|
|
|
|
case OPT_NUM_REPEATS:
|
|
|
|
if (!opt_imax(opt_arg(), &num_repeats)
|
|
|
|
|| num_repeats < 0)
|
|
|
|
return 0;
|
|
|
|
print_mode = 1;
|
|
|
|
break;
|
|
|
|
case OPT_TEST_CASES:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case OPT_ERR:
|
2019-07-16 18:24:10 +08:00
|
|
|
return 0;
|
2018-08-16 10:36:01 +08:00
|
|
|
}
|
2017-06-17 04:55:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef OPENSSL_NO_EC
|
|
|
|
ADD_TEST(test_curve);
|
|
|
|
#endif
|
2017-07-18 09:48:27 +08:00
|
|
|
return 1;
|
2017-06-17 04:55:19 +08:00
|
|
|
}
|