ecstresstest.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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.
  9. */
  10. #include "internal/nelem.h"
  11. #include "testutil.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #define NUM_REPEATS "1000000"
  17. static ossl_intmax_t num_repeats;
  18. static int print_mode = 0;
  19. #ifndef OPENSSL_NO_EC
  20. # include <openssl/ec.h>
  21. # include <openssl/err.h>
  22. # include <openssl/obj_mac.h>
  23. # include <openssl/objects.h>
  24. # include <openssl/rand.h>
  25. # include <openssl/bn.h>
  26. # include <openssl/opensslconf.h>
  27. static const char *kP256DefaultResult =
  28. "A1E24B223B8E81BC1FFF99BAFB909EDB895FACDE7D6DA5EF5E7B3255FB378E0F";
  29. /*
  30. * Perform a deterministic walk on the curve, by starting from |point| and
  31. * using the X-coordinate of the previous point as the next scalar for
  32. * point multiplication.
  33. * Returns the X-coordinate of the end result or NULL on error.
  34. */
  35. static BIGNUM *walk_curve(const EC_GROUP *group, EC_POINT *point,
  36. ossl_intmax_t num)
  37. {
  38. BIGNUM *scalar = NULL;
  39. ossl_intmax_t i;
  40. if (!TEST_ptr(scalar = BN_new())
  41. || !TEST_true(EC_POINT_get_affine_coordinates(group, point, scalar,
  42. NULL, NULL)))
  43. goto err;
  44. for (i = 0; i < num; i++) {
  45. if (!TEST_true(EC_POINT_mul(group, point, NULL, point, scalar, NULL))
  46. || !TEST_true(EC_POINT_get_affine_coordinates(group, point,
  47. scalar,
  48. NULL, NULL)))
  49. goto err;
  50. }
  51. return scalar;
  52. err:
  53. BN_free(scalar);
  54. return NULL;
  55. }
  56. static int test_curve(void)
  57. {
  58. EC_GROUP *group = NULL;
  59. EC_POINT *point = NULL;
  60. BIGNUM *result = NULL, *expected_result = NULL;
  61. int ret = 0;
  62. /*
  63. * We currently hard-code P-256, though adaptation to other curves.
  64. * would be straightforward.
  65. */
  66. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1))
  67. || !TEST_ptr(point = EC_POINT_dup(EC_GROUP_get0_generator(group),
  68. group))
  69. || !TEST_ptr(result = walk_curve(group, point, num_repeats)))
  70. return 0;
  71. if (print_mode) {
  72. BN_print(bio_out, result);
  73. BIO_printf(bio_out, "\n");
  74. ret = 1;
  75. } else {
  76. if (!TEST_true(BN_hex2bn(&expected_result, kP256DefaultResult))
  77. || !TEST_ptr(expected_result)
  78. || !TEST_BN_eq(result, expected_result))
  79. goto err;
  80. ret = 1;
  81. }
  82. err:
  83. EC_GROUP_free(group);
  84. EC_POINT_free(point);
  85. BN_free(result);
  86. BN_free(expected_result);
  87. return ret;
  88. }
  89. #endif
  90. typedef enum OPTION_choice {
  91. OPT_ERR = -1,
  92. OPT_EOF = 0,
  93. OPT_NUM_REPEATS,
  94. OPT_TEST_ENUM
  95. } OPTION_CHOICE;
  96. const OPTIONS *test_get_options(void)
  97. {
  98. static const OPTIONS test_options[] = {
  99. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  100. { "num", OPT_NUM_REPEATS, 'M', "Number of repeats" },
  101. { NULL }
  102. };
  103. return test_options;
  104. }
  105. /*
  106. * Stress test the curve. If the '-num' argument is given, runs the loop
  107. * |num| times and prints the resulting X-coordinate. Otherwise runs the test
  108. * the default number of times and compares against the expected result.
  109. */
  110. int setup_tests(void)
  111. {
  112. OPTION_CHOICE o;
  113. if (!opt_intmax(NUM_REPEATS, &num_repeats)) {
  114. TEST_error("Cannot parse " NUM_REPEATS);
  115. return 0;
  116. }
  117. while ((o = opt_next()) != OPT_EOF) {
  118. switch (o) {
  119. case OPT_NUM_REPEATS:
  120. if (!opt_intmax(opt_arg(), &num_repeats)
  121. || num_repeats < 0)
  122. return 0;
  123. print_mode = 1;
  124. break;
  125. case OPT_TEST_CASES:
  126. break;
  127. default:
  128. case OPT_ERR:
  129. return 0;
  130. }
  131. }
  132. #ifndef OPENSSL_NO_EC
  133. ADD_TEST(test_curve);
  134. #endif
  135. return 1;
  136. }