ecstresstest.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright 2017-2018 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 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, intmax_t num)
  36. {
  37. BIGNUM *scalar = NULL;
  38. intmax_t i;
  39. if (!TEST_ptr(scalar = BN_new())
  40. || !TEST_true(EC_POINT_get_affine_coordinates(group, point, scalar,
  41. NULL, NULL)))
  42. goto err;
  43. for (i = 0; i < num; i++) {
  44. if (!TEST_true(EC_POINT_mul(group, point, NULL, point, scalar, NULL))
  45. || !TEST_true(EC_POINT_get_affine_coordinates(group, point,
  46. scalar,
  47. NULL, NULL)))
  48. goto err;
  49. }
  50. return scalar;
  51. err:
  52. BN_free(scalar);
  53. return NULL;
  54. }
  55. static int test_curve(void)
  56. {
  57. EC_GROUP *group = NULL;
  58. EC_POINT *point = NULL;
  59. BIGNUM *result = NULL, *expected_result = NULL;
  60. int ret = 0;
  61. /*
  62. * We currently hard-code P-256, though adaptation to other curves.
  63. * would be straightforward.
  64. */
  65. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1))
  66. || !TEST_ptr(point = EC_POINT_dup(EC_GROUP_get0_generator(group),
  67. group))
  68. || !TEST_ptr(result = walk_curve(group, point, num_repeats)))
  69. return 0;
  70. if (print_mode) {
  71. BN_print(bio_out, result);
  72. BIO_printf(bio_out, "\n");
  73. ret = 1;
  74. } else {
  75. if (!TEST_true(BN_hex2bn(&expected_result, kP256DefaultResult))
  76. || !TEST_ptr(expected_result)
  77. || !TEST_BN_eq(result, expected_result))
  78. goto err;
  79. ret = 1;
  80. }
  81. err:
  82. EC_GROUP_free(group);
  83. EC_POINT_free(point);
  84. BN_free(result);
  85. BN_free(expected_result);
  86. return ret;
  87. }
  88. #endif
  89. typedef enum OPTION_choice {
  90. OPT_ERR = -1,
  91. OPT_EOF = 0,
  92. OPT_NUM_REPEATS,
  93. OPT_TEST_ENUM
  94. } OPTION_CHOICE;
  95. const OPTIONS *test_get_options(void)
  96. {
  97. static const OPTIONS test_options[] = {
  98. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  99. { "num", OPT_NUM_REPEATS, 'M', "Number of repeats" },
  100. { NULL }
  101. };
  102. return test_options;
  103. }
  104. /*
  105. * Stress test the curve. If the '-num' argument is given, runs the loop
  106. * |num| times and prints the resulting X-coordinate. Otherwise runs the test
  107. * the default number of times and compares against the expected result.
  108. */
  109. int setup_tests(void)
  110. {
  111. OPTION_CHOICE o;
  112. if (!opt_imax(NUM_REPEATS, &num_repeats)) {
  113. TEST_error("Cannot parse " NUM_REPEATS);
  114. return 0;
  115. }
  116. while ((o = opt_next()) != OPT_EOF) {
  117. switch (o) {
  118. case OPT_NUM_REPEATS:
  119. if (!opt_imax(opt_arg(), &num_repeats)
  120. || num_repeats < 0)
  121. return 0;
  122. print_mode = 1;
  123. break;
  124. case OPT_TEST_CASES:
  125. break;
  126. default:
  127. case OPT_ERR:
  128. return 0;
  129. }
  130. }
  131. #ifndef OPENSSL_NO_EC
  132. ADD_TEST(test_curve);
  133. #endif
  134. return 1;
  135. }