XilinxProcessorIPLib/drivers/hdcp22_rx/src/xhdcp22_rx_crypt.c
static int XHdcp22Rx_Pkcs1Rsadp(XHdcp22_Rx *InstancePtr, const XHdcp22_Rx_KprivRx *KprivRx,
	u8 *EncryptedMessage, u8 *Message)
{
	/* Verify arguments */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(KprivRx != NULL);
	Xil_AssertNonvoid(EncryptedMessage != NULL);
	Xil_AssertNonvoid(Message != NULL);

	u32 A[XHDCP22_RX_N_SIZE/4];
	u32 B[XHDCP22_RX_N_SIZE/4];
	u32 C[XHDCP22_RX_N_SIZE/4];
	u32 D[XHDCP22_RX_N_SIZE/4];
	u32 M1[XHDCP22_RX_N_SIZE/4];
	u32 M2[XHDCP22_RX_N_SIZE/4];
	u32 Status;

	/* Clear variables */
	memset(A, 0, sizeof(A));
	memset(B, 0, sizeof(B));
	memset(C, 0, sizeof(C));
	memset(D, 0, sizeof(D));
	memset(M1, 0, sizeof(M1));
	memset(M2, 0, sizeof(M2));

	/* Step 2b part I: Generate m1 = c^dP * mod(p) */
	mpConvFromOctets(A, XHdcp22Rx_MpSizeof(A), KprivRx->p, XHDCP22_RX_P_SIZE);
	mpConvFromOctets(B, XHdcp22Rx_MpSizeof(B), KprivRx->dp, XHDCP22_RX_P_SIZE);
	mpConvFromOctets(C, XHdcp22Rx_MpSizeof(C), EncryptedMessage, XHDCP22_RX_N_SIZE);
	mpConvFromOctets(D, XHdcp22Rx_MpSizeof(D), InstancePtr->NPrimeP, XHDCP22_RX_P_SIZE);
	//Status = mpModExp(M1, C, B, A, XHDCP22_RX_N_SIZE/4);
	Status = XHdcp22Rx_Pkcs1MontExp(InstancePtr, M1, C, B, A, D, 16);

	/* Step 2b part I: Generate m2 = c^dQ * mod(q) */
	mpConvFromOctets(A, XHdcp22Rx_MpSizeof(A), KprivRx->q, XHDCP22_RX_P_SIZE);
	mpConvFromOctets(B, XHdcp22Rx_MpSizeof(B), KprivRx->dq, XHDCP22_RX_P_SIZE);
	mpConvFromOctets(D, XHdcp22Rx_MpSizeof(D), InstancePtr->NPrimeQ, XHDCP22_RX_P_SIZE);
	//Status = mpModExp(M2, C, D, B, XHDCP22_RX_N_SIZE/4);
	Status = XHdcp22Rx_Pkcs1MontExp(InstancePtr, M2, C, B, A, D, 16);

	/* Step 2b part II: Skip since u=2 */

	/* Step 2b part III: Generate h = (m1 - m2) * qInv * mod(p) */
	mpConvFromOctets(A, XHdcp22Rx_MpSizeof(A), KprivRx->p, XHDCP22_RX_P_SIZE);
	Status = mpSubtract(D, M1, M2, XHdcp22Rx_MpSizeof(D)); // mdiff = m1 -m2
	if(Status != XST_SUCCESS)
	{
		mpAdd(M1, M1, A, XHdcp22Rx_MpSizeof(M1));
		mpSubtract(D, M1, M2, XHdcp22Rx_MpSizeof(D));
	}
	mpConvFromOctets(C, XHdcp22Rx_MpSizeof(C), KprivRx->qinv, XHDCP22_RX_P_SIZE);
	Status = mpModMult(C, D, C, A, XHDCP22_RX_N_SIZE/4); // h = mdiff * qInv * mod(p)

	/* Step 2b part IV: Generate m = m2 + q * h */
	mpConvFromOctets(A, XHdcp22Rx_MpSizeof(A), KprivRx->q, XHDCP22_RX_P_SIZE);
	Status = mpMultiply(D, A, C, XHDCP22_RX_P_SIZE/4); // qh = q * h
	Status = mpAdd(C, M2, D, XHDCP22_RX_N_SIZE/4); // m = m2 + qh

	/* Convert integer to octet string */
	mpConvToOctets(C, XHdcp22Rx_MpSizeof(C), Message, XHDCP22_RX_N_SIZE);

	return XST_SUCCESS;
}
/****************************************************************************/
/**
* This function implements the RSADP primitive using the Chinese Remainder
* Theorem (CRT).
*
* Reference: PKCS#1 v2.1, Section 5.1.2
*
* @param	InstancePtr is a pointer to the MMULT instance.
* @param	KprivRx is the RSA private key structure containing the quintuple.
* @param	EncryptedMessage is the 128 byte octet string to be decrypted.
* @param	Message is the 128 byte decrypted octet string.
*
* @return	XST_SUCCESS or XST_FAILURE.
*
* @note		None.
*****************************************************************************/
This function implements the RSADP primitive using the Chinese Remainder
Theorem (CRT).


This function implements the RSADP primitive using the Chinese Remainder Theorem ( CRT ) .
static int XHdcp22Rx_Pkcs1Rsadp(XHdcp22_Rx *InstancePtr, const XHdcp22_Rx_KprivRx *KprivRx,
	u8 *EncryptedMessage, u8 *Message)
{
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(KprivRx != NULL);
	Xil_AssertNonvoid(EncryptedMessage != NULL);
	Xil_AssertNonvoid(Message != NULL);
	u32 A[XHDCP22_RX_N_SIZE/4];
	u32 B[XHDCP22_RX_N_SIZE/4];
	u32 C[XHDCP22_RX_N_SIZE/4];
	u32 D[XHDCP22_RX_N_SIZE/4];
	u32 M1[XHDCP22_RX_N_SIZE/4];
	u32 M2[XHDCP22_RX_N_SIZE/4];
	u32 Status;
	memset(A, 0, sizeof(A));
	memset(B, 0, sizeof(B));
	memset(C, 0, sizeof(C));
	memset(D, 0, sizeof(D));
	memset(M1, 0, sizeof(M1));
	memset(M2, 0, sizeof(M2));
	mpConvFromOctets(A, XHdcp22Rx_MpSizeof(A), KprivRx->p, XHDCP22_RX_P_SIZE);
	mpConvFromOctets(B, XHdcp22Rx_MpSizeof(B), KprivRx->dp, XHDCP22_RX_P_SIZE);
	mpConvFromOctets(C, XHdcp22Rx_MpSizeof(C), EncryptedMessage, XHDCP22_RX_N_SIZE);
	mpConvFromOctets(D, XHdcp22Rx_MpSizeof(D), InstancePtr->NPrimeP, XHDCP22_RX_P_SIZE);
	Status = XHdcp22Rx_Pkcs1MontExp(InstancePtr, M1, C, B, A, D, 16);
	mpConvFromOctets(A, XHdcp22Rx_MpSizeof(A), KprivRx->q, XHDCP22_RX_P_SIZE);
	mpConvFromOctets(B, XHdcp22Rx_MpSizeof(B), KprivRx->dq, XHDCP22_RX_P_SIZE);
	mpConvFromOctets(D, XHdcp22Rx_MpSizeof(D), InstancePtr->NPrimeQ, XHDCP22_RX_P_SIZE);
	Status = XHdcp22Rx_Pkcs1MontExp(InstancePtr, M2, C, B, A, D, 16);
	mpConvFromOctets(A, XHdcp22Rx_MpSizeof(A), KprivRx->p, XHDCP22_RX_P_SIZE);
	Status = mpSubtract(D, M1, M2, XHdcp22Rx_MpSizeof(D)); 
	if(Status != XST_SUCCESS)
	{
		mpAdd(M1, M1, A, XHdcp22Rx_MpSizeof(M1));
		mpSubtract(D, M1, M2, XHdcp22Rx_MpSizeof(D));
	}
	mpConvFromOctets(C, XHdcp22Rx_MpSizeof(C), KprivRx->qinv, XHDCP22_RX_P_SIZE);
	Status = mpModMult(C, D, C, A, XHDCP22_RX_N_SIZE/4); 
	mpConvFromOctets(A, XHdcp22Rx_MpSizeof(A), KprivRx->q, XHDCP22_RX_P_SIZE);
	Status = mpMultiply(D, A, C, XHDCP22_RX_P_SIZE/4); 
	Status = mpAdd(C, M2, D, XHDCP22_RX_N_SIZE/4); 
	mpConvToOctets(C, XHdcp22Rx_MpSizeof(C), Message, XHDCP22_RX_N_SIZE);
	return XST_SUCCESS;
}
static int XHdcp22Rx_Pkcs1Rsadp ( XHdcp22_Rx * InstancePtr , const XHdcp22_Rx_KprivRx * KprivRx , u8 * EncryptedMessage , u8 * Message ) { Xil_AssertNonvoid ( InstancePtr != NULL ) ; Xil_AssertNonvoid ( KprivRx != NULL ) ; Xil_AssertNonvoid ( EncryptedMessage != NULL ) ; Xil_AssertNonvoid ( Message != NULL ) ; u32 A [ XHDCP22_RX_N_SIZE / 4 ] ; u32 B [ XHDCP22_RX_N_SIZE / 4 ] ; u32 C [ XHDCP22_RX_N_SIZE / 4 ] ; u32 D [ XHDCP22_RX_N_SIZE / 4 ] ; u32 M1 [ XHDCP22_RX_N_SIZE / 4 ] ; u32 M2 [ XHDCP22_RX_N_SIZE / 4 ] ; u32 Status ; memset ( A , 0 , sizeof ( A ) ) ; memset ( B , 0 , sizeof ( B ) ) ; memset ( C , 0 , sizeof ( C ) ) ; memset ( D , 0 , sizeof ( D ) ) ; memset ( M1 , 0 , sizeof ( M1 ) ) ; memset ( M2 , 0 , sizeof ( M2 ) ) ; mpConvFromOctets ( A , XHdcp22Rx_MpSizeof ( A ) , KprivRx -> p , XHDCP22_RX_P_SIZE ) ; mpConvFromOctets ( B , XHdcp22Rx_MpSizeof ( B ) , KprivRx -> dp , XHDCP22_RX_P_SIZE ) ; mpConvFromOctets ( C , XHdcp22Rx_MpSizeof ( C ) , EncryptedMessage , XHDCP22_RX_N_SIZE ) ; mpConvFromOctets ( D , XHdcp22Rx_MpSizeof ( D ) , InstancePtr -> NPrimeP , XHDCP22_RX_P_SIZE ) ; Status = XHdcp22Rx_Pkcs1MontExp ( InstancePtr , M1 , C , B , A , D , 16 ) ; mpConvFromOctets ( A , XHdcp22Rx_MpSizeof ( A ) , KprivRx -> q , XHDCP22_RX_P_SIZE ) ; mpConvFromOctets ( B , XHdcp22Rx_MpSizeof ( B ) , KprivRx -> dq , XHDCP22_RX_P_SIZE ) ; mpConvFromOctets ( D , XHdcp22Rx_MpSizeof ( D ) , InstancePtr -> NPrimeQ , XHDCP22_RX_P_SIZE ) ; Status = XHdcp22Rx_Pkcs1MontExp ( InstancePtr , M2 , C , B , A , D , 16 ) ; mpConvFromOctets ( A , XHdcp22Rx_MpSizeof ( A ) , KprivRx -> p , XHDCP22_RX_P_SIZE ) ; Status = mpSubtract ( D , M1 , M2 , XHdcp22Rx_MpSizeof ( D ) ) ; if ( Status != XST_SUCCESS ) { mpAdd ( M1 , M1 , A , XHdcp22Rx_MpSizeof ( M1 ) ) ; mpSubtract ( D , M1 , M2 , XHdcp22Rx_MpSizeof ( D ) ) ; } mpConvFromOctets ( C , XHdcp22Rx_MpSizeof ( C ) , KprivRx -> qinv , XHDCP22_RX_P_SIZE ) ; Status = mpModMult ( C , D , C , A , XHDCP22_RX_N_SIZE / 4 ) ; mpConvFromOctets ( A , XHdcp22Rx_MpSizeof ( A ) , KprivRx -> q , XHDCP22_RX_P_SIZE ) ; Status = mpMultiply ( D , A , C , XHDCP22_RX_P_SIZE / 4 ) ; Status = mpAdd ( C , M2 , D , XHDCP22_RX_N_SIZE / 4 ) ; mpConvToOctets ( C , XHdcp22Rx_MpSizeof ( C ) , Message , XHDCP22_RX_N_SIZE ) ; return XST_SUCCESS ; }
This function implements the RSADP primitive using the Chinese Remainder
Theorem (CRT).
This function implements the RSADP primitive using the Chinese Remainder Theorem ( CRT ) .
/* Verify arguments */ /* Clear variables */ /* Step 2b part I: Generate m1 = c^dP * mod(p) */ //Status = mpModExp(M1, C, B, A, XHDCP22_RX_N_SIZE/4); /* Step 2b part I: Generate m2 = c^dQ * mod(q) */ //Status = mpModExp(M2, C, D, B, XHDCP22_RX_N_SIZE/4); /* Step 2b part II: Skip since u=2 */ /* Step 2b part III: Generate h = (m1 - m2) * qInv * mod(p) */ // mdiff = m1 -m2 // h = mdiff * qInv * mod(p) /* Step 2b part IV: Generate m = m2 + q * h */ // qh = q * h // m = m2 + qh /* Convert integer to octet string */