Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

506 linhas
12 KiB

  1. /* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
  2. * and Bodo Moeller for the OpenSSL project. */
  3. /* ====================================================================
  4. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. *
  18. * 3. All advertising materials mentioning features or use of this
  19. * software must display the following acknowledgment:
  20. * "This product includes software developed by the OpenSSL Project
  21. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  22. *
  23. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  24. * endorse or promote products derived from this software without
  25. * prior written permission. For written permission, please contact
  26. * openssl-core@openssl.org.
  27. *
  28. * 5. Products derived from this software may not be called "OpenSSL"
  29. * nor may "OpenSSL" appear in their names without prior written
  30. * permission of the OpenSSL Project.
  31. *
  32. * 6. Redistributions of any form whatsoever must retain the following
  33. * acknowledgment:
  34. * "This product includes software developed by the OpenSSL Project
  35. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  38. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. * OF THE POSSIBILITY OF SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This product includes cryptographic software written by Eric Young
  52. * (eay@cryptsoft.com). This product includes software written by Tim
  53. * Hudson (tjh@cryptsoft.com). */
  54. #include <openssl/bn.h>
  55. #include <openssl/err.h>
  56. /* Returns 'ret' such that
  57. * ret^2 == a (mod p),
  58. * using the Tonelli/Shanks algorithm (cf. Henri Cohen, "A Course
  59. * in Algebraic Computational Number Theory", algorithm 1.5.1).
  60. * 'p' must be prime! */
  61. BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) {
  62. BIGNUM *ret = in;
  63. int err = 1;
  64. int r;
  65. BIGNUM *A, *b, *q, *t, *x, *y;
  66. int e, i, j;
  67. if (!BN_is_odd(p) || BN_abs_is_word(p, 1)) {
  68. if (BN_abs_is_word(p, 2)) {
  69. if (ret == NULL) {
  70. ret = BN_new();
  71. }
  72. if (ret == NULL) {
  73. goto end;
  74. }
  75. if (!BN_set_word(ret, BN_is_bit_set(a, 0))) {
  76. if (ret != in) {
  77. BN_free(ret);
  78. }
  79. return NULL;
  80. }
  81. return ret;
  82. }
  83. OPENSSL_PUT_ERROR(BN, BN_mod_sqrt, BN_R_P_IS_NOT_PRIME);
  84. return (NULL);
  85. }
  86. if (BN_is_zero(a) || BN_is_one(a)) {
  87. if (ret == NULL) {
  88. ret = BN_new();
  89. }
  90. if (ret == NULL) {
  91. goto end;
  92. }
  93. if (!BN_set_word(ret, BN_is_one(a))) {
  94. if (ret != in) {
  95. BN_free(ret);
  96. }
  97. return NULL;
  98. }
  99. return ret;
  100. }
  101. BN_CTX_start(ctx);
  102. A = BN_CTX_get(ctx);
  103. b = BN_CTX_get(ctx);
  104. q = BN_CTX_get(ctx);
  105. t = BN_CTX_get(ctx);
  106. x = BN_CTX_get(ctx);
  107. y = BN_CTX_get(ctx);
  108. if (y == NULL) {
  109. goto end;
  110. }
  111. if (ret == NULL) {
  112. ret = BN_new();
  113. }
  114. if (ret == NULL) {
  115. goto end;
  116. }
  117. /* A = a mod p */
  118. if (!BN_nnmod(A, a, p, ctx)) {
  119. goto end;
  120. }
  121. /* now write |p| - 1 as 2^e*q where q is odd */
  122. e = 1;
  123. while (!BN_is_bit_set(p, e)) {
  124. e++;
  125. }
  126. /* we'll set q later (if needed) */
  127. if (e == 1) {
  128. /* The easy case: (|p|-1)/2 is odd, so 2 has an inverse
  129. * modulo (|p|-1)/2, and square roots can be computed
  130. * directly by modular exponentiation.
  131. * We have
  132. * 2 * (|p|+1)/4 == 1 (mod (|p|-1)/2),
  133. * so we can use exponent (|p|+1)/4, i.e. (|p|-3)/4 + 1.
  134. */
  135. if (!BN_rshift(q, p, 2)) {
  136. goto end;
  137. }
  138. q->neg = 0;
  139. if (!BN_add_word(q, 1) ||
  140. !BN_mod_exp(ret, A, q, p, ctx)) {
  141. goto end;
  142. }
  143. err = 0;
  144. goto vrfy;
  145. }
  146. if (e == 2) {
  147. /* |p| == 5 (mod 8)
  148. *
  149. * In this case 2 is always a non-square since
  150. * Legendre(2,p) = (-1)^((p^2-1)/8) for any odd prime.
  151. * So if a really is a square, then 2*a is a non-square.
  152. * Thus for
  153. * b := (2*a)^((|p|-5)/8),
  154. * i := (2*a)*b^2
  155. * we have
  156. * i^2 = (2*a)^((1 + (|p|-5)/4)*2)
  157. * = (2*a)^((p-1)/2)
  158. * = -1;
  159. * so if we set
  160. * x := a*b*(i-1),
  161. * then
  162. * x^2 = a^2 * b^2 * (i^2 - 2*i + 1)
  163. * = a^2 * b^2 * (-2*i)
  164. * = a*(-i)*(2*a*b^2)
  165. * = a*(-i)*i
  166. * = a.
  167. *
  168. * (This is due to A.O.L. Atkin,
  169. * <URL:
  170. *http://listserv.nodak.edu/scripts/wa.exe?A2=ind9211&L=nmbrthry&O=T&P=562>,
  171. * November 1992.)
  172. */
  173. /* t := 2*a */
  174. if (!BN_mod_lshift1_quick(t, A, p)) {
  175. goto end;
  176. }
  177. /* b := (2*a)^((|p|-5)/8) */
  178. if (!BN_rshift(q, p, 3)) {
  179. goto end;
  180. }
  181. q->neg = 0;
  182. if (!BN_mod_exp(b, t, q, p, ctx)) {
  183. goto end;
  184. }
  185. /* y := b^2 */
  186. if (!BN_mod_sqr(y, b, p, ctx)) {
  187. goto end;
  188. }
  189. /* t := (2*a)*b^2 - 1*/
  190. if (!BN_mod_mul(t, t, y, p, ctx) ||
  191. !BN_sub_word(t, 1)) {
  192. goto end;
  193. }
  194. /* x = a*b*t */
  195. if (!BN_mod_mul(x, A, b, p, ctx) ||
  196. !BN_mod_mul(x, x, t, p, ctx)) {
  197. goto end;
  198. }
  199. if (!BN_copy(ret, x)) {
  200. goto end;
  201. }
  202. err = 0;
  203. goto vrfy;
  204. }
  205. /* e > 2, so we really have to use the Tonelli/Shanks algorithm.
  206. * First, find some y that is not a square. */
  207. if (!BN_copy(q, p)) {
  208. goto end; /* use 'q' as temp */
  209. }
  210. q->neg = 0;
  211. i = 2;
  212. do {
  213. /* For efficiency, try small numbers first;
  214. * if this fails, try random numbers.
  215. */
  216. if (i < 22) {
  217. if (!BN_set_word(y, i)) {
  218. goto end;
  219. }
  220. } else {
  221. if (!BN_pseudo_rand(y, BN_num_bits(p), 0, 0)) {
  222. goto end;
  223. }
  224. if (BN_ucmp(y, p) >= 0) {
  225. if (!(p->neg ? BN_add : BN_sub)(y, y, p)) {
  226. goto end;
  227. }
  228. }
  229. /* now 0 <= y < |p| */
  230. if (BN_is_zero(y)) {
  231. if (!BN_set_word(y, i)) {
  232. goto end;
  233. }
  234. }
  235. }
  236. r = BN_kronecker(y, q, ctx); /* here 'q' is |p| */
  237. if (r < -1) {
  238. goto end;
  239. }
  240. if (r == 0) {
  241. /* m divides p */
  242. OPENSSL_PUT_ERROR(BN, BN_mod_sqrt, BN_R_P_IS_NOT_PRIME);
  243. goto end;
  244. }
  245. } while (r == 1 && ++i < 82);
  246. if (r != -1) {
  247. /* Many rounds and still no non-square -- this is more likely
  248. * a bug than just bad luck.
  249. * Even if p is not prime, we should have found some y
  250. * such that r == -1.
  251. */
  252. OPENSSL_PUT_ERROR(BN, BN_mod_sqrt, BN_R_TOO_MANY_ITERATIONS);
  253. goto end;
  254. }
  255. /* Here's our actual 'q': */
  256. if (!BN_rshift(q, q, e)) {
  257. goto end;
  258. }
  259. /* Now that we have some non-square, we can find an element
  260. * of order 2^e by computing its q'th power. */
  261. if (!BN_mod_exp(y, y, q, p, ctx)) {
  262. goto end;
  263. }
  264. if (BN_is_one(y)) {
  265. OPENSSL_PUT_ERROR(BN, BN_mod_sqrt, BN_R_P_IS_NOT_PRIME);
  266. goto end;
  267. }
  268. /* Now we know that (if p is indeed prime) there is an integer
  269. * k, 0 <= k < 2^e, such that
  270. *
  271. * a^q * y^k == 1 (mod p).
  272. *
  273. * As a^q is a square and y is not, k must be even.
  274. * q+1 is even, too, so there is an element
  275. *
  276. * X := a^((q+1)/2) * y^(k/2),
  277. *
  278. * and it satisfies
  279. *
  280. * X^2 = a^q * a * y^k
  281. * = a,
  282. *
  283. * so it is the square root that we are looking for.
  284. */
  285. /* t := (q-1)/2 (note that q is odd) */
  286. if (!BN_rshift1(t, q)) {
  287. goto end;
  288. }
  289. /* x := a^((q-1)/2) */
  290. if (BN_is_zero(t)) /* special case: p = 2^e + 1 */
  291. {
  292. if (!BN_nnmod(t, A, p, ctx)) {
  293. goto end;
  294. }
  295. if (BN_is_zero(t)) {
  296. /* special case: a == 0 (mod p) */
  297. BN_zero(ret);
  298. err = 0;
  299. goto end;
  300. } else if (!BN_one(x)) {
  301. goto end;
  302. }
  303. } else {
  304. if (!BN_mod_exp(x, A, t, p, ctx)) {
  305. goto end;
  306. }
  307. if (BN_is_zero(x)) {
  308. /* special case: a == 0 (mod p) */
  309. BN_zero(ret);
  310. err = 0;
  311. goto end;
  312. }
  313. }
  314. /* b := a*x^2 (= a^q) */
  315. if (!BN_mod_sqr(b, x, p, ctx) ||
  316. !BN_mod_mul(b, b, A, p, ctx)) {
  317. goto end;
  318. }
  319. /* x := a*x (= a^((q+1)/2)) */
  320. if (!BN_mod_mul(x, x, A, p, ctx)) {
  321. goto end;
  322. }
  323. while (1) {
  324. /* Now b is a^q * y^k for some even k (0 <= k < 2^E
  325. * where E refers to the original value of e, which we
  326. * don't keep in a variable), and x is a^((q+1)/2) * y^(k/2).
  327. *
  328. * We have a*b = x^2,
  329. * y^2^(e-1) = -1,
  330. * b^2^(e-1) = 1.
  331. */
  332. if (BN_is_one(b)) {
  333. if (!BN_copy(ret, x)) {
  334. goto end;
  335. }
  336. err = 0;
  337. goto vrfy;
  338. }
  339. /* find smallest i such that b^(2^i) = 1 */
  340. i = 1;
  341. if (!BN_mod_sqr(t, b, p, ctx)) {
  342. goto end;
  343. }
  344. while (!BN_is_one(t)) {
  345. i++;
  346. if (i == e) {
  347. OPENSSL_PUT_ERROR(BN, BN_mod_sqrt, BN_R_NOT_A_SQUARE);
  348. goto end;
  349. }
  350. if (!BN_mod_mul(t, t, t, p, ctx)) {
  351. goto end;
  352. }
  353. }
  354. /* t := y^2^(e - i - 1) */
  355. if (!BN_copy(t, y)) {
  356. goto end;
  357. }
  358. for (j = e - i - 1; j > 0; j--) {
  359. if (!BN_mod_sqr(t, t, p, ctx)) {
  360. goto end;
  361. }
  362. }
  363. if (!BN_mod_mul(y, t, t, p, ctx) ||
  364. !BN_mod_mul(x, x, t, p, ctx) ||
  365. !BN_mod_mul(b, b, y, p, ctx)) {
  366. goto end;
  367. }
  368. e = i;
  369. }
  370. vrfy:
  371. if (!err) {
  372. /* verify the result -- the input might have been not a square
  373. * (test added in 0.9.8) */
  374. if (!BN_mod_sqr(x, ret, p, ctx)) {
  375. err = 1;
  376. }
  377. if (!err && 0 != BN_cmp(x, A)) {
  378. OPENSSL_PUT_ERROR(BN, BN_mod_sqrt, BN_R_NOT_A_SQUARE);
  379. err = 1;
  380. }
  381. }
  382. end:
  383. if (err) {
  384. if (ret != NULL && ret != in) {
  385. BN_clear_free(ret);
  386. }
  387. ret = NULL;
  388. }
  389. BN_CTX_end(ctx);
  390. return ret;
  391. }
  392. int BN_sqrt(BIGNUM *out_sqrt, const BIGNUM *in, BN_CTX *ctx) {
  393. BIGNUM *estimate, *tmp, *delta, *last_delta, *tmp2;
  394. int ok = 0, last_delta_valid = 0;
  395. if (in->neg) {
  396. OPENSSL_PUT_ERROR(BN, BN_sqrt, BN_R_NEGATIVE_NUMBER);
  397. return 0;
  398. }
  399. if (BN_is_zero(in)) {
  400. BN_zero(out_sqrt);
  401. return 1;
  402. }
  403. BN_CTX_start(ctx);
  404. if (out_sqrt == in) {
  405. estimate = BN_CTX_get(ctx);
  406. } else {
  407. estimate = out_sqrt;
  408. }
  409. tmp = BN_CTX_get(ctx);
  410. last_delta = BN_CTX_get(ctx);
  411. delta = BN_CTX_get(ctx);
  412. if (estimate == NULL || tmp == NULL || last_delta == NULL || delta == NULL) {
  413. OPENSSL_PUT_ERROR(BN, BN_sqrt, ERR_R_MALLOC_FAILURE);
  414. goto err;
  415. }
  416. /* We estimate that the square root of an n-bit number is 2^{n/2}. */
  417. BN_lshift(estimate, BN_value_one(), BN_num_bits(in)/2);
  418. /* This is Newton's method for finding a root of the equation |estimate|^2 -
  419. * |in| = 0. */
  420. for (;;) {
  421. /* |estimate| = 1/2 * (|estimate| + |in|/|estimate|) */
  422. if (!BN_div(tmp, NULL, in, estimate, ctx) ||
  423. !BN_add(tmp, tmp, estimate) ||
  424. !BN_rshift1(estimate, tmp) ||
  425. /* |tmp| = |estimate|^2 */
  426. !BN_sqr(tmp, estimate, ctx) ||
  427. /* |delta| = |in| - |tmp| */
  428. !BN_sub(delta, in, tmp)) {
  429. OPENSSL_PUT_ERROR(BN, BN_sqrt, ERR_R_BN_LIB);
  430. goto err;
  431. }
  432. delta->neg = 0;
  433. /* The difference between |in| and |estimate| squared is required to always
  434. * decrease. This ensures that the loop always terminates, but I don't have
  435. * a proof that it always finds the square root for a given square. */
  436. if (last_delta_valid && BN_cmp(delta, last_delta) >= 0) {
  437. break;
  438. }
  439. last_delta_valid = 1;
  440. tmp2 = last_delta;
  441. last_delta = delta;
  442. delta = tmp2;
  443. }
  444. if (BN_cmp(tmp, in) != 0) {
  445. OPENSSL_PUT_ERROR(BN, BN_sqrt, BN_R_NOT_A_SQUARE);
  446. goto err;
  447. }
  448. ok = 1;
  449. err:
  450. if (ok && out_sqrt == in) {
  451. BN_copy(out_sqrt, estimate);
  452. }
  453. BN_CTX_end(ctx);
  454. return ok;
  455. }