ecstresstest.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL licenses, (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 int64_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, int64_t num)
  36. {
  37. BIGNUM *scalar = NULL;
  38. int64_t i;
  39. if (!TEST_ptr(scalar = BN_new())
  40. || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, point,
  41. 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_GFp(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. static int atoi64(const char *in, int64_t *result)
  91. {
  92. int64_t ret = 0;
  93. for ( ; *in != '\0'; in++) {
  94. char c = *in;
  95. if (!isdigit((unsigned char)c))
  96. return 0;
  97. ret *= 10;
  98. ret += (c - '0');
  99. }
  100. *result = ret;
  101. return 1;
  102. }
  103. /*
  104. * Stress test the curve. If the '-num' argument is given, runs the loop
  105. * |num| times and prints the resulting X-coordinate. Otherwise runs the test
  106. * the default number of times and compares against the expected result.
  107. */
  108. int setup_tests(void)
  109. {
  110. const char *p;
  111. if (!atoi64(NUM_REPEATS, &num_repeats)) {
  112. TEST_error("Cannot parse " NUM_REPEATS);
  113. return 0;
  114. }
  115. /*
  116. * TODO(openssl-team): code under test/ should be able to reuse the option
  117. * parsing framework currently in apps/.
  118. */
  119. p = test_get_option_argument("-num");
  120. if (p != NULL) {
  121. if (!atoi64(p, &num_repeats)
  122. || num_repeats < 0)
  123. return 0;
  124. print_mode = 1;
  125. }
  126. #ifndef OPENSSL_NO_EC
  127. ADD_TEST(test_curve);
  128. #endif
  129. return 1;
  130. }