2 * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
11 #include "internal/nelem.h"
19 #define NUM_REPEATS "1000000"
21 static int64_t num_repeats;
22 static int print_mode = 0;
25 # include <openssl/ec.h>
26 # include <openssl/err.h>
27 # include <openssl/obj_mac.h>
28 # include <openssl/objects.h>
29 # include <openssl/rand.h>
30 # include <openssl/bn.h>
31 # include <openssl/opensslconf.h>
33 static const char *kP256DefaultResult =
34 "A1E24B223B8E81BC1FFF99BAFB909EDB895FACDE7D6DA5EF5E7B3255FB378E0F";
37 * Perform a deterministic walk on the curve, by starting from |point| and
38 * using the X-coordinate of the previous point as the next scalar for
39 * point multiplication.
40 * Returns the X-coordinate of the end result or NULL on error.
42 static BIGNUM *walk_curve(const EC_GROUP *group, EC_POINT *point, int64_t num)
44 BIGNUM *scalar = NULL;
47 if (!TEST_ptr(scalar = BN_new())
48 || !TEST_true(EC_POINT_get_affine_coordinates(group, point, scalar,
52 for (i = 0; i < num; i++) {
53 if (!TEST_true(EC_POINT_mul(group, point, NULL, point, scalar, NULL))
54 || !TEST_true(EC_POINT_get_affine_coordinates(group, point,
66 static int test_curve(void)
68 EC_GROUP *group = NULL;
69 EC_POINT *point = NULL;
70 BIGNUM *result = NULL, *expected_result = NULL;
74 * We currently hard-code P-256, though adaptation to other curves.
75 * would be straightforward.
77 if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1))
78 || !TEST_ptr(point = EC_POINT_dup(EC_GROUP_get0_generator(group),
80 || !TEST_ptr(result = walk_curve(group, point, num_repeats)))
84 BN_print(bio_out, result);
85 BIO_printf(bio_out, "\n");
88 if (!TEST_true(BN_hex2bn(&expected_result, kP256DefaultResult))
89 || !TEST_ptr(expected_result)
90 || !TEST_BN_eq(result, expected_result))
99 BN_free(expected_result);
104 static int atoi64(const char *in, int64_t *result)
108 for ( ; *in != '\0'; in++) {
111 if (!isdigit((unsigned char)c))
121 * Stress test the curve. If the '-num' argument is given, runs the loop
122 * |num| times and prints the resulting X-coordinate. Otherwise runs the test
123 * the default number of times and compares against the expected result.
125 int setup_tests(void)
129 if (!atoi64(NUM_REPEATS, &num_repeats)) {
130 TEST_error("Cannot parse " NUM_REPEATS);
134 * TODO(openssl-team): code under test/ should be able to reuse the option
135 * parsing framework currently in apps/.
137 p = test_get_option_argument("-num");
139 if (!atoi64(p, &num_repeats)
145 #ifndef OPENSSL_NO_EC
146 ADD_TEST(test_curve);