XilinxProcessorIPLib/drivers/hdcp22_rx/src/xhdcp22_rx_crypt.c
static int XHdcp22Rx_Pkcs1MontExp(XHdcp22_Rx *InstancePtr, u32 *C, u32 *A,
	u32 *E, u32 *N, const u32 *NPrime, int NDigits)
{
	int Offset;
	u32 R[XHDCP22_RX_N_SIZE/4];
	u32 Abar[XHDCP22_RX_N_SIZE/4];
	u32 Xbar[XHDCP22_RX_N_SIZE/4];

	memset(R, 0, sizeof(R));
	memset(Abar, 0, sizeof(Abar));
	memset(Xbar, 0, sizeof(Xbar));

#ifndef _XHDCP22_RX_SW_MMULT_
	XHdcp22Rx_Pkcs1MontMultFiosInit(InstancePtr, N, NPrime, NDigits);
#endif

	/* Step 0: R = 2^(NDigits*32) */
	R[0] = 1;
	mpShiftLeft(R, R, NDigits*32, XHDCP22_RX_N_SIZE/4);

	/* Step 1: Xbar = 1*R*mod(N) */
	mpModulo(Xbar, R, XHDCP22_RX_N_SIZE/4, N, NDigits); // Optimization

	/* Step 2: Abar = A*R*mod(N) */
	mpModMult(Abar, A, Xbar, N, 2*NDigits);

	/* Step 3: Binary square and multiply */
	for(Offset=32*NDigits-1; Offset>=0; Offset--)
	{
#ifndef _XHDCP22_RX_SW_MMULT_
		XHdcp22Rx_Pkcs1MontMultFios(InstancePtr, Xbar, Xbar, Xbar, NDigits);
#else
		XHdcp22Rx_Pkcs1MontMultFiosStub(Xbar, Xbar, Xbar, N, NPrime, NDigits);
#endif

		if(mpGetBit(E, NDigits, Offset) == TRUE)
		{
#ifndef _XHDCP22_RX_SW_MMULT_
			XHdcp22Rx_Pkcs1MontMultFios(InstancePtr, Xbar, Xbar, Abar, NDigits);
#else
			XHdcp22Rx_Pkcs1MontMultFiosStub(Xbar, Xbar, Abar, N, NPrime, NDigits);
#endif
		}
	}

	/* Step 4: C=MonPro(Xbar,1) */
	memset(R, 0, sizeof(R));
	R[0] = 1;

#ifndef _XHDCP22_RX_SW_MMULT_
	XHdcp22Rx_Pkcs1MontMultFios(InstancePtr, C, Xbar, R, NDigits);
#else
	XHdcp22Rx_Pkcs1MontMultFiosStub(C, Xbar, R, N, NPrime, NDigits);
#endif

	return XST_SUCCESS;
}
/****************************************************************************/
/**
* This function performs the modular exponentation operation using the
* binary square and multiply method.
*
* C = ModExp(A, E, N) = A^E*mod(N)
*
* @param	C is result of the modular exponentiation
* @param	A is the base
* @param	E is the exponent
* @param	N is the modulus
* @param	NPrime is a constant
* @param	NDigits is the integer precision of the arguments (C,A,B,N,NPrime).
* 			Maximum integer precision is 16.
*
* @return	None.
*
* @note		None.
*****************************************************************************/
This function performs the modular exponentation operation using the
binary square and multiply method.


