diff --git a/library/bignum.c b/library/bignum.c index b7b90ec3e7..da8e8ca3c7 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1465,7 +1465,7 @@ mbedtls_mpi_uint mpi_mul_hlp( mbedtls_mpi_uint *d, size_t d_len , int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ) { int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; - size_t i, j; + size_t i, j, k; mbedtls_mpi TA, TB; int result_is_zero = 0; MPI_VALIDATE_RET( X != NULL ); @@ -1492,8 +1492,14 @@ int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) ); - for( ; j > 0; j-- ) - mpi_mul_hlp( i, A->p, X->p + j - 1, B->p[j - 1] ); + for( k = 0; k < j; k++ ) + { + /* We know that there cannot be any carry-out since we're + * iterating from bottom to top. */ + (void) mpi_mul_hlp( X->p + k, i + 1, + A->p, i, + B->p[k] ); + } /* If the result is 0, we don't shortcut the operation, which reduces * but does not eliminate side channels leaking the zero-ness. We do