exptest.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 1995-2017 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. RAND_bytes(&c, 1);
  119. c = (c % BN_BITS) - BN_BITS2;
  120. BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  121. RAND_bytes(&c, 1);
  122. c = (c % BN_BITS) - BN_BITS2;
  123. BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  124. RAND_bytes(&c, 1);
  125. c = (c % BN_BITS) - BN_BITS2;
  126. BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
  127. if (!TEST_true(BN_mod(a, a, m, ctx))
  128. || !TEST_true(BN_mod(b, b, m, ctx))
  129. || !TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL))
  130. || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx))
  131. || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx))
  132. || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL)))
  133. goto err;
  134. if (!TEST_BN_eq(r_simple, r_mont)
  135. || !TEST_BN_eq(r_simple, r_recp)
  136. || !TEST_BN_eq(r_simple, r_mont_const)) {
  137. if (BN_cmp(r_simple, r_mont) != 0)
  138. TEST_info("simple and mont results differ");
  139. if (BN_cmp(r_simple, r_mont_const) != 0)
  140. TEST_info("simple and mont const time results differ");
  141. if (BN_cmp(r_simple, r_recp) != 0)
  142. TEST_info("simple and recp results differ");
  143. BN_print_var(a);
  144. BN_print_var(b);
  145. BN_print_var(m);
  146. BN_print_var(r_simple);
  147. BN_print_var(r_recp);
  148. BN_print_var(r_mont);
  149. BN_print_var(r_mont_const);
  150. goto err;
  151. }
  152. ret = 1;
  153. err:
  154. BN_free(r_mont);
  155. BN_free(r_mont_const);
  156. BN_free(r_recp);
  157. BN_free(r_simple);
  158. BN_free(a);
  159. BN_free(b);
  160. BN_free(m);
  161. BN_CTX_free(ctx);
  162. return ret;
  163. }
  164. int setup_tests(void)
  165. {
  166. ADD_TEST(test_mod_exp_zero);
  167. ADD_ALL_TESTS(test_mod_exp, 200);
  168. return 1;
  169. }