1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-06-05 00:22:11 +03:00

bignum_core: Aligned xxx_core_shift_l to xxx_core_shift_r

This patch modifies the left-shift implementation to closely
align in interface and behaviour to the existing right-shift
method.

Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
This commit is contained in:
Minos Galanakis 2023-04-20 14:22:16 +01:00
parent ad808dd5f1
commit ec09e25251
2 changed files with 32 additions and 37 deletions

View File

@ -353,52 +353,44 @@ void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
} }
} }
int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ) void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
size_t count)
{ {
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; size_t i, v0, v1;
size_t i, v0, t1;
mbedtls_mpi_uint r0 = 0, r1; mbedtls_mpi_uint r0 = 0, r1;
MPI_VALIDATE_RET( X != NULL );
v0 = count / (biL ); v0 = count / (biL);
t1 = count & (biL - 1); v1 = count & (biL - 1);
i = mbedtls_mpi_bitlen( X ) + count; if (v0 > limbs || (v0 == limbs && v1 > 0)) {
memset(X, 0, limbs * ciL);
if( X->n * biL < i ) return;
MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) ); }
ret = 0;
/* /*
* shift by count / limb_size * shift by count / limb_size
*/ */
if( v0 > 0 ) if (v0 > 0) {
{ for (i = limbs; i > v0; i--) {
for( i = X->n; i > v0; i-- ) X[i - 1] = X[i - v0 - 1];
X->p[i - 1] = X->p[i - v0 - 1]; }
for( ; i > 0; i-- ) for (; i > 0; i--) {
X->p[i - 1] = 0; X[i - 1] = 0;
}
} }
/* /*
* shift by count % limb_size * shift by count % limb_size
*/ */
if( t1 > 0 ) if (v1 > 0) {
{ for (i = v0; i < limbs; i++) {
for( i = v0; i < X->n; i++ ) r1 = X[i] >> (biL - v1);
{ X[i] <<= v1;
r1 = X->p[i] >> (biL - t1); X[i] |= r0;
X->p[i] <<= t1;
X->p[i] |= r0;
r0 = r1; r0 = r1;
} }
} }
cleanup:
return( ret );
} }
mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X, mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,

View File

@ -278,7 +278,7 @@ int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A,
unsigned char *output, unsigned char *output,
size_t output_length); size_t output_length);
/** \brief Shift an MPI right in place by a number of bits. /** \brief Shift an MPI in-place right by a number of bits.
* *
* Shifting by more bits than there are bit positions * Shifting by more bits than there are bit positions
* in \p X is valid and results in setting \p X to 0. * in \p X is valid and results in setting \p X to 0.
@ -294,16 +294,19 @@ void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
size_t count); size_t count);
/** /**
* \brief Perform a left-shift on an MPI: X <<= count * \brief Shift an MPI in-place left by a number of bits.
* *
* \param X The MPI to shift. This must point to an initialized MPI. * Shifting by more bits than there are bit positions
* \param count The number of bits to shift by. * in \p X is valid and results in setting \p X to 0.
* *
* \return \c 0 if successful. * This function's execution time depends on the value
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. * of \p count (and of course \p limbs).
* \return Another negative error code on different kinds of failure. * \param[in,out] X The number to shift.
* \param limbs The number of limbs of \p X. This must be at least 1.
* \param count The number of bits to shift by.
*/ */
int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ); void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
size_t count);
/** /**
* \brief Add two fixed-size large unsigned integers, returning the carry. * \brief Add two fixed-size large unsigned integers, returning the carry.