2
0

bn_sqrt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* crypto/bn/bn_sqrt.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. */
  66. {
  67. BIGNUM *ret = in;
  68. int err = 1;
  69. int r;
  70. BIGNUM *A, *b, *q, *t, *x, *y;
  71. int e, i, j;
  72. if (!BN_is_odd(p) || BN_abs_is_word(p, 1))
  73. {
  74. if (BN_abs_is_word(p, 2))
  75. {
  76. if (ret == NULL)
  77. ret = BN_new();
  78. if (ret == NULL)
  79. goto end;
  80. if (!BN_set_word(ret, BN_is_bit_set(a, 0)))
  81. {
  82. if (ret != in)
  83. BN_free(ret);
  84. return NULL;
  85. }
  86. bn_check_top(ret);
  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. if (ret != in)
  101. BN_free(ret);
  102. return NULL;
  103. }
  104. bn_check_top(ret);
  105. return ret;
  106. }
  107. BN_CTX_start(ctx);
  108. A = BN_CTX_get(ctx);
  109. b = BN_CTX_get(ctx);
  110. q = BN_CTX_get(ctx);
  111. t = BN_CTX_get(ctx);
  112. x = BN_CTX_get(ctx);
  113. y = BN_CTX_get(ctx);
  114. if (y == NULL) goto end;
  115. if (ret == NULL)
  116. ret = BN_new();
  117. if (ret == NULL) goto end;
  118. /* A = a mod p */
  119. if (!BN_nnmod(A, a, p, ctx)) goto end;
  120. /* now write |p| - 1 as 2^e*q where q is odd */
  121. e = 1;
  122. while (!BN_is_bit_set(p, e))
  123. e++;
  124. /* we'll set q later (if needed) */
  125. if (e == 1)
  126. {
  127. /* The easy case: (|p|-1)/2 is odd, so 2 has an inverse
  128. * modulo (|p|-1)/2, and square roots can be computed
  129. * directly by modular exponentiation.
  130. * We have
  131. * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
  132. * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
  133. */
  134. if (!BN_rshift(q, p, 2)) goto end;
  135. q->neg = 0;
  136. if (!BN_add_word(q, 1)) goto end;
  137. if (!BN_mod_exp(ret, A, q, p, ctx)) goto end;
  138. err = 0;
  139. goto vrfy;
  140. }
  141. if (e == 2)
  142. {
  143. /* |p| == 5 (mod 8)
  144. *
  145. * In this case 2 is always a non-square since
  146. * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
  147. * So if a really is a square, then 2*a is a non-square.
  148. * Thus for
  149. * b := (2*a)^((|p|-5)/8),
  150. * i := (2*a)*b^2
  151. * we have
  152. * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
  153. * = (2*a)^((p-1)/2)
  154. * = -1;
  155. * so if we set
  156. * x := a*b*(i-1),
  157. * then
  158. * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
  159. * = a^2 * b^2 * (-2*i)
  160. * = a*(-i)*(2*a*b^2)
  161. * = a*(-i)*i
  162. * = a.
  163. *
  164. * (This is due to A.O.L. Atkin,
  165. * <URL: http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
  166. * November 1992.)
  167. */
  168. /* t := 2*a */
  169. if (!BN_mod_lshift1_quick(t, A, p)) goto end;
  170. /* b := (2*a)^((|p|-5)/8) */
  171. if (!BN_rshift(q, p, 3)) goto end;
  172. q->neg = 0;
  173. if (!BN_mod_exp(b, t, q, p, ctx)) goto end;
  174. /* y := b^2 */
  175. if (!BN_mod_sqr(y, b, p, ctx)) goto end;
  176. /* t := (2*a)*b^2 - 1*/
  177. if (!BN_mod_mul(t, t, y, p, ctx)) goto end;
  178. if (!BN_sub_word(t, 1)) goto end;
  179. /* x = a*b*t */
  180. if (!BN_mod_mul(x, A, b, p, ctx)) goto end;
  181. if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
  182. if (!BN_copy(ret, x)) goto end;
  183. err = 0;
  184. goto vrfy;
  185. }
  186. /* e > 2, so we really have to use the Tonelli/Shanks algorithm.
  187. * First, find some y that is not a square. */
  188. if (!BN_copy(q, p)) goto end; /* use 'q' as temp */
  189. q->neg = 0;
  190. i = 2;
  191. do
  192. {
  193. /* For efficiency, try small numbers first;
  194. * if this fails, try random numbers.
  195. */
  196. if (i < 22)
  197. {
  198. if (!BN_set_word(y, i)) goto end;
  199. }
  200. else
  201. {
  202. if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) goto end;
  203. if (BN_ucmp(y, p) >= 0)
  204. {
  205. if (!(p->neg ? BN_add : BN_sub)(y, y, p)) goto end;
  206. }
  207. /* now 0 <= y < |p| */
  208. if (BN_is_zero(y))
  209. if (!BN_set_word(y, i)) goto end;
  210. }
  211. r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
  212. if (r < -1) goto end;
  213. if (r == 0)
  214. {
  215. /* m divides p */
  216. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  217. goto end;
  218. }
  219. }
  220. while (r == 1 && ++i < 82);
  221. if (r != -1)
  222. {
  223. /* Many rounds and still no non-square -- this is more likely
  224. * a bug than just bad luck.
  225. * Even if p is not prime, we should have found some y
  226. * such that r == -1.
  227. */
  228. BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);
  229. goto end;
  230. }
  231. /* Here's our actual 'q': */
  232. if (!BN_rshift(q, q, e)) goto end;
  233. /* Now that we have some non-square, we can find an element
  234. * of order 2^e by computing its q'th power. */
  235. if (!BN_mod_exp(y, y, q, p, ctx)) goto end;
  236. if (BN_is_one(y))
  237. {
  238. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  239. goto end;
  240. }
  241. /* Now we know that (if p is indeed prime) there is an integer
  242. * k, 0 <= k < 2^e, such that
  243. *
  244. * a^q * y^k == 1 (mod p).
  245. *
  246. * As a^q is a square and y is not, k must be even.
  247. * q+1 is even, too, so there is an element
  248. *
  249. * X := a^((q+1)/2) * y^(k/2),
  250. *
  251. * and it satisfies
  252. *
  253. * X^2 = a^q * a * y^k
  254. * = a,
  255. *
  256. * so it is the square root that we are looking for.
  257. */
  258. /* t := (q-1)/2 (note that q is odd) */
  259. if (!BN_rshift1(t, q)) goto end;
  260. /* x := a^((q-1)/2) */
  261. if (BN_is_zero(t)) /* special case: p = 2^e + 1 */
  262. {
  263. if (!BN_nnmod(t, A, p, ctx)) goto end;
  264. if (BN_is_zero(t))
  265. {
  266. /* special case: a == 0 (mod p) */
  267. BN_zero(ret);
  268. err = 0;
  269. goto end;
  270. }
  271. else
  272. if (!BN_one(x)) goto end;
  273. }
  274. else
  275. {
  276. if (!BN_mod_exp(x, A, t, p, ctx)) goto end;
  277. if (BN_is_zero(x))
  278. {
  279. /* special case: a == 0 (mod p) */
  280. BN_zero(ret);
  281. err = 0;
  282. goto end;
  283. }
  284. }
  285. /* b := a*x^2 (= a^q) */
  286. if (!BN_mod_sqr(b, x, p, ctx)) goto end;
  287. if (!BN_mod_mul(b, b, A, p, ctx)) goto end;
  288. /* x := a*x (= a^((q+1)/2)) */
  289. if (!BN_mod_mul(x, x, A, p, ctx)) goto end;
  290. while (1)
  291. {
  292. /* Now b is a^q * y^k for some even k (0 <= k < 2^E
  293. * where E refers to the original value of e, which we
  294. * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
  295. *
  296. * We have a*b = x^2,
  297. * y^2^(e-1) = -1,
  298. * b^2^(e-1) = 1.
  299. */
  300. if (BN_is_one(b))
  301. {
  302. if (!BN_copy(ret, x)) goto end;
  303. err = 0;
  304. goto vrfy;
  305. }
  306. /* find smallest i such that b^(2^i) = 1 */
  307. i = 1;
  308. if (!BN_mod_sqr(t, b, p, ctx)) goto end;
  309. while (!BN_is_one(t))
  310. {
  311. i++;
  312. if (i == e)
  313. {
  314. BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
  315. goto end;
  316. }
  317. if (!BN_mod_mul(t, t, t, p, ctx)) goto end;
  318. }
  319. /* t := y^2^(e - i - 1) */
  320. if (!BN_copy(t, y)) goto end;
  321. for (j = e - i - 1; j > 0; j--)
  322. {
  323. if (!BN_mod_sqr(t, t, p, ctx)) goto end;
  324. }
  325. if (!BN_mod_mul(y, t, t, p, ctx)) goto end;
  326. if (!BN_mod_mul(x, x, t, p, ctx)) goto end;
  327. if (!BN_mod_mul(b, b, y, p, ctx)) goto end;
  328. e = i;
  329. }
  330. vrfy:
  331. if (!err)
  332. {
  333. /* verify the result -- the input might have been not a square
  334. * (test added in 0.9.8) */
  335. if (!BN_mod_sqr(x, ret, p, ctx))
  336. err = 1;
  337. if (!err && 0 != BN_cmp(x, A))
  338. {
  339. BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
  340. err = 1;
  341. }
  342. }
  343. end:
  344. if (err)
  345. {
  346. if (ret != NULL && ret != in)
  347. {
  348. BN_clear_free(ret);
  349. }
  350. ret = NULL;
  351. }
  352. BN_CTX_end(ctx);
  353. bn_check_top(ret);
  354. return ret;
  355. }