Avoid in-out length parameter in bignum
diff --git a/include/mbedtls/bignum.h b/include/mbedtls/bignum.h
index 0dfe22e..d03b983 100644
--- a/include/mbedtls/bignum.h
+++ b/include/mbedtls/bignum.h
@@ -324,17 +324,19 @@
*
* \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 f94bf57..563e67a 100644
--- a/library/bignum.c
+++ b/library/bignum.c
@@ -490,7 +490,8 @@
/*
* 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 @@
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 @@
}
*p++ = '\0';
- *slen = p - s;
+ *olen = p - buf;
cleanup:
@@ -604,11 +605,9 @@
*/
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 134d220..2d68f8d 100644
--- a/tests/suites/test_suite_mpi.function
+++ b/tests/suites/test_suite_mpi.function
@@ -33,14 +33,14 @@
{
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 @@
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 @@
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: