Exp mod: handle negative zero

Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
Janos Follath 2024-02-21 11:25:41 +00:00
parent aec1a868fe
commit 86258f51b5
2 changed files with 6 additions and 1 deletions

View File

@ -1689,7 +1689,9 @@ int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A,
* Correct for negative A.
*/
if (A->s == -1 && (E->p[0] & 1) != 0) {
X->s = -1;
mbedtls_ct_condition_t is_x_non_zero = mbedtls_mpi_core_check_zero_ct(X->p, X->n);
X->s = mbedtls_ct_uint_if(is_x_non_zero, -1, 1);
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, N, X));
}

View File

@ -1362,6 +1362,9 @@ mpi_exp_mod:"04":"00":"09":"1":0
Test mbedtls_mpi_exp_mod: 10 ^ 0 (1 limb) mod 9
mpi_exp_mod:"0a":"00":"09":"1":0
Test mbedtls_mpi_exp_mod: -3 ^ 3 mod 27
mpi_exp_mod:"-3":"3":"1b":"1b":0
Test mbedtls_mpi_exp_mod: MAX_SIZE exponent
mpi_exp_mod_size:2:MBEDTLS_MPI_MAX_SIZE:10:"":0