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

📄 irdeto.c

📁 在原来基础上新添加了对ST、V、I、C卡的处理
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
本代码仅作为学习研究之目的使用,请您于24小时内自觉将其删除,请勿应用于商业活动或其它赢利性活动中,
否则后果自负!
*/


/******************************************************
 * 文件名:ird.c
 * 功  能:处理TCA命令
 * 作  者:
 * 日  期:
 *****************************************************/

#include "irdeto.h"


static U8 getCardNoSerial[7] = { 0x01,0x02,0x00,0x03,0x00,0x00,0x3F};
static U8 getHexSerial[7]   = { 0x01,0x02,0x01,0x03,0x00,0x00,0x3E};
static U8 getProvider[7]    = { 0x01,0x02,0x03,0x03,0x00,0x00,0x00};
static U8 getChannelID[7]   = { 0x01,0x02,0x04,0x00,0x00,0x01,0x00};
static U8 ecmCmd[7]	=	{ 0x01,0x05,0x00,0x00,0x02,0x00};
static U8 emmCmd[7] =   { 0x01,0x01,0x00,0x00,0x00,0x00};
static U8 getCamKey383C[] = {   0x01,0x02,0x09,0x03,0x00,0x40,
								0x11,0x22,0x33,0x44,0x55,0x66,
								0x77,0x88,0x11,0x22,0x33,0x44,
								0x55,0x66,0x77,0x88,0x11,0x22,
								0x33,0x44,0x55,0x66,0x77,0x88,
								0x12,0x34,0x56,0x78,0x90,0xAB,
								0xCD,0xEF,0xFF,0xFF,0xFF,0xFF,
								0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
								0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
								0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
								0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
								0xFF,0xFF,0xFF,0xFF			 };


static const U8 camKey[] = 
{
   0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88
};

static const U8 cryptTable[256] = 
{
	0xDA,0x26,0xE8,0x72,0x11,0x52,0x3E,0x46,0x32,0xFF,0x8C,0x1E,0xA7,0xBE,0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0xC5,0x13,0xB9,0x24,0x5A,0xDC,0x64,0x18,0x38,0x91,
	0x7F,0x5B,0x70,0x54,0x07,0xB6,0x4B,0x0E,0x36,0xAC,0x31,0xE6,0xD6,0x48,0xAA,0xB4
};

typedef struct 
{
	U8 uPBWords[2];
	bool		  bResult;
	char		  sComment[100];
}IrdStatusMsg_t;

static const IrdStatusMsg_t IrdetoStatusMsgs[] = 
{
	{ { 0x00,0x00 }, true, "Instruction executed without error" },
	{ { 0x55,0x00 }, true, "Instruction executed without error" },
	{ { 0x9D,0x00 }, true, "Decoding successfull" },
	{ { 0x90,0x00 }, false, "ChID missing. Not subscribed?" },
	{ { 0x93,0x00 }, false, "ChID out of date. Subscription expired?" },
	{ { 0x9C,0x00 }, false, "Master key error" },
	{ { 0x9E,0x00 }, false, "Wrong decryption key" },
	{ { 0x9F,0x00 }, false, "Missing key" },
	{ { 0x70,0x00 }, false, "Wrong hex serial" },
	{ { 0x71,0x00 }, false, "Wrong provider" },
	{ { 0x72,0x00 }, false, "Wrong provider group" },
	{ { 0x73,0x00 }, false, "Wrong provider group" },
	{ { 0x7C,0x00 }, false, "Wrong signature" },
	{ { 0x7D,0x00 }, false, "Masterkey missing" },
	{ { 0x7E,0x00 }, false, "Wrong provider identifier" },
	{ { 0x7F,0x00 }, false, "Invalid nano" },
	{ { 0x54,0x00 }, true, "No more ChID's" },
};


Irdeto_Info_t*	   pstIrdetoInfo;


static bool IrdetoCheckStatus(U8* PB0);
static U8 GetXorSum(const U8 *mem, int len);
static int IrdetoWrite(Smart_Handle_t Handle,
					   U8* cmd,
					   U8* Response,
					   U16 goodSB,
					   U8* IrdetoStatus);
