From 009d195a56522868cc7a887783f5db04706fd2ac Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 9 Sep 2022 21:00:00 +0200 Subject: [PATCH] Move mbedtls_mpi_core_fill_random to the proper .c file Signed-off-by: Gilles Peskine --- library/bignum.c | 28 ---------------------------- library/bignum_core.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/library/bignum.c b/library/bignum.c index b5431a0fec..76202fe514 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1931,34 +1931,6 @@ cleanup: return( ret ); } -/* Fill X with n_bytes random bytes. - * X must already have room for those bytes. - * The ordering of the bytes returned from the RNG is suitable for - * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_core_random()). - * The size and sign of X are unchanged. - * n_bytes must not be 0. - */ -int mbedtls_mpi_core_fill_random( - mbedtls_mpi_uint *X, size_t X_limbs, - size_t n_bytes, - int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) -{ - int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; - const size_t limbs = CHARS_TO_LIMBS( n_bytes ); - const size_t overhead = ( limbs * ciL ) - n_bytes; - - if( X_limbs < limbs ) - return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); - - memset( X, 0, overhead ); - memset( (unsigned char *) X + limbs * ciL, 0, ( X_limbs - limbs ) * ciL ); - MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X + overhead, n_bytes ) ); - mbedtls_mpi_core_bigendian_to_host( X, limbs ); - -cleanup: - return( ret ); -} - /* * Fill X with size bytes of random. * diff --git a/library/bignum_core.c b/library/bignum_core.c index b3bb3bcb88..e405995969 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -553,4 +553,33 @@ void mbedtls_mpi_core_ct_uint_table_lookup( mbedtls_mpi_uint *dest, } } + +/* Fill X with n_bytes random bytes. + * X must already have room for those bytes. + * The ordering of the bytes returned from the RNG is suitable for + * deterministic ECDSA (see RFC 6979 §3.3 and mbedtls_mpi_core_random()). + * The size and sign of X are unchanged. + * n_bytes must not be 0. + */ +int mbedtls_mpi_core_fill_random( + mbedtls_mpi_uint *X, size_t X_limbs, + size_t n_bytes, + int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + const size_t limbs = CHARS_TO_LIMBS( n_bytes ); + const size_t overhead = ( limbs * ciL ) - n_bytes; + + if( X_limbs < limbs ) + return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); + + memset( X, 0, overhead ); + memset( (unsigned char *) X + limbs * ciL, 0, ( X_limbs - limbs ) * ciL ); + MBEDTLS_MPI_CHK( f_rng( p_rng, (unsigned char *) X + overhead, n_bytes ) ); + mbedtls_mpi_core_bigendian_to_host( X, limbs ); + +cleanup: + return( ret ); +} + #endif /* MBEDTLS_BIGNUM_C */