bn_sqrt.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright 2000-2018 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 "internal/cryptlib.h"
  10. #include "bn_lcl.h"
  11. BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  12. /*
  13. * Returns 'ret' such that ret^2 == a (mod p), using the Tonelli/Shanks
  14. * algorithm (cf. Henri Cohen, "A Course in Algebraic Computational Number
  15. * Theory", algorithm 1.5.1). 'p' must be prime!
  16. */
  17. {
  18. BIGNUM *ret = in;
  19. int err = 1;
  20. int r;
  21. BIGNUM *A, *b, *q, *t, *x, *y;
  22. int e, i, j;
  23. if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
  24. if (BN_abs_is_word(p, 2)) {
  25. if (ret == NULL)
  26. ret = BN_new();
  27. if (ret == NULL)
  28. goto end;
  29. if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {
  30. if (ret != in)
  31. BN_free(ret);
  32. return NULL;
  33. }
  34. bn_check_top(ret);
  35. return ret;
  36. }
  37. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  38. return NULL;
  39. }
  40. if (BN_is_zero(a) || BN_is_one(a)) {
  41. if (ret == NULL)
  42. ret = BN_new();
  43. if (ret == NULL)
  44. goto end;
  45. if (!BN_set_word(ret, BN_is_one(a))) {
  46. if (ret != in)
  47. BN_free(ret);
  48. return NULL;
  49. }
  50. bn_check_top(ret);
  51. return ret;
  52. }
  53. BN_CTX_start(ctx);
  54. A = BN_CTX_get(ctx);
  55. b = BN_CTX_get(ctx);
  56. q = BN_CTX_get(ctx);
  57. t = BN_CTX_get(ctx);
  58. x = BN_CTX_get(ctx);
  59. y = BN_CTX_get(ctx);
  60. if (y == NULL)
  61. goto end;
  62. if (ret == NULL)
  63. ret = BN_new();
  64. if (ret == NULL)
  65. goto end;
  66. /* A = a mod p */
  67. if (!BN_nnmod(A, a, p, ctx))
  68. goto end;
  69. /* now write |p| - 1 as 2^e*q where q is odd */
  70. e = 1;
  71. while (!BN_is_bit_set(p, e))
  72. e++;
  73. /* we'll set q later (if needed) */
  74. if (e == 1) {
  75. /*-
  76. * The easy case: (|p|-1)/2 is odd, so 2 has an inverse
  77. * modulo (|p|-1)/2, and square roots can be computed
  78. * directly by modular exponentiation.
  79. * We have
  80. * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
  81. * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
  82. */
  83. if (!BN_rshift(q, p, 2))
  84. goto end;
  85. q->neg = 0;
  86. if (!BN_add_word(q, 1))
  87. goto end;
  88. if (!BN_mod_exp(ret, A, q, p, ctx))
  89. goto end;
  90. err = 0;
  91. goto vrfy;
  92. }
  93. if (e == 2) {
  94. /*-
  95. * |p| == 5 (mod 8)
  96. *
  97. * In this case 2 is always a non-square since
  98. * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
  99. * So if a really is a square, then 2*a is a non-square.
  100. * Thus for
  101. * b := (2*a)^((|p|-5)/8),
  102. * i := (2*a)*b^2
  103. * we have
  104. * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
  105. * = (2*a)^((p-1)/2)
  106. * = -1;
  107. * so if we set
  108. * x := a*b*(i-1),
  109. * then
  110. * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
  111. * = a^2 * b^2 * (-2*i)
  112. * = a*(-i)*(2*a*b^2)
  113. * = a*(-i)*i
  114. * = a.
  115. *
  116. * (This is due to A.O.L. Atkin,
  117. * <URL: http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
  118. * November 1992.)
  119. */
  120. /* t := 2*a */
  121. if (!BN_mod_lshift1_quick(t, A, p))
  122. goto end;
  123. /* b := (2*a)^((|p|-5)/8) */
  124. if (!BN_rshift(q, p, 3))
  125. goto end;
  126. q->neg = 0;
  127. if (!BN_mod_exp(b, t, q, p, ctx))
  128. goto end;
  129. /* y := b^2 */
  130. if (!BN_mod_sqr(y, b, p, ctx))
  131. goto end;
  132. /* t := (2*a)*b^2 - 1 */
  133. if (!BN_mod_mul(t, t, y, p, ctx))
  134. goto end;
  135. if (!BN_sub_word(t, 1))
  136. goto end;
  137. /* x = a*b*t */
  138. if (!BN_mod_mul(x, A, b, p, ctx))
  139. goto end;
  140. if (!BN_mod_mul(x, x, t, p, ctx))
  141. goto end;
  142. if (!BN_copy(ret, x))
  143. goto end;
  144. err = 0;
  145. goto vrfy;
  146. }
  147. /*
  148. * e > 2, so we really have to use the Tonelli/Shanks algorithm. First,
  149. * find some y that is not a square.
  150. */
  151. if (!BN_copy(q, p))
  152. goto end; /* use 'q' as temp */
  153. q->neg = 0;
  154. i = 2;
  155. do {
  156. /*
  157. * For efficiency, try small numbers first; if this fails, try random
  158. * numbers.
  159. */
  160. if (i < 22) {
  161. if (!BN_set_word(y, i))
  162. goto end;
  163. } else {
  164. if (!BN_priv_rand(y, BN_num_bits(p), 0, 0))
  165. goto end;
  166. if (BN_ucmp(y, p) >= 0) {
  167. if (!(p->neg ? BN_add : BN_sub) (y, y, p))
  168. goto end;
  169. }
  170. /* now 0 <= y < |p| */
  171. if (BN_is_zero(y))
  172. if (!BN_set_word(y, i))
  173. goto end;
  174. }
  175. r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
  176. if (r < -1)
  177. goto end;
  178. if (r == 0) {
  179. /* m divides p */
  180. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  181. goto end;
  182. }
  183. }
  184. while (r == 1 && ++i < 82);
  185. if (r != -1) {
  186. /*
  187. * Many rounds and still no non-square -- this is more likely a bug
  188. * than just bad luck. Even if p is not prime, we should have found
  189. * some y such that r == -1.
  190. */
  191. BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);
  192. goto end;
  193. }
  194. /* Here's our actual 'q': */
  195. if (!BN_rshift(q, q, e))
  196. goto end;
  197. /*
  198. * Now that we have some non-square, we can find an element of order 2^e
  199. * by computing its q'th power.
  200. */
  201. if (!BN_mod_exp(y, y, q, p, ctx))
  202. goto end;
  203. if (BN_is_one(y)) {
  204. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  205. goto end;
  206. }
  207. /*-
  208. * Now we know that (if p is indeed prime) there is an integer
  209. * k, 0 <= k < 2^e, such that
  210. *
  211. * a^q * y^k == 1 (mod p).
  212. *
  213. * As a^q is a square and y is not, k must be even.
  214. * q+1 is even, too, so there is an element
  215. *
  216. * X := a^((q+1)/2) * y^(k/2),
  217. *
  218. * and it satisfies
  219. *
  220. * X^2 = a^q * a * y^k
  221. * = a,
  222. *
  223. * so it is the square root that we are looking for.
  224. */
  225. /* t := (q-1)/2 (note that q is odd) */
  226. if (!BN_rshift1(t, q))
  227. goto end;
  228. /* x := a^((q-1)/2) */
  229. if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */
  230. if (!BN_nnmod(t, A, p, ctx))
  231. goto end;
  232. if (BN_is_zero(t)) {
  233. /* special case: a == 0 (mod p) */
  234. BN_zero(ret);
  235. err = 0;
  236. goto end;
  237. } else if (!BN_one(x))
  238. goto end;
  239. } else {
  240. if (!BN_mod_exp(x, A, t, p, ctx))
  241. goto end;
  242. if (BN_is_zero(x)) {
  243. /* special case: a == 0 (mod p) */
  244. BN_zero(ret);
  245. err = 0;
  246. goto end;
  247. }
  248. }
  249. /* b := a*x^2 (= a^q) */
  250. if (!BN_mod_sqr(b, x, p, ctx))
  251. goto end;
  252. if (!BN_mod_mul(b, b, A, p, ctx))
  253. goto end;
  254. /* x := a*x (= a^((q+1)/2)) */
  255. if (!BN_mod_mul(x, x, A, p, ctx))
  256. goto end;
  257. while (1) {
  258. /*-
  259. * Now b is a^q * y^k for some even k (0 <= k < 2^E
  260. * where E refers to the original value of e, which we
  261. * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
  262. *
  263. * We have a*b = x^2,
  264. * y^2^(e-1) = -1,
  265. * b^2^(e-1) = 1.
  266. */
  267. if (BN_is_one(b)) {
  268. if (!BN_copy(ret, x))
  269. goto end;
  270. err = 0;
  271. goto vrfy;
  272. }
  273. /* find smallest i such that b^(2^i) = 1 */
  274. i = 1;
  275. if (!BN_mod_sqr(t, b, p, ctx))
  276. goto end;
  277. while (!BN_is_one(t)) {
  278. i++;
  279. if (i == e) {
  280. BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
  281. goto end;
  282. }
  283. if (!BN_mod_mul(t, t, t, p, ctx))
  284. goto end;
  285. }
  286. /* t := y^2^(e - i - 1) */
  287. if (!BN_copy(t, y))
  288. goto end;
  289. for (j = e - i - 1; j > 0; j--) {
  290. if (!BN_mod_sqr(t, t, p, ctx))
  291. goto end;
  292. }
  293. if (!BN_mod_mul(y, t, t, p, ctx))
  294. goto end;
  295. if (!BN_mod_mul(x, x, t, p, ctx))
  296. goto end;
  297. if (!BN_mod_mul(b, b, y, p, ctx))
  298. goto end;
  299. e = i;
  300. }
  301. vrfy:
  302. if (!err) {
  303. /*
  304. * verify the result -- the input might have been not a square (test
  305. * added in 0.9.8)
  306. */
  307. if (!BN_mod_sqr(x, ret, p, ctx))
  308. err = 1;
  309. if (!err && 0 != BN_cmp(x, A)) {
  310. BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
  311. err = 1;
  312. }
  313. }
  314. end:
  315. if (err) {
  316. if (ret != in)
  317. BN_clear_free(ret);
  318. ret = NULL;
  319. }
  320. BN_CTX_end(ctx);
  321. bn_check_top(ret);
  322. return ret;
  323. }