Smart Card Technology ISO7816 T =0/1

Smart Card Working
=============
1. Insertion of the smart card in the slot.
2. Detection of the smart card insertion by the
interfacing device (i.e., microcontroller).
3. Cold reset of the smart card by the interfacing
device.
4. Answer to Reset (ATR) response by the card to
the microcontroller.
5. Protocol and Parameter Selection (PPS)
exchange between the smart card and the
microcontroller (if the smart card supports PPS).
6. Execution of the command(s) between the
smart card and the interfacing device.
7. Removal of the smart card from the slot.
8. Detection of the smart card removal by the
microcontroller.
9. Deactivation of the smart card contacts by the
microcontroller.

After Mechanical Contact with inter facung device interfacing
device has to perform a Cold Reset of the smart card
using the following steps:
1. Pull the RST line to low state.
2. Pull the VCC line to high state.
3. The UART module in the interfacing device
should be in the Reception mode in the software.
4. Provide the clock signal at CLK line of the smart
card.
5. The RST line has to be in the low state for at
least 400 clock cycles after the clock signal is
applied at CLK pin. Therefore, give a delay for at
least 400 clock cycles after providing the clock
at CLK pin of the smart card.
6. Pull the RST line to high state.

------------------------------------------------------------------------------------
ATR :
=====
ATR is a series of characters responded to by the card reader after the successful Cold Reset operation. The
ATR response on the I/O line starts between 400 to 40,000 clock cycles after the RST line is set to high
state. ATR characters determine the initial communication parameters, bit timing, and the data transfer details
between the card and the interfacing device.

The bit timing of the characters during the ATR is called
the “Initial ETU”.

Initial ETU = 372/Æ’ seconds
UART Baud Rate in Smart Card = 1/Initial ETU
Where,
Æ’ = Clock fed to the smart card (in hertz).

Current ETU :
=============
Current ETU = F/(D*Æ’) seconds
UART Baud Rate in PIC® Microcontroller = 1/Current ETU
Where,
F = Clock-rate conversion factor
D = Bit-rate adjustment factor
Æ’ = Clock fed to the smart card (in hertz)
--------------------------------------------------------------------------------------------------------------------

ANSWER TO RESET:
===============
The ATR message, which can be up to 33 characters,
(including the initial character (TS)) consists of these
fields:

• Initial Character (TS) (mandatory)
• Format Character (T0) (mandatory)
• Interface Characters (TA1,TB1,TC1 and TD1)
(optional)
• Historical Characters (T1,T2,...TK) (optional)
• Check Character (TCK) (conditional)


Initial Character (TS): 
----------------------
used for synchronization 
- The first four bits of TS are used for timing synchronization.
- The next three bits are either all high to indicate “Direct convention”, or all low to indicate the “Inverse convention”.
In direct convention LSb first
In Inverse convention Msb First
Format Character (T0) :
----------------------

The T0 byte is made up of Y1 and K nibbles
Y1 higher Nibble
K Lower Nibble

 FORMAT CHARACTER
b8 b7 b6 b5  b4 b3 b2 b1
<----Y1---> <----K----->
Y1 indicates the presence or absence of TA1, TB1,
TC1 and TD1 in the ATR.

• TA1 is transmitted when bit 5 = 1
• TB1 is transmitted when bit 6 = 1
• TC1 is transmitted when bit 7 = 1
• TD1 is transmitted when bit 8 (MSb) = 1

K nibble indicates the number (0 to 15)(T0 T1 T2 ...Tk) of historical
characters present in the remaining ATR sequence.

TA1 Interface Character :
-----------------------
The TA1 character is divided into upper and lower nibbles.
The upper nibble determines the clock rate conversion factor (F),
which is used to modify the frequency of the clock signal 
The lower nibble determines the bit rate adjustment factor (D).


Default value of F = 372 D= 1 for calculating 
Initial Etu.
this value only continued to be used untill not changed the during the PPS.

TB1 Interface Character:
------------------------

IT says about voltage requirment ,
TB1 = 0x00 means Vpp  not connnected
TC1 Interface Character:
------------------------
It is used for calculating Gaurd time.

TD1 Interface Character:
------------------------
-Its shows presense/absence of TA2/TB2/TC2/TD2 interfacing bytes.
-Type of smart card communication protocol supported.

TA2 Interface Character:
-----------------------------
The presence of TA2 in the ATR indicates the specific operative mode
The absence of TA2 indicates the negotiable operative mode. 

The PPS exchange can be done in the negotiable mode, but cannot be done in the
specific mode of smart card operation.

TB2 Interface Character:
-----------------------
The character TB2 conveys programming voltage required by the smart card.

