ST AN2598 APPLICATION NOTE

AN2598
Application note
Smartcard interface with the
STM32F10x microcontrollers
Introduction
This document describes a firmware and hardware Smartcard interface solution based on the STM32F10x USART peripheral. The main purpose of this firmware and hardware package is to provide resources that facilitate the development of an application using the USART peripheral in Smartcard mode.
The firmware interface consists of library source files developed so as to support the ISO 7816-3/4 specification. An application example based on STMicroelectronics STM3210B-EVAL, STM3210E-EVAL and STM3210C-EVAL evaluation boards is also provided.
This document and its associated firmware are available for download from the STMicroelectronics website: www.st.com.
Glossary
Low-density devices are STM32F101xx, STM32F102xx and STM32F103xx
microcontrollers where the Flash memory density ranges between 16 and 32 Kbytes.
Medium-density devices are STM32F101xx and STM32F103xx microcontrollers where the Flash memory density ranges between 32 and 128 Kbytes. Medium-density devices are implemented in the STMicroelectronics STM32F103xx evaluation board.
High-density devices are STM32F101xx and STM32F103xx microcontrollers where the Flash memory density ranges between 256 and 512 Kbytes. High-density devices are implemented in the STMicroelectronics STM3210E-EVAL evaluation board.
Connectivity line devices are STM32F105xx and STM32F107xx microcontrollers.
July 2009 Doc ID 13750 Rev 3 1/40
www.st.com
Contents AN2598

Contents

