XilinxProcessorIPLib/drivers/hdcp22_rx/src/xhdcp22_rx_crypt.c
static int XHdcp22Rx_Pkcs1EmeOaepEncode(const u8 *Message, const u32 MessageLen,
	const u8 *MaskingSeed, u8 *EncodedMessage)
{
	/* Verify arguments */
	Xil_AssertNonvoid(Message != NULL);
	Xil_AssertNonvoid(MessageLen > 0);
	Xil_AssertNonvoid(MaskingSeed != NULL);
	Xil_AssertNonvoid(EncodedMessage != NULL);

	u8  lHash[XHDCP22_RX_HASH_SIZE];
	u8  seed[XHDCP22_RX_HASH_SIZE];
	u8  dbMask[XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1];
	u8  DB[XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1];
	u8  seedMask[XHDCP22_RX_HASH_SIZE];
	u32 Status;

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

	/* Step 2b: Generate PS by initializing DB to zeros */
	memset(DB, 0x00, sizeof(DB));

	/* Step 2c: Generate DB = lHash || PS || 0x01 || M */
	memcpy(DB, lHash, XHDCP22_RX_HASH_SIZE);
	DB[XHDCP22_RX_N_SIZE-MessageLen-XHDCP22_RX_HASH_SIZE-2] = 0x01;
	memcpy(DB+XHDCP22_RX_N_SIZE-MessageLen-XHDCP22_RX_HASH_SIZE-1, Message, MessageLen);

	/* Step 2d: Generate random seed of length hLen
	 *   The random seed is passed in as an argument to this function.
	 */

	/* Step 2e: Generate dbMask = MGF1(seed, length(DB)) */
	Status = XHdcp22Rx_Pkcs1Mgf1(MaskingSeed, XHDCP22_RX_HASH_SIZE, dbMask,
						XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

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

	/* Step 2g: Generate seedMask = MGF(maskedDB, length(seed)) */
	Status = XHdcp22Rx_Pkcs1Mgf1(DB, XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1,
						seedMask, XHDCP22_RX_HASH_SIZE);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/* Step 2h: Generate maskedSeed = seed xor seedMask */
	XHdcp22Rx_Xor(seed, MaskingSeed, seedMask, XHDCP22_RX_HASH_SIZE);

	/* Step 2i: Form encoded message EM = 0x00 || maskedSeed || maskedDB */
	memset(EncodedMessage, 0x00, XHDCP22_RX_N_SIZE);
	memcpy(EncodedMessage+1, seed, XHDCP22_RX_HASH_SIZE);
	memcpy(EncodedMessage+1+XHDCP22_RX_HASH_SIZE, DB, XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1);

	return XST_SUCCESS;
}
/****************************************************************************/
/**
* This function implements EME-OAEP encoding. The label L is the empty
* string and the underlying hash function is SHA256.
*
* Reference: PKCS#1 v2.1, Section 7.1.1, Part 2
*
* @param	Message is the octet string to be encoded
* @param	MessageLen is the length of the octet string to be encoded.
* @param	MaskingSeed is the random octet string seed of length hLen.
* @param	EncodedMessage is the 128 byte encoded octet string.
*
* @return	XST_SUCCESS or XST_FAILURE.
*
* @note		None.
*****************************************************************************/
This function implements EME-OAEP encoding. The label L is the empty
string and the underlying hash function is SHA256.


This function implements EME - OAEP encoding . The label L is the empty string and the underlying hash function is SHA256 .
static int XHdcp22Rx_Pkcs1EmeOaepEncode(const u8 *Message, const u32 MessageLen,
	const u8 *MaskingSeed, u8 *EncodedMessage)
{
	Xil_AssertNonvoid(Message != NULL);
	Xil_AssertNonvoid(MessageLen > 0);
	Xil_AssertNonvoid(MaskingSeed != NULL);
	Xil_AssertNonvoid(EncodedMessage != NULL);
	u8  lHash[XHDCP22_RX_HASH_SIZE];
	u8  seed[XHDCP22_RX_HASH_SIZE];
	u8  dbMask[XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1];
	u8  DB[XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1];
	u8  seedMask[XHDCP22_RX_HASH_SIZE];
	u32 Status;
	XHdcp22Cmn_Sha256Hash(NULL,0,lHash);
	memset(DB, 0x00, sizeof(DB));
	memcpy(DB, lHash, XHDCP22_RX_HASH_SIZE);
	DB[XHDCP22_RX_N_SIZE-MessageLen-XHDCP22_RX_HASH_SIZE-2] = 0x01;
	memcpy(DB+XHDCP22_RX_N_SIZE-MessageLen-XHDCP22_RX_HASH_SIZE-1, Message, MessageLen);
	Status = XHdcp22Rx_Pkcs1Mgf1(MaskingSeed, XHDCP22_RX_HASH_SIZE, dbMask,
						XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	XHdcp22Rx_Xor(DB, DB, dbMask, XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1);
	Status = XHdcp22Rx_Pkcs1Mgf1(DB, XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1,
						seedMask, XHDCP22_RX_HASH_SIZE);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	XHdcp22Rx_Xor(seed, MaskingSeed, seedMask, XHDCP22_RX_HASH_SIZE);
	memset(EncodedMessage, 0x00, XHDCP22_RX_N_SIZE);
	memcpy(EncodedMessage+1, seed, XHDCP22_RX_HASH_SIZE);
	memcpy(EncodedMessage+1+XHDCP22_RX_HASH_SIZE, DB, XHDCP22_RX_N_SIZE-XHDCP22_RX_HASH_SIZE-1);
	return XST_SUCCESS;
}
static int XHdcp22Rx_Pkcs1EmeOaepEncode ( const u8 * Message , const u32 MessageLen , const u8 * MaskingSeed , u8 * EncodedMessage ) { Xil_AssertNonvoid ( Message != NULL ) ; Xil_AssertNonvoid ( MessageLen > 0 ) ; Xil_AssertNonvoid ( MaskingSeed != NULL ) ; Xil_AssertNonvoid ( EncodedMessage != NULL ) ; u8 lHash [ XHDCP22_RX_HASH_SIZE ] ; u8 seed [ XHDCP22_RX_HASH_SIZE ] ; u8 dbMask [ XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ] ; u8 DB [ XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ] ; u8 seedMask [ XHDCP22_RX_HASH_SIZE ] ; u32 Status ; XHdcp22Cmn_Sha256Hash ( NULL , 0 , lHash ) ; memset ( DB , 0x00 , sizeof ( DB ) ) ; memcpy ( DB , lHash , XHDCP22_RX_HASH_SIZE ) ; DB [ XHDCP22_RX_N_SIZE - MessageLen - XHDCP22_RX_HASH_SIZE - 2 ] = 0x01 ; memcpy ( DB + XHDCP22_RX_N_SIZE - MessageLen - XHDCP22_RX_HASH_SIZE - 1 , Message , MessageLen ) ; Status = XHdcp22Rx_Pkcs1Mgf1 ( MaskingSeed , XHDCP22_RX_HASH_SIZE , dbMask , XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } XHdcp22Rx_Xor ( DB , DB , dbMask , XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ) ; Status = XHdcp22Rx_Pkcs1Mgf1 ( DB , XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 , seedMask , XHDCP22_RX_HASH_SIZE ) ; if ( Status != XST_SUCCESS ) { return XST_FAILURE ; } XHdcp22Rx_Xor ( seed , MaskingSeed , seedMask , XHDCP22_RX_HASH_SIZE ) ; memset ( EncodedMessage , 0x00 , XHDCP22_RX_N_SIZE ) ; memcpy ( EncodedMessage + 1 , seed , XHDCP22_RX_HASH_SIZE ) ; memcpy ( EncodedMessage + 1 + XHDCP22_RX_HASH_SIZE , DB , XHDCP22_RX_N_SIZE - XHDCP22_RX_HASH_SIZE - 1 ) ; return XST_SUCCESS ; }
This function implements EME-OAEP encoding.
This function implements EME - OAEP encoding .
/* Verify arguments */ /* Step 2a: L is the empty string */ /* Step 2b: Generate PS by initializing DB to zeros */ /* Step 2c: Generate DB = lHash || PS || 0x01 || M */ /* Step 2d: Generate random seed of length hLen
	 *   The random seed is passed in as an argument to this function.
	 */ /* Step 2e: Generate dbMask = MGF1(seed, length(DB)) */ /* Step 2f: Generate maskedDB = DB xor dbMask */ /* Step 2g: Generate seedMask = MGF(maskedDB, length(seed)) */ /* Step 2h: Generate maskedSeed = seed xor seedMask */ /* Step 2i: Form encoded message EM = 0x00 || maskedSeed || maskedDB */