mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-07-29 11:41:15 +03:00
mbedtls_ecp_gen_privkey_mx: remove the exception for all-zero
The library rejected an RNG input of all-bits-zero, which led to the
key 2^{254} (for Curve25519) having a 31/32 chance of being generated
compared to other keys. This had no practical impact because the
probability of non-compliance was 2^{-256}, but needlessly
complicated the code.
The exception was added in 98e28a74e3
to
avoid the case where b - 1 wraps because b is 0. Instead, change the
comparison code to avoid calculating b - 1.
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
@ -3052,14 +3052,12 @@ int mbedtls_ecp_gen_privkey_mx( size_t high_bit,
|
||||
size_t n_bytes = ( high_bit + 7 ) / 8;
|
||||
|
||||
/* [Curve25519] page 5 */
|
||||
do {
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_bytes, f_rng, p_rng ) );
|
||||
} while( mbedtls_mpi_bitlen( d ) == 0);
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_bytes, f_rng, p_rng ) );
|
||||
|
||||
/* Make sure the most significant bit is high_bit */
|
||||
b = mbedtls_mpi_bitlen( d ) - 1; /* position of the highest bit in d */
|
||||
if( b > high_bit )
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - high_bit ) );
|
||||
b = mbedtls_mpi_bitlen( d ); /* mbedtls_mpi_bitlen is one-based */
|
||||
if( b > high_bit + 1 )
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - 1 - high_bit ) );
|
||||
else
|
||||
MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, high_bit, 1 ) );
|
||||
|
||||
|
Reference in New Issue
Block a user