1 Smartcard interface description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2 External interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3 Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Smartcard clock generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Smartcard reader hardware connection . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 ISO 7816: protocol overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2 ISO 7816-2 – pin location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4 ISO 7816-3 – Electronic signal and transmission protocol . . . . . . . . . 12
4.1 Card power-up and reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2 Data transfer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.3 Answer to reset (ATR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 ISO 7816-4 – Smartcard commands . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.1 T0 protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.2 Application-level protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
5.2.1 The ISO 7816-4 APDU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5.2.2 File system API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.2.3 ISO 7816-4 functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.2.4 Security API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6 Smartcard interface library: description . . . . . . . . . . . . . . . . . . . . . . . . 25
6.1 File organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
6.2 Smartcard interface library functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
6.2.1 SC_Handler function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
6.2.2 SC_PowerCmd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
6.2.3 SC_Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
6.2.4 SC_ParityErrorHandler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
6.2.5 SC_PTSConfig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2/40 Doc ID 13750 Rev 3
AN2598 Contents
6.3 How to send APDU commands to the Smartcard . . . . . . . . . . . . . . . . . . 30
6.3.1 SC_GET_A2R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.3.2 SELECT_FILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.3.3 SC_GET_RESPONSE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
6.3.4 SC_READ_BINARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.3.5 SC_CREATE_FILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6.3.6 SC_UPDATE_BINARY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
6.3.7 SC_VERIFY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
6.4 Parity error management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.4.1 Data sent from card to reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6.4.2 Data sent from reader to card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
7 Smartcard interface example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
7.1 Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
7.1.1 Smartcard startup: answer to reset (A2R) . . . . . . . . . . . . . . . . . . . . . . . 35
7.1.2 Reading a file at a specified path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
7.1.3 Enabling/disabling the PIN1 (CHV1) code . . . . . . . . . . . . . . . . . . . . . . . 36
7.1.4 Verifying the PIN1 (CHV1) code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
8 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
9 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Doc ID 13750 Rev 3 3/40
List of tables AN2598

List of tables

Table 1. Smartcard pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Table 2. STM32F10xxx and Smartcard connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Table 3. Pin assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Table 4. Answer-to-reset structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Table 5. CLA instruction set definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Table 6. ISO 7816-4 INS codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Table 7. File library description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Table 8. Smartcard library functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Table 9. SC_Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Table 10. SCState . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Table 11. SC_PowerCmd . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Table 12. SC_Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Table 13. SC_ParityErrorHandler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Table 14. SC_PTSConfig . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Table 15. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4/40 Doc ID 13750 Rev 3
AN2598 List of figures

List of figures

Figure 1. ISO 7816-3 asynchronous protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Figure 2. Smartcard interface hardware connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Figure 3. Contact definitions for Smartcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Figure 4. Reader and card FSM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Figure 5. Answer to reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Figure 6. Application communication architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Figure 7. Command APDU structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Figure 8. Response APDU structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Figure 9. Smartcard file system architecture
Figure 10. State machine for smartcard operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Figure 11. Smartcard example: file system description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
(1)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Doc ID 13750 Rev 3 5/40
Smartcard interface description AN2598

1 Smartcard interface description

1.1 Introduction

The Smartcard interface is developed using the USART Smartcard mode. For the description of the USART registers, please refer to the STM32F10x reference manual. The USART Smartcard mode supports asynchronous protocol Smartcards as defined in the ISO 7816-3 standard.
With the Smartcard mode enabled, the USART must be configured as:
eight data bits plus parity
0.5 or 1.5 stop bits
A 5-bit prescaler and the Smartcard clock generator provide the clock to the Smartcard. GPIO pins in conjunction with software are used to provide the rest of the functions required to interface to the Smartcard.
The inverse signalling convention as defined in ISO 7816-3, inverted data and MSB first, is not handled in the software.

1.2 External interface

Table 1. Smartcard pins
STM32F10x pin Smartcard pin Function
USART CK CLK Smartcard clock
USART_TX IO IO serial data: open drain
Any GPIO RST Reset to card
Any GPIO V
Any GPIO V
The Smartcard_RST (Smartcard reset), Smartcard_3/5V (3 V or 5 V), Smartcard_CMDVCC (command for V GPIO bits of the I/O ports under software control. Programming the GPIO bits of the port for alternate function open-drain mode connects the USART_TX data signal to the Smartcard_IO pin with the correct driver type and the clock generator to the Smartcard_CLK pin configured in alternate function output push-pull.

1.3 Protocol

The ISO 7816-3 standard defines the bit times for the asynchronous protocol in terms of time units called ETUs (elementary time units), that are related to the clock frequency input to the card. The length of an ETU is a bit time. The USART transmitter output and receiver input are internally connected through the Rx_SW line. For the transmission of data from the STM32F10x to the Smartcard, the USART must be set up in Smartcard mode.
CC
PP
), and Smartcard_OFF signals (signal for card detection) are provided by
CC
Supply voltage
Programming voltage
6/40 Doc ID 13750 Rev 3
AN2598 Smartcard interface description
Sabcde fghP
Start bit
8 data bits
Parity bit
ETU
Line pulled low by receiver during stop in case of parity error
ai14612
Figure 1. ISO 7816-3 asynchronous protocol

1.4 Smartcard clock generator

The Smartcard clock generator provides a clock signal to the connected Smartcard. The Smartcard uses this clock to derive the baud rate clock for the serial I/O between the Smartcard and another USART. The clock is also used for the CPU in the card, if present.
Operation of the Smartcard interface requires that the clock rate to the card is adjusted while the CPU in the card is running code so that the baud rate can be changed or the performance of the card can be increased. The protocols that govern the negotiation of these clock rates and the altering of the clock rate are detailed in the ISO 7816-3 standard.
The clock is used as the CPU clock for the Smartcard, therefore updates to the microcontroller clock rate must be synchronized to the Smartcard clock, that is the clock high or low pulse widths must not be shorter than either the old or new programmed value.
Doc ID 13750 Rev 3 7/40
Smartcard reader hardware connection AN2598

2 Smartcard reader hardware connection

To interface to the Smartcard, the ST8024 device was used. The ST8024 is a complete low­cost, analog interface for asynchronous 3 V and 5 V Smartcards. It is placed between the Smartcard and the STM32F10x with few external components to perform all supply protection and control functions.
Table 2. STM32F10x and Smartcard connection
Smartcard
pins
C3: CLK USART3 CK: PB12 USART3 CK: PB12 USART3 CK: PD10
C7: IO USART3_TX: PB10 USART3_TX: PB10 USART3_TX: PD08
C2: RST PB.11 PB.11 PD.09 Reset to card: output push-pull
C1: V
CC
OFF PE.14 PC.07 PE.07 Smartcard detect: input floating
3/5V PD.11 PB.00 PC.00 3 V or 5 V: output push-pull
STM3210B-EVAL STM3210E-EVAL STM3210C-EVAL Function
Smartcard clock: alternate function push-pull
IO serial data: alternate function open drain
PE.07 PC.06 PD.07 Supply voltage: output push-pull
8/40 Doc ID 13750 Rev 3
AN2598 Smartcard reader hardware connection
Figure 2. Smartcard interface hardware connection
Doc ID 13750 Rev 3 9/40
ISO 7816: protocol overview AN2598
ISO 7816-2
Governs the dimension and location of the chip contacts
1-V
CC
2-RST
3-CLK
4-RFU RFU-8
I/O-7
VPP-6
GND-5
Only one location accepted by ISO since 1/1/93. Chip can still be on the front or back of the card.
ai14617

3 ISO 7816: protocol overview

3.1 Introduction

"ISO 7816: Identification cards -- Integrated circuit(s) cards with contacts" provides the basis to transition the relatively simple identification card from a token that can be compromised through forgery, theft, or loss into a tamper-resistant and "intelligent" integrated circuit card (ICC), more popularly known as a Smartcard. ISO 7816 includes at least six approved parts and has several additional parts under review:
Part 1: Physical characteristics
Part 2: Dimensions and location of the contacts
Part 3: Electrical interface and transmission protocols
Part 3: Amendment 2-Revision of protocol type selection
Part 4: Organization, security and commands for interchange
Part 5: Registration of application providers

3.2 ISO 7816-2 – pin location

ISO 7816-2 specifies an ICC with eight electrical contacts present in a standardized position on the front face of the card. These are referred to as C1 through C8. Some of these contacts are electrically connected to the microprocessor chip embedded within the card; some are not, having been defined to allow for enhancements but unused at the present time. Figure 3 shows the contact positions.
Figure 3. Contact definitions for Smartcards
10/40 Doc ID 13750 Rev 3
AN2598 ISO 7816: protocol overview
Table 3. Pin assignment
Pins Functions
C1 V
CC
C2 Reset
C3 Clock
C4 RFU
C5 GND
C6 V
PP
C7 I/O
C8 RFU
= 5 V or 3.3 V
Doc ID 13750 Rev 3 11/40

ISO 7816-3 – Electronic signal and transmission protocol AN2598

Reader state diagram
Card removal
Reader
"idle"
Card insertion
Powe r the cord
Protocol
negotiation
Protocol
fail
Card removal
No ATR
Idle
command
Set protocol
Wait for
response
Command
completion
wait
Partial response
Send APDU to card
Request command
completion
Receive full response
Powe r
off
Card reset
Prepare AT R
APDU
dispatch
idle
APDU
processing
Dispatch APDU to processor
Receive ATR
Issue ATR
Issue response to reader
Card state diagram
ai14618
4 ISO 7816-3 – Electronic signal and transmission
protocol
ISO 7816-3 begins to delve into the specification of the "intelligent" aspects of the Smartcard. This standard describes the relationship between the Smartcard and the reader as one of "slave" (the Smartcard) and "master" (the reader). Communications are established by the reader signaling to the Smartcard through the contacts noted previously and are continued by the Smartcard responding accordingly.
Communication between the card and reader proceed according to various state transitions illustrated in Figure 4.
Figure 4. Reader and card FSM
The communication channel is single-threaded; once the reader sends a command to the Smartcard, it blocks until a response is received.
12/40 Doc ID 13750 Rev 3
Loading...
+ 28 hidden pages