Bignum core: Break shift_r function out of the classic shift_r
This commit contains the function prototype for mbedtls_mpi_core_shift_r,
and the implementation minimally modified from mbedtls_mpi_shift_r.
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/library/bignum.c b/library/bignum.c
index 1c7f919..0787272 100644
--- a/library/bignum.c
+++ b/library/bignum.c
@@ -771,26 +771,37 @@
*/
int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
{
+ MPI_VALIDATE_RET( X != NULL );
+ if( X->n != 0 )
+ mbedtls_mpi_core_shift_r( X->p, X->n, count );
+ return( 0 );
+}
+
+void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs,
+ size_t count )
+{
size_t i, v0, v1;
mbedtls_mpi_uint r0 = 0, r1;
- MPI_VALIDATE_RET( X != NULL );
v0 = count / biL;
v1 = count & (biL - 1);
- if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
- return mbedtls_mpi_lset( X, 0 );
+ if( v0 > limbs || ( v0 == limbs && v1 > 0 ) )
+ {
+ memset( X, 0, limbs * ciL );
+ return;
+ }
/*
* shift by count / limb_size
*/
if( v0 > 0 )
{
- for( i = 0; i < X->n - v0; i++ )
- X->p[i] = X->p[i + v0];
+ for( i = 0; i < limbs - v0; i++ )
+ X[i] = X[i + v0];
- for( ; i < X->n; i++ )
- X->p[i] = 0;
+ for( ; i < limbs; i++ )
+ X[i] = 0;
}
/*
@@ -798,16 +809,14 @@
*/
if( v1 > 0 )
{
- for( i = X->n; i > 0; i-- )
+ for( i = limbs; i > 0; i-- )
{
- r1 = X->p[i - 1] << (biL - v1);
- X->p[i - 1] >>= v1;
- X->p[i - 1] |= r0;
+ r1 = X[i - 1] << (biL - v1);
+ X[i - 1] >>= v1;
+ X[i - 1] |= r0;
r0 = r1;
}
}
-
- return( 0 );
}
/*