bn_sqrt.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* crypto/bn/bn_mod.c */
  2. /* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
  3. * and Bodo Moeller for the OpenSSL project. */
  4. /* ====================================================================
  5. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * openssl-core@openssl.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. #include "cryptlib.h"
  58. #include "bn_lcl.h"
  59. BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  60. /* Returns 'ret' such that
  61. * ret^2 == a (mod p),
  62. * using the Tonelli/Shanks algorithm (cf. Henri Cohen, "A Course
  63. * in Algebraic Computational Number Theory", algorithm 1.5.1).
  64. * 'p' must be prime!
  65. * If 'a' is not a square, this is not necessarily detected by
  66. * the algorithms; a bogus result must be expected in this case.
  67. */
  68. {
  69. BIGNUM *ret = in;
  70. int err = 1;
  71. int r;
  72. BIGNUM *b, *q, *t, *x, *y;
  73. int e, i, j;
  74. if (!BN_is_odd(p) || BN_abs_is_word(p, 1))
  75. {
  76. if (BN_abs_is_word(p, 2))
  77. {
  78. if (ret == NULL)
  79. ret = BN_new();
  80. if (ret == NULL)
  81. goto end;
  82. if (!BN_set_word(ret, BN_is_bit_set(a, 0)))
  83. {
  84. BN_free(ret);
  85. return NULL;
  86. }
  87. return ret;
  88. }
  89. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  90. return(NULL);
  91. }
  92. if (BN_is_zero(a) || BN_is_one(a))
  93. {
  94. if (ret == NULL)
  95. ret = BN_new();
  96. if (ret == NULL)
  97. goto end;
  98. if (!BN_set_word(ret, BN_is_one(a)))
  99. {
  100. BN_free(ret);
  101. return NULL;
  102. }
  103. return ret;
  104. }
  105. #if 0 /* if BN_mod_sqrt is used with correct input, this just wastes time */
  106. r = BN_kronecker(a, p, ctx);
  107. if (r < -1) return NULL;
  108. if (r == -1)
  109. {
  110. BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
  111. return(NULL);
  112. }
  113. #endif
  114. BN_CTX_start(ctx);
  115. b = BN_CTX_get(ctx);
  116. q = BN_CTX_get(ctx);
  117. t = BN_CTX_get(ctx);
  118. x = BN_CTX_get(ctx);
  119. y = BN_CTX_get(ctx);
  120. if (y == NULL) goto end;
  121. if (ret == NULL)
  122. ret = BN_new();
  123. if (ret == NULL) goto end;
  124. /* now write |p| - 1 as 2^e*q where q is odd */
  125. e = 1;
  126. while (!BN_is_bit_set(p, e))
  127. e++;
  128. /* we'll set q later (if needed) */
  129. if (e == 1)
  130. {
  131. /* The easy case: (|p|-1)/2 is odd, so 2 has an inverse
  132. * modulo (|p|-1)/2, and square roots can be computed
  133. * directly by modular exponentiation.
  134. * We have
  135. * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
  136. * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
  137. */
  138. if (!BN_rshift(q, p, 2)) goto end;
  139. q->neg = 0;
  140. if (!BN_add_word(q, 1)) goto end;
  141. if (!BN_mod_exp(ret, a, q, p, ctx)) goto end;
  142. err = 0;
  143. goto end;
  144. }
  145. if (e == 2)
  146. {
  147. /* |p| == 5 (mod 8)
  148. *
  149. * In this case 2 is always a non-square since
  150. * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
  151. * So if a really is a square, then 2*a is a non-square.
  152. * Thus for
  153. * b := (2*a)^((|p|-5)/8),
  154. * i := (2*a)*b^2
  155. * we have
  156. * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
  157. * = (2*a)^((p-1)/2)
  158. * = -1;
  159. * so if we set
  160. * x := a*b*(i-1),
  161. * then
  162. * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
  163. * = a^2 * b^2 * (-2*i)
  164. * = a*(-i)*(2*a*b^2)
  165. * = a*(-i)*i
  166. * = a.
  167. *
  168. * (This is due to A.O.L. Atkin,
  169. * <URL: http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
  170. * November 1992.)
  171. */
  172. /* make sure that a is reduced modulo p */
  173. if (a->neg || BN_ucmp(a, p) >= 0)
  174. {
  175. if (!BN_nnmod(x, a, p, ctx)) goto end;
  176. a = x; /* use x as temporary variable */
  177. }
  178. /* t := 2*a */
  179. if (!BN_mod_lshift1_quick(t, a, p)) goto end;
  180. /* b := (2*a)^((|p|-5)/8) */
  181. if (!BN_rshift(q, p, 3)) goto end;
  182. q->neg = 0;
  183. if (!BN_mod_exp(b, t, q, p, ctx)) goto end;
  184. /* y := b^2 */
  185. if (!BN_mod_sqr(y, b, p, ctx)) goto end;
  186. /* t := (2*a)*b^2 - 1*/
  187. if (!BN_mod_mul(t, t, y, p, ctx)) goto end;
  188. if (!BN_sub_word(t, 1)) goto end;
  189. /* x = a*b*t */
  190. if (!BN_mod_mul(x, a, b, p, ctx)) goto end;
  191. if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
  192. if (!BN_copy(ret, x)) goto end;
  193. err = 0;
  194. goto end;
  195. }
  196. /* e > 2, so we really have to use the Tonelli/Shanks algorithm.
  197. * First, find some y that is not a square. */
  198. if (!BN_copy(q, p)) goto end; /* use 'q' as temp */
  199. q->neg = 0;
  200. i = 2;
  201. do
  202. {
  203. /* For efficiency, try small numbers first;
  204. * if this fails, try random numbers.
  205. */
  206. if (i < 22)
  207. {
  208. if (!BN_set_word(y, i)) goto end;
  209. }
  210. else
  211. {
  212. if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) goto end;
  213. if (BN_ucmp(y, p) >= 0)
  214. {
  215. if (!(p->neg ? BN_add : BN_sub)(y, y, p)) goto end;
  216. }
  217. /* now 0 <= y < |p| */
  218. if (BN_is_zero(y))
  219. if (!BN_set_word(y, i)) goto end;
  220. }
  221. r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
  222. if (r < -1) goto end;
  223. if (r == 0)
  224. {
  225. /* m divides p */
  226. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  227. goto end;
  228. }
  229. }
  230. while (r == 1 && ++i < 82);
  231. if (r != -1)
  232. {
  233. /* Many rounds and still no non-square -- this is more likely
  234. * a bug than just bad luck.
  235. * Even if p is not prime, we should have found some y
  236. * such that r == -1.
  237. */
  238. BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);
  239. goto end;
  240. }
  241. /* Here's our actual 'q': */
  242. if (!BN_rshift(q, q, e)) goto end;
  243. /* Now that we have some non-square, we can find an element
  244. * of order 2^e by computing its q'th power. */
  245. if (!BN_mod_exp(y, y, q, p, ctx)) goto end;
  246. if (BN_is_one(y))
  247. {
  248. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  249. goto end;
  250. }
  251. /* Now we know that (if p is indeed prime) there is an integer
  252. * k, 0 <= k < 2^e, such that
  253. *
  254. * a^q * y^k == 1 (mod p).
  255. *
  256. * As a^q is a square and y is not, k must be even.
  257. * q+1 is even, too, so there is an element
  258. *
  259. * X := a^((q+1)/2) * y^(k/2),
  260. *
  261. * and it satisfies
  262. *
  263. * X^2 = a^q * a * y^k
  264. * = a,
  265. *
  266. * so it is the square root that we are looking for.
  267. */
  268. /* t := (q-1)/2 (note that q is odd) */
  269. if (!BN_rshift1(t, q)) goto end;
  270. /* x := a^((q-1)/2) */
  271. if (BN_is_zero(t)) /* special case: p = 2^e + 1 */
  272. {
  273. if (!BN_nnmod(t, a, p, ctx)) goto end;
  274. if (BN_is_zero(t))
  275. {
  276. /* special case: a == 0 (mod p) */
  277. if (!BN_zero(ret)) goto end;
  278. err = 0;
  279. goto end;
  280. }
  281. else
  282. if (!BN_one(x)) goto end;
  283. }
  284. else
  285. {
  286. if (!BN_mod_exp(x, a, t, p, ctx)) goto end;
  287. if (BN_is_zero(x))
  288. {
  289. /* special case: a == 0 (mod p) */
  290. if (!BN_zero(ret)) goto end;
  291. err = 0;
  292. goto end;
  293. }
  294. }
  295. /* b := a*x^2 (= a^q) */
  296. if (!BN_mod_sqr(b, x, p, ctx)) goto end;
  297. if (!BN_mod_mul(b, b, a, p, ctx)) goto end;
  298. /* x := a*x (= a^((q+1)/2)) */
  299. if (!BN_mod_mul(x, x, a, p, ctx)) goto end;
  300. while (1)
  301. {
  302. /* Now b is a^q * y^k for some even k (0 <= k < 2^E
  303. * where E refers to the original value of e, which we
  304. * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
  305. *
  306. * We have a*b = x^2,
  307. * y^2^(e-1) = -1,
  308. * b^2^(e-1) = 1.
  309. */
  310. if (BN_is_one(b))
  311. {
  312. if (!BN_copy(ret, x)) goto end;
  313. err = 0;
  314. goto end;
  315. }
  316. /* find smallest i such that b^(2^i) = 1 */
  317. i = 1;
  318. if (!BN_mod_sqr(t, b, p, ctx)) goto end;
  319. while (!BN_is_one(t))
  320. {
  321. i++;
  322. if (i == e)
  323. {
  324. BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
  325. goto end;
  326. }
  327. if (!BN_mod_mul(t, t, t, p, ctx)) goto end;
  328. }
  329. /* t := y^2^(e - i - 1) */
  330. if (!BN_copy(t, y)) goto end;
  331. for (j = e - i - 1; j > 0; j--)
  332. {
  333. if (!BN_mod_sqr(t, t, p, ctx)) goto end;
  334. }
  335. if (!BN_mod_mul(y, t, t, p, ctx)) goto end;
  336. if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
  337. if (!BN_mod_mul(b, b, y, p, ctx)) goto end;
  338. e = i;
  339. }
  340. end:
  341. if (err)
  342. {
  343. if (ret != NULL && ret != in)
  344. {
  345. BN_clear_free(ret);
  346. }
  347. ret = NULL;
  348. }
  349. BN_CTX_end(ctx);
  350. return ret;
  351. }