2
0

bn_mont.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_is_one(&tmod))
  242. BN_zero(Ri);
  243. else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  244. goto err;
  245. if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))
  246. goto err; /* R*Ri */
  247. if (!BN_is_zero(Ri)) {
  248. if (!BN_sub_word(Ri, 1))
  249. goto err;
  250. } else { /* if N mod word size == 1 */
  251. if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)
  252. goto err;
  253. /* Ri-- (mod double word size) */
  254. Ri->neg = 0;
  255. Ri->d[0] = BN_MASK2;
  256. Ri->d[1] = BN_MASK2;
  257. Ri->top = 2;
  258. }
  259. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  260. goto err;
  261. /*
  262. * Ni = (R*Ri-1)/N, keep only couple of least significant words:
  263. */
  264. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  265. mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
  266. # else
  267. BN_zero(R);
  268. if (!(BN_set_bit(R, BN_BITS2)))
  269. goto err; /* R */
  270. buf[0] = mod->d[0]; /* tmod = N mod word size */
  271. buf[1] = 0;
  272. tmod.top = buf[0] != 0 ? 1 : 0;
  273. /* Ri = R^-1 mod N */
  274. if (BN_is_one(&tmod))
  275. BN_zero(Ri);
  276. else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  277. goto err;
  278. if (!BN_lshift(Ri, Ri, BN_BITS2))
  279. goto err; /* R*Ri */
  280. if (!BN_is_zero(Ri)) {
  281. if (!BN_sub_word(Ri, 1))
  282. goto err;
  283. } else { /* if N mod word size == 1 */
  284. if (!BN_set_word(Ri, BN_MASK2))
  285. goto err; /* Ri-- (mod word size) */
  286. }
  287. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  288. goto err;
  289. /*
  290. * Ni = (R*Ri-1)/N, keep only least significant word:
  291. */
  292. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  293. mont->n0[1] = 0;
  294. # endif
  295. }
  296. #else /* !MONT_WORD */
  297. { /* bignum version */
  298. mont->ri = BN_num_bits(&mont->N);
  299. BN_zero(R);
  300. if (!BN_set_bit(R, mont->ri))
  301. goto err; /* R = 2^ri */
  302. /* Ri = R^-1 mod N */
  303. if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)
  304. goto err;
  305. if (!BN_lshift(Ri, Ri, mont->ri))
  306. goto err; /* R*Ri */
  307. if (!BN_sub_word(Ri, 1))
  308. goto err;
  309. /*
  310. * Ni = (R*Ri-1) / N
  311. */
  312. if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))
  313. goto err;
  314. }
  315. #endif
  316. /* setup RR for conversions */
  317. BN_zero(&(mont->RR));
  318. if (!BN_set_bit(&(mont->RR), mont->ri * 2))
  319. goto err;
  320. if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))
  321. goto err;
  322. ret = 1;
  323. err:
  324. BN_CTX_end(ctx);
  325. return ret;
  326. }
  327. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)
  328. {
  329. if (to == from)
  330. return to;
  331. if (!BN_copy(&(to->RR), &(from->RR)))
  332. return NULL;
  333. if (!BN_copy(&(to->N), &(from->N)))
  334. return NULL;
  335. if (!BN_copy(&(to->Ni), &(from->Ni)))
  336. return NULL;
  337. to->ri = from->ri;
  338. to->n0[0] = from->n0[0];
  339. to->n0[1] = from->n0[1];
  340. return to;
  341. }
  342. BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,
  343. const BIGNUM *mod, BN_CTX *ctx)
  344. {
  345. BN_MONT_CTX *ret;
  346. CRYPTO_THREAD_read_lock(lock);
  347. ret = *pmont;
  348. CRYPTO_THREAD_unlock(lock);
  349. if (ret)
  350. return ret;
  351. /*
  352. * We don't want to serialise globally while doing our lazy-init math in
  353. * BN_MONT_CTX_set. That punishes threads that are doing independent
  354. * things. Instead, punish the case where more than one thread tries to
  355. * lazy-init the same 'pmont', by having each do the lazy-init math work
  356. * independently and only use the one from the thread that wins the race
  357. * (the losers throw away the work they've done).
  358. */
  359. ret = BN_MONT_CTX_new();
  360. if (ret == NULL)
  361. return NULL;
  362. if (!BN_MONT_CTX_set(ret, mod, ctx)) {
  363. BN_MONT_CTX_free(ret);
  364. return NULL;
  365. }
  366. /* The locked compare-and-set, after the local work is done. */
  367. CRYPTO_THREAD_write_lock(lock);
  368. if (*pmont) {
  369. BN_MONT_CTX_free(ret);
  370. ret = *pmont;
  371. } else
  372. *pmont = ret;
  373. CRYPTO_THREAD_unlock(lock);
  374. return ret;
  375. }