mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-03-26 11:37:09 +00:00
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
@ -38,14 +38,14 @@
|
|||||||
|
|
||||||
#include "bignum_core.h"
|
#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;
|
size_t j;
|
||||||
mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
|
mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
|
||||||
|
|
||||||
for( j = 0; j < biL; j++ )
|
for( j = 0; j < biL; j++ )
|
||||||
{
|
{
|
||||||
if( x & mask ) break;
|
if( a & mask ) break;
|
||||||
|
|
||||||
mask >>= 1;
|
mask >>= 1;
|
||||||
}
|
}
|
||||||
@ -53,46 +53,46 @@ size_t mbedtls_mpi_core_clz( const mbedtls_mpi_uint x )
|
|||||||
return( j );
|
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;
|
size_t i, j;
|
||||||
|
|
||||||
if( nx == 0 )
|
if( A_limbs == 0 )
|
||||||
return( 0 );
|
return( 0 );
|
||||||
|
|
||||||
for( i = nx - 1; i > 0; i-- )
|
for( i = A_limbs - 1; i > 0; i-- )
|
||||||
if( X[i] != 0 )
|
if( A[i] != 0 )
|
||||||
break;
|
break;
|
||||||
|
|
||||||
j = biL - mbedtls_mpi_core_clz( X[i] );
|
j = biL - mbedtls_mpi_core_clz( A[i] );
|
||||||
|
|
||||||
return( ( i * biL ) + j );
|
return( ( i * biL ) + j );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
|
/* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
|
||||||
* into the storage form used by mbedtls_mpi. */
|
* 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;
|
uint8_t i;
|
||||||
unsigned char *x_ptr;
|
unsigned char *a_ptr;
|
||||||
mbedtls_mpi_uint tmp = 0;
|
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 <<= CHAR_BIT;
|
||||||
tmp |= (mbedtls_mpi_uint) *x_ptr;
|
tmp |= (mbedtls_mpi_uint) *a_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return( tmp );
|
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__)
|
#if defined(__BYTE_ORDER__)
|
||||||
|
|
||||||
/* Nothing to do on bigendian systems. */
|
/* Nothing to do on bigendian systems. */
|
||||||
#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
|
#if ( __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ )
|
||||||
return( x );
|
return( a );
|
||||||
#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
|
#endif /* __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ */
|
||||||
|
|
||||||
#if ( __BYTE_ORDER__ == __ORDER_LITTLE_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) )
|
switch( sizeof(mbedtls_mpi_uint) )
|
||||||
{
|
{
|
||||||
case 4:
|
case 4:
|
||||||
return( __builtin_bswap32(x) );
|
return( __builtin_bswap32(a) );
|
||||||
case 8:
|
case 8:
|
||||||
return( __builtin_bswap64(x) );
|
return( __builtin_bswap64(a) );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif /* __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ */
|
#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
|
/* Fall back to C-based reordering if we don't know the byte order
|
||||||
* or we couldn't use a compiler-specific builtin. */
|
* 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 )
|
size_t limbs )
|
||||||
{
|
{
|
||||||
mbedtls_mpi_uint *cur_limb_left;
|
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
|
* than the right index (it's not a problem if limbs is odd and the
|
||||||
* indices coincide in the last iteration).
|
* 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;
|
||||||
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,
|
int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
|
||||||
size_t nx,
|
size_t X_limbs,
|
||||||
const unsigned char *buf,
|
const unsigned char *input,
|
||||||
size_t buflen )
|
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 );
|
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
||||||
|
|
||||||
if( X != NULL )
|
if( X != NULL )
|
||||||
{
|
{
|
||||||
memset( X, 0, nx * ciL );
|
memset( X, 0, X_limbs * ciL );
|
||||||
|
|
||||||
for( size_t i = 0; i < buflen; i++ )
|
for( size_t i = 0; i < input_length; i++ )
|
||||||
X[i / ciL] |= ((mbedtls_mpi_uint) buf[i]) << ((i % ciL) << 3);
|
X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << ((i % ciL) << 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
return( 0 );
|
return( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
|
int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
|
||||||
size_t nx,
|
size_t X_limbs,
|
||||||
const unsigned char *buf,
|
const unsigned char *input,
|
||||||
size_t buflen )
|
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 );
|
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
||||||
|
|
||||||
/* If nx is 0, buflen must also be 0 (from previous test). Nothing to do. */
|
/* If X_limbs is 0, input_length must also be 0 (from previous test).
|
||||||
if( nx == 0 )
|
* Nothing to do. */
|
||||||
|
if( X_limbs == 0 )
|
||||||
return( 0 );
|
return( 0 );
|
||||||
|
|
||||||
memset( X, 0, nx * ciL );
|
memset( X, 0, X_limbs * ciL );
|
||||||
|
|
||||||
/* memcpy() with (NULL, 0) is undefined behaviour */
|
/* 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;
|
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 );
|
return( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
|
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
|
||||||
size_t nx,
|
size_t A_limbs,
|
||||||
unsigned char *buf,
|
unsigned char *output,
|
||||||
size_t buflen )
|
size_t output_length )
|
||||||
{
|
{
|
||||||
size_t stored_bytes = nx * ciL;
|
size_t stored_bytes = A_limbs * ciL;
|
||||||
size_t bytes_to_copy;
|
size_t bytes_to_copy;
|
||||||
|
|
||||||
if( stored_bytes < buflen )
|
if( stored_bytes < output_length )
|
||||||
{
|
{
|
||||||
bytes_to_copy = stored_bytes;
|
bytes_to_copy = stored_bytes;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bytes_to_copy = buflen;
|
bytes_to_copy = output_length;
|
||||||
|
|
||||||
/* The output buffer is smaller than the allocated size of X.
|
/* The output outputfer is smaller than the allocated size of A.
|
||||||
* However X may fit if its leading bytes are zero. */
|
* However A may fit if its leading bytes are zero. */
|
||||||
for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
|
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 );
|
return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for( size_t i = 0; i < bytes_to_copy; i++ )
|
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 */
|
/* Write trailing 0 bytes */
|
||||||
memset( buf + stored_bytes, 0, buflen - stored_bytes );
|
memset( output + stored_bytes, 0, output_length - stored_bytes );
|
||||||
}
|
}
|
||||||
|
|
||||||
return( 0 );
|
return( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
|
int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
|
||||||
size_t nx,
|
size_t X_limbs,
|
||||||
unsigned char *buf,
|
unsigned char *output,
|
||||||
size_t buflen )
|
size_t output_length )
|
||||||
{
|
{
|
||||||
size_t stored_bytes;
|
size_t stored_bytes;
|
||||||
size_t bytes_to_copy;
|
size_t bytes_to_copy;
|
||||||
unsigned char *p;
|
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
|
* null bytes and record the position at which to start
|
||||||
* writing the significant bytes. In this case, the execution
|
* writing the significant bytes. In this case, the execution
|
||||||
* trace of this function does not depend on the value of the
|
* trace of this function does not depend on the value of the
|
||||||
* number. */
|
* number. */
|
||||||
bytes_to_copy = stored_bytes;
|
bytes_to_copy = stored_bytes;
|
||||||
p = buf + buflen - stored_bytes;
|
p = output + output_length - stored_bytes;
|
||||||
memset( buf, 0, buflen - stored_bytes );
|
memset( output, 0, output_length - stored_bytes );
|
||||||
}
|
}
|
||||||
else
|
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. */
|
* However X may fit if its leading bytes are zero. */
|
||||||
bytes_to_copy = buflen;
|
bytes_to_copy = output_length;
|
||||||
p = buf;
|
p = output;
|
||||||
for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
|
for( size_t i = bytes_to_copy; i < stored_bytes; i++ )
|
||||||
{
|
{
|
||||||
if( GET_BYTE( X, i ) != 0 )
|
if( GET_BYTE( X, i ) != 0 )
|
||||||
|
@ -33,31 +33,31 @@
|
|||||||
|
|
||||||
/** Count leading zero bits in a given integer.
|
/** 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
|
/** Return the the minimum number of bits required to represent the value held
|
||||||
* in the MPI.
|
* 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[in] A The address of the MPI.
|
||||||
* \param nx The number of limbs of \p X.
|
* \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
|
/** Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
|
||||||
* into the storage form used by mbedtls_mpi.
|
* into the storage form used by mbedtls_mpi.
|
||||||
*
|
*
|
||||||
* \param[in,out] X The address of the MPI.
|
* \param[in,out] A The address of the MPI.
|
||||||
* \param limbs The number of limbs of \p X.
|
* \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 );
|
size_t limbs );
|
||||||
|
|
||||||
/** Import X from unsigned binary data, little endian.
|
/** Import X from unsigned binary data, little endian.
|
||||||
@ -66,18 +66,18 @@ void mbedtls_mpi_core_bigendian_to_host( mbedtls_mpi_uint *X,
|
|||||||
* most significant zero bytes in the input).
|
* most significant zero bytes in the input).
|
||||||
*
|
*
|
||||||
* \param[out] X The address of the MPI.
|
* \param[out] X The address of the MPI.
|
||||||
* \param nx The number of limbs of \p X.
|
* \param X_limbs The number of limbs of \p X.
|
||||||
* \param[in] buf The input buffer to import from.
|
* \param[in] input The input buffer to import from.
|
||||||
* \param buflen The length in bytes of \p buf.
|
* \param input_length The length bytes of \p input.
|
||||||
*
|
*
|
||||||
* \return \c 0 if successful.
|
* \return \c 0 if successful.
|
||||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
|
* \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,
|
int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
|
||||||
size_t nx,
|
size_t X_limbs,
|
||||||
const unsigned char *buf,
|
const unsigned char *input,
|
||||||
size_t buflen );
|
size_t input_length );
|
||||||
|
|
||||||
/** Import X from unsigned binary data, big endian.
|
/** Import X from unsigned binary data, big endian.
|
||||||
*
|
*
|
||||||
@ -85,60 +85,61 @@ int mbedtls_mpi_core_read_le( mbedtls_mpi_uint *X,
|
|||||||
* most significant zero bytes in the input).
|
* most significant zero bytes in the input).
|
||||||
*
|
*
|
||||||
* \param[out] X The address of the MPI.
|
* \param[out] X The address of the MPI.
|
||||||
* May only be #NULL if \nx is 0 and \p buflen is 0.
|
* May only be #NULL if \X_limbs is 0 and \p input_length
|
||||||
* \param nx The number of limbs of \p X.
|
* is 0.
|
||||||
* \param[in] buf The input buffer to import from.
|
* \param X_limbs The number of limbs of \p X.
|
||||||
* May only be #NULL if \p buflen is 0.
|
* \param[in] input The input buffer to import from.
|
||||||
* \param buflen The length in bytes of \p buf.
|
* 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 \c 0 if successful.
|
||||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
|
* \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,
|
int mbedtls_mpi_core_read_be( mbedtls_mpi_uint *X,
|
||||||
size_t nx,
|
size_t X_limbs,
|
||||||
const unsigned char *buf,
|
const unsigned char *input,
|
||||||
size_t buflen );
|
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
|
* \note If \p output is shorter than \p A the export is still successful if the
|
||||||
* value held in \p X fits in the buffer (that is, if enough of the most
|
* value held in \p A fits in the buffer (that is, if enough of the most
|
||||||
* significant bytes of \p X are 0).
|
* significant bytes of \p A are 0).
|
||||||
*
|
*
|
||||||
* \param[in] X The address of the MPI.
|
* \param[in] A The address of the MPI.
|
||||||
* \param nx The number of limbs of \p X.
|
* \param A_limbs The number of limbs of \p A.
|
||||||
* \param[out] buf The output buffer to export to.
|
* \param[out] output The output buffer to export to.
|
||||||
* \param buflen The length in bytes of \p buf.
|
* \param output_length The length in bytes of \p output.
|
||||||
*
|
*
|
||||||
* \return \c 0 if successful.
|
* \return \c 0 if successful.
|
||||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
|
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
|
||||||
* large enough to hold the value of \p X.
|
* large enough to hold the value of \p A.
|
||||||
*/
|
*/
|
||||||
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *X,
|
int mbedtls_mpi_core_write_le( const mbedtls_mpi_uint *A,
|
||||||
size_t nx,
|
size_t A_limbs,
|
||||||
unsigned char *buf,
|
unsigned char *output,
|
||||||
size_t buflen );
|
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
|
* \note If \p output is shorter than \p A the export is still successful if the
|
||||||
* value held in \p X fits in the buffer (that is, if enough of the most
|
* value held in \p A fits in the buffer (that is, if enough of the most
|
||||||
* significant bytes of \p X are 0).
|
* significant bytes of \p A are 0).
|
||||||
*
|
*
|
||||||
* \param[in] X The address of the MPI.
|
* \param[in] A The address of the MPI.
|
||||||
* \param nx The number of limbs of \p X.
|
* \param A_limbs The number of limbs of \p A.
|
||||||
* \param[out] buf The output buffer to export to.
|
* \param[out] output The output buffer to export to.
|
||||||
* \param buflen The length in bytes of \p buf.
|
* \param output_length The length in bytes of \p output.
|
||||||
*
|
*
|
||||||
* \return \c 0 if successful.
|
* \return \c 0 if successful.
|
||||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
|
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
|
||||||
* large enough to hold the value of \p X.
|
* large enough to hold the value of \p A.
|
||||||
*/
|
*/
|
||||||
int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X,
|
int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A,
|
||||||
size_t nx,
|
size_t A_limbs,
|
||||||
unsigned char *buf,
|
unsigned char *output,
|
||||||
size_t buflen );
|
size_t output_length );
|
||||||
|
|
||||||
#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
|
#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */
|
||||||
#define biL (ciL << 3) /* bits in limb */
|
#define biL (ciL << 3) /* bits in limb */
|
||||||
|
@ -45,9 +45,9 @@
|
|||||||
int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
|
int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
|
||||||
const mbedtls_mpi_mod_modulus *m,
|
const mbedtls_mpi_mod_modulus *m,
|
||||||
mbedtls_mpi_uint *p,
|
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 );
|
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
||||||
|
|
||||||
r->limbs = m->limbs;
|
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,
|
int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
||||||
const mbedtls_mpi_uint *p,
|
const mbedtls_mpi_uint *p,
|
||||||
size_t pn,
|
size_t p_limbs,
|
||||||
mbedtls_mpi_mod_ext_rep ext_rep,
|
mbedtls_mpi_mod_ext_rep ext_rep,
|
||||||
mbedtls_mpi_mod_rep_selector int_rep )
|
mbedtls_mpi_mod_rep_selector int_rep )
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
m->p = p;
|
m->p = p;
|
||||||
m->limbs = pn;
|
m->limbs = p_limbs;
|
||||||
m->bits = mbedtls_mpi_core_bitlen( p, pn );
|
m->bits = mbedtls_mpi_core_bitlen( p, p_limbs );
|
||||||
|
|
||||||
switch( ext_rep )
|
switch( ext_rep )
|
||||||
{
|
{
|
||||||
|
@ -76,16 +76,16 @@ typedef struct {
|
|||||||
* The memory pointed to by \p p will be used by \p r and must
|
* The memory pointed to by \p p will be used by \p r and must
|
||||||
* not be modified in any way until after
|
* not be modified in any way until after
|
||||||
* mbedtls_mpi_mod_residue_release() is called.
|
* 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 \c 0 if successful.
|
||||||
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p pn is less than the limbs
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p p_limbs is less than the
|
||||||
* in \p m or if \p p is not less than \p m.
|
* 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,
|
int mbedtls_mpi_mod_residue_setup( mbedtls_mpi_mod_residue *r,
|
||||||
const mbedtls_mpi_mod_modulus *m,
|
const mbedtls_mpi_mod_modulus *m,
|
||||||
mbedtls_mpi_uint *p,
|
mbedtls_mpi_uint *p,
|
||||||
size_t pn );
|
size_t p_limbs );
|
||||||
|
|
||||||
/** Unbind elements of a residue structure.
|
/** 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
|
* The memory pointed to by \p p will be used by \p m and must
|
||||||
* not be modified in any way until after
|
* not be modified in any way until after
|
||||||
* mbedtls_mpi_mod_modulus_free() is called.
|
* 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
|
* \param ext_rep The external representation to be used for residues
|
||||||
* associated with \p m (see #mbedtls_mpi_mod_ext_rep).
|
* associated with \p m (see #mbedtls_mpi_mod_ext_rep).
|
||||||
* \param int_rep The internal representation to be used for residues
|
* \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,
|
int mbedtls_mpi_mod_modulus_setup( mbedtls_mpi_mod_modulus *m,
|
||||||
const mbedtls_mpi_uint *p,
|
const mbedtls_mpi_uint *p,
|
||||||
size_t pn,
|
size_t p_limbs,
|
||||||
mbedtls_mpi_mod_ext_rep ext_rep,
|
mbedtls_mpi_mod_ext_rep ext_rep,
|
||||||
mbedtls_mpi_mod_rep_selector int_rep );
|
mbedtls_mpi_mod_rep_selector int_rep );
|
||||||
|
|
||||||
|
@ -43,18 +43,20 @@
|
|||||||
|
|
||||||
int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
|
int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
|
||||||
const mbedtls_mpi_mod_modulus *m,
|
const mbedtls_mpi_mod_modulus *m,
|
||||||
const unsigned char *buf,
|
const unsigned char *input,
|
||||||
size_t buflen )
|
size_t input_lentgth )
|
||||||
{
|
{
|
||||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||||
|
|
||||||
switch( m->ext_rep )
|
switch( m->ext_rep )
|
||||||
{
|
{
|
||||||
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
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;
|
break;
|
||||||
case MBEDTLS_MPI_MOD_EXT_REP_BE:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
||||||
@ -74,17 +76,19 @@ cleanup:
|
|||||||
return( ret );
|
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,
|
const mbedtls_mpi_mod_modulus *m,
|
||||||
unsigned char *buf,
|
unsigned char *output,
|
||||||
size_t buflen )
|
size_t output_length )
|
||||||
{
|
{
|
||||||
switch( m->ext_rep )
|
switch( m->ext_rep )
|
||||||
{
|
{
|
||||||
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
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:
|
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:
|
default:
|
||||||
return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
|
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
|
* The MPI needs to have enough limbs to store the full value (including any
|
||||||
* most significant zero bytes in the input).
|
* most significant zero bytes in the input).
|
||||||
*
|
*
|
||||||
* \param[out] X The address of the MPI. The size is determined by \p m. (In
|
* \param[out] X The address of the MPI. The size is determined by \p m.
|
||||||
* particular, it must have at least as many limbs as the
|
* (In particular, it must have at least as many limbs as
|
||||||
* modulus \p m.)
|
* the modulus \p m.)
|
||||||
* \param[in] m The address of the modulus related to \p X.
|
* \param[in] m The address of the modulus related to \p X.
|
||||||
* \param[in] buf The input buffer to import from.
|
* \param[in] input The input buffer to import from.
|
||||||
* \param buflen The length in bytes of \p buf.
|
* \param input_length The length in bytes of \p input.
|
||||||
*
|
*
|
||||||
* \return \c 0 if successful.
|
* \return \c 0 if successful.
|
||||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p X isn't
|
* \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
|
* \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.
|
* of \p m is invalid or \p X is not less than \p m.
|
||||||
*/
|
*/
|
||||||
int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
|
int mbedtls_mpi_mod_raw_read( mbedtls_mpi_uint *X,
|
||||||
const mbedtls_mpi_mod_modulus *m,
|
const mbedtls_mpi_mod_modulus *m,
|
||||||
const unsigned char *buf,
|
const unsigned char *input,
|
||||||
size_t buflen );
|
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
|
* \param[in] A The address of the MPI. The size is determined by \p m.
|
||||||
* particular, it must have at least as many limbs as the modulus
|
* (In particular, it must have at least as many limbs as
|
||||||
* \p m.)
|
* the modulus \p m.)
|
||||||
* \param[in] m The address of the modulus related to \p X.
|
* \param[in] m The address of the modulus related to \p A.
|
||||||
* \param[out] buf The output buffer to export to.
|
* \param[out] output The output buffer to export to.
|
||||||
* \param buflen The length in bytes of \p buf.
|
* \param output_length The length in bytes of \p output.
|
||||||
*
|
*
|
||||||
* \return \c 0 if successful.
|
* \return \c 0 if successful.
|
||||||
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
|
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p output isn't
|
||||||
* large enough to hold the value of \p X.
|
* large enough to hold the value of \p A.
|
||||||
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
|
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the external representation
|
||||||
* of \p m is invalid.
|
* 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,
|
const mbedtls_mpi_mod_modulus *m,
|
||||||
unsigned char *buf,
|
unsigned char *output,
|
||||||
size_t buflen );
|
size_t output_length );
|
||||||
|
|
||||||
#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */
|
#endif /* MBEDTLS_BIGNUM_MOD_RAW_H */
|
||||||
|
@ -744,9 +744,9 @@ cleanup:
|
|||||||
/*
|
/*
|
||||||
* Compare unsigned values in constant time
|
* Compare unsigned values in constant time
|
||||||
*/
|
*/
|
||||||
unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *X,
|
unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *A,
|
||||||
const mbedtls_mpi_uint *Y,
|
const mbedtls_mpi_uint *B,
|
||||||
size_t len )
|
size_t limbs )
|
||||||
{
|
{
|
||||||
unsigned ret, cond, done;
|
unsigned ret, cond, done;
|
||||||
|
|
||||||
@ -754,31 +754,31 @@ unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *X,
|
|||||||
* their scope. */
|
* their scope. */
|
||||||
ret = cond = done = 0;
|
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.
|
* remain 0.
|
||||||
*
|
*
|
||||||
* Again even if we can make a decision, we just mark the result and
|
* Again even if we can make a decision, we just mark the result and
|
||||||
* the fact that we are done and continue looping.
|
* 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;
|
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
|
* Again even if we can make a decision, we just mark the result and
|
||||||
* the fact that we are done and continue looping.
|
* 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 );
|
ret |= cond & ( 1 - done );
|
||||||
done |= cond;
|
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.
|
* 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
|
* \brief Check if one unsigned MPI is less than another in constant
|
||||||
* time.
|
* time.
|
||||||
*
|
*
|
||||||
* \param X The left-hand MPI. This must point to an array of limbs
|
* \param A The left-hand MPI. This must point to an array of limbs
|
||||||
* with the same allocated length as \p Y.
|
* with the same allocated length as \p B.
|
||||||
* \param Y The right-hand MPI. This must point to an array of limbs
|
* \param B The right-hand MPI. This must point to an array of limbs
|
||||||
* with the same allocated length as \p X.
|
* with the same allocated length as \p A.
|
||||||
* \param len The number of limbs in \p X and \p Y.
|
* \param limbs The number of limbs in \p A and \p B.
|
||||||
*
|
*
|
||||||
* \return The result of the comparison:
|
* \return The result of the comparison:
|
||||||
* \c 1 if \p X is less than \p Y.
|
* \c 1 if \p A is less than \p B.
|
||||||
* \c 0 if \p X is greater than or equal to \p Y.
|
* \c 0 if \p A is greater than or equal to \p B.
|
||||||
*/
|
*/
|
||||||
unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *X,
|
unsigned mbedtls_mpi_core_lt_ct( const mbedtls_mpi_uint *A,
|
||||||
const mbedtls_mpi_uint *Y,
|
const mbedtls_mpi_uint *B,
|
||||||
size_t len );
|
size_t limbs);
|
||||||
#endif /* MBEDTLS_BIGNUM_C */
|
#endif /* MBEDTLS_BIGNUM_C */
|
||||||
|
|
||||||
/** Choose between two integer values without branches.
|
/** Choose between two integer values without branches.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user