⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 master_key.c

📁 Zoran V966 DVD 解码 Soc芯片的源程序
💻 C
字号:
// $Header: /I76/I76_Common/I76_Reference/Playcore/Nav_Clips/AviDrm/LibDrmCommon/master_key.c 2     2/15/04 7:41p Lotan $
// Copyright (c) 2003 DivXNetworks, Inc. http://www.divxnetworks.com
// All rights reserved.
//
// This software is the confidential and proprietary information of DivxNetworks
// Inc. ("Confidential Information").  You shall not disclose such Confidential
// Information and shall use it only in accordance with the terms of the license
// agreement you entered into with DivXNetworks, Inc.
#include "Config.h"		// Global Configuration - do not remove!

#ifdef AVI_DRM_SUPPORT

#ifdef USE_ONE_MASTER_KEY
#include "Playcore\Nav_Clips\AviDrm\libDrmCommon\master_key_one.h"
#else
#include "Playcore\Nav_Clips\AviDrm\libDrmCommon\master_key_all.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmCommon\master_key_all_local.h"
#endif
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmSizes.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\crypt_util.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmErrors.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmLocal.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmCommon\sha1.h"

#include "Services\Memory\malloc.h" // when using far malloc we should add this include 
#include "Services\Include\_heap.h"

int get_master_key_from_id(unsigned char *masterKeyId, unsigned char *masterkey)
{
	// The aes portable keys for a 32 byte key are 44 bytes long
	// The portable key is divided into elevent four byte pieces 
	// each piece has a random number id.

	return(	get_local_master_key_from_id(masterKeyId, masterkey));

}

/*********************************************************************
  Function:			MasterKeyTable0Descramble
  Description:		descramble master key table row 0

  Input Parameters: const char* input - input buffer, 6 chars long, 
										digits only.
					char* output	  -	output buffer, 4 chars allocated.

  Return Value:		none

  lotan_3105_2004_A - function created.

*********************************************************************/
#ifdef AVI_DRM_SCRAMBLE
void MasterKeyTable0Descramble( const char* input , char* output )
{
	output[3]= (((input[0]-'0')<<4)+(input[1]-'0'))^ ( 'a');
	output[0]= (((input[2]-'0')<<4)+(input[3]-'0'))^ ( '0');
	output[1]= (((input[4]-'0')<<4)+(input[5]-'0'));
	output[2]= (((input[6]-'0')<<4)+(input[7]-'0'))^ ( '1');

	return;
}
/* this function is the descrambling function for the following scrambling one,
	used in an offline utility for creating master_key_table_0.tbl
	
void MasterKeyTable0Scramble( const char* input , char* output )
{
	
	sprintf(output+0,"%02x",input[3] ^ ( 'a'));
	sprintf(output+2,"%02x",input[0] ^ ( '0' ));
	sprintf(output+4,"%02x",input[1] );
	sprintf(output+6,"%02x",input[2] ^ ( '1' ));
	return;
}
*/



/*********************************************************************
  Function:			MasterKeyTable1Descramble
  Description:		descramble master key table row 1

  Input Parameters: const char* input - input buffer, 4 chars long.
					char* output	  -	output buffer, 4 chars allocated.

  Return Value:		none
  
  lotan_3105_2004_A - function created.
*********************************************************************/
void MasterKeyTable1Descramble( const char* input , char* output )
{
	output[0] = input[3]-1;
	output[1] = input[1]-2;
	output[2] = input[0]-3;
	output[3] = input[2]-4;

	return;
}

/* this function is the descrambling function for the following scrambling one,
	used in an offline utility for creating master_key_table_1.tbl
	
 void MasterKeyTable1Scramble( const char* input , char* output )
{
	output[0] = input[2]+3;
	output[1] = input[1]+2;
	output[2] = input[3]+4;
	output[3] = input[0]+1;
	output[4] = '\0';

	return;
}

 */


