Tuesday, January 21, 2014

Smart Card Interface - ATR String

What is ATR?

The ISO/IEC 7816-3 standard specifies the communication between a smart card and a reader. The very first part of this communication is ATR (Answer To Reset) string, which the card sends straight after reset.
There are two types of reset defined in the standard - 'Cold Reset' and 'Warm Reset'. The ATR string may differ for those two cases.
In the ATR string the card encodes important information for the following communication. The length is variable and can be up to 32 bytes.
The ATR always starts with TS character, also called Initial Character. The TS character encodes the conversion used for further communication. The possible values for the initial character are 0x3B (direct conversion - '1' is high level) or 0x3F (inverse conversion - '1' is low level).
The second character of the ATR string is T0 - format character. T0 encodes the presence of Interface Characters TA1,TB1,TC1,TD1 (bits 7:4) and presence of Historical bytes (bits 0:3)
After T0 follow the optional Interface characters, followed by Historical bytes and TCK - end character (exclusive OR of all the characters).
The interface Characters encode information such as

  • protocol type

  • clock frequency and data rate parameters

  • programming voltage and current

If the reader wants to change any of the parameters, it can be done only first thing after receiving the ATR string following the PPS (protocol Parameter Selection) procedure, described in the ISO/IEC 7816.

Smart card interface schematic.

The schematic show a simple interface between TTL-UART and smart card.

CS1001 abstracts the smart card interface and the ISO/IEC 7816 T=0 protocol, providing simple interface using AT commands via UART.


Communication via terminal

The interface chip (U1 - CS1001) is using AT commands to work with the card.

To power on the card the command is 'CON' (for Card ON), to power it off - 'COFF' (for Card OFF).
>at con
3B 68 00 00 00 73 C8 40 13 00 90 00 
>at coff

TS = 0x3B - Direct conversion

T0 = 0x68 -

- 0x6 (0110) - TB1 and TC1 are present

- 0x8 (1000) - 8 Historical bytes

The absence of TD1 means no further interface characters are present.

TB1 = 0x00 - this character is deprecated.

TC1 = 0x00 - extra guard time integer, N=0 (default)

Historical bytes:

T1 = 0x00 - Category indicator. 0x00 - the last 3 historical bytes are the status indicator.

T2 = 0x73 - TLV - nesting proprietary data objects.

T3 = 0xC8

T4 = 0x40

T5 = 0x13 - End of TLV structure.

T6 = 0x00 - Status indicator byte 1.

T7 = 0x90 - Status indicator byte 2.

T8 = 0x00 - Status indicator byte 3.

TCK is absent as the T=0 only is indicated (by default).