bn_mont.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright 1995-2018 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 < 0)
  86. return 0;
  87. if (i)
  88. memset(&rp[r->top], 0, sizeof(*rp) * i);
  89. r->top = max;
  90. n0 = mont->n0[0];
  91. /*
  92. * Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On
  93. * input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r|
  94. * includes |carry| which is stored separately.
  95. */
  96. for (carry = 0, i = 0; i < nl; i++, rp++) {
  97. v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
  98. v = (v + carry + rp[nl]) & BN_MASK2;
  99. carry |= (v != rp[nl]);
  100. carry &= (v <= rp[nl]);
  101. rp[nl] = v;
  102. }
  103. if (bn_wexpand(ret, nl) == NULL)
  104. return 0;
  105. ret->top = nl;
  106. ret->neg = r->neg;
  107. rp = ret->d;
  108. /*
  109. * Shift |nl| words to divide by R. We have |ap| < 2 * |n|. Note that |ap|
  110. * includes |carry| which is stored separately.
  111. */
  112. ap = &(r->d[nl]);
  113. /*
  114. * |v| is one if |ap| - |np| underflowed or zero if it did not. Note |v|
  115. * cannot be -1. That would imply the subtraction did not fit in |nl| words,
  116. * and we know at most one subtraction is needed.
  117. */
  118. v = bn_sub_words(rp, ap, np, nl) - carry;
  119. v = 0 - v;
  120. for (i = 0; i < nl; i++) {
  121. rp[i] = (v & ap[i]) | (~v & rp[i]);
  122. ap[i] = 0;
  123. }
  124. bn_correct_top(r);
  125. bn_correct_top(ret);
  126. bn_check_top(ret);
  127. return 1;
  128. }
  129. #endif /* MONT_WORD */
  130. int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
  131. BN_CTX *ctx)
  132. {
  133. int retn = 0;
  134. #ifdef MONT_WORD
  135. BIGNUM *t;
  136. BN_CTX_start(ctx);
  137. if ((t = BN_CTX_get(ctx)) && BN_copy(t, a))
  138. retn = BN_from_montgomery_word(ret, t, mont);
  139. BN_CTX_end(ctx);
  140. #else /* !MONT_WORD */
  141. BIGNUM *t1, *t2;
  142. BN_CTX_start(ctx);
  143. t1 = BN_CTX_get(ctx);
  144. t2 = BN_CTX_get(ctx);
  145. if (t2 == NULL)
  146. goto err;
  147. if (!BN_copy(t1, a))
  148. goto err;
  149. BN_mask_bits(t1, mont->ri);
  150. if (!BN_mul(t2, t1, &mont->Ni, ctx))
  151. goto err;
  152. BN_mask_bits(t2, mont->ri);
  153. if (!BN_mul(t1, t2, &mont->N, ctx))
  154. goto err;
  155. if (!BN_add(t2, a, t1))
  156. goto err;
  157. if (!BN_rshift(ret, t2, mont->ri))
  158. goto err;
  159. if (BN_ucmp(ret, &(mont->N)) >= 0) {
  160. if (!BN_usub(ret, ret, &(mont->N)))
  161. goto err;
  162. }
  163. retn = 1;
  164. bn_check_top(ret);
  165. err:
  166. BN_CTX_end(ctx);
  167. #endif /* MONT_WORD */
  168. return retn;
  169. }
  170. BN_MONT_CTX *BN_MONT_CTX_new(void)
  171. {
  172. BN_MONT_CTX *ret;
  173. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
  174. return NULL;
  175. BN_MONT_CTX_init(ret);
  176. ret->flags = BN_FLG_MALLOCED;
  177. return ret;
  178. }
  179. void BN_MONT_CTX_init(BN_MONT_CTX *ctx)
  180. {
  181. ctx->ri = 0;
  182. bn_init(&ctx->RR);
  183. bn_init(&ctx->N);
  184. bn_init(&ctx->Ni);
  185. ctx->n0[0] = ctx->n0[1] = 0;
  186. ctx->flags = 0;
  187. }
  188. void BN_MONT_CTX_free(BN_MONT_CTX *mont)
  189. {
  190. if (mont == NULL)
  191. return;
  192. BN_clear_free(&mont->RR);
  193. BN_clear_free(&mont->N);
  194. BN_clear_free(&mont->Ni);
  195. if (mont->flags & BN_FLG_MALLOCED)
  196. OPENSSL_free(mont);
  197. }
  198. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
  199. {
  200. int ret = 0;
  201. BIGNUM *Ri, *R;
  202. if (BN_is_zero(mod))
  203. return 0;
  204. BN_CTX_start(ctx);
  205. if ((Ri = BN_CTX_get(ctx)) == NULL)
  206. goto err;
  207. R = &(mont->RR); /* grab RR as a temp */
  208. if (!BN_copy(&(mont->N), mod))
  209. goto err; /* Set N */
  210. if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
  211. BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);
  212. mont->N.neg = 0;
  213. #ifdef MONT_WORD
  214. {
  215. BIGNUM tmod;
  216. BN_ULONG buf[2];
  217. bn_init(&tmod);
  218. tmod.d = buf;
  219. tmod.dmax = 2;
  220. tmod.neg = 0;
  221. if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
  222. BN_set_flags(&tmod, BN_FLG_CONSTTIME);
  223. mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
  224. # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)
  225. /*
  226. * Only certain BN_BITS2<=32 platforms actually make use of n0[1],
  227. * and we could use the #else case (with a shorter R value) for the
  228. * others. However, currently only the assembler files do know which
  229. * is which.
  230. */
  231. BN_zero(R);
  232. if (!(BN_set_bit(R, 2 * BN_BITS2)))
  233. goto err;
  234. tmod.top = 0;
  235. if ((buf[0] = mod->d[0]))
  236. tmod.top = 1;
  237. if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))
  238. tmod.top = 2;
  239. if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  240. goto err;
  241. if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))
  242. goto err; /* R*Ri */
  243. if (!BN_is_zero(Ri)) {
  244. if (!BN_sub_word(Ri, 1))
  245. goto err;
  246. } else { /* if N mod word size == 1 */
  247. if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)
  248. goto err;
  249. /* Ri-- (mod double word size) */
  250. Ri->neg = 0;
  251. Ri->d[0] = BN_MASK2;
  252. Ri->d[1] = BN_MASK2;
  253. Ri->top = 2;
  254. }
  255. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  256. goto err;
  257. /*
  258. * Ni = (R*Ri-1)/N, keep only couple of least significant words:
  259. */
  260. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  261. mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
  262. # else
  263. BN_zero(R);
  264. if (!(BN_set_bit(R, BN_BITS2)))
  265. goto err; /* R */
  266. buf[0] = mod->d[0]; /* tmod = N mod word size */
  267. buf[1] = 0;
  268. tmod.top = buf[0] != 0 ? 1 : 0;
  269. /* Ri = R^-1 mod N */
  270. if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  271. goto err;
  272. if (!BN_lshift(Ri, Ri, BN_BITS2))
  273. goto err; /* R*Ri */
  274. if (!BN_is_zero(Ri)) {
  275. if (!BN_sub_word(Ri, 1))
  276. goto err;
  277. } else { /* if N mod word size == 1 */
  278. if (!BN_set_word(Ri, BN_MASK2))
  279. goto err; /* Ri-- (mod word size) */
  280. }
  281. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  282. goto err;
  283. /*
  284. * Ni = (R*Ri-1)/N, keep only least significant word:
  285. */
  286. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  287. mont->n0[1] = 0;
  288. # endif
  289. }
  290. #else /* !MONT_WORD */
  291. { /* bignum version */
  292. mont->ri = BN_num_bits(&mont->N);
  293. BN_zero(R);
  294. if (!BN_set_bit(R, mont->ri))
  295. goto err; /* R = 2^ri */
  296. /* Ri = R^-1 mod N */
  297. if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)
  298. goto err;
  299. if (!BN_lshift(Ri, Ri, mont->ri))
  300. goto err; /* R*Ri */
  301. if (!BN_sub_word(Ri, 1))
  302. goto err;
  303. /*
  304. * Ni = (R*Ri-1) / N
  305. */
  306. if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))
  307. goto err;
  308. }
  309. #endif
  310. /* setup RR for conversions */
  311. BN_zero(&(mont->RR));
  312. if (!BN_set_bit(&(mont->RR), mont->ri * 2))
  313. goto err;
  314. if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))
  315. goto err;
  316. ret = 1;
  317. err:
  318. BN_CTX_end(ctx);
  319. return ret;
  320. }
  321. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)
  322. {
  323. if (to == from)
  324. return to;
  325. if (!BN_copy(&(to->RR), &(from->RR)))
  326. return NULL;
  327. if (!BN_copy(&(to->N), &(from->N)))
  328. return NULL;
  329. if (!BN_copy(&(to->Ni), &(from->Ni)))
  330. return NULL;
  331. to->ri = from->ri;
  332. to->n0[0] = from->n0[0];
  333. to->n0[1] = from->n0[1];
  334. return to;
  335. }
  336. BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,
  337. const BIGNUM *mod, BN_CTX *ctx)
  338. {
  339. BN_MONT_CTX *ret;
  340. CRYPTO_THREAD_read_lock(lock);
  341. ret = *pmont;
  342. CRYPTO_THREAD_unlock(lock);
  343. if (ret)
  344. return ret;
  345. /*
  346. * We don't want to serialise globally while doing our lazy-init math in
  347. * BN_MONT_CTX_set. That punishes threads that are doing independent
  348. * things. Instead, punish the case where more than one thread tries to
  349. * lazy-init the same 'pmont', by having each do the lazy-init math work
  350. * independently and only use the one from the thread that wins the race
  351. * (the losers throw away the work they've done).
  352. */
  353. ret = BN_MONT_CTX_new();
  354. if (ret == NULL)
  355. return NULL;
  356. if (!BN_MONT_CTX_set(ret, mod, ctx)) {
  357. BN_MONT_CTX_free(ret);
  358. return NULL;
  359. }
  360. /* The locked compare-and-set, after the local work is done. */
  361. CRYPTO_THREAD_write_lock(lock);
  362. if (*pmont) {
  363. BN_MONT_CTX_free(ret);
  364. ret = *pmont;
  365. } else
  366. *pmont = ret;
  367. CRYPTO_THREAD_unlock(lock);
  368. return ret;
  369. }