static void SetProviderIrdeto(U8 pb, const U8 *pi,int iNum);
static void RotateLeft8Byte(U8 *key);
static void RevCamCrypt(const U8 *key, U8 *data);


/************************************************************************/
/* Irdeto关于channel的处理,主要作用就是协助将智能卡中channel相关的信息存储于
结构体pstIrdetoInfo中 

   输入     Handle -- smart card 句柄
   输出     将通讯过程中得到的channel信息添加到结构体pstIrdetoInfo中
   返回值   无
   其他                                    
                                                          */
/************************************************************************/

static void Irdetofindchannelindex(U8 *input)
{
	int i;
	for(i=0;i<pstIrdetoInfo->aucChannelCount;i++)
	{
		if(pstIrdetoInfo->aucChannelId[i]==input[0]) 
		{
			if ((U32)((input[1]<<16)+(input[2]<<8)) > pstIrdetoInfo->aucDateInfo[i])
			{
				pstIrdetoInfo->aucDateInfo[i]=((input[1]<<16)+(input[2]<<8));
			}
			
			return;
		}
	}
	pstIrdetoInfo->aucChannelId[i]  = input[0];
	pstIrdetoInfo->aucDateInfo[i]	= ((input[1]<<16)+(input[2]<<8));
	pstIrdetoInfo->aucChannelCount ++;
}


/*-------------------------------------------------------------------*/
/*    函 数 名:irdeto_init                                          */
/*    函数功能:Irdeto CA初始化函数                                  */
/*    参    数:                                                     */
/*    返 回 值:                                                     */
/*    全局变量:                                                     */
/*    调用模块:                                                      */
/*    备    注:                                                     */
/*-------------------------------------------------------------------*/
bool irdeto_init(Smart_Handle_t Handle)
{
    int r;
	int rlen;
	int i,j,k;
	U8 ucIrdetoStatus[2];
	U8 Response[256];

	U8 cmd[100]={0};

	pstIrdetoInfo = &irdeto;
	memset(pstIrdetoInfo,0,sizeof(Irdeto_Info_t));


	/* Get Card Number (Ascii Serial) */
	r=IrdetoWrite(Handle,getCardNoSerial,Response,0,ucIrdetoStatus);
	if((r>0) &&IrdetoCheckStatus(ucIrdetoStatus) && r>=10)
	{
		memcpy(pstIrdetoInfo->acCardNoStr,&Response[8],10);
		pstIrdetoInfo->acCardNoStr[10]='X';
		printf("CardNumber = %s\n",pstIrdetoInfo->acCardNoStr);
	}
	else
	{
		/*printf("Get Irdeto Ascii Serial No failed !!\n");*/
		return false;
	}

	/* Get Hex Serial */
	r=IrdetoWrite(Handle,getHexSerial,Response,0,ucIrdetoStatus);
	if(IrdetoCheckStatus(ucIrdetoStatus)&&(r>=25))
	{
		pstIrdetoInfo->ucNoOfProv = Response[18];
		pstIrdetoInfo->ucHexBase = Response[23];
		memcpy(pstIrdetoInfo->ucHexSerial,&Response[20],3);
		printf("smartcardirdeto: Providers: %d HEX Serial: %02X%02X%02X  HEX Base: %02X\n",
				pstIrdetoInfo->ucNoOfProv,pstIrdetoInfo->ucHexSerial[0],pstIrdetoInfo->ucHexSerial[1],
				pstIrdetoInfo->ucHexSerial[2],pstIrdetoInfo->ucHexBase);
	}
	else
	{
		// printf("Get Irdeto Hex Serial No Failed !!\n");
		return false;
	}

	/* Get Provide Info */
	for(i=(pstIrdetoInfo->ucNoOfProv-1); i>=0;i--)
	{
		getProvider[4] = i;
		r=IrdetoWrite(Handle,getProvider,Response,0,ucIrdetoStatus);
		if(IrdetoCheckStatus(ucIrdetoStatus) && r>=33)
		{
			SetProviderIrdeto(Response[8],&Response[9],i);
			printf("Provider%d Info : ProvBase=%02x ProvId=%02x %02x %02x\n",i,
								pstIrdetoInfo->aucProvBase[i],pstIrdetoInfo->aucProvId[i][0],
								pstIrdetoInfo->aucProvId[i][1],pstIrdetoInfo->aucProvId[i][2]);
		}
		else
		{
			printf("SetProviderIrdeto %d error:  r =%d\n", i, r);
		}
	}

	/* Get Channel ID */
	pstIrdetoInfo->aucChannelCount = 0;
	
	
	
	for(i=0;i<pstIrdetoInfo->ucNoOfProv;i++)
	{
		for(j=0;j<MAX_CH_PER_PROV;j++)
		{
			memset(cmd,0,sizeof(cmd));
			memcpy(cmd,getChannelID,sizeof(getChannelID));

			printf("Read Channel Info ...  i=%d j=%d\n",i,j);
			cmd[4] = i;
			cmd[6] = j;

			if(pstIrdetoInfo->aucChannelCount>MAX_IRDETO_CHANNEL) 
				break;
			
			r = IrdetoWrite(Handle,cmd,Response,0,ucIrdetoStatus);
			for(k=0;k<Response[7];k+=6)
			{
				if(Response[k+8]==0xFF)
					continue;
				Irdetofindchannelindex(&Response[k+9]);
								
				printf("ChannelCount=%d ChannelID:%d\n",pstIrdetoInfo->aucChannelCount,\
						pstIrdetoInfo->aucChannelId[pstIrdetoInfo->aucChannelCount-1]);
			}
		}
	}


	/* Write Cam Key to Card */
	{
		r = IrdetoWrite(Handle,getCamKey383C,Response,0x5500,ucIrdetoStatus);
		rlen = 9;
	}
	if(!(r>0 && IrdetoCheckStatus(ucIrdetoStatus) && r==rlen))
	{
		printf("Irdeto Init : Write CAM ID failed !!\n");
		return false;
	}

	printf("Irdeto Init OK !!\n");
    return true;
}

