赞助论坛
  • 49634阅读
  • 0回复

布瑞克仿真卡制作CA卡的全过程:布瑞克仿真卡CA源代码 【片C原程序】 [复制链接]

楼层直达
级别: 中级会员
发帖
232
精华
0
金币
622
威望
3
贡献
1
好评
0
注册
2010-01-27
楼主    leiyutian 发表于: 2014-12-15 09:48:52 
[post]/*****************************************************************************
File Name   : ird.c
Description : Irdeto CA 处理程序
History   :
Author :雷雨田Time : 2013-6-26
Reference   :
*****************************************************************************/


/*---------------------------------------------------*/
/*         头文件引用                   */
/*---------------------------------------------------*/
#include "stdlib.h"
#include "st_stdio.h"
#include "string.h"
#include "st_os.h"
#include "ucas.h"
#include "irdeto.h"

/*---------------------------------------------------*/
/*         宏定义                       */
/*---------------------------------------------------*/

#define DEBUG_TEST 0

#if 0
#define IRDETO_PRINT(x) st_printf x
#else
#define IRDETO_PRINT(x)
#endif

#define ENABLE_ALL_CHANNELID
#define USE_CARD_RECOVER_TIME
#define XOR_START   0x3F /* Start value for xor checksum */
#define ADDRLEN     4   /* Address length in EMM commands */
#define MAX_PROV   16
#define MAX_CMD_ERROR_TIME 2
#define MAX_LEN   256
#define SER_DEFAULT_TO 1000
#define MAX_CH_PER_PROV 4

/*---------------------------------------------------*/
/*         类型定义                     */
/*---------------------------------------------------*/

/*---------------------------------------------------*/
/*         外部变量                     */
/*---------------------------------------------------*/

/*---------------------------------------------------*/
/*         全局变量                     */
/*---------------------------------------------------*/

/*---------------------------------------------------*/
/*         静态变量                     */
/*---------------------------------------------------*/

static st_U8 getCountryCode[7] = { 0x01,0x02,0x02,0x03,0x00,0x00,0x3D};
static st_U8 getCardNoSerial[7] = { 0x01,0x02,0x00,0x03,0x00,0x00,0x3F};
static st_U8 getHexSerial[7] = { 0x01,0x02,0x01,0x03,0x00,0x00,0x3E};
static st_U8 getProvider[7] = { 0x01,0x02,0x03,0x03,0x00,0x00,0x00};
static st_U8 getChannelID[7] = { 0x01,0x02,0x04,0x00,0x00,0x01,0x00};
//static st_U8 getCardFile2[]   = { 0x01,0x02,0x0E,0x02,0x00,0x00,0x00};
//static st_U8 getCardFile3[]   = { 0x01,0x02,0x0E,0x03,0x00,0x00 };
static st_U8 ecmCmd[7] = { 0x01,0x05,0x00,0x00,0x02,0x00};
static st_U8 emmCmd[7] =   { 0x01,0x01,0x00,0x00,0x00,0x00};
static st_U8 tempnum = 0;
//static st_U8 getCountryCode2[] = { 0x01,0x02,0x0B,0x00,0x00,0x00 };
//static st_U8 getCamKey384CZ[] = { 0x01,0x02,0x09,0x03,0x00,0x40,
//     0x18,0xD7,0x55,0x14,0xC0,0x83,
//     0xF1,0x38,0x39,0x6F,0xF2,0xEC,
//     0x4F,0xE3,0xF1,0x85,0x01,0x46,
//     0x06,0xCE,0x7D,0x08,0x2C,0x74,
//     0x46,0x8F,0x72,0xC4,0xEA,0xD7,
//     0x9C,0xE0,0xE1,0xFF,0x58,0xE7,
//     0x70,0x0C,0x92,0x45,0x26,0x18,
//     0x4F,0xA0,0xE2,0xF5,0x9E,0x46,
//     0x6F,0xAE,0x95,0x35,0xB0,0x49,
//     0xB2,0x0E,0xA4,0x1F,0x8E,0x47,
//     0xD0,0x24,0x11,0xD0   };
//static st_U8 getCamKey384DZ[] = { 0x01,0x02,0x09,0x03,0x00,0x40,
//     0x27,0xF2,0xD6,0xCD,0xE6,0x88,
//     0x62,0x46,0x81,0xB0,0xF5,0x3E,
//     0x6F,0x13,0x4D,0xCC,0xFE,0xD0,
//     0x67,0xB1,0x93,0xDD,0xF4,0xDE,
//     0xEF,0xF5,0x3B,0x04,0x1D,0xE5,
//     0xC3,0xB2,0x54,0x38,0x57,0x7E,
//     0xC8,0x39,0x07,0x2E,0xD2,0xF4,
//     0x05,0xAA,0x15,0xB5,0x55,0x24,
//     0x90,0xBB,0x9B,0x00,0x96,0xF0,
//     0xCB,0xF1,0x8A,0x08,0x7F,0x0B,
//     0xB8,0x79,0xC3,0x5D   };
static st_U8 getCamKey383C[80] = {   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 unsigned char camKey[] =
{
  0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88
};

static const unsigned char cryptTable[256] =
{
0xDA,0x26,0xE8,0x72,0x11,0x52,0x3E,0x46,0x32,0xFF,0x8C,0x1E,0xA7,0xBE,0x2C,0x29,
0x5F,0x86,0x7E,0x75,0x0A,0x08,0xA5,0x21,0x61,0xFB,0x7A,0x58,0x60,0xF7,0x81,0x4F,
0xE4,0xFC,0xDF,0xB1,0xBB,0x6A,0x02,0xB3,0x0B,0x6E,0x5D,0x5C,0xD5,0xCF,0xCA,0x2A,
0x14,0xB7,0x90,0xF3,0xD9,0x37,0x3A,0x59,0x44,0x69,0xC9,0x78,0x30,0x16,0x39,0x9A,
0x0D,0x05,0x1F,0x8B,0x5E,0xEE,0x1B,0xC4,0x76,0x43,0xBD,0xEB,0x42,0xEF,0xF9,0xD0,
0x4D,0xE3,0xF4,0x57,0x56,0xA3,0x0F,0xA6,0x50,0xFD,0xDE,0xD2,0x80,0x4C,0xD3,0xCB,
0xF8,0x49,0x8F,0x22,0x71,0x84,0x33,0xE0,0x47,0xC2,0x93,0xBC,0x7C,0x3B,0x9C,0x7D,
0xEC,0xC3,0xF1,0x89,0xCE,0x98,0xA2,0xE1,0xC1,0xF2,0x27,0x12,0x01,0xEA,0xE5,0x9B,
0x25,0x87,0x96,0x7B,0x34,0x45,0xAD,0xD1,0xB5,0xDB,0x83,0x55,0xB0,0x9E,0x19,0xD7,
0x17,0xC6,0x35,0xD8,0xF0,0xAE,0xD4,0x2B,0x1D,0xA0,0x99,0x8A,0x15,0x00,0xAF,0x2D,
0x09,0xA8,0xF5,0x6C,0xA1,0x63,0x67,0x51,0x3C,0xB2,0xC0,0xED,0x94,0x03,0x6F,0xBA,
0x3F,0x4E,0x62,0x92,0x85,0xDD,0xAB,0xFE,0x10,0x2E,0x68,0x65,0xE7,0x04,0xF6,0x0C,
0x20,0x1C,0xA9,0x53,0x40,0x77,0x2F,0xA4,0xFA,0x6D,0x73,0x28,0xE2,0xCD,0x79,0xC8,
0x97,0x66,0x8E,0x82,0x74,0x06,0xC7,0x88,0x1A,0x4A,0x6B,0xCC,0x41,0xE9,0x9D,0xB8,
0x23,0x9F,0x3D,0xBF,0x8D,0x95,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
};

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" },
};

static UCAS_SMARTStatus_t* pstSmartStatus;
static Irdeto_Info_t*   pstIrdetoInfo;
//static semaphore_t*   pstIrdetoLock;
static TXOS_SEMAPHORE* pstIrdetoLock;

/*---------------------------------------------------*/
/*         函数原型                     */
/*---------------------------------------------------*/
static st_BOOL IrdetoCheckStatus(st_U8* PB0);
static unsigned char GetXorSum(const unsigned char *mem, int len);
static int IrdetoWrite(SMART_Handle_t Handle,
      unsigned char* cmd,
      st_U8* Response,
      st_U16 goodSB,
      st_U8* IrdetoStatus);
//static void SetHexSer(unsigned char hb, const unsigned char *hs);
static void SetProviderIrdeto(unsigned char pb, const unsigned char *pi,int iNum);
static void RotateLeft8Byte(unsigned char *key);
static void RevCamCrypt(const unsigned char *key, unsigned char *data);

/*---------------------------------------------------*/
/*         程序主体                     */
/*---------------------------------------------------*/

