From: leslie Date: Mon, 22 Sep 2008 13:29:22 +0000 (+0800) Subject: nagra: update map code (from emunation) X-Git-Tag: 0.9.1~22 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=4066d0c7407e64b9cdb7fa7a8062c81330036d50;p=sasc-ng.git nagra: update map code (from emunation) --- diff --git a/systems/nagra/nagra2-0101.c b/systems/nagra/nagra2-0101.c index 7587dfc..79dd007 100644 --- a/systems/nagra/nagra2-0101.c +++ b/systems/nagra/nagra2-0101.c @@ -204,12 +204,8 @@ bool cMap0101::Map(int f, unsigned char *data, int l) l=GetOpSize(l); switch(f) { case 0x21: - AddMapCycles(288); - WS_START(1); - MakeJ0(J,D,C); - AddMapCycles(282); - BN_clear(C); - WS_END(); + AddMapCycles(169-6); + IMakeJ(); cycles=898; break; case 0x22: @@ -225,6 +221,18 @@ bool cMap0101::Map(int f, unsigned char *data, int l) cycles=1086 + j - ((j-2)%5) + 16923*val.quot - ((4*val.quot-2)%5); } break; + case 0x23: + AddMapCycles(169); + IMonInit0(); + break; + case 0x25: + AddMapCycles(254); + MakeJ0(B,D,C,wordsize<<6); + // valid for wordsize 1 and 2 + AddMapCycles(795*wordsize-492); + BN_zero(C); + cycles=49+800*wordsize; + break; case 0x29: { BN_add(B,B,C); @@ -263,10 +271,11 @@ bool cMap0101::Map(int f, unsigned char *data, int l) } BN_zero(J); break; + case 0x36: case 0x38: - AddMapCycles(232); - MonMul0(B,B,B,C,D,J,wordsize); - AddMapCycles(90); + AddMapCycles(230); + MonMul0(B,f==0x36?A:B,B,C,D,J,wordsize); + AddMapCycles(102); MonFin(B,D); break; case 0x3b: @@ -280,29 +289,15 @@ bool cMap0101::Map(int f, unsigned char *data, int l) if(l>wordsize) l=wordsize; cBN scalar; scalar.GetLE(data,l<<3); + AddMapCycles(441); if(BN_is_zero(scalar) || BN_num_bits(D)<=1) { - MakeJ0(J,D); + IMakeJ(); if(BN_num_bits(D)==1 || !BN_is_zero(scalar)) BN_zero(B); else BN_one(B); BN_one(A); } else { - WS_START(1); - MakeJ0(J,D,C); - AddMapCycles(860); - BN_zero(C); - WS_END(); - if(!BN_is_zero(D)) { - BN_zero(I); - BN_set_bit(I,68*wordsize); - BN_mod(B,I,D,ctx); - } - AddMapCycles(1390); - MonMul0(B,B,B,C,D,J,wordsize); - AddMapCycles(100); - MonFin(B,D); - for(int i=1; i<4; i++) MonMul(B,B,B); -// MonInit(); + IMonInit(); MonMul(B,A,B); MonExp(scalar); } @@ -666,8 +661,11 @@ bool cN2Prov0101::ProcessMap(int f) DoMap(f,tmp,l); break; case 0x21: + case 0x23: + case 0x25: case 0x30: case 0x31: + case 0x36: case 0x38: case 0x3a: case 0x43: diff --git a/systems/nagra/nagra2.c b/systems/nagra/nagra2.c index 7094559..2f01cb6 100644 --- a/systems/nagra/nagra2.c +++ b/systems/nagra/nagra2.c @@ -524,6 +524,48 @@ cMapCore::cMapCore(void) interruptible=false; } +void cMapCore::IMakeJ(void) +{ + AddMapCycles(19); + WS_START(1); + AddMapCycles(102); + MakeJ0(J,D,C); + AddMapCycles(303); + BN_zero(C); + AddMapCycles(34); + WS_END(); + AddMapCycles(10); +} + +void cMapCore::IMonInit0(void) +{ + AddMapCycles(132+(wordsize*8+3)/5*5); + if(BN_num_bits(D)>1) AddMapCycles(54); + if(!BN_is_zero(D)) { + AddMapCycles(54); + BN_zero(I); + BN_set_bit(I,68*wordsize); + BN_zero(B); + AddMapCycles(141+(wordsize*8+3)/5*5); + BN_set_bit(B,64*(wordsize-1)); + AddMapCycles(92+72*wordsize); + BN_mod(B,I,D,ctx); + AddMapCycles(639); + } + AddMapCycles(52); + for(int i=0; i<4; i++) { + MonMul0(B,B,B,C,D,J,0); + AddMapCycles(96+6*(i>0)); + MonFin(B,D); + } +} + +void cMapCore::IMonInit(void) +{ + IMakeJ(); + IMonInit0(); +} + void cMapCore::MonInit(int bits) { // Calculate J0 & H montgomery elements in J and B @@ -799,34 +841,15 @@ bool cMapCore::MapGeneric(int f, unsigned char *data, int l) last=3; C.Set(D,l1); cycles=462+(8*l1+3)/5*5-6; break; case 0x39: - WS_START(1); - MakeJ0(J,D,C); - AddMapCycles(860); - BN_zero(C); - WS_END(); - if(!BN_is_zero(D)) { - BN_zero(I); - BN_set_bit(I,68*wordsize); - BN_mod(B,I,D,ctx); - } - AddMapCycles(1370); - for(int i=0; i<4; i++) MonMul(B,B,B); -// MonInit(); - I.GetLE(data,wordsize<<3); - MonMul(B,I,B); - MonMul(B,A,B); - break; case 0x3a: - MakeJ0(J,D); - if(!BN_is_zero(D)) { - BN_zero(I); - BN_set_bit(I,68*wordsize); - BN_mod(B,I,D,ctx); + AddMapCycles(f==0x39?433:192); + IMonInit(); + if(f==0x39) { + I.GetLE(data,wordsize<<3); + MonMul(B,I,B); } - AddMapCycles(2000); - for(int i=0; i<4; i++) MonMul(B,B,B); -// MonInit(); - MonMul(B,A,B); + else + MonMul(B,A,B); MonMul(B,A,B); break; case 0x43: // init SHA1 diff --git a/systems/nagra/nagra2.h b/systems/nagra/nagra2.h index 5facdfe..e4fedba 100644 --- a/systems/nagra/nagra2.h +++ b/systems/nagra/nagra2.h @@ -156,6 +156,10 @@ protected: void MonInit(int bits=0); void MonExp(BIGNUM *scalar); void MonExpNeg(void); + // statefull, interruptible + void IMakeJ(void); + void IMonInit0(void); + void IMonInit(void); // ECC void DoubleP(int temp); void AddP(int temp);