mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-02-21 06:40:50 +00:00
bignum_mod: Refactored mbedtls_mpi_mod_modulus_setup()
This patch removes the `int_rep` input parameter for modular setup, aiming to align it with the optred variant. Test and test-suite helper functions have been updated accordingly. Signed-off-by: Minos Galanakis <minos.galanakis@arm.com>
This commit is contained in:
parent
67ebaaf8a0
commit
88e16dfa2a
@ -138,31 +138,15 @@ cleanup:
|
|||||||
|
|
||||||
int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
||||||
const mbedtls_mpi_uint *p,
|
const mbedtls_mpi_uint *p,
|
||||||
size_t p_limbs,
|
size_t p_limbs)
|
||||||
mbedtls_mpi_mod_rep_selector int_rep)
|
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
N->p = p;
|
N->p = p;
|
||||||
N->limbs = p_limbs;
|
N->limbs = p_limbs;
|
||||||
N->bits = mbedtls_mpi_core_bitlen(p, p_limbs);
|
N->bits = mbedtls_mpi_core_bitlen(p, p_limbs);
|
||||||
|
N->int_rep = MBEDTLS_MPI_MOD_REP_MONTGOMERY;
|
||||||
switch (int_rep) {
|
N->rep.mont.mm = mbedtls_mpi_core_montmul_init(N->p);
|
||||||
case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
|
ret = set_mont_const_square(&N->rep.mont.rr, N->p, N->limbs);
|
||||||
N->int_rep = int_rep;
|
|
||||||
N->rep.mont.mm = mbedtls_mpi_core_montmul_init(N->p);
|
|
||||||
ret = set_mont_const_square(&N->rep.mont.rr, N->p, N->limbs);
|
|
||||||
break;
|
|
||||||
case MBEDTLS_MPI_MOD_REP_OPT_RED:
|
|
||||||
N->int_rep = int_rep;
|
|
||||||
N->rep.ored = NULL;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
exit:
|
|
||||||
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
mbedtls_mpi_mod_modulus_free(N);
|
mbedtls_mpi_mod_modulus_free(N);
|
||||||
@ -248,8 +232,7 @@ static int mbedtls_mpi_mod_inv_non_mont(mbedtls_mpi_mod_residue *X,
|
|||||||
mbedtls_mpi_mod_modulus Nmont;
|
mbedtls_mpi_mod_modulus Nmont;
|
||||||
mbedtls_mpi_mod_modulus_init(&Nmont);
|
mbedtls_mpi_mod_modulus_init(&Nmont);
|
||||||
|
|
||||||
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_modulus_setup(&Nmont, N->p, N->limbs,
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_modulus_setup(&Nmont, N->p, N->limbs));
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
|
||||||
|
|
||||||
/* We'll use X->p to hold the Montgomery form of the input A->p */
|
/* We'll use X->p to hold the Montgomery form of the input A->p */
|
||||||
mbedtls_mpi_core_to_mont_rep(X->p, A->p, Nmont.p, Nmont.limbs,
|
mbedtls_mpi_core_to_mont_rep(X->p, A->p, Nmont.p, Nmont.limbs,
|
||||||
|
@ -197,16 +197,12 @@ void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N);
|
|||||||
* not be modified in any way until after
|
* not be modified in any way until after
|
||||||
* mbedtls_mpi_mod_modulus_free() is called.
|
* mbedtls_mpi_mod_modulus_free() is called.
|
||||||
* \param p_limbs The number of limbs of \p p.
|
* \param p_limbs The number of limbs of \p p.
|
||||||
* \param int_rep The internal representation to be used for residues
|
|
||||||
* associated with \p N (see #mbedtls_mpi_mod_rep_selector).
|
|
||||||
*
|
*
|
||||||
* \return \c 0 if successful.
|
* \return \c 0 if successful.
|
||||||
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p int_rep is invalid.
|
|
||||||
*/
|
*/
|
||||||
int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
||||||
const mbedtls_mpi_uint *p,
|
const mbedtls_mpi_uint *p,
|
||||||
size_t p_limbs,
|
size_t p_limbs);
|
||||||
mbedtls_mpi_mod_rep_selector int_rep);
|
|
||||||
|
|
||||||
/** Setup an optimised-reduction compatible modulus structure.
|
/** Setup an optimised-reduction compatible modulus structure.
|
||||||
*
|
*
|
||||||
|
@ -6003,8 +6003,7 @@ int mbedtls_ecp_modulus_setup(mbedtls_mpi_mod_modulus *N,
|
|||||||
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mbedtls_mpi_mod_modulus_setup(N, p, p_limbs,
|
if (mbedtls_mpi_mod_modulus_setup(N, p, p_limbs)) {
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY)) {
|
|
||||||
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -99,7 +99,18 @@ int mbedtls_test_read_mpi_modulus(mbedtls_mpi_mod_modulus *N,
|
|||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
ret = mbedtls_mpi_mod_modulus_setup(N, p, limbs, int_rep);
|
|
||||||
|
switch (int_rep) {
|
||||||
|
case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
|
||||||
|
ret = mbedtls_mpi_mod_modulus_setup(N, p, limbs);
|
||||||
|
break;
|
||||||
|
case MBEDTLS_MPI_MOD_REP_OPT_RED:
|
||||||
|
ret = mbedtls_mpi_mod_optred_modulus_setup(N, p, limbs, NULL);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
mbedtls_free(p);
|
mbedtls_free(p);
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,19 @@ void mpi_mod_setup(int int_rep, int iret)
|
|||||||
memset(mp, 0xFF, sizeof(mp));
|
memset(mp, 0xFF, sizeof(mp));
|
||||||
|
|
||||||
mbedtls_mpi_mod_modulus_init(&m);
|
mbedtls_mpi_mod_modulus_init(&m);
|
||||||
ret = mbedtls_mpi_mod_modulus_setup(&m, mp, MLIMBS, int_rep);
|
|
||||||
|
switch (int_rep) {
|
||||||
|
case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
|
||||||
|
ret = mbedtls_mpi_mod_modulus_setup(&m, mp, MLIMBS);
|
||||||
|
break;
|
||||||
|
case MBEDTLS_MPI_MOD_REP_OPT_RED:
|
||||||
|
ret = mbedtls_mpi_mod_optred_modulus_setup(&m, mp, MLIMBS, NULL);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
TEST_EQUAL(ret, iret);
|
TEST_EQUAL(ret, iret);
|
||||||
|
|
||||||
/* Only test if the constants have been set-up */
|
/* Only test if the constants have been set-up */
|
||||||
@ -539,8 +551,7 @@ void mpi_residue_setup(char *input_N, char *input_R, int ret)
|
|||||||
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&N, &n_limbs, input_N));
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&N, &n_limbs, input_N));
|
||||||
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&R, &r_limbs, input_R));
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&R, &r_limbs, input_R));
|
||||||
|
|
||||||
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs,
|
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs));
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
|
||||||
|
|
||||||
TEST_EQUAL(ret, mbedtls_mpi_mod_residue_setup(&r, &m, R, r_limbs));
|
TEST_EQUAL(ret, mbedtls_mpi_mod_residue_setup(&r, &m, R, r_limbs));
|
||||||
|
|
||||||
@ -581,8 +592,7 @@ void mpi_mod_io_neg(char *input_N, data_t *buf, int ret)
|
|||||||
mbedtls_mpi_mod_write(&r, &m, buf->x, buf->len, endian));
|
mbedtls_mpi_mod_write(&r, &m, buf->x, buf->len, endian));
|
||||||
|
|
||||||
/* Set up modulus and test with residue->p == NULL */
|
/* Set up modulus and test with residue->p == NULL */
|
||||||
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs,
|
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs));
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
|
||||||
|
|
||||||
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
||||||
mbedtls_mpi_mod_read(&r, &m, buf->x, buf->len, endian));
|
mbedtls_mpi_mod_read(&r, &m, buf->x, buf->len, endian));
|
||||||
@ -655,8 +665,7 @@ void mpi_mod_io(char *input_N, data_t *input_A, int endian)
|
|||||||
TEST_LE_U(a_bytes, n_bytes);
|
TEST_LE_U(a_bytes, n_bytes);
|
||||||
|
|
||||||
/* Init Structures */
|
/* Init Structures */
|
||||||
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs,
|
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs));
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
|
||||||
|
|
||||||
/* Enforcing p_limbs >= m->limbs */
|
/* Enforcing p_limbs >= m->limbs */
|
||||||
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&r, &m, R, n_limbs));
|
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&r, &m, R, n_limbs));
|
||||||
|
@ -54,8 +54,7 @@ void mpi_mod_raw_io(data_t *input, int nb_int, int nx_32_int,
|
|||||||
|
|
||||||
mbedtls_mpi_uint init[sizeof(X) / sizeof(X[0])];
|
mbedtls_mpi_uint init[sizeof(X) / sizeof(X[0])];
|
||||||
memset(init, 0xFF, sizeof(init));
|
memset(init, 0xFF, sizeof(init));
|
||||||
int ret = mbedtls_mpi_mod_modulus_setup(&m, init, nx,
|
int ret = mbedtls_mpi_mod_modulus_setup(&m, init, nx);
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY);
|
|
||||||
TEST_EQUAL(ret, 0);
|
TEST_EQUAL(ret, 0);
|
||||||
|
|
||||||
if (iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && iret != 0) {
|
if (iendian == MBEDTLS_MPI_MOD_EXT_REP_INVALID && iret != 0) {
|
||||||
@ -137,8 +136,7 @@ void mpi_mod_raw_cond_assign(char *input_X,
|
|||||||
ASSERT_ALLOC(buff_m, copy_limbs);
|
ASSERT_ALLOC(buff_m, copy_limbs);
|
||||||
memset(buff_m, 0xFF, copy_limbs);
|
memset(buff_m, 0xFF, copy_limbs);
|
||||||
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
||||||
&m, buff_m, copy_limbs,
|
&m, buff_m, copy_limbs), 0);
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0);
|
|
||||||
|
|
||||||
/* condition is false */
|
/* condition is false */
|
||||||
TEST_CF_SECRET(X, bytes);
|
TEST_CF_SECRET(X, bytes);
|
||||||
@ -208,8 +206,7 @@ void mpi_mod_raw_cond_swap(char *input_X,
|
|||||||
ASSERT_ALLOC(buff_m, copy_limbs);
|
ASSERT_ALLOC(buff_m, copy_limbs);
|
||||||
memset(buff_m, 0xFF, copy_limbs);
|
memset(buff_m, 0xFF, copy_limbs);
|
||||||
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
||||||
&m, buff_m, copy_limbs,
|
&m, buff_m, copy_limbs), 0);
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0);
|
|
||||||
|
|
||||||
ASSERT_ALLOC(X, limbs);
|
ASSERT_ALLOC(X, limbs);
|
||||||
memcpy(X, tmp_X, bytes);
|
memcpy(X, tmp_X, bytes);
|
||||||
@ -297,8 +294,7 @@ void mpi_mod_raw_sub(char *input_A,
|
|||||||
ASSERT_ALLOC(X, limbs);
|
ASSERT_ALLOC(X, limbs);
|
||||||
|
|
||||||
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
||||||
&m, N, limbs,
|
&m, N, limbs), 0);
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0);
|
|
||||||
|
|
||||||
mbedtls_mpi_mod_raw_sub(X, A, B, &m);
|
mbedtls_mpi_mod_raw_sub(X, A, B, &m);
|
||||||
ASSERT_COMPARE(X, bytes, res, bytes);
|
ASSERT_COMPARE(X, bytes, res, bytes);
|
||||||
@ -368,8 +364,7 @@ void mpi_mod_raw_fix_quasi_reduction(char *input_N,
|
|||||||
TEST_ASSERT(c || mbedtls_mpi_core_lt_ct(tmp, N, limbs));
|
TEST_ASSERT(c || mbedtls_mpi_core_lt_ct(tmp, N, limbs));
|
||||||
|
|
||||||
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
||||||
&m, N, limbs,
|
&m, N, limbs), 0);
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0);
|
|
||||||
|
|
||||||
mbedtls_mpi_mod_raw_fix_quasi_reduction(X, &m);
|
mbedtls_mpi_mod_raw_fix_quasi_reduction(X, &m);
|
||||||
ASSERT_COMPARE(X, bytes, res, bytes);
|
ASSERT_COMPARE(X, bytes, res, bytes);
|
||||||
@ -419,8 +414,7 @@ void mpi_mod_raw_mul(char *input_A,
|
|||||||
ASSERT_ALLOC(X, limbs);
|
ASSERT_ALLOC(X, limbs);
|
||||||
|
|
||||||
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
||||||
&m, N, limbs,
|
&m, N, limbs), 0);
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0);
|
|
||||||
|
|
||||||
const size_t limbs_T = limbs * 2 + 1;
|
const size_t limbs_T = limbs * 2 + 1;
|
||||||
ASSERT_ALLOC(T, limbs_T);
|
ASSERT_ALLOC(T, limbs_T);
|
||||||
@ -580,9 +574,7 @@ void mpi_mod_raw_add(char *input_N,
|
|||||||
ASSERT_ALLOC(X, limbs);
|
ASSERT_ALLOC(X, limbs);
|
||||||
|
|
||||||
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
||||||
&m, N, limbs,
|
&m, N, limbs), 0);
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY
|
|
||||||
), 0);
|
|
||||||
|
|
||||||
/* A + B => Correct result */
|
/* A + B => Correct result */
|
||||||
mbedtls_mpi_mod_raw_add(X, A, B, &m);
|
mbedtls_mpi_mod_raw_add(X, A, B, &m);
|
||||||
@ -720,8 +712,7 @@ void mpi_mod_raw_to_mont_rep(char *input_N, char *input_A, char *input_X)
|
|||||||
size_t limbs = n_limbs;
|
size_t limbs = n_limbs;
|
||||||
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
||||||
|
|
||||||
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs,
|
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs));
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
|
||||||
|
|
||||||
/* 1. Test low-level function first */
|
/* 1. Test low-level function first */
|
||||||
|
|
||||||
@ -785,8 +776,7 @@ void mpi_mod_raw_from_mont_rep(char *input_N, char *input_A, char *input_X)
|
|||||||
size_t limbs = n_limbs;
|
size_t limbs = n_limbs;
|
||||||
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
||||||
|
|
||||||
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs,
|
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs));
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
|
||||||
|
|
||||||
/* 1. Test low-level function first */
|
/* 1. Test low-level function first */
|
||||||
|
|
||||||
@ -847,8 +837,7 @@ void mpi_mod_raw_neg(char *input_N, char *input_A, char *input_X)
|
|||||||
ASSERT_ALLOC(R, n_limbs);
|
ASSERT_ALLOC(R, n_limbs);
|
||||||
ASSERT_ALLOC(Z, n_limbs);
|
ASSERT_ALLOC(Z, n_limbs);
|
||||||
|
|
||||||
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs,
|
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs));
|
||||||
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
|
||||||
|
|
||||||
/* Neg( A == 0 ) => Zero result */
|
/* Neg( A == 0 ) => Zero result */
|
||||||
mbedtls_mpi_mod_raw_neg(R, Z, &m);
|
mbedtls_mpi_mod_raw_neg(R, Z, &m);
|
||||||
|
@ -1373,8 +1373,7 @@ void ecp_mod_p_generic_raw(int curve_id,
|
|||||||
TEST_EQUAL(limbs_res, limbs_N);
|
TEST_EQUAL(limbs_res, limbs_N);
|
||||||
|
|
||||||
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
TEST_EQUAL(mbedtls_mpi_mod_modulus_setup(
|
||||||
&m, N, limbs_N,
|
&m, N, limbs_N), 0);
|
||||||
MBEDTLS_MPI_MOD_REP_OPT_RED), 0);
|
|
||||||
|
|
||||||
TEST_EQUAL((*curve_func)(X, limbs_X), 0);
|
TEST_EQUAL((*curve_func)(X, limbs_X), 0);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user