bn_sqrt.c 9.3 KB

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