diff --git a/library/bignum.c b/library/bignum.c index 1c5ef8cb13..59b10654e3 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1156,38 +1156,6 @@ int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint return( mbedtls_mpi_sub_mpi( X, A, &B ) ); } -mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, - const mbedtls_mpi_uint *s, size_t s_len, - mbedtls_mpi_uint b ) -{ - mbedtls_mpi_uint c = 0; /* carry */ - size_t excess_len = d_len - s_len; - - size_t steps_x8 = s_len / 8; - size_t steps_x1 = s_len & 7; - - while( steps_x8-- ) - { - MULADDC_X8_INIT - MULADDC_X8_CORE - MULADDC_X8_STOP - } - - while( steps_x1-- ) - { - MULADDC_X1_INIT - MULADDC_X1_CORE - MULADDC_X1_STOP - } - - while( excess_len-- ) - { - *d += c; c = ( *d < c ); d++; - } - - return( c ); -} - /* * Baseline multiplication: X = A * B (HAC 14.12) */ diff --git a/library/bignum_core.h b/library/bignum_core.h index 62d0151e5f..77364670a0 100644 --- a/library/bignum_core.h +++ b/library/bignum_core.h @@ -155,9 +155,35 @@ int mbedtls_mpi_core_write_be( const mbedtls_mpi_uint *A, #define GET_BYTE( X, i ) \ ( ( (X)[(i) / ciL] >> ( ( (i) % ciL ) * 8 ) ) & 0xff ) -/** Perform a known-size multiply accumulate operation +/** + * \brief Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) * - * Add \p b * \p s to \p d. + * \param[out] X The destination MPI, as a little-endian array of + * length \p n. + * On successful completion, X contains the result of + * the multiplication A * B * R^-1 mod N where + * R = (2^ciL)^n. + * \param[in] A Little-endian presentation of first operand. + * Must have exactly \p n limbs. + * \param[in] B Little-endian presentation of second operand. + * \param[in] B_len The number of limbs in \p B. + * \param[in] N Little-endian presentation of the modulus. + * This must be odd and have exactly \p n limbs. + * \param[in] n The number of limbs in \p X, \p A, \p N. + * \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL. + * This can be calculated by `mpi_montg_init()`. + * \param[in,out] T Temporary storage of size at least 2*n+1 limbs. + * Its initial content is unused and + * its final content is indeterminate. + */ +void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, size_t B_len, + const mbedtls_mpi_uint *N, size_t n, + mbedtls_mpi_uint mm, mbedtls_mpi_uint *T ); + +/** + * \brief Perform a known-size multiply accumulate operation: d += b * s * * \param[in,out] d The pointer to the (little-endian) array * representing the bignum to accumulate onto. @@ -176,55 +202,6 @@ mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, const mbedtls_mpi_uint *s, size_t s_len, mbedtls_mpi_uint b ); -#define MPI_CORE(func) mbedtls_mpi_core_ ## func ## _minimal - -/** Montgomery multiplication: X = A * B * R^-1 mod N (HAC 14.36) - * - * \param[out] X The destination MPI, as a big endian array of length \p n. - * On successful completion, X contains the result of - * the multiplication A * B * R^-1 mod N where - * R = (2^ciL)^n. - * \param[in] A Big endian presentation of first operand. - * Must have exactly \p n limbs. - * \param[in] B Big endian presentation of second operand. - * \param[in] B_len The number of limbs in \p B. - * \param[in] N Big endian presentation of the modulus. - * This must be odd and have exactly \p n limbs. - * \param[in] n The number of limbs in \p X, \p A, \p N. - * \param mm The Montgomery constant for \p N: -N^-1 mod 2^ciL. - * This can be calculated by `mpi_montg_init()`. - * \param[in,out] T Temporary storage of size at least 2*n+1 limbs. - * Its initial content is unused and - * its final content is indeterminate. - */ -void MPI_CORE(montmul)( mbedtls_mpi_uint *X, - const mbedtls_mpi_uint *A, - const mbedtls_mpi_uint *B, size_t B_len, - const mbedtls_mpi_uint *N, size_t n, - mbedtls_mpi_uint mm, mbedtls_mpi_uint *T ); - -/** - * \brief Perform a known-size multiply accumulate operation - * - * Add \p b * \p s to \p d. - * - * \param[in,out] d The pointer to the (little-endian) array - * representing the bignum to accumulate onto. - * \param d_len The number of limbs of \p d. This must be - * at least \p s_len. - * \param[in] s The pointer to the (little-endian) array - * representing the bignum to multiply with. - * This may be the same as \p d. Otherwise, - * it must be disjoint from \p d. - * \param s_len The number of limbs of \p s. - * \param b A scalar to multiply with. - * - * \return c The carry at the end of the operation. - */ -mbedtls_mpi_uint MPI_CORE(mla)( mbedtls_mpi_uint *d, size_t d_len , - const mbedtls_mpi_uint *s, size_t s_len, - mbedtls_mpi_uint b ); - /** * \brief Subtract two known-size large unsigned integers, returning the borrow. * @@ -235,17 +212,17 @@ mbedtls_mpi_uint MPI_CORE(mla)( mbedtls_mpi_uint *d, size_t d_len , * d may be aliased to l or r. * * \param[out] d The result of the subtraction. - * \param[in] l The left operand. - * \param[in] r The right operand. + * \param[in] l Little-endian presentation of left operand. + * \param[in] r Little-endian presentation of right operand. * \param n Number of limbs of \p d, \p l and \p r. * * \return 1 if `l < r`. * 0 if `l >= r`. */ -mbedtls_mpi_uint MPI_CORE(sub)( mbedtls_mpi_uint *d, - const mbedtls_mpi_uint *l, - const mbedtls_mpi_uint *r, - size_t n ); +mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *l, + const mbedtls_mpi_uint *r, + size_t n ); /** * \brief Constant-time conditional addition of two known-size large unsigned @@ -270,9 +247,9 @@ mbedtls_mpi_uint MPI_CORE(sub)( mbedtls_mpi_uint *d, * * \return 1 if `d + cond*r >= (2^{ciL})^n`, 0 otherwise. */ -mbedtls_mpi_uint MPI_CORE(add_if)( mbedtls_mpi_uint *d, - const mbedtls_mpi_uint *r, - size_t n, - unsigned cond ); +mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *r, + size_t n, + unsigned cond ); #endif /* MBEDTLS_BIGNUM_CORE_H */ diff --git a/library/bignum_new.c b/library/bignum_new.c index 29c1212665..1c7523ead7 100644 --- a/library/bignum_new.c +++ b/library/bignum_new.c @@ -27,14 +27,14 @@ #include -void MPI_CORE(montmul)( mbedtls_mpi_uint *X, - const mbedtls_mpi_uint *A, - const mbedtls_mpi_uint *B, - size_t B_len, - const mbedtls_mpi_uint *N, - size_t n, - mbedtls_mpi_uint mm, - mbedtls_mpi_uint *T ) +void mbedtls_mpi_core_montmul( mbedtls_mpi_uint *X, + const mbedtls_mpi_uint *A, + const mbedtls_mpi_uint *B, + size_t B_len, + const mbedtls_mpi_uint *N, + size_t n, + mbedtls_mpi_uint mm, + mbedtls_mpi_uint *T ) { memset( T, 0, (2*n+1)*ciL ); @@ -45,21 +45,21 @@ void MPI_CORE(montmul)( mbedtls_mpi_uint *X, u0 = A[i]; u1 = ( T[0] + u0 * B[0] ) * mm; - (void) MPI_CORE(mla)( T, n + 2, B, B_len, u0 ); - (void) MPI_CORE(mla)( T, n + 2, N, n, u1 ); + (void) mbedtls_mpi_core_mla( T, n + 2, B, B_len, u0 ); + (void) mbedtls_mpi_core_mla( T, n + 2, N, n, u1 ); } mbedtls_mpi_uint carry, borrow, fixup; carry = T[n]; - borrow = MPI_CORE(sub)( X, T, N, n ); + borrow = mbedtls_mpi_core_sub( X, T, N, n ); fixup = carry < borrow; - (void) MPI_CORE(add_if)( X, N, n, fixup ); + (void) mbedtls_mpi_core_add_if( X, N, n, fixup ); } -mbedtls_mpi_uint MPI_CORE(mla)( mbedtls_mpi_uint *d, size_t d_len, - const mbedtls_mpi_uint *s, size_t s_len, - mbedtls_mpi_uint b ) +mbedtls_mpi_uint mbedtls_mpi_core_mla( mbedtls_mpi_uint *d, size_t d_len, + const mbedtls_mpi_uint *s, size_t s_len, + mbedtls_mpi_uint b ) { mbedtls_mpi_uint c = 0; /* carry */ if( d_len < s_len ) @@ -90,10 +90,10 @@ mbedtls_mpi_uint MPI_CORE(mla)( mbedtls_mpi_uint *d, size_t d_len, return( c ); } -mbedtls_mpi_uint MPI_CORE(sub)( mbedtls_mpi_uint *d, - const mbedtls_mpi_uint *l, - const mbedtls_mpi_uint *r, - size_t n ) +mbedtls_mpi_uint mbedtls_mpi_core_sub( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *l, + const mbedtls_mpi_uint *r, + size_t n ) { mbedtls_mpi_uint c = 0, t, z; @@ -106,10 +106,10 @@ mbedtls_mpi_uint MPI_CORE(sub)( mbedtls_mpi_uint *d, return( c ); } -mbedtls_mpi_uint MPI_CORE(add_if)( mbedtls_mpi_uint *d, - const mbedtls_mpi_uint *r, - size_t n, - unsigned cond ) +mbedtls_mpi_uint mbedtls_mpi_core_add_if( mbedtls_mpi_uint *d, + const mbedtls_mpi_uint *r, + size_t n, + unsigned cond ) { mbedtls_mpi_uint c = 0, t; for( size_t i = 0; i < n; i++ )