/*
 * TEE_BigIntConvertFromFMM
 */
void TEE_BigIntConvertFromFMM(TEE_BigInt *dest,
			      const TEE_BigIntFMM *src,
			      const TEE_BigInt *n,
			      const TEE_BigIntFMMContext *context)
{
	mpanum mpa_dest = (mpa_num_base *)dest;
	mpanum mpa_op2 = (mpa_num_base *)src;
	mpanum mpa_n = (mpa_num_base *)n;
	mpa_fmm_context mpa_context = (mpa_fmm_context_base *)context;
	mpanum temp_dest;

	/*
	 * Since dest in BigIntFFMCompute (i.e. dest in mpa_montgomery_mul)
	 * must have alloc one word more than the size of n, we must
	 * use a temp variable during the conversion.
	 */
	mpa_alloc_static_temp_var(&temp_dest, mempool);

	/* calculate dest = Mont(1,src) */
	mpa_montgomery_mul(temp_dest, mpa_constant_one(), mpa_op2, mpa_n,
			   mpa_context->n_inv, mempool);

	mpa_copy(mpa_dest, temp_dest);
	mpa_free_static_temp_var(&temp_dest, mempool);
}
Beispiel #2
0
/* reduce */
static int montgomery_reduce(void *a, void *b, void *c)
{
	LTC_ARGCHK(a != NULL);
	LTC_ARGCHK(b != NULL);
	LTC_ARGCHK(c != NULL);
	mpanum tmp;
	init((void **)&tmp);
	// WARNING
	//  Workaround for a bug when a > b (a greater than the modulus)
	if (compare(a, b) == LTC_MP_GT) {
		mpa_mod((mpanum) a, (const mpanum) a, (const mpanum) b, external_mem_pool);
	}
	mpa_montgomery_mul(tmp,
			(mpanum) a,
			mpa_constant_one(),
			(mpanum) b,
			((mpa_fmm_context) c)->n_inv,
			external_mem_pool);
	mpa_copy(a, tmp);
	deinit(tmp);
	return CRYPT_OK;
}