From d3b17f019cee6a9602be29b6ad097ac017c23f52 Mon Sep 17 00:00:00 2001 From: leslie Date: Thu, 27 Dec 2007 22:13:55 +0100 Subject: [PATCH] new Nagra build system --- misc.h | 7 ++ system-common.h | 7 -- systems/nagra/nagra-def.h | 50 ++++++++++ systems/nagra/nagra.c | 1 + systems/nagra/nagra.h | 25 ----- systems/nagra/nagra.mk | 9 +- systems/nagra/nagra1.c | 1 + systems/nagra/nagra2-0101.c | 7 ++ systems/nagra/nagra2-0501.c | 6 ++ systems/nagra/nagra2-4101.c | 6 ++ systems/nagra/nagra2.c | 158 +----------------------------- systems/nagra/nagra2.h | 188 ++++++++++++++++++++++++++++++++++++ 12 files changed, 272 insertions(+), 193 deletions(-) create mode 100644 systems/nagra/nagra-def.h create mode 100644 systems/nagra/nagra2.h diff --git a/misc.h b/misc.h index f24f1ff..b02fd39 100644 --- a/misc.h +++ b/misc.h @@ -25,6 +25,13 @@ #define WORD(buffer,index,mask) (((buffer[(index)]<<8) + buffer[(index)+1]) & mask) #define SCT_LEN(sct) (3+(((sct)[1]&0x0f)<<8)+(sct)[2]) +#define MBC(a,b) (((a)<<8)+(b)) +#define ADDC3(ab,c) ((ab)+((c)<<16)) +#define MBC3(a,b,c) ADDC3(MBC((a),(b)),(c)) +#define C2(x) (((x)>>8)&0xFF) +#define C3(x) (((x)>>16)&0xFF) +#define C2MASK 0xFFFF + // replacement for variable-sized arrays #define AUTOARRAY(type,size) (type *)alloca(sizeof(type)*(size)) #define AUTOMEM(size) (unsigned char *)alloca(size) diff --git a/system-common.h b/system-common.h index 6728b49..396a0b7 100644 --- a/system-common.h +++ b/system-common.h @@ -23,13 +23,6 @@ #include "data.h" #include "system.h" -#define MBC(a,b) (((a)<<8)+(b)) -#define ADDC3(ab,c) ((ab)+((c)<<16)) -#define MBC3(a,b,c) ADDC3(MBC((a),(b)),(c)) -#define C2(x) (((x)>>8)&0xFF) -#define C3(x) (((x)>>16)&0xFF) -#define C2MASK 0xFFFF - // ---------------------------------------------------------------- class cHexKey : public cPlainKey { diff --git a/systems/nagra/nagra-def.h b/systems/nagra/nagra-def.h new file mode 100644 index 0000000..71b43bd --- /dev/null +++ b/systems/nagra/nagra-def.h @@ -0,0 +1,50 @@ +/* + * 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 + */ + +#ifndef __NAGRA_NAGRA_DEF_H +#define __NAGRA_NAGRA_DEF_H + +// ---------------------------------------------------------------- + +#define SYSTEM_NAGRA 0x1800 +#define SYSTEM_NAGRA2 0x1801 +#define SYSTEM_NAGRA_BEV 0x1234 + +// ---------------------------------------------------------------- + +// Nagra1 defines +#define DEF_TYPE 0 +#define DEF_PK 2 +#define DEF_ROM 0 + +#define TYPE(keynr) (((keynr)>>16)&1) +#define PK(keynr) (((keynr)>>17)&3) +#define ROM(keynr) (((keynr)>>19)&15) +#define KEYSET(rom,pk,type) ((((rom)&15)<<3)|(((pk)&3)<<1)|((type)&1)) + +// Nagra2 defines +#define N2_MAGIC 0x80 +#define N2_EMM_G_I 0x02 +#define N2_EMM_G_R 0x12 +#define N2_EMM_S_I 0x01 +#define N2_EMM_S_R 0x11 +#define N2_EMM_SEL 0x40 +#define N2_EMM_V 0x03 + +#endif diff --git a/systems/nagra/nagra.c b/systems/nagra/nagra.c index 392a837..37e4f10 100644 --- a/systems/nagra/nagra.c +++ b/systems/nagra/nagra.c @@ -22,6 +22,7 @@ #include #include "nagra.h" +#include "nagra-def.h" #include "log-nagra.h" #include "log-core.h" diff --git a/systems/nagra/nagra.h b/systems/nagra/nagra.h index 2b2874e..aedad54 100644 --- a/systems/nagra/nagra.h +++ b/systems/nagra/nagra.h @@ -25,35 +25,10 @@ // ---------------------------------------------------------------- -#define SYSTEM_NAGRA 0x1800 -#define SYSTEM_NAGRA2 0x1801 -#define SYSTEM_NAGRA_BEV 0x1234 - extern int minEcmTime; // ---------------------------------------------------------------- -// Nagra1 defines -#define DEF_TYPE 0 -#define DEF_PK 2 -#define DEF_ROM 0 - -#define TYPE(keynr) (((keynr)>>16)&1) -#define PK(keynr) (((keynr)>>17)&3) -#define ROM(keynr) (((keynr)>>19)&15) -#define KEYSET(rom,pk,type) ((((rom)&15)<<3)|(((pk)&3)<<1)|((type)&1)) - -// Nagra2 defines -#define N2_MAGIC 0x80 -#define N2_EMM_G_I 0x02 -#define N2_EMM_G_R 0x12 -#define N2_EMM_S_I 0x01 -#define N2_EMM_S_R 0x11 -#define N2_EMM_SEL 0x40 -#define N2_EMM_V 0x03 - -#define MATCH_ID(x,y) ((((x)^(y))&~0x107)==0) - class cPlainKeyNagra : public cDualKey { private: int ParseTag(const char *tag, const char * &line); diff --git a/systems/nagra/nagra.mk b/systems/nagra/nagra.mk index a8e8430..d511b13 100644 --- a/systems/nagra/nagra.mk +++ b/systems/nagra/nagra.mk @@ -2,11 +2,6 @@ # Nagra # TARGET = nagra -OBJS = nagra.o nagra1.o nagra2.o cpu.o +OBJS = nagra.o nagra1.o nagra2.o cpu.o \ + $(patsubst %.c,%.o,$(wildcard nagra2-[0-9][0-9][0-9][0-9].c)) LIBS = -lcrypto -CLEAN_RM = nagra2-prov.c - -nagra2-prov.c: - echo >$@ "/* generated file, do not edit */" - find -name "nagra2-[0-9][0-9][0-9][0-9].c" -printf '#include "%f"\n' >>$@ - @rm .dependencies diff --git a/systems/nagra/nagra1.c b/systems/nagra/nagra1.c index a2c4e0a..d65cae4 100644 --- a/systems/nagra/nagra1.c +++ b/systems/nagra/nagra1.c @@ -27,6 +27,7 @@ #include "helper.h" #include "nagra.h" +#include "nagra-def.h" #include "cpu.h" #include "log-nagra.h" diff --git a/systems/nagra/nagra2-0101.c b/systems/nagra/nagra2-0101.c index 2e5f2c3..e061d1a 100644 --- a/systems/nagra/nagra2-0101.c +++ b/systems/nagra/nagra2-0101.c @@ -17,11 +17,18 @@ * Or, point your browser to http://www.gnu.org/copyleft/gpl.html */ +#include +#include +#include + +#include "nagra2.h" + // -- cAuxSrv ------------------------------------------------------------------ //#define HAS_AUXSRV #ifdef HAS_AUXSRV +#include "network.h" #define AUX_PROTOCOL_VERSION 2 static int auxEnabled=0; static int auxPort=7777; diff --git a/systems/nagra/nagra2-0501.c b/systems/nagra/nagra2-0501.c index 6821da1..03dfce8 100644 --- a/systems/nagra/nagra2-0501.c +++ b/systems/nagra/nagra2-0501.c @@ -17,6 +17,12 @@ * Or, point your browser to http://www.gnu.org/copyleft/gpl.html */ +#include +#include +#include + +#include "nagra2.h" + // -- cMap0501 ----------------------------------------------------------------- class cMap0501 : public cMapCore { diff --git a/systems/nagra/nagra2-4101.c b/systems/nagra/nagra2-4101.c index 7366ccb..9e5af25 100644 --- a/systems/nagra/nagra2-4101.c +++ b/systems/nagra/nagra2-4101.c @@ -17,6 +17,12 @@ * Or, point your browser to http://www.gnu.org/copyleft/gpl.html */ +#include +#include +#include + +#include "nagra2.h" + // -- cMap4101 ----------------------------------------------------------------- class cMap4101 : public cMapCore { diff --git a/systems/nagra/nagra2.c b/systems/nagra/nagra2.c index d2bda22..b4700cd 100644 --- a/systems/nagra/nagra2.c +++ b/systems/nagra/nagra2.c @@ -22,37 +22,16 @@ #include #include "system.h" -#include "misc.h" #include "opts.h" -#include "network.h" -#include "crypto.h" -#include "helper.h" - -#include -#include -#include "openssl-compat.h" #include "nagra.h" -#include "cpu.h" -#include "log-nagra.h" +#include "nagra2.h" #define SYSTEM_NAME "Nagra2" #define SYSTEM_PRI -10 // -- cN2Emu ------------------------------------------------------------------- -class cN2Emu : protected c6805 { -private: - bool initDone; -protected: - bool Init(int id, int romv); - virtual bool RomInit(void) { return true; } - virtual void Stepper(void) {} -public: - cN2Emu(void); - virtual ~cN2Emu() {} - }; - cN2Emu::cN2Emu(void) { initDone=false; @@ -87,67 +66,6 @@ bool cN2Emu::Init(int id, int romv) // -- cMapCore ----------------------------------------------------------------- -#define SETSIZE 0x02 -#define IMPORT_J 0x03 -#define IMPORT_A 0x04 -#define IMPORT_B 0x05 -#define IMPORT_C 0x06 -#define IMPORT_D 0x07 -#define IMPORT_LAST 0x08 -#define EXPORT_J 0x09 -#define EXPORT_A 0x0A -#define EXPORT_B 0x0B -#define EXPORT_C 0x0C -#define EXPORT_D 0x0D -#define EXPORT_LAST 0x0E -#define SWAP_A 0x0F -#define SWAP_B 0x10 -#define SWAP_C 0x11 -#define SWAP_D 0x12 -#define CLEAR_A 0x13 -#define CLEAR_B 0x14 -#define CLEAR_C 0x15 -#define CLEAR_D 0x16 -#define COPY_A_B 0x17 -#define COPY_B_A 0x18 -#define COPY_A_C 0x19 -#define COPY_C_A 0x1A -#define COPY_C_D 0x1B -#define COPY_D_C 0x1C - -class cMapCore { -private: - int last; - cBN *regs[5]; - cBN x, y, s; -protected: - int wordsize; - cBN A, B, C, D, J, I; - cBN Px, Py, Pz,Qx, Qy, Qz; // 0x00,0x20,0x40,0x60,0x80,0x180 - cBN sA0, sC0, sE0, s100, s120, s140, s160; - cBNctx ctx; - SHA_CTX sctx; - // stateless - void MakeJ0(BIGNUM *j, BIGNUM *d); - void ModAdd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *d); - void ModSub(BIGNUM *r, BIGNUM *d, BIGNUM *b); - void MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b, BIGNUM *c, BIGNUM *d, BIGNUM *j); - // statefull - void MonInit(int bits=0); - void MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b) { MonMul(o,a,b,C,D,J); } - void MonExpNeg(void); - // ECC - void DoubleP(int temp); - void AddP(int temp); - void ToProjective(int set, BIGNUM *x, BIGNUM *y); - void ToAffine(void); - void CurveInit(BIGNUM *a); - // - bool DoMap(int f, unsigned char *data=0, int l=0); -public: - cMapCore(void); - }; - cMapCore::cMapCore(void) { wordsize=4; last=1; @@ -450,37 +368,6 @@ bool cMapCore::DoMap(int f, unsigned char *data, int l) // -- cN2Prov ------------------------------------------------------------------ -#define N2FLAG_NONE 0 -#define N2FLAG_MECM 1 -#define N2FLAG_Bx 2 -#define N2FLAG_POSTAU 4 -#define N2FLAG_Ex 8 -#define N2FLAG_INV 128 - -class cN2Prov { -private: - unsigned char seed[32], cwkey[8]; - bool keyValid; -protected: - int id, flags, seedSize; - cIDEA idea; - // - virtual bool Algo(int algo, unsigned char *hd, const unsigned char *ed, unsigned char *hw) { return false; } - virtual bool NeedsCwSwap(void) { return false; } - void ExpandInput(unsigned char *hw); -public: - cN2Prov(int Id, int Flags); - virtual ~cN2Prov() {} - bool MECM(unsigned char in15, int algo, const unsigned char *ed, unsigned char *cw); - void SwapCW(unsigned char *cw); - virtual int ProcessBx(unsigned char *data, int len, int pos) { return -1; } - virtual int ProcessEx(unsigned char *data, int len, int pos) { return -1; } - virtual bool PostProcAU(int id, unsigned char *data) { return true; } - bool CanHandle(int Id) { return MATCH_ID(Id,id); } - bool HasFlags(int Flags) { return (flags&Flags)==Flags; } - void PrintCaps(int c); - }; - cN2Prov::cN2Prov(int Id, int Flags) { keyValid=false; id=Id|0x100; flags=Flags; seedSize=5; @@ -564,40 +451,7 @@ void cN2Prov::SwapCW(unsigned char *cw) } } -// -- cN2ProvLink & cN2Providers ----------------------------------------------- - -class cN2Providers; - -class cN2ProvLink { -friend class cN2Providers; -private: - cN2ProvLink *next; -protected: - int id, flags; - // - virtual cN2Prov *Create(void)=0; - bool CanHandle(int Id) { return MATCH_ID(Id,id); } - bool HasFlags(int Flags) { return (flags&Flags)==Flags; } -public: - cN2ProvLink(int Id, int Flags); - virtual ~cN2ProvLink() {} - }; - -class cN2Providers { -friend class cN2ProvLink; -private: - static cN2ProvLink *first; - // - static void Register(cN2ProvLink *plink); -public: - static cN2Prov *GetProv(int Id, int Flags); - }; - -template class cN2ProvLinkReg : public cN2ProvLink { -public: - cN2ProvLinkReg(void):cN2ProvLink(ID,FLAGS) {} - virtual cN2Prov *Create(void) { return new PROV(id,flags); } - }; +// -- cN2Providers ------------------------------------------------------------- cN2ProvLink *cN2Providers::first=0; @@ -618,16 +472,14 @@ cN2Prov *cN2Providers::GetProv(int Id, int Flags) return 0; } +// -- cN2ProvLink -------------------------------------------------------------- + cN2ProvLink::cN2ProvLink(int Id, int Flags) { id=Id; flags=Flags; cN2Providers::Register(this); } -#include "nagra2-prov.c" - -#ifndef TESTER - // -- cNagra2 ------------------------------------------------------------------ class cNagra2 : public cNagra { @@ -1093,5 +945,3 @@ bool cSystemLinkNagra2::CanHandle(unsigned short SysId) return ((SysId&SYSTEM_MASK)==SYSTEM_NAGRA && (SysId&0xFF)>0) || SysId==SYSTEM_NAGRA_BEV; } - -#endif //TESTER diff --git a/systems/nagra/nagra2.h b/systems/nagra/nagra2.h new file mode 100644 index 0000000..96e00a1 --- /dev/null +++ b/systems/nagra/nagra2.h @@ -0,0 +1,188 @@ +/* + * 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 + */ + +#ifndef __NAGRA_NAGRA2_H +#define __NAGRA_NAGRA2_H + +#include "data.h" +#include "crypto.h" +#include "misc.h" +#include "helper.h" + +#include +#include +#include "openssl-compat.h" + +#include "nagra-def.h" +#include "cpu.h" +#include "log-nagra.h" + +// ---------------------------------------------------------------- + +#define MATCH_ID(x,y) ((((x)^(y))&~0x107)==0) + +// ---------------------------------------------------------------- + +class cN2Emu : protected c6805 { +private: + bool initDone; +protected: + bool Init(int id, int romv); + virtual bool RomInit(void) { return true; } + virtual void Stepper(void) {} +public: + cN2Emu(void); + virtual ~cN2Emu() {} + }; + +// ---------------------------------------------------------------- + +#define SETSIZE 0x02 +#define IMPORT_J 0x03 +#define IMPORT_A 0x04 +#define IMPORT_B 0x05 +#define IMPORT_C 0x06 +#define IMPORT_D 0x07 +#define IMPORT_LAST 0x08 +#define EXPORT_J 0x09 +#define EXPORT_A 0x0A +#define EXPORT_B 0x0B +#define EXPORT_C 0x0C +#define EXPORT_D 0x0D +#define EXPORT_LAST 0x0E +#define SWAP_A 0x0F +#define SWAP_B 0x10 +#define SWAP_C 0x11 +#define SWAP_D 0x12 +#define CLEAR_A 0x13 +#define CLEAR_B 0x14 +#define CLEAR_C 0x15 +#define CLEAR_D 0x16 +#define COPY_A_B 0x17 +#define COPY_B_A 0x18 +#define COPY_A_C 0x19 +#define COPY_C_A 0x1A +#define COPY_C_D 0x1B +#define COPY_D_C 0x1C + +class cMapCore { +private: + int last; + cBN *regs[5]; + cBN x, y, s; +protected: + int wordsize; + cBN A, B, C, D, J, I; + cBN Px, Py, Pz,Qx, Qy, Qz; // 0x00,0x20,0x40,0x60,0x80,0x180 + cBN sA0, sC0, sE0, s100, s120, s140, s160; + cBNctx ctx; + SHA_CTX sctx; + // stateless + void MakeJ0(BIGNUM *j, BIGNUM *d); + void ModAdd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *d); + void ModSub(BIGNUM *r, BIGNUM *d, BIGNUM *b); + void MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b, BIGNUM *c, BIGNUM *d, BIGNUM *j); + // statefull + void MonInit(int bits=0); + void MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b) { MonMul(o,a,b,C,D,J); } + void MonExpNeg(void); + // ECC + void DoubleP(int temp); + void AddP(int temp); + void ToProjective(int set, BIGNUM *x, BIGNUM *y); + void ToAffine(void); + void CurveInit(BIGNUM *a); + // + bool DoMap(int f, unsigned char *data=0, int l=0); +public: + cMapCore(void); + }; + +// ---------------------------------------------------------------- + +#define N2FLAG_NONE 0 +#define N2FLAG_MECM 1 +#define N2FLAG_Bx 2 +#define N2FLAG_POSTAU 4 +#define N2FLAG_Ex 8 +#define N2FLAG_INV 128 + +class cN2Prov { +private: + unsigned char seed[32], cwkey[8]; + bool keyValid; +protected: + int id, flags, seedSize; + cIDEA idea; + // + virtual bool Algo(int algo, unsigned char *hd, const unsigned char *ed, unsigned char *hw) { return false; } + virtual bool NeedsCwSwap(void) { return false; } + void ExpandInput(unsigned char *hw); +public: + cN2Prov(int Id, int Flags); + virtual ~cN2Prov() {} + bool MECM(unsigned char in15, int algo, const unsigned char *ed, unsigned char *cw); + void SwapCW(unsigned char *cw); + virtual int ProcessBx(unsigned char *data, int len, int pos) { return -1; } + virtual int ProcessEx(unsigned char *data, int len, int pos) { return -1; } + virtual bool PostProcAU(int id, unsigned char *data) { return true; } + virtual int RunEmu(unsigned char *data, int len, unsigned short load, unsigned short run, unsigned short stop, unsigned short fetch, int fetch_len) { return -1; } + bool CanHandle(int Id) { return MATCH_ID(Id,id); } + bool HasFlags(int Flags) { return (flags&Flags)==Flags; } + void PrintCaps(int c); + }; + +// ---------------------------------------------------------------- + +class cN2Providers; + +class cN2ProvLink { +friend class cN2Providers; +private: + cN2ProvLink *next; +protected: + int id, flags; + // + virtual cN2Prov *Create(void)=0; + bool CanHandle(int Id) { return MATCH_ID(Id,id); } + bool HasFlags(int Flags) { return (flags&Flags)==Flags; } +public: + cN2ProvLink(int Id, int Flags); + virtual ~cN2ProvLink() {} + }; + +template class cN2ProvLinkReg : public cN2ProvLink { +public: + cN2ProvLinkReg(void):cN2ProvLink(ID,FLAGS) {} + virtual cN2Prov *Create(void) { return new PROV(id,flags); } + }; + +// ---------------------------------------------------------------- + +class cN2Providers { +friend class cN2ProvLink; +private: + static cN2ProvLink *first; + // + static void Register(cN2ProvLink *plink); +public: + static cN2Prov *GetProv(int Id, int Flags); + }; + +#endif -- 2.39.5