TC2 Interface Character:
-----------------------
The TC2 character is specific to T = 0 protocol.
conveys the work Waiting-Time Integer (WI). The WI determines the maximum interval between the leading
edge of the Start bit of any character sent by the smart card, and the leading edge of the Start bit of the previous
character sent either by the card or by the interfacing device.
TD2 Interface Character:
------------------------
The TD2 character has the same function as the TD1 character.
TA3 Interface Character:
-------------------------
The TA3 character conveys the Information Field Size Integer (IFSI) for the smart card, which is used for T = 1 (T1)
protocol. 
For an ATR not containing TA3, the interfacing device will assume a default value of 0x20.

TB3 Interface Character:
-----------------------
Its contains 
CWI(character waiting time Interger) for calculating CWT.
BWI(Block waiting time Integer) for  calculating BWT. Aplicabe only for T=1 T1.
TC3 Interface Character:
-----------------------
If present : Indecates that type of block error detection used (LRC/CRC).
If Absent  : Default LRC will be used.

TD3 Interface Character:
------------------------
The TD3 indicates the interface bytes similar to that of TD1 and TD2.

Historical Characters (T1,T2,...TK):
-----------------------------------
Optional- includes smard card manufacturar,vendor,product id.

Check Character (TCK):
---------------------
If only T = 0 is indicated, then TCK shall be absent. If T = 0 and T = 15 are present and in all the other cases,
TCK shall be present. When TCK is present, exclusive- ORing of all the bytes from T0 to TCK should result as
0x00. Any other value is invalid.

PPS REQUEST AND RESPONSE
========================
1: It consist  initial byte, PPSS.
The PPSS is followed by a format byte, PPS0, three optional parameter bytes, PPS1, PPS2,
PPS3, and a check byte, PCK, as the last byte. 
• PPSS is set to 0xFF
• In PPS0 byte, bit 8 is reserved for future use.
Each bit 5, bit 6 or bit 7 is set to ‘1’, to indicate the
presence of optional bytes, PPS1, PPS2 and
PPS3, respectively. Bit 4 to bit 1 encode the
protocol type T (i.e., T = 0 or T = 1 or T = 2, etc.)
• PPS1 is same as TA1
• PPS2 is same as first TB byte for T = 15
• PPS3 is reserved for future use
• Exclusive-ORing for all the bytes from PPSS to
PCK should result as 0x00
After ATR and PPS done command processing :
• T = 0 (asynchronous half-duplex character
transmission protocol)
• T= 1 (asynchronous half-duplex block
transmission protocol)
========================================================

ISO7816 Compatile Smart Card Stack Implementation
• Answer-to-Reset
• Information exchange
• T=0 or T=1 protocols data exchange


Configuring the USART does not require the USART clock to be enabled.
The ISO7816 specification defines the bit rate with the following formula:
B = (Di/Fi) × f

Where:
• B is the bit rate
• Di is the bit-rate adjustment factor
• Fi is the clock frequency division factor
• f is the ISO7816 clock frequency [Hz]


The possible values for Di, Fi and Fi/Di radio can be found in the UART datasheet.
Note: The clock selected in the Mode Register (US_MR) is first divided by the value programmed in the field Clock
Divider (CD) in the Baud Rate Generator Register (US_BRGR). The resulting clock provides the smart
card clock inputs.


Smart Card device configuration  :
================================
• Detection of the Smart Card insertion to the interface device (optional)
• Warm reset of the smart card by the Interface device
• Answer-to-Reset (ATR) response by the Smart Card to the interface device
• Protocol and Parameter Selection (PPS) exchange

Information exchange :
====================
• Execution of the commands between the Smart Card and the Interface device


Answer-to-Reset (ATR) :
======================
ATR is a series of characters responded to by the card after the successful reset operation. ATR characters determine
the initial communication parameters, bit timing, and the data transfer details between the card and the interfacing
device.
The ATR message, which less than 33 characters, consists of these fields:
• Initial byte (TS) (mandatory)
• Format byte (T0) (mandatory)
• Interface bytes (TAi,TBi,TCi and TDi) (optional)
• Historical bytes (T1,T2,...TK) (optional)
• Check bytes (TCK) (conditional)

The TS is used to establish the bit-signaling and bit-ordering conventions between the interface device and the card. TS
have two possible patterns which should both be supported by the interface device.
The T0 byte is used to signal the presence or absence of subsequent interface bytes or Historical bytes. Interface bytes
are used to tailor the characteristics of the I/O channel, including the specific protocol used by the card and interface
device during subsequent exchange of commands and responses.
The Historical bytes, if present, are used to convey manufacturer information. Actually, there is no established standard
for the information presented in the Historical bytes


PPS request and response:
========================
The PPS request and response consists of an initial byte, PPSS, followed by a format byte PPS0, three optional
parameter bytes, PPS1, PPS2, PPS3, and a check byte PCK as the last byte.
• PPSS identifies the PPS request or response and is set to 0xFF
• In PPS0 byte, bit 8 is reserved for future use and set to 0. Each bit 5, bit 6, or bit 7 set to ‘1’ indicate the
presence of an optional byte, PPS1, PPS2, PPS3, respectively. Bit 4 to bit 1 encode the protocol type T to
propose a transmission protocol. (i.e., T = 0 or T = 1 or T = 2, etc.)
• PPS1 is encoded in the same way as in TA1 (Interface byte)
• PPS2 is encoded in the same way as the first TB byte for T = 15
• PPS3 is reserved for future use
• Exclusive-ORing all the bytes PPSS to PCK inclusive shall result as 0x00, any other value is invalid
In the most common cases, the PPS response is identical to the PPS request.

