bn_mont.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright 1995-2016 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. /*
  10. * Details about Montgomery multiplication algorithms can be found at
  11. * http://security.ece.orst.edu/publications.html, e.g.
  12. * http://security.ece.orst.edu/koc/papers/j37acmon.pdf and
  13. * sections 3.8 and 4.2 in http://security.ece.orst.edu/koc/papers/r01rsasw.pdf
  14. */
  15. #include "internal/cryptlib.h"
  16. #include "bn_lcl.h"
  17. #define MONT_WORD /* use the faster word-based algorithm */
  18. #ifdef MONT_WORD
  19. static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont);
  20. #endif
  21. int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  22. BN_MONT_CTX *mont, BN_CTX *ctx)
  23. {
  24. BIGNUM *tmp;
  25. int ret = 0;
  26. #if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)
  27. int num = mont->N.top;
  28. if (num > 1 && a->top == num && b->top == num) {
  29. if (bn_wexpand(r, num) == NULL)
  30. return (0);
  31. if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
  32. r->neg = a->neg ^ b->neg;
  33. r->top = num;
  34. bn_correct_top(r);
  35. return (1);
  36. }
  37. }
  38. #endif
  39. BN_CTX_start(ctx);
  40. tmp = BN_CTX_get(ctx);
  41. if (tmp == NULL)
  42. goto err;
  43. bn_check_top(tmp);
  44. if (a == b) {
  45. if (!BN_sqr(tmp, a, ctx))
  46. goto err;
  47. } else {
  48. if (!BN_mul(tmp, a, b, ctx))
  49. goto err;
  50. }
  51. /* reduce from aRR to aR */
  52. #ifdef MONT_WORD
  53. if (!BN_from_montgomery_word(r, tmp, mont))
  54. goto err;
  55. #else
  56. if (!BN_from_montgomery(r, tmp, mont, ctx))
  57. goto err;
  58. #endif
  59. bn_check_top(r);
  60. ret = 1;
  61. err:
  62. BN_CTX_end(ctx);
  63. return (ret);
  64. }
  65. #ifdef MONT_WORD
  66. static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
  67. {
  68. BIGNUM *n;
  69. BN_ULONG *ap, *np, *rp, n0, v, carry;
  70. int nl, max, i;
  71. n = &(mont->N);
  72. nl = n->top;
  73. if (nl == 0) {
  74. ret->top = 0;
  75. return (1);
  76. }
  77. max = (2 * nl); /* carry is stored separately */
  78. if (bn_wexpand(r, max) == NULL)
  79. return (0);
  80. r->neg ^= n->neg;
  81. np = n->d;
  82. rp = r->d;
  83. /* clear the top words of T */
  84. i = max - r->top;
  85. if (i)
  86. memset(&rp[r->top], 0, sizeof(*rp) * i);
  87. r->top = max;
  88. n0 = mont->n0[0];
  89. for (carry = 0, i = 0; i < nl; i++, rp++) {
  90. v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
  91. v = (v + carry + rp[nl]) & BN_MASK2;
  92. carry |= (v != rp[nl]);
  93. carry &= (v <= rp[nl]);
  94. rp[nl] = v;
  95. }
  96. if (bn_wexpand(ret, nl) == NULL)
  97. return (0);
  98. ret->top = nl;
  99. ret->neg = r->neg;
  100. rp = ret->d;
  101. ap = &(r->d[nl]);
  102. # define BRANCH_FREE 1
  103. # if BRANCH_FREE
  104. {
  105. BN_ULONG *nrp;
  106. size_t m;
  107. v = bn_sub_words(rp, ap, np, nl) - carry;
  108. /*
  109. * if subtraction result is real, then trick unconditional memcpy
  110. * below to perform in-place "refresh" instead of actual copy.
  111. */
  112. m = (0 - (size_t)v);
  113. nrp =
  114. (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));
  115. for (i = 0, nl -= 4; i < nl; i += 4) {
  116. BN_ULONG t1, t2, t3, t4;
  117. t1 = nrp[i + 0];
  118. t2 = nrp[i + 1];
  119. t3 = nrp[i + 2];
  120. ap[i + 0] = 0;
  121. t4 = nrp[i + 3];
  122. ap[i + 1] = 0;
  123. rp[i + 0] = t1;
  124. ap[i + 2] = 0;
  125. rp[i + 1] = t2;
  126. ap[i + 3] = 0;
  127. rp[i + 2] = t3;
  128. rp[i + 3] = t4;
  129. }
  130. for (nl += 4; i < nl; i++)
  131. rp[i] = nrp[i], ap[i] = 0;
  132. }
  133. # else
  134. if (bn_sub_words(rp, ap, np, nl) - carry)
  135. memcpy(rp, ap, nl * sizeof(BN_ULONG));
  136. # endif
  137. bn_correct_top(r);
  138. bn_correct_top(ret);
  139. bn_check_top(ret);
  140. return (1);
  141. }
  142. #endif /* MONT_WORD */
  143. int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
  144. BN_CTX *ctx)
  145. {
  146. int retn = 0;
  147. #ifdef MONT_WORD
  148. BIGNUM *t;
  149. BN_CTX_start(ctx);
  150. if ((t = BN_CTX_get(ctx)) && BN_copy(t, a))
  151. retn = BN_from_montgomery_word(ret, t, mont);
  152. BN_CTX_end(ctx);
  153. #else /* !MONT_WORD */
  154. BIGNUM *t1, *t2;
  155. BN_CTX_start(ctx);
  156. t1 = BN_CTX_get(ctx);
  157. t2 = BN_CTX_get(ctx);
  158. if (t1 == NULL || t2 == NULL)
  159. goto err;
  160. if (!BN_copy(t1, a))
  161. goto err;
  162. BN_mask_bits(t1, mont->ri);
  163. if (!BN_mul(t2, t1, &mont->Ni, ctx))
  164. goto err;
  165. BN_mask_bits(t2, mont->ri);
  166. if (!BN_mul(t1, t2, &mont->N, ctx))
  167. goto err;
  168. if (!BN_add(t2, a, t1))
  169. goto err;
  170. if (!BN_rshift(ret, t2, mont->ri))
  171. goto err;
  172. if (BN_ucmp(ret, &(mont->N)) >= 0) {
  173. if (!BN_usub(ret, ret, &(mont->N)))
  174. goto err;
  175. }
  176. retn = 1;
  177. bn_check_top(ret);
  178. err:
  179. BN_CTX_end(ctx);
  180. #endif /* MONT_WORD */
  181. return (retn);
  182. }
  183. BN_MONT_CTX *BN_MONT_CTX_new(void)
  184. {
  185. BN_MONT_CTX *ret;
  186. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
  187. return (NULL);
  188. BN_MONT_CTX_init(ret);
  189. ret->flags = BN_FLG_MALLOCED;
  190. return (ret);
  191. }
  192. void BN_MONT_CTX_init(BN_MONT_CTX *ctx)
  193. {
  194. ctx->ri = 0;
  195. bn_init(&(ctx->RR));
  196. bn_init(&(ctx->N));
  197. bn_init(&(ctx->Ni));
  198. ctx->n0[0] = ctx->n0[1] = 0;
  199. ctx->flags = 0;
  200. }
  201. void BN_MONT_CTX_free(BN_MONT_CTX *mont)
  202. {
  203. if (mont == NULL)
  204. return;
  205. BN_clear_free(&(mont->RR));
  206. BN_clear_free(&(mont->N));
  207. BN_clear_free(&(mont->Ni));
  208. if (mont->flags & BN_FLG_MALLOCED)
  209. OPENSSL_free(mont);
  210. }
  211. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
  212. {
  213. int ret = 0;
  214. BIGNUM *Ri, *R;
  215. if (BN_is_zero(mod))
  216. return 0;
  217. BN_CTX_start(ctx);
  218. if ((Ri = BN_CTX_get(ctx)) == NULL)
  219. goto err;
  220. R = &(mont->RR); /* grab RR as a temp */
  221. if (!BN_copy(&(mont->N), mod))
  222. goto err; /* Set N */
  223. mont->N.neg = 0;
  224. #ifdef MONT_WORD
  225. {
  226. BIGNUM tmod;
  227. BN_ULONG buf[2];
  228. bn_init(&tmod);
  229. tmod.d = buf;
  230. tmod.dmax = 2;
  231. tmod.neg = 0;
  232. mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
  233. # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)
  234. /*
  235. * Only certain BN_BITS2<=32 platforms actually make use of n0[1],
  236. * and we could use the #else case (with a shorter R value) for the
  237. * others. However, currently only the assembler files do know which
  238. * is which.
  239. */
  240. BN_zero(R);
  241. if (!(BN_set_bit(R, 2 * BN_BITS2)))
  242. goto err;
  243. tmod.top = 0;
  244. if ((buf[0] = mod->d[0]))
  245. tmod.top = 1;
  246. if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))
  247. tmod.top = 2;
  248. if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  249. goto err;
  250. if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))
  251. goto err; /* R*Ri */
  252. if (!BN_is_zero(Ri)) {
  253. if (!BN_sub_word(Ri, 1))
  254. goto err;
  255. } else { /* if N mod word size == 1 */
  256. if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)
  257. goto err;
  258. /* Ri-- (mod double word size) */
  259. Ri->neg = 0;
  260. Ri->d[0] = BN_MASK2;
  261. Ri->d[1] = BN_MASK2;
  262. Ri->top = 2;
  263. }
  264. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  265. goto err;
  266. /*
  267. * Ni = (R*Ri-1)/N, keep only couple of least significant words:
  268. */
  269. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  270. mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
  271. # else
  272. BN_zero(R);
  273. if (!(BN_set_bit(R, BN_BITS2)))
  274. goto err; /* R */
  275. buf[0] = mod->d[0]; /* tmod = N mod word size */
  276. buf[1] = 0;
  277. tmod.top = buf[0] != 0 ? 1 : 0;
  278. /* Ri = R^-1 mod N */
  279. if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  280. goto err;
  281. if (!BN_lshift(Ri, Ri, BN_BITS2))
  282. goto err; /* R*Ri */
  283. if (!BN_is_zero(Ri)) {
  284. if (!BN_sub_word(Ri, 1))
  285. goto err;
  286. } else { /* if N mod word size == 1 */
  287. if (!BN_set_word(Ri, BN_MASK2))
  288. goto err; /* Ri-- (mod word size) */
  289. }
  290. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  291. goto err;
  292. /*
  293. * Ni = (R*Ri-1)/N, keep only least significant word:
  294. */
  295. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  296. mont->n0[1] = 0;
  297. # endif
  298. }
  299. #else /* !MONT_WORD */
  300. { /* bignum version */
  301. mont->ri = BN_num_bits(&mont->N);
  302. BN_zero(R);
  303. if (!BN_set_bit(R, mont->ri))
  304. goto err; /* R = 2^ri */
  305. /* Ri = R^-1 mod N */
  306. if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)
  307. goto err;
  308. if (!BN_lshift(Ri, Ri, mont->ri))
  309. goto err; /* R*Ri */
  310. if (!BN_sub_word(Ri, 1))
  311. goto err;
  312. /*
  313. * Ni = (R*Ri-1) / N
  314. */
  315. if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))
  316. goto err;
  317. }
  318. #endif
  319. /* setup RR for conversions */
  320. BN_zero(&(mont->RR));
  321. if (!BN_set_bit(&(mont->RR), mont->ri * 2))
  322. goto err;
  323. if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))
  324. goto err;
  325. ret = 1;
  326. err:
  327. BN_CTX_end(ctx);
  328. return ret;
  329. }
  330. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)
  331. {
  332. if (to == from)
  333. return (to);
  334. if (!BN_copy(&(to->RR), &(from->RR)))
  335. return NULL;
  336. if (!BN_copy(&(to->N), &(from->N)))
  337. return NULL;
  338. if (!BN_copy(&(to->Ni), &(from->Ni)))
  339. return NULL;
  340. to->ri = from->ri;
  341. to->n0[0] = from->n0[0];
  342. to->n0[1] = from->n0[1];
  343. return (to);
  344. }
  345. BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,
  346. const BIGNUM *mod, BN_CTX *ctx)
  347. {
  348. BN_MONT_CTX *ret;
  349. CRYPTO_THREAD_read_lock(lock);
  350. ret = *pmont;
  351. CRYPTO_THREAD_unlock(lock);
  352. if (ret)
  353. return ret;
  354. /*
  355. * We don't want to serialise globally while doing our lazy-init math in
  356. * BN_MONT_CTX_set. That punishes threads that are doing independent
  357. * things. Instead, punish the case where more than one thread tries to
  358. * lazy-init the same 'pmont', by having each do the lazy-init math work
  359. * independently and only use the one from the thread that wins the race
  360. * (the losers throw away the work they've done).
  361. */
  362. ret = BN_MONT_CTX_new();
  363. if (ret == NULL)
  364. return NULL;
  365. if (!BN_MONT_CTX_set(ret, mod, ctx)) {
  366. BN_MONT_CTX_free(ret);
  367. return NULL;
  368. }
  369. /* The locked compare-and-set, after the local work is done. */
  370. CRYPTO_THREAD_write_lock(lock);
  371. if (*pmont) {
  372. BN_MONT_CTX_free(ret);
  373. ret = *pmont;
  374. } else
  375. *pmont = ret;
  376. CRYPTO_THREAD_unlock(lock);
  377. return ret;
  378. }