exptest.c 9.0 KB

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