bn_mod.c 7.8 KB

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