]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/bn/bn_mod.c
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|
20 if (!(BN_mod(r
, m
, d
, ctx
)))
24 /* now -|d| < r < 0, so we have to set r := r + |d| */
25 return (d
->neg
? BN_sub
: BN_add
) (r
, r
, d
);
28 int BN_mod_add(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
, const BIGNUM
*m
,
33 return BN_nnmod(r
, r
, m
, ctx
);
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
40 * if (!BN_uadd(r, a, b))
42 * if (BN_ucmp(r, m) >= 0)
43 * return BN_usub(r, r, m);
45 * which is replaced with addition, subtracting modulus, and conditional
46 * move depending on whether or not subtraction borrowed.
48 int bn_mod_add_fixed_top(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
,
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
;
56 if (bn_wexpand(r
, mtop
) == NULL
)
59 if (mtop
> sizeof(storage
) / sizeof(storage
[0])) {
60 tp
= OPENSSL_malloc(mtop
* sizeof(BN_ULONG
));
65 ap
= a
->d
!= NULL
? a
->d
: tp
;
66 bp
= b
->d
!= NULL
? b
->d
: tp
;
68 for (i
= 0, ai
= 0, bi
= 0, carry
= 0; i
< mtop
;) {
69 mask
= (BN_ULONG
)0 - ((i
- a
->top
) >> (8 * sizeof(i
) - 1));
70 temp
= ((ap
[ai
] & mask
) + carry
) & BN_MASK2
;
71 carry
= (temp
< carry
);
73 mask
= (BN_ULONG
)0 - ((i
- b
->top
) >> (8 * sizeof(i
) - 1));
74 tp
[i
] = ((bp
[bi
] & mask
) + temp
) & BN_MASK2
;
75 carry
+= (tp
[i
] < temp
);
78 ai
+= (i
- a
->dmax
) >> (8 * sizeof(i
) - 1);
79 bi
+= (i
- b
->dmax
) >> (8 * sizeof(i
) - 1);
82 carry
-= bn_sub_words(rp
, tp
, m
->d
, mtop
);
83 for (i
= 0; i
< mtop
; i
++) {
84 rp
[i
] = (carry
& tp
[i
]) | (~carry
& rp
[i
]);
85 ((volatile BN_ULONG
*)tp
)[i
] = 0;
88 r
->flags
|= BN_FLG_FIXED_TOP
;
97 int BN_mod_add_quick(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
,
100 int ret
= bn_mod_add_fixed_top(r
, a
, b
, m
);
108 int BN_mod_sub(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
, const BIGNUM
*m
,
111 if (!BN_sub(r
, a
, b
))
113 return BN_nnmod(r
, r
, m
, ctx
);
117 * BN_mod_sub variant that may be used if both a and b are non-negative,
118 * a is less than m, while b is of same bit width as m. It's implemented
119 * as subtraction followed by two conditional additions.
126 * -2*m < r = a - b < m
128 * Thus it takes up to two conditional additions to make |r| positive.
130 int bn_mod_sub_fixed_top(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
,
133 size_t i
, ai
, bi
, mtop
= m
->top
;
134 BN_ULONG borrow
, carry
, ta
, tb
, mask
, *rp
;
135 const BN_ULONG
*ap
, *bp
;
137 if (bn_wexpand(r
, mtop
) == NULL
)
141 ap
= a
->d
!= NULL
? a
->d
: rp
;
142 bp
= b
->d
!= NULL
? b
->d
: rp
;
144 for (i
= 0, ai
= 0, bi
= 0, borrow
= 0; i
< mtop
;) {
145 mask
= (BN_ULONG
)0 - ((i
- a
->top
) >> (8 * sizeof(i
) - 1));
148 mask
= (BN_ULONG
)0 - ((i
- b
->top
) >> (8 * sizeof(i
) - 1));
150 rp
[i
] = ta
- tb
- borrow
;
155 ai
+= (i
- a
->dmax
) >> (8 * sizeof(i
) - 1);
156 bi
+= (i
- b
->dmax
) >> (8 * sizeof(i
) - 1);
159 for (i
= 0, mask
= 0 - borrow
, carry
= 0; i
< mtop
; i
++) {
160 ta
= ((ap
[i
] & mask
) + carry
) & BN_MASK2
;
161 carry
= (ta
< carry
);
162 rp
[i
] = (rp
[i
] + ta
) & BN_MASK2
;
163 carry
+= (rp
[i
] < ta
);
166 for (i
= 0, mask
= 0 - borrow
, carry
= 0; i
< mtop
; i
++) {
167 ta
= ((ap
[i
] & mask
) + carry
) & BN_MASK2
;
168 carry
= (ta
< carry
);
169 rp
[i
] = (rp
[i
] + ta
) & BN_MASK2
;
170 carry
+= (rp
[i
] < ta
);
174 r
->flags
|= BN_FLG_FIXED_TOP
;
181 * BN_mod_sub variant that may be used if both a and b are non-negative and
184 int BN_mod_sub_quick(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
,
187 if (!BN_sub(r
, a
, b
))
190 return BN_add(r
, r
, m
);
195 int BN_mod_mul(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
, const BIGNUM
*m
,
206 if ((t
= BN_CTX_get(ctx
)) == NULL
)
209 if (!BN_sqr(t
, a
, ctx
))
212 if (!BN_mul(t
, a
, b
, ctx
))
215 if (!BN_nnmod(r
, t
, m
, ctx
))
224 int BN_mod_sqr(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*m
, BN_CTX
*ctx
)
226 if (!BN_sqr(r
, a
, ctx
))
228 /* r->neg == 0, thus we don't need BN_nnmod */
229 return BN_mod(r
, r
, m
, ctx
);
232 int BN_mod_lshift1(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*m
, BN_CTX
*ctx
)
234 if (!BN_lshift1(r
, a
))
237 return BN_nnmod(r
, r
, m
, ctx
);
241 * BN_mod_lshift1 variant that may be used if a is non-negative and less than
244 int BN_mod_lshift1_quick(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*m
)
246 if (!BN_lshift1(r
, a
))
249 if (BN_cmp(r
, m
) >= 0)
250 return BN_sub(r
, r
, m
);
254 int BN_mod_lshift(BIGNUM
*r
, const BIGNUM
*a
, int n
, const BIGNUM
*m
,
257 BIGNUM
*abs_m
= NULL
;
260 if (!BN_nnmod(r
, a
, m
, ctx
))
270 ret
= BN_mod_lshift_quick(r
, r
, n
, (abs_m
? abs_m
: m
));
278 * BN_mod_lshift variant that may be used if a is non-negative and less than
281 int BN_mod_lshift_quick(BIGNUM
*r
, const BIGNUM
*a
, int n
, const BIGNUM
*m
)
284 if (BN_copy(r
, a
) == NULL
)
292 max_shift
= BN_num_bits(m
) - BN_num_bits(r
);
296 ERR_raise(ERR_LIB_BN
, BN_R_INPUT_NOT_REDUCED
);
304 if (!BN_lshift(r
, r
, max_shift
))
308 if (!BN_lshift1(r
, r
))
313 /* BN_num_bits(r) <= BN_num_bits(m) */
315 if (BN_cmp(r
, m
) >= 0) {
316 if (!BN_sub(r
, r
, m
))