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>
diff --git a/library/bignum_core.c b/library/bignum_core.c
index 606765a..d3d74ff 100644
--- a/library/bignum_core.c
+++ b/library/bignum_core.c
@@ -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 @@
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 @@
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 @@
/* 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 @@
* 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 @@
}
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 )