exptest.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright 1995-2017 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 "../e_os.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) bn_print_var(#v, v)
  20. static void bn_print_var(const char *var, const BIGNUM *bn)
  21. {
  22. fprintf(stderr, "%s (%3d) = ", var, BN_num_bits(bn));
  23. BN_print_fp(stderr, bn);
  24. fprintf(stderr, "\n");
  25. }
  26. /*
  27. * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
  28. * returns zero and prints debug output otherwise.
  29. */
  30. static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
  31. const BIGNUM *a)
  32. {
  33. if (!BN_is_zero(r)) {
  34. fprintf(stderr, "%s failed:\n", method);
  35. fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n");
  36. BN_print_var(a);
  37. BN_print_var(r);
  38. return 0;
  39. }
  40. return 1;
  41. }
  42. /*
  43. * test_mod_exp_zero tests that x**0 mod 1 == 0. It returns zero on success.
  44. */
  45. static int test_mod_exp_zero()
  46. {
  47. BIGNUM *a = NULL, *p = NULL, *m = NULL;
  48. BIGNUM *r = NULL;
  49. BN_ULONG one_word = 1;
  50. BN_CTX *ctx = BN_CTX_new();
  51. int ret = 1, failed = 0;
  52. if (!TEST_ptr(m = BN_new())
  53. || !TEST_ptr(a = BN_new())
  54. || !TEST_ptr(p = BN_new())
  55. || !TEST_ptr(r = BN_new()))
  56. goto err;
  57. BN_one(m);
  58. BN_one(a);
  59. BN_zero(p);
  60. if (!TEST_true(BN_rand(a, 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)))
  61. goto err;
  62. if (!TEST_true(BN_mod_exp(r, a, p, m, ctx)))
  63. goto err;
  64. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp", r, a)))
  65. failed = 1;
  66. if (!TEST_true(BN_mod_exp_recp(r, a, p, m, ctx)))
  67. goto err;
  68. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_recp", r, a)))
  69. failed = 1;
  70. if (!TEST_true(BN_mod_exp_simple(r, a, p, m, ctx)))
  71. goto err;
  72. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_simple", r, a)))
  73. failed = 1;
  74. if (!TEST_true(BN_mod_exp_mont(r, a, p, m, ctx, NULL)))
  75. goto err;
  76. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont", r, a)))
  77. failed = 1;
  78. if (!TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)))
  79. goto err;
  80. if (!TEST_true(a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a)))
  81. failed = 1;
  82. /*
  83. * A different codepath exists for single word multiplication
  84. * in non-constant-time only.
  85. */
  86. if (!TEST_true(BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL)))
  87. goto err;
  88. if (!TEST_true(BN_is_zero(r))) {
  89. fprintf(stderr, "BN_mod_exp_mont_word failed:\n");
  90. fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n");
  91. BN_print_var(r);
  92. goto err;
  93. }
  94. ret = !failed;
  95. err:
  96. BN_free(r);
  97. BN_free(a);
  98. BN_free(p);
  99. BN_free(m);
  100. BN_CTX_free(ctx);
  101. return ret;
  102. }
  103. static int test_mod_exp(int round)
  104. {
  105. BN_CTX *ctx;
  106. unsigned char c;
  107. int ret = 0;
  108. BIGNUM *r_mont = NULL;
  109. BIGNUM *r_mont_const = NULL;
  110. BIGNUM *r_recp = NULL;
  111. BIGNUM *r_simple = NULL;
  112. BIGNUM *a = NULL;
  113. BIGNUM *b = NULL;
  114. BIGNUM *m = NULL;
  115. if (!TEST_ptr(ctx = BN_CTX_new()))
  116. goto err;
  117. if (!TEST_ptr(r_mont = BN_new())
  118. || !TEST_ptr(r_mont_const = BN_new())
  119. || !TEST_ptr(r_recp = BN_new())
  120. || !TEST_ptr(r_simple = BN_new())
  121. || !TEST_ptr(a = BN_new())
  122. || !TEST_ptr(b = BN_new())
  123. || !TEST_ptr(m = BN_new()))
  124. goto err;
  125. RAND_bytes(&c, 1);
  126. c = (c % BN_BITS) - BN_BITS2;
  127. BN_rand(a, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  128. RAND_bytes(&c, 1);
  129. c = (c % BN_BITS) - BN_BITS2;
  130. BN_rand(b, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY);
  131. RAND_bytes(&c, 1);
  132. c = (c % BN_BITS) - BN_BITS2;
  133. BN_rand(m, NUM_BITS + c, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ODD);
  134. BN_mod(a, a, m, ctx);
  135. BN_mod(b, b, m, ctx);
  136. if (!TEST_true(BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL))
  137. || !TEST_true(BN_mod_exp_recp(r_recp, a, b, m, ctx))
  138. || !TEST_true(BN_mod_exp_simple(r_simple, a, b, m, ctx))
  139. || !TEST_true(BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL)))
  140. goto err;
  141. if (TEST_int_eq(BN_cmp(r_simple, r_mont), 0)
  142. && TEST_int_eq(BN_cmp(r_simple, r_recp), 0)
  143. && TEST_int_eq(BN_cmp(r_simple, r_mont_const), 0)) {
  144. printf(".");
  145. fflush(stdout);
  146. } else {
  147. if (BN_cmp(r_simple, r_mont) != 0)
  148. fprintf(stderr, "simple and mont results differ\n");
  149. if (BN_cmp(r_simple, r_mont_const) != 0)
  150. fprintf(stderr, "simple and mont const time results differ\n");
  151. if (BN_cmp(r_simple, r_recp) != 0)
  152. fprintf(stderr, "simple and recp results differ\n");
  153. BN_print_var(a);
  154. BN_print_var(b);
  155. BN_print_var(m);
  156. BN_print_var(r_simple);
  157. BN_print_var(r_recp);
  158. BN_print_var(r_mont);
  159. BN_print_var(r_mont_const);
  160. goto err;
  161. }
  162. ret = 1;
  163. err:
  164. BN_free(r_mont);
  165. BN_free(r_mont_const);
  166. BN_free(r_recp);
  167. BN_free(r_simple);
  168. BN_free(a);
  169. BN_free(b);
  170. BN_free(m);
  171. BN_CTX_free(ctx);
  172. return ret;
  173. }
  174. void register_tests(void)
  175. {
  176. ADD_TEST(test_mod_exp_zero);
  177. ADD_ALL_TESTS(test_mod_exp, 200);
  178. }