static void IrdetoRefreshProvInfo(Smart_Handle_t Handle, bool bUpdateProvInfo)
{
	int i,j,k,r;
	U8 ucIrdetoStatus[2];
	U8 Response[256];
	U8 uLastChannelCount = pstIrdetoInfo->aucChannelCount;
	U8 cmd[100]={0};

	

	/* Get Hex Serial */
	if(bUpdateProvInfo)
	{
		/* Get Provide Info */
		for(i=0;i<pstIrdetoInfo->ucNoOfProv;i++)
		{
			getProvider[4] = i;
			r=IrdetoWrite(Handle,getProvider,Response,0,ucIrdetoStatus);
			if(IrdetoCheckStatus(ucIrdetoStatus) && r>=33)
			{
				SetProviderIrdeto(Response[8],&Response[9],i);

			}
			else
			{
				printf("IrdetoRefreshProvInfo bUpdateProvInfo Error\n");
				return;
			}

		}
	}
	else
	{
		/* Get Channel ID */

		for (i=0; i<pstIrdetoInfo->aucChannelCount;i++)
		{
			pstIrdetoInfo->aucChannelId[i] = 0;
			pstIrdetoInfo->aucDateInfo[i] = 0;
		}

		pstIrdetoInfo->aucChannelCount = 0;

		for(i=0;i<pstIrdetoInfo->ucNoOfProv;i++)
		{
			for(j=0;j<MAX_CH_PER_PROV;j++)
			{
				memset(cmd,0,sizeof(cmd));
				memcpy(cmd,getChannelID,sizeof(getChannelID));
				/*printf("Read Channel Info ...  i=%d j=%d\n",i,j);*/
				cmd[4] = i;
				cmd[6] = j;
				if(pstIrdetoInfo->aucChannelCount>MAX_IRDETO_CHANNEL) 
					break;
				r = IrdetoWrite(Handle,cmd,Response,0,ucIrdetoStatus);


				if(r<=0)
				{					
					continue;
				}

				for(k=0;k<Response[7];k+=6)
				{
					if(Response[k+8]==0xFF)
						continue;
					Irdetofindchannelindex(&Response[k+9]);
				}
			}
		}

⌨️ 快捷键说明

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