Skip to content

The HSM simulator

The HSM simulator answers payShield-compatible host commands, so a switch or issuer host can translate and verify PINs, check CVVs and MACs, and manage keys against it as it would against a real HSM. It’s also what simulated hosts use to check PINs and exchange keys. Every command it answers, its error codes and key types are in the HSM commands reference.

The HSM page shows the address to point your system at. The format is the payShield host command format:

  • a 2 byte length;
  • a header, 4 characters unless the install’s .env sets HSM_HEADER_LENGTH, that comes back unchanged;
  • a 2 character command code and its fields.

The response code advances the command code’s second character (NC is answered with ND, CA with CB), followed by a 2 digit error code. Commands on one connection are answered in order; open more connections for more in flight.

It listens on port 1500 over plain TCP, or TLS and mutual TLS when the install gives it a certificate (see the TLS walkthrough). Commands the simulator doesn’t offer are answered with error 68, so your system doesn’t wait for a timeout. The page lists the commands it offers.

A new install starts with the published Thales test LMK, so keys your system already holds under that LMK work as they are. The page marks it as public: anyone can decrypt keys under it, which is what a test LMK is for.

An admin can replace it with a random LMK, or one formed from components, by confirming the current LMK’s check value. Every stored key is re-encrypted under the new one in the same step. Keys your systems hold under the old LMK stop working, as they would on a real HSM. Every change is in the audit trail at the bottom of the page, without key material.

Add keys on the page: a name, a type (ZPK, ZMK, PVK, CVK, TAK and others), and how:

  • Generate it, at the length you choose.
  • Form it from clear components in hex, which are XORed together, to match a known key.
  • Import a TR-31 key block (ANSI X9.143), as keys are exchanged between organizations. Add the zone or terminal master key it was made under first (from its components), then paste the block and choose that key as the one protecting it. The block’s MAC is checked before anything is stored, and its usage must suit the key type: P0 for a ZPK or TPK, K0 or K1 for a ZMK or TMK, V0 to V2 for a PVK, C0 for a CVK, M0, M1 or M3 for a TAK or ZAK, B0 for a BDK, E0 to E4 for the EMV master keys, D0 for data keys. Versions A, B and C (TDES) are accepted; version D blocks need an AES key, which this key store doesn’t hold. In the classic key type table, TPK, TMK and PVK share one type code, as on a payShield, so a key stored as any of them can protect a key block.

The store keeps each key only as your system would, encrypted under the LMK, and shows its check value. Copy the encrypted value into your system’s configuration, or use the key from a simulated host by its name. The audit trail records where each key came from (generated, components, or a key block with its header and protecting key), never the key.

Issuers replace their CVKs, PVKs and MAC keys on a schedule, or after a suspected compromise, and keep the old key to verify cards made under it until they expire. To rehearse that, an editor chooses Rotate on a key:

  • a new random key of the same type and length takes the name, as its next version, with a new check value;
  • the old one is kept as a previous version, for as long as you choose (a day to five years, a year by default);
  • the page shows each key’s version and its previous versions, with their check values and when they retire.

From then on, the current version is the only one used to make anything: CVVs, PVVs, PIN blocks, the MACs a host signs, and exports. A previous version only verifies, and only after the current one has said no. A simulated host checking a CVV, a PIN or a MAC tries the previous versions newest first, so a card made before the rotation still gets through, and one that’s wrong under every version still fails. Live activity says when a previous version verified (“CVV verified with the previous CVK, version 1”), so you can see which cards and terminals still use the old key before it retires.

At its date, a previous version is retired: deleted from the store, checked hourly, and recorded in the audit trail. From then on, what only it verified fails, as at an issuer that has finished rotating. An admin can retire one early with Retire now. A key keeps up to five previous versions; a sixth rotation is refused until one retires. Replacing the LMK re-encrypts previous versions with the rest.

A host’s working ZPK (named “Host 1 working ZPK” and so on) isn’t rotated here: a key exchange from the host’s page replaces it, so both sides move together.

Rotation changes the key store, which simulated hosts and scenarios use by name. Your own systems hold their keys under the LMK themselves, so after a rotation give them the new one: copy its value under the LMK, or export it.

Export gives a key to a system that shares it, such as a switch after you rotate a ZAK, without it ever being in clear. An editor chooses how:

  • Under a ZMK, as the A8 command gives it: in the ANSI scheme for its length (Z, X or Y), under a ZMK in the store;
  • A TR-31 key block, version B, under a ZMK or TMK in the store. The block binds the key to a usage from the same list as imports; where a type allows several (M0, M1 or M3 for a MAC key), or several types share the key’s code, choose one. Its mode of use follows the usage, and its key version is the key’s.

Only the current version is exported, with its check value for the other side to confirm after importing it. Every export is in the audit trail with its format, the key that protected it and the block’s header, never the key.

Through the API: POST /api/hsm/keys/{name}/rotate with {"keep_days": 365} (editor), DELETE /api/hsm/keys/{name}/versions/{version} to retire one early (admin), and POST /api/hsm/keys/{name}/export with {"format": "tr31", "under": "Shared ZMK", "usage": "M3"} or {"format": "zmk", "under": "Shared ZMK"} (editor).

Live activity lists each command as it arrives: when, from where, the command and its response code, the result in words and how long it took. Keys, PIN blocks and card numbers are never shown. The counts at the top say how many commands came in, how many failed verification (01) and how many failed otherwise.

