bn_mod.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright 1998-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. #include "internal/cryptlib.h"
  10. #include "bn_lcl.h"
  11. int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
  12. {
  13. /*
  14. * like BN_mod, but returns non-negative remainder (i.e., 0 <= r < |d|
  15. * always holds)
  16. */
  17. if (!(BN_mod(r, m, d, ctx)))
  18. return 0;
  19. if (!r->neg)
  20. return 1;
  21. /* now -|d| < r < 0, so we have to set r := r + |d| */
  22. return (d->neg ? BN_sub : BN_add) (r, r, d);
  23. }
  24. int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  25. BN_CTX *ctx)
  26. {
  27. if (!BN_add(r, a, b))
  28. return 0;
  29. return BN_nnmod(r, r, m, ctx);
  30. }
  31. /*
  32. * BN_mod_add variant that may be used if both a and b are non-negative and
  33. * less than m. The original algorithm was
  34. *
  35. * if (!BN_uadd(r, a, b))
  36. * return 0;
  37. * if (BN_ucmp(r, m) >= 0)
  38. * return BN_usub(r, r, m);
  39. *
  40. * which is replaced with addition, subtracting modulus, and conditional
  41. * move depending on whether or not subtraction borrowed.
  42. */
  43. int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  44. const BIGNUM *m)
  45. {
  46. size_t i, ai, bi, mtop = m->top;
  47. BN_ULONG storage[1024 / BN_BITS2];
  48. BN_ULONG carry, temp, mask, *rp, *tp = storage;
  49. const BN_ULONG *ap, *bp;
  50. if (bn_wexpand(r, mtop) == NULL)
  51. return 0;
  52. if (mtop > sizeof(storage) / sizeof(storage[0])
  53. && (tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG))) == NULL)
  54. return 0;
  55. ap = a->d != NULL ? a->d : tp;
  56. bp = b->d != NULL ? b->d : tp;
  57. for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) {
  58. mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
  59. temp = ((ap[ai] & mask) + carry) & BN_MASK2;
  60. carry = (temp < carry);
  61. mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
  62. tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2;
  63. carry += (tp[i] < temp);
  64. i++;
  65. ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
  66. bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
  67. }
  68. rp = r->d;
  69. carry -= bn_sub_words(rp, tp, m->d, mtop);
  70. for (i = 0; i < mtop; i++) {
  71. rp[i] = (carry & tp[i]) | (~carry & rp[i]);
  72. ((volatile BN_ULONG *)tp)[i] = 0;
  73. }
  74. r->top = mtop;
  75. r->flags |= BN_FLG_FIXED_TOP;
  76. r->neg = 0;
  77. if (tp != storage)
  78. OPENSSL_free(tp);
  79. return 1;
  80. }
  81. int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  82. const BIGNUM *m)
  83. {
  84. int ret = bn_mod_add_fixed_top(r, a, b, m);
  85. if (ret)
  86. bn_correct_top(r);
  87. return ret;
  88. }
  89. int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  90. BN_CTX *ctx)
  91. {
  92. if (!BN_sub(r, a, b))
  93. return 0;
  94. return BN_nnmod(r, r, m, ctx);
  95. }
  96. /*
  97. * BN_mod_sub variant that may be used if both a and b are non-negative,
  98. * a is less than m, while b is of same bit width as m. It's implemented
  99. * as subtraction followed by two conditional additions.
  100. *
  101. * 0 <= a < m
  102. * 0 <= b < 2^w < 2*m
  103. *
  104. * after subtraction
  105. *
  106. * -2*m < r = a - b < m
  107. *
  108. * Thus it takes up to two conditional additions to make |r| positive.
  109. */
  110. int bn_mod_sub_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  111. const BIGNUM *m)
  112. {
  113. size_t i, ai, bi, mtop = m->top;
  114. BN_ULONG borrow, carry, ta, tb, mask, *rp;
  115. const BN_ULONG *ap, *bp;
  116. if (bn_wexpand(r, mtop) == NULL)
  117. return 0;
  118. rp = r->d;
  119. ap = a->d != NULL ? a->d : rp;
  120. bp = b->d != NULL ? b->d : rp;
  121. for (i = 0, ai = 0, bi = 0, borrow = 0; i < mtop;) {
  122. mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
  123. ta = ap[ai] & mask;
  124. mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
  125. tb = bp[bi] & mask;
  126. rp[i] = ta - tb - borrow;
  127. if (ta != tb)
  128. borrow = (ta < tb);
  129. i++;
  130. ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
  131. bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
  132. }
  133. ap = m->d;
  134. for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
  135. ta = ((ap[i] & mask) + carry) & BN_MASK2;
  136. carry = (ta < carry);
  137. rp[i] = (rp[i] + ta) & BN_MASK2;
  138. carry += (rp[i] < ta);
  139. }
  140. borrow -= carry;
  141. for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
  142. ta = ((ap[i] & mask) + carry) & BN_MASK2;
  143. carry = (ta < carry);
  144. rp[i] = (rp[i] + ta) & BN_MASK2;
  145. carry += (rp[i] < ta);
  146. }
  147. r->top = mtop;
  148. r->flags |= BN_FLG_FIXED_TOP;
  149. r->neg = 0;
  150. return 1;
  151. }
  152. /*
  153. * BN_mod_sub variant that may be used if both a and b are non-negative and
  154. * less than m
  155. */
  156. int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  157. const BIGNUM *m)
  158. {
  159. if (!BN_sub(r, a, b))
  160. return 0;
  161. if (r->neg)
  162. return BN_add(r, r, m);
  163. return 1;
  164. }
  165. /* slow but works */
  166. int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  167. BN_CTX *ctx)
  168. {
  169. BIGNUM *t;
  170. int ret = 0;
  171. bn_check_top(a);
  172. bn_check_top(b);
  173. bn_check_top(m);
  174. BN_CTX_start(ctx);
  175. if ((t = BN_CTX_get(ctx)) == NULL)
  176. goto err;
  177. if (a == b) {
  178. if (!BN_sqr(t, a, ctx))
  179. goto err;
  180. } else {
  181. if (!BN_mul(t, a, b, ctx))
  182. goto err;
  183. }
  184. if (!BN_nnmod(r, t, m, ctx))
  185. goto err;
  186. bn_check_top(r);
  187. ret = 1;
  188. err:
  189. BN_CTX_end(ctx);
  190. return ret;
  191. }
  192. int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
  193. {
  194. if (!BN_sqr(r, a, ctx))
  195. return 0;
  196. /* r->neg == 0, thus we don't need BN_nnmod */
  197. return BN_mod(r, r, m, ctx);
  198. }
  199. int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
  200. {
  201. if (!BN_lshift1(r, a))
  202. return 0;
  203. bn_check_top(r);
  204. return BN_nnmod(r, r, m, ctx);
  205. }
  206. /*
  207. * BN_mod_lshift1 variant that may be used if a is non-negative and less than
  208. * m
  209. */
  210. int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m)
  211. {
  212. if (!BN_lshift1(r, a))
  213. return 0;
  214. bn_check_top(r);
  215. if (BN_cmp(r, m) >= 0)
  216. return BN_sub(r, r, m);
  217. return 1;
  218. }
  219. int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
  220. BN_CTX *ctx)
  221. {
  222. BIGNUM *abs_m = NULL;
  223. int ret;
  224. if (!BN_nnmod(r, a, m, ctx))
  225. return 0;
  226. if (m->neg) {
  227. abs_m = BN_dup(m);
  228. if (abs_m == NULL)
  229. return 0;
  230. abs_m->neg = 0;
  231. }
  232. ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
  233. bn_check_top(r);
  234. BN_free(abs_m);
  235. return ret;
  236. }
  237. /*
  238. * BN_mod_lshift variant that may be used if a is non-negative and less than
  239. * m
  240. */
  241. int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)
  242. {
  243. if (r != a) {
  244. if (BN_copy(r, a) == NULL)
  245. return 0;
  246. }
  247. while (n > 0) {
  248. int max_shift;
  249. /* 0 < r < m */
  250. max_shift = BN_num_bits(m) - BN_num_bits(r);
  251. /* max_shift >= 0 */
  252. if (max_shift < 0) {
  253. BNerr(BN_F_BN_MOD_LSHIFT_QUICK, BN_R_INPUT_NOT_REDUCED);
  254. return 0;
  255. }
  256. if (max_shift > n)
  257. max_shift = n;
  258. if (max_shift) {
  259. if (!BN_lshift(r, r, max_shift))
  260. return 0;
  261. n -= max_shift;
  262. } else {
  263. if (!BN_lshift1(r, r))
  264. return 0;
  265. --n;
  266. }
  267. /* BN_num_bits(r) <= BN_num_bits(m) */
  268. if (BN_cmp(r, m) >= 0) {
  269. if (!BN_sub(r, r, m))
  270. return 0;
  271. }
  272. }
  273. bn_check_top(r);
  274. return 1;
  275. }