From: leslie Date: Sat, 9 Feb 2008 12:42:01 +0000 (+0100) Subject: nagra: generic map math X-Git-Tag: 0.9.1~121 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=29675107812df69aeac9a1f825eee4c6fdffa9d8;p=sasc-ng.git nagra: generic map math --- diff --git a/systems/nagra/nagra2.c b/systems/nagra/nagra2.c index fcfad1e..c9db747 100644 --- a/systems/nagra/nagra2.c +++ b/systems/nagra/nagra2.c @@ -229,22 +229,21 @@ bool cN2Emu::Init(int id, int romv) return initDone; } -// -- cMapCore ----------------------------------------------------------------- +// -- cMapMath ----------------------------------------------------------------- -cMapCore::cMapCore(void) +cMapMath::cMapMath(void) { - wordsize=4; last=1; - regs[0]=&J; regs[1]=&A; regs[2]=&B; regs[3]=&C; regs[4]=&D; + wordsize=DEF_WORDSIZE; } -void cMapCore::ModAdd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *d) +void cMapMath::ModAdd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BIGNUM *d) { BN_add(r,a,b); if(BN_cmp(r,d)>=0) BN_sub(r,r,d); BN_mask_bits(r,wordsize<<6); } -void cMapCore::ModSub(BIGNUM *r, BIGNUM *d, BIGNUM *b) +void cMapMath::ModSub(BIGNUM *r, BIGNUM *d, BIGNUM *b) { cBN p; BN_set_bit(p,wordsize<<6); @@ -252,7 +251,7 @@ void cMapCore::ModSub(BIGNUM *r, BIGNUM *d, BIGNUM *b) BN_mask_bits(r,wordsize<<6); } -void cMapCore::MakeJ0(BIGNUM *j, BIGNUM *d) +void cMapMath::MakeJ0(BIGNUM *j, BIGNUM *d) { #if OPENSSL_VERSION_NUMBER < 0x0090700fL #error BN_mod_inverse is probably buggy in your openssl version @@ -264,46 +263,81 @@ void cMapCore::MakeJ0(BIGNUM *j, BIGNUM *d) BN_mod_inverse(j,j,x,ctx); } -void cMapCore::MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b, BIGNUM *c, BIGNUM *d, BIGNUM *j, int words) +void cMapMath::MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b) +{ + MonMul(o,a,b,C,D,J,0); +} + +void cMapMath::MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b, int w) +{ + MonMul(o,a,b,C,D,J,w); +} + +void cMapMath::MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b, BIGNUM *c, BIGNUM *d, BIGNUM *j, int w) { - if(!words) words=wordsize; - BN_zero(s); - for(int i=0; i0) { BN_rshift(x,a,(i++)<<6); - BN_mask_bits(x,64); - BN_mul(x,x,b,ctx); - BN_add(s,s,x); + MonLoop(o,x,b,c,d,j); + } +} - BN_copy(x,s); - BN_mask_bits(x,64); - BN_mul(x,x,j,ctx); - if(i==words) { - BN_lshift(y,x,64); - BN_add(y,y,x); - // Low - BN_rshift(c,y,2); - BN_add(c,c,s); - BN_rshift(c,c,52); - BN_mask_bits(c,12); - } +void cMapMath::MonStart(int w) +{ + if(words<=0) { + words=w; + BN_zero(s); + } +} - BN_mask_bits(x,64); - BN_mul(x,x,d,ctx); - BN_add(s,s,x); - if(i==words) { - // High - BN_lshift(y,s,12); - BN_add(c,c,y); - BN_mask_bits(c,wordsize<<6); - } +// modifies a, but pointing a to x is allowed !! +void cMapMath::MonLoop(BIGNUM *o, BIGNUM *a, BIGNUM *b, BIGNUM *c, BIGNUM *d, BIGNUM *j) +{ + words--; + BN_mask_bits(a,64); + BN_mul(a,a,b,ctx); + BN_add(s,s,a); + + BN_copy(x,s); + BN_mask_bits(x,64); + BN_mul(x,x,j,ctx); + if(!words) { + BN_lshift(y,x,64); + BN_add(y,y,x); + // Low + BN_rshift(c,y,2); + BN_add(c,c,s); + BN_rshift(c,c,52); + BN_mask_bits(c,12); + } - BN_rshift(s,s,64); - if(BN_cmp(s,d)==1) { - BN_copy(x,s); - BN_sub(s,x,d); - } + BN_mask_bits(x,64); + BN_mul(x,x,d,ctx); + BN_add(s,s,x); + if(!words) { + // High + BN_lshift(y,s,12); + BN_add(c,c,y); + BN_mask_bits(c,wordsize<<6); } - BN_copy(o,s); + + BN_rshift(s,s,64); + if(BN_cmp(s,d)==1) { + BN_copy(x,s); + BN_sub(s,x,d); + } + + if(!words) BN_copy(o,s); +} + +// -- cMapCore ----------------------------------------------------------------- + +cMapCore::cMapCore(void) +{ + last=1; + regs[0]=&J; regs[1]=&A; regs[2]=&B; regs[3]=&C; regs[4]=&D; } void cMapCore::MonInit(int bits) @@ -319,7 +353,6 @@ void cMapCore::MonInit(int bits) void cMapCore::MonExpNeg(void) { if(BN_is_zero(D)) { BN_set_word(A,1); return; } - cBN e; BN_copy(e,D); BN_mask_bits(e,8); // check LSB unsigned int n=BN_get_word(e); @@ -471,7 +504,7 @@ bool cMapCore::DoMap(int f, unsigned char *data, int l) last=f-IMPORT_J; // fall through case IMPORT_LAST: - if(!cycles) cycles=656+160*l-6; // Even for 'J' cycles is dependent on 'l' + if(!cycles) cycles=656+160*l-6; regs[last]->GetLE(data,last>0?dl:8); break; @@ -496,9 +529,9 @@ bool cMapCore::DoMap(int f, unsigned char *data, int l) case SWAP_D: cycles=776+248*l1-6; last=f-SWAP_A+1; - x.GetLE(data,dl); + e.GetLE(data,dl); regs[last]->PutLE(data,dl); - BN_copy(*regs[last],x); + BN_copy(*regs[last],e); break; case CLEAR_A: diff --git a/systems/nagra/nagra2.h b/systems/nagra/nagra2.h index 3aa60bf..de0d729 100644 --- a/systems/nagra/nagra2.h +++ b/systems/nagra/nagra2.h @@ -50,6 +50,89 @@ extern char auxPassword[250]; // ---------------------------------------------------------------- +#define DEF_WORDSIZE 4 + +class cMapMath { +private: + cBN x, y, s; + int words; +protected: + int wordsize; + cBN A, B, C, D, J, I; + 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, int w); + void MonStart(int w); + void MonLoop(BIGNUM *o, BIGNUM *a, BIGNUM *b, BIGNUM *c, BIGNUM *d, BIGNUM *j); + // statefull + void MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b); + void MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b, int w); +public: + cMapMath(void); + }; + +// ---------------------------------------------------------------- + +#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 : public cMapMath { +private: + int last; + cBN e; + cBN *regs[5]; +protected: + unsigned int cycles; + cBN Px, Py, Pz,Qx, Qy, Qz; // 0x00,0x20,0x40,0x60,0x80,0x180 + cBN sA0, sC0, sE0, s100, s120, s140, s160; + // statefull + void MonInit(int bits=0); + 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); + // + int GetOpSize(int l); + bool DoMap(int f, unsigned char *data=0, int l=0); + unsigned int MapCycles() { return cycles; } +public: + cMapCore(void); + }; + +// ---------------------------------------------------------------- + class cN2Timer { private: int ctrl, divisor, cycles, remainder, latch; @@ -120,73 +203,6 @@ public: // ---------------------------------------------------------------- -#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: - unsigned int cycles; - 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, int words); - // statefull - void MonInit(int bits=0); - void MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b) { MonMul(o,a,b,C,D,J,0); } - void MonMul(BIGNUM *o, BIGNUM *a, BIGNUM *b, int words) { MonMul(o,a,b,C,D,J,words); } - 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); - // - int GetOpSize(int l); - bool DoMap(int f, unsigned char *data=0, int l=0); - unsigned int MapCycles() { return cycles; } -public: - cMapCore(void); - }; - -// ---------------------------------------------------------------- - #define N2FLAG_NONE 0 #define N2FLAG_MECM 1 #define N2FLAG_Bx 2