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

📄 seca.cpp

📁 DVB-S的softcam源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*
 * Softcam plugin to VDR (C++)
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <byteswap.h>
#include <openssl/sha.h>

#include "common.h"
#include "sc.h"
#include "system.h"
#include "system-common.h"
#include "data.h"
#include "helper.h"
#include "crypto.h"
#include "parse.h"
#include "misc.h"
#include "win32lacks.h"

#define SYSTEM_SECA          0x0100

#define SYSTEM_NAME          "Seca"
#define SYSTEM_PRI           -10
#define SYSTEM_CAN_HANDLE(x) ((x)==SYSTEM_SECA)

#define FILEMAP_DOMAIN       "seca"

#define DEBUG_SECA
//#define DEBUG_SECA_EXTRA
//#define DEBUG_LOG

#ifdef DEBUG_SECA
#define ds(x) { (x); }
#else
#define ds(x) ;
#endif

#ifdef DEBUG_SECA_EXTRA
#define dse(x) { x; }
#else
#define dse(x) ;
#endif

#ifdef DEBUG_LOG
#define dl(x) { (x); }
#else
#define dl(x) ; 
#endif

// -- cPlainKeySeca ------------------------------------------------------------

#define PLAINLEN_SECA_H 8
#define PLAINLEN_SECA_D 16
#define PLAINLEN_SECA_B 90
#define PLAINLEN_SECA_E 6  // exponent key len
#define EMM_MAGIC       1
#define N51_MAGIC       2

class cPlainKeySeca : public cDualKey {
protected:
  virtual bool IsBNKey(void) const;
  virtual int IdSize(void) { return 4; }
  virtual cString PrintKeyNr(void);
public:
  cPlainKeySeca(bool Super);
  virtual bool Parse(const char *line);
  };

static cPlainKeyTypeReg<cPlainKeySeca,'S'> KeyReg;

cPlainKeySeca::cPlainKeySeca(bool Super)
:cDualKey(Super,true)
{}

bool cPlainKeySeca::IsBNKey(void) const
{
  switch(keynr&C2MASK) {
    case MBC('E','1'):
    case MBC('M','1'):
    case MBC('E','3'):
    case MBC('M','3'):
    case MBC('E','5'):
    case MBC('M','5'):
    case MBC('E','9'):
    case MBC('M','9'): return true;
    }
  return false;
}

bool cPlainKeySeca::Parse(const char *line)
{
  unsigned char sid[2];
#ifdef DEBUG
  const char *sline=line;
#endif
  int len;
  if(GetChar(line,&type,1) && (len=GetHex(line,sid,2,false))) {
    type=toupper(type); id=Bin2Int(sid,len);
    line=skipspace(line);
    int emmnr=0, keylen=PLAINLEN_SECA_B;
    if(!strncasecmp(line,"EMM",3)) { // EMM RSA key
      emmnr=EMM_MAGIC;
      line+=3;
      line=skipspace(line);
      }
    else if(!strncasecmp(line,"N51",3)) { // Nano 51 RSA key
      emmnr=N51_MAGIC;
      line+=3;
      line=skipspace(line);
      keylen=toupper(*line)=='E'?1:129;
      }
    bool ok;
    switch(toupper(*line)) {
      case 'E':
      case 'M': ok=GetChar(line,&keynr,2);
                keynr=ADDC3(keynr,emmnr);
                break;
      default:  ok=emmnr ? false : GetHex(line,sid,1);
                keynr=sid[0];
                break;
      }
    if(ok) {
      unsigned char skey[256];
      len=GetHex(line,skey,keylen,false);
      if(IsBNKey()) {
        if(C2(keynr)=='E' && len==PLAINLEN_SECA_E) {
          // support short exponent keys
          memset(&skey[len],0,keylen-len);
          len=keylen;
          }
        }
      else {
        if(len==PLAINLEN_SECA_H || len==PLAINLEN_SECA_D) {
          // support for 16 & 8 byte keys
          keylen=len;
          }
        }
      if(len==keylen) {
        SetKey(skey,keylen);
        return true;
        }
      }
    }
  d(printf("secakey: bad key format '%s'\n",sline))
  return false;
}

cString cPlainKeySeca::PrintKeyNr(void)
{
  char nr[32];
  int q=0;
  switch(C3(keynr)) {
    case EMM_MAGIC: q+=_snprintf(nr+q,sizeof(nr)-q,"EMM "); break;
    case N51_MAGIC: q+=_snprintf(nr+q,sizeof(nr)-q,"N51 "); break;
    }
  if(IsBNKey()) {
    nr[q  ]=(keynr>>8) & 0xff;
    nr[q+1]= keynr     & 0xff;
    nr[q+2]=0;
    q+=2;
    }
  else q+=_snprintf(nr+q,sizeof(nr)-q,"%.2X",keynr);
  return nr;
}

// -- cSecaCardInfo ------------------------------------------------------------

class cSecaCardInfo : public cProviderSeca {
private:
  int len;
public:
  unsigned char key[16];
  //
  bool Parse(const char *line);
  bool Save(FILE *f) { return true; }
  bool IsUpdated(void) { return false; }
  void Updated(void) {}
  bool Cmp(cSecaCardInfo *ci) { return false; }
  int KeySize(void) { return len; }
  };

bool cSecaCardInfo::Parse(const char *line)
{
  return GetHex(line,provId,sizeof(provId)) &&
         GetHex(line,sa,sizeof(sa)) &&
         (len=GetHex(line,key,sizeof(key),false)) && 
         (len==PLAINLEN_SECA_H || len==PLAINLEN_SECA_D);
}

// -- cSecaCardInfos -----------------------------------------------------------

class cSecaCardInfos : public cCardInfos<cSecaCardInfo> {
public:
  cSecaCardInfos(void):cCardInfos<cSecaCardInfo>(SYSTEM_NAME) {}
  };

static cSecaCardInfos Scards;

// -- cSeca --------------------------------------------------------------------

static const unsigned char secaPC1[] = {
  42,57,29,34,  41,53,30,15,
  19,36,23,14,  43,61,12, 3,
  51,49,5,  6,  45,54,52,47,
  63,38,58,22,  60,33,10,26,
  37,35,44, 1,  20,62,28,18,
  46, 9,39, 4,  27,11,21,50,
  31,25, 2, 7,  13,55,59,17
  };

static const unsigned char secaPC2[] = {
  18, 3,21,15,  42,35,37, 8,
  49,41,30,55,  56,29,12,23,
  43,14,7 ,27,  13, 2,11,45,
   4,34,54,51,  22,40,16,25,
  26,48,53,28,   1,17, 5,31,
  50, 6,39,24,  33,47,38,32
  };

static inline void __swap8_4(unsigned char *data)
{
  unsigned char temp[4];
  memcpy(temp,data,4);
  memcpy(data,&data[4],4);
  memcpy(&data[4],temp,4);
}
#define swap8_4(d) __swap8_4(d)

class cSeca {
private:
  static const unsigned char TD[];
  //
  void Fase(unsigned char *data, const unsigned char *key, const unsigned char *T1, const unsigned char *T2);
protected:
  static const unsigned char T1_S1[], T2_S1[];
  //
  void Decrypt(unsigned char *data, const unsigned char *key, const unsigned char *T1, const unsigned char *T2);
  void Encrypt(unsigned char *data, const unsigned char *key, const unsigned char *T1, const unsigned char *T2);
  void CalcSignature(const unsigned char *buff, int len, unsigned char *signature, const unsigned char *k, const unsigned char *T1, const unsigned char *T2);
  // Seca2 functions
  void AdditionalAlgo(unsigned char *data, const unsigned char *key, const int mode);
  };

const unsigned char cSeca::TD[4] = { 1,3,0,2 };

const unsigned char cSeca::T1_S1[256] = { // Original Tables
  0x2a,0xe1,0x0b,0x13,0x3e,0x6e,0x32,0x48, 0xd3,0x31,0x08,0x8c,0x8f,0x95,0xbd,0xd0,
  0xe4,0x6d,0x50,0x81,0x20,0x30,0xbb,0x75, 0xf5,0xd4,0x7c,0x87,0x2c,0x4e,0xe8,0xf4,
  0xbe,0x24,0x9e,0x4d,0x80,0x37,0xd2,0x5f, 0xdb,0x04,0x7a,0x3f,0x14,0x72,0x67,0x2d,
  0xcd,0x15,0xa6,0x4c,0x2e,0x3b,0x0c,0x41, 0x62,0xfa,0xee,0x83,0x1e,0xa2,0x01,0x0e,//8
  0x7f,0x59,0xc9,0xb9,0xc4,0x9d,0x9b,0x1b, 0x9c,0xca,0xaf,0x3c,0x73,0x1a,0x65,0xb1,
  0x76,0x84,0x39,0x98,0xe9,0x53,0x94,0xba, 0x1d,0x29,0xcf,0xb4,0x0d,0x05,0x7d,0xd1,
  0xd7,0x0a,0xa0,0x5c,0x91,0x71,0x92,0x88, 0xab,0x93,0x11,0x8a,0xd6,0x5a,0x77,0xb5,
  0xc3,0x19,0xc1,0xc7,0x8e,0xf9,0xec,0x35, 0x4b,0xcc,0xd9,0x4a,0x18,0x23,0x9f,0x52,//16
  0xdd,0xe3,0xad,0x7b,0x47,0x97,0x60,0x10, 0x43,0xef,0x07,0xa5,0x49,0xc6,0xb3,0x55,
  0x28,0x51,0x5d,0x64,0x66,0xfc,0x44,0x42, 0xbc,0x26,0x09,0x74,0x6f,0xf7,0x6b,0x4f,
  0x2f,0xf0,0xea,0xb8,0xae,0xf3,0x63,0x6a, 0x56,0xb2,0x02,0xd8,0x34,0xa4,0x00,0xe6,
  0x58,0xeb,0xa3,0x82,0x85,0x45,0xe0,0x89, 0x7e,0xfd,0xf2,0x3a,0x36,0x57,0xff,0x06,//24
  0x69,0x54,0x79,0x9a,0xb6,0x6c,0xdc,0x8b, 0xa7,0x1f,0x90,0x03,0x17,0x1c,0xed,0xd5,
  0xaa,0x5e,0xfe,0xda,0x78,0xb0,0xbf,0x12, 0xa8,0x22,0x21,0x3d,0xc2,0xc0,0xb7,0xa9,
  0xe7,0x33,0xfb,0xf1,0x70,0xe5,0x17,0x96, 0xf8,0x8d,0x46,0xa1,0x86,0xe2,0x40,0x38,
  0xf6,0x68,0x25,0x16,0xac,0x61,0x27,0xcb, 0x5b,0xc8,0x2b,0x0f,0x99,0xde,0xce,0xc5
  };

const unsigned char cSeca::T2_S1[256] = {
  0xbf,0x11,0x6d,0xfa,0x26,0x7f,0xf3,0xc8, 0x9e,0xdd,0x3f,0x16,0x97,0xbd,0x08,0x80,
  0x51,0x42,0x93,0x49,0x5b,0x64,0x9b,0x25, 0xf5,0x0f,0x24,0x34,0x44,0xb8,0xee,0x2e,
  0xda,0x8f,0x31,0xcc,0xc0,0x5e,0x8a,0x61, 0xa1,0x63,0xc7,0xb2,0x58,0x09,0x4d,0x46,
  0x81,0x82,0x68,0x4b,0xf6,0xbc,0x9d,0x03, 0xac,0x91,0xe8,0x3d,0x94,0x37,0xa0,0xbb, //8
  0xce,0xeb,0x98,0xd8,0x38,0x56,0xe9,0x6b, 0x28,0xfd,0x84,0xc6,0xcd,0x5f,0x6e,0xb6,
  0x32,0xf7,0x0e,0xf1,0xf8,0x54,0xc1,0x53, 0xf0,0xa7,0x95,0x7b,0x19,0x21,0x23,0x7d,
  0xe1,0xa9,0x75,0x3e,0xd6,0xed,0x8e,0x6f, 0xdb,0xb7,0x07,0x41,0x05,0x77,0xb4,0x2d,
  0x45,0xdf,0x29,0x22,0x43,0x89,0x83,0xfc, 0xd5,0xa4,0x88,0xd1,0xf4,0x55,0x4f,0x78,//16
  0x62,0x1e,0x1d,0xb9,0xe0,0x2f,0x01,0x13, 0x15,0xe6,0x17,0x6a,0x8d,0x0c,0x96,0x7e,
  0x86,0x27,0xa6,0x0d,0xb5,0x73,0x71,0xaa, 0x36,0xd0,0x06,0x66,0xdc,0xb1,0x2a,0x5a,
  0x72,0xbe,0x3a,0xc5,0x40,0x65,0x1b,0x02, 0x10,0x9f,0x3b,0xf9,0x2b,0x18,0x5c,0xd7,
  0x12,0x47,0xef,0x1a,0x87,0xd2,0xc2,0x8b, 0x99,0x9c,0xd3,0x57,0xe4,0x76,0x67,0xca,//24
  0x3c,0xfb,0x90,0x20,0x14,0x48,0xc9,0x60, 0xb0,0x70,0x4e,0xa2,0xad,0x35,0xea,0xc4,
  0x74,0xcb,0x39,0xde,0xe7,0xd4,0xa3,0xa5, 0x04,0x92,0x8c,0xd9,0x7c,0x1c,0x7a,0xa8,
  0x52,0x79,0xf2,0x33,0xba,0x1f,0x30,0x9a, 0x00,0x50,0x4c,0xff,0xe5,0xcf,0x59,0xc3,
  0xe3,0x0a,0x85,0xb3,0xae,0xec,0x0b,0xfe, 0xe2,0xab,0x4a,0xaf,0x69,0x6c,0x2c,0x5d
  };

