exptest.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "internal/nelem.h"
  13. #include <openssl/bio.h>
  14. #include <openssl/bn.h>
  15. #include <openssl/rand.h>
  16. #include <openssl/err.h>
  17. #include "testutil.h"
  18. #define NUM_BITS (BN_BITS2 * 4)
  19. #define BN_print_var(v) test_output_bignum(#v, v)
  20. /*
  21. * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
  22. * returns zero and prints debug output otherwise.
  23. */
  24. static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
  25. const BIGNUM *a)
  26. {
  27. if (!BN_is_zero(r)) {
  28. TEST_error("%s failed: a ** 0 mod 1 = r (should be 0)", method);
  29. BN_print_var(a);
  30. BN_print_var(r);
  31. return 0;
  32. }
  33. return 1;
  34. }
  35. /*
  36. * test_mod_exp_zero tests that x**0 mod 1 == 0. It returns zero on success.
  37. */
  38. static int test_mod_exp_zero(void)
  39. {
  40. BIGNUM *a = NULL, *p = NULL, *m = NULL;
  41. BIGNUM *r = NULL;
  42. BN_ULONG one_word = 1;
  43. BN_CTX *ctx = BN_CTX_new();
  44. int ret = 0, failed = 0;
  45. BN_MONT_CTX *mont = NULL;
  46. if (!TEST_ptr(m = BN_new())
  47. || !TEST_ptr(a = BN_new())
  48. || !TEST_ptr(p = BN_new())
  49. || !TEST_ptr(r = BN_new()))
  50. goto err;
  51. BN_one(m);
  52. BN_one(a);
  53. BN_zero(p);
  54. if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))
  55. goto err;
  56. if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))
  57. goto err;
  58. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))
  59. failed = 1;
  60. if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))
  61. goto err;
  62. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))
  63. failed = 1;
  64. if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))
  65. goto err;
  66. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))
  67. failed = 1;
  68. if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))
  69. goto err;
  70. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))
  71. failed = 1;
  72. if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))
  73. goto err;
  74. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))
  75. failed = 1;
  76. if (!TEST_ptr(mont = BN_MONT_CTX_new()))
  77. goto err;
  78. ERR_set_mark();
  79. /* mont is not set but passed in */
  80. if (!TEST_false(BN_mod_exp_mont_consttime(r, p, a, m, ctx, mont)))
  81. goto err;
  82. if (!TEST_false(BN_mod_exp_mont(r, p, a, m, ctx, mont)))
  83. goto err;
  84. ERR_pop_to_mark();
  85. if (!TEST_true(BN_MONT_CTX_set(mont, m, ctx)))
  86. goto err;
  87. /* we compute 0 ** a mod 1 here, to execute code that uses mont */
  88. if (!TEST_true(BN_mod_exp_mont_consttime(r, p, a, m, ctx, mont)))
  89. goto err;
  90. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))
  91. failed = 1;
  92. if (!TEST_true(BN_mod_exp_mont(r, p, a, m, ctx, mont)))
  93. goto err;
  94. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))
  95. failed = 1;
  96. /*
  97. * A different codepath exists for single word multiplication
  98. * in non-constant-time only.
  99. */
  100. if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))
  101. goto err;
  102. if (!TEST_BN_eq_zero(r)) {
  103. TEST_error("BN_mod_exp_mont_word failed: "
  104. "1 ** 0 mod 1 = r (should be 0)");
  105. BN_print_var(r);
  106. goto err;
  107. }
  108. ret = !failed;
  109. err:
  110. BN_free(r);
  111. BN_free(a);
  112. BN_free(p);
  113. BN_free(m);
  114. BN_MONT_CTX_free(mont);
  115. BN_CTX_free(ctx);
  116. return ret;
  117. }
  118. static int test_mod_exp(int round)
  119. {
  120. BN_CTX *ctx;
  121. unsigned char c;
  122. int ret = 0;
  123. BIGNUM *r_mont = NULL;
  124. BIGNUM *r_mont_const = NULL;
  125. BIGNUM *r_recp = NULL;
  126. BIGNUM *r_simple = NULL;
  127. BIGNUM *a = NULL;
  128. BIGNUM *b = NULL;
  129. BIGNUM *m = NULL;
  130. if (!TEST_ptr(ctx = BN_CTX_new()))
  131. goto err;
  132. if (!TEST_ptr(r_mont = BN_new())
  133. || !TEST_ptr(r_mont_const = BN_new())
  134. || !TEST_ptr(r_recp = BN_new())
  135. || !TEST_ptr(r_simple = BN_new())
  136. || !TEST_ptr(a = BN_new())
  137. || !TEST_ptr(b = BN_new())
  138. || !TEST_ptr(m = BN_new()))
  139. goto err;
  140. RAND_bytes(&c, 1);
  141. c = (c % BN_BITS) - BN_BITS2;
  142. BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  143. RAND_bytes(&c, 1);
  144. c = (c % BN_BITS) - BN_BITS2;
  145. BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  146. RAND_bytes(&c, 1);
  147. c = (c % BN_BITS) - BN_BITS2;
  148. BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
  149. if (!TEST_true(BN_mod(a, a, m, ctx))
  150. || !TEST_true(BN_mod(b, b, m, ctx))
  151. || !TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL))
  152. || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx))
  153. || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx))
  154. || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL)))
  155. goto err;
  156. if (!TEST_BN_eq(r_simple, r_mont)
  157. || !TEST_BN_eq(r_simple, r_recp)
  158. || !TEST_BN_eq(r_simple, r_mont_const)) {
  159. if (BN_cmp(r_simple, r_mont) != 0)
  160. TEST_info("simple and mont results differ");
  161. if (BN_cmp(r_simple, r_mont_const) != 0)
  162. TEST_info("simple and mont const time results differ");
  163. if (BN_cmp(r_simple, r_recp) != 0)
  164. TEST_info("simple and recp results differ");
  165. BN_print_var(a);
  166. BN_print_var(b);
  167. BN_print_var(m);
  168. BN_print_var(r_simple);
  169. BN_print_var(r_recp);
  170. BN_print_var(r_mont);
  171. BN_print_var(r_mont_const);
  172. goto err;
  173. }
  174. ret = 1;
  175. err:
  176. BN_free(r_mont);
  177. BN_free(r_mont_const);
  178. BN_free(r_recp);
  179. BN_free(r_simple);
  180. BN_free(a);
  181. BN_free(b);
  182. BN_free(m);
  183. BN_CTX_free(ctx);
  184. return ret;
  185. }
  186. int setup_tests(void)
  187. {
  188. ADD_TEST(test_mod_exp_zero);
  189. ADD_ALL_TESTS(test_mod_exp, 200);
  190. return 1;
  191. }