Back

ISO_7816

Loading views...

ISO 7816

Overview

ISO 7816 is the international standard for contact smart card communication, defining physical characteristics, electrical interfaces, and protocols (T=0 and T=1) used in SIM cards, EMV payment chips, ID cards, and health cards.

1. Theory & Fundamentals

  • Two protocols: T=0 (character-oriented, byte-by-byte) and T=1 (block-oriented)
  • Contacts: VCC (C1), RST (C2), CLK (C3), GND (C5), VPP (C6), I/O (C7)
  • Voltage classes: Class A=5V, Class B=3V, Class C=1.8V
  • Half-duplex: Single I/O line open-drain with pull-up
  • ATR (Answer To Reset): Card identifies itself on power-up
  • Default speed: 9600 bps; negotiable up to ~1 Mbps via PPS

2. Frame / Packet Structure

ATR: TS | T0 | [TA1 TB1 TC1 TD1...] | Historical bytes | TCK
  TS=0x3B: direct convention (MSB first, 0=low)
  TS=0x3F: inverse convention

APDU Command: CLA | INS | P1 | P2 | [Lc | Data] | [Le]
APDU Response: [Data] | SW1 | SW2
  SW1=0x90, SW2=0x00 → Success
  SW1=0x6X → Error/Warning

3. Protocol Mechanics

  • T=0: Each command byte acknowledged; null bytes (0x60) indicate processing
  • T=1: Block protocol; I-blocks carry data, R-blocks acknowledge, S-blocks supervise
  • Guard time: Minimum idle period between characters
  • PPS (Protocol Parameter Selection): Negotiate speed after ATR

4. Hardware Implementation

  • Smart card socket with spring contacts (ISO 7816 pinout)
  • CLK source: 3.5–4 MHz oscillator
  • I/O pull-up: 10kΩ to VCC
  • Transceiver ICs: SC16IS750, ZST6691
  • ESD protection on all lines

5. Register-Level / Configuration

void SmartCard_PowerOn(void) {
    VCC_Enable();  delay_ms(10);
    CLK_Enable();  delay_us(400);
    RST_High();    // Release reset
    // Read ATR from I/O line
}
void SmartCard_SendAPDU(uint8_t cla, uint8_t ins, uint8_t p1, uint8_t p2,
                         uint8_t *data, uint8_t lc, uint8_t le) {
    SC_Send(cla); SC_Send(ins); SC_Send(p1); SC_Send(p2);
    SC_Send(lc);
    for(int i=0;i<lc;i++) SC_Send(data[i]);
    SC_Send(le);
    // Receive SW1/SW2
}

6. Driver / Software Development

uint8_t SmartCard_GetATR(uint8_t *atr, uint8_t *len) {
    SmartCard_PowerOn();
    *len = 0;
    uint8_t ts = SC_ReceiveByte(); atr[(*len)++] = ts;
    uint8_t t0 = SC_ReceiveByte(); atr[(*len)++] = t0;
    // Parse T0 to determine how many more interface bytes follow
    return 1;
}

7. Debugging & Testing

  • Logic analyzer with ISO 7816 decode
  • Verify ATR byte sequence on power-up
  • SW1=0x6X codes: 0x67=wrong length, 0x6A=wrong params, 0x6D=unknown INS
  • Smart card test tools: PC/SC framework, pyscard library

8. Real-World Applications

  1. SIM cards (mobile phones)
  2. EMV payment cards (chip and PIN)
  3. National ID cards and ePassports
  4. Healthcare smart cards
  5. Physical access control tokens

9. Advanced Topics & Edge Cases

  • EMV: Full payment card transaction defined in EMV Book 1–4
  • Secure Messaging: Encrypted/MACed APDUs for sensitive operations
  • Logical Channels: Multiple simultaneous applications on one card
  • Contactless extension: ISO 14443 uses similar APDU model over RF

10. Standards & Variants

Protocol Description
T=0 Character-oriented, byte-by-byte
T=1 Block-oriented, better error recovery
ISO 14443 Contactless smart card
EMV Payment card standard on ISO 7816

💡 Practical Examples

Example 1: Read SIM ICCID

uint8_t select[] = {0xA0,0xA4,0x00,0x00,0x02,0x2F,0xE2}; // SELECT EF_ICCID
SmartCard_SendRaw(select,7);
uint8_t read[] = {0xA0,0xB0,0x00,0x00,0x0A}; // READ BINARY 10 bytes
SmartCard_SendRaw(read,5); // Returns ICCID

Example 2: EMV SELECT application

uint8_t aid[] = {0x00,0xA4,0x04,0x00,0x07,0xA0,0x00,0x00,0x00,0x03,0x10,0x10};
SmartCard_SendRaw(aid,12); // Select Visa application

Example 3: Verify PIN (T=0)

uint8_t verify[] = {0x00,0x20,0x00,0x01,0x04,0x31,0x32,0x33,0x34};
SmartCard_SendRaw(verify,9);

🧪 Practice Questions

Beginner

  1. What are the 6 contacts on an ISO 7816 smart card?
  2. What is ATR and what information does it contain?
  3. What does SW1=0x90, SW2=0x00 mean?
  4. What is an APDU?
  5. What is the difference between T=0 and T=1?

Intermediate

  1. Parse an ATR and determine supported protocol and voltage class.
  2. Implement a T=0 APDU exchange in C.
  3. How does PPS negotiation increase communication speed?
  4. Implement SELECT + READ BINARY for a SIM EF file.
  5. How does the inverse convention (TS=0x3F) affect bit ordering?

Advanced

  1. Implement a full T=1 block protocol state machine with EDC.
  2. Build an EMV card reader compliant with EMV Book 3 command flow.
  3. Design a smart card socket circuit for dual-voltage (3V/5V) cards.
  4. Implement secure messaging with DES/AES-encrypted APDUs.
  5. Debug ATR parsing failures across different card vendors.

Hands-on Projects

  1. SIM Reader: Read ICCID and IMSI from SIM card using ISO 7816 T=0.
  2. EMV Terminal: Implement basic EMV transaction flow with chip card.
  3. Smart Card Wallet: Store/retrieve encrypted data from JavaCard applet.

Checklist

  • [ ] Explain ISO 7816 contacts and their functions
  • [ ] Parse ATR bytes for protocol and interface byte info
  • [ ] Implement T=0 APDU exchange
  • [ ] Implement T=1 block protocol
  • [ ] Send SELECT, READ BINARY, VERIFY APDUs
  • [ ] Implement PPS negotiation
  • [ ] Handle all SW1/SW2 response codes
  • [ ] Interface with real SIM or payment card
  • [ ] Debug with logic analyzer
  • [ ] Implement secure messaging