exptest.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 = 1, failed = 0;
  45. if (!TEST_ptr(m = BN_new())
  46. || !TEST_ptr(a = BN_new())
  47. || !TEST_ptr(p = BN_new())
  48. || !TEST_ptr(r = BN_new()))
  49. goto err;
  50. BN_one(m);
  51. BN_one(a);
  52. BN_zero(p);
  53. if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))
  54. goto err;
  55. if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))
  56. goto err;
  57. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))
  58. failed = 1;
  59. if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))
  60. goto err;
  61. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))
  62. failed = 1;
  63. if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))
  64. goto err;
  65. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))
  66. failed = 1;
  67. if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))
  68. goto err;
  69. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))
  70. failed = 1;
  71. if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))
  72. goto err;
  73. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))
  74. failed = 1;
  75. /*
  76. * A different codepath exists for single word multiplication
  77. * in non-constant-time only.
  78. */
  79. if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))
  80. goto err;
  81. if (!TEST_BN_eq_zero(r)) {
  82. TEST_error("BN_mod_exp_mont_word failed: "
  83. "1 ** 0 mod 1 = r (should be 0)");
  84. BN_print_var(r);
  85. goto err;
  86. }
  87. ret = !failed;
  88. err:
  89. BN_free(r);
  90. BN_free(a);
  91. BN_free(p);
  92. BN_free(m);
  93. BN_CTX_free(ctx);
  94. return ret;
  95. }
  96. static int test_mod_exp(int round)
  97. {
  98. BN_CTX *ctx;
  99. unsigned char c;
  100. int ret = 0;
  101. BIGNUM *r_mont = NULL;
  102. BIGNUM *r_mont_const = NULL;
  103. BIGNUM *r_recp = NULL;
  104. BIGNUM *r_simple = NULL;
  105. BIGNUM *a = NULL;
  106. BIGNUM *b = NULL;
  107. BIGNUM *m = NULL;
  108. if (!TEST_ptr(ctx = BN_CTX_new()))
  109. goto err;
  110. if (!TEST_ptr(r_mont = BN_new())
  111. || !TEST_ptr(r_mont_const = BN_new())
  112. || !TEST_ptr(r_recp = BN_new())
  113. || !TEST_ptr(r_simple = BN_new())
  114. || !TEST_ptr(a = BN_new())
  115. || !TEST_ptr(b = BN_new())
  116. || !TEST_ptr(m = BN_new()))
  117. goto err;
  118. if (!TEST_int_gt(RAND_bytes(&c, 1), 0))
  119. goto err;
  120. c = (c % BN_BITS) - BN_BITS2;
  121. if (!TEST_true(BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE,
  122. BN_RAND_BOTTOM_ANY)))
  123. goto err;
  124. if (!TEST_int_gt(RAND_bytes(&c, 1), 0))
  125. goto err;
  126. c = (c % BN_BITS) - BN_BITS2;
  127. if (!TEST_true(BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE,
  128. BN_RAND_BOTTOM_ANY)))
  129. goto err;
  130. if (!TEST_int_gt(RAND_bytes(&c, 1), 0))
  131. goto err;
  132. c = (c % BN_BITS) - BN_BITS2;
  133. if (!TEST_true(BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE,
  134. BN_RAND_BOTTOM_ODD)))
  135. goto err;
  136. if (!TEST_true(BN_mod(a, a, m, ctx))
  137. || !TEST_true(BN_mod(b, b, m, ctx))
  138. || !TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL))
  139. || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx))
  140. || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx))
  141. || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL)))
  142. goto err;
  143. if (!TEST_BN_eq(r_simple, r_mont)
  144. || !TEST_BN_eq(r_simple, r_recp)
  145. || !TEST_BN_eq(r_simple, r_mont_const)) {
  146. if (BN_cmp(r_simple, r_mont) != 0)
  147. TEST_info("simple and mont results differ");
  148. if (BN_cmp(r_simple, r_mont_const) != 0)
  149. TEST_info("simple and mont const time results differ");
  150. if (BN_cmp(r_simple, r_recp) != 0)
  151. TEST_info("simple and recp results differ");
  152. BN_print_var(a);
  153. BN_print_var(b);
  154. BN_print_var(m);
  155. BN_print_var(r_simple);
  156. BN_print_var(r_recp);
  157. BN_print_var(r_mont);
  158. BN_print_var(r_mont_const);
  159. goto err;
  160. }
  161. ret = 1;
  162. err:
  163. BN_free(r_mont);
  164. BN_free(r_mont_const);
  165. BN_free(r_recp);
  166. BN_free(r_simple);
  167. BN_free(a);
  168. BN_free(b);
  169. BN_free(m);
  170. BN_CTX_free(ctx);
  171. return ret;
  172. }
  173. static int test_mod_exp_x2(int idx)
  174. {
  175. BN_CTX *ctx;
  176. int ret = 0;
  177. BIGNUM *r_mont_const_x2_1 = NULL;
  178. BIGNUM *r_mont_const_x2_2 = NULL;
  179. BIGNUM *r_simple1 = NULL;
  180. BIGNUM *r_simple2 = NULL;
  181. BIGNUM *a1 = NULL;
  182. BIGNUM *b1 = NULL;
  183. BIGNUM *m1 = NULL;
  184. BIGNUM *a2 = NULL;
  185. BIGNUM *b2 = NULL;
  186. BIGNUM *m2 = NULL;
  187. int factor_size = 0;
  188. if (idx <= 100)
  189. factor_size = 1024;
  190. else if (idx <= 200)
  191. factor_size = 1536;
  192. else if (idx <= 300)
  193. factor_size = 2048;
  194. if (!TEST_ptr(ctx = BN_CTX_new()))
  195. goto err;
  196. if (!TEST_ptr(r_mont_const_x2_1 = BN_new())
  197. || !TEST_ptr(r_mont_const_x2_2 = BN_new())
  198. || !TEST_ptr(r_simple1 = BN_new())
  199. || !TEST_ptr(r_simple2 = BN_new())
  200. || !TEST_ptr(a1 = BN_new())
  201. || !TEST_ptr(b1 = BN_new())
  202. || !TEST_ptr(m1 = BN_new())
  203. || !TEST_ptr(a2 = BN_new())
  204. || !TEST_ptr(b2 = BN_new())
  205. || !TEST_ptr(m2 = BN_new()))
  206. goto err;
  207. BN_rand(a1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  208. BN_rand(b1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  209. BN_rand(m1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
  210. BN_rand(a2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  211. BN_rand(b2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  212. BN_rand(m2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
  213. if (!TEST_true(BN_mod(a1, a1, m1, ctx))
  214. || !TEST_true(BN_mod(b1, b1, m1, ctx))
  215. || !TEST_true(BN_mod(a2, a2, m2, ctx))
  216. || !TEST_true(BN_mod(b2, b2, m2, ctx))
  217. || !TEST_true(BN_mod_exp_simple(r_simple1, a1, b1, m1, ctx))
  218. || !TEST_true(BN_mod_exp_simple(r_simple2, a2, b2, m2, ctx))
  219. || !TEST_true(BN_mod_exp_mont_consttime_x2(r_mont_const_x2_1, a1, b1, m1, NULL,
  220. r_mont_const_x2_2, a2, b2, m2, NULL,
  221. ctx)))
  222. goto err;
  223. if (!TEST_BN_eq(r_simple1, r_mont_const_x2_1)
  224. || !TEST_BN_eq(r_simple2, r_mont_const_x2_2)) {
  225. if (BN_cmp(r_simple1, r_mont_const_x2_1) != 0)
  226. TEST_info("simple and mont const time x2 (#1) results differ");
  227. if (BN_cmp(r_simple2, r_mont_const_x2_2) != 0)
  228. TEST_info("simple and mont const time x2 (#2) results differ");
  229. BN_print_var(a1);
  230. BN_print_var(b1);
  231. BN_print_var(m1);
  232. BN_print_var(a2);
  233. BN_print_var(b2);
  234. BN_print_var(m2);
  235. BN_print_var(r_simple1);
  236. BN_print_var(r_simple2);
  237. BN_print_var(r_mont_const_x2_1);
  238. BN_print_var(r_mont_const_x2_2);
  239. goto err;
  240. }
  241. ret = 1;
  242. err:
  243. BN_free(r_mont_const_x2_1);
  244. BN_free(r_mont_const_x2_2);
  245. BN_free(r_simple1);
  246. BN_free(r_simple2);
  247. BN_free(a1);
  248. BN_free(b1);
  249. BN_free(m1);
  250. BN_free(a2);
  251. BN_free(b2);
  252. BN_free(m2);
  253. BN_CTX_free(ctx);
  254. return ret;
  255. }
  256. int setup_tests(void)
  257. {
  258. ADD_TEST(test_mod_exp_zero);
  259. ADD_ALL_TESTS(test_mod_exp, 200);
  260. ADD_ALL_TESTS(test_mod_exp_x2, 300);
  261. return 1;
  262. }