bn_sqrt.c 12 KB

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