bn_mont.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. BNerr(BN_F_BN_MONT_CTX_NEW, ERR_R_MALLOC_FAILURE);
  175. return NULL;
  176. }
  177. BN_MONT_CTX_init(ret);
  178. ret->flags = BN_FLG_MALLOCED;
  179. return ret;
  180. }
  181. void BN_MONT_CTX_init(BN_MONT_CTX *ctx)
  182. {
  183. ctx->ri = 0;
  184. bn_init(&ctx->RR);
  185. bn_init(&ctx->N);
  186. bn_init(&ctx->Ni);
  187. ctx->n0[0] = ctx->n0[1] = 0;
  188. ctx->flags = 0;
  189. }
  190. void BN_MONT_CTX_free(BN_MONT_CTX *mont)
  191. {
  192. if (mont == NULL)
  193. return;
  194. BN_clear_free(&mont->RR);
  195. BN_clear_free(&mont->N);
  196. BN_clear_free(&mont->Ni);
  197. if (mont->flags & BN_FLG_MALLOCED)
  198. OPENSSL_free(mont);
  199. }
  200. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
  201. {
  202. int ret = 0;
  203. BIGNUM *Ri, *R;
  204. if (BN_is_zero(mod))
  205. return 0;
  206. BN_CTX_start(ctx);
  207. if ((Ri = BN_CTX_get(ctx)) == NULL)
  208. goto err;
  209. R = &(mont->RR); /* grab RR as a temp */
  210. if (!BN_copy(&(mont->N), mod))
  211. goto err; /* Set N */
  212. if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
  213. BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);
  214. mont->N.neg = 0;
  215. #ifdef MONT_WORD
  216. {
  217. BIGNUM tmod;
  218. BN_ULONG buf[2];
  219. bn_init(&tmod);
  220. tmod.d = buf;
  221. tmod.dmax = 2;
  222. tmod.neg = 0;
  223. if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
  224. BN_set_flags(&tmod, BN_FLG_CONSTTIME);
  225. mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
  226. # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)
  227. /*
  228. * Only certain BN_BITS2<=32 platforms actually make use of n0[1],
  229. * and we could use the #else case (with a shorter R value) for the
  230. * others. However, currently only the assembler files do know which
  231. * is which.
  232. */
  233. BN_zero(R);
  234. if (!(BN_set_bit(R, 2 * BN_BITS2)))
  235. goto err;
  236. tmod.top = 0;
  237. if ((buf[0] = mod->d[0]))
  238. tmod.top = 1;
  239. if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))
  240. tmod.top = 2;
  241. if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  242. goto err;
  243. if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))
  244. goto err; /* R*Ri */
  245. if (!BN_is_zero(Ri)) {
  246. if (!BN_sub_word(Ri, 1))
  247. goto err;
  248. } else { /* if N mod word size == 1 */
  249. if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)
  250. goto err;
  251. /* Ri-- (mod double word size) */
  252. Ri->neg = 0;
  253. Ri->d[0] = BN_MASK2;
  254. Ri->d[1] = BN_MASK2;
  255. Ri->top = 2;
  256. }
  257. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  258. goto err;
  259. /*
  260. * Ni = (R*Ri-1)/N, keep only couple of least significant words:
  261. */
  262. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  263. mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
  264. # else
  265. BN_zero(R);
  266. if (!(BN_set_bit(R, BN_BITS2)))
  267. goto err; /* R */
  268. buf[0] = mod->d[0]; /* tmod = N mod word size */
  269. buf[1] = 0;
  270. tmod.top = buf[0] != 0 ? 1 : 0;
  271. /* Ri = R^-1 mod N */
  272. if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  273. goto err;
  274. if (!BN_lshift(Ri, Ri, BN_BITS2))
  275. goto err; /* R*Ri */
  276. if (!BN_is_zero(Ri)) {
  277. if (!BN_sub_word(Ri, 1))
  278. goto err;
  279. } else { /* if N mod word size == 1 */
  280. if (!BN_set_word(Ri, BN_MASK2))
  281. goto err; /* Ri-- (mod word size) */
  282. }
  283. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  284. goto err;
  285. /*
  286. * Ni = (R*Ri-1)/N, keep only least significant word:
  287. */
  288. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  289. mont->n0[1] = 0;
  290. # endif
  291. }
  292. #else /* !MONT_WORD */
  293. { /* bignum version */
  294. mont->ri = BN_num_bits(&mont->N);
  295. BN_zero(R);
  296. if (!BN_set_bit(R, mont->ri))
  297. goto err; /* R = 2^ri */
  298. /* Ri = R^-1 mod N */
  299. if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)
  300. goto err;
  301. if (!BN_lshift(Ri, Ri, mont->ri))
  302. goto err; /* R*Ri */
  303. if (!BN_sub_word(Ri, 1))
  304. goto err;
  305. /*
  306. * Ni = (R*Ri-1) / N
  307. */
  308. if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))
  309. goto err;
  310. }
  311. #endif
  312. /* setup RR for conversions */
  313. BN_zero(&(mont->RR));
  314. if (!BN_set_bit(&(mont->RR), mont->ri * 2))
  315. goto err;
  316. if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))
  317. goto err;
  318. ret = 1;
  319. err:
  320. BN_CTX_end(ctx);
  321. return ret;
  322. }
  323. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)
  324. {
  325. if (to == from)
  326. return to;
  327. if (!BN_copy(&(to->RR), &(from->RR)))
  328. return NULL;
  329. if (!BN_copy(&(to->N), &(from->N)))
  330. return NULL;
  331. if (!BN_copy(&(to->Ni), &(from->Ni)))
  332. return NULL;
  333. to->ri = from->ri;
  334. to->n0[0] = from->n0[0];
  335. to->n0[1] = from->n0[1];
  336. return to;
  337. }
  338. BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,
  339. const BIGNUM *mod, BN_CTX *ctx)
  340. {
  341. BN_MONT_CTX *ret;
  342. CRYPTO_THREAD_read_lock(lock);
  343. ret = *pmont;
  344. CRYPTO_THREAD_unlock(lock);
  345. if (ret)
  346. return ret;
  347. /*
  348. * We don't want to serialise globally while doing our lazy-init math in
  349. * BN_MONT_CTX_set. That punishes threads that are doing independent
  350. * things. Instead, punish the case where more than one thread tries to
  351. * lazy-init the same 'pmont', by having each do the lazy-init math work
  352. * independently and only use the one from the thread that wins the race
  353. * (the losers throw away the work they've done).
  354. */
  355. ret = BN_MONT_CTX_new();
  356. if (ret == NULL)
  357. return NULL;
  358. if (!BN_MONT_CTX_set(ret, mod, ctx)) {
  359. BN_MONT_CTX_free(ret);
  360. return NULL;
  361. }
  362. /* The locked compare-and-set, after the local work is done. */
  363. CRYPTO_THREAD_write_lock(lock);
  364. if (*pmont) {
  365. BN_MONT_CTX_free(ret);
  366. ret = *pmont;
  367. } else
  368. *pmont = ret;
  369. CRYPTO_THREAD_unlock(lock);
  370. return ret;
  371. }