XilinxProcessorIPLib/drivers/hdcp22_rx/src/xhdcp22_rx_crypt.c
static int XHdcp22Rx_Pkcs1EmeOaepDecode(u8 *EncodedMessage, u8 *Message, int *MessageLen)
{
	/* Verify arguments */
	Xil_AssertNonvoid(EncodedMessage != NULL);
	Xil_AssertNonvoid(Message != NULL);
	Xil_AssertNonvoid(MessageLen != NULL);

	u8  lHash[XHDCP22_RX_HASH_SIZE];
	u8  *maskedSeed;
	u8  seed[XHDCP22_RX_HASH_SIZE];
	u8  *maskedDB;
	u8  DB[XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1];
	u32 Offset = XHDCP22_RX_HASH_SIZE;
	u32 Status;

	/* Step 3a: L is the empty string */
	XHdcp22Cmn_Sha256Hash(NULL,0,lHash);

	/* Step 3b: Separate EM = Y || maskedSeed || maskedDB */
	maskedSeed = EncodedMessage+1;
	maskedDB = EncodedMessage+1+XHDCP22_RX_HASH_SIZE;

	/* Step 3c: Generate seedMask = MGF(maskedDB, hLen) */
	Status = XHdcp22Rx_Pkcs1Mgf1(maskedDB, XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1,
						seed, XHDCP22_RX_HASH_SIZE);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/* Step 3d: Generate seed = maskedSeed xor seedMask */
	XHdcp22Rx_Xor(seed, maskedSeed, seed, XHDCP22_RX_HASH_SIZE);

	/* Step 3e: Generate dbMask = MGF(seed, k-hLen-1) */
	Status = XHdcp22Rx_Pkcs1Mgf1(seed, XHDCP22_RX_HASH_SIZE, DB,
						XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/* Step 3f: Generate DB = maskedDB xor dbMask */
	XHdcp22Rx_Xor(DB, maskedDB, DB, XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1);

	/*
	 * Step 3g: Separate DB = lHash' || PS || 0x01 || M
	 *
	 * - Note: To avoid side channel attacks from now on run all
	 * code even in the error case. This avoids possible timing
	 * attacks as described by Manger.
	 */
	Status = XST_SUCCESS;

	/* Compare Y */
	if(*(EncodedMessage) != 0x00)
	{
		Status = XST_FAILURE;
	}

	/* Compare lHash' */
	if(memcmp(DB, lHash, XHDCP22_RX_HASH_SIZE) != 0)
	{
		Status = XST_FAILURE;
	}

	/* Compare PS */
	for(Offset = XHDCP22_RX_HASH_SIZE; Offset < (XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1); Offset++)
	{
		if(DB[Offset] == 0x01)
		{
			break;
		}
		else if(DB[Offset] != 0x00)
		{
			Status = XST_FAILURE;
		}
	}

	/* Return Error */
	if(Status != XST_SUCCESS)
	{
		return XST_FAILURE;
	}

	/* Extract M */
	*MessageLen = (XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1) - (Offset + 1);
	memcpy(Message, DB+Offset+1, *MessageLen);

	return XST_SUCCESS;
}
/****************************************************************************/
/**
* This function implements EME-OAEP decoding. The label L is the empty string
* and the underlying hash function is SHA256.
*
* Reference: PKCS#1 v2.1, Section 7.1.2, Part 3
*
* @param	EncodedMessage is the 128 byte encoded octet string.
* @param	Message is decoded octet string.
* @param	MessageLen is the length of the decoded octet string.
*
* @return	XST_SUCCESS or XST_FAILURE.
*
* @note		None.
*****************************************************************************/
This function implements EME-OAEP decoding. The label L is the empty string
and the underlying hash function is SHA256.


This function implements EME - OAEP decoding . The label L is the empty string and the underlying hash function is SHA256 .
static int XHdcp22Rx_Pkcs1EmeOaepDecode(u8 *EncodedMessage, u8 *Message, int *MessageLen)
{
	Xil_AssertNonvoid(EncodedMessage != NULL);
	Xil_AssertNonvoid(Message != NULL);
	Xil_AssertNonvoid(MessageLen != NULL);
	u8  lHash[XHDCP22_RX_HASH_SIZE];
	u8  *maskedSeed;
	u8  seed[XHDCP22_RX_HASH_SIZE];
	u8  *maskedDB;
	u8  DB[XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1];
	u32 Offset = XHDCP22_RX_HASH_SIZE;
	u32 Status;
	XHdcp22Cmn_Sha256Hash(NULL,0,lHash);
	maskedSeed = EncodedMessage+1;
	maskedDB = EncodedMessage+1+XHDCP22_RX_HASH_SIZE;
	Status = XHdcp22Rx_Pkcs1Mgf1(maskedDB, XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1,
						seed, XHDCP22_RX_HASH_SIZE);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	XHdcp22Rx_Xor(seed, maskedSeed, seed, XHDCP22_RX_HASH_SIZE);
	Status = XHdcp22Rx_Pkcs1Mgf1(seed, XHDCP22_RX_HASH_SIZE, DB,
						XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	XHdcp22Rx_Xor(DB, maskedDB, DB, XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1);
	Status = XST_SUCCESS;
	if(*(EncodedMessage) != 0x00)
	{
		Status = XST_FAILURE;
	}
	if(memcmp(DB, lHash, XHDCP22_RX_HASH_SIZE) != 0)
	{
		Status = XST_FAILURE;
	}
	for(Offset = XHDCP22_RX_HASH_SIZE; Offset < (XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1); Offset++)
	{
		if(DB[Offset] == 0x01)
		{
			break;
		}
		else if(DB[Offset] != 0x00)
		{
			Status = XST_FAILURE;
		}
	}
	if(Status != XST_SUCCESS)
	{
		return XST_FAILURE;
	}
	*MessageLen = (XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1) - (Offset + 1);
	memcpy(Message, DB+Offset+1, *MessageLen);
	return XST_SUCCESS;
}
static int XHdcp22Rx_Pkcs1EmeOaepDecode ( u8 * EncodedMessage , u8 * Message , int * MessageLen ) { Xil_AssertNonvoid ( EncodedMessage != NULL ) ; Xil_AssertNonvoid ( Message != NULL ) ; Xil_AssertNonvoid ( MessageLen != NULL ) ; u8 lHash [ XHDCP22_RX_HASH_SIZE ] ; u8 * maskedSeed ; u8 seed [ XHDCP22_RX_HASH_SIZE ] ; u8 * maskedDB ; u8 DB [ XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ] ; u32 Offset = XHDCP22_RX_HASH_SIZE ; u32 Status ; XHdcp22Cmn_Sha256Hash ( NULL , 0 , lHash ) ; maskedSeed = EncodedMessage + 1 ; maskedDB = EncodedMessage + 1 + XHDCP22_RX_HASH_SIZE ; Status = XHdcp22Rx_Pkcs1Mgf1 ( maskedDB , XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 , seed , XHDCP22_RX_HASH_SIZE ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } XHdcp22Rx_Xor ( seed , maskedSeed , seed , XHDCP22_RX_HASH_SIZE ) ; Status = XHdcp22Rx_Pkcs1Mgf1 ( seed , XHDCP22_RX_HASH_SIZE , DB , XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } XHdcp22Rx_Xor ( DB , maskedDB , DB , XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ) ; Status = XST_SUCCESS ; if ( * ( EncodedMessage ) != 0x00 ) { Status = XST_FAILURE ; } if ( memcmp ( DB , lHash , XHDCP22_RX_HASH_SIZE ) != 0 ) { Status = XST_FAILURE ; } for ( Offset = XHDCP22_RX_HASH_SIZE ; Offset < ( XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ) ; Offset ++ ) { if ( DB [ Offset ] == 0x01 ) { break ; } else if ( DB [ Offset ] != 0x00 ) { Status = XST_FAILURE ; } } if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } * MessageLen = ( XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ) - ( Offset + 1 ) ; memcpy ( Message , DB + Offset + 1 , * MessageLen ) ; return XST_SUCCESS ; }
This function implements EME-OAEP decoding.
This function implements EME - OAEP decoding .
/* Verify arguments */ /* Step 3a: L is the empty string */ /* Step 3b: Separate EM = Y || maskedSeed || maskedDB */ /* Step 3c: Generate seedMask = MGF(maskedDB, hLen) */ /* Step 3d: Generate seed = maskedSeed xor seedMask */ /* Step 3e: Generate dbMask = MGF(seed, k-hLen-1) */ /* Step 3f: Generate DB = maskedDB xor dbMask */ /*
	 * Step 3g: Separate DB = lHash' || PS || 0x01 || M
	 *
	 * - Note: To avoid side channel attacks from now on run all
	 * code even in the error case. This avoids possible timing
	 * attacks as described by Manger.
	 */ /* Compare Y */ /* Compare lHash' */ /* Compare PS */ /* Return Error */ /* Extract M */