void cSeca::Fase(unsigned char *D, const unsigned char *key, const unsigned char *T1, const unsigned char *T2)
{
  for(int l=0; l<4; ++l) { D[l]^=key[l]; D[l]=T1[D[l]&0xff]; }
  for(int l=6; l>3; --l) {
    D[(l+2)&3]^=D[(l+1)&3];
    D[l&3]=T2[(sn8(D[(l+1)&3])+D[l&3])&0xff];
    } 
  for(int l=3; l>0; --l) {
    D[(l+2)&3]^=D[(l+1)&3];
    D[l&3]=T1[(sn8(D[(l+1)&3])+D[l&3])&0xff];
    }
  D[2]^=D[1];
  D[1]^=D[0];
}

void cSeca::Decrypt(unsigned char *d, const unsigned char *key, const unsigned char *T1, const unsigned char *T2)
{
  unsigned char k[16],D[4];
  memcpy(k,key,sizeof(k));
  unsigned char C=0xff;
  for(int j=0; j<4; ++j) {
    for(int i=0; i<16; ++i) {
      if((i&3)==0) ++C;
      k[i]^=T1[(k[(15+i)&0xf]^k[(i+1)&0xf]^C)&0xff]; 
      } 
    }
  int j=0;
  for(int i=0; i<16; ++i) {
    for(int l=0; l<4; ++l) D[l]=d[l+4];
    j=(j+12)&0xf;
    Fase(D,&k[j&0xc],T1,T2);
    for(int l=0; l<4; ++l) d[l]^=T2[D[TD[l]]&0xff];
    for(int l=3; l>=0; --l) k[(j+l)&0xf]^=T1[(k[(j+l+1)&0xf]^k[(j+l+15)&0xf]^(15-i))&0xff];
    if(i<15) swap8_4(d);
    }
}

void cSeca::Encrypt(unsigned char *d, const unsigned char *key, const unsigned char *T1, const unsigned char *T2)
{
  unsigned char D[4],kk[16];
  memcpy(kk, key, sizeof(kk));
  for(int j=0, i=0; i<16; ++i, j=(j+4)&0xf) {
    for(int l=0; l<4; ++l) kk[(j+l)&0xf] ^= T1[(kk[(j+l+1)&0xf]^kk[(j+l+15)&0xf]^i)&0xff]; 
    if(i>0) swap8_4(d);
    for(int l=0; l<4; ++l) D[l]=d[l+4];
    Fase(D,&kk[j&0xc],T1,T2);
    for(int l=0; l<4; ++l) d[l]^=T2[D[TD[l]]&0xff];
    } 
}