int get_master_key_from_id(unsigned char *masterKeyId, unsigned char *masterkey)
{
	// The aes portable keys for a 32 byte key are 44 bytes long
	// The portable key is divided into elevent four byte pieces 
	// each piece has a random number id.
	int j = 0;
	int k = 0;
	int keyTableOffset = -1;

	char master_key_table0_Desc[5]={0};
	char master_key_table1_Desc[5]={0};

	for(j=0; j<11; j++)
	{
		int firstChar = j*4;
		char idPart[4];

		strncpy(idPart, masterKeyId + firstChar, 4);

		for(k=0; k < master_key_table_rows; k++)
		{
			MasterKeyTable0Descramble(master_key_table0[k], master_key_table0_Desc);
			
			if(0 == strncmp(idPart, master_key_table0_Desc,4))
			{
				keyTableOffset = k;
				MasterKeyTable1Descramble(master_key_table1[k], master_key_table1_Desc);

				memcpy(masterkey + firstChar, master_key_table1_Desc, 4);
				break;
			}
		}

		if(keyTableOffset == -1)
		{
			return -1;
		}
	}

	return 1;
}

#else
int get_local_master_key_from_id(unsigned char *masterKeyId, unsigned char *masterkey)
{
	// The aes portable keys for a 32 byte key are 44 bytes long
	// The portable key is divided into elevent four byte pieces 
	// each piece has a random number id.
	int j = 0;
	int k = 0;
	int keyTableOffset = -1;

	for(j=0; j<11; j++)
	{
		int firstChar = j*4;
		char idPart[4];

		strncpy(idPart, (char*)&masterKeyId[firstChar], 4);

		for(k=0; k < master_key_table_rows; k++)
		{
			if(0 == strncmp(idPart, master_key_table[k][0],4))
			{
				keyTableOffset = k;
				memcpy(masterkey + firstChar, master_key_table[k][1], 4);
				break;
			}
		}

		if(keyTableOffset == -1)
		{
			return -1;
		}
	}

	return 1;
}

#endif
int getLocalizedMasterKey(unsigned char *masterKey)
{
	unsigned char hardwareSecret[MASTER_KEY_SIZE_BYTES] ;
	unsigned char hardwareKey[KEY_SIZE_BYTES];
	char *hardwareSecretHash;
	unsigned int outlength;
	keyInstance* hardwareKeyInstance;
	int encryptedSize;
	unsigned char ciphered[MASTER_KEY_SIZE_BYTES] = {0};
	cipherInstance cipher;
	int result = DRM_ERROR_NONE;

	localGetHardwareKey(hardwareSecret);
	hardwareSecretHash = sha1GetHash(hardwareSecret, MASTER_KEY_SIZE_BYTES, &outlength);
	memcpy(hardwareKey, hardwareSecretHash, KEY_SIZE_BYTES);
	MEM_Free(SC_POOL, hardwareSecretHash);
	memcpy(ciphered, masterKey, MASTER_KEY_SIZE_BYTES);

	cipherInit(&cipher, MODE_ECB, NULL);

	hardwareKeyInstance = (keyInstance*)MEM_Allocate(SC_POOL, sizeof (keyInstance));
	if(!hardwareKeyInstance)
    {
    	tr_printf(("\nDRM hardware key instance malloc failed\n"));
    	return DRM_ERROR_DECRYPTION_FAILED;
    }
	crypt_util_make_key(hardwareKey, DIR_DECRYPT, hardwareKeyInstance, KEY_SIZE_BYTES);
	crypt_util_decrypt(&cipher, hardwareKeyInstance, ciphered, MASTER_KEY_SIZE_BYTES, masterKey, &encryptedSize);
	MEM_Free(SC_POOL, hardwareKeyInstance);
	
	if (MASTER_KEY_SIZE_BYTES != encryptedSize)
	{
		result = DRM_ERROR_DECRYPTION_FAILED;
	}

	return result;
}
#endif // AVI_DRM_SUPPORT

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -