Bignum Mod: improve documentation and style

Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
Janos Follath 2022-11-26 17:34:37 +00:00
parent 0020df9cf9
commit 6eb92c0410
2 changed files with 18 additions and 21 deletions

View file

@ -50,7 +50,7 @@ int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
{
if ( r == NULL )
if( r == NULL )
return;
r->limbs = 0;
@ -59,7 +59,7 @@ void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
{
if ( m == NULL )
if( m == NULL )
return;
m->p = NULL;
@ -70,7 +70,7 @@ void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
{
if ( m == NULL )
if( m == NULL )
return;
switch( m->int_rep )
@ -110,17 +110,17 @@ static int set_mont_const_square( const mbedtls_mpi_uint **X,
mbedtls_mpi_init( &N );
mbedtls_mpi_init( &RR );
if ( A == NULL || limbs == 0 || limbs >= ( MBEDTLS_MPI_MAX_LIMBS / 2 ) - 2 )
if( A == NULL || limbs == 0 || limbs >= ( MBEDTLS_MPI_MAX_LIMBS / 2 ) - 2 )
goto cleanup;
if ( mbedtls_mpi_grow( &N, limbs ) )
if( mbedtls_mpi_grow( &N, limbs ) )
goto cleanup;
memcpy( N.p, A, sizeof(mbedtls_mpi_uint) * limbs );
ret = mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N);
if ( ret == 0 )
if( ret == 0 )
{
*X = RR.p;
RR.p = NULL;
@ -205,20 +205,19 @@ int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
/* Do our best to check if r and m have been set up */
if ( r->limbs == 0 || m->limbs == 0 )
if( r->limbs == 0 || m->limbs == 0 )
goto cleanup;
if ( r->limbs != m->limbs )
if( r->limbs != m->limbs )
goto cleanup;
ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep );
if( ret != 0 )
goto cleanup;
r->limbs = m->limbs;
if (m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)
ret = mbedtls_mpi_mod_raw_to_mont_rep(r->p, m);
if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
cleanup:
return ( ret );
@ -234,12 +233,12 @@ int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
int conv_ret = 0;
/* Do our best to check if r and m have been set up */
if ( r->limbs == 0 || m->limbs == 0 )
if( r->limbs == 0 || m->limbs == 0 )
goto cleanup;
if ( r->limbs != m->limbs )
if( r->limbs != m->limbs )
goto cleanup;
if ( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
{
conv_ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
if( conv_ret != 0 )
@ -248,12 +247,12 @@ int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
ret = mbedtls_mpi_mod_raw_write( r->p, m, buf, buflen, ext_rep );
if ( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
conv_ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
cleanup:
if ( ret == 0 )
if( ret == 0 )
ret = conv_ret;
return ( ret );

View file

@ -82,9 +82,7 @@ typedef struct {
* and interpreted according to the value of the `m->int_rep` field.
*
* The modulus \p m will be the modulus associated with \p r. The residue \p r
* should only be used in operations where the modulus is \p m or a modulus
* equivalent to \p m (in the sense that all their fields or memory pointed to by
* their fields hold the same value).
* should only be used in operations where the modulus is \p m.
*
* \param[out] r The address of the residue to setup.
* \param[in] m The address of the modulus related to \p r.
@ -96,7 +94,7 @@ typedef struct {
* pointed to by `m->p`) and already in the representation
* indicated by `m->int_rep`.
* \param p_limbs The number of limbs of \p p. Must be the same as the number
* of limbs in the modulus \p m.)
* of limbs in the modulus \p m.
*
* \return \c 0 if successful.
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
@ -219,7 +217,7 @@ int mbedtls_mpi_mod_read( mbedtls_mpi_mod_residue *r,
* based on the value of `m->int_rep` field.
*
* \warning If the buffer is smaller than `m->bits`, the number of
* leading zeroes is leaked through side channels. If \p r is
* leading zeroes is leaked through timing. If \p r is
* secret, the caller must ensure that \p buflen is at least
* (`m->bits`+7)/8.
*