Skip to content

myrand() in benchmark.c on big-endian causes rsa key generation to loop #10434

@jbsgh

Description

@jbsgh

Summary

benchmark.c generates pseudo-random numbers by calling rand() in myrand(). These numbers are used to feed the RSA key-generator. mbedtls_mpi_gen_prime() checks that the most significant uint32_t is greater or equal than CEIL_MAXUINT_DIV_SQRT2. This requires at least a set MSB in X->p[n-1]. On big-endian machines this uint32 is effectively set by a call to rand() in myrand(). rand() produces pseudo-random int numbers in the range 0..RAND_MAX. Thus, the MSB is never set (rand() is int!) and the loop in mbedtls_mpi_gen_prime() cannot terminate when comparing the value as uint32_t.

myrand() in benchmark.c has to provide uint32_t random numbers over the full range instead of 0..RAND_MAX.

System information

Mbed TLS version: 3.6.4
Configuration: big endian, 32 Bit

Expected behavior

The benchmark test terminates after completing all tests.

Actual behavior

The benchmark test loops in RSA key generation and does not terminate at all.

Steps to reproduce

Start benchmark test on a machine with big endian.

Additional information

On little-endian machines the bignum X is converted to big endian before. The least significant byte of the rand()-call is shifted to the most significant position. The resulting value in the little-endian comparison may then fulfill the neccessary condition in mbedtls_mpi_gen_prime() to leave the loop.

Metadata

Metadata

Assignees

Labels

bugpriority-mediumMedium priority - this can be reviewed as time permitssize-sEstimated task size: small (~2d)

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions