exptest.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include "../e_os.h"
  61. #include <openssl/bio.h>
  62. #include <openssl/bn.h>
  63. #include <openssl/rand.h>
  64. #include <openssl/err.h>
  65. #define NUM_BITS (BN_BITS2 * 4)
  66. static const char rnd_seed[] =
  67. "string to make the random number generator think it has entropy";
  68. /*
  69. * Test that r == 0 in test_exp_mod_zero(). Returns one on success,
  70. * returns zero and prints debug output otherwise.
  71. */
  72. static int a_is_zero_mod_one(const char *method, const BIGNUM *r,
  73. const BIGNUM *a) {
  74. if (!BN_is_zero(r)) {
  75. fprintf(stderr, "%s failed:\n", method);
  76. fprintf(stderr, "a ** 0 mod 1 = r (should be 0)\n");
  77. fprintf(stderr, "a = ");
  78. BN_print_fp(stderr, a);
  79. fprintf(stderr, "\nr = ");
  80. BN_print_fp(stderr, r);
  81. fprintf(stderr, "\n");
  82. return 0;
  83. }
  84. return 1;
  85. }
  86. /*
  87. * test_exp_mod_zero tests that x**0 mod 1 == 0. It returns zero on success.
  88. */
  89. static int test_exp_mod_zero()
  90. {
  91. BIGNUM *a = NULL, *p = NULL, *m = NULL;
  92. BIGNUM *r = NULL;
  93. BN_ULONG one_word = 1;
  94. BN_CTX *ctx = BN_CTX_new();
  95. int ret = 1, failed = 0;
  96. m = BN_new();
  97. if (!m)
  98. goto err;
  99. BN_one(m);
  100. a = BN_new();
  101. if (!a)
  102. goto err;
  103. BN_one(a);
  104. p = BN_new();
  105. if (!p)
  106. goto err;
  107. BN_zero(p);
  108. r = BN_new();
  109. if (!r)
  110. goto err;
  111. if (!BN_rand(a, 1024, 0, 0))
  112. goto err;
  113. if (!BN_mod_exp(r, a, p, m, ctx))
  114. goto err;
  115. if (!a_is_zero_mod_one("BN_mod_exp", r, a))
  116. failed = 1;
  117. if (!BN_mod_exp_recp(r, a, p, m, ctx))
  118. goto err;
  119. if (!a_is_zero_mod_one("BN_mod_exp_recp", r, a))
  120. failed = 1;
  121. if (!BN_mod_exp_simple(r, a, p, m, ctx))
  122. goto err;
  123. if (!a_is_zero_mod_one("BN_mod_exp_simple", r, a))
  124. failed = 1;
  125. if (!BN_mod_exp_mont(r, a, p, m, ctx, NULL))
  126. goto err;
  127. if (!a_is_zero_mod_one("BN_mod_exp_mont", r, a))
  128. failed = 1;
  129. if (!BN_mod_exp_mont_consttime(r, a, p, m, ctx, NULL)) {
  130. goto err;
  131. }
  132. if (!a_is_zero_mod_one("BN_mod_exp_mont_consttime", r, a))
  133. failed = 1;
  134. /*
  135. * A different codepath exists for single word multiplication
  136. * in non-constant-time only.
  137. */
  138. if (!BN_mod_exp_mont_word(r, one_word, p, m, ctx, NULL))
  139. goto err;
  140. if (!BN_is_zero(r)) {
  141. fprintf(stderr, "BN_mod_exp_mont_word failed:\n");
  142. fprintf(stderr, "1 ** 0 mod 1 = r (should be 0)\n");
  143. fprintf(stderr, "r = ");
  144. BN_print_fp(stderr, r);
  145. fprintf(stderr, "\n");
  146. return 0;
  147. }
  148. ret = failed;
  149. err:
  150. BN_free(r);
  151. BN_free(a);
  152. BN_free(p);
  153. BN_free(m);
  154. BN_CTX_free(ctx);
  155. return ret;
  156. }
  157. int main(int argc, char *argv[])
  158. {
  159. BN_CTX *ctx;
  160. BIO *out = NULL;
  161. int i, ret;
  162. unsigned char c;
  163. BIGNUM *r_mont, *r_mont_const, *r_recp, *r_simple, *a, *b, *m;
  164. RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we
  165. * don't even check its return
  166. * value (which we should) */
  167. ctx = BN_CTX_new();
  168. if (ctx == NULL)
  169. EXIT(1);
  170. r_mont = BN_new();
  171. r_mont_const = BN_new();
  172. r_recp = BN_new();
  173. r_simple = BN_new();
  174. a = BN_new();
  175. b = BN_new();
  176. m = BN_new();
  177. if ((r_mont == NULL) || (r_recp == NULL) || (a == NULL) || (b == NULL))
  178. goto err;
  179. out = BIO_new(BIO_s_file());
  180. if (out == NULL)
  181. EXIT(1);
  182. BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
  183. for (i = 0; i < 200; i++) {
  184. RAND_bytes(&c, 1);
  185. c = (c % BN_BITS) - BN_BITS2;
  186. BN_rand(a, NUM_BITS + c, 0, 0);
  187. RAND_bytes(&c, 1);
  188. c = (c % BN_BITS) - BN_BITS2;
  189. BN_rand(b, NUM_BITS + c, 0, 0);
  190. RAND_bytes(&c, 1);
  191. c = (c % BN_BITS) - BN_BITS2;
  192. BN_rand(m, NUM_BITS + c, 0, 1);
  193. BN_mod(a, a, m, ctx);
  194. BN_mod(b, b, m, ctx);
  195. ret = BN_mod_exp_mont(r_mont, a, b, m, ctx, NULL);
  196. if (ret <= 0) {
  197. printf("BN_mod_exp_mont() problems\n");
  198. ERR_print_errors(out);
  199. EXIT(1);
  200. }
  201. ret = BN_mod_exp_recp(r_recp, a, b, m, ctx);
  202. if (ret <= 0) {
  203. printf("BN_mod_exp_recp() problems\n");
  204. ERR_print_errors(out);
  205. EXIT(1);
  206. }
  207. ret = BN_mod_exp_simple(r_simple, a, b, m, ctx);
  208. if (ret <= 0) {
  209. printf("BN_mod_exp_simple() problems\n");
  210. ERR_print_errors(out);
  211. EXIT(1);
  212. }
  213. ret = BN_mod_exp_mont_consttime(r_mont_const, a, b, m, ctx, NULL);
  214. if (ret <= 0) {
  215. printf("BN_mod_exp_mont_consttime() problems\n");
  216. ERR_print_errors(out);
  217. EXIT(1);
  218. }
  219. if (BN_cmp(r_simple, r_mont) == 0
  220. && BN_cmp(r_simple, r_recp) == 0
  221. && BN_cmp(r_simple, r_mont_const) == 0) {
  222. printf(".");
  223. fflush(stdout);
  224. } else {
  225. if (BN_cmp(r_simple, r_mont) != 0)
  226. printf("\nsimple and mont results differ\n");
  227. if (BN_cmp(r_simple, r_mont_const) != 0)
  228. printf("\nsimple and mont const time results differ\n");
  229. if (BN_cmp(r_simple, r_recp) != 0)
  230. printf("\nsimple and recp results differ\n");
  231. printf("a (%3d) = ", BN_num_bits(a));
  232. BN_print(out, a);
  233. printf("\nb (%3d) = ", BN_num_bits(b));
  234. BN_print(out, b);
  235. printf("\nm (%3d) = ", BN_num_bits(m));
  236. BN_print(out, m);
  237. printf("\nsimple =");
  238. BN_print(out, r_simple);
  239. printf("\nrecp =");
  240. BN_print(out, r_recp);
  241. printf("\nmont =");
  242. BN_print(out, r_mont);
  243. printf("\nmont_ct =");
  244. BN_print(out, r_mont_const);
  245. printf("\n");
  246. EXIT(1);
  247. }
  248. }
  249. BN_free(r_mont);
  250. BN_free(r_mont_const);
  251. BN_free(r_recp);
  252. BN_free(r_simple);
  253. BN_free(a);
  254. BN_free(b);
  255. BN_free(m);
  256. BN_CTX_free(ctx);
  257. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  258. if (CRYPTO_mem_leaks(out) <= 0)
  259. goto err;
  260. #endif
  261. BIO_free(out);
  262. printf("\n");
  263. if (test_exp_mod_zero() != 0)
  264. goto err;
  265. printf("done\n");
  266. EXIT(0);
  267. err:
  268. ERR_print_errors(out);
  269. #ifdef OPENSSL_SYS_NETWARE
  270. printf("ERROR\n");
  271. #endif
  272. EXIT(1);
  273. }