2022-10-04 17:16:16 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include "mbedtls/bignum.h"
|
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "bignum_core.h"
|
|
|
|
#include "constant_time_internal.h"
|
|
|
|
#include "test/constant_flow.h"
|
|
|
|
|
2022-10-25 18:01:19 +02:00
|
|
|
/** Verifies mbedtls_mpi_core_add().
|
|
|
|
*
|
|
|
|
* \param[in] A Little-endian presentation of the left operand.
|
|
|
|
* \param[in] B Little-endian presentation of the right operand.
|
|
|
|
* \param limbs Number of limbs in each MPI (\p A, \p B, \p S and \p X).
|
|
|
|
* \param[in] S Little-endian presentation of the expected sum.
|
|
|
|
* \param carry Expected carry from the addition.
|
|
|
|
* \param[in,out] X Temporary storage to be used for results.
|
|
|
|
*
|
|
|
|
* \return 1 if mbedtls_mpi_core_add() passes this test, otherwise 0.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mpi_core_verify_add(mbedtls_mpi_uint *A,
|
|
|
|
mbedtls_mpi_uint *B,
|
|
|
|
size_t limbs,
|
|
|
|
mbedtls_mpi_uint *S,
|
|
|
|
int carry,
|
|
|
|
mbedtls_mpi_uint *X)
|
2022-10-25 18:01:19 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t bytes = limbs * sizeof(*A);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* The test cases have A <= B to avoid repetition, so we test A + B then,
|
|
|
|
* if A != B, B + A. If A == B, we can test when A and B are aliased */
|
|
|
|
|
|
|
|
/* A + B */
|
|
|
|
|
|
|
|
/* A + B => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add(X, A, B, limbs));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* A + B; alias output and first operand => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X, A, bytes);
|
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add(X, X, B, limbs));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* A + B; alias output and second operand => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X, B, bytes);
|
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add(X, A, X, limbs));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(A, B, bytes) == 0) {
|
2022-10-25 18:01:19 +02:00
|
|
|
/* A == B, so test where A and B are aliased */
|
|
|
|
|
|
|
|
/* A + A => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add(X, A, A, limbs));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* A + A, output aliased to both operands => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X, A, bytes);
|
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add(X, X, X, limbs));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
|
|
|
} else {
|
2022-10-25 18:01:19 +02:00
|
|
|
/* A != B, so test B + A */
|
|
|
|
|
|
|
|
/* B + A => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add(X, B, A, limbs));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* B + A; alias output and first operand => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X, B, bytes);
|
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add(X, X, A, limbs));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* B + A; alias output and second operand => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X, A, bytes);
|
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add(X, B, X, limbs));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Verifies mbedtls_mpi_core_add_if().
|
|
|
|
*
|
|
|
|
* \param[in] A Little-endian presentation of the left operand.
|
|
|
|
* \param[in] B Little-endian presentation of the right operand.
|
|
|
|
* \param limbs Number of limbs in each MPI (\p A, \p B, \p S and \p X).
|
|
|
|
* \param[in] S Little-endian presentation of the expected sum.
|
|
|
|
* \param carry Expected carry from the addition.
|
|
|
|
* \param[in,out] X Temporary storage to be used for results.
|
|
|
|
*
|
|
|
|
* \return 1 if mbedtls_mpi_core_add_if() passes this test, otherwise 0.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mpi_core_verify_add_if(mbedtls_mpi_uint *A,
|
|
|
|
mbedtls_mpi_uint *B,
|
|
|
|
size_t limbs,
|
|
|
|
mbedtls_mpi_uint *S,
|
|
|
|
int carry,
|
|
|
|
mbedtls_mpi_uint *X)
|
2022-10-25 18:01:19 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t bytes = limbs * sizeof(*A);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* The test cases have A <= B to avoid repetition, so we test A + B then,
|
|
|
|
* if A != B, B + A. If A == B, we can test when A and B are aliased */
|
|
|
|
|
|
|
|
/* A + B */
|
|
|
|
|
|
|
|
/* cond = 0 => X unchanged, no carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X, A, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_core_add_if(X, B, limbs, 0));
|
|
|
|
ASSERT_COMPARE(X, bytes, A, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* cond = 1 => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add_if(X, B, limbs, 1));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(A, B, bytes) == 0) {
|
2022-10-25 18:01:19 +02:00
|
|
|
/* A == B, so test where A and B are aliased */
|
|
|
|
|
|
|
|
/* cond = 0 => X unchanged, no carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X, B, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_core_add_if(X, X, limbs, 0));
|
|
|
|
ASSERT_COMPARE(X, bytes, B, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* cond = 1 => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add_if(X, X, limbs, 1));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
|
|
|
} else {
|
2022-10-25 18:01:19 +02:00
|
|
|
/* A != B, so test B + A */
|
|
|
|
|
|
|
|
/* cond = 0 => d unchanged, no carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(X, B, bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_core_add_if(X, A, limbs, 0));
|
|
|
|
ASSERT_COMPARE(X, bytes, B, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
|
|
|
|
/* cond = 1 => correct result and carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_add_if(X, A, limbs, 1));
|
|
|
|
ASSERT_COMPARE(X, bytes, S, bytes);
|
2022-10-25 18:01:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-04 17:16:16 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_BIGNUM_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_core_io_null()
|
|
|
|
{
|
|
|
|
mbedtls_mpi_uint X = 0;
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_mpi_core_read_be(&X, 1, NULL, 0);
|
|
|
|
TEST_EQUAL(ret, 0);
|
|
|
|
ret = mbedtls_mpi_core_write_be(&X, 1, NULL, 0);
|
|
|
|
TEST_EQUAL(ret, 0);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_mpi_core_read_be(NULL, 0, NULL, 0);
|
|
|
|
TEST_EQUAL(ret, 0);
|
|
|
|
ret = mbedtls_mpi_core_write_be(NULL, 0, NULL, 0);
|
|
|
|
TEST_EQUAL(ret, 0);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_mpi_core_read_le(&X, 1, NULL, 0);
|
|
|
|
TEST_EQUAL(ret, 0);
|
|
|
|
ret = mbedtls_mpi_core_write_le(&X, 1, NULL, 0);
|
|
|
|
TEST_EQUAL(ret, 0);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_mpi_core_read_le(NULL, 0, NULL, 0);
|
|
|
|
TEST_EQUAL(ret, 0);
|
|
|
|
ret = mbedtls_mpi_core_write_le(NULL, 0, NULL, 0);
|
|
|
|
TEST_EQUAL(ret, 0);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_io_be(data_t *input, int nb_int, int nx_32_int, int iret,
|
|
|
|
int oret)
|
2022-10-04 17:16:16 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (iret != 0) {
|
|
|
|
TEST_ASSERT(oret == 0);
|
|
|
|
}
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_S(0, nb_int);
|
2022-10-04 17:16:16 +02:00
|
|
|
size_t nb = nb_int;
|
|
|
|
|
|
|
|
unsigned char buf[1024];
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(nb, sizeof(buf));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
|
|
|
* to halve the number of limbs to have the same size. */
|
|
|
|
size_t nx;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_S(0, nx_32_int);
|
|
|
|
if (sizeof(mbedtls_mpi_uint) == 8) {
|
2022-10-04 17:16:16 +02:00
|
|
|
nx = nx_32_int / 2 + nx_32_int % 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-10-04 17:16:16 +02:00
|
|
|
nx = nx_32_int;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint X[sizeof(buf) / sizeof(mbedtls_mpi_uint)];
|
|
|
|
TEST_LE_U(nx, sizeof(X) / sizeof(X[0]));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_mpi_core_read_be(X, nx, input->x, input->len);
|
|
|
|
TEST_EQUAL(ret, iret);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (iret == 0) {
|
|
|
|
ret = mbedtls_mpi_core_write_be(X, nx, buf, nb);
|
|
|
|
TEST_EQUAL(ret, oret);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((iret == 0) && (oret == 0)) {
|
|
|
|
if (nb > input->len) {
|
2022-10-04 17:16:16 +02:00
|
|
|
size_t leading_zeroes = nb - input->len;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(buf + nb - input->len, input->x, input->len) == 0);
|
|
|
|
for (size_t i = 0; i < leading_zeroes; i++) {
|
|
|
|
TEST_EQUAL(buf[i], 0);
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-04 17:16:16 +02:00
|
|
|
size_t leading_zeroes = input->len - nb;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(input->x + input->len - nb, buf, nb) == 0);
|
|
|
|
for (size_t i = 0; i < leading_zeroes; i++) {
|
|
|
|
TEST_EQUAL(input->x[i], 0);
|
|
|
|
}
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_io_le(data_t *input, int nb_int, int nx_32_int, int iret,
|
|
|
|
int oret)
|
2022-10-04 17:16:16 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (iret != 0) {
|
|
|
|
TEST_ASSERT(oret == 0);
|
|
|
|
}
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_S(0, nb_int);
|
2022-10-04 17:16:16 +02:00
|
|
|
size_t nb = nb_int;
|
|
|
|
|
|
|
|
unsigned char buf[1024];
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(nb, sizeof(buf));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* nx_32_int is the number of 32 bit limbs, if we have 64 bit limbs we need
|
|
|
|
* to halve the number of limbs to have the same size. */
|
|
|
|
size_t nx;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_S(0, nx_32_int);
|
|
|
|
if (sizeof(mbedtls_mpi_uint) == 8) {
|
2022-10-04 17:16:16 +02:00
|
|
|
nx = nx_32_int / 2 + nx_32_int % 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-10-04 17:16:16 +02:00
|
|
|
nx = nx_32_int;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint X[sizeof(buf) / sizeof(mbedtls_mpi_uint)];
|
|
|
|
TEST_LE_U(nx, sizeof(X) / sizeof(X[0]));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_mpi_core_read_le(X, nx, input->x, input->len);
|
|
|
|
TEST_EQUAL(ret, iret);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (iret == 0) {
|
|
|
|
ret = mbedtls_mpi_core_write_le(X, nx, buf, nb);
|
|
|
|
TEST_EQUAL(ret, oret);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((iret == 0) && (oret == 0)) {
|
|
|
|
if (nb > input->len) {
|
|
|
|
TEST_ASSERT(memcmp(buf, input->x, input->len) == 0);
|
|
|
|
for (size_t i = input->len; i < nb; i++) {
|
|
|
|
TEST_EQUAL(buf[i], 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(memcmp(input->x, buf, nb) == 0);
|
|
|
|
for (size_t i = nb; i < input->len; i++) {
|
|
|
|
TEST_EQUAL(input->x[i], 0);
|
|
|
|
}
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-10-12 18:15:26 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_bitlen(char *input_X, int nr_bits)
|
2022-10-12 18:15:26 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
size_t limbs;
|
|
|
|
|
2023-04-25 19:07:29 +02:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_core(&X, &limbs, input_X), 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_core_bitlen(X, limbs), nr_bits);
|
2022-10-12 18:15:26 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(X);
|
2022-10-12 18:15:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-04-19 18:40:28 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-04-21 13:54:40 +02:00
|
|
|
void mpi_core_clz(int leading_zeros, int trailing_zeros)
|
2023-04-19 18:40:28 +02:00
|
|
|
{
|
2023-04-21 13:54:40 +02:00
|
|
|
if ((size_t) (leading_zeros + trailing_zeros) >= (sizeof(mbedtls_mpi_uint) * 8)) {
|
2023-04-19 18:40:28 +02:00
|
|
|
// can't fit required number of leading and trailing zeros - skip test
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-04-21 13:54:40 +02:00
|
|
|
// Construct a test input value where the count of leading zeros and
|
|
|
|
// trailing zeros is given in the test case, and we add ones to fill
|
|
|
|
// the gap.
|
2023-04-19 18:40:28 +02:00
|
|
|
mbedtls_mpi_uint x;
|
2023-04-21 13:54:40 +02:00
|
|
|
if ((leading_zeros + trailing_zeros) > 0) {
|
2023-04-20 13:28:59 +02:00
|
|
|
// some zero bits
|
2023-04-21 13:54:40 +02:00
|
|
|
uint32_t s = (sizeof(mbedtls_mpi_uint) * 8 - leading_zeros - trailing_zeros);
|
|
|
|
x = ((((mbedtls_mpi_uint) 1) << s) - 1) << trailing_zeros;
|
2023-04-19 18:40:28 +02:00
|
|
|
} else {
|
2023-04-20 13:28:59 +02:00
|
|
|
// all bits set
|
|
|
|
x = ~((mbedtls_mpi_uint) 0);
|
2023-04-19 18:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t n = mbedtls_mpi_core_clz(x);
|
2023-04-21 13:54:40 +02:00
|
|
|
TEST_EQUAL(n, leading_zeros);
|
2023-04-19 18:40:28 +02:00
|
|
|
exit:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
|
2022-10-04 17:16:16 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_lt_ct(char *input_X, char *input_Y, int exp_ret)
|
2022-10-04 17:16:16 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
size_t X_limbs;
|
|
|
|
mbedtls_mpi_uint *Y = NULL;
|
|
|
|
size_t Y_limbs;
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&X, &X_limbs, input_X));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&Y, &Y_limbs, input_Y));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* We need two same-length limb arrays */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(X_limbs, Y_limbs);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_SECRET(X, X_limbs * sizeof(mbedtls_mpi_uint));
|
|
|
|
TEST_CF_SECRET(Y, X_limbs * sizeof(mbedtls_mpi_uint));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_mpi_core_lt_ct(X, Y, X_limbs);
|
|
|
|
TEST_EQUAL(ret, exp_ret);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(X);
|
|
|
|
mbedtls_free(Y);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-09-20 18:38:35 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_uint_le_mpi(char *input_A)
|
2022-09-20 18:38:35 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *A = NULL;
|
|
|
|
size_t A_limbs = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &A_limbs, input_A), 0);
|
2022-09-20 18:38:35 +02:00
|
|
|
|
|
|
|
int is_large = 0; /* nonzero limbs beyond the lowest-order one? */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 1; i < A_limbs; i++) {
|
|
|
|
if (A[i] != 0) {
|
2022-09-20 18:38:35 +02:00
|
|
|
is_large = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_SECRET(A, A_limbs * sizeof(*A));
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_mpi_core_uint_le_mpi(0, A, A_limbs), 1);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_core_uint_le_mpi(A[0], A, A_limbs), 1);
|
|
|
|
|
|
|
|
if (is_large) {
|
|
|
|
TEST_EQUAL(mbedtls_mpi_core_uint_le_mpi(A[0] + 1,
|
|
|
|
A, A_limbs), 1);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_core_uint_le_mpi((mbedtls_mpi_uint) (-1) >> 1,
|
|
|
|
A, A_limbs), 1);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_core_uint_le_mpi((mbedtls_mpi_uint) (-1),
|
|
|
|
A, A_limbs), 1);
|
|
|
|
} else {
|
|
|
|
TEST_EQUAL(mbedtls_mpi_core_uint_le_mpi(A[0] + 1,
|
|
|
|
A, A_limbs),
|
|
|
|
A[0] + 1 <= A[0]);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_core_uint_le_mpi((mbedtls_mpi_uint) (-1) >> 1,
|
|
|
|
A, A_limbs),
|
|
|
|
(mbedtls_mpi_uint) (-1) >> 1 <= A[0]);
|
|
|
|
TEST_EQUAL(mbedtls_mpi_core_uint_le_mpi((mbedtls_mpi_uint) (-1),
|
|
|
|
A, A_limbs),
|
|
|
|
(mbedtls_mpi_uint) (-1) <= A[0]);
|
2022-09-20 18:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(A);
|
2022-09-20 18:38:35 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_cond_assign(char *input_X,
|
|
|
|
char *input_Y,
|
|
|
|
int input_bytes)
|
2022-10-19 16:15:29 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
mbedtls_mpi_uint *Y = NULL;
|
2022-10-19 17:22:15 +02:00
|
|
|
size_t limbs_X;
|
|
|
|
size_t limbs_Y;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_core(&X, &limbs_X, input_X), 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_core(&Y, &limbs_Y, input_Y), 0);
|
2022-10-19 17:22:15 +02:00
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
size_t limbs = limbs_X;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t copy_limbs = CHARS_TO_LIMBS(input_bytes);
|
|
|
|
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
|
|
|
size_t copy_bytes = copy_limbs * sizeof(mbedtls_mpi_uint);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(limbs_X, limbs_Y);
|
|
|
|
TEST_ASSERT(copy_limbs <= limbs);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
|
|
|
/* condition is false */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_SECRET(X, bytes);
|
|
|
|
TEST_CF_SECRET(Y, bytes);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_cond_assign(X, Y, copy_limbs, 0);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_PUBLIC(X, bytes);
|
|
|
|
TEST_CF_PUBLIC(Y, bytes);
|
2022-10-20 12:27:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(X, Y, bytes) != 0);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
|
|
|
/* condition is true */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_SECRET(X, bytes);
|
|
|
|
TEST_CF_SECRET(Y, bytes);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_cond_assign(X, Y, copy_limbs, 1);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_PUBLIC(X, bytes);
|
|
|
|
TEST_CF_PUBLIC(Y, bytes);
|
2022-10-20 12:27:36 +02:00
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
/* Check if the given length is copied even it is smaller
|
|
|
|
than the length of the given MPIs. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (copy_limbs < limbs) {
|
|
|
|
TEST_CF_PUBLIC(X, bytes);
|
|
|
|
TEST_CF_PUBLIC(Y, bytes);
|
|
|
|
|
|
|
|
ASSERT_COMPARE(X, copy_bytes, Y, copy_bytes);
|
|
|
|
TEST_ASSERT(memcmp(X, Y, bytes) != 0);
|
|
|
|
} else {
|
|
|
|
ASSERT_COMPARE(X, bytes, Y, bytes);
|
2022-10-19 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(X);
|
|
|
|
mbedtls_free(Y);
|
2022-10-19 16:15:29 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_cond_swap(char *input_X,
|
|
|
|
char *input_Y,
|
|
|
|
int input_bytes)
|
2022-10-19 16:15:29 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *tmp_X = NULL;
|
|
|
|
mbedtls_mpi_uint *tmp_Y = NULL;
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
mbedtls_mpi_uint *Y = NULL;
|
2022-10-19 17:22:15 +02:00
|
|
|
size_t limbs_X;
|
|
|
|
size_t limbs_Y;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_core(&tmp_X, &limbs_X, input_X), 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_core(&tmp_Y, &limbs_Y, input_Y), 0);
|
2022-10-19 17:22:15 +02:00
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
size_t limbs = limbs_X;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t copy_limbs = CHARS_TO_LIMBS(input_bytes);
|
|
|
|
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
|
|
|
size_t copy_bytes = copy_limbs * sizeof(mbedtls_mpi_uint);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(limbs_X, limbs_Y);
|
|
|
|
TEST_ASSERT(copy_limbs <= limbs);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X, limbs);
|
|
|
|
memcpy(X, tmp_X, bytes);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(Y, limbs);
|
|
|
|
memcpy(Y, tmp_Y, bytes);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
|
|
|
/* condition is false */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_SECRET(X, bytes);
|
|
|
|
TEST_CF_SECRET(Y, bytes);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_cond_swap(X, Y, copy_limbs, 0);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_PUBLIC(X, bytes);
|
|
|
|
TEST_CF_PUBLIC(Y, bytes);
|
2022-10-20 12:27:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(X, bytes, tmp_X, bytes);
|
|
|
|
ASSERT_COMPARE(Y, bytes, tmp_Y, bytes);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
|
|
|
/* condition is true */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_SECRET(X, bytes);
|
|
|
|
TEST_CF_SECRET(Y, bytes);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_cond_swap(X, Y, copy_limbs, 1);
|
2022-10-19 16:15:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_PUBLIC(X, bytes);
|
|
|
|
TEST_CF_PUBLIC(Y, bytes);
|
2022-10-20 12:27:36 +02:00
|
|
|
|
2022-10-19 16:15:29 +02:00
|
|
|
/* Check if the given length is copied even it is smaller
|
|
|
|
than the length of the given MPIs. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (copy_limbs < limbs) {
|
|
|
|
ASSERT_COMPARE(X, copy_bytes, tmp_Y, copy_bytes);
|
|
|
|
ASSERT_COMPARE(Y, copy_bytes, tmp_X, copy_bytes);
|
|
|
|
TEST_ASSERT(memcmp(X, tmp_X, bytes) != 0);
|
|
|
|
TEST_ASSERT(memcmp(X, tmp_Y, bytes) != 0);
|
|
|
|
TEST_ASSERT(memcmp(Y, tmp_X, bytes) != 0);
|
|
|
|
TEST_ASSERT(memcmp(Y, tmp_Y, bytes) != 0);
|
|
|
|
} else {
|
|
|
|
ASSERT_COMPARE(X, bytes, tmp_Y, bytes);
|
|
|
|
ASSERT_COMPARE(Y, bytes, tmp_X, bytes);
|
2022-10-19 16:15:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(tmp_X);
|
|
|
|
mbedtls_free(tmp_Y);
|
|
|
|
mbedtls_free(X);
|
|
|
|
mbedtls_free(Y);
|
2022-10-19 16:15:29 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-09-21 23:13:33 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_shift_r(char *input, int count, char *result)
|
2022-09-21 23:13:33 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
mbedtls_mpi_uint *Y = NULL;
|
|
|
|
size_t limbs, n;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&X, &limbs, input));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&Y, &n, result));
|
|
|
|
TEST_EQUAL(limbs, n);
|
2022-09-21 23:13:33 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_shift_r(X, limbs, count);
|
|
|
|
ASSERT_COMPARE(X, limbs * ciL, Y, limbs * ciL);
|
2022-09-21 23:13:33 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(X);
|
|
|
|
mbedtls_free(Y);
|
2022-09-21 23:13:33 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-04-25 13:11:44 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void mpi_core_shift_l(char *input, int count, char *result)
|
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
mbedtls_mpi_uint *Y = NULL;
|
|
|
|
size_t limbs, n;
|
|
|
|
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&X, &limbs, input));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&Y, &n, result));
|
|
|
|
TEST_EQUAL(limbs, n);
|
|
|
|
|
|
|
|
mbedtls_mpi_core_shift_l(X, limbs, count);
|
|
|
|
ASSERT_COMPARE(X, limbs * ciL, Y, limbs * ciL);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free(X);
|
|
|
|
mbedtls_free(Y);
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-10-04 17:16:16 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_add_and_add_if(char *input_A, char *input_B,
|
|
|
|
char *input_S, int carry)
|
2022-10-04 17:16:16 +02:00
|
|
|
{
|
2022-10-17 14:47:13 +02:00
|
|
|
mbedtls_mpi_uint *A = NULL; /* first value to add */
|
|
|
|
mbedtls_mpi_uint *B = NULL; /* second value to add */
|
|
|
|
mbedtls_mpi_uint *S = NULL; /* expected result */
|
|
|
|
mbedtls_mpi_uint *X = NULL; /* destination - the in/out first operand */
|
2022-10-25 13:45:50 +02:00
|
|
|
size_t A_limbs, B_limbs, S_limbs;
|
2022-10-17 14:47:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&A, &A_limbs, input_A));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&B, &B_limbs, input_B));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&S, &S_limbs, input_S));
|
2022-10-17 14:47:13 +02:00
|
|
|
|
2022-10-25 13:45:50 +02:00
|
|
|
/* add and add_if expect all operands to be the same length */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(A_limbs, B_limbs);
|
|
|
|
TEST_EQUAL(A_limbs, S_limbs);
|
2022-10-17 14:47:13 +02:00
|
|
|
|
2022-10-25 18:01:19 +02:00
|
|
|
size_t limbs = A_limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(X, limbs);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mpi_core_verify_add(A, B, limbs, S, carry, X));
|
|
|
|
TEST_ASSERT(mpi_core_verify_add_if(A, B, limbs, S, carry, X));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(A);
|
|
|
|
mbedtls_free(B);
|
|
|
|
mbedtls_free(S);
|
|
|
|
mbedtls_free(X);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_sub(char *input_A, char *input_B,
|
|
|
|
char *input_X, int carry)
|
2022-10-04 17:16:16 +02:00
|
|
|
{
|
2022-12-12 18:04:16 +01:00
|
|
|
mbedtls_mpi A, B, X;
|
2022-10-04 17:16:16 +02:00
|
|
|
mbedtls_mpi_uint *a = NULL;
|
|
|
|
mbedtls_mpi_uint *b = NULL;
|
|
|
|
mbedtls_mpi_uint *x = NULL; /* expected */
|
|
|
|
mbedtls_mpi_uint *r = NULL; /* result */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&A);
|
|
|
|
mbedtls_mpi_init(&B);
|
|
|
|
mbedtls_mpi_init(&X);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&A, input_A));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&B, input_B));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&X, input_X));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(1, A.s);
|
|
|
|
TEST_EQUAL(1, B.s);
|
|
|
|
TEST_EQUAL(1, X.s);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* Get the number of limbs we will need */
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t limbs = MAX(A.n, B.n);
|
2022-10-04 17:16:16 +02:00
|
|
|
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
|
|
|
|
|
|
|
/* The result shouldn't have more limbs than the longest input */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(X.n, limbs);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
|
|
|
|
|
|
|
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(a, bytes);
|
|
|
|
ASSERT_ALLOC(b, bytes);
|
|
|
|
ASSERT_ALLOC(x, bytes);
|
|
|
|
ASSERT_ALLOC(r, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
|
|
|
* processed by mbedtls_mpi_core_sub()) are little endian, we can just
|
|
|
|
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC())
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(a, A.p, A.n * sizeof(mbedtls_mpi_uint));
|
|
|
|
memcpy(b, B.p, B.n * sizeof(mbedtls_mpi_uint));
|
|
|
|
memcpy(x, X.p, X.n * sizeof(mbedtls_mpi_uint));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* 1a) r = a - b => we should get the correct carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_sub(r, a, b, limbs));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* 1b) r = a - b => we should get the correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(r, bytes, x, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* 2 and 3 test "r may be aliased to a or b" */
|
|
|
|
/* 2a) r = a; r -= b => we should get the correct carry (use r to avoid clobbering a) */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(r, a, bytes);
|
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_sub(r, r, b, limbs));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* 2b) r -= b => we should get the correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(r, bytes, x, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* 3a) r = b; r = a - r => we should get the correct carry (use r to avoid clobbering b) */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(r, b, bytes);
|
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_sub(r, a, r, limbs));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* 3b) r = a - b => we should get the correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(r, bytes, x, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* 4 tests "r may be aliased to [...] both" */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (A.n == B.n && memcmp(A.p, B.p, bytes) == 0) {
|
|
|
|
memcpy(r, b, bytes);
|
|
|
|
TEST_EQUAL(carry, mbedtls_mpi_core_sub(r, r, r, limbs));
|
|
|
|
ASSERT_COMPARE(r, bytes, x, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(a);
|
|
|
|
mbedtls_free(b);
|
|
|
|
mbedtls_free(x);
|
|
|
|
mbedtls_free(r);
|
|
|
|
|
|
|
|
mbedtls_mpi_free(&A);
|
|
|
|
mbedtls_mpi_free(&B);
|
|
|
|
mbedtls_mpi_free(&X);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_mla(char *input_A, char *input_B, char *input_S,
|
|
|
|
char *input_X4, char *input_cy4,
|
|
|
|
char *input_X8, char *input_cy8)
|
2022-10-04 17:16:16 +02:00
|
|
|
{
|
|
|
|
/* We are testing A += B * s; A, B are MPIs, s is a scalar.
|
|
|
|
*
|
|
|
|
* However, we encode s as an MPI in the .data file as the test framework
|
|
|
|
* currently only supports `int`-typed scalars, and that doesn't cover the
|
|
|
|
* full range of `mbedtls_mpi_uint`.
|
|
|
|
*
|
|
|
|
* We also have the different results for sizeof(mbedtls_mpi_uint) == 4 or 8.
|
|
|
|
*/
|
|
|
|
mbedtls_mpi A, B, S, X4, X8, cy4, cy8;
|
|
|
|
mbedtls_mpi_uint *a = NULL;
|
|
|
|
mbedtls_mpi_uint *x = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&A);
|
|
|
|
mbedtls_mpi_init(&B);
|
|
|
|
mbedtls_mpi_init(&S);
|
|
|
|
mbedtls_mpi_init(&X4);
|
|
|
|
mbedtls_mpi_init(&X8);
|
|
|
|
mbedtls_mpi_init(&cy4);
|
|
|
|
mbedtls_mpi_init(&cy8);
|
|
|
|
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&A, input_A));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&B, input_B));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&S, input_S));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&X4, input_X4));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&cy4, input_cy4));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&X8, input_X8));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&cy8, input_cy8));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* The MPI encoding of scalar s must be only 1 limb */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(1, S.n);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* We only need to work with X4 or X8, and cy4 or cy8, depending on sizeof(mbedtls_mpi_uint) */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi *X = (sizeof(mbedtls_mpi_uint) == 4) ? &X4 : &X8;
|
|
|
|
mbedtls_mpi *cy = (sizeof(mbedtls_mpi_uint) == 4) ? &cy4 : &cy8;
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* The carry should only have one limb */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(1, cy->n);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(1, A.s);
|
|
|
|
TEST_EQUAL(1, B.s);
|
|
|
|
TEST_EQUAL(1, S.s);
|
|
|
|
TEST_EQUAL(1, X->s);
|
|
|
|
TEST_EQUAL(1, cy->s);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* Get the (max) number of limbs we will need */
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t limbs = MAX(A.n, B.n);
|
2022-10-04 17:16:16 +02:00
|
|
|
size_t bytes = limbs * sizeof(mbedtls_mpi_uint);
|
|
|
|
|
|
|
|
/* The result shouldn't have more limbs than the longest input */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(X->n, limbs);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* Now let's get arrays of mbedtls_mpi_uints, rather than MPI structures */
|
|
|
|
|
|
|
|
/* ASSERT_ALLOC() uses calloc() under the hood, so these do get zeroed */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(a, bytes);
|
|
|
|
ASSERT_ALLOC(x, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* Populate the arrays. As the mbedtls_mpi_uint[]s in mbedtls_mpis (and as
|
|
|
|
* processed by mbedtls_mpi_core_mla()) are little endian, we can just
|
|
|
|
* copy what we have as long as MSBs are 0 (which they are from ASSERT_ALLOC()).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(a, A.p, A.n * sizeof(mbedtls_mpi_uint));
|
|
|
|
memcpy(x, X->p, X->n * sizeof(mbedtls_mpi_uint));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* 1a) A += B * s => we should get the correct carry */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_core_mla(a, limbs, B.p, B.n, *S.p), *cy->p);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* 1b) A += B * s => we should get the correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(a, bytes, x, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (A.n == B.n && memcmp(A.p, B.p, bytes) == 0) {
|
2022-10-04 17:16:16 +02:00
|
|
|
/* Check when A and B are aliased */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(a, A.p, A.n * sizeof(mbedtls_mpi_uint));
|
|
|
|
TEST_EQUAL(mbedtls_mpi_core_mla(a, limbs, a, limbs, *S.p), *cy->p);
|
|
|
|
ASSERT_COMPARE(a, bytes, x, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(a);
|
|
|
|
mbedtls_free(x);
|
|
|
|
|
|
|
|
mbedtls_mpi_free(&A);
|
|
|
|
mbedtls_mpi_free(&B);
|
|
|
|
mbedtls_mpi_free(&S);
|
|
|
|
mbedtls_mpi_free(&X4);
|
|
|
|
mbedtls_mpi_free(&X8);
|
|
|
|
mbedtls_mpi_free(&cy4);
|
|
|
|
mbedtls_mpi_free(&cy8);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_montg_init(char *input_N, char *input_mm)
|
2022-10-04 17:16:16 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi N, mm;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&N);
|
|
|
|
mbedtls_mpi_init(&mm);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&N, input_N));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&mm, input_mm));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* The MPI encoding of mm should be 1 limb (sizeof(mbedtls_mpi_uint) == 8) or
|
|
|
|
* 2 limbs (sizeof(mbedtls_mpi_uint) == 4).
|
|
|
|
*
|
|
|
|
* The data file contains the expected result for sizeof(mbedtls_mpi_uint) == 8;
|
|
|
|
* for sizeof(mbedtls_mpi_uint) == 4 it's just the LSW of this.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mm.n == 1 || mm.n == 2);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(1, N.s);
|
|
|
|
TEST_EQUAL(1, mm.s);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* mbedtls_mpi_core_montmul_init() only returns a result, no error possible */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint result = mbedtls_mpi_core_montmul_init(N.p);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* Check we got the correct result */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(result, mm.p[0]);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&N);
|
|
|
|
mbedtls_mpi_free(&mm);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_montmul(int limbs_AN4, int limbs_B4,
|
|
|
|
int limbs_AN8, int limbs_B8,
|
|
|
|
char *input_A,
|
|
|
|
char *input_B,
|
|
|
|
char *input_N,
|
|
|
|
char *input_X4,
|
|
|
|
char *input_X8)
|
2022-10-04 17:16:16 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi A, B, N, X4, X8, T, R;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&A);
|
|
|
|
mbedtls_mpi_init(&B);
|
|
|
|
mbedtls_mpi_init(&N);
|
|
|
|
mbedtls_mpi_init(&X4); /* expected result, sizeof(mbedtls_mpi_uint) == 4 */
|
|
|
|
mbedtls_mpi_init(&X8); /* expected result, sizeof(mbedtls_mpi_uint) == 8 */
|
|
|
|
mbedtls_mpi_init(&T);
|
|
|
|
mbedtls_mpi_init(&R); /* for the result */
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&A, input_A));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&B, input_B));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&N, input_N));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&X4, input_X4));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&X8, input_X8));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi *X = (sizeof(mbedtls_mpi_uint) == 4) ? &X4 : &X8;
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int limbs_AN = (sizeof(mbedtls_mpi_uint) == 4) ? limbs_AN4 : limbs_AN8;
|
|
|
|
int limbs_B = (sizeof(mbedtls_mpi_uint) == 4) ? limbs_B4 : limbs_B8;
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(A.n, (size_t) limbs_AN);
|
|
|
|
TEST_LE_U(X->n, (size_t) limbs_AN);
|
|
|
|
TEST_LE_U(B.n, (size_t) limbs_B);
|
|
|
|
TEST_LE_U(limbs_B, limbs_AN);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(1, A.s);
|
|
|
|
TEST_EQUAL(1, B.s);
|
|
|
|
TEST_EQUAL(1, N.s);
|
|
|
|
TEST_EQUAL(1, X->s);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_grow(&A, limbs_AN));
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_grow(&N, limbs_AN));
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_grow(X, limbs_AN));
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_grow(&B, limbs_B));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t working_limbs = mbedtls_mpi_core_montmul_working_limbs(limbs_AN);
|
|
|
|
TEST_EQUAL(working_limbs, limbs_AN * 2 + 1);
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_grow(&T, working_limbs));
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* Calculate the Montgomery constant (this is unit tested separately) */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N.p);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_grow(&R, limbs_AN)); /* ensure it's got the right number of limbs */
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(R.p, A.p, B.p, B.n, N.p, N.n, mm, T.p);
|
2022-10-04 17:16:16 +02:00
|
|
|
size_t bytes = N.n * sizeof(mbedtls_mpi_uint);
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_COMPARE(R.p, bytes, X->p, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* The output (R, above) may be aliased to A - use R to save the value of A */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(R.p, A.p, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(A.p, A.p, B.p, B.n, N.p, N.n, mm, T.p);
|
|
|
|
ASSERT_COMPARE(A.p, bytes, X->p, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(A.p, R.p, bytes); /* restore A */
|
2022-10-04 17:16:16 +02:00
|
|
|
|
|
|
|
/* The output may be aliased to N - use R to save the value of N */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(R.p, N.p, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(N.p, A.p, B.p, B.n, N.p, N.n, mm, T.p);
|
|
|
|
ASSERT_COMPARE(N.p, bytes, X->p, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(N.p, R.p, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (limbs_AN == limbs_B) {
|
2022-10-04 17:16:16 +02:00
|
|
|
/* Test when A aliased to B (requires A == B on input values) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(A.p, B.p, bytes) == 0) {
|
2022-10-04 17:16:16 +02:00
|
|
|
/* Test with A aliased to B and output, since this is permitted -
|
|
|
|
* don't bother with yet another test with only A and B aliased */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(B.p, B.p, B.p, B.n, N.p, N.n, mm, T.p);
|
|
|
|
ASSERT_COMPARE(B.p, bytes, X->p, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(B.p, A.p, bytes); /* restore B from equal value A */
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The output may be aliased to B - last test, so we don't save B */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_montmul(B.p, A.p, B.p, B.n, N.p, N.n, mm, T.p);
|
|
|
|
ASSERT_COMPARE(B.p, bytes, X->p, bytes);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&A);
|
|
|
|
mbedtls_mpi_free(&B);
|
|
|
|
mbedtls_mpi_free(&N);
|
|
|
|
mbedtls_mpi_free(&X4);
|
|
|
|
mbedtls_mpi_free(&X8);
|
|
|
|
mbedtls_mpi_free(&T);
|
|
|
|
mbedtls_mpi_free(&R);
|
2022-10-04 17:16:16 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_get_mont_r2_unsafe_neg()
|
2022-10-25 16:59:09 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi N, RR;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&N);
|
|
|
|
mbedtls_mpi_init(&RR);
|
|
|
|
const char *n = "7ffffffffffffff1";
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
/* Test for zero divisor */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_DIVISION_BY_ZERO,
|
|
|
|
mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N));
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
/* Test for negative input */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&N, n));
|
2022-10-25 16:59:09 +02:00
|
|
|
N.s = -1;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(MBEDTLS_ERR_MPI_NEGATIVE_VALUE,
|
|
|
|
mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N));
|
2022-10-25 16:59:09 +02:00
|
|
|
N.s = 1;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&N);
|
|
|
|
mbedtls_mpi_free(&RR);
|
2022-10-25 16:59:09 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_get_mont_r2_unsafe(char *input_N,
|
|
|
|
char *input_RR_X4,
|
|
|
|
char *input_RR_X8)
|
2022-10-25 16:59:09 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi N, RR, RR_REF;
|
|
|
|
|
|
|
|
/* Select the appropriate output */
|
2023-01-11 14:50:10 +01:00
|
|
|
char *input_rr = (sizeof(mbedtls_mpi_uint) == 4) ? input_RR_X4 : input_RR_X8;
|
2022-10-25 16:59:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&N);
|
|
|
|
mbedtls_mpi_init(&RR);
|
|
|
|
mbedtls_mpi_init(&RR_REF);
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
/* Read inputs */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&N, input_N));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi(&RR_REF, input_rr));
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
/* All of the inputs are +ve (or zero) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(1, N.s);
|
|
|
|
TEST_EQUAL(1, RR_REF.s);
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
/* Test valid input */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N));
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
/* Test that the moduli is odd */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(N.p[0] ^ 1, N.p[0] - 1);
|
2022-10-25 16:59:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
/* Output is +ve (or zero) */
|
|
|
|
TEST_EQUAL(1, RR_REF.s);
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
/* rr is updated to a valid pointer */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(RR.p != NULL);
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
/* Calculated rr matches expected value */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_mpi_cmp_mpi(&RR, &RR_REF) == 0);
|
2022-10-25 16:59:09 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&N);
|
|
|
|
mbedtls_mpi_free(&RR);
|
|
|
|
mbedtls_mpi_free(&RR_REF);
|
2022-10-25 16:59:09 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-10-26 20:10:29 +02:00
|
|
|
|
2022-10-28 19:13:43 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_ct_uint_table_lookup(int bitlen, int window_size)
|
2022-10-26 20:10:29 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t limbs = BITS_TO_LIMBS(bitlen);
|
|
|
|
size_t count = ((size_t) 1) << window_size;
|
2022-10-26 20:10:29 +02:00
|
|
|
|
|
|
|
mbedtls_mpi_uint *table = NULL;
|
|
|
|
mbedtls_mpi_uint *dest = NULL;
|
2022-10-31 16:11:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(table, limbs * count);
|
|
|
|
ASSERT_ALLOC(dest, limbs);
|
2022-10-26 20:10:29 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill the table with a unique counter so that differences are easily
|
|
|
|
* detected. (And have their relationship to the index relatively non-trivial just
|
|
|
|
* to be sure.)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < count * limbs; i++) {
|
2022-10-26 20:10:29 +02:00
|
|
|
table[i] = ~i - 1;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2022-11-02 10:51:02 +01:00
|
|
|
mbedtls_mpi_uint *current = table + i * limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(dest, 0x00, limbs * sizeof(*dest));
|
2022-11-02 10:51:02 +01:00
|
|
|
|
2022-10-28 11:54:46 +02:00
|
|
|
/*
|
|
|
|
* We shouldn't leak anything through timing.
|
|
|
|
* We need to set these in every loop as we need to make the loop
|
2022-11-02 10:51:02 +01:00
|
|
|
* variable public for the loop head and the buffers for comparison.
|
2022-10-28 11:54:46 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_SECRET(&i, sizeof(i));
|
|
|
|
TEST_CF_SECRET(dest, limbs * sizeof(*dest));
|
|
|
|
TEST_CF_SECRET(table, count * limbs * sizeof(*table));
|
2022-10-28 11:54:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_ct_uint_table_lookup(dest, table, limbs, count, i);
|
2022-10-26 20:10:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_PUBLIC(dest, limbs * sizeof(*dest));
|
|
|
|
TEST_CF_PUBLIC(table, count * limbs * sizeof(*table));
|
|
|
|
ASSERT_COMPARE(dest, limbs * sizeof(*dest),
|
|
|
|
current, limbs * sizeof(*current));
|
|
|
|
TEST_CF_PUBLIC(&i, sizeof(i));
|
2022-10-26 20:10:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2022-10-31 16:11:49 +01:00
|
|
|
mbedtls_free(table);
|
|
|
|
mbedtls_free(dest);
|
2022-10-26 20:10:29 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-09-09 20:55:53 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_fill_random(int wanted_bytes_arg, int extra_rng_bytes,
|
|
|
|
int extra_limbs, int before, int expected_ret)
|
2022-09-09 20:55:53 +02:00
|
|
|
{
|
|
|
|
size_t wanted_bytes = wanted_bytes_arg;
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t X_limbs = CHARS_TO_LIMBS(wanted_bytes) + extra_limbs;
|
2022-09-09 20:55:53 +02:00
|
|
|
size_t rng_bytes = wanted_bytes + extra_rng_bytes;
|
|
|
|
unsigned char *rnd_data = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_test_rnd_buf_info rnd_info = { NULL, rng_bytes, NULL, NULL };
|
2022-09-09 20:55:53 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Prepare an RNG with known output, limited to rng_bytes. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(rnd_data, rng_bytes);
|
|
|
|
TEST_EQUAL(0, mbedtls_test_rnd_std_rand(NULL, rnd_data, rng_bytes));
|
2022-09-09 20:55:53 +02:00
|
|
|
rnd_info.buf = rnd_data;
|
|
|
|
|
|
|
|
/* Allocate an MPI with room for wanted_bytes plus extra_limbs.
|
|
|
|
* extra_limbs may be negative but the total limb count must be positive.
|
|
|
|
* Fill the MPI with the byte value in before. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(1, X_limbs);
|
|
|
|
ASSERT_ALLOC(X, X_limbs);
|
|
|
|
memset(X, before, X_limbs * sizeof(*X));
|
2022-09-09 20:55:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_mpi_core_fill_random(X, X_limbs, wanted_bytes,
|
|
|
|
mbedtls_test_rnd_buffer_rand,
|
|
|
|
&rnd_info);
|
|
|
|
TEST_EQUAL(expected_ret, ret);
|
2022-09-09 20:55:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (expected_ret == 0) {
|
2022-09-09 20:55:53 +02:00
|
|
|
/* mbedtls_mpi_core_fill_random is documented to use bytes from the
|
|
|
|
* RNG as a big-endian representation of the number. We used an RNG
|
|
|
|
* with known output, so check that the output contains the
|
|
|
|
* expected value. Bytes above wanted_bytes must be zero. */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < wanted_bytes; i++) {
|
|
|
|
mbedtls_test_set_step(i);
|
|
|
|
TEST_EQUAL(GET_BYTE(X, i), rnd_data[wanted_bytes - 1 - i]);
|
2022-09-09 20:55:53 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = wanted_bytes; i < X_limbs * ciL; i++) {
|
|
|
|
mbedtls_test_set_step(i);
|
|
|
|
TEST_EQUAL(GET_BYTE(X, i), 0);
|
2022-09-09 20:55:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(rnd_data);
|
|
|
|
mbedtls_free(X);
|
2022-09-09 20:55:53 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2022-11-02 15:35:17 +01:00
|
|
|
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-03-31 16:04:42 +02:00
|
|
|
void mpi_core_mul(char *input_A,
|
|
|
|
char *input_B,
|
|
|
|
char *result)
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
{
|
2023-03-31 16:04:42 +02:00
|
|
|
mbedtls_mpi_uint *A = NULL;
|
|
|
|
mbedtls_mpi_uint *A_orig = NULL;
|
|
|
|
mbedtls_mpi_uint *B = NULL;
|
|
|
|
mbedtls_mpi_uint *B_orig = NULL;
|
|
|
|
mbedtls_mpi_uint *R = NULL;
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
size_t A_limbs, B_limbs, R_limbs;
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
|
2023-03-31 16:04:42 +02:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_core(&A, &A_limbs, input_A), 0);
|
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_core(&B, &B_limbs, input_B), 0);
|
2023-04-03 14:13:46 +02:00
|
|
|
TEST_EQUAL(mbedtls_test_read_mpi_core(&R, &R_limbs, result), 0);
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
|
2023-03-31 16:04:42 +02:00
|
|
|
TEST_EQUAL(R_limbs, A_limbs + B_limbs);
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
|
2023-03-31 16:04:42 +02:00
|
|
|
const size_t X_limbs = A_limbs + B_limbs;
|
|
|
|
const size_t X_bytes = X_limbs * sizeof(mbedtls_mpi_uint);
|
|
|
|
ASSERT_ALLOC(X, X_limbs);
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
|
2023-03-31 16:04:42 +02:00
|
|
|
const size_t A_bytes = A_limbs * sizeof(mbedtls_mpi_uint);
|
|
|
|
ASSERT_ALLOC(A_orig, A_limbs);
|
|
|
|
memcpy(A_orig, A, A_bytes);
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
|
2023-03-31 16:04:42 +02:00
|
|
|
const size_t B_bytes = B_limbs * sizeof(mbedtls_mpi_uint);
|
|
|
|
ASSERT_ALLOC(B_orig, B_limbs);
|
|
|
|
memcpy(B_orig, B, B_bytes);
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
|
|
|
|
/* Set result to something that is unlikely to be correct */
|
2023-03-31 16:04:42 +02:00
|
|
|
memset(X, '!', X_bytes);
|
|
|
|
|
|
|
|
/* 1. X = A * B - result should be correct, A and B unchanged */
|
|
|
|
mbedtls_mpi_core_mul(X, A, A_limbs, B, B_limbs);
|
|
|
|
ASSERT_COMPARE(X, X_bytes, R, X_bytes);
|
|
|
|
ASSERT_COMPARE(A, A_bytes, A_orig, A_bytes);
|
|
|
|
ASSERT_COMPARE(B, B_bytes, B_orig, B_bytes);
|
|
|
|
|
|
|
|
/* 2. A == B: alias A and B - result should be correct, A and B unchanged */
|
|
|
|
if (A_bytes == B_bytes && memcmp(A, B, A_bytes) == 0) {
|
|
|
|
memset(X, '!', X_bytes);
|
|
|
|
mbedtls_mpi_core_mul(X, A, A_limbs, A, A_limbs);
|
|
|
|
ASSERT_COMPARE(X, X_bytes, R, X_bytes);
|
|
|
|
ASSERT_COMPARE(A, A_bytes, A_orig, A_bytes);
|
|
|
|
}
|
|
|
|
/* 3. X = B * A - result should be correct, A and B unchanged */
|
|
|
|
else {
|
|
|
|
memset(X, '!', X_bytes);
|
|
|
|
mbedtls_mpi_core_mul(X, B, B_limbs, A, A_limbs);
|
|
|
|
ASSERT_COMPARE(X, X_bytes, R, X_bytes);
|
|
|
|
ASSERT_COMPARE(A, A_bytes, A_orig, A_bytes);
|
|
|
|
ASSERT_COMPARE(B, B_bytes, B_orig, B_bytes);
|
|
|
|
}
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
|
|
|
|
exit:
|
2023-03-31 16:04:42 +02:00
|
|
|
mbedtls_free(A);
|
|
|
|
mbedtls_free(A_orig);
|
|
|
|
mbedtls_free(B);
|
|
|
|
mbedtls_free(B_orig);
|
|
|
|
mbedtls_free(R);
|
|
|
|
mbedtls_free(X);
|
Add unit tests for mbedtls_mpi_core_mul()
The test cases use the following MPI values.
The .data file only includes those (a, b) values where a <= b; the test code
does a * b and b * a.
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0
fffffffffffffffffefefefefefefefe 100000000000000000000000000000000
1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
The lines in the .data file were generated by the following script
```
#!/usr/bin/env perl
#
# mpi-test-core-mul.pl - generate MPI tests in Perl for mbedtls_mpi_core_mul()
#
use strict;
use warnings;
use Math::BigInt;
use sort 'stable';
my $echo = 0;
my @mul_mpis = qw(
0 1 80 ff 100 fffe ffff 10000 ffffffff 100000000
20000000000000 7f7f7f7f7f7f7f7f 8000000000000000 ffffffffffffffff
10000000000000000 10000000000000001 1234567890abcdef0 fffffffffffffffffefefefefefefefe
100000000000000000000000000000000 1234567890abcdef01234567890abcdef0
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
1234567890abcdef01234567890abcdef01234567890abcdef01234567890abcdef0
4df72d07b4b71c8dacb6cffa954f8d88254b6277099308baf003fab73227f34029643b5a263f66e0d3c3fa297ef71755efd53b8fb6cb812c6bbf7bcf179298bd9947c4c8b14324140a2c0f5fad7958a69050a987a6096e9f055fb38edf0c5889eca4a0cfa99b45fbdeee4c696b328ddceae4723945901ec025076b12b
);
generate_tests();
sub generate_tests {
generate_mbedtls_mpi_core_mul();
}
sub generate_mbedtls_mpi_core_mul {
my $sub_name = (caller(0))[3]; # e.g. main::generate_mbedtls_mpi_sub_mpi
my ($ignore, $test_name) = split("main::generate_", $sub_name);
my @cases = ();
for my $ah (@mul_mpis) {
for my $bh (@mul_mpis) {
my $a = Math::BigInt->from_hex($ah);
my $b = Math::BigInt->from_hex($bh);
next if $a > $b; # don't need to repeat test cases
my $r = $a * $b;
my $rh = $r->to_hex();
my $desc = "$test_name #NUMBER: 0x$ah * 0x$bh = 0x$rh";
my $case = output($test_name, str($ah), str($bh), str($rh));
push(@cases, [$case, $desc]);
}
}
output_cases("", @cases);
}
sub output_cases {
my ($explain, @cases) = @_;
my $count = 1;
for my $c (@cases) {
my ($case, $desc, $dep) = @$c;
$desc =~ s/NUMBER/$count/; $count++;
if (defined($explain) && $desc =~ /EXPLAIN/) {
$desc =~ s/EXPLAIN/$explain/;
$explain = "";
}
my $depends = "";
$depends = "depends_on:$dep\n" if defined($dep) && length($dep);
print <<EOF;
$desc
$depends$case
EOF
}
}
sub output {
return join(":", @_);
}
sub str {
return '"' . $_[0] . '"';
}
```
Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
Signed-off-by: Gabor Mezei <gabor.mezei@arm.com>
2022-08-24 18:34:08 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-10-06 14:36:21 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_exp_mod(char *input_N, char *input_A,
|
|
|
|
char *input_E, char *input_X)
|
2022-10-06 14:36:21 +02:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *A = NULL;
|
|
|
|
mbedtls_mpi_uint *E = NULL;
|
|
|
|
mbedtls_mpi_uint *N = NULL;
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
2022-12-06 15:37:18 +01:00
|
|
|
size_t A_limbs, E_limbs, N_limbs, X_limbs;
|
2022-10-06 14:36:21 +02:00
|
|
|
const mbedtls_mpi_uint *R2 = NULL;
|
|
|
|
mbedtls_mpi_uint *Y = NULL;
|
2022-12-06 15:37:18 +01:00
|
|
|
mbedtls_mpi_uint *T = NULL;
|
2022-10-06 14:36:21 +02:00
|
|
|
/* Legacy MPIs for computing R2 */
|
|
|
|
mbedtls_mpi N_mpi;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&N_mpi);
|
2022-10-06 14:36:21 +02:00
|
|
|
mbedtls_mpi R2_mpi;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&R2_mpi);
|
2022-10-06 14:36:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&A, &A_limbs, input_A));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&E, &E_limbs, input_E));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&N, &N_limbs, input_N));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&X, &X_limbs, input_X));
|
|
|
|
ASSERT_ALLOC(Y, N_limbs);
|
2022-10-06 14:36:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(A_limbs, N_limbs);
|
|
|
|
TEST_EQUAL(X_limbs, N_limbs);
|
2022-10-06 14:36:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_mpi_grow(&N_mpi, N_limbs));
|
|
|
|
memcpy(N_mpi.p, N, N_limbs * sizeof(*N));
|
2022-10-06 14:36:21 +02:00
|
|
|
N_mpi.n = N_limbs;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0,
|
|
|
|
mbedtls_mpi_core_get_mont_r2_unsafe(&R2_mpi, &N_mpi));
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_grow(&R2_mpi, N_limbs));
|
2022-10-06 14:36:21 +02:00
|
|
|
R2 = R2_mpi.p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t working_limbs = mbedtls_mpi_core_exp_mod_working_limbs(N_limbs,
|
|
|
|
E_limbs);
|
2022-12-06 15:37:18 +01:00
|
|
|
|
|
|
|
/* No point exactly duplicating the code in mbedtls_mpi_core_exp_mod_working_limbs()
|
|
|
|
* to see if the output is correct, but we can check that it's in a
|
|
|
|
* reasonable range. The current calculation works out as
|
|
|
|
* `1 + N_limbs * (welem + 3)`, where welem is the number of elements in
|
|
|
|
* the window (1 << 1 up to 1 << 6).
|
|
|
|
*/
|
|
|
|
size_t min_expected_working_limbs = 1 + N_limbs * 4;
|
|
|
|
size_t max_expected_working_limbs = 1 + N_limbs * 67;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(min_expected_working_limbs, working_limbs);
|
|
|
|
TEST_LE_U(working_limbs, max_expected_working_limbs);
|
2022-12-06 15:37:18 +01:00
|
|
|
|
2022-12-12 18:06:27 +01:00
|
|
|
/* Should also be at least mbedtls_mpi_core_montmul_working_limbs() */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_LE_U(mbedtls_mpi_core_montmul_working_limbs(N_limbs),
|
|
|
|
working_limbs);
|
2022-12-12 18:06:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(T, working_limbs);
|
2022-12-06 15:37:18 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_exp_mod(Y, A, N, N_limbs, E, E_limbs, R2, T);
|
2022-12-06 15:37:18 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, memcmp(X, Y, N_limbs * sizeof(mbedtls_mpi_uint)));
|
2022-10-06 14:36:21 +02:00
|
|
|
|
2022-12-07 14:29:07 +01:00
|
|
|
/* Check when output aliased to input */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_core_exp_mod(A, A, N, N_limbs, E, E_limbs, R2, T);
|
2022-12-07 14:29:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, memcmp(X, A, N_limbs * sizeof(mbedtls_mpi_uint)));
|
2022-12-07 14:29:07 +01:00
|
|
|
|
2022-10-06 14:36:21 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(T);
|
|
|
|
mbedtls_free(A);
|
|
|
|
mbedtls_free(E);
|
|
|
|
mbedtls_free(N);
|
|
|
|
mbedtls_free(X);
|
|
|
|
mbedtls_free(Y);
|
|
|
|
mbedtls_mpi_free(&N_mpi);
|
|
|
|
mbedtls_mpi_free(&R2_mpi);
|
2022-10-06 14:36:21 +02:00
|
|
|
// R2 doesn't need to be freed as it is only aliasing R2_mpi
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-11-22 16:07:31 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_sub_int(char *input_A, char *input_B,
|
|
|
|
char *input_X, int borrow)
|
2022-11-22 16:07:31 +01:00
|
|
|
{
|
|
|
|
/* We are testing A - b, where A is an MPI and b is a scalar, expecting
|
|
|
|
* result X with borrow borrow. However, for ease of handling we encode b
|
|
|
|
* as a 1-limb MPI (B) in the .data file. */
|
|
|
|
|
|
|
|
mbedtls_mpi_uint *A = NULL;
|
|
|
|
mbedtls_mpi_uint *B = NULL;
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
mbedtls_mpi_uint *R = NULL;
|
|
|
|
size_t A_limbs, B_limbs, X_limbs;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&A, &A_limbs, input_A));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&B, &B_limbs, input_B));
|
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&X, &X_limbs, input_X));
|
2022-11-22 16:07:31 +01:00
|
|
|
|
|
|
|
/* The MPI encoding of scalar b must be only 1 limb */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(B_limbs, 1);
|
2022-11-22 16:07:31 +01:00
|
|
|
|
|
|
|
/* The subtraction is fixed-width, so A and X must have the same number of limbs */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(A_limbs, X_limbs);
|
2022-11-22 16:07:31 +01:00
|
|
|
size_t limbs = A_limbs;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ASSERT_ALLOC(R, limbs);
|
2022-11-22 16:07:31 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define TEST_COMPARE_CORE_MPIS(A, B, limbs) \
|
|
|
|
ASSERT_COMPARE(A, (limbs) * sizeof(mbedtls_mpi_uint), B, (limbs) * sizeof(mbedtls_mpi_uint))
|
2022-11-22 16:07:31 +01:00
|
|
|
|
|
|
|
/* 1. R = A - b. Result and borrow should be correct */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_core_sub_int(R, A, B[0], limbs), borrow);
|
|
|
|
TEST_COMPARE_CORE_MPIS(R, X, limbs);
|
2022-11-22 16:07:31 +01:00
|
|
|
|
|
|
|
/* 2. A = A - b. Result and borrow should be correct */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(mbedtls_mpi_core_sub_int(A, A, B[0], limbs), borrow);
|
|
|
|
TEST_COMPARE_CORE_MPIS(A, X, limbs);
|
2022-11-22 16:07:31 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(A);
|
|
|
|
mbedtls_free(B);
|
|
|
|
mbedtls_free(X);
|
|
|
|
mbedtls_free(R);
|
2022-11-22 16:07:31 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2022-12-12 17:54:57 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mpi_core_check_zero_ct(char *input_X, int expected_is_zero)
|
2022-12-12 17:54:57 +01:00
|
|
|
{
|
|
|
|
mbedtls_mpi_uint *X = NULL;
|
|
|
|
size_t X_limbs;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_test_read_mpi_core(&X, &X_limbs, input_X));
|
2022-12-12 17:54:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_CF_SECRET(X, X_limbs * sizeof(mbedtls_mpi_uint));
|
2022-12-12 17:54:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_uint check = mbedtls_mpi_core_check_zero_ct(X, X_limbs);
|
2022-12-12 17:54:57 +01:00
|
|
|
int is_zero = (check == 0);
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(is_zero, expected_is_zero);
|
2022-12-12 17:54:57 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(X);
|
2022-12-12 17:54:57 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|