bn_mod.c 7.7 KB

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