/*-------------------------------------------------------------------*/
/*   函 数 名:IrdetoInit(void)                         */
/*   函数功能:Irdeto CA初始化函数                       */
/*   参   数:                                   */
/*   返 回 值:                                   */
/*   全局变量:                                   */
/*   调用模块:                                     */
/*   作   者:                               */
/*   日   期:2005-12-29                             */
/*   修   改:                                   */
/*   日   期:                                   */
/*   备   注:                                   */
/*-------------------------------------------------------------------*/
void Irdetofindchannelindex(st_U8 *input)
{
int i;
for(i=0;i<pstIrdetoInfo->aucChannelCount;i++)
{
  if(pstIrdetoInfo->aucChannelId==input[0])
  {
  pstIrdetoInfo->aucDateInfo=((input[1]<<16)+(input[2]<<8)+input[3]);
  return;
  }
}
pstIrdetoInfo->aucChannelId = input[0];
pstIrdetoInfo->aucDateInfo = ((input[1]<<16)+(input[2]<<8)+input[3]);
pstIrdetoInfo->aucChannelCount ++;
}
st_BOOL IrdetoInit(int index)
{
  int r;
int rlen;
int i,j,k;
unsigned char ucIrdetoStatus[2];
unsigned char Response[256];

st_U8 cmd[100]={0};

/* Get Smart Status Block */
pstSmartStatus = UCAS_GetSMCStatus(index);
if(pstSmartStatus == NULL)
{
  IRDETO_PRINT(("Irdeto Init failed : can not get status block !!\n"));
  return FALSE;
}
pstIrdetoInfo = &(pstSmartStatus->smart_info.irdeto);
pstIrdetoLock = &(pstSmartStatus->SmartLockSem);
st_memset(pstIrdetoInfo,0,sizeof(Irdeto_Info_t));
// semaphore_init_fifo(pstIrdetoLock,1);
if(st_OS_CreateSemaphore(pstIrdetoLock, "pstIrdetoLock",1,EN_TXOS_SUSPEND_FIFO)!=EN_TXOS_SUCCESS)                      
{
  //return FALSE;
}

/* Get Irdeto Country Code */
r=IrdetoWrite(pstSmartStatus->Handle,getCountryCode,Response,0x0000,ucIrdetoStatus);
if((r>0) &&IrdetoCheckStatus(ucIrdetoStatus) && r>=16)
{
  pstIrdetoInfo->uACS = Response[8]*256+Response[9];

  /* Byte11 ~ Byte19 are Irdeto Super CAS Id */
  pstIrdetoInfo->uSmartCAId = Response[13]*256+Response[14];
  pstIrdetoInfo->ucNationality[0] = Response[21];
  pstIrdetoInfo->ucNationality[1] = Response[22];
  pstIrdetoInfo->ucNationality[2] = Response[23];
  IRDETO_PRINT(("IrdetoGetCountryCode : ACS=%x Nationality: %02x %02x %02x\n",pstIrdetoInfo->uACS,pstIrdetoInfo->ucNationality[0],
          pstIrdetoInfo->ucNationality[1],pstIrdetoInfo->ucNationality[2]));
  IRDETO_PRINT(("IrdetoCAID=%x\n",pstIrdetoInfo->uSmartCAId));
}
else
{
  IRDETO_PRINT(("Get Irdeto Country Code Failed !!\n"));
  return FALSE;
}

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

/* Get Hex Serial */
r=IrdetoWrite(pstSmartStatus->Handle,getHexSerial,Response,0,ucIrdetoStatus);
if(IrdetoCheckStatus(ucIrdetoStatus)&&(r>=25))
{
  pstIrdetoInfo->ucNoOfProv = Response[18];
  pstIrdetoInfo->ucHexBase = Response[23];
  st_memcpy(pstIrdetoInfo->ucHexSerial,&Response[20],3);
  IRDETO_PRINT(("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
{
  IRDETO_PRINT(("Get Irdeto Hex Serial No Failed !!\n"));
  return FALSE;
}

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

/* Get Channel ID */
pstIrdetoInfo->aucChannelCount = 0;
 
for(i=0;i<pstIrdetoInfo->ucNoOfProv;i++)
{
  for(j=0;j<MAX_CH_PER_PROV;j++)
  {
  st_memcpy(cmd,getChannelID,sizeof(getChannelID));
  IRDETO_PRINT(("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(pstSmartStatus->Handle,cmd,Response,0,ucIrdetoStatus);
  for(k=0;k<Response[7];k+=6)
  {
  if(Response[k+8]==0xFF)
    continue;
  Irdetofindchannelindex(&Response[k+9]);
 
//   pstIrdetoInfo->aucChannelId[pstIrdetoInfo->aucChannelCount] = Response[k+9];
//   pstIrdetoInfo->aucDateInfo[pstIrdetoInfo->aucChannelCount] = \
//   (Response[k+10]<<16)+(Response[k+11]<<8)+Response[k+12];
//  
//   st_printf("ChannelCount=%d ChannelID:%d\n",pstIrdetoInfo->aucChannelCount,\
//     pstIrdetoInfo->aucChannelId[pstIrdetoInfo->aucChannelCount]);
//
//   pstIrdetoInfo->aucChannelCount++;
  }
  }
}


/*
/ * This is no longer useful * /
if(!((r=IrdetoWrite(getCardFile2,Response,0,ucIrdetoStatus))>0 &&\
  IrdetoCheckStatus(ucIrdetoStatus) && r>=73))
{
  return;
}
if(!((r=IrdetoWrite(getCardFile3,Response,0,ucIrdetoStatus))>0 &&\
  IrdetoCheckStatus(ucIrdetoStatus) && r>=73))
{
  return;
}
*/

/*
if(ACS==0x0384)
{
  if(IrdetoSmartCAId==0x1702)
  r = IrdetoWrite(getCamKey384CZ,Response,0,ucIrdetoStatus);
  else
  r = IrdetoWrite(getCamKey384DZ,Response,0,ucIrdetoStatus);
  rlen = 17;
}
else
*/

/* Write Cam Key to Card */
{
  r = IrdetoWrite(pstSmartStatus->Handle,getCamKey383C,Response,0x5500,ucIrdetoStatus);
  rlen = 9;
}
if(!(r>0 && IrdetoCheckStatus(ucIrdetoStatus) && r==rlen))
{
  IRDETO_PRINT(("Irdeto Init : Write CAM ID failed !!\n"));
  return FALSE;
}
/*
if(!((r=IrdetoWrite(getCountryCode2,Response,0,ucIrdetoStatus))>0 &&\
  IrdetoCheckStatus(ucIrdetoStatus) && r>=32))
  st_printf("Irdeto get country code 2 failed !!\n");
*/
IRDETO_PRINT(("Irdeto Init OK !!\n"));
  return TRUE;
}

void IrdetoRefreshProvInfo(st_BOOL bUpdateProvInfo)
{
int i,j,k,r;
unsigned char ucIrdetoStatus[2];
unsigned char Response[256];
unsigned char uLastChannelCount = pstIrdetoInfo->aucChannelCount;
st_U8 cmd[100]={0};



/* Get Hex Serial */
if(bUpdateProvInfo)
{
  /* Get Provide Info */
  for(i=0;i<pstIrdetoInfo->ucNoOfProv;i++)
  {
  getProvider[4] = i;
  r=IrdetoWrite(pstSmartStatus->Handle,getProvider,Response,0,ucIrdetoStatus);
  if(IrdetoCheckStatus(ucIrdetoStatus) && r>=33)
  {
  SetProviderIrdeto(Response[8],&Response[9],i);
/*
  st_printf("Provider%d Info : ProvBase=%02x ProvId=%02x %02x %02x\n",i,
      provBase,provId[0],provId[1],provId[2]);
*/
  }else
  {
  return;/*niu 2007.04.26*/
  }
/*
  else
  {
  st_printf("Get Provider Info i=%d r=%d\n",i,r);
  }
*/
  }
}
else
{
  /* Get Channel ID */
  pstIrdetoInfo->aucChannelCount = 0;

  st_memcpy(cmd,getChannelID,sizeof(getChannelID));

  for(i=0;i<pstIrdetoInfo->ucNoOfProv;i++)
  {
  for(j=0;j<MAX_CH_PER_PROV;j++)
  {
  /*st_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(pstSmartStatus->Handle,cmd,Response,0,ucIrdetoStatus);
  if(r<=0)
  {  
    return;/*niu 2007.04.26*/
  }
  for(k=0;k<Response[7];k+=6)
  {
    if(Response[k+8]==0xFF)
    continue;
//   pstIrdetoInfo->aucChannelId[pstIrdetoInfo->aucChannelCount] = Response[k+9];
//   pstIrdetoInfo->aucDateInfo[pstIrdetoInfo->aucChannelCount] = \
//   (Response[k+10]<<16)+(Response[k+11]<<8)+Response[k+12];
//   pstIrdetoInfo->aucChannelCount++;
    Irdetofindchannelindex(&Response[k+9]);
  }
  }
  }
  if(pstIrdetoInfo->aucChannelCount != uLastChannelCount)
  {
  /* TODO : Send message to UCAS to request ECM */
  UCAS_ReportStatus(UCAS_CA_MODULE,UCAS_RESTART_ECM,EN_IRDETO_CARD);
  }
}
}

void IrdetoNotify(int index)
{
pstSmartStatus = UCAS_GetSMCStatus(index);
if(pstSmartStatus != NULL)
  st_memset(&(pstSmartStatus->smart_info.irdeto),0,sizeof(Irdeto_Info_t));
}

static st_BOOL IrdetoCheckStatus(st_U8* PB)
{
int i;
for(i=0;i<17;i++)
{
  if(PB[0]==IrdetoStatusMsgs.uPBWords[0] && \
  (PB[1]==IrdetoStatusMsgs.uPBWords[1] || IrdetoStatusMsgs.uPBWords[1]==0xFF))
  return IrdetoStatusMsgs.bResult;
}
return FALSE;
}

static unsigned char GetXorSum(const unsigned char *mem, int len)
{
  unsigned char cs=0;
  while(len>0) { cs ^= *mem++; len--; }
  return cs;
}

static int IrdetoWrite(SMART_Handle_t Handle,unsigned char* cmd,st_U8* Response,st_U16 goodSB,st_U8* Status)
{
int len;
int r;
st_U16 Read =0;
SMC_ErrorCode_t error;
int iRetries;
len = cmd[5]+6;
cmd[len] = GetXorSum(cmd,len)^XOR_START;
len += 1;
r = 0;
//semaphore_wait(pstIrdetoLock);
st_OS_WaitOnSemaphore(pstIrdetoLock,TXOS_WAIT);
iRetries = 3;
//do
{
  error = SMART_Transfer(Handle,cmd,len,Response,0,&Read,Status);
  IRDETO_PRINT(("Read = %x Response[0]=%x\n",Read,Response[0]));
} //while((Response[0] != 0x01)&&((--iRetries)>0));

if(error==SMC_NO_ERROR)
{
  len = 4;
  if(Response[0]==cmd[0] && Response[1]==cmd[1])
  {
  if(Status)
  {
  Status[0] = Response[2];
  Status[1] = Response[3];
  }
  if((Response[2]*256+Response[3])==goodSB)
  {
  len += 5;
  if(Response[7])
  {
    len += Response[7];
    if(GetXorSum(Response,len)==XOR_START)
    r = len;
  }
  else
  {
    r = len;
  }
  }
  }
  else
  {
  if(Status)
  {
  Status[0] = Response[1];
  Status[1] = Response[2];
  }
  r = 3;
  }
}
//semaphore_signal(pstIrdetoLock);
st_OS_FreeSemaphore(pstIrdetoLock);

// st_printf("r=======================%d\n",r);
return r;
}

st_BOOL IrdetoDecode(SMART_Handle_t Handle,unsigned char* data,unsigned char* cw)
{
int len;
unsigned char cmd[255]={0};
unsigned char ucIrdStatus[2];
unsigned char Response[100];

int i;

len = data[2]-3;
#if DEBUG_TEST
st_printf("IrdetoDecode---------------->\n");
for(i=0;i<7;i++)
{
  st_printf("ecmCmd_1[%d]==0x%02x ",i,ecmCmd);
}
st_printf(" \n\n");
#endif

st_memcpy(cmd,ecmCmd,sizeof(ecmCmd));

#if DEBUG_TEST
for(i=0;i<6;i++)
{
  st_printf("cmd[%d]==0x%02x ",i,cmd);
}
st_printf(" \n\n");
#endif

cmd[5] = len;
st_memcpy(cmd+6,&data[6],len);
#if DEBUG_TEST
st_printf("Irdeto Decode Write ..........\n");
for(i=0;i<len+5;i++)
{
  if(i%10==0)
  st_printf("\n");
  st_printf("0x%02x ",cmd);
}
st_printf("\n");
#endif
len=IrdetoWrite(Handle,cmd,Response,0x9D00,ucIrdStatus);
if(len>0)
{
  //st_printf("111111111111111111111111111\n");
  if(IrdetoCheckStatus(ucIrdStatus) && (len>=23))
  {
  if(Response[6]==0x02)
  {
  RevCamCrypt(camKey,&Response[14]);
  RevCamCrypt(camKey,&Response[22]);
  st_memcpy(cw,&Response[14],8);
  st_memcpy(cw+8,&Response[22],8);
  }
  return TRUE;
  }
}
return FALSE;
}

//static void SetHexSer(unsigned char hb, const unsigned char *hs)
//{
// pstIrdetoInfo->ucHexBase=hb;
// st_memcpy(pstIrdetoInfo->ucHexSerial,hs,3);
//}

static void SetProviderIrdeto(unsigned char pb, const unsigned char *pi,int iNum)
{
if(iNum>=11) return;
if((pi[0]==0x00)&&(pi[1]==0x00))
{
  pstIrdetoInfo->aucProvId[iNum][0] = 0xFF;
  pstIrdetoInfo->aucProvId[iNum][1] = 0xFF;
  pstIrdetoInfo->aucProvId[iNum][2] = 0xFF;
  pstIrdetoInfo->aucProvBase[iNum] = 0xFF;
}
else if((pi[0]==0xFF)&&(pi[1]==0xFF))
{
  pstIrdetoInfo->aucProvId[iNum][0] = 0xFF;
  pstIrdetoInfo->aucProvId[iNum][1] = 0xFF;
  pstIrdetoInfo->aucProvId[iNum][2] = 0xFF;
  pstIrdetoInfo->aucProvBase[iNum] = 0xFF;
}
else
{
  pstIrdetoInfo->aucProvBase[iNum]=pb;
  st_memcpy(pstIrdetoInfo->aucProvId[iNum],pi,3);
}
}

static void RotateLeft8Byte(unsigned char *key)
{
unsigned char t1=key[7];
int k;
for(k=0 ; k<8 ; k++)
{
  unsigned char t2=t1>>7;
  t1=key[k]; key[k]=(t1<<1) | t2;
}
}

static void RevCamCrypt(const unsigned char *key, unsigned char *data)
{
  unsigned char localKey[8];
  int idx1,idx2;
  st_memcpy(localKey,key,sizeof(localKey));
  for(idx1=0 ; idx1<8 ; idx1++)
  {
  for(idx2=0 ; idx2<8 ; idx2++)
  {
  const unsigned char tmp1=cryptTable[data[7] ^ localKey[idx2] ^ idx1];
  const unsigned char tmp2=data[0];
  data[0]=data[1];
  data[1]=data[2];
  data[2]=data[3];
  data[3]=data[4];
  data[4]=data[5];
  data[5]=data[6] ^ tmp1;
  data[6]=data[7];
  data[7]=tmp1 ^ tmp2 ;
  }
  RotateLeft8Byte(localKey);
  }
}

st_BOOL IrdetoParseECM(SMART_Handle_t Handle,unsigned char* pbuf,unsigned char* pucCW)
{
unsigned char cw[16];
pstSmartStatus = UCAS_GetSmartStatusByHandle(Handle);
if(pstSmartStatus == NULL)
{
  st_printf("Irdeto Ecm Error : Get Control Block failed !!\n");
  return FALSE;
}
pstIrdetoInfo = &(pstSmartStatus->smart_info.irdeto);
if(CheckIrdetoECMCHID(pbuf[7]))
{
  if(IrdetoDecode(Handle,pbuf,cw))
  {
  cw[3] = cw[0]+cw[1]+cw[2];
  cw[7] = cw[4]+cw[5]+cw[6];
  cw[11] = cw[8]+cw[9]+cw[10];
  cw[15] = cw[12]+cw[13]+cw[14];
  st_memcpy(pucCW,cw,16);
  return TRUE;
  }else
  {
  st_printf("Irdeto Ecm Error : IrdetoDecode failed !!\n");
  }
}
else
{
  st_printf("Irdeto Ecm Error : CheckIrdetoECMCHID failed !!\n");
}
return FALSE;
}

unsigned int AddrLen(const unsigned char *data)
{
  return data[3] & 0x07;
}

unsigned int AddrBase(const unsigned char *data)
{
  return data[3] >> 3;
}

st_BOOL IrdetoUpdate(SMART_Handle_t Handle,unsigned char *data)
{
unsigned char ucIrdetoStatus[2];
unsigned char Response[256];
int iUAType = 0;
int i=0;
int r=0;
unsigned int mode=AddrBase(data);
unsigned int len=AddrLen(data);

emmCmd[0]=0x01;
emmCmd[1]=0x01;
emmCmd[2]=0x00;
emmCmd[3]=0x00;
emmCmd[4]=0x00;
emmCmd[5]=0x00;

/*st_printf("mode = %x len=%d\n",mode,len);*/
if(mode&0x10)
{
  if(mode==pstIrdetoInfo->ucHexBase && (!len || !memcmp(&data[4],pstIrdetoInfo->ucHexSerial,3)))
  iUAType=1;
  }
else
{
  for(i=0; i<pstIrdetoInfo->ucNoOfProv; i++)
  {
  if(!len || !memcmp(&data[4],pstIrdetoInfo->aucProvId,2))
  {
  iUAType=2;
  break;
  }
  }
  }
/*st_printf("UAType=%d\n",iUAType);*/
if(iUAType > 0)
{
  unsigned char cmdBuff[188];
  st_memcpy(cmdBuff,emmCmd,sizeof(emmCmd)); /* copy card command */
  len=AddrLen(data)+1;

  /*st_printf("length = %d\n",len);*/

  if(iUAType == 1) /* Hex Address */
  {
  /*st_printf("Hex Address EMM !!\n");*/
        cmdBuff[5]=data[2]-2;
  cmdBuff[6]=data[3];
  cmdBuff[7]=pstIrdetoInfo->ucHexSerial[0];
  cmdBuff[8]=pstIrdetoInfo->ucHexSerial[1];
  cmdBuff[9]=pstIrdetoInfo->ucHexSerial[2];
  st_memcpy(&cmdBuff[10],&data[9],data[10]+2);
  cmdBuff[5] = data[10]+2+4;
  }
  else if(iUAType==2)
  {
  cmdBuff[6] = data[3];
  cmdBuff[7] = data[4];
  cmdBuff[8] = data[5];
  cmdBuff[9] = data[6];
  /*st_printf("Provider Address EMM !!\n");*/
  if(AddrLen(data) == 2)
  {
  cmdBuff[5] = data[2]-1;
  /*st_printf("cpy data len sa 1=%d\n",len);*/
  st_memcpy(&cmdBuff[10],&data[8],cmdBuff[5]-4);
  }
  else /*fix subscribtion bugs must copy 3 data of SA*/
  {
  cmdBuff[5] = data[2]-2;
  /*st_printf("cpy data len sa 2=%d\n",len);*/
  st_memcpy(&cmdBuff[10],&data[9],cmdBuff[5]-4);
  }
  }
  r=IrdetoWrite(Handle,cmdBuff,Response,0,ucIrdetoStatus);
  if((r>0)&& IrdetoCheckStatus(ucIrdetoStatus))
  {
/*
  st_printf("Irdeto Update Successful !! status=%02x%02x\n",ucIrdetoStatus[0],ucIrdetoStatus[1]);
  for(i=0;i<r;i++) st_printf("%02x ",Response);
  st_printf("\n\n");
*/
  return TRUE;
  }
  }
return FALSE;
}

st_BOOL IrdetoParseEMM(SMART_Handle_t Handle,unsigned char* buf,int length)
{
int i,len,mod,r;
st_BOOL accept,bIsHexSerial,bIsProviderUp;
unsigned char aucEmmCmd[255];
unsigned char ucIrdetoStatus[2];
unsigned char Response[255];

/* Get Smart Status Control Block */
pstSmartStatus = UCAS_GetSmartStatusByHandle(Handle);
if(pstSmartStatus == NULL)
{
  IRDETO_PRINT(("Irdeto Emm Error : Get Control Block failed !!\n"));
  return FALSE;
}
pstIrdetoInfo = &(pstSmartStatus->smart_info.irdeto);

/* Calculate which mode will be used */
len = buf[0]&0x07;
mod = buf[0]>>3;
accept = FALSE;
bIsHexSerial = FALSE;
bIsProviderUp = FALSE;

/* Check EMM Authorize */
if(mod&0x10)
{
  /* Hex Address Case */
  if(mod==pstIrdetoInfo->ucHexBase && (!len || !memcmp(&buf[1],pstIrdetoInfo->ucHexSerial,3)))
  {
  accept = TRUE;
  bIsHexSerial = TRUE;
  }
}
else
{
  /* Provider Address Case */
  for(i=0;i<pstIrdetoInfo->ucNoOfProv;i++)
  {
  if((!len || !memcmp(&buf[1],pstIrdetoInfo->aucProvId,2))) /* Not 3 !! */
  {
  accept = TRUE;
  bIsProviderUp = TRUE;
  break;
  }
  }
}

/* if accept,send emm to card */
if(accept)
{
  len++;
  st_memset(aucEmmCmd,0,sizeof(aucEmmCmd));
  st_memcpy(aucEmmCmd,emmCmd,sizeof(emmCmd));

  /* HEX Processing */
  if(mod&0x10)
  {
  aucEmmCmd[5] = length-1;
  st_memcpy(aucEmmCmd+6,buf,4);
  st_memcpy(aucEmmCmd+10,buf+6,length-5);
  }
  else
  {
  aucEmmCmd[5] = length-1;
  if(len==3)
  {
  st_memcpy(aucEmmCmd+6,buf,4);
  st_memcpy(aucEmmCmd+10,buf+5,aucEmmCmd[5]-3);
  }
  else
  {
  st_memcpy(aucEmmCmd+6,buf,4);
  st_memcpy(aucEmmCmd+10,buf+6,aucEmmCmd[5]-4);
  }

  /* Adjust length filed, Very important !! */
  if(len==3)
  aucEmmCmd[5]++;
  }

  /* Send Cmd to Card */
  r=IrdetoWrite(Handle,aucEmmCmd,Response,0,ucIrdetoStatus);
  if((r>0 )&& IrdetoCheckStatus(ucIrdetoStatus))
  {
  if(bIsHexSerial)
  {
  IrdetoRefreshProvInfo(TRUE);

  /* TODO : Referesh EMM Section Request */
  /* We have to send message to UCAS to refresh EMM section request */
  UCAS_ReportStatus(UCAS_CA_MODULE,UCAS_RESTART_EMM,EN_IRDETO_CARD);
  }
  if(bIsProviderUp)
  {
  IrdetoRefreshProvInfo(FALSE);
  }
  return TRUE;
  }
}
return FALSE;
}

void IrdetoEMMSectionReq(unsigned short pid)
{
// int iChannelID,iFilterID,i,j;
// st_BOOL bAlreadyAlloc = FALSE;
// st_U8 aucData[8],aucMask[8];
// uIrdetoEmmPid = pid;
// SECTION_FreeSlot(iIrdetoEmmSlot);
// iChannelID = SECTION_AllocateSlot(pid,IrdetoEmmSectionCallBack);
// if(iChannelID >= 0)
// {
// iIrdetoEmmSlot = iChannelID;
// iFilterID = SECTION_AllocateFilter(iChannelID);
// if(iFilterID >= 0)
// {
//   st_memset(aucData,0,sizeof(aucData));
//   st_memset(aucMask,0,sizeof(aucMask));
//   aucData[0] = 0x82;
//   aucData[1] = 0x00;
//   aucData[2] = hexSer[0];
//   aucData[3] = hexSer[1];
//   aucData[4] = hexSer[2];
//   aucMask[0] = 0xFF;
//   aucMask[1] = 0x00;
//   aucMask[2] = 0xFF;
//   aucMask[3] = 0xFF;
//   aucMask[4] = 0xFF;
//   SECTION_SetFilter(iChannelID,iFilterID,aucMask,aucData);
// }
// for(i=0;i<numProv && i<10;i++)
// {
//   bAlreadyAlloc = FALSE;
//   for(j=0;j<i;j++)
//   {
//   if((provId[j][0]==provId[0]) && (provId[j][1]==provId[1]))
//   {
//   bAlreadyAlloc = TRUE;
//   break;
//   }
//   }
//   if(bAlreadyAlloc) continue;
//
//   /* ProvId不为全FF且ProvId不为全00 */
//   if(!(((provId[0]==0x00) && (provId[1]==0x00) && (provId[2]==0x00))||\
//     ((provId[0]==0xFF) && (provId[1]==0xFF) && (provId[2]==0xFF))))
//   {
//   iFilterID = SECTION_AllocateFilter(iChannelID);
//   if(iFilterID >= 0)
//   {
//   st_memset(aucMask,0,8);
//   st_memset(aucData,0,8);
//   aucData[0] = 0x82;
//   aucData[1] = 0x00;
//   aucData[2] = provId[0];
//   aucData[3] = provId[1];
//   aucMask[0] = 0xFF;
//   aucMask[1] = 0x00;
//   aucMask[2] = 0xFF;
//   aucMask[3] = 0xFF;
//   SECTION_SetFilter(iChannelID,iFilterID,aucMask,aucData);
//   }
//   }
// }
// SECTION_ControlSlot(iChannelID,SECTION_ENABLE_SLOT);
// }
}

st_BOOL CheckIrdetoECMCHID(st_U8 uChID)
{
int i;
for(i=0;i<pstIrdetoInfo->aucChannelCount;i++)
  if(pstIrdetoInfo->aucChannelId==uChID)
  return TRUE;
return FALSE;
}

void IrdetoECMSectionReq(st_U16 pid,st_BOOL bFirstReq)
{
// int i,iSlotId;
// st_U8 aucMatch[8],aucMusk[8];
// st_memset(aucMatch,0,sizeof(aucMatch));
// st_memset(aucMusk,0,sizeof(aucMusk));
// if(bFirstReq)
// {
// aucMatch[0] = 0x80;
// aucMusk[0] = 0xFE;
// }
// else
// {
// aucMatch[0] = ucIrdetoEcmTableId;
// aucMusk[0] = ucIrdetoEcmTableMatch;
// }
// FreeSectionReq(iIrdetoEcmSlot);
// iSlotId = ECM_SectionReq(pid);
// if(iSlotId >= 0)
// {
// if(uIrdetoChannelCount>0)
// {
//   aucMatch[5] = uIrdChannelId[0];
//   aucMusk[5] = 0xFF;
//   ResetSectionFilter(iSlotId,aucMatch,aucMusk);
//   for(i=1;i<uIrdetoChannelCount;i++)
//   {
//   aucMatch[5] = uIrdChannelId;
//   aucMusk[5] = 0xFF;
//   IncreaseFilterOnSlot(iSlotId,aucMatch,aucMusk);
//   }
// }
// iIrdetoEcmSlot = iSlotId;
// uIrdetoEcmPid = pid;
// }
}

st_BOOL BetaParseECM(SMART_Handle_t Handle,unsigned char* pbuf,st_U8* pucCW)
{
unsigned char cw[16];
if(IrdetoDecode(Handle,pbuf,cw))
{
  cw[3] = cw[0]+cw[1]+cw[2];
  cw[7] = cw[4]+cw[5]+cw[6];
  cw[11] = cw[8]+cw[9]+cw[10];
  cw[15] = cw[12]+cw[13]+cw[14];
  st_memcpy(pucCW,cw,16);
  return TRUE;
}
return FALSE;
}

单片机用:STC11F02E下载程序即是!以下资料设定了隐藏[/post]