Bignum Mod: improve documentation and style
Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
parent
0020df9cf9
commit
6eb92c0410
2 changed files with 18 additions and 21 deletions
|
@ -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 )
|
void mbedtls_mpi_mod_residue_release( mbedtls_mpi_mod_residue *r )
|
||||||
{
|
{
|
||||||
if ( r == NULL )
|
if( r == NULL )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
r->limbs = 0;
|
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 )
|
void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m )
|
||||||
{
|
{
|
||||||
if ( m == NULL )
|
if( m == NULL )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m->p = NULL;
|
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 )
|
void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
|
||||||
{
|
{
|
||||||
if ( m == NULL )
|
if( m == NULL )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
switch( m->int_rep )
|
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( &N );
|
||||||
mbedtls_mpi_init( &RR );
|
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;
|
goto cleanup;
|
||||||
|
|
||||||
if ( mbedtls_mpi_grow( &N, limbs ) )
|
if( mbedtls_mpi_grow( &N, limbs ) )
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
memcpy( N.p, A, sizeof(mbedtls_mpi_uint) * limbs );
|
memcpy( N.p, A, sizeof(mbedtls_mpi_uint) * limbs );
|
||||||
|
|
||||||
ret = mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N);
|
ret = mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N);
|
||||||
|
|
||||||
if ( ret == 0 )
|
if( ret == 0 )
|
||||||
{
|
{
|
||||||
*X = RR.p;
|
*X = RR.p;
|
||||||
RR.p = NULL;
|
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 */
|
/* 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;
|
goto cleanup;
|
||||||
if ( r->limbs != m->limbs )
|
if( r->limbs != m->limbs )
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep );
|
ret = mbedtls_mpi_mod_raw_read( r->p, m, buf, buflen, ext_rep );
|
||||||
|
|
||||||
if( ret != 0 )
|
if( ret != 0 )
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
r->limbs = m->limbs;
|
r->limbs = m->limbs;
|
||||||
|
|
||||||
if (m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY)
|
if( m->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY )
|
||||||
ret = mbedtls_mpi_mod_raw_to_mont_rep(r->p, m);
|
ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
return ( ret );
|
return ( ret );
|
||||||
|
@ -234,12 +233,12 @@ int mbedtls_mpi_mod_write( const mbedtls_mpi_mod_residue *r,
|
||||||
int conv_ret = 0;
|
int conv_ret = 0;
|
||||||
|
|
||||||
/* Do our best to check if r and m have been set up */
|
/* 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;
|
goto cleanup;
|
||||||
if ( r->limbs != m->limbs )
|
if( r->limbs != m->limbs )
|
||||||
goto cleanup;
|
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 );
|
conv_ret = mbedtls_mpi_mod_raw_from_mont_rep( r->p, m );
|
||||||
if( conv_ret != 0 )
|
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 );
|
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 );
|
conv_ret = mbedtls_mpi_mod_raw_to_mont_rep( r->p, m );
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
|
|
||||||
if ( ret == 0 )
|
if( ret == 0 )
|
||||||
ret = conv_ret;
|
ret = conv_ret;
|
||||||
|
|
||||||
return ( ret );
|
return ( ret );
|
||||||
|
|
|
@ -82,9 +82,7 @@ typedef struct {
|
||||||
* and interpreted according to the value of the `m->int_rep` field.
|
* 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
|
* 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
|
* should only be used in operations where the modulus is \p m.
|
||||||
* equivalent to \p m (in the sense that all their fields or memory pointed to by
|
|
||||||
* their fields hold the same value).
|
|
||||||
*
|
*
|
||||||
* \param[out] r The address of the residue to setup.
|
* \param[out] r The address of the residue to setup.
|
||||||
* \param[in] m The address of the modulus related to \p r.
|
* \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
|
* pointed to by `m->p`) and already in the representation
|
||||||
* indicated by `m->int_rep`.
|
* indicated by `m->int_rep`.
|
||||||
* \param p_limbs The number of limbs of \p p. Must be the same as the number
|
* \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 \c 0 if successful.
|
||||||
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
|
* \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.
|
* based on the value of `m->int_rep` field.
|
||||||
*
|
*
|
||||||
* \warning If the buffer is smaller than `m->bits`, the number of
|
* \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
|
* secret, the caller must ensure that \p buflen is at least
|
||||||
* (`m->bits`+7)/8.
|
* (`m->bits`+7)/8.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue