一、ATR是什么
ATR也叫復位應答,是一個字節序列,這些字節是由卡作為對復位命令的響應發送給讀卡器的。在I/O電路上,每個成功的復位操作都會導致I/O上的一個由初始字符TS開始,后跟最多32個字符的復位應答。ATR的作用是告訴讀卡器,我是一張什么樣的卡片,以便讀卡器知道接下來該以什么樣的方式和卡片通信。
二、ATR的數據元
ISO/IEC 7816-3標准中對ATR的數據串和數據元做了規定和描述。ATR的數據元和它們的意義:
數據元 |
說明 |
TS |
起始字符 |
T0 |
格式字符 |
TA1,TB1,TC1,TD1,... |
接口字符 |
T1,T2,... ,TK |
歷史字符 |
TCK |
校驗字符 |
三、ATR的基本數據結構
復位應答ATR的基本數據結構和數據元如下:
(一)起始字符TS
起始字符規定了用於在ATR中的所有數據和后繼通信過程的約定。此外,TS字節含有特殊的位模式可由終端用以識別分頻值。終端測量TS中前兩個下降沿之間的時間並除以3,其結果就是一個etu的持續時間。然而,由於ATR的分頻值固定在372,終端通常並不評估同步模式。
TS是ATR的強制部分,總是必須送出的。此字節只允許有兩種編碼:3B為正向約定,3F為反向約定。
使用反向邏輯約定時, I/O的低電平狀態等效於邏輯1,且該數據字節的最高位在起始位之后首先發送。
使用正向邏輯約定時, I/O的高電平狀態等效於邏輯1,且該數據字節的最低位在起始位之后首先發送。
正向約定的波形如下,一般終端讀取到的字符為3B:
反向約定的波形,一般終端讀取到的字符為03:
(二)格式字符T0
格式字符T0含有一組位表明將要傳送哪個接口字符,它同時也指出后繼歷史字符的個數。像TS一樣,每個ATR中都必須有這個字節。
高半字節( b5-b8) 表示后續字符TA1到TD1是否存在。(b5對應TA1,b8對應TD1);
低半字節( b1-b4) 表明可選歷史字符的數目( 0到15);
終端應接受包含任何T0值的ATR。一般情況下,基本ATR中,當僅選擇T=0時, IC卡應回送T0=6x,表示字符TB1和TC1存在;當僅選擇T=1時, IC卡應回送T0=Ex,表示字符TB1到TD1存在。
(三)接口字符
接口字符規定所用協議的所用傳輸參數,它們包含TAi、TBi、TCi、TDi各字節。然而,這些字節在ATR中是可選的,它們也可以被略去。由於對協議的所用參數的缺省值都做了規定,對於一般的通信處理可以不需要ATR中的接口字符。
接口字符可以分為全局接口字符和專用接口字符。全局接口字符規定基本傳輸協議參數,諸如分頻值,它們用於所有后繼的協議;專用接口字符則是用於指明特定的傳輸協議中的參數。“工作等待時間(work waiting time)”就是T=0協議中這類參數的典型例子。
全程接口字符基本上用於所有協議,出於歷史原因(因為在ISO標准中,最初僅包含有T=0協議),這些字符中的幾個僅和T=0協議有關。如果沒有實現T=0協議,可以忽略它們,這這種情況下將使用缺省值。
TDi字節僅用於對任何跟隨接口字符的鏈接保護,TDi字節的高4位組合有一個指示后繼接口字符的存在的位模式。它類似於格式字符T0的編碼,TDi字節的低4位則標識現行使用的傳輸協議。如果沒有TDi字節的存在,則TAi+1、TBi+1、TCi+1和TDi+1都不傳送。
其他接口字符(TAi、TBi、TCi)它們都不用於鏈接,而是規定了可用的傳輸協議,按照ISO/IEC 7816-3標准,它們的含義如下:
1. 全局接口字符TA1:(重要)
TA1高半字節 FI 用於確定 F 的值, F 為時鍾速率轉換因子。用於修改復位應答之后終端所提供的時鍾頻率。低半字節 DI 用於確定 D 的值, D 為位速率調節因子。用於調整復位應答之后所使用的位持續時間。etu =(F/D) * (1/f)
如果ATR中存在TA1,且TA2的b5='0',則:
- 如果TA1的值在'11'到'13'之間,終端必須接收ATR,且必須立即采用指明的F和D值(F=372,D=1,2,4);
- 如果TA1的值不在'11'到'13'之間,終端必須拒絕ATR,除非它支持並立即采用指明的條件;(ICS會有選擇,TA1支持的值)
如果ATR中存在TA1,且TA2沒有返回(協商模式),終端必須接收ATR且繼續在后續信息交換過程中使用缺省值D=1和F=372,除非它支持使用協商參數的特殊方法。
如果ATR中沒有返回TA1,則后續信息交換中使用缺省值D=1和F=372。
FI和DI編碼如下:
FI |
0000 |
0001 |
0010 |
0011 |
0100 |
0101 |
0110 |
0111 |
F |
372 |
372 |
558 |
744 |
1116 |
1488 |
1860 |
RFU |
FI |
1000 |
1001 |
1010 |
1011 |
1100 |
1101 |
1110 |
1111 |
F |
RFU |
512 |
768 |
1024 |
1536 |
2048 |
RFU |
RFU |
DI |
0000 |
0001 |
0010 |
0011 |
0100 |
0101 |
0110 |
0111 |
D |
RFU |
1 |
2 |
4 |
8 |
16 |
32 |
RFU |
DI |
1000 |
1001 |
1010 |
1011 |
1100 |
1101 |
1110 |
1111 |
D |
12 |
20 |
RFU |
RFU |
RFU |
RFU |
RFU |
RFU |
2. 全局接口字符TB1:(沒有意義了)
TB1傳送PI1和II的值,PI1 在 b1 到 b5 位中定義,用於確定 IC 卡所需的編程電壓 P 值;II 在 b6 和 b7 位中定義,用於確定 IC 卡所需的最大編程電流 I 值。一般情況下ATR中必須包含TB1=00,表示IC卡不使用VPP。
3. 全局接口字符TC1(重要)
TC1傳送N值,N用於表示增加到最小持續時間的額外保護時間,此處的最小持續時間表示從終端發送到IC卡的、作為后續信息交換的兩個連續字符的起始位下降沿之間的時間。N在TC1的b1-b8位為二進制編碼,其值作為額外保護時間表示增加的etu數目,其值可在0到255之間任選。N=255具有特殊含義,表示在使用T=0協議時,兩個連續字符的起始位下降沿之間的最小延遲時間可減少到12個etu,而在使用T=1協議時可減小到11個etu。
如果TC1值在00到FE之間,增加到字符間最小持續時間的額外保護時間為0到254個etu。對於后續傳輸,額外保護時間必須在12到266個etu之間。如果TC1=FF,則后續傳輸的字符間最小持續時間在使用T=0協議時為12個etu,使用T=1協議時為11個etu。
TC1只適用於終端向IC卡發送的兩個連續字符間的時序,而不適用於IC卡向終端發送字符的情況,也不適用於在相反方向發送字符的情況。
4. 全局接口字符TB2:(沒有意義了)
TB2傳送PI2, PI2用於確定IC卡所需的編程電壓P的值,當PI2出現時,它將取代TB1中回送的PI1的值。通常在ATR中不再使用。
5. T=0傳輸協議專用的接口字符:
1) 專用接口字符TC2(重要)
TC2專用於T=0協議, 傳輸工作等待時間整數(WI),WI用來確定由IC卡發送的任意一個字符起始位下降沿與IC卡或終端發送的前一個字符起始位下降沿之間的最大時間間隔。工作等待時間為:960×D×WI etu。
若TC2字符不出現在ATR中,則使用工作等待時間的缺省值WI=10。
6. T=1傳輸協議專用的接口字符:
1) 專用接口字符TA3(重要)
TA3(如果TD2中指明T=1)回送IC卡的信息域大小整數(IFSI),IFSI決定了IFSC,並指明了卡片可接收的塊信息域的最大長度(INF)。 TA3以字節形式表示IFSC的長度,其取值范圍從01到FE。 00和FF預留(RFU)。
2) 專用接口字符TB3(重要)
TB3(如果TD2中指明T=1)表明了用來計算字符等待時間CWT和字組等待時間BWT的CWI和BWI值, TB3由兩部分組成。低半字節(b1-b4)用於表明CWI值,而高半字節(b5-b8)用於表明BWI值。
BWI的值在0到4之間,CWI的值在0到5之間。
3) 專用接口字符TC3(重要)
TC3(如果TD2中指明T=1)指明了所用的塊錯誤校驗碼的類型,所用代碼類型用b1位表示, b2到b8位未使用。
終端必須能夠接收包括TC3='00'的ATR,拒絕TC3為其它任何值得ATR。
4) 全局接口字符TA2
TA2的存在與否表示IC卡是以“特定模式/專用模式”還是以“協商模式”工作。TA2的低半字節表明的協議類型正是ATR中第一次表明的協議類型。
TA2存在時是“特定模式/專用模式”;
TA2不存在時是“協商模式”;
“協商模式”:終端可以發送“協議和參數選擇”指令PPS,選擇要和IC卡進行通信的協議和相關參數。
(四)歷史字符
很長一段時間,沒有任何標准對歷史字符做出規定,結果是隨操作系統生產者而不同,它們包含了變化廣泛的數據。
(五)校驗字符TCK
TCK具有一個檢驗復位應答期間所發送數據完整性的值。TCK的值應使從T0到包括TCK在內的所有字節進行異或運算的結果為零。
如果在ATR中僅指出了T=0協議,TCK校驗和可以不在ATR的尾部出現。在這種情況下,完全沒有發送它,因為用奇偶校驗已經知道了差錯字節而在T=0協議中重復發送出錯字節又是強制性的。相反,在T=1協議中,TCK字節必須出現,校驗和的計算從字節T0開始,結束於最后的接口字符,如果有則是最后的歷史字符。
【EMV文檔】接收到的ATR不在EMV規定范圍,終端需要的操作
Required terminal behaviour in the event that a terminal receives characters outside the range allowed by EMV.
/************************* TS *************************/
The terminal shall be capable of supporting both inverse and direct convention and shall accept an ATR containing TS having a value of either '3B' or '3F'. An ICC returning an ATR containing TS having any other value shall be rejected.
/************************* T0 *************************/
The terminal shall accept an ATR containing T0 of any value provided that the value returned correctly indicates and is consistent with the interface characters TA1 to TD1 and historical bytes actually returned.
/************************* TA1 *************************/
If TA1 is present in the ATR (indicated by b5 of T0 set to 1) and TA2 is returned with b5 = 0 (specific mode, parameters defined by the interface bytes), the terminal shall:
1. Accept the ATR if the value of TA1 is in the range '11' to '13', and immediately implement the values of F and D indicated (F=372 and D = 1, 2, or 4).
2. Reject the ATR if the value of TA1 is not in the range '11' to '13', unless it is able to support and immediately implement the conditions indicated.
If TA1 is present in the ATR (indicated by b5 of T0 set to 1) and TA2 is not returned (negotiable mode), the terminal shall accept the ATR and shall continue using the default values of D = 1 and F = 372 during all subsequent exchanges, unless it supports a proprietary technique for negotiating the parameters to be used.
If TA1 is absent from the ATR, the default values of D = 1 and F = 372 shall be used during all subsequent exchanges.
/************************* TB1 *************************/
In response to a cold reset, the terminal shall accept only an ATR containing TB1 = '00'.
In response to a warm reset the terminal shall accept an ATR containing TB1 of any value (provided that b6 of T0 is set to 1) or not containing TB1 (provided that b6 of T0 is set to 0) and shall continue the card session as though TB1 = '00' had been returned.
VPP shall never be generated.
/************************* TC1 *************************/
If the value of TC1 is in the range '00' to 'FE', between 0 and 254 etus of extra guardtime shall be added to the minimum character to character duration, which for subsequent transmissions shall be between 12 and 266 etus.
If the value of TC1 = 'FF', then the minimum character to character duration for subsequent transmissions shall be 12 etus if T=0 is to be used, or 11 etus if T=1 is to be used.
The terminal shall accept an ATR not containing TC1 (provided that b7 of T0 is set to 0), and shall continue the card session as though TC1 = '00' had been returned.
/************************* TD1 *************************/
The terminal shall accept an ATR containing TD1 with the m.s. nibble having any value (provided that the value returned correctly indicates and is consistent with the interface characters TA2 to TD2 actually returned), and the l.s. nibble having a value of '0' or '1'. The terminal shall reject an ATR containing other values of TD1.
/************************* TA2 *************************/
The terminal shall accept an ATR containing TA2 provided that all the following conditions are met:
1. The protocol indicated in the l.s. nibble is also the first indicated protocol in the ATR.
2. b5 = 0
3. The terminal is able to support the exact conditions indicated in the applicable interface characters and immediately uses those conditions.
Otherwise, the terminal shall reject an ATR containing TA2.
/************************* TB2 *************************/
The terminal shall reject an ATR containing TB2.
/************************* TC2 *************************/
The terminal shall:
1. reject an ATR containing TC2 = '00'
2. accept an ATR containing TC2 = '0A'
3. reject an ATR containing TC2 having any other value unless it is able to support it.
/************************* TD2 *************************/
The terminal shall accept an ATR containing TD2 with the m.s. nibble having any value (provided that the value returned correctly indicates and is consistent with the interface characters TA3 to TD3 actually returned), and the l.s. nibble having a value of '1' (or 'E' if the l.s. nibble of TD1 is '0'). The terminal shall reject an ATR containing other values of TD2.
/************************* TA3 *************************/
The terminal shall accept an ATR not containing TA3 (provided that b5 of TD2 is set to 0), and shall continue the card session using a value of '20' for TA3.
The terminal shall reject an ATR containing TA3 having a value in the range '00' to '0F' or a value of 'FF'.
/************************* TB3 *************************/
TB3 (if T=1 is indicated in TD2) indicates the values of the CWI and the BWI used to compute the CWT and BWT respectively. The l.s. nibble (b1–b4) is used to indicate the value of CWI, whilst the m.s. nibble (b5–b8) is used to indicate the value of BWI.
The terminal shall reject an ATR not containing TB3, or containing a TB3 indicating BWI greater than 4 and/or CWI greater than 5, or having a value such that 2^CWI ≤ (N + 1). It shall accept an ATR containing a TB3 having any other value.
Note: N is the extra guardtime indicated in TC1. When using T=1, if TC1='FF', the value of N shall be taken as –1. Since the maximum value for CWI allowed by these specifications is 5, note that when T=1 is used, TC1 shall have a value in the range '00' to '1E' or a value of 'FF' in order to avoid a conflict between TC1 and TB3.
/************************* TC3 *************************/
The terminal shall accept an ATR containing TC3 = '00'. It shall reject an ATR containing TC3 having any other value.