Smart Card Reader Programming Guide
Smart Card Reader Programming Guide
April 1999
Introduction:
Why this manual
It is not easy for users to develop their application program according ISO-7816 standard, user
should know detail command format, computing cryptography .. etc. That is why Unitech
provide this manual and utility library to help user to develop application program by simple API.
This utility library also include functions for decryption and encryption. We hope it can speed up
programmer’s developing time.
Programmer requirement
Before you use this manual, we presume that you are good at or you have studied below 3 items
1. C programming
2. MR350MKII programming
3. MPCOS-EMV
2
Demo disk
All necessary files are included into demo disk, you can find it from sub-directory Smart Card of
DEMO disk. It includes
1. SAMPLE01.c – sample program for issuing new Smart Card
2. SAMPLE02.c – sample program for reading / writing Smart Card
3. Function library
3.1 GEMSmart Card.LIB – C source code
3.2 GEMSmart Card.OBJ – object file
3.3 Smart CardINCL.h – C include file.
3.4 MAKE.BAT – batch file for compile and link user’s application
Notice :
Currently, MR350MKII only support T=0 and T=1 protocol and the function
library only provide GEMPLUS MPCOS-EMV format. If you need other format,
please contact us for further support.
3
Table of contents :
Introduction:...........................................................................................................................................2
4
3.18 F_SEL_ID ..................................................................................................................................... 27
3.19 F_SET_FKEY ............................................................................................................................... 28
3.20 F_SET_SC .................................................................................................................................... 29
3.21 F_UPD_BIN.................................................................................................................................. 30
3.22 F_UPD_REC................................................................................................................................. 31
3.23 F_VERIFY .................................................................................................................................... 32
3.24 F_WR_BIN ................................................................................................................................... 33
5
Chapter 1. System architecture
The following diagram shows the Smart Card transaction system structure.
Application
program
MR350MKII’s Smart Card reader can support T=0 and T=1 protocol. Users can directly use this
system function call to develop their application. Actually, system function only provides
primary command to access and control Smart Card reader. User should know detail control
register and data structure level when they directly use system function call to write their
application program.
Although, there are variety of smart cards on the market to provide a design reference, Unitech
develops an Smart Card library to provide all MPCOS-EMV command set, so user can directly
use those function libraries to develop their application program without knowing detailed low
level control register and data structure. Some of command sets (in our Smart Card function
library) are following ISO 7816 standard, so they are not dedicated for GEMPLUS
MPCOS-EMV. So, users can also use part of this Smart Card library to develop their application
program for other format card. In chapter 3, we will mark every function library to tell user
whether it is standard command or dedicated for GEMPLUS.
6
1.2 Relationship between Smart Card function Library and System function call
Below diagram show relationship between Smart Card function library and system function call.
The function call inside the black frame are only available for MPCOS-EMV card, other function
library follow ISO 7816 standard. In this diagram, F/W means MR350MKII system function call.
S_SEL_ID
F_SEL_ID
F_RD_BIN
F_WR_BIN
F_CHK_ICC F_OPEN_ICC F_CLOSE_ICC F_UPD_BIN
S_EX_APDU
F_EX_APDU
F/W
Card Card Card (AH=3)
7
1.3 Developing flow for user application program
Below diagram is a flow chart for developing Smart Card application program.
Exchange message
(AH=3)
Power off Smart Card This step is necessary before taking Smart
(AH=2) Card out, or the Smart Card may be
damaged.
According above diagram, user should follow this flow chart to develop their application program.
Step 1: There is a detector in Smart Card reader to check whether Smart Card is inserted into
reader. MR350MKII also provides function call to get its status.
Step 2: According ISO standard, Smart Card reader will not always supply power to VCC pin,
application program should execute a special function call to inform Smart Card reader to
provide power to Smart Card
Step 3: After Smart Card are inserted into reader and reader feeds VCC to this card, application can
access this card by system function call or function library.
Step 4: Before removing Smart Card from reader, application program should power off Smart
Card to avoid possibly damaging Smart Card when user inserts or removes it.
Step 5: This step is the same with step 1. Application program can use function call to know
whether Smart Card still stays in reader.
8
Chapter 2. System function call (Firmware function call):
Below 3 system function calls are implemented on the Firmware of MR350MKII. Those function
call only support short format and protocol are available for T=0 and T=1. All of the system
function calls are implemented on system interrupt vector 06H (INT 06H)
Entry Parameter :
INT 06H
AH = 0
Return value :
AL = 0 NO CARD PRESENT
1 Smart Card SHORT
2 Smart Card EXIST
If AL = 2 AH = 0 Smart Card isn’t power-on
1 Smart Card is already power-on
9
2.2 Power-on Smart Card
This function informs Smart Card reader to supply power to VCC pin on Smart card and it also
needs to send pointer of ATR (Answer to reset) structure to get its status
Entry Parameter
INT 06H
AH = 1 POWER ON Smart Card
ES:DI= Buffer of ATR structure
Output Value
AH = 0 Successfully power-on
If AH=0 CX = ATR length
1 Time-out ( 1 Sec ,RETRY=1 )
2 No card present
4 Data pin not stable
5 Parity error
10
2.3 Power off Smart Card
Entry parameter
INT 06H
AH = 2 ; POWER OFF Smart Card
Entry parameter
INT 06H
AH = 3 ;SEND COMMAND TO Smart Card
DS:SI= buffer ; pointer of exchange command buffer(output)
CW = length ; Length of message excahnge buffer
ES:DI=buffer ; pointer of return message (input)
Return value
AH = 0 ; SUCCESS
If AH=0 CX=length of return message
1 ; timeout and deactive Smart Card
2 ; No card present deactive Smart Card
3 ; EDC error
4 ; Data not stable
5 ; Parity check error
6 ; Other error & deactive Smart Card
11
Chapter 3. Function library
This chapter introduces whole function library by function name sequence. It includes function
description, input/output parameter and data structure. This C function library can be included into
user’s application program and please use LARGE MEMORY MODEL to compile it.
12
3.2 F_APD_REC
This function writes data and initializes a record EF. When secure messaging is used, the function
dose not accept this command of length greater than the communication buffer size(64 bytes).
Function call
void F_APD_REC( S_APD_REC* );
Input parameter :
Class : 0 = NORMAL
: 1 = SECURE
ShortId : short identifier if implicit select,0 for direct select.
RecLen : The length of record to be update.
RecData[] : data
13
3.3 F_CHK_ICC
This function is used to check Smart Card current stage.
Function call
void F_CHK_ICC( S_CHK_ICC* );
Input parameter :
No
Output parameter :.
ChkPower : currently power stage
; 0 = OFF
; 1 = ON
ChkPresent ; 0 = No card present
; 1 = card short
; 2 = card exist
3.4 F_CLOSE_ICC
This function closes previously opened session and power off the card.
Function call
void F_CLOSE_ICC( void );
14
3.5 F_CRT_DF
This function creates a dedicated File. User should use function F_INI_ACC
to load Access Condition before creates DF/EF.
Function call
void F_CRT_DF( S_CRT_DF* );
Input parameter :
Class : 0 = NORMAL
; 1 = SECURE
FileId[] : DF identifier
Opt : RFU
NameLen : 0--16
DFName[] : store DF name
Output parameter :
IdOk : 0 = OK 1 = Timeout 2 = No card
; 3 = EDC error 5 = Parity error 7 = see SW1-SW2
Sw12[] :SW1-SW2
CRYin[] :CRYcks 2,1,0 only for secure message
CRYout[] :CRYcks 7,6,5 only for secure message
15
3.6 F_CRT_EF
This function creates an Elementary File. User should use function F_INI_ACC
to load Access Condition before creates DF/EF.
Function call
void F_CRT_EF( S_CRT_EF* );
Input parameter :
Class : 0 = NORMAL
; 1 = SECURE
FileId[] : EF identifier
FDB : File descrirtion byte
RecLen : record length (for fix length structure only) 0:other
BodyLen : 0x0000--0xFFFF
16
3.7 F_ERR_MSG
Convert Smart Card reader return code to text description
Function call
void F_ERR_MSG( unsigned char* p_sw ,unsigned char* msg);
Input parameter :
P_sw : point to status word.
Output parameter :
Msg : string buffer of text description
17
3.8 F_EX_APDU
This function call exchanges APDU command with Smart Card and returns the card’s response.
Function call
void F_EX_APDU( S_EX_APDU* );
Input parameter :
InLen : holds the number of APDU byte to send to Smart Card.
InApdu[] ; APDU command to send to Smart Card.
18
3.9 F_INI_ACC
This function initializes the Access Conditions registers, which is pre-requisite for creation of EF or
DF. Before useing function “F_CRT_EF” or “F_CRT_DF”. User should initializes Access
Conditions registers. Access Conditions registers will reset to 0 when function “F_OPEN_ICC” or
“F_SEL_ID” is executed.
Compatible with ISO7816 or Gemplus command set
Gemplus
Function call
void F_INI_ACC( S_INI_ACC* );
Data structure of S_INI_ACC
typedef struct
{
int AccNum;
int Validity;
int KeyLevel;
unsigned char KeyId;
int Sc1Level;
int Sc1Num;
int Sc2Level;
int Sc2Num;
} S_INI_ACC;
Input parameter :
AccNum : 1~3 for access condition 1--3
Validity : 0 = unrestricted access 1 = SC1 protect
; 2 = SC1 & SC2 protect 3 = access never allowed
KeyLevel : 0 = EFkey is in grobal level 1 = local level
KeyId :KEY file short id
Sc1Level :first secret code level 0:grobal 1:local
Sc1Num :first secret code number from 0--7
Sc2Level :second secret code level 0:grobal 1:local
Sc2Num :second secret code number from 0--7
3.10 F_INI_APDU
Initialize structure S_EX_APDU.
Compatible with ISO7816 or Gemplus command set
No
Function call
void F_INI_APDU( S_EX_APDU* new );
19
3.11 F_LD_KEY
This function loads/updates a triple DES key into a key file.
Function call:
void F_LD_KEY( S_LD_KEY* );
Input parameter :
Class : 0 = NORMAL 1 = SECURE
Mode : 0 = direct select 1 = implicit select
ShortId : short EF identifier, This is needed if Mode=1.
KeyNum : key number 0,2,4,6 only.
KeyType : 0 = ADM 1 = pay 2 = Log
; 3 = Authen 16 = sign
KeyVer :key version for AP
KeyValue[] :holds key data,[0]=K15,[1]=K14,[15]=K0
20
3.12 F_LD_SC
This function loads a code into secret code file.
Input parameter :
Class :0 = NORMAL 1 = SECURE
Mode :0:direct select 1 = implicit select
ShortId :short EFsc identifier,This is needed if Mode=1.
CodeNum :code number 0-7 only.
CodeType :0 = plain mode 1 = cipher mode
CodeMPN :1~7
UcrLevel :0 = global 1 = local
UcrNum :unlock code number 0~7
CodeValue[] :holds secret code value
Output parameter :
IdOk : 0 = OK 1 = Timeout 2 = No card
; 3 = EDC error 5 = Parity error 7 = see SW1-SW2
Sw12[] :SW1-SW2
21
3.13 F_LIB_VER
This function is used to get library version .
Function call
void F_LIB_VER( int* main, int* extend );
Input parameter :
No
Output parameter :
Main : store main version of library.
Extend : store extend version of library.
22
3.14 F_OPEN_ICC
Reset Smart Card and get Answer To Reset. This function sets Access Condition 1,2 and 3 to zero.
Function call
void F_OPEN_ICC( S_OPEN_ICC* );
Input parameter :
Smart CardType :Reserve for Future Use.
Output parameter :
AtrReady :0=OK 1=Timeout 2=No card
;4=data not stable 5=Parity error
AtrLen :holds ATR length ,from TS to TCK.
AtrBuf[] :holds ATR char, at most 32 char (7816-3,6.1.4)
HistLen :holds the number of history char, from T1 to TK
HistOffset :History char start from Atr_buf[Hist_offset]
23
3.15 F_RD_BIN
This function is used to read a continuous block of bytes in a transparent EF. When secure
messaging is used the function dose not accept the Read command of length greater than the
communication buffer size(64 bytes).
Compatible with ISO7816 or Gemplus command set
IS7816 if class = normal
Genplus if class = secure
Function call
void F_RD_BIN( S_RD_BIN* );
Data structure of S_RD_BIN
typedef struct
{
int Class;
int Mode;
unsigned char ShortId;
int Offset;
int RbLenIn;
int IdOk;
unsigned char Sw12[2];
unsigned char Response[MaxDATA];
int RespLen;
unsigned char CRYin[3];
unsigned char CRYout[3];
} S_RD_BIN;
Input parameter :
Class ; 0=NORMAL 1=SECURE
Mode :0=direct select 1=implicit select
ShortId :short EF identifier, This is needed if Mode=1.
Offset :0000-7FFF if Mode=0, 00-FF if Mode=1.
RbLenIn :00-7F,data length to be read.
Output parameter :.
IdOk : 0 = OK 1 = Timeout 2 = No card
; 3 = EDC error 5 = Parity error 7 = see SW1-SW2
Sw12[] :SW1-SW2
Response[] :storage for output data
RespLen :length of out data
CRYin[] :Reserve
CRYout[] :CRYcks 7,6,5 only for secure message
24
3.16 F_RD_REC
This function is used to read data from a record EF. When secure messaging is used this function
dose not accept this command of length greater than the communication buffer size(64 bytes).
Input parameter :
Class :0:NORMAL 1:SECURE
RecNumber :0 for current record of cyclic record file
ShortId :short identifier if implcit select,0 for direct select.
ExpLen :data length to be read.
Output parameter :.
IdOk : 0 = OK 1 = Timeout 2 = No card
; 3 = EDC error 5 = Parity error 7 = see SW1-SW2
Sw12[] :SW1-SW2
Response[] :storage for output data
RespLen :length of out data
CRYin[] :Reserve
CRYout[] :CRYcks 7,6,5 only for secure message
25
3.17 F_SEL_FKEY
Computes a temporary administration key using a specified key. The temporary administration key
is used for secure messaging. Only one temporary key is allowed at a given time.
Function call
void F_SEL_FKEY( S_SEL_FKEY* );
Input parameter :
KeyNum :key number (0,2,4,6 is accept)
KeyLevel :0:key is in grobal level 1:local level
KeyId :KEY file short id
TerRand[] :store terminal random data
26
3.18 F_SEL_ID
This function allows you to select the Master File, a Dedicated File, or an Elementary File using its
Identifier. The Get Response command is chained to retrieve the file descriptor. This function will
set access condition register 1,2 and 3 to zero.
Function call
void F_SEL_ID( S_SEL_ID* );
Input parameter :
Type :0:MF/EF 1:DF(if MF is selected) 2:EF(under current DF)
IdRef[] :file identifier
27
3.19 F_SET_FKEY
This function is used to set temporary session key which has been established by function
“F_SEL_FKEY”.
Function call
void F_SET_FKEY( S_SEL_FKEY* GG ,unsigned char* key )
Input parameter :
GG : a structure pointer and it is previously returned by function
“F_SEL_FKEY”.
key : point to a specified key which you want to Computes a temporary
administration key
28
3.20 F_SET_SC
This function unlocks/changes a secret code in the secret code file under the currently selected DF.
There is no limit to the number of times that a secret code can be unlocked/changed.
Function call
void F_SET_SC( S_SET_SC* );
Input parameter :
Class :0:NORMAL 1:SECURE
Mode :0:change secret code 1:unlock code
CodeNum :code number 0-7 only.
ChkValue[] :holds Previous/unlock code
NewValue[] :holds new code value
29
3.21 F_UPD_BIN
This function updates data in a transparent EF. When secure messaging is used the function
dose not accept this command of length greater than the communication buffer size(64 bytes).
Input parameter :
Class :0:NORMAL 1:SECURE
Mode :0:direct select 1:implicit select
ShortId :short EF identifier,This is needed if Mode=1.
Offset :0000-7FFF if Mode=0, 00-FF if Mode=1.
UpdateLen :00-7F,data length to be read.
UpdData[] :holds data to replace exist file contents.
30
3.22 F_UPD_REC
This function updates data to a record EF. When secure messaging is used the function dose not
accept this command of length greater than the communication buffer size(64 bytes).
Function call
void F_UPD_REC( S_UPD_REC* );
Input parameter :
Class :0:NORMAL 1:SECURE
RecNumber :0 for current record of cyclic record file
ShortId :short identifier if implcit select,0 for direct select.
LcLen :The length of record to be update.
RecData[] :data
Output parameter :
IdOk : 0 = OK 1 = Timeout 2 = No card
; 3 = EDC error 5 = Parity error 7 = see SW1-SW2
Sw12[] :SW1-SW2
CRYin[] :CRYcks 2,1,0 only for secure message
CRYout[] :CRYcks 7,6,5 only for secure message
31
3.23 F_VERIFY
This function presents a code to the card.
Function call
void F_VERIFY( S_VERIFY* );
Input parameter :
Class :0:NORMAL 1:SECURE
ScNumber :secret code number from 0 to 7
ScValue[] :secret code valure(8 byte)
32
3.24 F_WR_BIN
This function writes data to a transparent EF. When secure messaging is used the function dose not
accept this command of length greater than the communication buffer size(64 bytes).
Input parameter :
Class :0:NORMAL 1:SECURE
Mode :0:direct select 1:implicit select
ShortId :short EF identifier,This is needed if Mode=1.
Offset :0000-7FFF if Mode=0, 00-FF if Mode=1.
UpdateLen :00-7F,data length to be read.
UpdData[] :holds data to replace exist file contents.
33