From f79b425226e1d67359f9f44fb0afc8354dcae9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 2 Jun 2015 15:41:48 +0100 Subject: [PATCH] Avoid in-out length parameter in bignum --- include/mbedtls/bignum.h | 14 ++++++++------ library/bignum.c | 17 ++++++++--------- tests/suites/test_suite_mpi.function | 8 ++++---- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/include/mbedtls/bignum.h b/include/mbedtls/bignum.h index 0dfe22e89..d03b983f1 100644 --- a/include/mbedtls/bignum.h +++ b/include/mbedtls/bignum.h @@ -324,17 +324,19 @@ int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s ); * * \param X Source MPI * \param radix Output numeric base - * \param s String buffer - * \param slen String buffer size + * \param buf Buffer to write the string to + * \param buflen Length of buf + * \param olen Length of the string written, including final NUL byte * * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code. - * *slen is always updated to reflect the amount + * *olen is always updated to reflect the amount * of data that has (or would have) been written. * - * \note Call this function with *slen = 0 to obtain the - * minimum required buffer size in *slen. + * \note Call this function with buflen = 0 to obtain the + * minimum required buffer size in *olen. */ -int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, char *s, size_t *slen ); +int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, + char *buf, size_t buflen, size_t *olen ); #if defined(MBEDTLS_FS_IO) /** diff --git a/library/bignum.c b/library/bignum.c index f94bf5790..563e67a54 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -490,7 +490,8 @@ cleanup: /* * Export into an ASCII string */ -int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, char *s, size_t *slen ) +int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, + char *buf, size_t buflen, size_t *olen ) { int ret = 0; size_t n; @@ -505,13 +506,13 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, char *s, size_t * if( radix >= 16 ) n >>= 1; n += 3; - if( *slen < n ) + if( buflen < n ) { - *slen = n; + *olen = n; return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); } - p = s; + p = buf; mbedtls_mpi_init( &T ); if( X->s == -1 ) @@ -548,7 +549,7 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, char *s, size_t * } *p++ = '\0'; - *slen = p - s; + *olen = p - buf; cleanup: @@ -604,11 +605,9 @@ int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE */ char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ]; - n = sizeof( s ); - memset( s, 0, n ); - n -= 2; + memset( s, 0, sizeof( s ) ); - MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, (size_t *) &n ) ); + MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, sizeof( s ) - 2, &n ) ); if( p == NULL ) p = ""; diff --git a/tests/suites/test_suite_mpi.function b/tests/suites/test_suite_mpi.function index 134d22046..2d68f8d57 100644 --- a/tests/suites/test_suite_mpi.function +++ b/tests/suites/test_suite_mpi.function @@ -33,14 +33,14 @@ void mpi_read_write_string( int radix_X, char *input_X, int radix_A, { mbedtls_mpi X; char str[1000]; - size_t len = output_size; + size_t len; mbedtls_mpi_init( &X ); TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read ); if( result_read == 0 ) { - TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, &len ) == result_write ); + TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write ); if( result_write == 0 ) { TEST_ASSERT( strcasecmp( str, input_A ) == 0 ); @@ -58,7 +58,7 @@ void mbedtls_mpi_read_binary( char *input_X, int radix_A, char *input_A ) mbedtls_mpi X; unsigned char str[1000]; unsigned char buf[1000]; - size_t len = 1000; + size_t len; size_t input_len; mbedtls_mpi_init( &X ); @@ -66,7 +66,7 @@ void mbedtls_mpi_read_binary( char *input_X, int radix_A, char *input_A ) input_len = unhexify( buf, input_X ); TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf, input_len ) == 0 ); - TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, &len ) == 0 ); + TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, (char *) str, sizeof( str ), &len ) == 0 ); TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 ); exit: