Avoid in-out length parameter in bignum

This commit is contained in:
Manuel Pégourié-Gonnard 2015-06-02 15:41:48 +01:00
parent 9693668c23
commit f79b425226
3 changed files with 20 additions and 19 deletions

View File

@ -324,17 +324,19 @@ int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
* *
* \param X Source MPI * \param X Source MPI
* \param radix Output numeric base * \param radix Output numeric base
* \param s String buffer * \param buf Buffer to write the string to
* \param slen String buffer size * \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. * \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. * of data that has (or would have) been written.
* *
* \note Call this function with *slen = 0 to obtain the * \note Call this function with buflen = 0 to obtain the
* minimum required buffer size in *slen. * 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) #if defined(MBEDTLS_FS_IO)
/** /**

View File

@ -490,7 +490,8 @@ cleanup:
/* /*
* Export into an ASCII string * 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; int ret = 0;
size_t n; 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; if( radix >= 16 ) n >>= 1;
n += 3; n += 3;
if( *slen < n ) if( buflen < n )
{ {
*slen = n; *olen = n;
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL ); return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
} }
p = s; p = buf;
mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T );
if( X->s == -1 ) 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'; *p++ = '\0';
*slen = p - s; *olen = p - buf;
cleanup: 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 ]; char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
n = sizeof( s ); memset( s, 0, sizeof( s ) );
memset( s, 0, n );
n -= 2;
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 = ""; if( p == NULL ) p = "";

View File

@ -33,14 +33,14 @@ void mpi_read_write_string( int radix_X, char *input_X, int radix_A,
{ {
mbedtls_mpi X; mbedtls_mpi X;
char str[1000]; char str[1000];
size_t len = output_size; size_t len;
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X );
TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read ); TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
if( result_read == 0 ) 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 ) if( result_write == 0 )
{ {
TEST_ASSERT( strcasecmp( str, input_A ) == 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; mbedtls_mpi X;
unsigned char str[1000]; unsigned char str[1000];
unsigned char buf[1000]; unsigned char buf[1000];
size_t len = 1000; size_t len;
size_t input_len; size_t input_len;
mbedtls_mpi_init( &X ); 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 ); input_len = unhexify( buf, input_X );
TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf, input_len ) == 0 ); 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 ); TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
exit: exit: