Silicon Laboratories EFM32 Application Note

...the world's most energy friendly microcontrollers

EFM32 USB Smart Card Reader

AN0820 - Application Note
Introduction
This application note gives an overview of the ISO/IEC-7816 smart card standard and a short introduction to the communication and protocol of a smart card. Along with this document there is a working example of a smart card-reader, implemented using the EFM32. The included software example implements a USB-CCID card reader device using the STK3700 Giant Gecko Starter Kit. The software example is developed and tested with an ACS (Advanced Card Systems Ltd.) ACOS-1/3 type of smart card.
This application note includes:
• This PDF document
• Source files (zip)
• Example C-code
• Multiple IDE projects
...the world's most energy friendly microcontrollers

1 Smart Card Introduction

A smart card, chip card or integrated circuit card (ICC) is a device that includes an embedded integrated circuit chip that can be either a secure microcontroller or equivalent intelligence with internal memory or a memory chip alone. The card is connected to a reader via direct physical contact or via a remote contactless RF interface. With the usage of an embedded microcontroller, smart cards have the ability to store large amounts of data, carry out their own on card functions (such as encryption and mutual authentication) and interact intelligently with a smart card reader.
Smart card technology conforms to international standards (ISO 7816 and ISO 14443) and is available in a variety of form factors, including plastic cards, fobs, SIM cards used in GSM mobile phones, and USB tokens.
Smart cards are used in many applications worldwide, including:
• Secure ID - employee ID badges, citizen ID documents, electronic passports, driver's licenses, online authentication devices
• Healthcare - citizen health ID cards, physician ID cards, portable medical records cards
• Payment - contact and contactless credit/debit cards, transit payment cards
• Telecom - GSM Subscriber Identity Modules, pay telephone payment cards
The scope of this application note is to give an overview of the different parts of the ISO 7816 standard, governing physical contact smart cards themselves and smart card interface devices or readers. The EFM32 microcontroller is used to implement a USB-enabled smart card reader and the included software example utilizes the EFM32 USART's 7816 smart card mode for automatic parity generation/check and ACK/NACK generation.
2014-01-23 - an0820_Rev1.00 2
www.silabs.com
...the world's most energy friendly microcontrollers

2 The ISO 7816 Standard

ISO/IEC 7816, (only referred to as ISO 7816 from here on) is an international standard related to electronic identification cards with contacts, especially smart cards, managed jointly by the International Organization for Standardization (ISO) and the International Electro-technical Commission (IEC). The full standard is available through www.iso.org, (See Reference 4 (p. 20) ).

2.1 Different Parts of the 7816-Standard

The standard currently has 15 parts (or levels), but only 2 of them, part 3 and part 4, are relevant for this application note. The first 5 parts of the standard are listed here:
• 7816-1: Physical characteristics. Describes the physical characteristics of integrated circuit cards, from environment exposure limits to
degree of bending or flexing; important for manufacturers in their choice of materials and establishing a process that embeds the integrated circuit into the card
• 7816-2: Cards with contacts - Dimensions and location of the contacts. The following table contains the contact definition of the electrical contacts, according to ISO7816-2:
Table 2.1. Smart Card Electrical Connection
Contact Designation Use
C1 V
C2 RST Reset line (from reader to card) C3 CLK Clock signal line (from reader to card) C4 RFU Reserved for future use C5 GND Common ground of contact reader and card C6 V
C7 I/O Input/output data line that provides a half-duplex communication
C8 RFU Reserved for future use
cc
pp
Power connection through which operating power is supplied to the microprocessor chip in the card (typically a reader must support up to 5V, max 200mA draw, but the device implemented in this example only supports 3.3V since that is the operating voltage of the EFM32 on the STK3700 starter kit.)
Higher voltage used to program EEPROM of older chips. Usually Not used.
channel between the reader and the smart card
Figure 2.1. Smart Card Physical Connections
Figure 2.1 (p. 3) shows a typical pad layout of the contacts on the smart card.
2014-01-23 - an0820_Rev1.00 3
www.silabs.com
...the world's most energy friendly microcontrollers
• 7816-3: Cards with contacts - Electrical interface and transmission protocols. Describes the electrical levels and waveforms, as well as the physical layer of the communication
protocol. This part includes specification of clock generation, reset sequence, baud rate, frame and parity check as well as negotiation of parameters related to the communication.
• 7816-4: Organization, security and commands for interchange. It specifies the contents of command-response pairs exchanged at the interface, means of retrieval
of data elements and data objects in the card, applications and data in the card, access methods to files and data in the card including a security architecture.
• 7816-5: Registration of application providers. Defines how to use an application identifier to ascertain the presence of and/or perform the retrieval
of an application in a card. ISO/IEC 7816-5:2004 shows how to grant the uniqueness of application identifiers.
The rest of the 7816 standard deals with security protocols, card management and life-cycle management, cryptographic considerations and also newer types of smart card interfaces, including synchronous communication and USB-enabled cards.
Since a USB CCID device operates as a bridge, relaying packets between host computer and smart card, this application note will mainly relate to the electrical waveforms and transportation of messages described in part 3 and 4 of the standard. These parts are discussed in detail in the following sections.

2.2 Part 3; Electrical Signal and Transmission Protocol

Communication with the smart card takes place over a single bi-directional, half-duplex data line. The voltage levels for high / low states is the same as the supply voltage to the smart card. The direction of communication is assumed to be known a-priori by both master (reader) and slave (smart card). Parameters such as protocol format, type of smart card, baud rate and other electrical parameters are read out of the smart card right after reset. This is known as the Answer to Reset or ATR. Based on the information given in the ATR, the reader can adapt to the transmission format supported by the card.

2.2.1 Electrical Connection of the Bi-directional Data Interface

Since the data communication is bi-directional, the terminal is often configured to transmit data through an open-drain output. This means that it can pull the data line low, but it needs a pull-up resistor to go high again. And in the same way, the smart card can pull the line low, but needs the pull-up to take the line high again. Some interface-devices use more clever ways of doing this to achieve faster rise-times, but for the EFM32 implementation of the bi-directional data line the following configuration is used:
Figure 2.2. Bi-directional Data Line with Pull-Up Resistor
Smart
R
pull
Smart
Card
Card
Terminal
2014-01-23 - an0820_Rev1.00 4
Data- line
www.silabs.com
...the world's most energy friendly microcontrollers
The pull resistor used in the EFM32-implementation is the internal pull-up in the EFM32's GPIO pin.

2.2.2 Power Up, Clock and Reset

When a card is inserted into the reader, all the contacts are disconnected from the terminal/reader, including ground. The reason for this is that the contacts in the terminal slide over the smart card contacts while the card is inserted. Applying power to the wrong pins could in theory damage the card or the reader. When the card is properly inserted, there is usually an electrical contact that breaks connection, indicating to the reader that the card is fully inserted. Now the reader initiates a power on and reset sequence.
The interface is responsible for the timing of the sequence. The most important parameters are the clock­cycle count before and after reset is released. The interface should enable the clock and keep it running for 40k cycles before reset is released. After reset, the card must respond with its ATR within a window of 400-40000 clock cycles. The timing is illustrated in Figure 2.3 (p. 5)
Figure 2.3. Answer to Reset Sequence
Vcc
Clk
Reset
IO
Card
Inserted
Power
enabled, IO-
pin pulled high
Clock
Started
Clock Running
Wait 40 000 clock cycles
Reset
Released
ATR
Received
ATR-Data
< 40 000
clock cycles

2.2.3 Byte Transfers

During the power on and reset sequence, the smart card answers with its "Answer to Reset". The transmission of this first data-packet is carried out with default communication parameters listed below:
• Baud rate = F
• 8 data bits.
• 1 start bit.
• 1 parity bit, even.
• 1.5 stop bits (guard period for Ack/Nack).
• Coding of 0's and 1's: This can vary, the correct naming of this is: "Direct" or "Inverse" convention. It is based on the very first character transmitted, called TS. "Direct" indicated by TS=HLHHLHHHLLH (H = Vdd, L = GND) means that the following data is transferred as 0=GND, 1=Vdd and with the least significant bit first. For the Inverse convention, indicated by TS=HLHHLLLLLLH, data is transmitted with most significant bit first and 0=Vdd, 1=GND.
/372. Clock should be approximately 3.57 MHz, resulting in 9.6 kBaud/s.
clock
Using the above parameters, the interface must also adhere to the Ack/Nack of bytes, based on the parity bit. A Nack is indicated when the receiver pulls the data line low in the guard period after the parity bit is transmitted. If this happens, the transmitter should immediately retransmit the last byte.
One bit-period is called an "Elementary Time Unit" or ETU, this is used throughout the 7816 standard as a basis for defining the timing of the protocol. An illustration of one transmitted byte is given in Figure 2.4 (p.
6) .
2014-01-23 - an0820_Rev1.00 5
www.silabs.com
...the world's most energy friendly microcontrollers
Figure 2.4. Byte Transmission, Parity and Elementary Time Unit
Line pulled
low in case
of error
Start bit
8 bit Frame Parity bit
S 1 2 3 4 5 6 7 8 P Err
ETU
Guard Period
Elementary
Time Unit
Note that this byte-format with parity error detection is always used during the Answer to Reset, but only for further communication if the selected protocol is "T=0" (based on what is indicated in the ATR). What T=0 protocol means and why it is called that is explained in Section 2.3 (p. 6) .

2.2.4 Answer to Reset

The Answer to Reset is a string of 33 or fewer characters (bytes). With naming as defined in ISO 7816-3, the ATR consists of the following mandatory and optional characters:
• TS - a mandatory initial character, conveying byte-format, (the direct or inverse convention).
• T0 - a mandatory format character, indicating the contents of the ATR.
• TAi, TBi, TCi, TDi - optional interface characters, these characters indicate supported clock speeds, voltage levels and protocols supported by the card. Since they are optional, if not given, the card reader must continue with the default protocol used during the ATR sequence.
• T1, T2, TK - optional historical characters, these characters typically hold information about the card manufacturer, type of card (size etc.), version number and the state of the card.
• TCK - a conditional check character, presence of this byte is indicated by the optional interface characters, if present, it is exclusive OR of all the bytes in the ATR, excluding TS and TCK.
This document will not go further into the meaning of these characters, please refer to the ISO 7816-3 Standard for more information, (Reference 1 (p. 20) ).

2.3 Part 4; Commands and Exchange of Data Packets

The previous section described how the first basic communication channel is established between the card and the reader. The channel is a half-duplex, physical channel. Part 4 of the ISO 7816 standard defines the link-level protocols on top of this physical channel. The link-level protocol provides an error­free communication channel for the application-level protocol. This section describes the message structure consisting of application protocol data units (APDUs), which are exchanged between the reader application and the smart card application by the link-level protocol. Since only one type of the link-level protocol is supported by the EFM32-reader-implementation (T=0), this will be described in more detail than the others.
2014-01-23 - an0820_Rev1.00 6
www.silabs.com
...the world's most energy friendly microcontrollers
Part 4 of the 7816 standard also defines a file system API to manipulate files and a security service API allowing smart card and reader to mutually authenticate with each other. This is not discussed further in this application note.

2.3.1 Protocol Data Units

The data units transmitted in the link-layer protocol is called transmission protocol data units (TPDUs). These are the packets of data going between the reader and the smart card. On top of these are the APDUs, which comes directly from the application layer of the protocol. The APDUs are usually transmitted unaltered by the smart card interface device, but this depends on the level of automatism supported by the reader. This naming convention is illustrated in Figure 2.5 (p. 7) .
Figure 2.5. Protocol Data Units and Where they Apply
Host Application
Smart Card
Application Protocol
Data Units
(APDU)
Smart Card
Reader
Transmission Protocol
Data Units
(TPDU)
CPU
APDU
Physical/Link-
Layer

2.3.2 T=0 Protocol Introduction

The link-layer protocol is usually one of two variants, either a byte-oriented protocol with error handling performed on each byte by using a parity bit, or a block-oriented protocol with checksum-based error detection. The first one, the byte-oriented protocol is called "T=0". The name comes from the indication of this protocol in the ATR by having one of the bits in one of the T-characters equal to 0. The block­oriented protocol is called "T=1". In fact there is even a T=2 protocol for full-duplex interfaces, with T=3-15 reserved for future protocols.
The T=0 protocol re-uses the same byte-transmission format used during the ATR-sequence. The parity error detection and retransmission at byte-level is also re-used. The TPDU-messages exchanged between reader and smart card has two distinct structures, based on the direction of communication:
• A command, this is sent from reader to smart card. Consists of at least 5 characters named: CLA, INS, P1, P2, P3, in addition comes a number of data bytes if writing to the smart card.
• CLA: Class designation of the command set to establish a collection of instructions.
• INS: The INS byte is used to identify a specific instruction within a class of instructions identified
by the CLA value.
• 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.
• A response, this is sent from smart card to reader. Consists of 1 or 2 status characters named SW1 and SW2, in addition comes a number of data bytes if reading from the smart card. The SW1 byte is also called "procedure-byte" if it is just an ACK or NULL character, indicating that the card is not finished with the transaction.
2014-01-23 - an0820_Rev1.00 7
www.silabs.com
...the world's most energy friendly microcontrollers
• SW1: Status response of the current command. 0x90 means success for example. If this byte is
equal to for instance 0x60=NULL or the INS sent in the command, it means the card need more time to process the command or that it has more data to receive/send. This character is then called a procedure byte.
• SW2: (optional) also conveys a status response to the reader
The flow of communication is handled by the reader depending on the first SW1 character of the smart card-response. All transfers are initiated by the reader and followed by a response from the smart card. In some cases, depending on SW1, the reader should wait for further bytes, in other cases it should forward the SW1 and possibly SW2 characters and any read data bytes to the host application.
In the case of SW1 indicating that the reader should wait for further data or the smart card needs more processing time, the SW1 is called a "procedure byte", indicating that it should not be forwarded to the host application as a response. Figure 2.6 (p. 8) illustrates how transmission of a data packet to the smart card looks like, following the T=0 protocol. In fact, for the T=0 protocol, the bytes of the APDU directly overlays the bytes of the TPDU, this means that the APDU in Figure 2.6 (p. 8) simply consists of CLA, INS, P1, P2, P3 and DATA in that order. Further, the APDU going back to the application is simply SW1 and SW2.
Figure 2.6. Host Sends Data to Card
Smart Card
Terminal
7816- 3, T=0 Protocol
Smart Card
CLA INS P1 P2 P3
Procedure
byte
DATA
SW1 SW2
In the case of error on one byte using the T=0 protocol, the failing byte is retransmitted immediately by the reader, this is illustrated in Figure 2.7 (p. 9) . Since the error detection is a single parity bit, this will fail if there is an even number of bits flipped in the character transmitted. This is one of the limitations of the T=0 protocol.
2014-01-23 - an0820_Rev1.00 8
www.silabs.com
...the world's most energy friendly microcontrollers
Figure 2.7. Parity Error and Re-transmission for the T=0 Protocol
Smart Card
Terminal
7816- 3, T=0 Protocol
Parity
Error
CLA INS P1 P2 P3
Single Byte
Retransmitted
INS
Procedure
byte
Smart Card
DATA
SW1 SW2
For further explanation of CLA the (class byte), INS (instruction) and P1-3 (parameters) and their contents in the T=0 protocol, please see the 7816 standard, (Reference 4 (p. 20) ).

2.3.3 T=1 Protocol Introduction

The T=1 protocol is more complex than the T=0 protocol. The physical interface is still half-duplex and uses the same configuration as T=0. T=1 is actually built on top of T=0, but it does not use the T=0 error correction functionality, rather it uses a block oriented protocol. The main benefits of T=1 is the block-oriented error detection mechanism which provides much better error detection and correction capabilities than T=0. For more information about this protocol, please see Reference 2 (p. 20) .
2014-01-23 - an0820_Rev1.00 9
www.silabs.com
Loading...
+ 18 hidden pages