A real HSM is sometimes slow, busy or unreachable. To test what your switch does then (time out, retry, decline, reconnect), add Failure modes on the page. Each one has:

  • Commands it applies to, such as CA CC, or all when empty;
  • an Action:
    • answer as usual after a delay;
    • answer with an error code instead of running the command;
    • never answer, so your system times out (later commands on the connection are answered as usual);
    • close the connection;
  • a Delay in milliseconds, plus up to so much random extra delay. A delay also holds up the commands queued behind it on the connection, as on a slow HSM;
  • a Probability. Below 100% it becomes a failure rate, such as 1% of PIN translations failing with error 41.

They’re checked from top to bottom, and the first that matches and passes its roll applies. A mode that’s off is kept for later. Changes apply from the next command (on other HSM instances within seconds) and survive restarts. They affect every system connected to the HSM, so the page says when any are on, and activity marks each command one applied to.

Setting failure modes needs the editor role. Through the API: PUT /api/hsm/faults with {"faults": [{"name": "Busy HSM", "commands": ["CA"], "action": "respond", "delay_ms": 1500, "probability": 0.1}]}.

  • PIN checks: a stateful issuer verifies field 52 through EC or EA, answering 55 and 75 as an issuer does (see the stateful issuer walkthrough).

  • Key exchange: a host can have the HSM generate a new zone PIN key and send it to connected systems (see the message flows walkthrough).

  • CVV checks: a stateful issuer verifies the CVV in track 2, the iCVV from the chip and the CVV2 of a card-not-present payment with CY (see the stateful issuer walkthrough).

  • MACs: a host can verify the MAC on every request and sign what it sends, with a ZAK or TAK from the key store. In the host form, tick Verify and sign MACs through the HSM and choose:

    • the key, the algorithm (the retail MAC, ISO 9797-1 algorithm 3, or algorithm 1) and the padding;
    • whether requests must all carry a valid MAC, are checked only when they have one, or aren’t checked;
    • whether the host signs what it sends (its responses and its own messages);
    • the messages MACed, by class: authorizations, financial messages and reversals by default, network management too if your link MACs it;
    • the code a missing or wrong MAC answers (96 by default, ours: set your network’s).

    A MAC covers the message as it travels, from the MTI to the MAC field: 64, or 128 when the message has a secondary bitmap. The host verifies a request’s MAC with M8 before rules or the issuer see it, and signs with M6. Live activity names a missing or wrong MAC. When the HSM doesn’t answer, a request gets 91, and a response that can’t be signed isn’t sent.

After a key is rotated, these checks fall back to its previous versions until they retire (see Rotating a key, above).

When the HSM doesn’t answer a host’s PIN check in time, or answers with an error other than a failed verification (a failure mode, or the HSM is down), the host answers 91 and counts no try, as an issuer does when its HSM is unavailable.

A scenario step can send an HSM command and check its answer, to make the values your system checks (a CVV, a PVV, a PIN block), to set things up, or to test HSM client code against the simulator’s failure modes. examples/scenarios/hsm-card-values.yaml makes a test card’s CVV with CW, verifies it with CY, and sends it in track 2:

steps:
- name: The HSM makes the card's CVV (CW)
hsm: {command: CW, fields: "{{key:Issuer CVK}}{{pan}};{{expiry}}{{service_code}}"}
expect: {hsm_response: "\\d{3}"}
save: {cvv: hsm}
- name: The HSM verifies it (CY)
hsm: {command: CY, fields: "{{key:Issuer CVK}}{{cvv}}{{pan}};{{expiry}}{{service_code}}"}
  • command is the two-character code, and fields what follows it, as on the wire. {{key:Name}} is a key from the key store, encrypted under the LMK with its scheme letter, as commands carry keys; {{key:Name@1}} is version 1 of a rotated key, current or previous, to make a CVV under the old key and check that a card from before the rotation still gets through. Other {{variables}} work as in send steps.
  • expect: hsm_error is the error code the answer must carry (00 when left out); hsm_response is a regular expression the answer’s fields must match in full; max_latency_ms and timeout: true work as for messages. With a failure mode that never answers, timeout: true makes “no answer” the passing result.
  • save: hsm saves the answer’s fields, and a slice such as hsm[0:3] part of them, for later steps.
  • Another HSM: host and port (and header_length, 4 by default) send the command to a different HSM, such as one in your lab, with the fields written out in full. {{key:...}} works with the simulator only.

Steps run over the HSM’s TCP port, as your systems do, so they appear in live activity and failure modes apply to them. The test-data guard checks card numbers in a command’s fields, and results mask them; keys only ever travel encrypted under the LMK.

Send steps can MAC their requests and check the MACs that come back, to test a host that requires them, or a system that signs its answers:

target: {simulated_host: Switch link} # a host with MACs uses its own key
steps:
- name: A MACed request is approved, and the answer's MAC verifies
send: {sample: auth, mac: true}
expect: {fields: {39: "00"}, mac: valid}
- name: A wrong MAC is refused
send: {sample: auth, mac: wrong}
expect: {fields: {39: "96"}}
  • send.mac: true has the HSM MAC the request (M6) into field 64, or 128 when it has a secondary bitmap; wrong sends a MAC that doesn’t verify.
  • expect.mac: valid checks the response’s MAC (M8); absent checks there isn’t one.
  • A simulated host target uses the host’s key and method. For your own system, name them: target: {host: ..., port: ..., mac: {key: "Link ZAK", algorithm: "3", padding: 1}}.