2022-09-30 14:02:16 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "bignum_mod.h"
|
2022-12-13 10:53:50 +01:00
|
|
|
#include "bignum_mod_raw.h"
|
2022-09-30 14:02:16 +02:00
|
|
|
#include "constant_time_internal.h"
|
|
|
|
#include "test/constant_flow.h"
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define TEST_COMPARE_MPI_RESIDUES(a, b) \
|
|
|
|
ASSERT_COMPARE((a).p, (a).limbs * sizeof(mbedtls_mpi_uint), \
|
|
|
|
(b).p, (b).limbs * sizeof(mbedtls_mpi_uint))
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int test_read_residue(mbedtls_mpi_mod_residue *r,
|
|
|
|
const mbedtls_mpi_mod_modulus *m,
|
|
|
|
char *input,
|
|
|
|
int skip_limbs_and_value_checks)
|
2022-12-01 15:27:37 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *p = NULL;
|
|
|
|
size_t limbs;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_test_read_mpi_core(&p, &limbs, input);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (skip_limbs_and_value_checks) {
|
2022-12-01 15:27:37 +01:00
|
|
|
r->p = p;
|
|
|
|
r->limbs = limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-12-01 15:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mbedtls_mpi_mod_residue_setup() checks limbs, and that value < m */
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_mpi_mod_residue_setup(r, m, p, limbs);
|
2022-12-01 15:27:37 +01:00
|
|
|
}
|
2022-09-30 14:02:16 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_BIGNUM_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_setup(int int_rep, int iret)
|
2022-09-30 14:02:16 +02:00
|
|
|
{
|
|
|
|
#define MLIMBS 8
|
|
|
|
mbedtls_mpi_uint mp[MLIMBS];
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(mp, 0xFF, sizeof(mp));
|
2022-09-30 14:02:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&m);
|
|
|
|
ret = mbedtls_mpi_mod_modulus_setup(&m, mp, MLIMBS, int_rep);
|
|
|
|
TEST_EQUAL(ret, iret);
|
2022-09-30 14:02:16 +02:00
|
|
|
|
2022-10-19 02:48:32 +02:00
|
|
|
/* Only test if the constants have been set-up */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY) {
|
2022-10-19 02:48:32 +02:00
|
|
|
/* Test that the consts have been calculated */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(m.rep.mont.rr != NULL);
|
|
|
|
TEST_ASSERT(m.rep.mont.mm != 0);
|
2022-10-19 02:48:32 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:02:16 +02:00
|
|
|
/* Address sanitiser should catch if we try to free mp */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_free(&m);
|
2022-09-30 14:02:16 +02:00
|
|
|
|
|
|
|
/* Make sure that the modulus doesn't have reference to mp anymore */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(m.p != mp);
|
2022-09-30 14:02:16 +02:00
|
|
|
|
2022-10-19 02:48:32 +02:00
|
|
|
/* Only test if the constants have been set-up */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0 && int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY) {
|
2022-10-19 02:48:32 +02:00
|
|
|
/* Verify the data and pointers allocated have been properly wiped */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(m.rep.mont.rr == NULL);
|
|
|
|
TEST_ASSERT(m.rep.mont.mm == 0);
|
2022-10-19 02:48:32 +02:00
|
|
|
}
|
2022-09-30 14:02:16 +02:00
|
|
|
exit:
|
|
|
|
/* It should be safe to call an mbedtls free several times */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_free(&m);
|
2022-09-30 14:02:16 +02:00
|
|
|
|
|
|
|
#undef MLIMBS
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-12-13 10:53:50 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_mul(char *input_A,
|
|
|
|
char *input_B,
|
|
|
|
char *input_N,
|
|
|
|
char *result)
|
2022-12-13 10:53:50 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
|
2022-12-20 18:09:49 +01:00
|
|
|
mbedtls_mpi_mod_residue rA = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue rB = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue rR = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue rX = { NULL, 0 };
|
|
|
|
|
2022-12-13 10:53:50 +01:00
|
|
|
mbedtls_mpi_mod_modulus m;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&m);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-05-09 15:26:26 +02:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_modulus(&m, input_N,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(test_read_residue(&rA, &m, input_A, 0), 0);
|
|
|
|
TEST_EQUAL(test_read_residue(&rB, &m, input_B, 0), 0);
|
|
|
|
TEST_EQUAL(test_read_residue(&rR, &m, result, 0), 0);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2022-12-16 16:31:59 +01:00
|
|
|
const size_t limbs = m.limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(rA.limbs, limbs);
|
|
|
|
TEST_EQUAL(rB.limbs, limbs);
|
|
|
|
TEST_EQUAL(rR.limbs, limbs);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X, limbs);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_residue_setup(&rX, &m, X, limbs), 0);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rA, &rB, &m), 0);
|
|
|
|
ASSERT_COMPARE(rX.p, bytes, rR.p, bytes);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
|
|
|
/* alias X to A */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(rX.p, rA.p, bytes);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rX, &rB, &m), 0);
|
|
|
|
ASSERT_COMPARE(rX.p, bytes, rR.p, bytes);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
|
|
|
/* alias X to B */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(rX.p, rB.p, bytes);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rA, &rX, &m), 0);
|
|
|
|
ASSERT_COMPARE(rX.p, bytes, rR.p, bytes);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
|
|
|
/* A == B: alias A and B */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(rA.p, rB.p, bytes) == 0) {
|
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rA, &rA, &m), 0);
|
|
|
|
ASSERT_COMPARE(rX.p, bytes, rR.p, bytes);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
|
|
|
/* X, A, B all aliased together */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(rX.p, rA.p, bytes);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rX, &rX, &m), 0);
|
|
|
|
ASSERT_COMPARE(rX.p, bytes, rR.p, bytes);
|
2022-12-13 10:53:50 +01:00
|
|
|
}
|
|
|
|
/* A != B: test B * A */
|
2023-01-11 14:50:10 +01:00
|
|
|
else {
|
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rB, &rA, &m), 0);
|
|
|
|
ASSERT_COMPARE(rX.p, bytes, rR.p, bytes);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
|
|
|
/* B * A: alias X to A */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(rX.p, rA.p, bytes);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rB, &rX, &m), 0);
|
|
|
|
ASSERT_COMPARE(rX.p, bytes, rR.p, bytes);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
|
|
|
/* B + A: alias X to B */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(rX.p, rB.p, bytes);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rX, &rA, &m), 0);
|
|
|
|
ASSERT_COMPARE(rX.p, bytes, rR.p, bytes);
|
2022-12-13 10:53:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(rA.p);
|
|
|
|
mbedtls_free(rB.p);
|
|
|
|
mbedtls_free(rR.p);
|
|
|
|
mbedtls_free(X);
|
|
|
|
mbedtls_free((mbedtls_mpi_uint *) m.p);
|
2022-12-20 13:55:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_free(&m);
|
2022-12-13 10:53:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_mul_neg(char *input_A,
|
|
|
|
char *input_B,
|
|
|
|
char *input_N,
|
|
|
|
char *result,
|
|
|
|
int exp_ret)
|
2022-12-13 10:53:50 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
|
2022-12-20 18:09:49 +01:00
|
|
|
mbedtls_mpi_mod_residue rA = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue rB = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue rR = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue rX = { NULL, 0 };
|
|
|
|
|
2022-12-13 10:53:50 +01:00
|
|
|
mbedtls_mpi_mod_modulus m;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&m);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2022-12-21 11:54:22 +01:00
|
|
|
mbedtls_mpi_mod_modulus fake_m;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&fake_m);
|
2022-12-21 11:54:22 +01:00
|
|
|
|
2023-05-09 15:26:26 +02:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_modulus(&m, input_N,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY), 0);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(test_read_residue(&rA, &m, input_A, 1), 0);
|
|
|
|
TEST_EQUAL(test_read_residue(&rB, &m, input_B, 1), 0);
|
|
|
|
TEST_EQUAL(test_read_residue(&rR, &m, result, 1), 0);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2022-12-16 16:31:59 +01:00
|
|
|
const size_t limbs = m.limbs;
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X, limbs);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_residue_setup(&rX, &m, X, limbs), 0);
|
2022-12-16 16:31:59 +01:00
|
|
|
rX.limbs = rR.limbs;
|
2022-12-13 10:53:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rA, &rB, &m), exp_ret);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
|
|
|
/* Check when m is not initialized */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_mod_mul(&rX, &rA, &rB, &fake_m),
|
|
|
|
MBEDTLS_ERR_MPI_BAD_INPUT_DATA);
|
2022-12-13 10:53:50 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(rA.p);
|
|
|
|
mbedtls_free(rB.p);
|
|
|
|
mbedtls_free(rR.p);
|
|
|
|
mbedtls_free(X);
|
|
|
|
mbedtls_free((mbedtls_mpi_uint *) m.p);
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus_free(&m);
|
|
|
|
mbedtls_mpi_mod_modulus_free(&fake_m);
|
2022-12-13 10:53:50 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-12-01 15:27:37 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_sub(char *input_N,
|
|
|
|
char *input_A, char *input_B,
|
|
|
|
char *input_D, int expected_ret)
|
2022-12-01 15:27:37 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_mod_residue a = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue b = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue d = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue x = { NULL, 0 };
|
|
|
|
mbedtls_mpi_uint *X_raw = NULL;
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&m);
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0,
|
2023-05-09 15:26:26 +02:00
|
|
|
mbedtls_test_read_mpi_modulus(&m, input_N,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
2022-12-01 15:27:37 +01:00
|
|
|
|
|
|
|
/* test_read_residue() normally checks that inputs have the same number of
|
|
|
|
* limbs as the modulus. For negative testing we can ask it to skip this
|
|
|
|
* with a non-zero final parameter. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, test_read_residue(&a, &m, input_A, expected_ret != 0));
|
|
|
|
TEST_EQUAL(0, test_read_residue(&b, &m, input_B, expected_ret != 0));
|
|
|
|
TEST_EQUAL(0, test_read_residue(&d, &m, input_D, expected_ret != 0));
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-12-01 15:27:37 +01:00
|
|
|
size_t limbs = m.limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t bytes = limbs * sizeof(*X_raw);
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_ret == 0) {
|
2022-12-15 11:55:15 +01:00
|
|
|
/* Negative test with too many limbs in output */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X_raw, limbs + 1);
|
2022-12-01 15:27:37 +01:00
|
|
|
|
|
|
|
x.p = X_raw;
|
|
|
|
x.limbs = limbs + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_sub(&x, &a, &b, &m));
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(X_raw);
|
2022-12-15 11:55:15 +01:00
|
|
|
X_raw = NULL;
|
|
|
|
|
|
|
|
/* Negative test with too few limbs in output */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (limbs > 1) {
|
|
|
|
ASSERT_ALLOC(X_raw, limbs - 1);
|
2022-12-15 11:55:15 +01:00
|
|
|
|
2022-12-01 15:27:37 +01:00
|
|
|
x.p = X_raw;
|
|
|
|
x.limbs = limbs - 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_sub(&x, &a, &b, &m));
|
2022-12-15 11:55:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(X_raw);
|
2022-12-15 11:55:15 +01:00
|
|
|
X_raw = NULL;
|
2022-12-01 15:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Negative testing with too many/too few limbs in a and b is covered by
|
2022-12-15 11:55:15 +01:00
|
|
|
* manually-written test cases with expected_ret != 0. */
|
2022-12-01 15:27:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X_raw, limbs);
|
2022-12-15 11:55:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&x, &m, X_raw, limbs));
|
2022-12-01 15:27:37 +01:00
|
|
|
|
|
|
|
/* a - b => Correct result, or expected error */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(expected_ret, mbedtls_mpi_mod_sub(&x, &a, &b, &m));
|
|
|
|
if (expected_ret != 0) {
|
2022-12-01 15:27:37 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, d);
|
2022-12-01 15:27:37 +01:00
|
|
|
|
|
|
|
/* a - b: alias x to a => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(x.p, a.p, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_sub(&x, &x, &b, &m));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, d);
|
2022-12-01 15:27:37 +01:00
|
|
|
|
|
|
|
/* a - b: alias x to b => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(x.p, b.p, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_sub(&x, &a, &x, &m));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, d);
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(a.p, b.p, bytes) == 0) {
|
2022-12-01 15:27:37 +01:00
|
|
|
/* a == b: alias a and b */
|
|
|
|
|
|
|
|
/* a - a => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_sub(&x, &a, &a, &m));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, d);
|
2022-12-01 15:27:37 +01:00
|
|
|
|
|
|
|
/* a - a: x, a, b all aliased together => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(x.p, a.p, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_sub(&x, &x, &x, &m));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, d);
|
2022-12-01 15:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free((void *) m.p); /* mbedtls_mpi_mod_modulus_free() sets m.p = NULL */
|
|
|
|
mbedtls_mpi_mod_modulus_free(&m);
|
2022-12-01 15:27:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(a.p);
|
|
|
|
mbedtls_free(b.p);
|
|
|
|
mbedtls_free(d.p);
|
|
|
|
mbedtls_free(X_raw);
|
2022-12-01 15:27:37 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-12-15 17:59:40 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_inv_mont(char *input_N,
|
|
|
|
char *input_A, char *input_I,
|
|
|
|
int expected_ret)
|
2022-12-15 17:59:40 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_mod_residue a = { NULL, 0 }; /* argument */
|
|
|
|
mbedtls_mpi_mod_residue i = { NULL, 0 }; /* expected inverse wrt N */
|
|
|
|
mbedtls_mpi_mod_residue x = { NULL, 0 }; /* output */
|
|
|
|
mbedtls_mpi_uint *X_raw = NULL;
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus N;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&N);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0,
|
2023-05-09 15:26:26 +02:00
|
|
|
mbedtls_test_read_mpi_modulus(&N, input_N,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
2022-12-15 17:59:40 +01:00
|
|
|
|
|
|
|
/* test_read_residue() normally checks that inputs have the same number of
|
|
|
|
* limbs as the modulus. For negative testing we can ask it to skip this
|
|
|
|
* with a non-zero final parameter. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, test_read_residue(&a, &N, input_A, expected_ret != 0));
|
|
|
|
TEST_EQUAL(0, test_read_residue(&i, &N, input_I, expected_ret != 0));
|
2022-12-15 17:59:40 +01:00
|
|
|
|
|
|
|
size_t limbs = N.limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t bytes = limbs * sizeof(*X_raw);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X_raw, limbs);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&x, &N, X_raw, limbs));
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(expected_ret, mbedtls_mpi_mod_inv(&x, &a, &N));
|
|
|
|
if (expected_ret == 0) {
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, i);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
|
|
|
/* a^-1: alias x to a => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(x.p, a.p, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_inv(&x, &x, &N));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, i);
|
2022-12-15 17:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free((void *) N.p); /* mbedtls_mpi_mod_modulus_free() sets N.p = NULL */
|
|
|
|
mbedtls_mpi_mod_modulus_free(&N);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(a.p);
|
|
|
|
mbedtls_free(i.p);
|
|
|
|
mbedtls_free(X_raw);
|
2022-12-15 17:59:40 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_inv_non_mont(char *input_N,
|
|
|
|
char *input_A, char *input_I,
|
|
|
|
int expected_ret)
|
2022-12-15 17:59:40 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_mod_residue a = { NULL, 0 }; /* argument */
|
|
|
|
mbedtls_mpi_mod_residue i = { NULL, 0 }; /* expected inverse wrt N */
|
|
|
|
mbedtls_mpi_mod_residue x = { NULL, 0 }; /* output */
|
|
|
|
mbedtls_mpi_uint *X_raw = NULL;
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus N;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&N);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0,
|
2023-05-09 15:26:26 +02:00
|
|
|
mbedtls_test_read_mpi_modulus(&N, input_N,
|
|
|
|
MBEDTLS_MPI_MOD_REP_OPT_RED));
|
2022-12-15 17:59:40 +01:00
|
|
|
|
|
|
|
/* test_read_residue() normally checks that inputs have the same number of
|
|
|
|
* limbs as the modulus. For negative testing we can ask it to skip this
|
|
|
|
* with a non-zero final parameter. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, test_read_residue(&a, &N, input_A, expected_ret != 0));
|
|
|
|
TEST_EQUAL(0, test_read_residue(&i, &N, input_I, expected_ret != 0));
|
2022-12-15 17:59:40 +01:00
|
|
|
|
|
|
|
size_t limbs = N.limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t bytes = limbs * sizeof(*X_raw);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X_raw, limbs);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&x, &N, X_raw, limbs));
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(expected_ret, mbedtls_mpi_mod_inv(&x, &a, &N));
|
|
|
|
if (expected_ret == 0) {
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, i);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
|
|
|
/* a^-1: alias x to a => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(x.p, a.p, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_inv(&x, &x, &N));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, i);
|
2022-12-15 17:59:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free((void *) N.p); /* mbedtls_mpi_mod_modulus_free() sets N.p = NULL */
|
|
|
|
mbedtls_mpi_mod_modulus_free(&N);
|
2022-12-15 17:59:40 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(a.p);
|
|
|
|
mbedtls_free(i.p);
|
|
|
|
mbedtls_free(X_raw);
|
2022-12-15 17:59:40 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-11-29 13:25:05 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_add(char *input_N,
|
|
|
|
char *input_A, char *input_B,
|
|
|
|
char *input_S, int expected_ret)
|
2022-11-29 13:25:05 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_mod_residue a = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue b = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue s = { NULL, 0 };
|
|
|
|
mbedtls_mpi_mod_residue x = { NULL, 0 };
|
|
|
|
mbedtls_mpi_uint *X_raw = NULL;
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&m);
|
2022-11-29 13:25:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0,
|
2023-05-09 15:26:26 +02:00
|
|
|
mbedtls_test_read_mpi_modulus(&m, input_N,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
2022-11-29 13:25:05 +01:00
|
|
|
|
|
|
|
/* test_read_residue() normally checks that inputs have the same number of
|
|
|
|
* limbs as the modulus. For negative testing we can ask it to skip this
|
|
|
|
* with a non-zero final parameter. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, test_read_residue(&a, &m, input_A, expected_ret != 0));
|
|
|
|
TEST_EQUAL(0, test_read_residue(&b, &m, input_B, expected_ret != 0));
|
|
|
|
TEST_EQUAL(0, test_read_residue(&s, &m, input_S, expected_ret != 0));
|
2022-11-29 13:25:05 +01:00
|
|
|
|
|
|
|
size_t limbs = m.limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t bytes = limbs * sizeof(*X_raw);
|
2022-11-29 13:25:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_ret == 0) {
|
2022-11-29 13:25:05 +01:00
|
|
|
/* Negative test with too many limbs in output */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X_raw, limbs + 1);
|
2022-12-13 18:19:01 +01:00
|
|
|
|
2022-11-29 13:25:05 +01:00
|
|
|
x.p = X_raw;
|
|
|
|
x.limbs = limbs + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_add(&x, &a, &b, &m));
|
2022-11-29 13:25:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(X_raw);
|
2022-12-13 18:19:01 +01:00
|
|
|
X_raw = NULL;
|
|
|
|
|
2022-11-29 13:25:05 +01:00
|
|
|
/* Negative test with too few limbs in output */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (limbs > 1) {
|
|
|
|
ASSERT_ALLOC(X_raw, limbs - 1);
|
2022-12-13 18:19:01 +01:00
|
|
|
|
2022-11-29 13:25:05 +01:00
|
|
|
x.p = X_raw;
|
|
|
|
x.limbs = limbs - 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_add(&x, &a, &b, &m));
|
2022-12-13 18:19:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(X_raw);
|
2022-12-13 18:19:01 +01:00
|
|
|
X_raw = NULL;
|
2022-11-29 13:25:05 +01:00
|
|
|
}
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-11-29 13:25:05 +01:00
|
|
|
/* Negative testing with too many/too few limbs in a and b is covered by
|
|
|
|
* manually-written test cases with oret != 0. */
|
|
|
|
}
|
|
|
|
|
2022-12-13 18:19:01 +01:00
|
|
|
/* Allocate correct number of limbs for X_raw */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X_raw, limbs);
|
2022-12-13 18:19:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&x, &m, X_raw, limbs));
|
2022-11-29 13:25:05 +01:00
|
|
|
|
|
|
|
/* A + B => Correct result or expected error */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(expected_ret, mbedtls_mpi_mod_add(&x, &a, &b, &m));
|
|
|
|
if (expected_ret != 0) {
|
2022-11-29 13:25:05 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-29 13:25:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, s);
|
2022-11-29 13:25:05 +01:00
|
|
|
|
|
|
|
/* a + b: alias x to a => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(x.p, a.p, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_add(&x, &x, &b, &m));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, s);
|
2022-11-29 13:25:05 +01:00
|
|
|
|
|
|
|
/* a + b: alias x to b => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(x.p, b.p, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_add(&x, &a, &x, &m));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, s);
|
2022-11-29 13:25:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(a.p, b.p, bytes) == 0) {
|
2022-11-29 13:25:05 +01:00
|
|
|
/* a == b: alias a and b */
|
|
|
|
|
|
|
|
/* a + a => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_add(&x, &a, &a, &m));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, s);
|
2022-11-29 13:25:05 +01:00
|
|
|
|
|
|
|
/* a + a: x, a, b all aliased together => Correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(x.p, a.p, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_add(&x, &x, &x, &m));
|
|
|
|
TEST_COMPARE_MPI_RESIDUES(x, s);
|
2022-11-29 13:25:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free((void *) m.p); /* mbedtls_mpi_mod_modulus_free() sets m.p = NULL */
|
|
|
|
mbedtls_mpi_mod_modulus_free(&m);
|
2022-11-29 13:25:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(a.p);
|
|
|
|
mbedtls_free(b.p);
|
|
|
|
mbedtls_free(s.p);
|
|
|
|
mbedtls_free(X_raw);
|
2022-11-29 13:25:05 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-11-10 17:56:02 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_residue_setup(char *input_N, char *input_R, int ret)
|
2022-11-16 17:29:15 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *N = NULL;
|
|
|
|
mbedtls_mpi_uint *R = NULL;
|
2022-11-24 10:09:47 +01:00
|
|
|
size_t n_limbs, r_limbs;
|
2022-11-16 17:29:15 +01:00
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
mbedtls_mpi_mod_residue r;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&m);
|
2022-11-16 17:29:15 +01:00
|
|
|
|
2022-11-24 10:09:47 +01:00
|
|
|
/* Allocate the memory for intermediate data structures */
|
2023-01-11 14:50:10 +01:00
|
|
|
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));
|
2022-11-24 10:09:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
2022-11-16 17:29:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(ret, mbedtls_mpi_mod_residue_setup(&r, &m, R, r_limbs));
|
2022-11-16 17:29:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
TEST_EQUAL(r.limbs, r_limbs);
|
|
|
|
TEST_ASSERT(r.p == R);
|
2022-11-26 12:47:14 +01:00
|
|
|
}
|
|
|
|
|
2022-11-16 17:29:15 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_free(&m);
|
|
|
|
mbedtls_free(N);
|
|
|
|
mbedtls_free(R);
|
2022-11-16 17:29:15 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-11-24 10:09:47 +01:00
|
|
|
|
2022-11-16 17:29:15 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_io_neg(char *input_N, data_t *buf, int ret)
|
2022-11-10 17:56:02 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *N = NULL;
|
|
|
|
mbedtls_mpi_uint *R = NULL;
|
|
|
|
|
|
|
|
mbedtls_mpi_mod_modulus m;
|
2022-11-26 19:46:54 +01:00
|
|
|
mbedtls_mpi_mod_residue r = { NULL, 0 };
|
2022-11-25 20:32:10 +01:00
|
|
|
mbedtls_mpi_mod_ext_rep endian = MBEDTLS_MPI_MOD_EXT_REP_LE;
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&m);
|
2022-11-25 16:57:04 +01:00
|
|
|
|
2022-11-26 13:20:41 +01:00
|
|
|
size_t n_limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&N, &n_limbs, input_N));
|
2022-11-26 13:20:41 +01:00
|
|
|
size_t r_limbs = n_limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(R, r_limbs);
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-26 19:46:54 +01:00
|
|
|
/* modulus->p == NULL || residue->p == NULL ( m has not been set-up ) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_read(&r, &m, buf->x, buf->len, endian));
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_write(&r, &m, buf->x, buf->len, endian));
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-26 19:46:54 +01:00
|
|
|
/* Set up modulus and test with residue->p == NULL */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_read(&r, &m, buf->x, buf->len, endian));
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_write(&r, &m, buf->x, buf->len, endian));
|
2022-11-26 19:46:54 +01:00
|
|
|
|
|
|
|
/* Do the rest of the tests with a residue set up with the input data */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&r, &m, R, r_limbs));
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-26 15:59:27 +01:00
|
|
|
/* Fail for r_limbs < m->limbs */
|
|
|
|
r.limbs--;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(r.limbs < m.limbs);
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_read(&r, &m, buf->x, buf->len, endian));
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_write(&r, &m, buf->x, buf->len, endian));
|
2022-11-26 15:59:27 +01:00
|
|
|
r.limbs++;
|
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
/* Fail for r_limbs > m->limbs */
|
2022-11-26 15:59:27 +01:00
|
|
|
m.limbs--;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(r.limbs > m.limbs);
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_read(&r, &m, buf->x, buf->len, endian));
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
|
|
|
|
mbedtls_mpi_mod_write(&r, &m, buf->x, buf->len, endian));
|
2022-11-26 15:59:27 +01:00
|
|
|
m.limbs++;
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
/* Test the read */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(ret, mbedtls_mpi_mod_read(&r, &m, buf->x, buf->len, endian));
|
2022-11-02 15:35:17 +01:00
|
|
|
|
2022-11-25 20:32:10 +01:00
|
|
|
/* Test write overflow only when the representation is large and read is successful */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (r.limbs > 1 && ret == 0) {
|
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL,
|
|
|
|
mbedtls_mpi_mod_write(&r, &m, buf->x, 1, endian));
|
|
|
|
}
|
2022-11-26 15:59:27 +01:00
|
|
|
|
2022-11-10 17:56:02 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_residue_release(&r);
|
|
|
|
mbedtls_mpi_mod_modulus_free(&m);
|
|
|
|
mbedtls_free(N);
|
|
|
|
mbedtls_free(R);
|
2022-11-10 17:56:02 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_mod_io(char *input_N, data_t *input_A, int endian)
|
2022-11-10 17:56:02 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *N = NULL;
|
|
|
|
mbedtls_mpi_uint *R = NULL;
|
2022-11-26 16:39:02 +01:00
|
|
|
mbedtls_mpi_uint *R_COPY = NULL;
|
2022-11-26 18:23:16 +01:00
|
|
|
unsigned char *obuf = NULL;
|
|
|
|
unsigned char *ref_buf = NULL;
|
2022-11-10 17:56:02 +01:00
|
|
|
mbedtls_mpi_mod_modulus m;
|
|
|
|
mbedtls_mpi_mod_residue r;
|
2022-11-26 16:39:02 +01:00
|
|
|
mbedtls_mpi_mod_residue r_copy;
|
2022-11-10 17:56:02 +01:00
|
|
|
size_t n_limbs, n_bytes, a_bytes;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_init(&m);
|
2022-11-25 16:57:04 +01:00
|
|
|
|
2022-11-10 17:56:02 +01:00
|
|
|
/* Read inputs */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&N, &n_limbs, input_N));
|
|
|
|
n_bytes = n_limbs * sizeof(mbedtls_mpi_uint);
|
2022-11-26 15:19:02 +01:00
|
|
|
a_bytes = input_A->len;
|
2022-11-10 17:56:02 +01:00
|
|
|
|
|
|
|
/* Allocate the memory for intermediate data structures */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(R, n_bytes);
|
|
|
|
ASSERT_ALLOC(R_COPY, n_bytes);
|
2022-11-10 17:56:02 +01:00
|
|
|
|
|
|
|
/* Test that input's size is not greater to modulo's */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(a_bytes, n_bytes);
|
2022-11-10 17:56:02 +01:00
|
|
|
|
|
|
|
/* Init Structures */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_modulus_setup(&m, N, n_limbs,
|
|
|
|
MBEDTLS_MPI_MOD_REP_MONTGOMERY));
|
2022-11-10 17:56:02 +01:00
|
|
|
|
|
|
|
/* Enforcing p_limbs >= m->limbs */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&r, &m, R, n_limbs));
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_read(&r, &m, input_A->x, input_A->len,
|
|
|
|
endian));
|
2022-11-10 17:56:02 +01:00
|
|
|
|
2022-11-26 18:23:16 +01:00
|
|
|
/* Read a copy for checking that writing didn't change the value of r */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_residue_setup(&r_copy, &m,
|
|
|
|
R_COPY, n_limbs));
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_read(&r_copy, &m, input_A->x, input_A->len,
|
|
|
|
endian));
|
2022-11-26 16:39:02 +01:00
|
|
|
|
2022-11-26 18:23:16 +01:00
|
|
|
/* Get number of bytes without leading zeroes */
|
|
|
|
size_t a_bytes_trimmed = a_bytes;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (a_bytes_trimmed > 0) {
|
|
|
|
unsigned char *r_byte_array = (unsigned char *) r.p;
|
|
|
|
if (r_byte_array[--a_bytes_trimmed] != 0) {
|
2022-11-26 18:23:16 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-26 18:23:16 +01:00
|
|
|
}
|
|
|
|
a_bytes_trimmed++;
|
|
|
|
|
|
|
|
/* Test write with three output buffer sizes: tight, same as input and
|
|
|
|
* longer than the input */
|
|
|
|
size_t obuf_sizes[3];
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t obuf_sizes_len = sizeof(obuf_sizes) / sizeof(obuf_sizes[0]);
|
2022-11-26 18:23:16 +01:00
|
|
|
obuf_sizes[0] = a_bytes_trimmed;
|
|
|
|
obuf_sizes[1] = a_bytes;
|
|
|
|
obuf_sizes[2] = a_bytes + 8;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < obuf_sizes_len; i++) {
|
|
|
|
ASSERT_ALLOC(obuf, obuf_sizes[i]);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_mod_write(&r, &m, obuf, obuf_sizes[i], endian));
|
2022-11-26 18:23:16 +01:00
|
|
|
|
|
|
|
/* Make sure that writing didn't corrupt the value of r */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(r.p, r.limbs, r_copy.p, r_copy.limbs);
|
2022-11-26 18:23:16 +01:00
|
|
|
|
|
|
|
/* Set up reference output for checking the result */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(ref_buf, obuf_sizes[i]);
|
|
|
|
switch (endian) {
|
2022-11-26 18:23:16 +01:00
|
|
|
case MBEDTLS_MPI_MOD_EXT_REP_LE:
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ref_buf, input_A->x, a_bytes_trimmed);
|
2022-11-26 18:23:16 +01:00
|
|
|
break;
|
|
|
|
case MBEDTLS_MPI_MOD_EXT_REP_BE:
|
2023-01-11 14:50:10 +01:00
|
|
|
{
|
|
|
|
size_t a_offset = input_A->len - a_bytes_trimmed;
|
|
|
|
size_t ref_offset = obuf_sizes[i] - a_bytes_trimmed;
|
|
|
|
memcpy(ref_buf + ref_offset, input_A->x + a_offset,
|
|
|
|
a_bytes_trimmed);
|
|
|
|
}
|
|
|
|
break;
|
2022-11-26 18:23:16 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(0);
|
2022-11-26 18:23:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the result */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(obuf, obuf_sizes[i], ref_buf, obuf_sizes[i]);
|
2022-11-26 18:23:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(ref_buf);
|
2022-11-26 18:23:16 +01:00
|
|
|
ref_buf = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(obuf);
|
2022-11-26 18:23:16 +01:00
|
|
|
obuf = NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:56:02 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_mod_modulus_free(&m);
|
|
|
|
mbedtls_free(N);
|
|
|
|
mbedtls_free(R);
|
|
|
|
mbedtls_free(R_COPY);
|
|
|
|
mbedtls_free(obuf);
|
2022-11-10 17:56:02 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|