Skip to content

Commit

Permalink
libmbedtls: make mbedtls_mpi_mont*() available
Browse files Browse the repository at this point in the history
Makes mbedtls_mpi_montg_init(), mbedtls_mpi_montmul() and
mbedtls_mpi_montred() available for external use.

Acked-by: Jerome Forissier <jerome.forissier@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
[jf: rebased onto mbedtls-2.22.0]
[jf: rebased onto mbedtls-2.27.0, keep static functions]
Signed-off-by: Jerome Forissier <jerome@forissier.org>
[jf: rebased onto mbedtls-2.28.1]
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
[jw: rebased onto mbedtls-3.4.0]
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
  • Loading branch information
jenswi-linaro committed Oct 6, 2023
1 parent 804fe3a commit 9a111f0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/libmbedtls/mbedtls/include/mbedtls/bignum.h
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,36 @@ int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng);

/**
* \brief Montgomery initialization
*
* \param mm The -1/m mod N result
* \param N The modulus
*/
void mbedtls_mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N );

/**
* \brief Montgomery multiplication: A = A * B * R^-1 mod N
* \A Parameter and result
* \B Parameter
* \N Modulus
* \mm Parameter from mbedtls_mpi_montg_init()
* \T Temporary variable, should be as twice as big as N + 2
*/
void mbedtls_mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
const mbedtls_mpi *N, mbedtls_mpi_uint mm,
mbedtls_mpi *T );

/**
* \brief Montgomery reduction: A = A * R^-1 mod N
* \A Parameter and result
* \N Modulus
* \mm Parameter from mbedtls_mpi_montg_init()
* \T Temporary variable, should be as twice as big as N + 2
*/
void mbedtls_mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
mbedtls_mpi_uint mm, mbedtls_mpi *T);

#if defined(MBEDTLS_SELF_TEST)

/**
Expand Down
18 changes: 18 additions & 0 deletions lib/libmbedtls/mbedtls/library/bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,11 @@ static void mpi_montg_init(mbedtls_mpi_uint *mm, const mbedtls_mpi *N)
*mm = mbedtls_mpi_core_montmul_init(N->p);
}

void mbedtls_mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
{
mpi_montg_init( mm, N );
}

/** Montgomery multiplication: A = A * B * R^-1 mod N (HAC 14.36)
*
* \param[in,out] A One of the numbers to multiply.
Expand Down Expand Up @@ -1599,6 +1604,13 @@ static void mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
mbedtls_mpi_core_montmul(A->p, A->p, B->p, B->n, N->p, N->n, mm, T->p);
}

void mbedtls_mpi_montmul(mbedtls_mpi *A, const mbedtls_mpi *B,
const mbedtls_mpi *N, mbedtls_mpi_uint mm,
mbedtls_mpi *T )
{
mpi_montmul( A, B, N, mm, T);
}

/*
* Montgomery reduction: A = A * R^-1 mod N
*
Expand All @@ -1616,6 +1628,12 @@ static void mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
mpi_montmul(A, &U, N, mm, T);
}

void mbedtls_mpi_montred(mbedtls_mpi *A, const mbedtls_mpi *N,
mbedtls_mpi_uint mm, mbedtls_mpi *T)
{
mpi_montred(A, N, mm, T);
}

/**
* Select an MPI from a table without leaking the index.
*
Expand Down

0 comments on commit 9a111f0

Please sign in to comment.