Page 1 of 1

T0 Parsing

Posted: Thu Jul 26, 2018 1:12 am
by mabel
Just want to write something about T0, welcome any comments.

1. Introduction to JavaCard power-on process
The following figure shows the data captured during the JavaCard power-on process. It sent 3B firstly, then ATR value.
After the JavaCard is powered on, it will be converted to each other in various states. The following figure shows the conversion relationship of JavaCard various states.
2. Introduction to ATR
ATR consists of 5 parts:
|TS|T0|interface character|historical character|check character|
TS: initial character.
T0: format byte.
TS
TS defines whether convention is forward or reverse. Usually it uses 3B,3B is HHL HHH LL as shown above.

T0
Indicates whether the interface byte exists and the number of history byte

Interface byte
Communications parameters

historical byte
Smart card operating feature

check character

Example: 3B 61 00 00 80
HHLH HHLL 0110 0001 0000 0000 0000 0000 1000 0000
3B H = 1,Transfer the low bit firstly
T0 TB1、TC1 exsit,one historical bit.
TB1 Abandon
TC1 Extra guard time integer N=0。
historical character 80
When T0 is only supported, the check character cannot be added.
Please refer to 7816-3 to know more about T0.

Re: T0 Parsing

Posted: Thu Jul 26, 2018 2:00 am
by mabel
3. Introduction to PPS
PPS consists of 4 parts:

|PPSS|PPS0|PPS1,PPS2,PPS3|PCK|

PPSS: 0xFF
PPS0(format byte): indicate whether the following PPSx exists and specify the protocol type
PPS1 PPS2 PPS3: Communications parameters and so on
PCK: check character

4. Basic concept in T0

Character frame in T0
T0 is a character transfer protocol. Each frame of it is a character, the following figure shows how a character looks like in transmission

Basic time unit etu
1 etu = (Fi/Di) * (1/f)

GT : Minimum time interval between two consecutive characters in the same direction
GT = 12etu + R*(N/f)
N:Extra guard time integer, defined by TC1. The default is 0.

WT:The maximum time interval between the characters sent by card and the characters sent by the previous one (card or card reader)
WT = WI*960*(Fi/f)
WI:It’s TC2 when TC2 exists. When TC2 does not exist, the default value is 10.


To be continued...

Re: T0 Parsing

Posted: Wed Aug 08, 2018 1:22 am
by mabel
5. Basic principles of T0 protocol

(1) The communication protocol is opened after PPS is completed.
(2) The interface device sends a 5-byte command header to the card
(3) The card sends process byte.
(4) Transfer data in one direction under the control of the process byte
Note: Under T0 protocol, both reader and card know the transmission direction and length of data in advance.
There are 3 kinds of process bytes.

Re: T0 Parsing

Posted: Wed Aug 08, 2018 1:27 am
by mabel
6. APDU formats

There are 4 kinds of APDU formats
Case1: CLA INS P1 P2
Case2: CLA INS P1 P2 Le
Case3: CLA INS P1 P2 Lc Data
Case4: CLA INS P1 P2 Lc Data Le

Case1
TPDU:CLA INS P1 P2 00
CLA INS P1 P2 00 <- Smart Card Reader
Card-> SW1 SW2

java code:
Case (byte)0x11:
break;

Re: T0 Parsing

Posted: Wed Aug 08, 2018 1:41 am
by mabel
Case2S

TPDU: CLA INS P1 P2 Le
(1)Le is in line with expectation.
CLA INS P1 P2 Le <- Smart Card Reader
Card-> DATA SW1 SW2
(2)Le does not meet expectations and does not prompt for data length as well.
CLA INS P1 P2 Le <- Smart Card Reader
Card -> 67 00
(3)Le does not meet expectations, but prompts for data length.
CLA INS P1 P2 Le <- Smart Card Reader
Card -> 6C SW2
CLA INS P1 P2 SW2 <- Smart Card Reader
Card -> DATA SW1 SW2
(4)SW=9XYZ,且SW != 9000
CLA INS P1 P2 Le <- Smart Card Reader
Card -> DATA 9XYZ

Java code:
case (byte)0x20:
le = (short)(buf[ISO7816.OFFSET_LC]);
if (le != 0x6)
{
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
for(i = 1; i <= 6; i++)
{
buf[(short)(i - 1)] = (byte)i;
}
apdu.setOutgoingAndSend((short)0, (short)6);
break;

When Case2S Le is in line with expectation:

Re: T0 Parsing

Posted: Wed Aug 08, 2018 1:42 am
by mabel
When Case2S Le doesn’t meet the expectation:

Re: T0 Parsing

Posted: Wed Aug 08, 2018 1:45 am
by mabel
Another example of Case2S:
Java code:
case (byte)0x21:
for(i = 1; i <= 6; i++)
{
buf[(short)(i - 1)] = (byte)i;
}
apdu.setOutgoingAndSend((short)0, (short)6);
break;
When needing 6C00:

Re: T0 Parsing

Posted: Wed Aug 08, 2018 1:49 am
by mabel
Case 3S

TPDU: CLA INS P1 P2 Lc
CLA INS P1 P2 Lc <- Smart Card Reader
Card-> SW1 SW2
java code:
case (byte)0x30:
le = apdu.setIncomingAndReceive();
break;

Re: T0 Parsing

Posted: Wed Aug 08, 2018 1:52 am
by mabel
Case 4S
TPDU: CLA INS P1 P2 Lc DATA Le
(1)SW1=6X, and SW1̸=61,62,63
CLA INS P1 P2 Lc <- Smart Card Reader
Card-> 6X SW2
(2)61XX
CLA INS P1 P2 Lc <- Smart Card Reader
Card -> INS
DATA <- Smart Card Reader
Card -> 61 Nx
CLA C0 P1 P2 P3=min(Le, Nx) <- Smart Card Reader
Card -> DATA SW1 SW2

java code:
case (byte)0x41:
le = apdu.setIncomingAndReceive();
for(i = 1; i <= 6; i++)
{
buf[(short)(i - 1)] = (byte)i;
}
apdu.setOutgoingAndSend((short)0, (short)6);
break;

Re: T0 Parsing

Posted: Tue Aug 14, 2018 4:16 am
by DaHuFa
I expect learn more~
For example, T1 and contactless and so on. :lol: