Bignum: Apply naming conventions
Numbers: - A, B for mbedtls_mpi_uint* operands - a, b for mbedtls_mpi_uint operands - X or x for result - HAC references where applicable Lengths: - Reserve size or length for length/size in bytes or byte buffers. - For length of mbedtls_mpi_uint* buffers use limbs - Length parameters are qualified if possible (eg. input_length or a_limbs) Setup functions: - The parameters match the corresponding structure member's name - The structure to set up is a standard lower case name even if in other functions different naming conventions would apply Scope of changes/conventions: - bignum_core - bignum_mod - bignum_mod_raw Signed-off-by: Janos Follath <janos.follath@arm.com>
This commit is contained in:
parent
6b8a4ad0d8
commit
b7a88eca42
8 changed files with 185 additions and 179 deletions
|
@ -38,14 +38,14 @@
|
|||
|
||||
#include "bignum_core.h"
|
||||
|
||||
size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x )
|
||||
size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint a )
|
||||
{
|
||||
size_t j;
|
||||
mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
|
||||
|
||||
for( j = 0; j < biL; j++ )
|
||||
{
|
||||
if( x & mask ) break;
|
||||
if( a & mask ) break;
|
||||
|
||||
mask >>= 1;
|
||||
}
|
||||
|
@ -53,46 +53,46 @@ size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x )
|
|||
return( j );
|
||||
}
|
||||
|
||||
size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx )
|
||||
size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t A_limbs )
|
||||
{
|
||||
size_t i, j;
|
||||
|
||||
if( nx == 0 )
|
||||
if( A_limbs == 0 )
|
||||
return( 0 );
|
||||
|
||||
for( i = nx - 1; i > 0; i-- )
|
||||
if( X[i] != 0 )
|
||||
for( i = A_limbs - 1; i > 0; i-- )
|
||||
if( A[i] != 0 )
|
||||
break;
|
||||
|
||||
j = biL - mbedtls_mpi_core_clz( X[i] );
|
||||
j = biL - mbedtls_mpi_core_clz( A[i] );
|
||||
|
||||
return( ( i * biL ) + j );
|
||||
}
|
||||
|
||||
/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
|
||||
* into the storage form used by mbedtls_mpi. */
|
||||
static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint x )
|
||||
static mbedtls_mpi_uint mpi_bigendian_to_host_c( mbedtls_mpi_uint a )
|
||||
{
|
||||
uint8_t i;
|
||||
unsigned char *x_ptr;
|
||||
unsigned char *a_ptr;
|
||||
mbedtls_mpi_uint tmp = 0;
|
||||
|
||||
for( i = 0, x_ptr = (unsigned char *) &x; i < ciL; i++, x_ptr++ )
|
||||
for( i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++ )
|
||||
{
|
||||
tmp <<= CHAR_BIT;
|
||||
tmp |= (mbedtls_mpi_uint) *x_ptr;
|
||||
tmp |= (mbedtls_mpi_uint) *a_ptr;
|
||||
}
|
||||
|
||||
return( tmp );
|
||||
}
|
||||
|
||||
static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
|
||||
static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint a )
|
||||
{
|
||||
#if defined(__BYTE_ORDER__)
|
||||
|
||||
/* Nothing to do on bigendian systems. */
|
||||
#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
|
||||
return( x );
|
||||
return( a );
|
||||
#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
|
||||
|
||||
#if ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
|
||||
|
@ -116,9 +116,9 @@ static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
|
|||
switch( sizeof(mbedtls_mpi_uint) )
|
||||
{
|
||||
case 4:
|
||||
return( __builtin_bswap32(x) );
|
||||
return( __builtin_bswap32(a) );
|
||||
case 8:
|
||||
return( __builtin_bswap64(x) );
|
||||
return( __builtin_bswap64(a) );
|
||||
}
|
||||
#endif
|
||||
#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
|
||||
|
@ -126,10 +126,10 @@ static mbedtls_mpi_uint mpi_bigendian_to_host( mbedtls_mpi_uint x )
|
|||
|
||||
/* Fall back to C-based reordering if we don't know the byte order
|
||||
* or we couldn't use a compiler-specific builtin. */
|
||||
return( mpi_bigendian_to_host_c( x ) );
|
||||
return( mpi_bigendian_to_host_c( a ) );
|
||||
}
|
||||
|
||||
void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *X,
|
||||
void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
|
||||
size_t limbs )
|
||||
{
|
||||
mbedtls_mpi_uint *cur_limb_left;
|
||||
|
@ -146,7 +146,7 @@ void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *X,
|
|||
* than the right index (it's not a problem if limbs is odd and the
|
||||
* indices coincide in the last iteration).
|
||||
*/
|
||||
for( cur_limb_left = X, cur_limb_right = X + ( limbs - 1 );
|
||||
for( cur_limb_left = A, cur_limb_right = A + ( limbs - 1 );
|
||||
cur_limb_left <= cur_limb_right;
|
||||
cur_limb_left++, cur_limb_right-- )
|
||||
{
|
||||
|
@ -160,120 +160,121 @@ void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *X,
|
|||
}
|
||||
|
||||
int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
|
||||
size_t nx,
|
||||
const unsigned char *buf,
|
||||
size_t buflen )
|
||||
size_t X_limbs,
|
||||
const unsigned char *input,
|
||||
size_t input_length )
|
||||
{
|
||||
const size_t limbs = CHARS_TO_LIMBS( buflen );
|
||||
const size_t limbs = CHARS_TO_LIMBS( input_length );
|
||||
|
||||
if( nx < limbs )
|
||||
if( X_limbs < limbs )
|
||||
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
||||
|
||||
if( X != NULL )
|
||||
{
|
||||
memset( X, 0, nx * ciL );
|
||||
memset( X, 0, X_limbs * ciL );
|
||||
|
||||
for( size_t i = 0; i < buflen; i++ )
|
||||
X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
|
||||
for( size_t i = 0; i < input_length; i++ )
|
||||
X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << ((i % ciL) << 3);
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
|
||||
size_t nx,
|
||||
const unsigned char *buf,
|
||||
size_t buflen )
|
||||
size_t X_limbs,
|
||||
const unsigned char *input,
|
||||
size_t input_length )
|
||||
{
|
||||
const size_t limbs = CHARS_TO_LIMBS( buflen );
|
||||
const size_t limbs = CHARS_TO_LIMBS( input_length );
|
||||
|
||||
if( nx < limbs )
|
||||
if( X_limbs < limbs )
|
||||
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
||||
|
||||
/* If nx is 0, buflen must also be 0 (from previous test). Nothing to do. */
|
||||
if( nx == 0 )
|
||||
/* If X_limbs is 0, input_length must also be 0 (from previous test).
|
||||
* Nothing to do. */
|
||||
if( X_limbs == 0 )
|
||||
return( 0 );
|
||||
|
||||
memset( X, 0, nx * ciL );
|
||||
memset( X, 0, X_limbs * ciL );
|
||||
|
||||
/* memcpy() with (NULL, 0) is undefined behaviour */
|
||||
if( buflen != 0 )
|
||||
if( input_length != 0 )
|
||||
{
|
||||
size_t overhead = ( nx * ciL ) - buflen;
|
||||
size_t overhead = ( X_limbs * ciL ) - input_length;
|
||||
unsigned char *Xp = (unsigned char *) X;
|
||||
memcpy( Xp + overhead, buf, buflen );
|
||||
memcpy( Xp + overhead, input, input_length );
|
||||
}
|
||||
|
||||
mbedtls_mpi_core_bigendian_to_host( X, nx );
|
||||
mbedtls_mpi_core_bigendian_to_host( X, X_limbs );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
|
||||
size_t nx,
|
||||
unsigned char *buf,
|
||||
size_t buflen )
|
||||
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
|
||||
size_t A_limbs,
|
||||
unsigned char *output,
|
||||
size_t output_length )
|
||||
{
|
||||
size_t stored_bytes = nx * ciL;
|
||||
size_t stored_bytes = A_limbs * ciL;
|
||||
size_t bytes_to_copy;
|
||||
|
||||
if( stored_bytes < buflen )
|
||||
if( stored_bytes < output_length )
|
||||
{
|
||||
bytes_to_copy = stored_bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
bytes_to_copy = buflen;
|
||||
bytes_to_copy = output_length;
|
||||
|
||||
/* The output buffer is smaller than the allocated size of X.
|
||||
* However X may fit if its leading bytes are zero. */
|
||||
/* The output outputfer is smaller than the allocated size of A.
|
||||
* However A may fit if its leading bytes are zero. */
|
||||
for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
|
||||
{
|
||||
if( GET_BYTE( X, i ) != 0 )
|
||||
if( GET_BYTE( A, i ) != 0 )
|
||||
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
||||
}
|
||||
}
|
||||
|
||||
for( size_t i = 0; i < bytes_to_copy; i++ )
|
||||
buf[i] = GET_BYTE( X, i );
|
||||
output[i] = GET_BYTE( A, i );
|
||||
|
||||
if( stored_bytes < buflen )
|
||||
if( stored_bytes < output_length )
|
||||
{
|
||||
/* Write trailing 0 bytes */
|
||||
memset( buf + stored_bytes, 0, buflen - stored_bytes );
|
||||
memset( output + stored_bytes, 0, output_length - stored_bytes );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
|
||||
size_t nx,
|
||||
unsigned char *buf,
|
||||
size_t buflen )
|
||||
size_t X_limbs,
|
||||
unsigned char *output,
|
||||
size_t output_length )
|
||||
{
|
||||
size_t stored_bytes;
|
||||
size_t bytes_to_copy;
|
||||
unsigned char *p;
|
||||
|
||||
stored_bytes = nx * ciL;
|
||||
stored_bytes = X_limbs * ciL;
|
||||
|
||||
if( stored_bytes < buflen )
|
||||
if( stored_bytes < output_length )
|
||||
{
|
||||
/* There is enough space in the output buffer. Write initial
|
||||
/* There is enough space in the output outputfer. Write initial
|
||||
* null bytes and record the position at which to start
|
||||
* writing the significant bytes. In this case, the execution
|
||||
* trace of this function does not depend on the value of the
|
||||
* number. */
|
||||
bytes_to_copy = stored_bytes;
|
||||
p = buf + buflen - stored_bytes;
|
||||
memset( buf, 0, buflen - stored_bytes );
|
||||
p = output + output_length - stored_bytes;
|
||||
memset( output, 0, output_length - stored_bytes );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* The output buffer is smaller than the allocated size of X.
|
||||
/* The output outputfer is smaller than the allocated size of X.
|
||||
* However X may fit if its leading bytes are zero. */
|
||||
bytes_to_copy = buflen;
|
||||
p = buf;
|
||||
bytes_to_copy = output_length;
|
||||
p = output;
|
||||
for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
|
||||
{
|
||||
if( GET_BYTE( X, i ) != 0 )
|
||||
|
|
|
@ -33,31 +33,31 @@
|
|||
|
||||
/** Count leading zero bits in a given integer.
|
||||
*
|
||||
* \param x Integer to count leading zero bits.
|
||||
* \param a Integer to count leading zero bits.
|
||||
*
|
||||
* \return The number of leading zero bits in \p x.
|
||||
* \return The number of leading zero bits in \p a.
|
||||
*/
|
||||
size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x );
|
||||
size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint a );
|
||||
|
||||
/** Return the the minimum number of bits required to represent the value held
|
||||
* in the MPI.
|
||||
*
|
||||
* \note This function returns 0 if all the limbs of \p X are 0.
|
||||
* \note This function returns 0 if all the limbs of \p A are 0.
|
||||
*
|
||||
* \param[in] X The address of the MPI.
|
||||
* \param nx The number of limbs of \p X.
|
||||
* \param[in] A The address of the MPI.
|
||||
* \param a_limbs The number of limbs of \p A.
|
||||
*
|
||||
* \return The number of bits in \p X.
|
||||
* \return The number of bits in \p A.
|
||||
*/
|
||||
size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *X, size_t nx );
|
||||
size_t mbedtls_mpi_core_bitlen( const mbedtls_mpi_uint *A, size_t a_limbs );
|
||||
|
||||
/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
|
||||
* into the storage form used by mbedtls_mpi.
|
||||
*
|
||||
* \param[in,out] X The address of the MPI.
|
||||
* \param limbs The number of limbs of \p X.
|
||||
* \param[in,out] A The address of the MPI.
|
||||
* \param limbs The number of limbs of \p A.
|
||||
*/
|
||||
void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *X,
|
||||
void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *A,
|
||||
size_t limbs );
|
||||
|
||||
/** Import X from unsigned binary data, little endian.
|
||||
|
@ -65,80 +65,81 @@ void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *X,
|
|||
* The MPI needs to have enough limbs to store the full value (including any
|
||||
* most significant zero bytes in the input).
|
||||
*
|
||||
* \param[out] X The address of the MPI.
|
||||
* \param nx The number of limbs of \p X.
|
||||
* \param[in] buf The input buffer to import from.
|
||||
* \param buflen The length in bytes of \p buf.
|
||||
* \param[out] X The address of the MPI.
|
||||
* \param X_limbs The number of limbs of \p X.
|
||||
* \param[in] input The input buffer to import from.
|
||||
* \param input_length The length bytes of \p input.
|
||||
*
|
||||
* \return \c 0 if successful.
|
||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
|
||||
* large enough to hold the value in \p buf.
|
||||
* large enough to hold the value in \p input.
|
||||
*/
|
||||
int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
|
||||
size_t nx,
|
||||
const unsigned char *buf,
|
||||
size_t buflen );
|
||||
size_t X_limbs,
|
||||
const unsigned char *input,
|
||||
size_t input_length );
|
||||
|
||||
/** Import X from unsigned binary data, big endian.
|
||||
*
|
||||
* The MPI needs to have enough limbs to store the full value (including any
|
||||
* most significant zero bytes in the input).
|
||||
*
|
||||
* \param[out] X The address of the MPI.
|
||||
* May only be #NULL if \nx is 0 and \p buflen is 0.
|
||||
* \param nx The number of limbs of \p X.
|
||||
* \param[in] buf The input buffer to import from.
|
||||
* May only be #NULL if \p buflen is 0.
|
||||
* \param buflen The length in bytes of \p buf.
|
||||
* \param[out] X The address of the MPI.
|
||||
* May only be #NULL if \X_limbs is 0 and \p input_length
|
||||
* is 0.
|
||||
* \param X_limbs The number of limbs of \p X.
|
||||
* \param[in] input The input buffer to import from.
|
||||
* May only be #NULL if \p input_length is 0.
|
||||
* \param input_length The length in bytes of \p input.
|
||||
*
|
||||
* \return \c 0 if successful.
|
||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
|
||||
* large enough to hold the value in \p buf.
|
||||
* large enough to hold the value in \p input.
|
||||
*/
|
||||
int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
|
||||
size_t nx,
|
||||
const unsigned char *buf,
|
||||
size_t buflen );
|
||||
size_t X_limbs,
|
||||
const unsigned char *input,
|
||||
size_t input_length );
|
||||
|
||||
/** Export X into unsigned binary data, little endian.
|
||||
/** Export A into unsigned binary data, little endian.
|
||||
*
|
||||
* \note If \p buf is shorter than \p X the export is still successful if the
|
||||
* value held in \p X fits in the buffer (that is, if enough of the most
|
||||
* significant bytes of \p X are 0).
|
||||
* \note If \p output is shorter than \p A the export is still successful if the
|
||||
* value held in \p A fits in the buffer (that is, if enough of the most
|
||||
* significant bytes of \p A are 0).
|
||||
*
|
||||
* \param[in] X The address of the MPI.
|
||||
* \param nx The number of limbs of \p X.
|
||||
* \param[out] buf The output buffer to export to.
|
||||
* \param buflen The length in bytes of \p buf.
|
||||
* \param[in] A The address of the MPI.
|
||||
* \param A_limbs The number of limbs of \p A.
|
||||
* \param[out] output The output buffer to export to.
|
||||
* \param output_length The length in bytes of \p output.
|
||||
*
|
||||
* \return \c 0 if successful.
|
||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
|
||||
* large enough to hold the value of \p X.
|
||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
|
||||
* large enough to hold the value of \p A.
|
||||
*/
|
||||
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
|
||||
size_t nx,
|
||||
unsigned char *buf,
|
||||
size_t buflen );
|
||||
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
|
||||
size_t A_limbs,
|
||||
unsigned char *output,
|
||||
size_t output_length );
|
||||
|
||||
/** Export X into unsigned binary data, big endian.
|
||||
/** Export A into unsigned binary data, big endian.
|
||||
*
|
||||
* \note If \p buf is shorter than \p X the export is still successful if the
|
||||
* value held in \p X fits in the buffer (that is, if enough of the most
|
||||
* significant bytes of \p X are 0).
|
||||
* \note If \p output is shorter than \p A the export is still successful if the
|
||||
* value held in \p A fits in the buffer (that is, if enough of the most
|
||||
* significant bytes of \p A are 0).
|
||||
*
|
||||
* \param[in] X The address of the MPI.
|
||||
* \param nx The number of limbs of \p X.
|
||||
* \param[out] buf The output buffer to export to.
|
||||
* \param buflen The length in bytes of \p buf.
|
||||
* \param[in] A The address of the MPI.
|
||||
* \param A_limbs The number of limbs of \p A.
|
||||
* \param[out] output The output buffer to export to.
|
||||
* \param output_length The length in bytes of \p output.
|
||||
*
|
||||
* \return \c 0 if successful.
|
||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
|
||||
* large enough to hold the value of \p X.
|
||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
|
||||
* large enough to hold the value of \p A.
|
||||
*/
|
||||
int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
|
||||
size_t nx,
|
||||
unsigned char *buf,
|
||||
size_t buflen );
|
||||
int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
|
||||
size_t A_limbs,
|
||||
unsigned char *output,
|
||||
size_t output_length );
|
||||
|
||||
#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
|
||||
#define biL (ciL << 3) /* bits in limb */
|
||||
|
|
|
@ -45,9 +45,9 @@
|
|||
int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
|
||||
const mbedtls_mpi_mod_modulus *m,
|
||||
mbedtls_mpi_uint *p,
|
||||
size_t pn )
|
||||
size_t p_limbs )
|
||||
{
|
||||
if( pn < m->limbs || !mbedtls_mpi_core_lt_ct( m->p, p, pn ) )
|
||||
if( p_limbs < m->limbs || !mbedtls_mpi_core_lt_ct( m->p, p, p_limbs ) )
|
||||
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
||||
|
||||
r->limbs = m->limbs;
|
||||
|
@ -103,15 +103,15 @@ void mbedtls_mpi_mod_modulus_free( mbedtls_mpi_mod_modulus *m )
|
|||
|
||||
int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
||||
const mbedtls_mpi_uint *p,
|
||||
size_t pn,
|
||||
size_t p_limbs,
|
||||
mbedtls_mpi_mod_ext_rep ext_rep,
|
||||
mbedtls_mpi_mod_rep_selector int_rep )
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
m->p = p;
|
||||
m->limbs = pn;
|
||||
m->bits = mbedtls_mpi_core_bitlen( p, pn );
|
||||
m->limbs = p_limbs;
|
||||
m->bits = mbedtls_mpi_core_bitlen( p, p_limbs );
|
||||
|
||||
switch( ext_rep )
|
||||
{
|
||||
|
|
|
@ -76,16 +76,16 @@ typedef struct {
|
|||
* The memory pointed to by \p p will be used by \p r and must
|
||||
* not be modified in any way until after
|
||||
* mbedtls_mpi_mod_residue_release() is called.
|
||||
* \param pn The number of limbs of \p p.
|
||||
* \param p_limbs The number of limbs of \p p.
|
||||
*
|
||||
* \return \c 0 if successful.
|
||||
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p pn is less than the limbs
|
||||
* in \p m or if \p p is not less than \p m.
|
||||
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
|
||||
* limbs in \p m or if \p p is not less than \p m.
|
||||
*/
|
||||
int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
|
||||
const mbedtls_mpi_mod_modulus *m,
|
||||
mbedtls_mpi_uint *p,
|
||||
size_t pn );
|
||||
size_t p_limbs );
|
||||
|
||||
/** Unbind elements of a residue structure.
|
||||
*
|
||||
|
@ -112,7 +112,7 @@ void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
|
|||
* The memory pointed to by \p p will be used by \p m and must
|
||||
* not be modified in any way until after
|
||||
* mbedtls_mpi_mod_modulus_free() is called.
|
||||
* \param pn The number of limbs of \p p.
|
||||
* \param p_limbs The number of limbs of \p p.
|
||||
* \param ext_rep The external representation to be used for residues
|
||||
* associated with \p m (see #mbedtls_mpi_mod_ext_rep).
|
||||
* \param int_rep The internal representation to be used for residues
|
||||
|
@ -124,7 +124,7 @@ void mbedtls_mpi_mod_modulus_init( mbedtls_mpi_mod_modulus *m );
|
|||
*/
|
||||
int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
||||
const mbedtls_mpi_uint *p,
|
||||
size_t pn,
|
||||
size_t p_limbs,
|
||||
mbedtls_mpi_mod_ext_rep ext_rep,
|
||||
mbedtls_mpi_mod_rep_selector int_rep );
|
||||
|
||||
|
|
|
@ -43,18 +43,20 @@
|
|||
|
||||
int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
|
||||
const mbedtls_mpi_mod_modulus *m,
|
||||
const unsigned char *buf,
|
||||
size_t buflen )
|
||||
const unsigned char *input,
|
||||
size_t input_lentgth )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
switch( m->ext_rep )
|
||||
{
|
||||
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
||||
ret = mbedtls_mpi_core_read_le( X, m->limbs, buf, buflen );
|
||||
ret = mbedtls_mpi_core_read_le( X, m->limbs,
|
||||
input, input_lentgth );
|
||||
break;
|
||||
case MBEDTLS_MPI_MOD_EXT_REP_BE:
|
||||
ret = mbedtls_mpi_core_read_be( X, m->limbs, buf, buflen );
|
||||
ret = mbedtls_mpi_core_read_be( X, m->limbs,
|
||||
input, input_lentgth );
|
||||
break;
|
||||
default:
|
||||
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
||||
|
@ -74,17 +76,19 @@ cleanup:
|
|||
return( ret );
|
||||
}
|
||||
|
||||
int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *X,
|
||||
int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
|
||||
const mbedtls_mpi_mod_modulus *m,
|
||||
unsigned char *buf,
|
||||
size_t buflen )
|
||||
unsigned char *output,
|
||||
size_t output_length )
|
||||
{
|
||||
switch( m->ext_rep )
|
||||
{
|
||||
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
||||
return( mbedtls_mpi_core_write_le( X, m->limbs, buf, buflen ) );
|
||||
return( mbedtls_mpi_core_write_le( A, m->limbs,
|
||||
output, output_length ) );
|
||||
case MBEDTLS_MPI_MOD_EXT_REP_BE:
|
||||
return( mbedtls_mpi_core_write_be( X, m->limbs, buf, buflen ) );
|
||||
return( mbedtls_mpi_core_write_be( A, m->limbs,
|
||||
output, output_length ) );
|
||||
default:
|
||||
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
||||
}
|
||||
|
|
|
@ -38,42 +38,42 @@
|
|||
* The MPI needs to have enough limbs to store the full value (including any
|
||||
* most significant zero bytes in the input).
|
||||
*
|
||||
* \param[out] X The address of the MPI. The size is determined by \p m. (In
|
||||
* particular, it must have at least as many limbs as the
|
||||
* modulus \p m.)
|
||||
* \param[in] m The address of the modulus related to \p X.
|
||||
* \param[in] buf The input buffer to import from.
|
||||
* \param buflen The length in bytes of \p buf.
|
||||
* \param[out] X The address of the MPI. The size is determined by \p m.
|
||||
* (In particular, it must have at least as many limbs as
|
||||
* the modulus \p m.)
|
||||
* \param[in] m The address of the modulus related to \p X.
|
||||
* \param[in] input The input buffer to import from.
|
||||
* \param input_length The length in bytes of \p input.
|
||||
*
|
||||
* \return \c 0 if successful.
|
||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
|
||||
* large enough to hold the value in \p buf.
|
||||
* large enough to hold the value in \p input.
|
||||
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
|
||||
* of \p m is invalid or \p X is not less than \p m.
|
||||
*/
|
||||
int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
|
||||
const mbedtls_mpi_mod_modulus *m,
|
||||
const unsigned char *buf,
|
||||
size_t buflen );
|
||||
const unsigned char *input,
|
||||
size_t input_length );
|
||||
|
||||
/** Export X into unsigned binary data.
|
||||
/** Export A into unsigned binary data.
|
||||
*
|
||||
* \param[in] X The address of the MPI. The size is determined by \p m. (In
|
||||
* particular, it must have at least as many limbs as the modulus
|
||||
* \p m.)
|
||||
* \param[in] m The address of the modulus related to \p X.
|
||||
* \param[out] buf The output buffer to export to.
|
||||
* \param buflen The length in bytes of \p buf.
|
||||
* \param[in] A The address of the MPI. The size is determined by \p m.
|
||||
* (In particular, it must have at least as many limbs as
|
||||
* the modulus \p m.)
|
||||
* \param[in] m The address of the modulus related to \p A.
|
||||
* \param[out] output The output buffer to export to.
|
||||
* \param output_length The length in bytes of \p output.
|
||||
*
|
||||
* \return \c 0 if successful.
|
||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
|
||||
* large enough to hold the value of \p X.
|
||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
|
||||
* large enough to hold the value of \p A.
|
||||
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
|
||||
* of \p m is invalid.
|
||||
*/
|
||||
int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *X,
|
||||
int mbedtls_mpi_mod_raw_write( const mbedtls_mpi_uint *A,
|
||||
const mbedtls_mpi_mod_modulus *m,
|
||||
unsigned char *buf,
|
||||
size_t buflen );
|
||||
unsigned char *output,
|
||||
size_t output_length );
|
||||
|
||||
#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */
|
||||
|
|
|
@ -744,9 +744,9 @@ cleanup:
|
|||
/*
|
||||
* Compare unsigned values in constant time
|
||||
*/
|
||||
unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *X,
|
||||
const mbedtls_mpi_uint *Y,
|
||||
size_t len )
|
||||
unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *A,
|
||||
const mbedtls_mpi_uint *B,
|
||||
size_t limbs )
|
||||
{
|
||||
unsigned ret, cond, done;
|
||||
|
||||
|
@ -754,31 +754,31 @@ unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *X,
|
|||
* their scope. */
|
||||
ret = cond = done = 0;
|
||||
|
||||
for( size_t i = len; i > 0; i-- )
|
||||
for( size_t i = limbs; i > 0; i-- )
|
||||
{
|
||||
/*
|
||||
* If Y[i - 1] < X[i - 1] then X < Y is false and the result must
|
||||
* If B[i - 1] < A[i - 1] then A < B is false and the result must
|
||||
* remain 0.
|
||||
*
|
||||
* Again even if we can make a decision, we just mark the result and
|
||||
* the fact that we are done and continue looping.
|
||||
*/
|
||||
cond = mbedtls_ct_mpi_uint_lt( Y[i - 1], X[i - 1] );
|
||||
cond = mbedtls_ct_mpi_uint_lt( B[i - 1], A[i - 1] );
|
||||
done |= cond;
|
||||
|
||||
/*
|
||||
* If X[i - 1] < Y[i - 1] then X < Y is true.
|
||||
* If A[i - 1] < B[i - 1] then A < B is true.
|
||||
*
|
||||
* Again even if we can make a decision, we just mark the result and
|
||||
* the fact that we are done and continue looping.
|
||||
*/
|
||||
cond = mbedtls_ct_mpi_uint_lt( X[i - 1], Y[i - 1] );
|
||||
cond = mbedtls_ct_mpi_uint_lt( A[i - 1], B[i - 1] );
|
||||
ret |= cond & ( 1 - done );
|
||||
done |= cond;
|
||||
}
|
||||
|
||||
/*
|
||||
* If all the limbs were equal, then the numbers are equal, X < Y is false
|
||||
* If all the limbs were equal, then the numbers are equal, A < B is false
|
||||
* and leaving the result 0 is correct.
|
||||
*/
|
||||
|
||||
|
|
|
@ -133,19 +133,19 @@ unsigned mbedtls_ct_mpi_uint_lt( const mbedtls_mpi_uint x,
|
|||
* \brief Check if one unsigned MPI is less than another in constant
|
||||
* time.
|
||||
*
|
||||
* \param X The left-hand MPI. This must point to an array of limbs
|
||||
* with the same allocated length as \p Y.
|
||||
* \param Y The right-hand MPI. This must point to an array of limbs
|
||||
* with the same allocated length as \p X.
|
||||
* \param len The number of limbs in \p X and \p Y.
|
||||
* \param A The left-hand MPI. This must point to an array of limbs
|
||||
* with the same allocated length as \p B.
|
||||
* \param B The right-hand MPI. This must point to an array of limbs
|
||||
* with the same allocated length as \p A.
|
||||
* \param limbs The number of limbs in \p A and \p B.
|
||||
*
|
||||
* \return The result of the comparison:
|
||||
* \c 1 if \p X is less than \p Y.
|
||||
* \c 0 if \p X is greater than or equal to \p Y.
|
||||
* \c 1 if \p A is less than \p B.
|
||||
* \c 0 if \p A is greater than or equal to \p B.
|
||||
*/
|
||||
unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *X,
|
||||
const mbedtls_mpi_uint *Y,
|
||||
size_t len );
|
||||
unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *A,
|
||||
const mbedtls_mpi_uint *B,
|
||||
size_t limbs);
|
||||
#endif /* MBEDTLS_BIGNUM_C */
|
||||
|
||||
/** Choose between two integer values without branches.
|
||||
|
|
Loading…
Reference in a new issue