From: tb Date: Tue, 9 May 2023 05:38:11 +0000 (+0000) Subject: bn_exp: also special case -1 modulus X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=99065ed9a1b47572ad2ed09d823767b270b60d90;p=openbsd bn_exp: also special case -1 modulus Anything taken to the power of 0 is 1, and then reduced mod 1 or mod -1 it will be 0. If "anything" includes 0 or not is a matter of convention, but it should not depend on the sign of the modulus... Reported by Guido Vranken ok jsing (who had the same diff) --- diff --git a/lib/libcrypto/bn/bn_exp.c b/lib/libcrypto/bn/bn_exp.c index ff9933578ca..9e5d1fd26db 100644 --- a/lib/libcrypto/bn/bn_exp.c +++ b/lib/libcrypto/bn/bn_exp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_exp.c,v 1.45 2023/03/30 14:21:10 tb Exp $ */ +/* $OpenBSD: bn_exp.c,v 1.46 2023/05/09 05:38:11 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -194,7 +194,7 @@ BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, bits = BN_num_bits(p); if (bits == 0) { /* x**0 mod 1 is still zero. */ - if (BN_is_one(m)) { + if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(r); } else @@ -402,7 +402,7 @@ BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, bits = BN_num_bits(p); if (bits == 0) { /* x**0 mod 1 is still zero. */ - if (BN_is_one(m)) { + if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(rr); } else @@ -658,7 +658,7 @@ BN_mod_exp_mont_internal(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIG bits = BN_num_bits(p); if (bits == 0) { /* x**0 mod 1 is still zero. */ - if (BN_is_one(m)) { + if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(rr); } else @@ -843,7 +843,7 @@ BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p, const BIGNUM *m, bits = BN_num_bits(p); if (bits == 0) { /* x**0 mod 1 is still zero. */ - if (BN_is_one(m)) { + if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(rr); } else @@ -968,7 +968,7 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, bits = BN_num_bits(p); if (bits == 0) { /* x**0 mod 1 is still zero. */ - if (BN_is_one(m)) { + if (BN_abs_is_word(m, 1)) { ret = 1; BN_zero(r); } else