Smart card data exchange:
=========================
After the ATR and PPS exchange between the card and the interfacing device, the next step is to execute the
command(s) between the card and the interfacing device.
Currently, there are two protocols, which are widely used for Smart Card communication:
• T = 0 (asynchronous half-duplex character transmission protocol)
• T = 1 (asynchronous half-duplex block transmission protocol


Application protocol data unit :
===============================
The Smart Card performs the requested operations and communicates the result as a response from the Smart Card.
This command response message pair is known as an Application Protocol Data Unit (APDU).


Command APDU structure.
--------------------------------------------------------------------
| Common header  | Common body |
--------------------------------------------------------------------
| CLA INS P1 P2  | [Lc Field] [Data Field] [Le Field]  |
--------------------------------------------------------------------

The body of the APDU can take four different forms:
• Case 1: No data is transferred to or from the card, so the APDU only contains the header
• Case 2: No data is transferred to the card, but data is returned from the card. The body of the APDU only
contains a non-null Le field
• Case 3: Data is transferred to the card, but none is returned from it. The body of the APDU includes the Lc and
data fields
• Case 4: Data is transferred to the card and is also returned from the card as a result of the command. The
body of the APDU includes the Lc, data and Le fields


Response APDU structure.
----------------------
_______________________________
| Body  Trailer   |
-------------------------------
| Data field  SW1 | SW2  |
-------------------------------

defines the response APDU structure. 
- It consists of a body and a trailer. 
- The body is either null or it includes a data field – depending on the specific command. 
- The length of the data field is determined by the Le field in the corresponding command APDU.
- The trailer consists of up to two fields of status bytes called SW1 and SW2. 
- These fields return a status code in which one byte is used to specify an error category and the other is used to specify a
command-specific status or error indication.


T=0 protocol :
=============

The T=0 protocol is a byte-oriented protocol where a character is transmitted across the channel between the interface
device and the card.
Error handling is performed on each byte by looking at the parity bit. If the actual parity bit does not correspond to the
parity of the transmitted data, an error must have occurred. 
In the T=0 protocol, the receiving side signals require the byte to be retransmitted in case of detecting a parity error. This is done by holding the I/O line low (normally the I/O line is set high before the transfer of a byte).
When the transmitting side detects the I/O line low, it resends the byte that was not received by the receiving side correctly.

The APDU for T=0 protocol consists of two distinct structures:
• A command sent from the interface device to the card
• A response sent from the card to the interface device
The command header includes the following five fields; the length of each field is one byte:
• CLA: class designation of the command set to establish a collection of instructions
• INS: specifies a specific instruction from within the set of instructions
• P1: used to specify the addressing used by the [CLA, INS] instruction
• P2: also used to specify the addressing used by the [CLA, INS] instruction
• P3: specifies the number of data bytes transferred to or from the card as part of the [CLA, INS] instruction
execution
Each value of CLA defines an application-specific set of instructions.


T=1 protocol :
=============
The T=1 protocol is a block-oriented protocol where a block is transmitted across the channel between the interface
device and the card. A block is a byte string conveyed in asynchronous characters.
  

• An information block (I-block) is used to convey information for use by the application layer. In addition, it
conveys a positive or negative acknowledgmen

• A receive ready block (R-block) is used to convey a positive or negative acknowledgment. Its information field
shall be absent
• A supervisory block (S-block) is used to exchange control information between the interface device and the
card. Its information field may be present depending on its controlling function


Block frame structure.

Prologue field   Information field Epilogue filed
Node address | Protocol control byte | Data length   Optional (INF) Error detection (EDC)
1 byte  1 byte 1 byte       0 ~ 254 Bytes 1 or 2 Bytes
defines the block frame structure. The three fields involved in the block frames are:
• Prologue field
• Information field
• Epilogue field


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
By the end of activation 
- RST in state L, 
- VCC powered, 
- I/O in reception mode in the interface device, 
- CLK provided with a suitable and stable clock signal)

- The clock signal is applied to CLK at time Ta.
Ta<= 200/f.
The card shall set I/O to state H within
200 clock cycles (delay ta) after the clock signal is applied to CLK (at time Ta + ta).

- The interface device shall ignore the state on I/O while RST is at state L.

- Tb>= 400/f ,At Tb RST is put at high.
The answer on I/O shall begin between 400 and 40 000 clock cycles (delay
tc) after the rising edge of the signal on RST (at time Tb + tc).
- If Atr does not contain Class Indicator then for Card does not operate.
Interface device should perform deactivation after 10Ms delay.
-TBD

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Very good Resource is available 



Post a Comment

2 Comments