APDU Message Command-Response Pairs in Smart Card Communication

APDU Message Command-Response Pairs

In the world of smart card technology, communication between a card reader and a smart card relies on a specific messaging format known as the Application Protocol Data Unit (APDU). This structured exchange ensures that the reader can request specific actions and the card can provide accurate data or status updates in a standardized way.

The communication follows a strict command-response pattern: the reader sends a command APDU, and the card replies with a response APDU. Together, these form a complete transaction cycle.

Key Facts

  • Command APDUs are sent from the reader to the card.
  • Response APDUs are sent from the card back to the reader.
  • Every command must include a 4-byte header consisting of CLA, INS, P1, and P2.
  • Every response must conclude with two status bytes (SW1 and SW2).
  • Command data can range from 0 to 65,535 bytes.
  • Response data can range from 0 to 65,536 bytes.

The Command APDU Structure

A command APDU is the request sent by the reader. It is composed of a mandatory header, optional data, and an optional expectation of response length.

The Mandatory Header

The header consists of four bytes that define the nature of the request:

  • CLA (Instruction Class): A 1-byte field that indicates the type of command, such as whether it is a proprietary command or one following interindustry standards.
  • INS (Instruction Code): A 1-byte field that specifies the exact action to be performed, such as "select" or "write data".
  • P1 and P2 (Instruction Parameters): Two bytes used to provide additional parameters for the command, such as specifying a file offset for data writing.

Data and Length Fields

Following the header, the command may include data and length indicators:

  • Lc (Length of Command Data): This field encodes the number of bytes (Nc) of command data that follow. It can be 0 bytes (meaning Nc=0), 1 byte (for values 1 to 255), or 3 bytes (where the first byte must be 0, allowing for a range of 1 to 65,535).
  • Command Data: The actual payload of Nc bytes.
  • Le (Length of Expected Response): This field encodes the maximum number of response bytes (Ne) the reader expects. Depending on the presence of Lc, Le can be 0, 1, 2, or 3 bytes, supporting response sizes up to 65,536 bytes.
[ไม่มีภาพประกอบ]

The Response APDU Structure

Once the card processes the command, it returns a response APDU. This message is simpler in structure than the command but is critical for verifying the outcome of the operation.

The response consists of two primary parts: the Response Data and the Response Trailer. The response data contains up to Ne bytes of information requested by the reader. The trailer consists of two mandatory status bytes, SW1 and SW2, which indicate the processing status. For example, the hexadecimal value 90 00 typically indicates that the command was processed successfully.

[ไม่มีภาพประกอบ]

APDU Technical Summary

Comparison of Command and Response APDU Components
Component Command APDU (Reader → Card) Response APDU (Card → Reader)
Mandatory Header 4 Bytes (CLA, INS, P1, P2) None
Data Payload 0 to 65,535 bytes (Nc) 0 to 65,536 bytes (Nr)
Length Indicators Lc (Command) and Le (Expected) None
Mandatory Trailer None 2 Bytes (SW1, SW2)

Frequently Asked Questions

What is the purpose of the CLA byte in a command APDU?

The CLA (Instruction Class) byte tells the smart card the category of the command being sent, distinguishing between standard interindustry commands and proprietary ones.

How does the reader know if a command was successful?

The reader checks the two status bytes (SW1 and SW2) at the end of the response APDU. A value of 90 00 in hexadecimal generally signifies a successful operation.

What is the difference between Lc and Le?

Lc specifies the length of the data being sent to the card, while Le specifies the maximum length of the data the reader expects to receive from the card.

What is the maximum amount of data a response APDU can contain?

A response APDU can contain up to 65,536 bytes of data.

What do P1 and P2 represent?

P1 and P2 are instruction parameters that provide specific details for the INS code, such as an offset within a file where data should be written.