bn_sqrt.c 9.6 KB

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