Home / Ciberseguridad / Behind the approved on the terminal: the two-second encrypted conversation inside your payment card

Behind the approved on the terminal: the two-second encrypted conversation inside your payment card

You tap to pay for a coffee, the terminal beeps, two seconds pass and it says approved. In that gap something far more interesting than a read has happened: the chip in your card ran a small program, generated a cryptogram with a key that never leaves it, and convinced the terminal it is not a copy. All without a battery, powered by the reader itself.

A card with no battery that runs a program

The gold rectangle is not a passive contact: it is the interface of a microcontroller. It is called a secure element and it contains a CPU, non-volatile memory, RAM, a random number generator and, on modern parts, a cryptographic accelerator. The standard that describes it, ISO/IEC 7816, defines eight contacts; in practice five are used: power (VCC, 3 or 5 volts), ground, clock (CLK, between 1 and 5 MHz), reset and a single bidirectional data line (I/O).

That single line explains a lot. Card and terminal do not talk in parallel: they exchange bytes one at a time, half-duplex, waiting for their turn. A modern 3 GHz processor would settle this conversation in microseconds; here the reader supplies the clock and the chip divides it, so every operation costs real cycles. The slowness of the payment is not the chip’s fault: it is the price of making sure an attacker with an oscilloscope cannot guess keys by measuring current draw.

APDU: the card’s language

The dialogue is made of APDUs (Application Protocol Data Units), messages with five fixed fields: class (CLA), instruction (INS), two parameters (P1 and P2), data length and, optionally, how many response bytes are expected. The card answers with data and, always, two status bytes: 0x90 0x00 means OK, and any other combination is an error with a name.

On top of that very simple transport, EMV — the Europay, Mastercard and Visa standard that now governs nearly every card — builds a data layer in TLV format (tag, length, value). Each tag is a hexadecimal number identifying a field: the PAN (the card number), the expiry date, the terminal country, the amount. The terminal does not read a magnetic stripe: it asks for tags and the chip decides what to hand over.

From PPSE to AID: finding the application

First it has to know what is inside. The terminal sends a SELECT on the file 2PAY.SYS.DDF01 (PPSE, for contactless) or 1PAY.SYS.DDF01 (for contact) and the card replies with the list of available applications. Each one is identified by an AID: A0000000031010 for Visa credit, A0000000041010 for Mastercard. One card may carry several — credit, debit, another network — and the terminal chooses according to its configuration.

Then comes GET PROCESSING OPTIONS, where the terminal sends the data the card requested beforehand in a template called PDOL. The answer carries the AIP (which authentication capabilities the chip supports) and the AFL, a map of which files to read. With READ RECORD the terminal retrieves the equivalent of the magnetic stripe, the cardholder name, the cardholder verification list and the CDOL templates, which say which fields must be included in the payment order.

The unpredictable number that blocks replay

Here is one of the key pieces. Before asking for the cryptogram, the terminal generates a four-byte unpredictable number with its own random generator and puts it into the transaction data, alongside the amount, currency, date, country and the result of its checks (the TVR). If someone recorded a full transaction and replayed it, the cryptogram would not match: it was tied to that specific number.

The second defence is a counter. The ATC (Application Transaction Counter) is a two-byte integer the card increments on every operation and which travels inside the cryptogram. The issuer keeps the last value it saw: an equal or lower ATC is rejected. With 65,535 uses per card there is plenty of margin for a normal lifetime, and when it runs out the chip forces online authentication.

How the ARQC cryptogram is built

The terminal sends GENERATE AC with the data from the CDOL template and the chip returns, among other fields, the ARQC (Authorization Request Cryptogram). It is computed as a MAC in EMV-CBC mode: data blocks are chained with a session key and 3DES is applied, or AES in recent implementations. The session key is not stored anywhere: it is derived on the fly. The issuer holds a master key per card range (IMKAC); with it and the PAN it obtains the card master key, and with this transaction’s ATC it obtains the session key. The card performs the same derivation from its own key, and the issuer repeats it in its HSM when the authorisation arrives.

That is why a cloned card without the chip is useless, and why the data sent to the bank cannot be used to forge the next cryptogram: without the master key, the ATC is not enough. The authorisation message then leaves over the network in ISO 8583 format, with the cryptogram and TVR in field 55.

SDA, DDA and CDA: why copying the chip is not enough

The cryptogram protects the transaction, but someone could build a chip that answers without being the original. Three levels of card authentication exist to prevent that. In SDA (static data authentication) the issuer signs the card’s fixed data with its private key and the terminal verifies the certificate chain up to the brand’s public key. In DDA the chip has its own key pair and signs the terminal’s unpredictable number: a different signature on every payment.

CDA combines both and also signs the output of GENERATE AC, so that cryptogram and signature are bound together. It is the level most networks now require. Verifying a 2048-bit RSA signature on a card microcontroller clocked at a few megahertz is one of the reasons the chip seems to think for a fraction of a second.

Two seconds, twenty round trips

Add it up: application selection, reading four or five records, generating the cryptogram, the signature, and in many cases an online query to the issuer that can take hundreds of milliseconds. The terminal also runs its own checks: that the date has not expired, that the amount does not exceed the no-verification limit, that the terminal country is allowed. When the issuer answers it returns an ARPC and the terminal sends a second GENERATE AC so the chip closes the transaction and, where applicable, burns the ATC.

Contactless: the same APDUs over radio

Paying by holding the card close does not change the protocol, it changes the cable. ISO/IEC 14443 defines a 13.56 MHz electromagnetic field that powers the chip by induction and carries the same APDUs over ISO-DEP, at up to 848 kbit/s. Because the card is passive and has no battery, useful range stays at a few centimetres, which is why terminals insist on holding it so close.

That convenience has a cost: whatever travels through the air can be heard by anyone with an antenna. The answer is not to encrypt the channel — legacy mode does not — but to make what is overheard useless. The ARQC is valid once, for that amount and that unpredictable number. And so the card number does not circulate, networks replace the PAN with a token (DPAN) issued by services such as MDES or VTS, which only works on the device or merchant it was generated for.

What can actually be snooped

An attacker holding the card can ask the chip questions, but the chip limits PIN attempts and answers with cryptograms that are worthless out of context. What has historically failed is not the cryptography but the implementation: tampered terminals that request the transaction in one currency and charge in another, or that declare no verification when the card asked for a PIN. That is why the TVR, the result of the terminal’s checks, travels inside the cryptogram: the issuer can verify that the right steps were taken and refuse the operation if not.

Next time the terminal takes an extra second, the explanation is probably a chip a few square millimetres across running 3DES, verifying a signature and waiting for its bank to answer over the network. Two seconds, and not one of those pieces trusts any of the others.