Cryptographic Key Generation, Derivation, and Management Utilities
Derived Unique Key Per Transaction (DUKPT) is a key management scheme used in financial applications where a unique key is derived for each transaction. It's commonly used in PIN pad devices and payment terminals to protect cardholder data.
| Bytes | Bits | Field | Description |
|---|---|---|---|
| 0-2 | 24 | IIN/BIN | Issuer Identification Number (Key Set ID high order) |
| 3-4 | 16 | Device ID | Unique device identifier (Key Set ID low order) |
| 5-9 | 40 | Transaction Counter | 21-bit counter (0x00000 to 0xFFFFF) + encryption register |
Master key stored securely in HSM
Derived: IPEK = 3DES(BDK, KSN[0:7])
Derived through BlackBox algorithm using transaction counter
XOR with key variant mask for specific use (PIN, MAC, Data)
| Usage | Variant Mask | Description |
|---|---|---|
| PIN Encryption | 0x00000000 00000000 00FF00FF 00FF00FF | Used for PIN block encryption |
| MAC Generation | 0x00000000 00000000 FF00FF00 FF00FF00 | Used for message authentication |
| Data Encryption | 0x00000000 00000000 FFFFFFFF FFFFFFFF | Used for data confidentiality |
TR-31 (also known as ANSI X9.143) is a standard for symmetric key exchange using a key block format. It provides a secure method to protect cryptographic keys during distribution and storage. TR-31 is widely used in payment systems for key injection and key exchange.
D[Length][Key Usage][Algorithm][Mode of Use][Key Version][Exportability][Opt Blocks Len][Reserved][Optional Blocks][Encrypted Key][MAC]
| Field | Length | Description |
|---|---|---|
| Version ID | 1 char | Key block version (A, B, C, D, E) |
| Key Block Length | 4 digits | Total length of key block in ASCII |
| Key Usage | 2 chars | Purpose of the key (P0, D0, M0, etc.) |
| Algorithm | 1 char | Cryptographic algorithm (T, A, D, R, E) |
| Mode of Use | 1 char | Permitted operations (E, D, B, G, V, etc.) |
| Key Version Number | 2 chars | Optional version identifier (00-99, hex) |
| Exportability | 1 char | Export restrictions (E, N, S) |
| Num Optional Blocks | 2 digits | Number of optional header blocks |
| Reserved | 2 chars | Reserved for future use (00) |
| Optional Blocks | Variable | TLV formatted optional data |
| Encrypted Key Data | Variable | The wrapped key (CBC encrypted, padded) |
| MAC | 8 bytes | Message Authentication Code (CMAC or HMAC) |
| Version | Key Encryption | MAC | Status |
|---|---|---|---|
| A | TDEA 2-key ECB | None | Legacy (deprecated) |
| B | TDEA 3-key ECB | TDEA CBC MAC | Legacy |
| C | TDEA 3-key ECB | TDEA CMAC | Legacy |
| D | AES Key Wrap (RFC 3394) | AES CMAC | Current (recommended) |
| E | AES Key Wrap | HMAC-SHA256 | Current |
A PIN block is a formatted and encrypted representation of a customer's Personal Identification Number (PIN). It protects the PIN during transmission and storage in payment systems.
Most Common Format - Used Worldwide
PIN Block = P || PIN || F... (padding)
PAN Block = 0000 || PAN[3:14]
Clear PIN Block = PIN Block XOR PAN Block
| Step | Description | Example (PIN=1234, PAN=4111111111111111) |
|---|---|---|
| 1 | Control Field + PIN Length + PIN + Padding | 0 4 1234 FFFFFFFFFF → 041234FFFFFFFFFF |
| 2 | Extract PAN[3:14] (12 digits, right-aligned) | 4111111111111111 → 000011111111111 |
| 3 | XOR PIN Block with PAN Block | 041234FFFFFFFFFF XOR 0000111111111111 |
| 4 | Result: Clear PIN Block | 041225EEEEEEEEEE |
Format Structure:
Enhanced Security - Random Padding
PIN Block = 1 || PIN Length || PIN || Random
PAN Block = 0000 || PAN[3:14]
Clear PIN Block = PIN Block XOR PAN Block
Key Differences from Format 0:
No PAN Required
PIN Block = 2 || PIN Length || PIN || Random
No XOR with PAN - Direct encryption
Characteristics:
EMV & Chip Cards
PIN Block = 3 || PIN Length || PIN || Random
PAN Block = 3333 || PAN[0:11]
Clear PIN Block = PIN Block XOR PAN Block
Key Features:
AES Encryption (128-bit blocks)
PIN Block = 4 || Reserved || PIN Length || PIN || Padding || MAC
16-byte block for AES encryption
Modern Standard:
| Format | Control | PAN Required | Padding | Usage |
|---|---|---|---|---|
| ISO-0 | 0 | Yes | 0xF | Most common, ATMs, POS |
| ISO-1 | 1 | Yes | Random | Enhanced security |
| ISO-2 | 2 | No | Random | Rarely used |
| ISO-3 | 3 | Yes | Random | EMV, chip cards |
| ISO-4 | 4 | Optional | Structured | AES, future standard |
Common cryptographic operations used in payment systems. These tools demonstrate encryption, encoding, and hashing algorithms.
| Algorithm | Type | Key Size | Security | Speed | Usage |
|---|---|---|---|---|---|
| DES | Symmetric | 56-bit | ❌ Broken | Fast | Legacy only |
| 3DES | Symmetric | 168-bit | ⚠️ Weak | Slow | Legacy payments |
| AES-128 | Symmetric | 128-bit | ✅ Strong | Very Fast | General purpose |
| AES-256 | Symmetric | 256-bit | ✅ Very Strong | Fast | High security |
| RSA-2048 | Asymmetric | 2048-bit | ✅ Strong | Slow | Key exchange, signatures |
| SHA-256 | Hash | N/A | ✅ Strong | Fast | Integrity, signatures |
Each byte in a DES key has odd parity (LSB is parity bit). When generating DES/3DES keys:
Key Check Value (KCV) is a cryptographic checksum used to verify that a key has been correctly received or entered. It's derived by encrypting a known plaintext (usually zero block) with the key and taking the first 3-6 bytes of the result.
| Method | Algorithm | Plaintext | Length |
|---|---|---|---|
| ANSI X9.24-1 | 3DES ECB | 0000000000000000 | 3 bytes (6 hex) |
| Visa CVV/CVC | 3DES ECB | 0000000000000000 | 6 bytes (12 hex) |
| CMAC (AES) | AES CMAC | 0000...0000 (16 bytes) | 4 bytes (8 hex) |
| Legacy DES | DES ECB | 0000000000000000 | 3 bytes (6 hex) |
| Key Type | Size | Usage | Standard |
|---|---|---|---|
| BDK | 16/24 bytes | DUKPT Base Derivation Key | ANSI X9.24-1 |
| IPEK | 16/24 bytes | Initial PIN Encryption Key | ANSI X9.24-1 |
| KEK | 16/24/32 bytes | Key Encryption Key | ANSI X9.24 |
| DEK | 16/24/32 bytes | Data Encryption Key | Various |
| MAC Key | 16/24 bytes | Message Authentication | ISO 9797 |
| AC Master Key | 16/24 bytes | EMV Application Cryptogram | EMV Book 2 |
| ZMK | 16 bytes | Zone Master Key | Legacy |
| ZPK | 16 bytes | Zone PIN Key | Legacy |
Retail Financial Services Symmetric Key Management Part 1: Using Symmetric Techniques
Interoperable Secure Key Exchange Key Block Specification
Requirements for secure PIN handling in payment systems
Security Requirements for Cryptographic Modules
A0 - Generate Key
A2 - Generate Key Component
BK - Generate DUKPT Key
M0 - Generate MAC
CA - Translate Key Scheme
EE - Encrypt PIN
DA - Decrypt PIN
KQ - Derive DUKPT Key