bn_sqrt.c 9.5 KB

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