diff options
author | Tomas Mraz <tomas@openssl.org> | 2023-10-04 09:30:43 +0200 |
---|---|---|
committer | Tomas Mraz <tomas@openssl.org> | 2023-10-05 12:05:48 +0200 |
commit | f41af715c110fd81b8f100ac08a99332595c5a7b (patch) | |
tree | 0330eb1d9845b9624755443075cc69bf6b3d0199 /crypto/bn | |
parent | 65f206da8fbd52f9094d49cabf74afc7e51f612f (diff) |
BN_gcd(): Avoid shifts of negative values
Fixes #22216
Thanks to Leland Mills for investigation and testing.
Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/22272)
(cherry picked from commit 0f7a3b0caa33a87c900536dc1c02fa553d2193cc)
Diffstat (limited to 'crypto/bn')
-rw-r--r-- | crypto/bn/bn_gcd.c | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/crypto/bn/bn_gcd.c b/crypto/bn/bn_gcd.c index d0a3d3eb94..bf29530e39 100644 --- a/crypto/bn/bn_gcd.c +++ b/crypto/bn/bn_gcd.c @@ -642,9 +642,9 @@ int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) for (i = 0; i < m; i++) { /* conditionally flip signs if delta is positive and g is odd */ - cond = (-delta >> (8 * sizeof(delta) - 1)) & g->d[0] & 1 + cond = ((unsigned int)-delta >> (8 * sizeof(delta) - 1)) & g->d[0] & 1 /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */ - & (~((g->top - 1) >> (sizeof(g->top) * 8 - 1))); + & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1))); delta = (-cond & -delta) | ((cond - 1) & delta); r->neg ^= cond; /* swap */ @@ -656,7 +656,7 @@ int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) goto err; BN_consttime_swap(g->d[0] & 1 /* g is odd */ /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */ - & (~((g->top - 1) >> (sizeof(g->top) * 8 - 1))), + & (~((unsigned int)(g->top - 1) >> (sizeof(g->top) * 8 - 1))), g, temp, top); if (!BN_rshift1(g, g)) goto err; |