exptest.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_true(RAND_bytes(&c, 1)))
  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_true(RAND_bytes(&c, 1)))
  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_true(RAND_bytes(&c, 1)))
  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. /*
  189. * Currently only 1024-bit factor size is supported.
  190. */
  191. if (idx <= 100)
  192. factor_size = 1024;
  193. if (!TEST_ptr(ctx = BN_CTX_new()))
  194. goto err;
  195. if (!TEST_ptr(r_mont_const_x2_1 = BN_new())
  196. || !TEST_ptr(r_mont_const_x2_2 = BN_new())
  197. || !TEST_ptr(r_simple1 = BN_new())
  198. || !TEST_ptr(r_simple2 = BN_new())
  199. || !TEST_ptr(a1 = BN_new())
  200. || !TEST_ptr(b1 = BN_new())
  201. || !TEST_ptr(m1 = BN_new())
  202. || !TEST_ptr(a2 = BN_new())
  203. || !TEST_ptr(b2 = BN_new())
  204. || !TEST_ptr(m2 = BN_new()))
  205. goto err;
  206. BN_rand(a1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  207. BN_rand(b1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  208. BN_rand(m1, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
  209. BN_rand(a2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  210. BN_rand(b2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  211. BN_rand(m2, factor_size, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
  212. if (!TEST_true(BN_mod(a1, a1, m1, ctx))
  213. || !TEST_true(BN_mod(b1, b1, m1, ctx))
  214. || !TEST_true(BN_mod(a2, a2, m2, ctx))
  215. || !TEST_true(BN_mod(b2, b2, m2, ctx))
  216. || !TEST_true(BN_mod_exp_simple(r_simple1, a1, b1, m1, ctx))
  217. || !TEST_true(BN_mod_exp_simple(r_simple2, a2, b2, m2, ctx))
  218. || !TEST_true(BN_mod_exp_mont_consttime_x2(r_mont_const_x2_1, a1, b1, m1, NULL,
  219. r_mont_const_x2_2, a2, b2, m2, NULL,
  220. ctx)))
  221. goto err;
  222. if (!TEST_BN_eq(r_simple1, r_mont_const_x2_1)
  223. || !TEST_BN_eq(r_simple2, r_mont_const_x2_2)) {
  224. if (BN_cmp(r_simple1, r_mont_const_x2_1) != 0)
  225. TEST_info("simple and mont const time x2 (#1) results differ");
  226. if (BN_cmp(r_simple2, r_mont_const_x2_2) != 0)
  227. TEST_info("simple and mont const time x2 (#2) results differ");
  228. BN_print_var(a1);
  229. BN_print_var(b1);
  230. BN_print_var(m1);
  231. BN_print_var(a2);
  232. BN_print_var(b2);
  233. BN_print_var(m2);
  234. BN_print_var(r_simple1);
  235. BN_print_var(r_simple2);
  236. BN_print_var(r_mont_const_x2_1);
  237. BN_print_var(r_mont_const_x2_2);
  238. goto err;
  239. }
  240. ret = 1;
  241. err:
  242. BN_free(r_mont_const_x2_1);
  243. BN_free(r_mont_const_x2_2);
  244. BN_free(r_simple1);
  245. BN_free(r_simple2);
  246. BN_free(a1);
  247. BN_free(b1);
  248. BN_free(m1);
  249. BN_free(a2);
  250. BN_free(b2);
  251. BN_free(m2);
  252. BN_CTX_free(ctx);
  253. return ret;
  254. }
  255. int setup_tests(void)
  256. {
  257. ADD_TEST(test_mod_exp_zero);
  258. ADD_ALL_TESTS(test_mod_exp, 200);
  259. ADD_ALL_TESTS(test_mod_exp_x2, 100);
  260. return 1;
  261. }