void cSeca::CalcSignature(const unsigned char *buff, int len, unsigned char *signature, const unsigned char *k, const unsigned char *T1, const unsigned char *T2)
{
  memset(signature,0,8);
  for(int i=0 ; i<len ; i+=8) {
    for(int j=0 ; j<8 && i+j<len; j++) signature[j]^=buff[i+j];
    Encrypt(signature,k,T1,T2);
    }
}

void cSeca::AdditionalAlgo(unsigned char *data, const unsigned char *key, const int mode)
{
  static const unsigned int adders[] = {
    0x0000,0xe555,0xafff,0x5ffe,0xf552,0x6ffb,0xcff9,0x154c,
    0x3ff4,0x4ff1,0x4543,0x1fea,0xdfe6,0x8537,0x0fdd,0x7fd8
    };
  const unsigned short * const k = (const unsigned short *)key;
  unsigned short * const dd = (unsigned short *)data;
  unsigned int d1=dd[0], d2=dd[1], d3=dd[2], d4=dd[3];
  dse(printf("additional algo: %s\n",!mode?"encrypt":"decrypt"))
  if(!mode) {
    for(int i=0; i<0x10; i++) {
      const unsigned int adder = adders[i];
      d1 += (k[0] + d3 + adder) ^ (k[1] + d4 + adder);
      d2 += (k[2] + d3 + adder) ^ (k[3] + d4 + adder);
      d1 = ror16(d1,5);
      d2 = rol16(d2,3);
      d3 += (k[0] + d1 + adder) ^ (k[1] + d2 + adder);
      d4 += (k[2] + d1 + adder) ^ (k[3] + d2 + adder);
      d3 = rol16(d3,3);
      d4 = ror16(d4,5);
      }
    }
  else {
    for(int i=0xf; i>=0; i--) {
      const unsigned int adder = adders[i];
      d4 = rol16(d4,5);
      d3 = ror16(d3,3);
      d4 -= (k[2] + d1 + adder) ^ (k[3] + d2 + adder);
      d3 -= (k[0] + d1 + adder) ^ (k[1] + d2 + adder);
      d2 = ror16(d2,3);
      d1 = rol16(d1,5);
      d2 -= (k[2] + d3 + adder) ^ (k[3] + d4 + adder);
      d1 -= (k[0] + d3 + adder) ^ (k[1] + d4 + adder);
      }
    }
  dd[0]=d1; dd[1]=d2; dd[2]=d3; dd[3]=d4;
}

// -- cSeca2Prov ---------------------------------------------------------------

#define MAX_PERMS    10
#define KEY2INDEX(x) (((x)>>5)&0x03)

struct Perm {
  unsigned char P1[8], P2[8], P3[8], P4[8];
  };

struct ProvData {
  unsigned short LoadId, MTLoadSize, MTSize;
  unsigned char SHA1Pad, SHA1End;
  unsigned char FP[8];
  };

class cSeca2Prov: protected cSeca {
private:
  cFileMap *hash, *mask;
  unsigned short id;
  //
  unsigned short Sum(const unsigned char *data, int n) const;
protected:
  const struct ProvData *pData;
  const struct Perm *perm;
  //
  cFileMap *GetMap(const char *type, int size, bool generic) const;
public:
  cSeca2Prov(unsigned short Id);
  virtual ~cSeca2Prov();
  // provider specific mods
  virtual void PreSSE(unsigned char *data, int pos) {}
  virtual void PostSSE(unsigned char *data, int pos) {}
  virtual void SignatureMod(unsigned char *MD, const unsigned char *PK) {}
  virtual void PreCW(unsigned char *data) {}
  virtual void PostCW(unsigned char *data) {}
  virtual void ChainTableXor(unsigned char *data, unsigned short index)=0;
  virtual bool DoSigCheck(void) { return true; }
  //
  virtual bool Init(void);
  void CalcSHASignature(const unsigned char *data, int n, unsigned char *signature, bool PadMode=true);
  bool DecryptSE(unsigned char *data, const unsigned char *key, int n, int start, const unsigned char *T1, const unsigned char *T2);
  bool Matches(unsigned short Id) const { return Id==id; }
  //
  const unsigned char *T1(int index) const;
  const unsigned char *T2(int index) const;
  const struct Perm *P(int index) const;
  inline const unsigned char *MT(void) const { return mask->Addr(); }

⌨️ 快捷键说明

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