Payment Cryptography, Hands-On
Module 4 · Moving keys between companies

Key blocks: TR-31, AKB and X9.143

Encrypting a key under a ZMK (Zone Master Key) hides it, but it doesn’t say what the key is for. Attackers exploited exactly that gap for decades. Key blocks close it: the key travels together with its permitted use, and a MAC (Message Authentication Code) glues them so neither can be changed.

By the end you can
  • explain the key-usage attack that key blocks prevent;
  • read any TR-31 header: version, usage, algorithm, mode, exportability;
  • unwrap a TR-31 block and verify it with the KCV;
  • place AKB, TR-31, X9.143 and TR-34 in history.

1. The problem: a key without a label

Suppose an HSM (Hardware Security Module) receives encrypt(ZMK, K) and the caller says “this is a data key”. In reality K is a PIN (Personal Identification Number) key. The HSM imports it as a data key, and the attacker asks it to decrypt data with K — handing them clear PIN blocks. HSM vendors patched this with key variants (XOR the KEK with a constant per key type before wrapping), but variants were proprietary and fragile.

The real fix: put the key’s attributes inside the protected structure.

2. History in four lines

  • AKB — Atalla Key Block: 8-byte header + 3DES-CBC encrypted key + MAC. The original.
  • TR-31 — the ANSI (American National Standards Institute) technical report that standardised the idea (versions A, B, C with 3DES; D with AES). PCI (Payment Card Industry) PIN requires key blocks for key exchange.
  • ANSI X9.143 — TR-31’s successor (2022); essentially the same format.
  • TR-34 — different problem: delivering a key using RSA (Rivest–Shamir–Adleman, a public-key algorithm) and certificates, no paper components (used for remote key loading and cloud HSMs).

3. Anatomy of a TR-31 key block

Header16 chars + optional blocks, in clear, ASCII
+
Encrypted key datalength ‖ key ‖ padding
+
MACover header and key data

The header is readable by anyone — that’s fine, it contains no secret. But because the MAC covers it, changing a single character (say, turning a PIN key into a data key) makes the block invalid.

For version B and D, the block protection key (KBPK — the ZMK, in a zone) is never used directly: two keys are derived from it with CMAC (Cipher-based MAC), one for encryption (KBEK) and one for the MAC (KBAK). The MAC doubles as the IV (Initialization Vector) of the CBC (Cipher Block Chaining) encryption.

The default block above is the worked example from the TR-31 specification itself. Press Unwrap, then Tamper and unwrap again.

4. The header fields that matter

PositionFieldValues you will meet
1VersionB 3DES with derived keys · D AES · A/C legacy variants
2–5LengthWhole block, in characters
6–7Key usageK0 KEK/ZMK · P0 PIN key · C0 CVK · E0 EMV MK-AC · E1 MK-SMC · E2 MK-SMI · E4 dynamic numbers · V2 PVV key · B0 BDK
8AlgorithmT 3DES · A AES
9Mode of useE encrypt · D decrypt · B both · C generate+verify · G generate · V verify · X derive keys · N no restriction
10–11Key version00 unless versioned
12ExportabilityE exportable under a trusted key · N never · S sensitive
13–14Optional blockscount; e.g. KS key set ID, KC KCV, PB padding
15–16Reserved00

Note mode X: issuer master keys (E0–E6) are used to derive card keys, not to encrypt anything directly.

5. Build your own

In real life When a network or bureau sends keys, you typically receive: the key block(s), the KCV (Key Check Value) of each key, and the name of the ZMK they’re under. Your HSM imports the block, checks the MAC, and you compare the KCV it reports with the partner’s letter. Mismatch → stop, don’t go live.

Exercises

Which two-character key usage code marks a card verification key (used for CVV/CVC)?

HintSee the table, positions 6–7.

Header D0144P0AE00N0000. Which statement is right?

  • An AES (Advanced Encryption Standard) PIN-encryption key, encrypt-only, that may never be exported
  • A 3DES (Triple DES) data key that can be exported
  • An exportable AES MAC key

Paste the spec example into the inspector and change position 6–7 from P0 to D0. Unwrap with the right KBPK (Key Block Protection Key). What happens?

  • MAC check fails — the block is rejected
  • It imports as a data key
  • It decrypts to a different key

An issuer master key for application cryptograms is used only to derive card keys. Which mode-of-use character should its header have?

HintTR-31 inspector: paste the block, put the KBPK, Unwrap. The KCV is shown.

Write the 16-character header (use 0000 for the length; the builder fills it) for: version B, a CVK (Card Verification Key), 3DES, used to both generate and verify, key version 00, exportable under a trusted key, no optional blocks.

HintVersion · 0000 · usage · algorithm · mode · version · exportability · 00 · 00.
SolutionB0000C0TC00E0000 — B, length, C0, T, C, 00, E, 00 optional blocks, 00 reserved.

You wrap the same CVK twice with the builder, same KBPK, same header. The two key blocks differ. Your colleague says the wrapping is broken. Who’s right, and how would you prove the key inside is the same?

  • You: random padding makes every wrap different; equal KCVs prove it’s the same key
  • Your colleague: a deterministic standard must give the same output