2 * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/cryptlib.h"
13 int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx)
16 * like BN_mod, but returns non-negative remainder (i.e., 0 <= r < |d|
21 ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
25 if (!(BN_mod(r, m, d, ctx)))
29 /* now -|d| < r < 0, so we have to set r := r + |d| */
30 return (d->neg ? BN_sub : BN_add) (r, r, d);
33 int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
38 return BN_nnmod(r, r, m, ctx);
42 * BN_mod_add variant that may be used if both a and b are non-negative and
43 * less than m. The original algorithm was
45 * if (!BN_uadd(r, a, b))
47 * if (BN_ucmp(r, m) >= 0)
48 * return BN_usub(r, r, m);
50 * which is replaced with addition, subtracting modulus, and conditional
51 * move depending on whether or not subtraction borrowed.
53 int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
56 size_t i, ai, bi, mtop = m->top;
57 BN_ULONG storage[1024 / BN_BITS2];
58 BN_ULONG carry, temp, mask, *rp, *tp = storage;
59 const BN_ULONG *ap, *bp;
61 if (bn_wexpand(r, mtop) == NULL)
64 if (mtop > sizeof(storage) / sizeof(storage[0])) {
65 tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG));
70 ap = a->d != NULL ? a->d : tp;
71 bp = b->d != NULL ? b->d : tp;
73 for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) {
74 mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
75 temp = ((ap[ai] & mask) + carry) & BN_MASK2;
76 carry = (temp < carry);
78 mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
79 tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2;
80 carry += (tp[i] < temp);
83 ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
84 bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
87 carry -= bn_sub_words(rp, tp, m->d, mtop);
88 for (i = 0; i < mtop; i++) {
89 rp[i] = (carry & tp[i]) | (~carry & rp[i]);
90 ((volatile BN_ULONG *)tp)[i] = 0;
93 r->flags |= BN_FLG_FIXED_TOP;
102 int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
105 int ret = bn_mod_add_fixed_top(r, a, b, m);
113 int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
116 if (!BN_sub(r, a, b))
118 return BN_nnmod(r, r, m, ctx);
122 * BN_mod_sub variant that may be used if both a and b are non-negative,
123 * a is less than m, while b is of same bit width as m. It's implemented
124 * as subtraction followed by two conditional additions.
131 * -2*m < r = a - b < m
133 * Thus it takes up to two conditional additions to make |r| positive.
135 int bn_mod_sub_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
138 size_t i, ai, bi, mtop = m->top;
139 BN_ULONG borrow, carry, ta, tb, mask, *rp;
140 const BN_ULONG *ap, *bp;
142 if (bn_wexpand(r, mtop) == NULL)
146 ap = a->d != NULL ? a->d : rp;
147 bp = b->d != NULL ? b->d : rp;
149 for (i = 0, ai = 0, bi = 0, borrow = 0; i < mtop;) {
150 mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));
153 mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));
155 rp[i] = ta - tb - borrow;
160 ai += (i - a->dmax) >> (8 * sizeof(i) - 1);
161 bi += (i - b->dmax) >> (8 * sizeof(i) - 1);
164 for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
165 ta = ((ap[i] & mask) + carry) & BN_MASK2;
166 carry = (ta < carry);
167 rp[i] = (rp[i] + ta) & BN_MASK2;
168 carry += (rp[i] < ta);
171 for (i = 0, mask = 0 - borrow, carry = 0; i < mtop; i++) {
172 ta = ((ap[i] & mask) + carry) & BN_MASK2;
173 carry = (ta < carry);
174 rp[i] = (rp[i] + ta) & BN_MASK2;
175 carry += (rp[i] < ta);
179 r->flags |= BN_FLG_FIXED_TOP;
186 * BN_mod_sub variant that may be used if both a and b are non-negative and
189 int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
193 ERR_raise(ERR_LIB_BN, ERR_R_PASSED_INVALID_ARGUMENT);
197 if (!BN_sub(r, a, b))
200 return BN_add(r, r, m);
205 int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
216 if ((t = BN_CTX_get(ctx)) == NULL)
219 if (!BN_sqr(t, a, ctx))
222 if (!BN_mul(t, a, b, ctx))
225 if (!BN_nnmod(r, t, m, ctx))
234 int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
236 if (!BN_sqr(r, a, ctx))
238 /* r->neg == 0, thus we don't need BN_nnmod */
239 return BN_mod(r, r, m, ctx);
242 int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx)
244 if (!BN_lshift1(r, a))
247 return BN_nnmod(r, r, m, ctx);
251 * BN_mod_lshift1 variant that may be used if a is non-negative and less than
254 int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m)
256 if (!BN_lshift1(r, a))
259 if (BN_cmp(r, m) >= 0)
260 return BN_sub(r, r, m);
264 int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
267 BIGNUM *abs_m = NULL;
270 if (!BN_nnmod(r, a, m, ctx))
280 ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
288 * BN_mod_lshift variant that may be used if a is non-negative and less than
291 int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m)
294 if (BN_copy(r, a) == NULL)
302 max_shift = BN_num_bits(m) - BN_num_bits(r);
306 ERR_raise(ERR_LIB_BN, BN_R_INPUT_NOT_REDUCED);
314 if (!BN_lshift(r, r, max_shift))
318 if (!BN_lshift1(r, r))
323 /* BN_num_bits(r) <= BN_num_bits(m) */
325 if (BN_cmp(r, m) >= 0) {
326 if (!BN_sub(r, r, m))