1178476Sjb/*
2178476Sjb * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
3178476Sjb *
4178476Sjb * Licensed under the OpenSSL licenses, (the "License");
5178476Sjb * you may not use this file except in compliance with the License.
6178476Sjb * You may obtain a copy of the License at
7178476Sjb * https://www.openssl.org/source/license.html
8178476Sjb * or in the file LICENSE in the source distribution.
9178476Sjb */
10178476Sjb
11178476Sjb#include "internal/nelem.h"
12178476Sjb#include "testutil.h"
13178476Sjb
14178476Sjb#include <stdio.h>
15178476Sjb#include <stdlib.h>
16178476Sjb#include <string.h>
17178476Sjb#include <ctype.h>
18178476Sjb
19178476Sjb#define NUM_REPEATS "1000000"
20178476Sjb
21178476Sjbstatic int64_t num_repeats;
22178476Sjbstatic int print_mode = 0;
23178476Sjb
24178476Sjb#ifndef OPENSSL_NO_EC
25178476Sjb# include <openssl/ec.h>
26178476Sjb# include <openssl/err.h>
27178476Sjb# include <openssl/obj_mac.h>
28178476Sjb# include <openssl/objects.h>
29178476Sjb# include <openssl/rand.h>
30178476Sjb# include <openssl/bn.h>
31178476Sjb# include <openssl/opensslconf.h>
32178476Sjb
33178476Sjbstatic const char *kP256DefaultResult =
34178476Sjb    "A1E24B223B8E81BC1FFF99BAFB909EDB895FACDE7D6DA5EF5E7B3255FB378E0F";
35178476Sjb
36178476Sjb/*
37178476Sjb * Perform a deterministic walk on the curve, by starting from |point| and
38178476Sjb * using the X-coordinate of the previous point as the next scalar for
39178476Sjb * point multiplication.
40 * Returns the X-coordinate of the end result or NULL on error.
41 */
42static BIGNUM *walk_curve(const EC_GROUP *group, EC_POINT *point, int64_t num)
43{
44    BIGNUM *scalar = NULL;
45    int64_t i;
46
47    if (!TEST_ptr(scalar = BN_new())
48            || !TEST_true(EC_POINT_get_affine_coordinates(group, point, scalar,
49                                                          NULL, NULL)))
50        goto err;
51
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,
55                                                              scalar,
56                                                              NULL, NULL)))
57            goto err;
58    }
59    return scalar;
60
61err:
62    BN_free(scalar);
63    return NULL;
64}
65
66static int test_curve(void)
67{
68    EC_GROUP *group = NULL;
69    EC_POINT *point = NULL;
70    BIGNUM *result = NULL, *expected_result = NULL;
71    int ret = 0;
72
73    /*
74     * We currently hard-code P-256, though adaptation to other curves.
75     * would be straightforward.
76     */
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),
79                                              group))
80            || !TEST_ptr(result = walk_curve(group, point, num_repeats)))
81        return 0;
82
83    if (print_mode) {
84        BN_print(bio_out, result);
85        BIO_printf(bio_out, "\n");
86        ret = 1;
87    } else {
88        if (!TEST_true(BN_hex2bn(&expected_result, kP256DefaultResult))
89                || !TEST_ptr(expected_result)
90                || !TEST_BN_eq(result, expected_result))
91            goto err;
92        ret = 1;
93    }
94
95err:
96    EC_GROUP_free(group);
97    EC_POINT_free(point);
98    BN_free(result);
99    BN_free(expected_result);
100    return ret;
101}
102#endif
103
104static int atoi64(const char *in, int64_t *result)
105{
106    int64_t ret = 0;
107
108    for ( ; *in != '\0'; in++) {
109        char c = *in;
110
111        if (!isdigit((unsigned char)c))
112            return 0;
113        ret *= 10;
114        ret += (c - '0');
115    }
116    *result = ret;
117    return 1;
118}
119
120/*
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.
124 */
125int setup_tests(void)
126{
127    const char *p;
128
129    if (!atoi64(NUM_REPEATS, &num_repeats)) {
130        TEST_error("Cannot parse " NUM_REPEATS);
131        return 0;
132    }
133    /*
134     * TODO(openssl-team): code under test/ should be able to reuse the option
135     * parsing framework currently in apps/.
136     */
137    p = test_get_option_argument("-num");
138    if (p != NULL) {
139        if (!atoi64(p, &num_repeats)
140                || num_repeats < 0)
141            return 0;
142        print_mode = 1;
143    }
144
145#ifndef OPENSSL_NO_EC
146    ADD_TEST(test_curve);
147#endif
148    return 1;
149}
150