diff --git a/library/bignum_core.c b/library/bignum_core.c index 00837298b0..3f4e6510aa 100644 --- a/library/bignum_core.c +++ b/library/bignum_core.c @@ -316,8 +316,6 @@ int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *X, return( 0 ); } - - void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs, size_t count ) { @@ -360,7 +358,21 @@ void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs, } } - +mbedtls_mpi_uint MPI_CORE(add)( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *l, + const mbedtls_mpi_uint *r, + size_t n ) +{ + mbedtls_mpi_uint c = 0, t; + for( size_t i = 0; i < n; i++ ) + { + t = c; + t += l[i]; c = ( t < l[i] ); + t += r[i]; c += ( t < r[i] ); + d[i] = t; + } + return( c ); +} mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *X, const mbedtls_mpi_uint *A, diff --git a/library/bignum_core.h b/library/bignum_core.h index 56a3bf874f..0d7b89f20a 100644 --- a/library/bignum_core.h +++ b/library/bignum_core.h @@ -277,6 +277,29 @@ int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A, void mbedtls_mpi_core_shift_r( mbedtls_mpi_uint *X, size_t limbs, size_t count ); +#define MPI_CORE(func) mbedtls_mpi_core_ ## func ## _minimal + +/** + * \brief Add two known-size large unsigned integers, returning the carry. + * + * Calculate l + r where l and r have the same size. + * This function operates modulo (2^ciL)^n and returns the carry + * (1 if there was a wraparound, and 0 otherwise). + * + * d may be aliased to l or r. + * + * \param[out] d The result of the addition. + * \param[in] l The left operand. + * \param[in] r The right operand. + * \param n Number of limbs of \p d, \p l and \p r. + * + * \return 1 if `l + r >= (2^{ciL})^n`, 0 otherwise. + */ +mbedtls_mpi_uint MPI_CORE(add)( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *l, + const mbedtls_mpi_uint *r, + size_t n ); + /** * \brief Conditional addition of two fixed-size large unsigned integers, * returning the carry.