Kernels
A kernel runs one family of contactless cards through a transaction, once Entry Point has
selected the card’s application and activated it (see Entry Point). It
lives in emvco::l2::k, one module per kernel. Terms in bold are in the
glossary.
What a kernel does
Section titled “What a kernel does”Every kernel follows the same broad steps, in its own way and with its own data:
- Initiate the transaction: send the card the data it asks for (amount, currency, country, the terminal’s capabilities, an unpredictable number) and get back what it will do (GET PROCESSING OPTIONS).
- Read the card’s data: card number, expiry date, certificates, verification methods (READ RECORD).
- Authenticate the card offline (ODA): check the card’s certificate chain against the scheme’s CA public keys, and the card’s signature over this transaction.
- Processing restrictions: expiry, the countries and transaction types the card allows, the terminal’s exception file.
- Cardholder verification: choose the CVM (online PIN, the phone’s own check, signature, none) from the card’s preferences, the terminal’s capabilities and the limits.
- Risk management and the decision: compare the terminal’s findings (the TVR) with the card’s and the terminal’s rules, and ask the card for an approval offline (TC), an online authorisation (ARQC) or a decline (AAC).
- Outcome: hand Entry Point the result, the message to display, and the Data Record the payment application sends the acquirer.
Some kernels add steps before or after:
- relay resistance timing (Kernels 2 and 8);
- a secure channel with the card (Kernel 8);
- data storage on the card (Kernels 2, 3, 6 and 8);
- the issuer’s answer delivered to the kernel after the online authorisation, with a second tap (Kernels 3 and 6) or without one (Kernel 4).
The kernels
Section titled “The kernels”| Kernel | Book | Cards | In a7pay | Checked against |
|---|---|---|---|---|
| Kernel 2 | C-2 v2.12 | Mastercard | Complete, with the optional DE/DS; Mag-stripe Mode left out (deprecated) | Mastercard’s certification suite, every test passing |
| Kernel 3 | C-3 v2.12 | Visa | Main flow (fDDA, online PIN, CDCVM), with optional Issuer Update, Integrated Data Storage and fDDA for Online | Unit and end-to-end tests; no scheme suite in our runs yet |
| Kernel 4 | C-4 v2.12 | American Express | Main flow, Contactless Mobile CVM, card removed early, re-activation after the online answer | American Express’s certification suite, every test passing |
| Kernel 6 | C-6 v2.12 | Discover, Diners Club | Main flow, issuer update, with optional Data Storage, Extended Logging and Tearing Recovery | Discover’s certification suite, every imported test passing |
| Kernel 8 | C-8 v1.1, with Book E v1.0 | EMVCo’s common kernel, which any scheme can adopt | Complete, with the optional DE/DS | The Book’s own requirements, end to end against a simulated card computing its side of the cryptography |
The suites are the labs’ test material, run in a private repository: see Level 2 test suites for what “passing” covers.
Not implemented:
- the contact kernel (EMV 4.4 Books 1-4), needed for chip-and-PIN with an inserted card, coming next;
- the magnetic stripe;
- Kernels 1, 5 and 7 (legacy Visa and JCB, JCB, UnionPay).
todo.md lists the known gaps kernel by kernel.
Kernel 2 (Mastercard)
Section titled “Kernel 2 (Mastercard)”The most complete and most tested. Beyond the main flow:
- the Relay Resistance Protocol: it times one exchange with the card to detect a relay attack;
- the phone’s own verification and “See phone” restarts;
- the kernel’s messages during the transaction (MSG signals);
- the full error reporting of Book C-2.
The deprecated Mag-stripe Mode (Specification Bulletin No. 317) is deliberately left out.
Kernel 3 (Visa)
Section titled “Kernel 3 (Visa)”fDDA (Visa’s fast offline authentication), online PIN, the phone’s verification (CDCVM), and the Visa-specific reader settings (TTQ). Optional features: the second tap for the issuer’s answer, Integrated Data Storage, and fDDA for online authorisations at transit gates (below).
Kernel 4 (American Express)
Section titled “Kernel 4 (American Express)”- CDA, enabled from the terminal’s capabilities;
- Contactless Mobile CVM (the phone’s verification);
- the card taken away too early: Try Again;
- re-activation after the online answer, which decides on the issuer’s response code.
Kernel 6 (Discover)
Section titled “Kernel 6 (Discover)”CDA in GET PROCESSING OPTIONS, the issuer update on a second tap, and three optional
features: Data Storage, Extended Logging, Tearing Recovery (below). Book C-6 leaves some
reader settings to the terminal vendor; a7pay gives them configuration tags of its own
(k::c6::TAG_DEFERRED_AUTHORIZATION_SUPPORTED, TAG_DATA_STORAGE_SUPPORTED, …).
Kernel 8 (EMVCo’s common kernel)
Section titled “Kernel 8 (EMVCo’s common kernel)”EMVCo’s newer kernel, meant to replace the scheme-specific ones. It differs most from the others:
- a secure channel: the kernel and the card agree on session keys (blinded Diffie-Hellman on elliptic curves), and the card’s sensitive records come back encrypted;
- ECC certificates (and RSA), checked locally;
- MACs over the issuer’s data and the authentication data;
- relay resistance, as in Kernel 2.
All of its cryptography goes through the platform’s provider: its keys never leave it (see Platform). It runs its cryptographic process alongside its state machine, with the two signal queues the Book describes. It supports the P-256 curve only, as Book C-8 v1.1 does: P-521 is reserved for contingency, and SM2 is proprietary.
Optional features
Section titled “Optional features”What a Book leaves optional for the reader (“an implementation option”) is a Cargo feature
of its own, named c<kernel>-<feature>, off by default, and bringing its kernel:
- without the feature, the kernel is built without that behaviour, whatever its configuration says;
- with it, the configuration still decides whether a given reader uses it.
Each is tested both ways.
| Feature | What it adds |
|---|---|
c2-de-ds |
Kernel 2’s Data Exchange and Data Storage (Book C-2 3.4, 3.5). During the transaction, the kernel and the terminal exchange data (the kernel sends what it read, the terminal answers with what the kernel needs). The terminal can also read and write small data containers on the card: loyalty points, transit passes. |
c8-de-ds |
The same for Kernel 8 (Book C-8 3.4, 3.5), the data read and written over the secure channel. |
c3-issuer-update |
Kernel 3’s Issuer Update Processing (Book C-3 chapter 6): after an online authorisation, a second tap delivers the issuer’s answer and scripts to the card. nexo FAST uses it. |
c3-ids |
Kernel 3’s Integrated Data Storage (Book C-3 chapter 7): the terminal’s IDS application reads the card’s IDS records and has them updated as part of the transaction. |
c3-oda-for-online |
Kernel 3’s fDDA for Online (Book C-3 3.3.4, 5.6.2), for special-purpose readers such as transit gates: the card’s offline authentication checked on online transactions too, its result left for the reader. |
c6-data-storage |
Kernel 6’s Data Storage (Book C-6 2.5): data containers on the card, read and written by the terminal’s operator logic. |
c6-extended-logging |
Kernel 6’s Extended Logging (Book C-6 2.6): the operator’s data written into the card’s transaction log. |
c6-tearing-recovery |
Kernel 6’s Tearing Recovery (Book C-6 2.7): if the card leaves the field at the wrong moment, the transaction resumes when it is tapped again, from a log the terminal keeps. |
Where the Book leaves the business logic to the terminal, the terminal supplies it to the
kernel through an interface: core::data_exchange::DataExchange (Kernels 2 and 8),
k::c6::operator::Operator (Kernel 6), k::c3::ids::IdsOperator (Kernel 3). The reader’s
transaction takes them (l2::transaction::TerminalProcessing), and l2::transaction::scripted
has versions of each giving fixed answers, as a test does.
Brand features
Section titled “Brand features”The kernels follow EMVCo’s public Books. A scheme’s certification suite sometimes expects something else: its certified readers behave a certain way, or the Book left room for interpretation. Such behaviour sits behind a brand feature of the scheme, off by default. Each deviation is commented in the code with the text of the Book it departs from and the certification tests that need it, and is tested both ways.
| Feature | Kernel | What changes |
|---|---|---|
brand-mastercard |
2 | Data from the card that it isn’t allowed to update is ignored, whatever its kind, as are unknown tags the terminal already holds |
brand-amex |
4 | “Processing error” is shown before the transaction ends with End Application |
brand-discover |
6 | The Card Verification Results are read from the Issuer Application Data (Discover’s layout); CDA fails on an approval returned without its signature; “See phone” then Try Again on status ‘6986’/‘6987’, instead of the Book’s restart; processing restrictions run before cardholder verification (EMV Book 3’s order) |
Brand features touch different kernels, so one build can carry several. Entry Point’s scheme-specific behaviours are runtime settings instead, since one reader serves every scheme: see Entry Point.
How the code is organised
Section titled “How the code is organised”Reading the code with the Book open beside it:
- One function per requirement. Each numbered requirement, or each symbol of a flow
diagram, is a function named after it and documented with its reference, e.g.
/// EMVCo Book C-2 6.5.3 S3.5. Each has its own test. - States are pure functions. A kernel state takes the transaction’s data and a signal (the
card’s response, the terminal’s data, a timeout) and returns the next state. When a state
needs the card, it leaves a pending command; the kernel’s session loop sends it through the
Readerand feeds the answer back as a signal. Every state can be tested without a card reader. - The data.
TlvDatabaseholds the transaction’s data items by tag, with the Book’s rules for each: known or not, its length, the templates it may come in, who may update it. The kernel’s other working data, which the Book gives no tag, is inTransactionContext, always namedctx. - The kernels share nothing but
core: TLV, data object lists, commands, offline data authentication, CVM lists, processing restrictions, outcomes. That is what lets a build carry any subset of them.
When the specification is ambiguous, the reading chosen is commented where it is applied and
listed in todo.md.