关于smartcard 通信协议

本文详细介绍了ISO7816智能卡标准中ATR(Answer to Reset)协议的结构和内容,包括整体构成、各个字段的意义及解析方法。特别关注了TS、T0等关键字段,并展示了如何通过这些字段确定智能卡的通信参数。
摘要由CSDN通过智能技术生成

http://hi.baidu.com/zhaofeng068318/blog/item/9329ec094f9c78d862d986a2.html

 

关于smartcard 通信协议
2009-07-24 08:42

参考 ISO7816 智能卡标准.chm---7916 patr3

Structures and content

1,atr的整体构成:最多不超过32个字节

A reset operation results in the answer from the card consisting of the initial character TS followed by at most 32 characters in the following order:
- T0 ................... Format character (Mandatory)
         - TAi, TBi, TCi, TDi ... Interface characters  (Optional)
         - T1, T2, ... ,TK ...... Historical characters (Optional)
         - TCK .................. Check character    (Conditional)

Reset
 |
 |    _________________________________________         _______   _________
 |   |   |   |   |   |   |   |   |   |   |   |           |   |     |   |   |
 '-->| TS| T0|TA1|TB1|TC1|TD1|TA2|TB2|TC2|TD2| ......... | T1| ... | TK|TCK|
     |___|___|___|___|___|___|___|___|___|___|_         _|___|_   _|__ |___|

     TS  : Initial character
     TO  : Format character
     TAi : Interface character [ codes FI,DI ]
     TBi : Interface character [ codes II,PI1 ]
     TCi : Interface character [ codes N ]
     TDi : Interface character [ codes Yi+1, T ]
     T1, ... , TK : Historical characters (max,15)
     TCK : Check character
Figure  : General configuration of the Answer to Reset
2,atr的第一个字节TS,也就是atr[0]的位说明:

The two possible values of TS (ten consecutive bits from start to bi and corresponding hexadecimal value) are

- Inverse convention : (Z)ZZAAAAAZ
where logic level ONE is A, ba is b8 (msb is first), equal to $3F when decoded by inverse convention.

- Direct convention : (Z)ZZAZZZAAZ
where logic level ONE is Z, ba is b1 (lsb first), equal to $3B when decoded by direct convention.

Start  ba  bb  bc  bd  be  bf  bg  bh  bi
          Z   ____     _______     ___________         ______
                  |   |   |   |   | Z   Z   Z |       |       |   |
               (Z)| A | Z   Z | A |     or    |       | Z  (Z)
          A       |___|       |___|_A___A___A_|___|___|

                    Figure  : Initial character TS
上面是对TS也就是atr的第一个字节atr[0]的说明:
当TS的逻辑电平时A时,atr[0]=3B,正向传输;
当TS的逻辑电平时Z时,atr[0]=3F,反向传输。
 
3,atr的第二个字节atr[1],也就是T0的位说明:
主要是其中的b1-b4组成的K,来说明历史字节数。这4个为最大可以表示15,这个K表明在这个字节后面还跟有K个历史字节,除开这K个
历史字节后才是真正的atr数据。
Format character T0 
-------------------
The T0 character contains two parts:

- The most significant half byte (b5, b6, b7, b8) is named Y1 and indicates with a logic level ONE the presence of subsequent characters TA1, TB1, TC1, TD1 respectively.

- The least significant half byte (b4 to b1) is named K and indicates the number (0 to 15) of historical characters.

,----,----,----,----,----,----,----,----,
         | b8 | b7 | b6 | b5 | b4 | b3 | b2 | b1 |
         '----'----'----'----'----'----'----'----'
         :<------- Y1 ------>:<-------- K ------>:

         Y1 : indicator for the presence of the interface characters
                TA1 is transmitted when b5=1
                TB1 is transmitted when b6=1
                TC1 is transmitted when b7=1
                TD1 is transmitted when b8=1

         K : number of hitorical characters

         Figure : Informations provided by T0
 
4,协议类型说明字节:TAi, TBi, TCi (i=1, 2, 3, ... ) indicate the protocol parameters
 
此段代码就是根据上面的协议来解析atr数据的,可以仔细参考
nRet = eis_smartcard_reset(index, atr);
      atr_len += 2; //CLA/INS
      if (atr[1] & 0x0f0) {
       if (atr[1] & 0x80) {
        // TD1 is transmitted when b8=1
        td1_flag = 1;
        atr_len ++;
       }
       else {
        me->irdtV3Mgr->vdMgr->sc_info[index].protocol = IRDETO_V3_SC_T0;
       }
       if (atr[1] & 0x40) {
        atr_len ++;
       }
       if (atr[1] & 0x20) {
        atr_len ++;
       }
       if (atr[1] & 0x10) {
        atr_len ++;
       }
      }
      if (td1_flag) {//TD1---T>0, TCK
       if ((atr[atr_len - 1] & 0x0f) == 0) {
        me->irdtV3Mgr->vdMgr->sc_info[index].protocol = IRDETO_V3_SC_T0;
       }
       else if ((atr[atr_len - 1] & 0x0f) == 1) {
        me->irdtV3Mgr->vdMgr->sc_info[index].protocol = IRDETO_V3_SC_T1;
       }
       else if ((atr[atr_len - 1] & 0x0f) == 14) {
        me->irdtV3Mgr->vdMgr->sc_info[index].protocol = IRDETO_V3_SC_T14;
       }
       atr_len ++;
      }
      atr_len += (atr[1] & 0x0f);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值