Ejemplo n.º 1
0
static void test_div_random(void)
{
	int i;
	char str[MAX_RAND_STRING_SIZE];

	TB_INFO("   Testing random divisions");

	DEF_BIGINT(n, 2048);
	DEF_BIGINT(d, 2048);
	DEF_BIGINT(q, 2048);
	DEF_BIGINT(r, 2048);
	DEF_BIGINT(v, 2048);

	for (i = 0; i < 10000; i++) {
		tb_set_random_value(n, str, 1);
		/* don't divide by zero */
		do {
			tb_set_random_value(d, str, 1);
		} while (TEE_BigIntCmpS32(d, 0) == 0);
		TEE_BigIntDiv(q, r, n, d);
		TEE_BigIntMul(v, q, d);
		TEE_BigIntAdd(v, v, r);
		TB_ASSERT_BIGINT_EQ(n, v);
		if (TEE_BigIntCmpS32(d, 0) > 0)
			TB_ASSERT_BIGINT_LESS(r, d);

	}

	DEL_BIGINT(n);
	DEL_BIGINT(d);
	DEL_BIGINT(q);
	DEL_BIGINT(r);
	DEL_BIGINT(v);
}
void TEE_BigIntMul(TEE_BigInt *dest, const TEE_BigInt *op1,
		   const TEE_BigInt *op2)
{
	size_t bs1 = TEE_BigIntGetBitCount(op1);
	size_t bs2 = TEE_BigIntGetBitCount(op2);
	size_t s = TEE_BigIntSizeInU32(bs1) + TEE_BigIntSizeInU32(bs2);
	TEE_BigInt zero[TEE_BigIntSizeInU32(1)] = { 0 };
	TEE_BigInt *tmp = NULL;

	tmp = mempool_alloc(mbedtls_mpi_mempool, sizeof(uint32_t) * s);
	if (!tmp)
		TEE_Panic(TEE_ERROR_OUT_OF_MEMORY);

	TEE_BigIntInit(tmp, s);
	TEE_BigIntInit(zero, TEE_BigIntSizeInU32(1));

	bigint_binary(tmp, op1, op2, mbedtls_mpi_mul_mpi);

	TEE_BigIntAdd(dest, tmp, zero);

	mempool_free(mbedtls_mpi_mempool, tmp);
}