2
0

bn_sqrt.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright 2000-2019 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!
  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. * Subject: Square Roots and Cognate Matters modulo p=8n+5.
  118. * URL: https://listserv.nodak.edu/cgi-bin/wa.exe?A2=ind9211&L=NMBRTHRY&P=4026
  119. * November 1992.)
  120. */
  121. /* t := 2*a */
  122. if (!BN_mod_lshift1_quick(t, A, p))
  123. goto end;
  124. /* b := (2*a)^((|p|-5)/8) */
  125. if (!BN_rshift(q, p, 3))
  126. goto end;
  127. q->neg = 0;
  128. if (!BN_mod_exp(b, t, q, p, ctx))
  129. goto end;
  130. /* y := b^2 */
  131. if (!BN_mod_sqr(y, b, p, ctx))
  132. goto end;
  133. /* t := (2*a)*b^2 - 1 */
  134. if (!BN_mod_mul(t, t, y, p, ctx))
  135. goto end;
  136. if (!BN_sub_word(t, 1))
  137. goto end;
  138. /* x = a*b*t */
  139. if (!BN_mod_mul(x, A, b, p, ctx))
  140. goto end;
  141. if (!BN_mod_mul(x, x, t, p, ctx))
  142. goto end;
  143. if (!BN_copy(ret, x))
  144. goto end;
  145. err = 0;
  146. goto vrfy;
  147. }
  148. /*
  149. * e > 2, so we really have to use the Tonelli/Shanks algorithm. First,
  150. * find some y that is not a square.
  151. */
  152. if (!BN_copy(q, p))
  153. goto end; /* use 'q' as temp */
  154. q->neg = 0;
  155. i = 2;
  156. do {
  157. /*
  158. * For efficiency, try small numbers first; if this fails, try random
  159. * numbers.
  160. */
  161. if (i < 22) {
  162. if (!BN_set_word(y, i))
  163. goto end;
  164. } else {
  165. if (!BN_priv_rand(y, BN_num_bits(p), 0, 0))
  166. goto end;
  167. if (BN_ucmp(y, p) >= 0) {
  168. if (!(p->neg ? BN_add : BN_sub) (y, y, p))
  169. goto end;
  170. }
  171. /* now 0 <= y < |p| */
  172. if (BN_is_zero(y))
  173. if (!BN_set_word(y, i))
  174. goto end;
  175. }
  176. r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
  177. if (r < -1)
  178. goto end;
  179. if (r == 0) {
  180. /* m divides p */
  181. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  182. goto end;
  183. }
  184. }
  185. while (r == 1 && ++i < 82);
  186. if (r != -1) {
  187. /*
  188. * Many rounds and still no non-square -- this is more likely a bug
  189. * than just bad luck. Even if p is not prime, we should have found
  190. * some y such that r == -1.
  191. */
  192. BNerr(BN_F_BN_MOD_SQRT, BN_R_TOO_MANY_ITERATIONS);
  193. goto end;
  194. }
  195. /* Here's our actual 'q': */
  196. if (!BN_rshift(q, q, e))
  197. goto end;
  198. /*
  199. * Now that we have some non-square, we can find an element of order 2^e
  200. * by computing its q'th power.
  201. */
  202. if (!BN_mod_exp(y, y, q, p, ctx))
  203. goto end;
  204. if (BN_is_one(y)) {
  205. BNerr(BN_F_BN_MOD_SQRT, BN_R_P_IS_NOT_PRIME);
  206. goto end;
  207. }
  208. /*-
  209. * Now we know that (if p is indeed prime) there is an integer
  210. * k, 0 <= k < 2^e, such that
  211. *
  212. * a^q * y^k == 1 (mod p).
  213. *
  214. * As a^q is a square and y is not, k must be even.
  215. * q+1 is even, too, so there is an element
  216. *
  217. * X := a^((q+1)/2) * y^(k/2),
  218. *
  219. * and it satisfies
  220. *
  221. * X^2 = a^q * a * y^k
  222. * = a,
  223. *
  224. * so it is the square root that we are looking for.
  225. */
  226. /* t := (q-1)/2 (note that q is odd) */
  227. if (!BN_rshift1(t, q))
  228. goto end;
  229. /* x := a^((q-1)/2) */
  230. if (BN_is_zero(t)) { /* special case: p = 2^e + 1 */
  231. if (!BN_nnmod(t, A, p, ctx))
  232. goto end;
  233. if (BN_is_zero(t)) {
  234. /* special case: a == 0 (mod p) */
  235. BN_zero(ret);
  236. err = 0;
  237. goto end;
  238. } else if (!BN_one(x))
  239. goto end;
  240. } else {
  241. if (!BN_mod_exp(x, A, t, p, ctx))
  242. goto end;
  243. if (BN_is_zero(x)) {
  244. /* special case: a == 0 (mod p) */
  245. BN_zero(ret);
  246. err = 0;
  247. goto end;
  248. }
  249. }
  250. /* b := a*x^2 (= a^q) */
  251. if (!BN_mod_sqr(b, x, p, ctx))
  252. goto end;
  253. if (!BN_mod_mul(b, b, A, p, ctx))
  254. goto end;
  255. /* x := a*x (= a^((q+1)/2)) */
  256. if (!BN_mod_mul(x, x, A, p, ctx))
  257. goto end;
  258. while (1) {
  259. /*-
  260. * Now b is a^q * y^k for some even k (0 <= k < 2^E
  261. * where E refers to the original value of e, which we
  262. * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
  263. *
  264. * We have a*b = x^2,
  265. * y^2^(e-1) = -1,
  266. * b^2^(e-1) = 1.
  267. */
  268. if (BN_is_one(b)) {
  269. if (!BN_copy(ret, x))
  270. goto end;
  271. err = 0;
  272. goto vrfy;
  273. }
  274. /* find smallest i such that b^(2^i) = 1 */
  275. i = 1;
  276. if (!BN_mod_sqr(t, b, p, ctx))
  277. goto end;
  278. while (!BN_is_one(t)) {
  279. i++;
  280. if (i == e) {
  281. BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
  282. goto end;
  283. }
  284. if (!BN_mod_mul(t, t, t, p, ctx))
  285. goto end;
  286. }
  287. /* t := y^2^(e - i - 1) */
  288. if (!BN_copy(t, y))
  289. goto end;
  290. for (j = e - i - 1; j > 0; j--) {
  291. if (!BN_mod_sqr(t, t, p, ctx))
  292. goto end;
  293. }
  294. if (!BN_mod_mul(y, t, t, p, ctx))
  295. goto end;
  296. if (!BN_mod_mul(x, x, t, p, ctx))
  297. goto end;
  298. if (!BN_mod_mul(b, b, y, p, ctx))
  299. goto end;
  300. e = i;
  301. }
  302. vrfy:
  303. if (!err) {
  304. /*
  305. * verify the result -- the input might have been not a square (test
  306. * added in 0.9.8)
  307. */
  308. if (!BN_mod_sqr(x, ret, p, ctx))
  309. err = 1;
  310. if (!err && 0 != BN_cmp(x, A)) {
  311. BNerr(BN_F_BN_MOD_SQRT, BN_R_NOT_A_SQUARE);
  312. err = 1;
  313. }
  314. }
  315. end:
  316. if (err) {
  317. if (ret != in)
  318. BN_clear_free(ret);
  319. ret = NULL;
  320. }
  321. BN_CTX_end(ctx);
  322. bn_check_top(ret);
  323. return ret;
  324. }