Merge remote-tracking branch 'origin/pr/2214' into development

This commit is contained in:
Jaeden Amero 2019-01-30 15:08:25 +00:00
commit 91af329a55
2 changed files with 27 additions and 13 deletions

View file

@ -17,6 +17,8 @@ Bugfix
build error. Fixed by Haijun Gu #2319. build error. Fixed by Haijun Gu #2319.
* Fix signed-to-unsigned integer conversion warning * Fix signed-to-unsigned integer conversion warning
in X.509 module. Fixes #2212. in X.509 module. Fixes #2212.
* Reduce stack usage of `mpi_write_hlp()` by eliminating recursion.
Fixes #2190.
Changes Changes
* Include configuration file in all header files that use configuration, * Include configuration file in all header files that use configuration,

View file

@ -527,26 +527,38 @@ cleanup:
} }
/* /*
* Helper to write the digits high-order first * Helper to write the digits high-order first.
*/ */
static int mpi_write_hlp( mbedtls_mpi *X, int radix, char **p ) static int mpi_write_hlp( mbedtls_mpi *X, int radix,
char **p, const size_t buflen )
{ {
int ret; int ret;
mbedtls_mpi_uint r; mbedtls_mpi_uint r;
size_t length = 0;
char *p_end = *p + buflen;
if( radix < 2 || radix > 16 ) do
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); {
if( length >= buflen )
{
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
}
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) ); MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
/*
if( mbedtls_mpi_cmp_int( X, 0 ) != 0 ) * Write the residue in the current position, as an ASCII character.
MBEDTLS_MPI_CHK( mpi_write_hlp( X, radix, p ) ); */
if( r < 0xA )
if( r < 10 ) *(--p_end) = (char)( '0' + r );
*(*p)++ = (char)( r + 0x30 );
else else
*(*p)++ = (char)( r + 0x37 ); *(--p_end) = (char)( 'A' + ( r - 0xA ) );
length++;
} while( mbedtls_mpi_cmp_int( X, 0 ) != 0 );
memmove( *p, p_end, length );
*p += length;
cleanup: cleanup:
@ -619,7 +631,7 @@ int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
if( T.s == -1 ) if( T.s == -1 )
T.s = 1; T.s = 1;
MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p ) ); MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p, buflen ) );
} }
*p++ = '\0'; *p++ = '\0';