bn_mod.c 8.0 KB

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