bn_mont.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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_local.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. int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);
  25. bn_correct_top(r);
  26. bn_check_top(r);
  27. return ret;
  28. }
  29. int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  30. BN_MONT_CTX *mont, BN_CTX *ctx)
  31. {
  32. BIGNUM *tmp;
  33. int ret = 0;
  34. int num = mont->N.top;
  35. #if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)
  36. if (num > 1 && a->top == num && b->top == num) {
  37. if (bn_wexpand(r, num) == NULL)
  38. return 0;
  39. if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
  40. r->neg = a->neg ^ b->neg;
  41. r->top = num;
  42. r->flags |= BN_FLG_FIXED_TOP;
  43. return 1;
  44. }
  45. }
  46. #endif
  47. if ((a->top + b->top) > 2 * num)
  48. return 0;
  49. BN_CTX_start(ctx);
  50. tmp = BN_CTX_get(ctx);
  51. if (tmp == NULL)
  52. goto err;
  53. bn_check_top(tmp);
  54. if (a == b) {
  55. if (!bn_sqr_fixed_top(tmp, a, ctx))
  56. goto err;
  57. } else {
  58. if (!bn_mul_fixed_top(tmp, a, b, ctx))
  59. goto err;
  60. }
  61. /* reduce from aRR to aR */
  62. #ifdef MONT_WORD
  63. if (!bn_from_montgomery_word(r, tmp, mont))
  64. goto err;
  65. #else
  66. if (!BN_from_montgomery(r, tmp, mont, ctx))
  67. goto err;
  68. #endif
  69. ret = 1;
  70. err:
  71. BN_CTX_end(ctx);
  72. return ret;
  73. }
  74. #ifdef MONT_WORD
  75. static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
  76. {
  77. BIGNUM *n;
  78. BN_ULONG *ap, *np, *rp, n0, v, carry;
  79. int nl, max, i;
  80. unsigned int rtop;
  81. n = &(mont->N);
  82. nl = n->top;
  83. if (nl == 0) {
  84. ret->top = 0;
  85. return 1;
  86. }
  87. max = (2 * nl); /* carry is stored separately */
  88. if (bn_wexpand(r, max) == NULL)
  89. return 0;
  90. r->neg ^= n->neg;
  91. np = n->d;
  92. rp = r->d;
  93. /* clear the top words of T */
  94. for (rtop = r->top, i = 0; i < max; i++) {
  95. v = (BN_ULONG)0 - ((i - rtop) >> (8 * sizeof(rtop) - 1));
  96. rp[i] &= v;
  97. }
  98. r->top = max;
  99. r->flags |= BN_FLG_FIXED_TOP;
  100. n0 = mont->n0[0];
  101. /*
  102. * Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On
  103. * input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r|
  104. * includes |carry| which is stored separately.
  105. */
  106. for (carry = 0, i = 0; i < nl; i++, rp++) {
  107. v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
  108. v = (v + carry + rp[nl]) & BN_MASK2;
  109. carry |= (v != rp[nl]);
  110. carry &= (v <= rp[nl]);
  111. rp[nl] = v;
  112. }
  113. if (bn_wexpand(ret, nl) == NULL)
  114. return 0;
  115. ret->top = nl;
  116. ret->flags |= BN_FLG_FIXED_TOP;
  117. ret->neg = r->neg;
  118. rp = ret->d;
  119. /*
  120. * Shift |nl| words to divide by R. We have |ap| < 2 * |n|. Note that |ap|
  121. * includes |carry| which is stored separately.
  122. */
  123. ap = &(r->d[nl]);
  124. carry -= bn_sub_words(rp, ap, np, nl);
  125. /*
  126. * |carry| is -1 if |ap| - |np| underflowed or zero if it did not. Note
  127. * |carry| cannot be 1. That would imply the subtraction did not fit in
  128. * |nl| words, and we know at most one subtraction is needed.
  129. */
  130. for (i = 0; i < nl; i++) {
  131. rp[i] = (carry & ap[i]) | (~carry & rp[i]);
  132. ap[i] = 0;
  133. }
  134. return 1;
  135. }
  136. #endif /* MONT_WORD */
  137. int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
  138. BN_CTX *ctx)
  139. {
  140. int retn;
  141. retn = bn_from_mont_fixed_top(ret, a, mont, ctx);
  142. bn_correct_top(ret);
  143. bn_check_top(ret);
  144. return retn;
  145. }
  146. int bn_from_mont_fixed_top(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
  147. BN_CTX *ctx)
  148. {
  149. int retn = 0;
  150. #ifdef MONT_WORD
  151. BIGNUM *t;
  152. BN_CTX_start(ctx);
  153. if ((t = BN_CTX_get(ctx)) && BN_copy(t, a)) {
  154. retn = bn_from_montgomery_word(ret, t, mont);
  155. }
  156. BN_CTX_end(ctx);
  157. #else /* !MONT_WORD */
  158. BIGNUM *t1, *t2;
  159. BN_CTX_start(ctx);
  160. t1 = BN_CTX_get(ctx);
  161. t2 = BN_CTX_get(ctx);
  162. if (t2 == NULL)
  163. goto err;
  164. if (!BN_copy(t1, a))
  165. goto err;
  166. BN_mask_bits(t1, mont->ri);
  167. if (!BN_mul(t2, t1, &mont->Ni, ctx))
  168. goto err;
  169. BN_mask_bits(t2, mont->ri);
  170. if (!BN_mul(t1, t2, &mont->N, ctx))
  171. goto err;
  172. if (!BN_add(t2, a, t1))
  173. goto err;
  174. if (!BN_rshift(ret, t2, mont->ri))
  175. goto err;
  176. if (BN_ucmp(ret, &(mont->N)) >= 0) {
  177. if (!BN_usub(ret, ret, &(mont->N)))
  178. goto err;
  179. }
  180. retn = 1;
  181. bn_check_top(ret);
  182. err:
  183. BN_CTX_end(ctx);
  184. #endif /* MONT_WORD */
  185. return retn;
  186. }
  187. int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  188. BN_CTX *ctx)
  189. {
  190. return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);
  191. }
  192. BN_MONT_CTX *BN_MONT_CTX_new(void)
  193. {
  194. BN_MONT_CTX *ret;
  195. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
  196. ERR_raise(ERR_LIB_BN, ERR_R_MALLOC_FAILURE);
  197. return NULL;
  198. }
  199. BN_MONT_CTX_init(ret);
  200. ret->flags = BN_FLG_MALLOCED;
  201. return ret;
  202. }
  203. void BN_MONT_CTX_init(BN_MONT_CTX *ctx)
  204. {
  205. ctx->ri = 0;
  206. bn_init(&ctx->RR);
  207. bn_init(&ctx->N);
  208. bn_init(&ctx->Ni);
  209. ctx->n0[0] = ctx->n0[1] = 0;
  210. ctx->flags = 0;
  211. }
  212. void BN_MONT_CTX_free(BN_MONT_CTX *mont)
  213. {
  214. if (mont == NULL)
  215. return;
  216. BN_clear_free(&mont->RR);
  217. BN_clear_free(&mont->N);
  218. BN_clear_free(&mont->Ni);
  219. if (mont->flags & BN_FLG_MALLOCED)
  220. OPENSSL_free(mont);
  221. }
  222. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
  223. {
  224. int i, ret = 0;
  225. BIGNUM *Ri, *R;
  226. if (BN_is_zero(mod))
  227. return 0;
  228. BN_CTX_start(ctx);
  229. if ((Ri = BN_CTX_get(ctx)) == NULL)
  230. goto err;
  231. R = &(mont->RR); /* grab RR as a temp */
  232. if (!BN_copy(&(mont->N), mod))
  233. goto err; /* Set N */
  234. if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
  235. BN_set_flags(&(mont->N), BN_FLG_CONSTTIME);
  236. mont->N.neg = 0;
  237. #ifdef MONT_WORD
  238. {
  239. BIGNUM tmod;
  240. BN_ULONG buf[2];
  241. bn_init(&tmod);
  242. tmod.d = buf;
  243. tmod.dmax = 2;
  244. tmod.neg = 0;
  245. if (BN_get_flags(mod, BN_FLG_CONSTTIME) != 0)
  246. BN_set_flags(&tmod, BN_FLG_CONSTTIME);
  247. mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
  248. # if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)
  249. /*
  250. * Only certain BN_BITS2<=32 platforms actually make use of n0[1],
  251. * and we could use the #else case (with a shorter R value) for the
  252. * others. However, currently only the assembler files do know which
  253. * is which.
  254. */
  255. BN_zero(R);
  256. if (!(BN_set_bit(R, 2 * BN_BITS2)))
  257. goto err;
  258. tmod.top = 0;
  259. if ((buf[0] = mod->d[0]))
  260. tmod.top = 1;
  261. if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))
  262. tmod.top = 2;
  263. if (BN_is_one(&tmod))
  264. BN_zero(Ri);
  265. else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  266. goto err;
  267. if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))
  268. goto err; /* R*Ri */
  269. if (!BN_is_zero(Ri)) {
  270. if (!BN_sub_word(Ri, 1))
  271. goto err;
  272. } else { /* if N mod word size == 1 */
  273. if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)
  274. goto err;
  275. /* Ri-- (mod double word size) */
  276. Ri->neg = 0;
  277. Ri->d[0] = BN_MASK2;
  278. Ri->d[1] = BN_MASK2;
  279. Ri->top = 2;
  280. }
  281. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  282. goto err;
  283. /*
  284. * Ni = (R*Ri-1)/N, keep only couple of least significant words:
  285. */
  286. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  287. mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
  288. # else
  289. BN_zero(R);
  290. if (!(BN_set_bit(R, BN_BITS2)))
  291. goto err; /* R */
  292. buf[0] = mod->d[0]; /* tmod = N mod word size */
  293. buf[1] = 0;
  294. tmod.top = buf[0] != 0 ? 1 : 0;
  295. /* Ri = R^-1 mod N */
  296. if (BN_is_one(&tmod))
  297. BN_zero(Ri);
  298. else if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)
  299. goto err;
  300. if (!BN_lshift(Ri, Ri, BN_BITS2))
  301. goto err; /* R*Ri */
  302. if (!BN_is_zero(Ri)) {
  303. if (!BN_sub_word(Ri, 1))
  304. goto err;
  305. } else { /* if N mod word size == 1 */
  306. if (!BN_set_word(Ri, BN_MASK2))
  307. goto err; /* Ri-- (mod word size) */
  308. }
  309. if (!BN_div(Ri, NULL, Ri, &tmod, ctx))
  310. goto err;
  311. /*
  312. * Ni = (R*Ri-1)/N, keep only least significant word:
  313. */
  314. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  315. mont->n0[1] = 0;
  316. # endif
  317. }
  318. #else /* !MONT_WORD */
  319. { /* bignum version */
  320. mont->ri = BN_num_bits(&mont->N);
  321. BN_zero(R);
  322. if (!BN_set_bit(R, mont->ri))
  323. goto err; /* R = 2^ri */
  324. /* Ri = R^-1 mod N */
  325. if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)
  326. goto err;
  327. if (!BN_lshift(Ri, Ri, mont->ri))
  328. goto err; /* R*Ri */
  329. if (!BN_sub_word(Ri, 1))
  330. goto err;
  331. /*
  332. * Ni = (R*Ri-1) / N
  333. */
  334. if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))
  335. goto err;
  336. }
  337. #endif
  338. /* setup RR for conversions */
  339. BN_zero(&(mont->RR));
  340. if (!BN_set_bit(&(mont->RR), mont->ri * 2))
  341. goto err;
  342. if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))
  343. goto err;
  344. for (i = mont->RR.top, ret = mont->N.top; i < ret; i++)
  345. mont->RR.d[i] = 0;
  346. mont->RR.top = ret;
  347. mont->RR.flags |= BN_FLG_FIXED_TOP;
  348. ret = 1;
  349. err:
  350. BN_CTX_end(ctx);
  351. return ret;
  352. }
  353. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from)
  354. {
  355. if (to == from)
  356. return to;
  357. if (!BN_copy(&(to->RR), &(from->RR)))
  358. return NULL;
  359. if (!BN_copy(&(to->N), &(from->N)))
  360. return NULL;
  361. if (!BN_copy(&(to->Ni), &(from->Ni)))
  362. return NULL;
  363. to->ri = from->ri;
  364. to->n0[0] = from->n0[0];
  365. to->n0[1] = from->n0[1];
  366. return to;
  367. }
  368. BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_RWLOCK *lock,
  369. const BIGNUM *mod, BN_CTX *ctx)
  370. {
  371. BN_MONT_CTX *ret;
  372. CRYPTO_THREAD_read_lock(lock);
  373. ret = *pmont;
  374. CRYPTO_THREAD_unlock(lock);
  375. if (ret)
  376. return ret;
  377. /*
  378. * We don't want to serialize globally while doing our lazy-init math in
  379. * BN_MONT_CTX_set. That punishes threads that are doing independent
  380. * things. Instead, punish the case where more than one thread tries to
  381. * lazy-init the same 'pmont', by having each do the lazy-init math work
  382. * independently and only use the one from the thread that wins the race
  383. * (the losers throw away the work they've done).
  384. */
  385. ret = BN_MONT_CTX_new();
  386. if (ret == NULL)
  387. return NULL;
  388. if (!BN_MONT_CTX_set(ret, mod, ctx)) {
  389. BN_MONT_CTX_free(ret);
  390. return NULL;
  391. }
  392. /* The locked compare-and-set, after the local work is done. */
  393. CRYPTO_THREAD_write_lock(lock);
  394. if (*pmont) {
  395. BN_MONT_CTX_free(ret);
  396. ret = *pmont;
  397. } else
  398. *pmont = ret;
  399. CRYPTO_THREAD_unlock(lock);
  400. return ret;
  401. }