From ec09e2525147cb7cf7c82df3a8e1aa69dd62fcf1 Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Thu, 20 Apr 2023 14:22:16 +0100 Subject: [PATCH] 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 --- library/bignum_core.c | 50 ++++++++++++++++++------------------------- library/bignum_core.h | 19 +++++++++------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/library/bignum_core.c b/library/bignum_core.c index 26aff15f1..92a9d558a 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -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, t1; + size_t i, v0, v1; mbedtls_mpi_uint r0 = 0, r1; - MPI_VALIDATE_RET( X != NULL ); - v0 = count / (biL ); - t1 = count & (biL - 1); + v0 = count / (biL); + v1 = count & (biL - 1); - i = mbedtls_mpi_bitlen( X ) + count; - - if( X->n * biL < i ) - MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) ); - - ret = 0; + if (v0 > limbs || (v0 == limbs && v1 > 0)) { + memset(X, 0, limbs * ciL); + return; + } /* * shift by count / limb_size */ - if( v0 > 0 ) - { - for( i = X->n; i > v0; i-- ) - X->p[i - 1] = X->p[i - v0 - 1]; + if (v0 > 0) { + for (i = limbs; i > v0; i--) { + X[i - 1] = X[i - v0 - 1]; + } - for( ; i > 0; i-- ) - X->p[i - 1] = 0; + for (; i > 0; i--) { + X[i - 1] = 0; + } } /* * shift by count % limb_size */ - if( t1 > 0 ) - { - for( i = v0; i < X->n; i++ ) - { - r1 = X->p[i] >> (biL - t1); - X->p[i] <<= t1; - X->p[i] |= r0; + if (v1 > 0) { + for (i = v0; i < limbs; i++) { + r1 = X[i] >> (biL - v1); + X[i] <<= v1; + X[i] |= r0; r0 = r1; } } - -cleanup: - - return( ret ); } mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X, diff --git a/library/bignum_core.h b/library/bignum_core.h index 25e7e1a6b..2b11ccaff 100644 --- a/library/bignum_core.h +++ b/library/bignum_core.h @@ -278,7 +278,7 @@ int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *A, unsigned char *output, 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 * 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); /** - * \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. - * \param count The number of bits to shift by. + * Shifting by more bits than there are bit positions + * in \p X is valid and results in setting \p X to 0. * - * \return \c 0 if successful. - * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. - * \return Another negative error code on different kinds of failure. + * This function's execution time depends on the value + * of \p count (and of course \p limbs). + * \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.