Changeset 11429


Ignore:
Timestamp:
Jul 28, 2018, 9:31:01 PM (16 months ago)
Author:
felixka
Message:

Nagra:

  • prepare code for Nagra Merlin HD03, HD04 support
  • no ks included
Location:
trunk
Files:
11 added
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/Makefile

    r11412 r11429  
    315315SRC-$(CONFIG_READER_IRDETO) += reader-irdeto.c
    316316SRC-$(CONFIG_READER_NAGRA) += reader-nagra.c
     317SRC-$(CONFIG_READER_NAGRA_MERLIN) += cscrypt/mdc2.c
     318SRC-$(CONFIG_READER_NAGRA_MERLIN) += cscrypt/aescbc.c
     319SRC-$(CONFIG_READER_NAGRA_MERLIN) += cscrypt/fast_aes.c
     320SRC-$(CONFIG_READER_NAGRA_MERLIN) += cscrypt/sha256.c
     321SRC-$(CONFIG_READER_NAGRA_MERLIN) += reader-nagracak7.c
    317322SRC-$(CONFIG_READER_SECA) += reader-seca.c
    318323SRC-$(CONFIG_READER_TONGFANG) += reader-tongfang.c
  • trunk/config.h

    r11427 r11429  
    4343
    4444#define READER_NAGRA 1
     45//#define READER_NAGRA_MERLIN 1
    4546#define READER_IRDETO 1
    4647#define READER_CONAX 1
  • trunk/config.sh

    r11428 r11429  
    33addons="WEBIF WEBIF_LIVELOG WEBIF_JQUERY TOUCH WITH_SSL HAVE_DVBAPI WITH_NEUTRINO READ_SDT_CHARSETS IRDETO_GUESSING CS_ANTICASC WITH_DEBUG MODULE_MONITOR WITH_LB CS_CACHEEX CW_CYCLE_CHECK LCDSUPPORT LEDSUPPORT CLOCKFIX IPV6SUPPORT"
    44protocols="MODULE_CAMD33 MODULE_CAMD35 MODULE_CAMD35_TCP MODULE_NEWCAMD MODULE_CCCAM MODULE_CCCSHARE MODULE_GBOX MODULE_RADEGAST MODULE_SCAM MODULE_SERIAL MODULE_CONSTCW MODULE_PANDORA MODULE_GHTTP"
    5 readers="READER_NAGRA READER_IRDETO READER_CONAX READER_CRYPTOWORKS READER_SECA READER_VIACCESS READER_VIDEOGUARD READER_DRE READER_TONGFANG READER_BULCRYPT READER_GRIFFIN READER_DGCRYPT"
     5readers="READER_NAGRA READER_NAGRA_MERLIN READER_IRDETO READER_CONAX READER_CRYPTOWORKS READER_SECA READER_VIACCESS READER_VIDEOGUARD READER_DRE READER_TONGFANG READER_BULCRYPT READER_GRIFFIN READER_DGCRYPT"
    66card_readers="CARDREADER_PHOENIX CARDREADER_INTERNAL CARDREADER_SC8IN1 CARDREADER_MP35 CARDREADER_SMARGO CARDREADER_DB2COM CARDREADER_STAPI CARDREADER_STAPI5 CARDREADER_STINGER CARDREADER_DRECAS"
    77
     
    4141CONFIG_WITH_CARDREADER=y
    4242CONFIG_READER_NAGRA=y
     43# CONFIG_READER_NAGRA_MERLIN=n
    4344CONFIG_READER_IRDETO=y
    4445CONFIG_READER_CONAX=y
     
    347348    not_have_flag USE_LIBCRYPTO && enabled MODULE_CCCAM && echo "CONFIG_LIB_SHA1=y" || echo "# CONFIG_LIB_SHA1=n"
    348349    enabled_any READER_DRE MODULE_SCAM READER_VIACCESS && echo "CONFIG_LIB_DES=y" || echo "# CONFIG_LIB_DES=n"
    349     enabled_any MODULE_CCCAM READER_NAGRA READER_SECA && echo "CONFIG_LIB_IDEA=y" || echo "# CONFIG_LIB_IDEA=n"
    350     not_have_flag USE_LIBCRYPTO && enabled_any READER_CONAX READER_CRYPTOWORKS READER_NAGRA && echo "CONFIG_LIB_BIGNUM=y" || echo "# CONFIG_LIB_BIGNUM=n"
     350    enabled_any MODULE_CCCAM READER_NAGRA READER_NAGRA_MERLIN READER_SECA && echo "CONFIG_LIB_IDEA=y" || echo "# CONFIG_LIB_IDEA=n"
     351    not_have_flag USE_LIBCRYPTO && enabled_any READER_CONAX READER_CRYPTOWORKS READER_NAGRA READER_NAGRA_MERLIN && echo "CONFIG_LIB_BIGNUM=y" || echo "# CONFIG_LIB_BIGNUM=n"
    351352}
    352353
     
    496497    ${DIALOG} --checklist "\nChoose readers (CA systems):\n " $height $width $listheight \
    497498        READER_NAGRA        "Nagravision"       $(check_test "READER_NAGRA") \
     499        READER_NAGRA_MERLIN     "Nagravision_cak7"      $(check_test "READER_NAGRA_MERLIN") \
    498500        READER_IRDETO       "Irdeto"            $(check_test "READER_IRDETO") \
    499501        READER_CONAX        "Conax"             $(check_test "READER_CONAX") \
  • trunk/cscrypt/des.c

    r11413 r11429  
    138138#include "des.h"
    139139
    140 static const uint8_t weak_keys[16][8] = {
    141         // weak keys
    142         {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
    143         {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
    144         {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
    145         {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
    146         // semi-weak keys
    147         {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
    148         {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
    149         {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
    150         {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
    151         {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
    152         {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
    153         {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
    154         {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
    155         {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
    156         {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
    157         {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
    158         {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}};
    159 
    160 static const uint8_t odd_parity[] ={
    161         1,  1,  2,  2,  4,  4,  7,  7,  8,  8, 11, 11, 13, 13, 14, 14,
    162         16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
    163         32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
    164         49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
    165         64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
    166         81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
    167         97, 97, 98, 98, 100,100,103,103,104,104,107,107,109,109,110,110,
    168         112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
    169         128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
    170         145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
    171         161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
    172         176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
    173         193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
    174         208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
    175         224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
    176         241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254};
     140static const uint8_t weak_keys[16][8] =
     141{
     142    // weak keys
     143    {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
     144    {0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE},
     145    {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F},
     146    {0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0,0xE0},
     147    // semi-weak keys
     148    {0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE},
     149    {0xFE,0x01,0xFE,0x01,0xFE,0x01,0xFE,0x01},
     150    {0x1F,0xE0,0x1F,0xE0,0x0E,0xF1,0x0E,0xF1},
     151    {0xE0,0x1F,0xE0,0x1F,0xF1,0x0E,0xF1,0x0E},
     152    {0x01,0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1},
     153    {0xE0,0x01,0xE0,0x01,0xF1,0x01,0xF1,0x01},
     154    {0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E,0xFE},
     155    {0xFE,0x1F,0xFE,0x1F,0xFE,0x0E,0xFE,0x0E},
     156    {0x01,0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E},
     157    {0x1F,0x01,0x1F,0x01,0x0E,0x01,0x0E,0x01},
     158    {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
     159    {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}
     160};
     161
     162static const uint8_t odd_parity[] =
     163{
     164    1,  1,  2,  2,  4,  4,  7,  7,  8,  8, 11, 11, 13, 13, 14, 14,
     165    16, 16, 19, 19, 21, 21, 22, 22, 25, 25, 26, 26, 28, 28, 31, 31,
     166    32, 32, 35, 35, 37, 37, 38, 38, 41, 41, 42, 42, 44, 44, 47, 47,
     167    49, 49, 50, 50, 52, 52, 55, 55, 56, 56, 59, 59, 61, 61, 62, 62,
     168    64, 64, 67, 67, 69, 69, 70, 70, 73, 73, 74, 74, 76, 76, 79, 79,
     169    81, 81, 82, 82, 84, 84, 87, 87, 88, 88, 91, 91, 93, 93, 94, 94,
     170    97, 97, 98, 98, 100,100,103,103,104,104,107,107,109,109,110,110,
     171    112,112,115,115,117,117,118,118,121,121,122,122,124,124,127,127,
     172    128,128,131,131,133,133,134,134,137,137,138,138,140,140,143,143,
     173    145,145,146,146,148,148,151,151,152,152,155,155,157,157,158,158,
     174    161,161,162,162,164,164,167,167,168,168,171,171,173,173,174,174,
     175    176,176,179,179,181,181,182,182,185,185,186,186,188,188,191,191,
     176    193,193,194,194,196,196,199,199,200,200,203,203,205,205,206,206,
     177    208,208,211,211,213,213,214,214,217,217,218,218,220,220,223,223,
     178    224,224,227,227,229,229,230,230,233,233,234,234,236,236,239,239,
     179    241,241,242,242,244,244,247,247,248,248,251,251,253,253,254,254
     180};
    177181
    178182static const uint8_t shifts2[16] = {0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
    179183
    180 static const uint32_t des_skb[8][64] = {
    181         {
     184static const uint32_t des_skb[8][64] =
     185{
     186    {
    182187        0x00000000,0x00000010,0x20000000,0x20000010,
    183188        0x00010000,0x00010010,0x20010000,0x20010010,
     
    196201        0x00080820,0x00080830,0x20080820,0x20080830,
    197202        0x00090820,0x00090830,0x20090820,0x20090830,
    198         },{
     203    },{
    199204
    200205        0x00000000,0x02000000,0x00002000,0x02002000,
     
    214219        0x10000404,0x12000404,0x10002404,0x12002404,
    215220        0x10200404,0x12200404,0x10202404,0x12202404,
    216         },{
     221    },{
    217222
    218223        0x00000000,0x00000001,0x00040000,0x00040001,
     
    232237        0x08000202,0x08000203,0x08040202,0x08040203,
    233238        0x09000202,0x09000203,0x09040202,0x09040203,
    234         },{
     239    },{
    235240
    236241        0x00000000,0x00100000,0x00000100,0x00100100,
     
    250255        0x04021000,0x04121000,0x04021100,0x04121100,
    251256        0x04021008,0x04121008,0x04021108,0x04121108,
    252         },{
     257    },{
    253258
    254259        0x00000000,0x10000000,0x00010000,0x10010000,
     
    268273        0x20101000,0x30101000,0x20111000,0x30111000,
    269274        0x20101004,0x30101004,0x20111004,0x30111004,
    270         },{
     275    },{
    271276
    272277        0x00000000,0x08000000,0x00000008,0x08000008,
     
    286291        0x02020001,0x0A020001,0x02020009,0x0A020009,
    287292        0x02020401,0x0A020401,0x02020409,0x0A020409,
    288         },{
     293    },{
    289294
    290295        0x00000000,0x00000100,0x00080000,0x00080100,
     
    304309        0x00200210,0x00200310,0x00280210,0x00280310,
    305310        0x01200210,0x01200310,0x01280210,0x01280310,
    306         },{
     311    },{
    307312
    308313        0x00000000,0x04000000,0x00040000,0x04040000,
     
    322327        0x00002820,0x04002820,0x00042820,0x04042820,
    323328        0x00002822,0x04002822,0x00042822,0x04042822,
    324 }};
    325 
    326 static const uint32_t des_SPtrans[8][64] = {
     329    }
     330};
     331
     332static const uint32_t des_SPtrans[8][64] =
     333{
    327334    {
    328335        0x00820200, 0x00020000, 0x80800000, 0x80820200,
     
    342349        0x00020000, 0x00800000, 0x80800200, 0x00820200,
    343350        0x80000000, 0x80820000, 0x00000200, 0x80020200,
    344         },{
     351    },{
    345352
    346353        0x10042004, 0x00000000, 0x00042000, 0x10040000,
     
    360367        0x10040004, 0x00040000, 0x00002004, 0x10002000,
    361368        0x10002004, 0x00000004, 0x10040000, 0x00042000,
    362         },{
     369    },{
    363370
    364371        0x41000000, 0x01010040, 0x00000040, 0x41000040,
     
    378385        0x01000040, 0x00000000, 0x41010000, 0x40000040,
    379386        0x41000000, 0x40010040, 0x00000040, 0x01010000,
    380         },{
     387    },{
    381388
    382389        0x00100402, 0x04000400, 0x00000002, 0x04100402,
     
    396403        0x00100002, 0x00100400, 0x04100000, 0x04000402,
    397404        0x00000402, 0x04000000, 0x04000002, 0x04100400,
    398         },{
     405    },{
    399406
    400407        0x02000000, 0x00004000, 0x00000100, 0x02004108,
     
    414421        0x00004100, 0x02004008, 0x02000100, 0x00000108,
    415422        0x00000008, 0x00004108, 0x02004000, 0x02000008,
    416         },{
     423    },{
    417424
    418425        0x20000010, 0x00080010, 0x00000000, 0x20080800,
     
    432439        0x00000800, 0x20000010, 0x20000810, 0x20080800,
    433440        0x20080000, 0x00000810, 0x00000010, 0x20080010,
    434         },{
     441    },{
    435442
    436443        0x00001000, 0x00000080, 0x00400080, 0x00400001,
     
    450457        0x00401080, 0x00000001, 0x00001001, 0x00401081,
    451458        0x00400001, 0x00401080, 0x00401000, 0x00001001,
    452         },{
     459    },{
    453460
    454461        0x08200020, 0x08208000, 0x00008020, 0x00000000,
     
    468475        0x00000020, 0x08208000, 0x00208020, 0x00000000,
    469476        0x08000000, 0x08200020, 0x00008000, 0x00208020,
    470     }};
     477    }
     478};
    471479
    472480static const int32_t DES_KEY_SZ=8;
     
    475483{
    476484    int32_t i;
    477    
     485
    478486    for (i=0; i < DES_KEY_SZ; i++)
    479487        key[i]=odd_parity[key[i]&0xff];
     
    483491{
    484492    int32_t i;
    485    
    486     for (i=0; i < DES_KEY_SZ; i++) {
    487             if (key[i] != odd_parity[key[i]&0xff])
     493
     494    for (i=0; i < DES_KEY_SZ; i++)
     495    {
     496        if (key[i] != odd_parity[key[i]&0xff])
    488497            return 0;
    489498    }
     
    494503{
    495504    int32_t i, j;
    496    
    497     for (i=0; i < 16; i++) {
    498         for(j=0;j < DES_KEY_SZ; j++) {
    499             if (weak_keys[i][j] != key[j]) {
     505
     506    for (i=0; i < 16; i++)
     507    {
     508        for(j=0; j < DES_KEY_SZ; j++)
     509        {
     510            if (weak_keys[i][j] != key[j])
     511            {
    500512                // not weak
    501513                continue;
     
    508520}
    509521
    510 static uint32_t Get32bits(const uint8_t* key, int32_t kindex) {
     522static uint32_t Get32bits(const uint8_t* key, int32_t kindex)
     523{
    511524    return(((key[kindex+3]&0xff)<<24) + ((key[kindex+2]&0xff)<<16) + ((key[kindex+1]&0xff)<<8) + (key[kindex]&0xff));
    512525}
     
    518531    int32_t kIndex;
    519532    int32_t i;
    520 
    521     //if (!check_parity(key)) {
    522     //  return 0;
    523     //}
    524 
    525     //if (des_is_weak_key(key)) {
    526     //  return 0;
    527     //}
    528 
    529533    inIndex=0;
    530534    kIndex=0;
    531 
    532535    c =Get32bits(key, inIndex);
    533536    d =Get32bits(key, inIndex+4);
    534 
    535537    t=(((d>>4)^c)&0x0f0f0f0f);
    536538    c^=t;
    537539    d^=(t<<4);
    538 
    539540    t=(((c<<(16-(-2)))^c)&0xcccc0000);
    540541    c=c^t^(t>>(16-(-2)));
    541 
    542542    t=((d<<(16-(-2)))^d)&0xcccc0000;
    543543    d=d^t^(t>>(16-(-2)));
    544 
    545544    t=((d>>1)^c)&0x55555555;
    546545    c^=t;
    547546    d^=(t<<1);
    548 
    549547    t=((c>>8)^d)&0x00ff00ff;
    550548    d^=t;
    551549    c^=(t<<8);
    552 
    553550    t=((d>>1)^c)&0x55555555;
    554551    c^=t;
    555552    d^=(t<<1);
    556 
    557553    d=  (((d&0x000000ff)<<16)| (d&0x0000ff00) |((d&0x00ff0000)>>16)|((c&0xf0000000)>>4));
    558554    c&=0x0fffffff;
    559 
    560     for (i=0; i < 16; i++) {
    561         if (shifts2[i]) {
     555    for (i=0; i < 16; i++)
     556    {
     557        if (shifts2[i])
     558        {
    562559            c=((c>>2)|(c<<26));
    563560            d=((d>>2)|(d<<26));
    564         } else {
     561        }
     562        else
     563        {
    565564            c=((c>>1)|(c<<27));
    566565            d=((d>>1)|(d<<27));
    567566        }
    568 
    569567        c&=0x0fffffff;
    570568        d&=0x0fffffff;
    571 
    572569        s=  des_skb[0][ (c    )&0x3f                ]|
    573570            des_skb[1][((c>> 6)&0x03)|((c>> 7)&0x3c)]|
    574571            des_skb[2][((c>>13)&0x0f)|((c>>14)&0x30)]|
    575572            des_skb[3][((c>>20)&0x01)|((c>>21)&0x06) |
    576                       ((c>>22)&0x38)];
     573                          ((c>>22)&0x38)];
    577574        t=  des_skb[4][ (d    )&0x3f                ]|
    578575            des_skb[5][((d>> 7)&0x03)|((d>> 8)&0x3c)]|
    579576            des_skb[6][ (d>>15)&0x3f                ]|
    580577            des_skb[7][((d>>21)&0x0f)|((d>>22)&0x30)];
    581 
    582578        schedule[kIndex++]=((t<<16)|(s&0x0000ffff))&0xffffffff;
    583 
    584579        s=((s>>16)|(t&0xffff0000));
    585 
    586580        s=(s<<4)|(s>>28);
    587 
    588581        schedule[kIndex++]=s&0xffffffff;
    589582    }
    590    
    591583    return 1;
    592584}
    593585
    594 static uint32_t _lrotr(uint32_t i) {
     586static uint32_t _lrotr(uint32_t i)
     587{
    595588    return((i>>4) | ((i&0xff)<<28));
    596589}
     
    631624    r&=0xffffffff;
    632625
    633     if (do_encrypt) {
    634         for (i=0; i < 32; i+=8) {
    635             { u=(r^ks[i+0 ]); t=r^ks[i+0+1]; t=(_lrotr(t)); l^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f]; };
    636             { u=(l^ks[i+2 ]); t=l^ks[i+2+1]; t=(_lrotr(t)); r^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f]; };
    637             { u=(r^ks[i+4 ]); t=r^ks[i+4+1]; t=(_lrotr(t)); l^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f]; };
    638             { u=(l^ks[i+6 ]); t=l^ks[i+6+1]; t=(_lrotr(t)); r^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f]; };
     626    if (do_encrypt)
     627    {
     628        for (i=0; i < 32; i+=8)
     629        {
     630            {
     631                u=(r^ks[i+0 ]);
     632                t=r^ks[i+0+1];
     633                t=(_lrotr(t));
     634                l^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f];
     635            };
     636            {
     637                u=(l^ks[i+2 ]);
     638                t=l^ks[i+2+1];
     639                t=(_lrotr(t));
     640                r^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f];
     641            };
     642            {
     643                u=(r^ks[i+4 ]);
     644                t=r^ks[i+4+1];
     645                t=(_lrotr(t));
     646                l^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f];
     647            };
     648            {
     649                u=(l^ks[i+6 ]);
     650                t=l^ks[i+6+1];
     651                t=(_lrotr(t));
     652                r^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f];
     653            };
    639654        }
    640     } else {
    641         for (i=30; i > 0; i-=8) {
    642             { u=(r^ks[i-0 ]); t=r^ks[i-0+1]; t=(_lrotr(t)); l^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f]; };
    643             { u=(l^ks[i-2 ]); t=l^ks[i-2+1]; t=(_lrotr(t)); r^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f]; };
    644             { u=(r^ks[i-4 ]); t=r^ks[i-4+1]; t=(_lrotr(t)); l^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f]; };
    645             { u=(l^ks[i-6 ]); t=l^ks[i-6+1]; t=(_lrotr(t)); r^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f]; };
     655    }
     656    else
     657    {
     658        for (i=30; i > 0; i-=8)
     659        {
     660            {
     661                u=(r^ks[i-0 ]);
     662                t=r^ks[i-0+1];
     663                t=(_lrotr(t));
     664                l^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f];
     665            };
     666            {
     667                u=(l^ks[i-2 ]);
     668                t=l^ks[i-2+1];
     669                t=(_lrotr(t));
     670                r^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f];
     671            };
     672            {
     673                u=(r^ks[i-4 ]);
     674                t=r^ks[i-4+1];
     675                t=(_lrotr(t));
     676                l^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f];
     677            };
     678            {
     679                u=(l^ks[i-6 ]);
     680                t=l^ks[i-6+1];
     681                t=(_lrotr(t));
     682                r^= des_SPtrans[1][(t )&0x3f]| des_SPtrans[3][(t>> 8)&0x3f]| des_SPtrans[5][(t>>16)&0x3f]| des_SPtrans[7][(t>>24)&0x3f]| des_SPtrans[0][(u )&0x3f]| des_SPtrans[2][(u>> 8)&0x3f]| des_SPtrans[4][(u>>16)&0x3f]| des_SPtrans[6][(u>>24)&0x3f];
     683            };
    646684        }
    647685    }
     
    714752    {
    715753    case 16:
    716         for(i = 8; i < 16; ++i){
     754        for(i = 8; i < 16; ++i)
     755        {
    717756            data[i] = v1[i] ^ v2[i];
    718         }               /* fallthrough */
     757        }
    719758    case 8:
    720         for(i = 4; i < 8; ++i){
     759        for(i = 4; i < 8; ++i)
     760        {
    721761            data[i] = v1[i] ^ v2[i];
    722         }               /* fallthrough */
     762        }
    723763    case 4:
    724         for(i = 0; i < 4; ++i){
     764        for(i = 0; i < 4; ++i)
     765        {
    725766            data[i] = v1[i] ^ v2[i];
    726         }               /* fallthrough */
     767        }
    727768        break;
    728769    default:
    729         while(len--) { *data++ = *v1++ ^ *v2++; }
     770        while(len--)
     771        {
     772            *data++ = *v1++ ^ *v2++;
     773        }
    730774        break;
    731775    }
     
    735779{
    736780    uint32_t schedule[32];
    737     int32_t i; 
    738    
     781    int32_t i;
     782
    739783    des_set_key(key, schedule);
    740    
     784
    741785    len&=~7;
    742    
    743     for(i=0; i<len; i+=8) {
     786
     787    for(i=0; i<len; i+=8)
     788    {
    744789        des(&data[i], schedule, 1);
    745790    }
     
    749794{
    750795    uint32_t schedule[32];
    751     int32_t i; 
    752    
     796    int32_t i;
     797
    753798    des_set_key(key, schedule);
    754    
     799
    755800    len&=~7;
    756801
    757     for(i=0; i<len; i+=8) {
     802    for(i=0; i<len; i+=8)
     803    {
    758804        des(&data[i], schedule, 0);
    759805    }
     
    762808void des_cbc_encrypt(uint8_t* data, const uint8_t* iv, const uint8_t* key, int32_t len)
    763809{
    764     const uint8_t *civ = iv; 
     810    const uint8_t *civ = iv;
    765811    uint32_t schedule[32];
    766     int32_t i; 
     812    int32_t i;
    767813
    768814    des_set_key(key, schedule);
    769    
     815
    770816    len&=~7;
    771817
    772     for(i=0; i<len; i+=8) {
     818    for(i=0; i<len; i+=8)
     819    {
    773820        xxor(&data[i],8,&data[i],civ);
    774821        civ=&data[i];
     
    788835
    789836    memcpy(civ[n],iv,8);
    790     for(i=0; i<len; i+=8,data+=8,n^=1) {
     837    for(i=0; i<len; i+=8,data+=8,n^=1)
     838    {
    791839        memcpy(civ[1-n],data,8);
    792         des(data, schedule ,0);
     840        des(data, schedule,0);
    793841        xxor(data,8,data,civ[n]);
    794842    }
     
    797845void des_ede2_cbc_encrypt(uint8_t* data, const uint8_t* iv, const uint8_t* key1, const uint8_t* key2, int32_t len)
    798846{
    799     const uint8_t *civ = iv; 
     847    const uint8_t *civ = iv;
    800848    uint32_t schedule1[32], schedule2[32];
    801     int32_t i; 
     849    int32_t i;
    802850
    803851    des_set_key(key1, schedule1);
     
    806854    len&=~7;
    807855
    808     for(i=0; i<len; i+=8) {
     856    for(i=0; i<len; i+=8)
     857    {
    809858        xxor(&data[i],8,&data[i],civ);
    810859        civ=&data[i];
    811        
     860
    812861        des(&data[i], schedule1, 1);
    813862        des(&data[i], schedule2, 0);
     
    824873    des_set_key(key1, schedule1);
    825874    des_set_key(key2, schedule2);
    826    
     875
    827876    len&=~7;
    828877
    829878    memcpy(civ[n],iv,8);
    830     for(i=0; i<len; i+=8,data+=8,n^=1) {
     879    for(i=0; i<len; i+=8,data+=8,n^=1)
     880    {
    831881        memcpy(civ[1-n],data,8);
    832882        des(data, schedule1, 0);
    833883        des(data, schedule2, 1);
    834         des(data, schedule1, 0);       
     884        des(data, schedule1, 0);
    835885        xxor(data,8,data,civ[n]);
    836886    }
    837887}
     888
     889void _3DES(uint8_t *data, uint8_t *key)
     890{
     891    uint32_t ks1[32], ks2[32];
     892    des_set_key(key, ks1);
     893    des_set_key(key+8, ks2);
     894    des(data, ks1, 0);
     895    des(data, ks2, 1);
     896    des(data, ks1, 0);
     897}
  • trunk/cscrypt/des.h

    r10882 r11429  
    3535    void des_ede2_cbc_decrypt(uint8_t* data, const uint8_t* iv, const uint8_t* key1, const uint8_t* key2, int32_t len);
    3636
     37    void _3DES(uint8_t *data, uint8_t *key);
     38
    3739#endif
  • trunk/csctapi/icc_async.c

    r11413 r11429  
    1 /*
    2     icc_async.c
    3     Asynchronous ICC's handling functions
    4 
    5     This file is part of the Unix driver for Towitoko smartcard readers
    6     Copyright (C) 2000 2001 Carlos Prados <cprados@yahoo.com>
    7 
    8     This version is modified by doz21 to work in a special manner ;)
    9 
    10     This library is free software; you can redistribute it and/or
    11     modify it under the terms of the GNU Lesser General Public
    12     License as published by the Free Software Foundation; either
    13     version 2 of the License, or (at your option) any later version.
    14 
    15     This library is distributed in the hope that it will be useful,
    16     but WITHOUT ANY WARRANTY; without even the implied warranty of
    17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    18     Lesser General Public License for more details.
    19 
    20     You should have received a copy of the GNU Lesser General Public
    21     License along with this library; if not, write to the Free Software
    22     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 */
    24 
    251#include "../globals.h"
    262#ifdef WITH_CARDREADER
     
    328#include "ifd_phoenix.h"
    339#include "../oscam-time.h"
    34 
    35 #define OK 0
    36 #define ERROR 1
     10#include "../cardlist.h"
     11#include "../oscam-work.h"
     12#ifdef READER_NAGRA_MERLIN
     13#include "../cscrypt/fast_aes.h"
     14#include "../cscrypt/sha256.h"
     15#include "../cscrypt/aescbc.h"
     16#include "../cscrypt/mdc2.h"
     17#include "../cscrypt/bn.h"
     18#include "../cscrypt/idea.h"
     19#endif
     20
     21#define OK  0
     22#define ERROR   1
    3723
    3824// Default T0/T14 settings
    39 #define DEFAULT_WI      10
     25#define DEFAULT_WI  10
    4026// Default T1 settings
    41 #define DEFAULT_IFSC    32
    42 #define MAX_IFSC        251  /* Cannot send > 255 buffer */
    43 #define DEFAULT_CWI     13
    44 #define DEFAULT_BWI     4
    45 #define EDC_LRC             0
    46 
    47 #define PPS_MAX_LENGTH  6
    48 #define PPS_HAS_PPS1(block)       ((block[1] & 0x10) == 0x10)
    49 #define PPS_HAS_PPS2(block)       ((block[1] & 0x20) == 0x20)
    50 #define PPS_HAS_PPS3(block)       ((block[1] & 0x40) == 0x40)
    51 
    52 /*
    53  * Not exported functions declaration
    54  */
    55 static uint16_t tempfi; // used to capture FI and use it for rounding or not
     27#define DEFAULT_IFSC    32
     28#define MAX_IFSC    251
     29#define DEFAULT_CWI 13
     30#define DEFAULT_BWI 4
     31#define EDC_LRC     0
     32
     33#define PPS_MAX_LENGTH  6
     34#define PPS_HAS_PPS1(block) ((block[1] & 0x10) == 0x10)
     35#define PPS_HAS_PPS2(block) ((block[1] & 0x20) == 0x20)
     36#define PPS_HAS_PPS3(block) ((block[1] & 0x40) == 0x40)
     37
     38static uint16_t tempfi; // used to capture FI and use it for rounding or not
    5639static void ICC_Async_InvertBuffer(struct s_reader *reader, uint32_t size, unsigned char *buffer);
    5740static int32_t Parse_ATR(struct s_reader *reader, ATR *atr, uint16_t deprecated);
     
    6346static int32_t SetRightParity(struct s_reader *reader);
    6447
    65 /*
    66  * Exported functions definition
    67  */
     48#ifdef READER_NAGRA_MERLIN
     49static uint32_t crctab[256] =
     50{
     51    0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
     52    0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
     53    0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
     54    0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
     55    0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
     56    0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
     57    0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
     58    0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
     59    0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
     60    0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
     61    0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
     62    0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
     63    0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
     64    0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
     65    0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
     66    0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
     67    0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
     68    0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
     69    0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
     70    0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
     71    0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
     72    0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
     73    0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
     74    0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
     75    0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
     76    0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
     77    0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
     78    0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
     79    0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
     80    0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
     81    0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
     82    0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4,
     83};
     84
     85uint32_t calc_ccitt32(uint8_t *buf, uint8_t count)
     86{
     87#define M1 0xffffffff
     88#define M2 0xffffff00
     89    uint32_t crc = M1;
     90    while(count--)
     91    {
     92        crc=((crc<<8)&M2)^crctab[((crc>>24)&0xff)^*buf++];
     93    }
     94    return(crc);
     95}
     96
     97static void calculate_cas7_vars(struct s_reader *reader, const ATR *atr)
     98{
     99    uint8_t aes_key[32];
     100    const uint8_t aes_iv[] = { 0x4E, 0x61, 0x67, 0x72, 0x61, 0x63, 0x61, 0x72, 0x64, 0x28, 0x63, 0x29, 0x32, 0x30, 0x30, 0x36 }; // Nagracard(c)2006
     101    mbedtls_sha256_context ctx_sha256;
     102    mbedtls_sha256_init(&ctx_sha256);
     103    mbedtls_sha256_starts(&ctx_sha256, 0);
     104    mbedtls_sha256_update(&ctx_sha256, atr->hb, atr->hbn);
     105    mbedtls_sha256_finish(&ctx_sha256, aes_key);
     106    mbedtls_sha256_free(&ctx_sha256);
     107    memcpy(reader->cas7_aes_key,aes_key,32);
     108    memcpy(reader->cas7_aes_iv,aes_iv,16);
     109}
     110
     111void calculate_cas7_cmd(struct s_reader *reader, uint8_t *cmdin,uint8_t cmdlen,uint8_t *cmdout)
     112{
     113    uint32_t crc = calc_ccitt32(cmdin+4, cmdlen-4);
     114    i2b_buf(4, crc, cmdin);
     115    AesCtx ctx;
     116    AesCtxIni(&ctx, reader->cas7_aes_iv, &reader->cas7_aes_key[16], KEY128, CBC);
     117    AesEncrypt(&ctx, cmdin, cmdout, cmdlen);
     118}
     119
     120void do_cas7_cmd(struct s_reader *reader,unsigned char *cta_res, uint16_t *p_cta_lr,uint8_t *data,uint8_t inlen,uint8_t resplen)
     121{
     122    reader->cas7_seq++;
     123    uint8_t req[inlen+5+1]; // +head+len
     124    memset(req,0x00,sizeof(req));
     125    // head
     126    req[0]=0x80;
     127    req[1]=0xCA;
     128    // len
     129    req[4]=inlen;
     130    req[sizeof(req)-1]=resplen;
     131    data[4]=(reader->cas7_seq>>16)&0xFF;
     132    data[5]=(reader->cas7_seq>>8)&0xFF;
     133    data[6]=(reader->cas7_seq)&0xFF;
     134    calculate_cas7_cmd(reader,data,inlen,&req[5]);
     135    if(!ICC_Async_CardWrite(reader, req, sizeof(req), cta_res, p_cta_lr, 0x21))
     136    {
     137        AesCtx ctx;
     138        AesCtxIni(&ctx, reader->cas7_aes_iv, &reader->cas7_aes_key[16], KEY128, CBC);
     139        AesDecrypt(&ctx, cta_res, cta_res,  *p_cta_lr-2);
     140    }
     141    else
     142    {
     143        *p_cta_lr=0;
     144    }
     145}
     146
     147static void calculate_changerom_cmd(struct s_reader *reader, const ATR *atr, uint8_t *cmd)
     148{
     149    uint8_t cmd_data[] = { 0xCC, 0xCC, 0xCC, 0xCC, 0x00, 0x00, 0x01, 0x01, 0x01, 0x95, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC };
     150    calculate_cas7_vars(reader,atr);
     151    calculate_cas7_cmd(reader,cmd_data,sizeof(cmd_data),cmd);
     152}
     153#endif
    68154
    69155int32_t ICC_Async_Device_Init(struct s_reader *reader)
     
    76162    int32_t ret = crdr_ops->reader_init(reader);
    77163    if(ret == OK)
    78         { rdr_log_dbg(reader, D_IFD, "Device %s succesfully opened", reader->device); }
     164    {
     165        rdr_log_dbg(reader, D_IFD, "Device %s succesfully opened", reader->device);
     166    }
    79167    else
    80168    {
    81         if(reader->typ != R_SC8in1) { NULLFREE(reader->crdr_data); }
     169        if(reader->typ != R_SC8in1)
     170        {
     171            NULLFREE(reader->crdr_data);
     172        }
    82173        rdr_log_dbg(reader, D_IFD, "ERROR: Can't open %s device", reader->device);
    83174    }
     
    103194{
    104195    const struct s_cardreader *crdr_ops = reader->crdr;
    105     if (!crdr_ops) return ERROR;
    106     if (reader->typ == R_SMART && reader->smartdev_found >= 4) {
     196    if (!crdr_ops)
     197    {
     198        return ERROR;
     199    }
     200    if (reader->typ == R_SMART && reader->smartdev_found >= 4)
     201    {
    107202        reader->statuscnt = reader->statuscnt + 1;
    108         if (reader->statuscnt == 6) {
     203        if (reader->statuscnt == 6)
     204        {
     205            int32_t in = 0;
     206            call(crdr_ops->get_status(reader, &in));
     207            if(in)
     208            {
     209                reader->modemstat = 1;
     210                *card = 1;
     211                reader->statuscnt = 0;
     212            }
     213            else
     214            {
     215                reader->modemstat = 0;
     216                *card = 0;
     217                reader->statuscnt = 0;
     218            }
     219            return OK;
     220        }
     221        else
     222        {
     223            *card = reader->modemstat;
     224            return OK;
     225        }
     226    }
     227    else
     228    {
    109229        int32_t in = 0;
    110230        call(crdr_ops->get_status(reader, &in));
    111231        if(in)
    112             {reader->modemstat = 1; *card = 1; reader->statuscnt = 0;}
     232        {
     233            *card = 1;
     234        }
    113235        else
    114             {reader->modemstat = 0; *card = 0; reader->statuscnt = 0;}
     236        {
     237            *card = 0;
     238        }
    115239        return OK;
    116         } else {
    117         *card = reader->modemstat;
    118         return OK;
    119         }
    120     }
    121     else {
    122         int32_t in = 0;
    123         call(crdr_ops->get_status(reader, &in));
    124         if(in)
    125             { *card = 1;}
    126         else
    127             { *card = 0;}
    128         return OK;
    129240    }
    130241
     
    134245{
    135246    rdr_log_dbg(reader, D_IFD, "Activating card");
    136 
    137247    const struct s_cardreader *crdr_ops = reader->crdr;
    138248    if (!crdr_ops) return ERROR;
    139 
    140249    reader->current_baudrate = DEFAULT_BAUDRATE;
    141250    if(reader->atr[0] != 0 && !reader->ins7e11_fast_reset)
     
    149258        call(crdr_ops->activate(reader, atr));
    150259        if(crdr_ops->skip_extra_atr_parsing)
    151             { return OK; }
     260        {
     261            return OK;
     262        }
    152263    }
    153264
     
    155266    uint32_t atr_size;
    156267    ATR_GetRaw(atr, atrarr, &atr_size);
    157     char tmp[atr_size * 3 + 1];
    158     rdr_log(reader, "ATR: %s", cs_hexdump(1, atrarr, atr_size, tmp, sizeof(tmp)));
     268    char tmp1[atr_size * 3 + 1];
     269    memcpy(current.atr, cs_hexdump(1, atrarr, atr_size, tmp1, sizeof(tmp1)), atr_size * 3 - 1);
     270    current.atr[atr_size * 3 - 1] = '\0';
     271    rdr_log(reader, "ATR: %s", current.atr);
    159272    memcpy(reader->card_atr, atrarr, atr_size);
    160273    reader->card_atr_length = atr_size;
    161 
    162     /* Get ICC reader->convention */
    163     if(ATR_GetConvention(atr, &(reader->convention)) != ATR_OK)
    164     {
    165         rdr_log(reader, "ERROR: Could not read reader->convention");
    166         reader->convention = 0;
    167         reader->protocol_type = 0;
     274    findatr(reader);
     275    if ( current.found == 1 )
     276    {
     277        rdr_log(reader, "%s recognized", current.providername);
     278    }
     279
     280    if(current.badcard == 1 )  /* set badcard in cardlist.h */
     281    {
     282        current.badcard = 0;
     283        rdr_log(reader, "Bad Card -> quick restart");
     284        add_job(reader->client, ACTION_READER_RESTART, NULL, 0);
    168285        return ERROR;
    169286    }
    170 
    171     reader->protocol_type = ATR_PROTOCOL_TYPE_T0;
    172 
    173     // Parse_ATR and InitCard need to be included in lock because they change parity of serial port
    174     if(crdr_ops->lock)
    175         { crdr_ops->lock(reader); }
    176 
    177     int32_t ret = Parse_ATR(reader, atr, deprecated);
    178 
    179     if(crdr_ops->unlock)
    180         { crdr_ops->unlock(reader); }
    181 
    182     if(ret)
    183         { rdr_log(reader, "ERROR: Parse_ATR returned error"); }
    184     if(ret)
    185         { return ERROR; }
    186     rdr_log_dbg(reader, D_IFD, "Card succesfully activated");
    187 
     287    else
     288    {
     289
     290        /* Get ICC reader->convention */
     291        if(ATR_GetConvention(atr, &(reader->convention)) != ATR_OK)
     292        {
     293            rdr_log(reader, "ERROR: Could not read reader->convention");
     294            reader->convention = 0;
     295            reader->protocol_type = 0;
     296            return ERROR;
     297        }
     298        reader->protocol_type = ATR_PROTOCOL_TYPE_T0;
     299        if(crdr_ops->lock)
     300        {
     301            crdr_ops->lock(reader);
     302        }
     303        int32_t ret = Parse_ATR(reader, atr, deprecated);
     304        if(crdr_ops->unlock)
     305        {
     306            crdr_ops->unlock(reader);
     307        }
     308        if(ret)
     309        {
     310            rdr_log(reader, "ERROR: Parse_ATR returned error");
     311        }
     312        if(ret)
     313        {
     314            return ERROR;
     315        }
     316    }
     317   
     318#ifdef READER_NAGRA_MERLIN
     319
     320    char tmp[atr_size * 3 + 1];
     321
     322    if(current.ishd04 == 1)
     323    {
     324        rdr_log_dbg(reader, D_READER, "HD04 merlin handling");
     325        calculate_cas7_vars(reader, atr);
     326    }
     327    else if(current.ishd03 == 1 || current.ishd04 == 1)   // Switch ROM
     328    {
     329
     330        static uint8_t changerom_handshake[] = { 0x80, 0xCA, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 };
     331
     332        calculate_changerom_cmd(reader, atr, &changerom_handshake[5]);
     333        memset(reader->rom, 0, 15);
     334        unsigned char cta_res[CTA_RES_LEN];
     335        memset(cta_res, 0, CTA_RES_LEN);
     336        uint16_t cta_lr;
     337        uint8_t block_nad = 0;
     338
     339        changerom_handshake[4] = 0x11; // 0x11: length of data we will send
     340        uint8_t cta_res1_ok = 0x61;
     341        uint8_t cta_res2_ok = 0x10;
     342
     343        if(reader->protocol_type != ATR_PROTOCOL_TYPE_T0)
     344        {
     345            block_nad = 0x21;
     346            changerom_handshake[0] = 0x80; // fix for mipsel router
     347            changerom_handshake[4] = 0x10; // 0x10: length of data we will send
     348            cta_res1_ok = 0x90;
     349            cta_res2_ok = 0x00;
     350        }
     351
     352        //rdr_log_dbg(reader, D_READER, "try to init nagra layer");
     353         rdr_log(reader,"try to init nagra layer");
     354 
     355        //rdr_log_dump_dbg(reader, D_READER, changerom_handshake, sizeof(changerom_handshake), "changerom_handshake data:");
     356
     357        if(!ICC_Async_CardWrite(reader, changerom_handshake, sizeof(changerom_handshake), cta_res, &cta_lr, block_nad))
     358        {
     359            if(cta_res[cta_lr-2] == cta_res1_ok && cta_res[cta_lr-1] == cta_res2_ok)
     360            {
     361                //rdr_log_dbg(reader, D_READER, "switch to nagra layer OK");
     362                rdr_log(reader, "switch to nagra layer OK");
     363                uint8_t cmd_data[cta_lr+2] ;
     364                AES128_CBC_decrypt_buffer(cmd_data,cta_res, (cta_lr-2), &reader->cas7_aes_key[16], reader->cas7_aes_iv);
     365                memset(atr, 0, 1);
     366                call(crdr_ops->activate(reader, atr)); //try to read the atr of this layer
     367                ATR_GetRaw(atr, atrarr, &atr_size);
     368                //rdr_log_dbg(reader, D_READER, "Nagra layer ATR: %s", cs_hexdump(1, atrarr, atr_size, tmp, sizeof(tmp)));
     369                rdr_log(reader,"Nagra layer ATR: %s", cs_hexdump(1, atrarr, atr_size, tmp, sizeof(tmp)));
     370                calculate_cas7_vars(reader, atr);
     371                if(crdr_ops->lock)
     372                {
     373                    crdr_ops->lock(reader);
     374                }
     375                Parse_ATR(reader, atr, deprecated);
     376                if(crdr_ops->unlock)
     377                {
     378                    crdr_ops->unlock(reader);
     379                }
     380            }
     381            else
     382            {
     383                //rdr_log_dbg(reader, D_READER, "Switch to nagra layer failed!");
     384                rdr_log(reader,"Switch to nagra layer failed!");
     385                return ERROR;
     386            }
     387        }
     388        else
     389        {
     390            //rdr_log_dbg(reader, D_READER, "Switch to nagra layer command failed!");
     391            rdr_log(reader,"Switch to nagra layer command failed!");
     392            return ERROR;
     393        }
     394        memcpy(reader->card_atr, atrarr, atr_size);
     395        reader->card_atr_length = atr_size;
     396        memcpy(reader->rom, atr->hb, (atr->hbn>15)?15:atr->hbn);// get historical bytes from atr
     397    }
     398#endif 
     399    rdr_log_dbg(reader, D_READER, "Card succesfully activated");
     400   
    188401    return OK;
    189 }
    190 
    191 int32_t ICC_Async_CardWrite(struct s_reader *reader, unsigned char *command, uint16_t command_len, unsigned char *rsp, uint16_t *lr)
     402} 
     403
     404int32_t ICC_Async_CardWrite(struct s_reader *reader, unsigned char *command, uint16_t command_len, unsigned char *rsp, uint16_t *lr, uint8_t block_nad)
    192405{
    193406    const struct s_cardreader *crdr_ops = reader->crdr;
    194407    if (!crdr_ops) return ERROR;
    195408    int32_t ret;
    196 
    197409    *lr = 0; //will be returned in case of error
    198410    if(crdr_ops->card_write)
     
    202414        return OK;
    203415    }
    204 
    205416    if(crdr_ops->lock)
    206         { crdr_ops->lock(reader); }
    207 
     417    {
     418        crdr_ops->lock(reader);
     419    }
    208420    int32_t try = 1;
    209421    uint16_t type = 0;
    210422    do
    211423    {
    212         if(try > 1){
    213             rdr_log(reader, "Warning: needed try nr %i, next ECM has some delay", try);
    214         }
    215 
    216         switch(reader->protocol_type){
    217 
     424        switch(reader->protocol_type)
     425        {
     426            if(try > 1)
     427                    rdr_log(reader, "Warning: needed try nr %i, next ECM has some delay", try);
    218428        case ATR_PROTOCOL_TYPE_T0:
    219429            ret = Protocol_T0_Command(reader, command, command_len, rsp, lr);
     
    221431            break;
    222432        case ATR_PROTOCOL_TYPE_T1:
    223             ret = Protocol_T1_Command(reader, command, command_len, rsp, lr);
     433            ret = Protocol_T1_Command(reader, command, command_len, rsp, lr, block_nad);
    224434            type = 1;
    225435            if(ret != OK && !crdr_ops->skip_t1_command_retries)
     
    228438                rdr_log(reader, "Resync error: readtimeouts %d/%d (max/min) us, writetimeouts %d/%d (max/min) us", reader->maxreadtimeout, reader->minreadtimeout, reader->maxwritetimeout, reader->minwritetimeout);
    229439                unsigned char resync[] = { 0x21, 0xC0, 0x00, 0xE1 };
    230                 ret = Protocol_T1_Command(reader, resync, sizeof(resync), rsp, lr);
     440                ret = Protocol_T1_Command(reader, resync, sizeof(resync), rsp, lr, 0);
    231441                if(ret == OK)
    232442                {
    233                     //reader->ifsc = DEFAULT_IFSC; //tryfix cardtimeouts: ifsc is setup at card init, on resync it should not return to default_ifsc
     443                    //reader->ifsc = DEFAULT_IFSC;
     444                    //tryfix cardtimeouts: ifsc is setup at card init, on resync it should not return to default_ifsc
    234445                    rdr_log(reader, "T1 Resync command succesfull ifsc = %i", reader->ifsc);
    235446                    ret = ERROR;
     
    241452                    ICC_Async_Activate(reader, &atr, reader->deprecated);
    242453                    if(crdr_ops->unlock)
    243                         { crdr_ops->unlock(reader); }
     454                    {
     455                        crdr_ops->unlock(reader);
     456                    }
    244457                    return ERROR;
    245458                }
     
    257470        try++;
    258471    }
    259     while((try < 3) && (ret != OK));    //always do one retry when failing
    260 
     472    while((try < 3) && (ret != OK)); //always do one retry when failing
    261473    if(crdr_ops->unlock)
    262         { crdr_ops->unlock(reader); }
    263 
     474    {
     475        crdr_ops->unlock(reader);
     476    }
    264477    if(ret)
    265478    {
     
    267480        return ERROR;
    268481    }
    269 
    270482    rdr_log_dump_dbg(reader, D_READER, rsp, *lr, "Answer from cardreader:");
    271483    return OK;
     
    274486int32_t ICC_Async_GetTimings(struct s_reader *reader, uint32_t wait_etu)
    275487{
    276     int32_t timeout = ETU_to_us(reader, wait_etu);
     488    int32_t timeout = ETU_to_us(reader, wait_etu)*2;
    277489    rdr_log_dbg(reader, D_IFD, "Setting timeout to %i ETU (%d us)", wait_etu, timeout);
    278490    return timeout;
     
    284496    if (!crdr_ops) return ERROR;
    285497
    286     if(expectedlen)  //expectedlen = 0 means expected len is unknown
    287         { rdr_log_dbg(reader, D_IFD, "Transmit size %d bytes, expected len %d bytes, delay %d us, timeout=%d us", size, expectedlen, delay, timeout); }
     498    if(expectedlen)
     499    {
     500        rdr_log_dbg(reader, D_IFD, "Transmit size %d bytes, expected len %d bytes, delay %d us, timeout=%d us", size, expectedlen, delay, timeout);
     501    }
    288502    else
    289         { rdr_log_dbg(reader, D_IFD, "Transmit size %d bytes, delay %d us, timeout=%d us", size, delay, timeout); }
    290     rdr_log_dump_dbg(reader, D_IFD, data, size, "Transmit:");
     503    {
     504        rdr_log_dbg(reader, D_IFD, "Transmit size %d bytes, delay %d us, timeout=%d us", size, delay, timeout);
     505    }
     506    //rdr_log_dump_dbg(reader, D_IFD, data, size, "Transmit:");
     507    rdr_log_dump_dbg(reader, D_READER, data, size, "Transmit:");
    291508    unsigned char *sent = data;
    292 
    293509    if(reader->convention == ATR_CONVENTION_INVERSE && crdr_ops->need_inverse)
    294510    {
    295511        ICC_Async_InvertBuffer(reader, size, sent);
    296512    }
    297 
    298513    call(crdr_ops->transmit(reader, sent, size, expectedlen, delay, timeout));
    299514    rdr_log_dbg(reader, D_IFD, "Transmit succesful");
     
    303518        ICC_Async_InvertBuffer(reader, size, sent);
    304519    }
    305 
    306520    return OK;
    307521}
     
    313527
    314528    rdr_log_dbg(reader, D_IFD, "Receive size %d bytes, delay %d us, timeout=%d us", size, delay, timeout);
     529    rdr_log_dump_dbg(reader, D_IFD, data, size, "RECEIVED:");
    315530    call(crdr_ops->receive(reader, data, size, delay, timeout));
    316531    rdr_log_dbg(reader, D_IFD, "Receive succesful");
    317532    if(reader->convention == ATR_CONVENTION_INVERSE && crdr_ops->need_inverse == 1)
     533    {
    318534        ICC_Async_InvertBuffer(reader, size, data);
     535    }
    319536    return OK;
    320537}
     
    324541    const struct s_cardreader *crdr_ops = reader->crdr;
    325542    if (!crdr_ops) return ERROR;
    326 
    327543    rdr_log_dbg(reader, D_IFD, "Closing device %s", reader->device);
    328544    call(crdr_ops->close(reader));
    329545    if(reader->typ != R_SC8in1)
    330         {
    331            NULLFREE(reader->crdr_data);
    332            NULLFREE(reader->csystem_data);
    333         }
     546    {
     547        NULLFREE(reader->crdr_data);
     548        NULLFREE(reader->csystem_data);
     549    }
    334550    rdr_log_dbg(reader, D_IFD, "Device %s succesfully closed", reader->device);
    335551    return OK;
     
    352568}
    353569
    354 /*static uint32_t ICC_Async_GetClockRate_NewSmart(int32_t cardmhz)
    355 {
    356     return (cardmhz * 10000L);
    357 
    358 }*/
    359 
    360570static uint32_t ICC_Async_GetClockRate(int32_t cardmhz)
    361571{
     
    365575    case 358:
    366576        return (372L * 9600L);
    367     case 368: 
     577    case 368:
    368578        return (384L * 9600L);
    369579    default:
     
    374584static int32_t ICC_Async_GetPLL_Divider(struct s_reader *reader)
    375585{
    376     if(reader->divider != 0) { return reader->divider; }
    377 
     586    if(reader->divider != 0)
     587    {
     588        return reader->divider;
     589    }
    378590    if(reader->cardmhz != 8300)  /* Check dreambox is not DM7025 */
    379591    {
     
    381593        divider = ((float) reader->cardmhz) / ((float) reader->mhz);
    382594        if (tempfi == 9) reader->divider = (int32_t) divider; // some card's runs only when slightly oveclocked like HD02
    383         else {
    384         reader->divider = (int32_t) divider;
    385         if(divider > reader->divider) { reader->divider++; }  /* to prevent over clocking, ceil (round up) the divider */
     595        else
     596        {
     597            reader->divider = (int32_t) divider;
     598            if(divider > reader->divider)
     599            {
     600                reader->divider++;    /* to prevent over clocking, ceil (round up) the divider */
     601            }
    386602        }
    387603        rdr_log_dbg(reader, D_DEVICE, "PLL maxmhz = %.2f, wanted mhz = %.2f, divider used = %d, actualcardclock=%.2f", (float) reader->cardmhz / 100, (float) reader->mhz / 100,
    388                        reader->divider, (float) reader->cardmhz / reader->divider / 100);
     604                    reader->divider, (float) reader->cardmhz / reader->divider / 100);
    389605        reader->mhz = reader->cardmhz / reader->divider;
    390606    }
    391     else /* STB is DM7025 */
    392     {
    393         int32_t i, dm7025_clock_freq[] = {518, 461, 395, 360, 319, 296, 267, 244, 230, 212, 197},
    394                                          dm7025_PLL_setting[] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, t_cardmhz = reader->mhz;
    395 
     607    else
     608    {
     609        int32_t i, dm7025_clock_freq[] = {518, 461, 395, 360, 319, 296, 267, 244, 230, 212, 197}, dm7025_PLL_setting[] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, t_cardmhz = reader->mhz;
    396610        for(i = 0; i < 11; i++)
    397             if(t_cardmhz >= dm7025_clock_freq[i]) { break; }
    398 
    399         if(i > 10) { i = 10; }
    400 
     611            if(t_cardmhz >= dm7025_clock_freq[i])
     612            {
     613                break;
     614            }
     615        if(i > 10)
     616        {
     617            i = 10;
     618        }
    401619        reader->mhz = dm7025_clock_freq[i];
    402620        reader->divider = dm7025_PLL_setting[i]; /*Nicer way of codeing is: reader->divider = i + 6;*/
    403 
    404         rdr_log_dbg(reader, D_DEVICE, "DM7025 PLL maxmhz = %.2f, wanted mhz = %.2f, PLL setting used = %d, actualcardclock=%.2f", (float) reader->cardmhz / 100, (float) t_cardmhz / 100,
    405                        reader->divider, (float) reader->mhz / 100);
    406     }
    407 
     621        rdr_log_dbg(reader, D_DEVICE, "DM7025 PLL maxmhz = %.2f, wanted mhz = %.2f, PLL setting used = %d, actualcardclock=%.2f",
     622                    (float) reader->cardmhz / 100,
     623                    (float) t_cardmhz / 100,
     624                    reader->divider,
     625                    (float) reader->mhz / 100);
     626    }
    408627    return reader->divider;
    409628}
     
    415634    rdr_log_dbg(reader, D_IFD, "%s: size=%u buf[0]=%02x", __func__, size, buffer[0]);
    416635    for(i = 0; i < size; i++)
    417         { buffer[i] = ~(INVERT_BYTE(buffer[i])); }
     636    {
     637        buffer[i] = ~(INVERT_BYTE(buffer[i]));
     638    }
    418639}
    419640
     
    431652    if(ATR_GetInterfaceByte(atr, numprot - 1, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK)
    432653        if((tx & 0xF0) == 0)
    433             { numprot--; }
     654        {
     655            numprot--;
     656        }
    434657    int32_t i, point;
    435658    char txt[50];
    436659    bool OffersT[3]; //T14 stored as T2
    437660    for(i = 0; i <= 2; i++)
    438         { OffersT[i] = 0; }
     661    {
     662        OffersT[i] = 0;
     663    }
    439664    for(i = 1; i <= numprot; i++)
    440665    {
     
    462687            snprintf((char *)txt + point, sizeof(txt) - point, "(T%i)", tx);
    463688            if(tx == 14)
    464                 { OffersT[2] = 1; }
     689            {
     690                OffersT[2] = 1;
     691            }
    465692            else
    466                 { OffersT[tx] = 1; }
     693            {
     694                OffersT[tx] = 1;
     695            }
    467696        }
    468697        else
     
    477706    for(i = 0; i <= 2; i++)
    478707        if(OffersT[i])
    479             { numprottype ++; }
    480     rdr_log_dbg(reader, D_ATR, "%i protocol types detected. Historical bytes: %s",
    481                    numprottype, cs_hexdump(1, atr->hb, atr->hbn, tmp, sizeof(tmp)));
     708        {
     709            numprottype ++;
     710        }
     711    rdr_log_dbg(reader, D_ATR, "%i protocol types detected. Historical bytes: %s", numprottype, cs_hexdump(1, atr->hb, atr->hbn, tmp, sizeof(tmp)));
    482712
    483713    ATR_GetParameter(atr, ATR_PARAMETER_N, &(N));
     
    492722        {
    493723            unsigned char TA1;
    494             if(ATR_GetInterfaceByte(atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK)
     724            if(ATR_GetInterfaceByte(atr, 1, ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK)
    495725            {
    496726                FI = TA1 >> 4;
     
    510740        }
    511741        uint32_t F = atr_f_table[FI];
    512         rdr_log_dbg(reader, D_ATR, "Specific mode: T%i, F=%d, D=%d, N=%d",
    513                        reader->protocol_type, F, D, N);
     742        rdr_log_dbg(reader, D_ATR, "Specific mode: T%i, F=%d, D=%d, N=%d", reader->protocol_type, F, D, N);
    514743    }
    515744    else   //negotiable mode
     
    526755            req[1] = 0x10 | reader->protocol_type; //PTS0 always flags PTS1 to be sent always
    527756            if(ATR_GetInterfaceByte(atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK)      //PTS1
    528                 { req[2] = 0x11; } //defaults FI and DI to 1
     757            {
     758                req[2] = 0x11;    //defaults FI and DI to 1
     759            }
    529760            uint32_t len = 0;
    530761            call(SetRightParity(reader));
     
    537768                uint32_t F = atr_f_table[FI];
    538769                PPS_success = 1;
    539                 rdr_log_dbg(reader, D_ATR, "PTS Succesfull, selected protocol: T%i, F=%d, D=%d, N=%d",
    540                                reader->protocol_type, F, D, N);
     770                rdr_log_dbg(reader, D_ATR, "PTS Succesfull, selected protocol: T%i, F=%d, D=%d, N=%d", reader->protocol_type, F, D, N);
    541771            }
    542772            else
    543                 { rdr_log_dump_dbg(reader, D_ATR, req, len, "PTS Failure, response:"); }
     773            {
     774                rdr_log_dump_dbg(reader, D_ATR, req, len, "PTS Failure, response:");
     775            }
    544776        }
    545777
     
    548780        {
    549781            unsigned char TA1;
    550             if(ATR_GetInterfaceByte(atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK)
     782            if(ATR_GetInterfaceByte(atr, 1, ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK)
    551783            {
    552784                FI = TA1 >> 4;
     
    561793            {
    562794                if((D == 32) || (D == 12) || (D == 20))  //those values were RFU in old table
    563                     { D = 0; } // viaccess cards that fail PTS need this
     795                {
     796                    D = 0;    // viaccess cards that fail PTS need this
     797                }
    564798            }
    565799            uint32_t F = atr_f_table[FI];
    566800            rdr_log_dbg(reader, D_ATR, "No PTS %s, selected protocol T%i, F=%d, D=%d, N=%d",
    567                            NeedsPTS ? "happened" : "needed", reader->protocol_type, F, D, N);
     801                        NeedsPTS ? "happened" : "needed", reader->protocol_type, F, D, N);
    568802        }
    569803    }//end negotiable mode
     
    581815        rdr_log(reader, "Warning: D=0 is invalid, forcing D=%d", D);
    582816    }
    583     rdr_log(reader, "Init card protocol T%i, FI=%d, F=%d, D=%d, N=%d", reader->protocol_type, FI, F, D, N);
     817    rdr_log_dbg(reader, D_ATR, "Init card protocol T%i, FI=%d, F=%d, D=%d, N=%d", reader->protocol_type, FI, F, D, N);
    584818    if(deprecated == 0)
    585         { return InitCard(reader, atr, FI, D, N, deprecated); }
     819    {
     820        return InitCard(reader, atr, FI, D, N, deprecated);
     821    }
    586822    else
    587         { return InitCard(reader, atr, ATR_DEFAULT_FI, ATR_DEFAULT_D, N, deprecated); }
     823    {
     824        return InitCard(reader, atr, ATR_DEFAULT_FI, ATR_DEFAULT_D, N, deprecated);
     825    }
    588826}
    589827
     
    601839    params[len_request - 1] = PPS_GetPCK(params, len_request - 1);
    602840    rdr_log_dbg(reader, D_IFD, "PTS: Sending request: %s",
    603                    cs_hexdump(1, params, len_request, tmp, sizeof(tmp)));
     841                cs_hexdump(1, params, len_request, tmp, sizeof(tmp)));
    604842
    605843    if(crdr_ops->set_protocol)
     
    618856
    619857    rdr_log_dbg(reader, D_IFD, "PTS: Receiving confirm: %s",
    620                    cs_hexdump(1, confirm, len_confirm, tmp, sizeof(tmp)));
     858                cs_hexdump(1, confirm, len_confirm, tmp, sizeof(tmp)));
    621859    if((len_request != len_confirm) || (memcmp(params, confirm, len_request)))
    622         { ret = ERROR; }
     860    {
     861        ret = ERROR;
     862    }
    623863    else
    624         { ret = OK; }
     864    {
     865        ret = OK;
     866    }
    625867
    626868    /* Copy PPS handshake */
     
    635877
    636878    if(PPS_HAS_PPS1(block))
    637         { length++; }
     879    {
     880        length++;
     881    }
    638882
    639883    if(PPS_HAS_PPS2(block))
    640         { length++; }
     884    {
     885        length++;
     886    }
    641887
    642888    if(PPS_HAS_PPS3(block))
    643         { length++; }
     889    {
     890        length++;
     891    }
    644892
    645893    return length;
     
    648896static uint32_t ETU_to_us(struct s_reader *reader, uint32_t ETU)
    649897{
    650    
    651     return (uint32_t)((double) ETU * reader->worketu);  // in us
     898
     899    return (uint32_t)((double) ETU * reader->worketu*2);  // in us
    652900}
    653901
     
    673921    uint16_t parity = PARITY_EVEN;
    674922    if(reader->convention == ATR_CONVENTION_INVERSE)
    675         { parity = PARITY_ODD; }
     923    {
     924        parity = PARITY_ODD;
     925    }
    676926    else if(reader->protocol_type == ATR_PROTOCOL_TYPE_T14)
    677         { parity = PARITY_NONE; }
     927    {
     928        parity = PARITY_NONE;
     929    }
    678930
    679931    call(ICC_Async_SetParity(reader, parity));
    680932
    681933    if(crdr_ops->flush && reader->crdr_flush)
    682         { IO_Serial_Flush(reader); }
     934    {
     935        IO_Serial_Flush(reader);
     936    }
    683937
    684938    return OK;
     
    695949    //set the amps and the volts according to ATR
    696950    if(ATR_GetParameter(atr, ATR_PARAMETER_I, &I) != ATR_OK)
    697         { I = 0; }
     951    {
     952        I = 0;
     953    }
    698954
    699955    tempfi = FI;
     
    703959    {
    704960        if(reader->autospeed == 1)  //no overclocking
    705             { reader->mhz = atr_fs_table[FI] / 10000; } //we are going to clock the card to this nominal frequency
     961        {
     962            reader->mhz = atr_fs_table[FI] / 10000;    //we are going to clock the card to this nominal frequency
     963        }
    706964
    707965        if(reader->cardmhz > 2000 && reader->autospeed == 1)  // -1 replaced by autospeed parameter is magic number pll internal reader set cardmhz according to optimal atr speed
    708966        {
    709967            reader->mhz = atr_fs_table[FI] / 10000 ;
    710             if((!strncmp(boxtype_get(), "vu", 2 ))||(boxtype_is("ini-8000am"))){reader->mhz = 450;}
     968            if((!strncmp(boxtype_get(), "vu", 2 ))||(boxtype_is("ini-8000am")))
     969            {
     970                reader->mhz = 450;
     971            }
    711972        }
    712973    }
     
    737998        }
    738999    }
    739     if(reader->cardmhz > 2000 && reader->typ == R_INTERNAL) { F = reader->mhz; }  // for PLL based internal readers
    740     else {
    741     if (reader->typ == R_SMART || is_smargo_reader(reader))
    742     {
    743         if (reader->autospeed == 1) {
    744         uint32_t Fsmart = atr_fs_table[FI];
    745         reader->mhz = Fsmart/10000;
    746         if(reader->mhz >= 1600) { reader->mhz = 1600; }
    747         else if(reader->mhz >= 1200) { reader->mhz = 1200; }
    748         else if(reader->mhz >= 961)  { reader->mhz =  961; }
    749         else if(reader->mhz >= 800)  { reader->mhz =  800; }
    750         else if(reader->mhz >= 686)  { reader->mhz =  686; }
    751         else if(reader->mhz >= 600)  { reader->mhz =  600; }
    752         else if(reader->mhz >= 534)  { reader->mhz =  534; }
    753         else if(reader->mhz >= 480)  { reader->mhz =  534; }
    754         else if(reader->mhz >= 436)  { reader->mhz =  436; }
    755         else if(reader->mhz >= 400)  { reader->mhz =  400; }
    756         else if(reader->mhz >= 369)  { reader->mhz =  369; }
    757         else if(reader->mhz >= 357)  { reader->mhz =  369; } // 357 not suported by smartreader
    758         else if(reader->mhz >= 343)  { reader->mhz =  343; }
    759         else
    760         { reader->mhz =  320; }
    761         }
    762     }   
    763     F = reader->mhz; } // all other readers
    764         reader->worketu = (double)((double)(1 / (double)D) * ((double)Fi / (double)((double)F / 100)));
    765     rdr_log(reader, "Calculated work ETU is %.2f us reader mhz = %u", reader->worketu, reader->mhz);
     1000    if(reader->cardmhz > 2000 && reader->typ == R_INTERNAL)
     1001    {
     1002        F = reader->mhz;    // for PLL based internal readers
     1003    }
     1004    else
     1005    {
     1006        if (reader->typ == R_SMART || is_smargo_reader(reader))
     1007        {
     1008            if (reader->autospeed == 1)
     1009            {
     1010                uint32_t Fsmart = atr_fs_table[FI];
     1011                reader->mhz = Fsmart/10000;
     1012                if(reader->mhz >= 1600)
     1013                {
     1014                    reader->mhz = 1600;
     1015                }
     1016                else if(reader->mhz >= 1200)
     1017                {
     1018                    reader->mhz = 1200;
     1019                }
     1020                else if(reader->mhz >= 961)
     1021                {
     1022                    reader->mhz =  961;
     1023                }
     1024                else if(reader->mhz >= 800)
     1025                {
     1026                    reader->mhz =  800;
     1027                }
     1028                else if(reader->mhz >= 686)
     1029                {
     1030                    reader->mhz =  686;
     1031                }
     1032                else if(reader->mhz >= 600)
     1033                {
     1034                    reader->mhz =  600;
     1035                }
     1036                else if(reader->mhz >= 534)
     1037                {
     1038                    reader->mhz =  534;
     1039                }
     1040                else if(reader->mhz >= 480)
     1041                {
     1042                    reader->mhz =  534;
     1043                }
     1044                else if(reader->mhz >= 436)
     1045                {
     1046                    reader->mhz =  436;
     1047                }
     1048                else if(reader->mhz >= 400)
     1049                {
     1050                    reader->mhz =  400;
     1051                }
     1052                else if(reader->mhz >= 369)
     1053                {
     1054                    reader->mhz =  369;
     1055                }
     1056                else if(reader->mhz >= 357)
     1057                {
     1058                    reader->mhz =  369;    // 357 not suported by smartreader
     1059                }
     1060                else if(reader->mhz >= 343)
     1061                {
     1062                    reader->mhz =  343;
     1063                }
     1064                else
     1065                {
     1066                    reader->mhz =  320;
     1067                }
     1068            }
     1069        }
     1070        F = reader->mhz;
     1071    } // all other readers
     1072    reader->worketu = (double)((double)(1 / (double)D) * ((double)Fi / (double)((double)F / 100)));
     1073    rdr_log_dbg(reader, D_ATR, "Calculated work ETU is %.2f us reader mhz = %u", reader->worketu, reader->mhz);
    7661074
    7671075    //set timings according to ATR
     
    7821090
    7831091        WWT = (uint32_t) 960 * D * wi; //in work ETU
    784 
    785         /*unsigned char tmpatr[7]; // this is card atr of conax with pairingecmrotation, they need some additional WWT time but this isnt in those ATRs
    786         tmpatr[0] = 0x3B;
    787         tmpatr[1] = 0x24;
    788         tmpatr[2] = 0x00;
    789         tmpatr[3] = 0x30;
    790         tmpatr[4] = 0x42;
    791         tmpatr[5] = 0x30;
    792         tmpatr[6] = 0x30;
    793        
    794 
    795     //  WWT = (uint32_t) 960 * D * wi; //in work ETU
    796         if (!memcmp(reader->card_atr, tmpatr, sizeof(tmpatr))){ // check for conax pairingecmrotation card atr.
    797             WWT = WWT * 600; // if found add some additional WWT time
    798         }*/
    7991092        GT = 2; // standard guardtime
    8001093        GT += 1; // start bit
     
    8031096
    8041097        if(N != 255)  //add extra Guard Time by ATR
    805             { EGT += N; }  // T0 protocol, if TC1 = 255 then dont add extra guardtime
     1098        {
     1099            EGT += N;    // T0 protocol, if TC1 = 255 then dont add extra guardtime
     1100        }
    8061101        reader->CWT = 0; // T0 protocol doesnt have char waiting time (used to detect errors within 1 single block of data)
    8071102        reader->BWT = 0; // T0 protocol doesnt have block waiting time (used to detect unresponsive card, this is max time for starting a block answer)
    8081103
    809        
    8101104        rdr_log_dbg(reader, D_ATR, "Protocol: T=%i, WWT=%u, Clockrate=%u", reader->protocol_type, WWT, F * 10000);
    811 
    8121105        reader->read_timeout = WWT; // Work waiting time used in T0 (max time to signal unresponsive card!)
    8131106        reader->char_delay = GT + EGT; // Character delay is used on T0
    814         rdr_log_dbg(reader, D_ATR, "Setting timings: timeout=%u ETU, block_delay=%u ETU, char_delay=%u ETU",
    815                        reader->read_timeout, reader->block_delay, reader->char_delay);
     1107        rdr_log_dbg(reader, D_ATR, "Setting timings: timeout=%u ETU, block_delay=%u ETU, char_delay=%u ETU", reader->read_timeout, reader->block_delay, reader->char_delay);
    8161108        break;
    8171109    }
     
    8221114        // Set IFSC
    8231115        if(ATR_GetInterfaceByte(atr, 3, ATR_INTERFACE_BYTE_TA, &ta) == ATR_NOT_FOUND)
    824             { reader->ifsc = DEFAULT_IFSC; }
     1116        {
     1117            reader->ifsc = DEFAULT_IFSC;
     1118        }
    8251119        else if((ta != 0x00) && (ta != 0xFF))
    826             { reader->ifsc = ta; }
     1120        {
     1121            reader->ifsc = ta;
     1122        }
    8271123        else
    828             { reader->ifsc = DEFAULT_IFSC; }
     1124        {
     1125            reader->ifsc = DEFAULT_IFSC;
     1126        }
    8291127
    8301128        //FIXME workaround for Smargo until native mode works
    8311129        if(reader->smargopatch == 1)
    832             { reader->ifsc = MIN(reader->ifsc, 28); }
     1130        {
     1131            reader->ifsc = MIN(reader->ifsc, 28);
     1132        }
    8331133        else
    8341134            // Towitoko and smartreaders dont allow IFSC > 251
    835             { reader->ifsc = MIN(reader->ifsc, MAX_IFSC); }
     1135        {
     1136            reader->ifsc = MIN(reader->ifsc, MAX_IFSC);
     1137        }
    8361138
    8371139#ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
     
    8531155        // Set CWT = 11+(2^CWI) work etu
    8541156        reader->CWT = (uint16_t) 11 + (1 << cwi); // in work ETU
    855         // Set BWT = (2^BWI * 960 * 372 / clockspeed in mhz) us + 11*work etu
    856         // Set BWT = (2^BWI * 960 * 372 / clockspeed in mhz) / worketu + 11
    8571157
    8581158        reader->BWT = (uint32_t) ((1<<bwi) * 960 * 372 / (double)((double)F / 100) / (double) reader->worketu) + 11;  // BWT in work ETU
    8591159
    860         // Set BGT = 22 * work etu
    8611160        BGT = 22L; // Block Guard Time in ETU used to interspace between block responses
    862 
    8631161        GT = 2; // standard guardtime
    8641162        GT += 1; // start bit
     
    8671165
    8681166        if(N == 255)
    869             { GT -= 1; } // special case, ATR says standard 2 etu guardtime is decreased by 1 (in ETU) EGT remains zero!
     1167        {
     1168            GT -= 1;    // special case, ATR says standard 2 etu guardtime is decreased by 1 (in ETU) EGT remains zero!
     1169        }
    8701170        else
    871             { EGT += N; } // ATR says add extra guardtime (in ETU)
     1171        {
     1172            EGT += N;    // ATR says add extra guardtime (in ETU)
     1173        }
    8721174
    8731175        // Set the error detection code type
    8741176        if(ATR_GetInterfaceByte(atr, 3, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND)
    875             { edc = EDC_LRC; }
     1177        {
     1178            edc = EDC_LRC;
     1179        }
    8761180        else
    877             { edc = tc & 0x01; }
     1181        {
     1182            edc = tc & 0x01;
     1183        }
    8781184
    8791185        // Set initial send sequence (NS)
    8801186        reader->ns = 1;
    881 
     1187#ifdef READER_NAGRA_MERLIN
     1188        cas7_seq=0x00;
     1189#endif
    8821190        rdr_log_dbg(reader, D_ATR, "Protocol: T=%i: IFSC=%d, CWT=%d etu, BWT=%d etu, BGT=%d etu, EDC=%s, N=%d",
    883                        reader->protocol_type, reader->ifsc,
    884                        reader->CWT, reader->BWT,
    885                        BGT, (edc == EDC_LRC) ? "LRC" : "CRC", N);
     1191                    reader->protocol_type, reader->ifsc,
     1192                    reader->CWT, reader->BWT,
     1193                    BGT, (edc == EDC_LRC) ? "LRC" : "CRC", N);
    8861194        reader->read_timeout = reader->BWT;
    8871195        reader->block_delay = BGT;
    8881196        reader->char_delay = GT + EGT;
    889         rdr_log_dbg(reader, D_ATR, "Setting timings: reader timeout=%u ETU, block_delay=%u ETU, char_delay=%u ETU",
    890                        reader->read_timeout, reader->block_delay, reader->char_delay);
     1197        rdr_log_dbg(reader, D_ATR, "Setting timings: reader timeout=%u ETU, block_delay=%u ETU, char_delay=%u ETU", reader->read_timeout, reader->block_delay, reader->char_delay);
    8911198
    8921199        break;
     
    9021209    if(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && reader->protocol_type == ATR_PROTOCOL_TYPE_T14)
    9031210    {
    904         ETU = 0;    // for Irdeto T14 cards, do not set ETU
    905 //      if ((reader->typ == R_SMART) && (reader->smart_type >= 2))
    906 //      reader->worketu *= 2.1; else // increase the worketu for v2 and tripple zigo try out
     1211        ETU = 0;
    9071212        reader->worketu *= 2; // overclocked T14 needs this otherwise high ecm reponses
    9081213    }
    9091214
    910     struct s_cardreader_settings s = {
     1215    struct s_cardreader_settings s =
     1216    {
    9111217        .ETU = ETU,
    9121218        .EGT = EGT,
     
    9261232    }
    9271233
    928     if(reader->typ == R_INTERNAL)
    929     {
    930         if(reader->cardmhz > 2000)
    931         {
    932             rdr_log(reader, "PLL Reader: ATR Fsmax is %i MHz, clocking card to %.2f Mhz (nearest possible mhz specified reader->mhz)",
    933                     atr_fs_table[FI] / 1000000, (float) reader->mhz / 100);
    934         }
    935         else
    936         {
    937             rdr_log(reader, "ATR Fsmax is %i MHz, clocking card to %.2f (specified in reader->mhz)",
    938                     atr_fs_table[FI] / 1000000, (float) reader->mhz / 100);
    939         }
    940     }
    941     else
    942     {
    943         if ((reader->typ == R_SMART) && (reader->autospeed == 1))
    944             rdr_log(reader, "ATR Fsmax is %i MHz, clocking card to ATR Fsmax for smartreader cardspeed of %.2f MHz (specified in reader->mhz)",
    945                 atr_fs_table[FI] / 1000000, (float) reader->mhz / 100);
    946         else
    947             rdr_log(reader, "ATR Fsmax is %i MHz, clocking card to wanted user cardclock of %.2f MHz (specified in reader->mhz)",
    948                 atr_fs_table[FI] / 1000000, (float) reader->mhz / 100);
    949     }
     1234    /*
     1235        if(reader->typ == R_INTERNAL)
     1236        {
     1237            if(reader->cardmhz > 2000){
     1238                rdr_log(reader, "PLL Reader: ATR Fsmax is %i MHz, clocking card to %.2f Mhz (nearest possible mhz specified reader->mhz)", atr_fs_table[FI] / 1000000, (float) reader->mhz / 100);
     1239            }else{
     1240                rdr_log(reader, "ATR Fsmax is %i MHz, clocking card to %.2f (specified in reader->mhz)", atr_fs_table[FI] / 1000000, (float) reader->mhz / 100);
     1241            }
     1242        }else{
     1243            if ((reader->typ == R_SMART) && (reader->autospeed == 1)){
     1244                rdr_log(reader, "ATR Fsmax is %i MHz, clocking card to ATR Fsmax for smartreader cardspeed of %.2f MHz (specified in reader->mhz)", atr_fs_table[FI] / 1000000, (float) reader->mhz / 100);
     1245            }else{
     1246                rdr_log(reader, "ATR Fsmax is %i MHz, clocking card to wanted user cardclock of %.2f MHz (specified in reader->mhz)",atr_fs_table[FI] / 1000000, (float) reader->mhz / 100);
     1247            }
     1248        }
     1249    */
    9501250
    9511251    //Communicate to T1 card IFSD -> we use same as IFSC
     1252
    9521253    if(reader->protocol_type == ATR_PROTOCOL_TYPE_T1 && reader->ifsc != DEFAULT_IFSC && !crdr_ops->skip_setting_ifsc)
    9531254    {
     
    9581259        tmp[3] = reader->ifsc; // Information Field size
    9591260        tmp[4] = reader->ifsc ^ 0xE1;
    960         ret = Protocol_T1_Command(reader, tmp, sizeof(tmp), rsp, &lr);
    961         if(ret != OK) { rdr_log(reader, "Warning: Card returned error on setting ifsd value to %d", reader->ifsc); }
    962         else { rdr_log(reader, "Card responded ok for ifsd request of %d", reader->ifsc); }
     1261        ret = Protocol_T1_Command(reader, tmp, sizeof(tmp), rsp, &lr, 0);
     1262        if(ret != OK)
     1263        {
     1264            rdr_log(reader, "Warning: Card returned error on setting ifsd value to %d", reader->ifsc);
     1265        }
     1266        else
     1267        {
     1268            rdr_log_dbg(reader, D_ATR, "Card responded ok for ifsd request of %d", reader->ifsc);
     1269        }
    9631270    }
    9641271    return OK;
     
    9691276    unsigned char pck;
    9701277    uint32_t i;
    971 
    9721278    pck = block[0];
    9731279    for(i = 1; i < length; i++)
    974         { pck ^= block[i]; }
    975 
     1280    {
     1281        pck ^= block[i];
     1282    }
    9761283    return pck;
    9771284}
  • trunk/csctapi/icc_async.h

    r8980 r11429  
    4242int32_t ICC_Async_GetStatus(struct s_reader *reader, int32_t *has_card);
    4343
    44 
    4544/* Operations */
    46 int32_t ICC_Async_CardWrite(struct s_reader *reader, unsigned char *cmd, uint16_t lc, unsigned char *rsp, uint16_t *lr);
     45int32_t ICC_Async_CardWrite(struct s_reader *reader, unsigned char *cmd, uint16_t lc, unsigned char *rsp, uint16_t *lr, uint8_t block_nad);
    4746int32_t ICC_Async_Transmit(struct s_reader *reader, uint32_t size, uint32_t expectedlen, unsigned char *buffer, uint32_t delay, uint32_t timeout);
    4847int32_t ICC_Async_Receive(struct s_reader *reader, uint32_t size, unsigned char *buffer, uint32_t delay, uint32_t timeout);
    4948
    5049void ICC_Async_DisplayMsg(struct s_reader *, char *msg);
    51 int32_t ICC_Async_Reset(struct s_reader *, struct s_ATR *,
    52                         int32_t (*rdr_activate_card)(struct s_reader *, struct s_ATR *, uint16_t deprecated),
    53                         int32_t (*rdr_get_cardsystem)(struct s_reader *, struct s_ATR *)
    54                        );
     50int32_t ICC_Async_Reset(struct s_reader *, struct s_ATR *, int32_t (*rdr_activate_card)(struct s_reader *, struct s_ATR *, uint16_t deprecated), int32_t (*rdr_get_cardsystem)(struct s_reader *, struct s_ATR *));
     51
     52#ifdef READER_NAGRA_MERLIN
     53uint32_t calc_ccitt32(uint8_t *buf, uint8_t count);
     54uint32_t cas7_seq;
     55void calculate_cas7_cmd(struct s_reader *reader, uint8_t *cmdin,uint8_t cmdlen,uint8_t *cmdout);
     56void do_cas7_cmd(struct s_reader *reader,unsigned char *cta_res, uint16_t *p_cta_lr,uint8_t *data,uint8_t inlen,uint8_t resplen);
     57#endif
    5558
    5659#endif /* _ICC_ASYNC_ */
    5760
     61
  • trunk/csctapi/protocol_t0.h

    r8960 r11429  
    2929int32_t Protocol_T0_Command(struct s_reader *reader, unsigned char *command, uint16_t command_len, unsigned char *rsp, uint16_t *lr);
    3030int32_t Protocol_T14_ExchangeTPDU(struct s_reader *reader, unsigned char *command, uint16_t command_len, unsigned char *rsp, uint16_t *lr);
    31 int32_t Protocol_T1_Command(struct s_reader *reader, unsigned char *command, uint16_t command_len, unsigned char *rsp, uint16_t *lr);
     31int32_t Protocol_T1_Command(struct s_reader *reader, unsigned char *command, uint16_t command_len, unsigned char *rsp, uint16_t *lr, uint8_t block_nad);
    3232
    3333#endif /* _PROTOCOL_T0_ */
  • trunk/csctapi/protocol_t1.c

    r10283 r11429  
    1 /*
    2     protocol_t1.c
    3     Handling of ISO 7816 T=1 protocol
    4 
    5     This file is part of the Unix driver for Towitoko smartcard readers
    6     Copyright (C) 2000 Carlos Prados <cprados@yahoo.com>
    7 
    8     This version is modified by doz21 to work in a special manner ;)
    9 
    10     This library is free software; you can redistribute it and/or
    11     modify it under the terms of the GNU Lesser General Public
    12     License as published by the Free Software Foundation; either
    13     version 2 of the License, or (at your option) any later version.
    14 
    15     This library is distributed in the hope that it will be useful,
    16     but WITHOUT ANY WARRANTY; without even the implied warranty of
    17     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    18     Lesser General Public License for more details.
    19 
    20     You should have received a copy of the GNU Lesser General Public
    21     License along with this library; if not, write to the Free Software
    22     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 */
    24 
    251#include "../globals.h"
    262#include "../oscam-time.h"
     
    284#include "icc_async.h"
    295
    30 #define OK 0
    31 #define ERROR 1
     6#define OK  0
     7#define ERROR   1
    328
    339/* Buffer sizes */
    34 #define T1_BLOCK_MAX_SIZE                259
     10#define T1_BLOCK_MAX_SIZE   259
    3511
    3612/* Types of block */
    37 #define T1_BLOCK_I                0x00
    38 #define T1_BLOCK_R_OK             0x80
    39 #define T1_BLOCK_R_EDC_ERR        0x81
    40 #define T1_BLOCK_R_OTHER_ERR      0x82
    41 #define T1_BLOCK_S_RESYNCH_REQ    0xC0
    42 #define T1_BLOCK_S_RESYNCH_RES    0xE0
    43 #define T1_BLOCK_S_IFS_REQ        0xC1
    44 #define T1_BLOCK_S_IFS_RES        0xE1
    45 #define T1_BLOCK_S_ABORT_REQ      0xC2
    46 #define T1_BLOCK_S_ABORT_RES      0xE2
    47 #define T1_BLOCK_S_WTX_REQ        0xC3
    48 #define T1_BLOCK_S_WTX_RES        0xE3
    49 #define T1_BLOCK_S_VPP_ERR        0xE4
    50 
    51 #define T1_BLOCK_NAD        0x00
     13#define T1_BLOCK_I      0x00
     14#define T1_BLOCK_R_OK       0x80
     15#define T1_BLOCK_R_EDC_ERR  0x81
     16#define T1_BLOCK_R_OTHER_ERR    0x82
     17#define T1_BLOCK_S_RESYNCH_REQ  0xC0
     18#define T1_BLOCK_S_RESYNCH_RES  0xE0
     19#define T1_BLOCK_S_IFS_REQ  0xC1
     20#define T1_BLOCK_S_IFS_RES  0xE1
     21#define T1_BLOCK_S_ABORT_REQ    0xC2
     22#define T1_BLOCK_S_ABORT_RES    0xE2
     23#define T1_BLOCK_S_WTX_REQ  0xC3
     24#define T1_BLOCK_S_WTX_RES  0xE3
     25#define T1_BLOCK_S_VPP_ERR  0xE4
     26
     27#define T1_BLOCK_NAD        0x21
    5228
    5329#define T1_Block_GetNS(a)   ((a[1] >> 6) & 0x01)
     
    6238    uint32_t i;
    6339    for(i = 0; i < length; i++)
    64         { lrc ^= data[i]; }
     40    {
     41        lrc ^= data[i];
     42    }
    6543    return lrc;
    6644}
    6745
    68 static int32_t T1_Block_SendIBlock(struct s_reader *reader, uint8_t *block_data, unsigned char len, unsigned char *inf, unsigned char ns, int32_t more,                    uint32_t timeout)
     46static int32_t T1_Block_SendIBlock(struct s_reader *reader, uint8_t *block_data, unsigned char len, unsigned char *inf, unsigned char ns, int32_t more, uint32_t timeout, uint8_t block_nad)
    6947{
    7048    int length = len + 4;
    7149
    72     block_data[0] = T1_BLOCK_NAD;
     50    block_data[0] = block_nad;
    7351    block_data[1] = T1_BLOCK_I | ((ns << 6) & 0x40);
    7452    if(more)
    75         { block_data[1] |= 0x20; }
     53    {
     54        block_data[1] |= 0x20;
     55    }
    7656    block_data[2] = len;
    7757    if(len != 0x00)
    78         { memcpy(block_data + 3, inf, len); }
     58    {
     59        memcpy(block_data + 3, inf, len);
     60    }
    7961    block_data[len + 3] = T1_Block_LRC(block_data, len + 3);
    8062
     
    10284    block_data[2] = len;
    10385    if(len != 0x00)
    104         { memcpy(block_data + 3, inf, len); }
     86    {
     87        memcpy(block_data + 3, inf, len);
     88    }
    10589
    10690    block_data[len + 3] = T1_Block_LRC(block_data, len + 3);
     
    11599    /* Receive four mandatory bytes */
    116100    if(ICC_Async_Receive(reader, 4, block_data, 0, timeout))
    117         { ret = ERROR; }
     101    {
     102        ret = ERROR;
     103    }
    118104    else
    119105    {
     
    125111            /* Receive remaining bytes */
    126112            if(ICC_Async_Receive(reader, *block_length - 4, block_data + 4, 0, timeout))
    127                 { ret = ERROR; }
     113            {
     114                ret = ERROR;
     115            }
    128116            else
    129                 { ret = OK; }
     117            {
     118                ret = OK;
     119            }
    130120        }
    131121        else
     
    140130}
    141131
    142 int32_t Protocol_T1_Command(struct s_reader *reader, unsigned char *command, uint16_t command_len, unsigned char *rsp, uint16_t *lr)
     132int32_t Protocol_T1_Command(struct s_reader *reader, unsigned char *command, uint16_t command_len, unsigned char *rsp, uint16_t *lr, uint8_t block_nad)
    143133{
    144134    uint8_t block_data[T1_BLOCK_MAX_SIZE];
     
    148138    bool more;
    149139    uint32_t block_length = 0;
     140
     141    if(block_nad == 0)
     142    {
     143        block_nad = T1_BLOCK_NAD;
     144    }
     145
    150146    if(command[1] == T1_BLOCK_S_IFS_REQ)
    151147    {
     148        rdr_log_dbg(reader, D_READER, "command[1] == T1_BLOCK_S_IFS_REQ");
     149
    152150        uint8_t inf = command[3];
    153151
     
    177175    else if(command[1] == T1_BLOCK_S_RESYNCH_REQ)
    178176    {
     177        rdr_log_dbg(reader, D_READER, "command[1] == T1_BLOCK_S_RESYNCH_REQ");
    179178        /* Create an Resynch request S-Block */
    180179        timeout = ICC_Async_GetTimings(reader, reader->CWT);  // we are going to send: CWT timeout
     
    213212    timeout = ICC_Async_GetTimings(reader, reader->CWT);  // we are going to send: CWT timeout
    214213    //cs_sleepus(reader->block_delay); // we were receiving, now sending so wait BGT time
    215     ret = T1_Block_SendIBlock(reader, block_data, bytes, command, reader->ns, more, timeout);
     214    ret = T1_Block_SendIBlock(reader, block_data, bytes, command, reader->ns, more, timeout, block_nad);
    216215    rdr_log_dbg(reader, D_IFD, "Sending block I(%d,%d)", reader->ns, more);
    217216
     
    244243                timeout = ICC_Async_GetTimings(reader, reader->CWT);  // we are going to send: CWT timeout
    245244                //cs_sleepus(reader->block_delay); // we were receiving, now sending so wait BGT time
    246                 ret = T1_Block_SendIBlock(reader, block_data, bytes, command + counter, reader->ns, more, timeout);
     245                ret = T1_Block_SendIBlock(reader, block_data, bytes, command + counter, reader->ns, more, timeout, block_nad);
    247246                rdr_log_dbg(reader, D_IFD, "Protocol: Sending block I(%d,%d)", reader->ns, more);
    248247
     
    286285                nr = (T1_Block_GetNS(block_data) + 1) % 2;
    287286
    288                 if(counter + bytes > T1_BLOCK_MAX_SIZE) { return ERROR; }
     287                if(counter + bytes > T1_BLOCK_MAX_SIZE)
     288                {
     289                    return ERROR;
     290                }
    289291
    290292                memcpy(rsp + counter, block_data + 3, bytes);
     
    324326
    325327    if(ret == OK)
    326         { *lr = counter; }
     328    {
     329        *lr = counter;
     330    }
    327331
    328332    return ret;
  • trunk/globals.h

    r11414 r11429  
    3737#include <inttypes.h>
    3838#include <sys/utsname.h>
    39 #include <sys/sysmacros.h>
    4039
    4140/*
     
    363362 *         constants
    364363 * =========================== */
    365 #define CS_VERSION    "1.20-unstable_svn"
     364#define CS_VERSION    "1.20_svn"
    366365#ifndef CS_SVN_VERSION
    367366#   define CS_SVN_VERSION "test"
     
    14801479    FTAB            localcards;
    14811480    FTAB            disablecrccws_only_for;         // ignore checksum for selected caid provid
     1481#ifdef READER_NAGRA_MERLIN
     1482    unsigned char   kdt05_00[216];
     1483    unsigned char   kdt05_10[208];
     1484    unsigned char   cardid[8];
     1485    unsigned char   edata[255];
     1486    unsigned char   dt5num;
     1487    unsigned char   out[255];
     1488    unsigned char   ideakey1[16];
     1489    unsigned char   block3[8];
     1490    unsigned char   v[8];
     1491    unsigned char   iout[8];
     1492    unsigned char   dtdata[0x10];
     1493    uint32_t        dword_83DBC;
     1494    unsigned char   data2[4];
     1495    unsigned char   cas7expo[0x11];
     1496    unsigned char   data[0x80];
     1497    unsigned char   step1[0x60];
     1498    unsigned char   step2[0x68];
     1499    unsigned char   step3[0x6c];
     1500    unsigned char   encrypted[0x68];
     1501    uchar           result[104];
     1502    uchar           stillencrypted[0x50];
     1503    uchar           resultrsa[0x50];
     1504    uint32_t        cas7_seq;
     1505#endif
    14821506#ifdef CS_CACHEEX
    14831507    CECSP           cacheex; //CacheEx Settings
     
    15161540    int8_t          card_atr_length;                // length of ATR
    15171541    int8_t          seca_nagra_card;                // seca nagra card
     1542#ifdef READER_NAGRA_MERLIN
     1543    uint8_t         cas7_aes_key[32];
     1544    uint8_t         cas7_aes_iv[16];
     1545#endif
    15181546    int32_t         atrlen;
    15191547    SIDTABS         sidtabs;
     
    15231551    uchar           prid[CS_MAXPROV][8];
    15241552    uchar           sa[CS_MAXPROV][4];              // viaccess & seca
    1525     uint8_t         read_old_classes;               // viaccess
    1526     uint8_t         maturity;                       // viaccess & seca maturity level
     1553    uint8_t         read_old_classes;               // viaccess
     1554    uint8_t         maturity;                       // viaccess & seca maturity level
    15271555    uint16_t        caid;
    15281556    uint16_t        b_nano;
     
    22122240    struct s_ip *scam_allowed;
    22132241#endif
     2242
    22142243    int32_t    max_cache_time;  //seconds ecms are stored in ecmcwcache
    22152244    int32_t    max_hitcache_time;  //seconds hits are stored in cspec_hitcache (to detect dyn wait_time)
  • trunk/module-webif-tpl.c

    r11427 r11429  
    448448                                            check_conf(READER_IRDETO, ptr2);
    449449                                            check_conf(READER_NAGRA, ptr2);
     450                                            check_conf(READER_NAGRA_MERLIN, ptr2);
    450451                                            check_conf(READER_SECA, ptr2);
    451452                                            check_conf(READER_TONGFANG, ptr2);
  • trunk/oscam.c

    r11427 r11429  
    273273            oscam_pidfile = optarg;
    274274            break;
    275 #if defined(WITH_STAPI) || defined(WITH_STAPI5)
    276275        case 'f': // --foreground
    277276            bg = 0;
    278277            break;
    279 #else
    280278        case 'b': // --daemon
    281279            bg = 1;
    282280            break;
    283 #endif
    284281        case 'c': // --config-dir
    285282            cs_strncpy(cs_confdir, optarg, sizeof(cs_confdir));
     
    453450        fprintf(fp, "\n");
    454451        write_readerconf(READER_NAGRA, "Nagra");
     452        write_readerconf(READER_NAGRA_MERLIN, "Nagra_Merlin");
    455453        write_readerconf(READER_IRDETO, "Irdeto");
    456454        write_readerconf(READER_CONAX, "Conax");
     
    501499    do { \
    502500        if (!config_enabled(CONFIG_VAR)) \
    503             cs_log("Binary without %s module - no EMM processing for %s possible!", text, text); \
     501            cs_log_dbg(D_TRACE, "Binary without %s module - no EMM processing for %s possible!", text, text); \
    504502    } while(0)
    505503
     
    513511    {
    514512        report_emm_support(READER_NAGRA, "Nagra");
     513        report_emm_support(READER_NAGRA_MERLIN, "Nagra_Merlin");
    515514        report_emm_support(READER_IRDETO, "Irdeto");
    516515        report_emm_support(READER_CONAX, "Conax");
     
    15581557const struct s_cardsystem *cardsystems[] =
    15591558{
     1559#ifdef READER_NAGRA_MERLIN
     1560    &reader_nagracak7,
     1561#endif
    15601562#ifdef READER_NAGRA
    15611563    &reader_nagra,
     
    16431645    &cardreader_stinger,
    16441646#endif
     1647
    16451648    NULL
    16461649};
  • trunk/reader-common.c

    r11362 r11429  
    1616//#include "csctapi/atr.h"
    1717#include "csctapi/icc_async.h"
     18#include "readers.h"
    1819
    1920extern const struct s_cardsystem *cardsystems[];
     
    4748    *p_cta_lr = CTA_RES_LEN - 1; //FIXME not sure whether this one is necessary
    4849    rdr_log_dump_dbg(reader, D_READER, buf, l, "write to cardreader");
    49     rc = ICC_Async_CardWrite(reader, (uchar *)buf, (uint16_t)l, cta_res, p_cta_lr);
     50    rc = ICC_Async_CardWrite(reader, (uchar *)buf, (uint16_t)l, cta_res, p_cta_lr, 0);
    5051    return rc;
    5152}
  • trunk/readers.h

    r11246 r11429  
    33
    44extern const struct s_cardsystem reader_nagra;
     5extern const struct s_cardsystem reader_nagracak7;
    56extern const struct s_cardsystem reader_irdeto;
    67extern const struct s_cardsystem reader_cryptoworks;
Note: See TracChangeset for help on using the changeset viewer.