This function performs the modular exponentation operation using the binary square and multiply method .
static int XHdcp22Rx_Pkcs1MontExp(XHdcp22_Rx *InstancePtr, u32 *C, u32 *A,
	u32 *E, u32 *N, const u32 *NPrime, int NDigits)
{
	int Offset;
	u32 R[XHDCP22_RX_N_SIZE/4];
	u32 Abar[XHDCP22_RX_N_SIZE/4];
	u32 Xbar[XHDCP22_RX_N_SIZE/4];
	memset(R, 0, sizeof(R));
	memset(Abar, 0, sizeof(Abar));
	memset(Xbar, 0, sizeof(Xbar));
#ifndef _XHDCP22_RX_SW_MMULT_
	XHdcp22Rx_Pkcs1MontMultFiosInit(InstancePtr, N, NPrime, NDigits);
#endif
	R[0] = 1;
	mpShiftLeft(R, R, NDigits*32, XHDCP22_RX_N_SIZE/4);
	mpModulo(Xbar, R, XHDCP22_RX_N_SIZE/4, N, NDigits); 
	mpModMult(Abar, A, Xbar, N, 2*NDigits);
	for(Offset=32*NDigits-1; Offset>=0; Offset--)
	{
#ifndef _XHDCP22_RX_SW_MMULT_
		XHdcp22Rx_Pkcs1MontMultFios(InstancePtr, Xbar, Xbar, Xbar, NDigits);
#else
		XHdcp22Rx_Pkcs1MontMultFiosStub(Xbar, Xbar, Xbar, N, NPrime, NDigits);
#endif
		if(mpGetBit(E, NDigits, Offset) == TRUE)
		{
#ifndef _XHDCP22_RX_SW_MMULT_
			XHdcp22Rx_Pkcs1MontMultFios(InstancePtr, Xbar, Xbar, Abar, NDigits);
#else
			XHdcp22Rx_Pkcs1MontMultFiosStub(Xbar, Xbar, Abar, N, NPrime, NDigits);
#endif
		}
	}
	memset(R, 0, sizeof(R));
	R[0] = 1;
#ifndef _XHDCP22_RX_SW_MMULT_
	XHdcp22Rx_Pkcs1MontMultFios(InstancePtr, C, Xbar, R, NDigits);
#else
	XHdcp22Rx_Pkcs1MontMultFiosStub(C, Xbar, R, N, NPrime, NDigits);
#endif
	return XST_SUCCESS;
}
static int XHdcp22Rx_Pkcs1MontExp ( XHdcp22_Rx * InstancePtr , u32 * C , u32 * A , u32 * E , u32 * N , const u32 * NPrime , int NDigits ) { int Offset ; u32 R [ XHDCP22_RX_N_SIZE / 4 ] ; u32 Abar [ XHDCP22_RX_N_SIZE / 4 ] ; u32 Xbar [ XHDCP22_RX_N_SIZE / 4 ] ; memset ( R , 0 , sizeof ( R ) ) ; memset ( Abar , 0 , sizeof ( Abar ) ) ; memset ( Xbar , 0 , sizeof ( Xbar ) ) ; #ifndef _XHDCP22_RX_SW_MMULT_ XHdcp22Rx_Pkcs1MontMultFiosInit ( InstancePtr , N , NPrime , NDigits ) ; #endif R [ 0 ] = 1 ; mpShiftLeft ( R , R , NDigits * 32 , XHDCP22_RX_N_SIZE / 4 ) ; mpModulo ( Xbar , R , XHDCP22_RX_N_SIZE / 4 , N , NDigits ) ; mpModMult ( Abar , A , Xbar , N , 2 * NDigits ) ; for ( Offset = 32 * NDigits - 1 ; Offset >= 0 ; Offset -- ) { #ifndef _XHDCP22_RX_SW_MMULT_ XHdcp22Rx_Pkcs1MontMultFios ( InstancePtr , Xbar , Xbar , Xbar , NDigits ) ; #else XHdcp22Rx_Pkcs1MontMultFiosStub ( Xbar , Xbar , Xbar , N , NPrime , NDigits ) ; #endif if ( mpGetBit ( E , NDigits , Offset ) == TRUE ) { #ifndef _XHDCP22_RX_SW_MMULT_ XHdcp22Rx_Pkcs1MontMultFios ( InstancePtr , Xbar , Xbar , Abar , NDigits ) ; #else XHdcp22Rx_Pkcs1MontMultFiosStub ( Xbar , Xbar , Abar , N , NPrime , NDigits ) ; #endif } } memset ( R , 0 , sizeof ( R ) ) ; R [ 0 ] = 1 ; #ifndef _XHDCP22_RX_SW_MMULT_ XHdcp22Rx_Pkcs1MontMultFios ( InstancePtr , C , Xbar , R , NDigits ) ; #else XHdcp22Rx_Pkcs1MontMultFiosStub ( C , Xbar , R , N , NPrime , NDigits ) ; #endif return XST_SUCCESS ; }
This function performs the modular exponentation operation using the
binary square and multiply method.
This function performs the modular exponentation operation using the binary square and multiply method .
/* Step 0: R = 2^(NDigits*32) */ /* Step 1: Xbar = 1*R*mod(N) */ // Optimization /* Step 2: Abar = A*R*mod(N) */ /* Step 3: Binary square and multiply */ /* Step 4: C=MonPro(Xbar,1) */