chushpan
Professional
- Messages
- 704
- Reaction score
- 466
- Points
- 63
What is APDU?
APDU (Application Protocol Data Unit) is a data format used to exchange information between a smart card and a terminal. APDU is the main interaction mechanism in card-based systems such as bank cards, SIM cards, electronic passports and other microchip-based devices.How does APDU work?
APDU is used to transmit commands from the terminal to the card and to receive responses from the card. Each command and response has a strict structure defined by ISO/IEC 7816 standards (for smart cards).Main types of APDU:
- Command APDU:
- Sent by the terminal to the card.
- Contains instructions for performing an operation (e.g. reading data, writing data, authentication).
- Response APDU:
- Sent by card in response to a command.
- Contains the result of the operation or the requested data.
Command APDU structure
The command APDU consists of several fields:- CLA (Class Byte):
- Specifies the class of command (e.g. standard command or application-specific).
- INS (Instruction Byte):
- Specifies a specific operation (e.g. SELECT, READ, WRITE).
- P1 и P2 (Parameter Bytes):
- Additional parameters for the command.
- Lc (Length of Command Data):
- Specifies the length of the data sent in the command (if any).
- Data:
- Data that is transmitted along with a command (for example, data to be written).
- Le (Length of Expected Response Data):
- Specifies how much data is expected in the response (if applicable).
Example of Command APDU structure:
Code:
CLA | INS | P1 | P2 | Lc | Data | Le
Structure Response APDU
The response APDU contains the result of the command execution:- Data:
- The requested information or result of the operation.
- SW1 и SW2 (Status Words):
- Two bytes indicating the execution status of the command.
- For example:
- 90 00 - successful completion.
- 6A 82 - file not found.
Example of Response APDU structure:
Code:
Data | SW1 | SW2
Example of using APDU
Scenario: Reading data from a card
- The terminal sends Command APDU:
Code:CLA = 0x00 INS = 0xB0 (READ BINARY) P1 = 0x00 P2 = 0x00 Le = 0x10 (expecting 16 bytes of data)
- The card processes the command:
- Reads 16 bytes of data from the specified address.
- The card sends a Response APDU:
Code:Data = 0x1234567890ABCDEF1234567890ABCDEF SW1 = 0x90 SW2 = 0x00 (successful completion)
Why do you need APDU?
- Standardization:
- APDU provides a universal format for interaction between terminals and cards, regardless of manufacturer.
- Safety:
- APDU commands can be encrypted or signed to protect data.
- Flexibility:
- Supports a wide range of operations, from simple data reading to complex cryptographic calculations.
APDU Command Examples
SELECT (application selection)
- Used to select an applet (application) on the card.
- Example:
Code:CLA = 0x00 INS = 0xA4 (SELECT) P1 = 0x04 P2 = 0x00 Lc = 0x07 Data = AID (application id)
READ BINARY (reading data)
- Used to read data from a file on the card.
- Example:
Code:CLA = 0x00 INS = 0xB0 (READ BINARY) P1 = 0x00 P2 = 0x00 Le = 0x10 (read 16 bytes)
VERIFY (PIN code verification)
- Used to verify the card owner's PIN code.
- Example:
Code:CLA = 0x00 INS = 0x20 (VERIFY) P1 = 0x00 P2 = 0x00 Lc = 0x04 Data = 1234 (PIN-code)
Conclusion
APDU is a key element of the smart card communication protocol. It provides a standardized way to send commands and receive responses, making it a universal tool for working with cards. Understanding the structure and operation of APDU is important for developers, testers, and security specialists.If you have additional questions about how APDU works or examples of its use, ask them!
Last edited by a moderator: