Skip to content

Commit d19a193

Browse files
committed
Fix code review comments:
1. variable name accoriding to the Mbed TLS coding style; 2. add a comment explaining safety of the optimization; 3. safer T2 initialization and memory zeroing on the function exit;
1 parent 35d6d46 commit d19a193

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

library/bignum.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,7 @@ int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
16321632
int ret;
16331633
size_t i, n, t, k;
16341634
mbedtls_mpi X, Y, Z, T1, T2;
1635-
mbedtls_mpi_uint __tp2[3];
1635+
mbedtls_mpi_uint TP2[3];
16361636
MPI_VALIDATE_RET( A != NULL );
16371637
MPI_VALIDATE_RET( B != NULL );
16381638

@@ -1641,10 +1641,16 @@ int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
16411641

16421642
mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
16431643
mbedtls_mpi_init( &T1 );
1644-
/* Avoid dynamic memory allocations for constant-size T2. */
1644+
/*
1645+
* Avoid dynamic memory allocations for constant-size T2.
1646+
*
1647+
* T2 is used for comparison only and the 3 limbs are assigned explicitly,
1648+
* so nobody increase the size of the MPI and we're safe to use an on-stack
1649+
* buffer.
1650+
*/
16451651
T2.s = 1;
1646-
T2.n = 3;
1647-
T2.p = __tp2;
1652+
T2.n = sizeof( TP2 ) / sizeof( *TP2 );
1653+
T2.p = TP2;
16481654

16491655
if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
16501656
{
@@ -1740,6 +1746,7 @@ int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
17401746

17411747
mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
17421748
mbedtls_mpi_free( &T1 );
1749+
mbedtls_platform_zeroize( TP2, sizeof( TP2 ) );
17431750

17441751
return( ret );
17451752
}

0 commit comments

Comments
 (0)