Skip to content

Magic Keyboard with Touch ID: Touch ID channel on USB, trusted-accessory client and pairing relay - #1810

Closed
RyanTheTide wants to merge 18 commits into
mainfrom
stac/magic-keyboard-touchid
Closed

RyanTheTide wants to merge 18 commits into
mainfrom
stac/magic-keyboard-touchid

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Commits:

  • HID: apple: map the Touch ID key of the Magic Keyboards with Touch ID to power (Diljit Singh, from linux-aurora#59, unchanged, his authorship)
  • soc: apple: sep: typed trusted-accessory operations
  • soc: apple: sep: trusted-accessory unpair and session operations
  • HID: usbhid: take input from a bulk IN endpoint on the Touch ID interface
  • HID: apple-ta: transport for the Touch ID channel of the Magic Keyboards
  • soc: apple: sep: trusted-accessory lifecycle
  • soc: apple: sep: read-only trusted-accessory client for the Touch ID keyboards
  • soc: apple: sep: fingerprint the host attestation object for unpaired keyboards
  • HID: apple-ta: take the Touch ID reports the descriptor does not declare
  • soc: apple: sep: wire format of the keyboard pairing exchange
  • soc: apple: sep: pair a Magic Keyboard with Touch ID on request
  • soc: apple: sep: carry the live command sequence into the pairing check

What it does

  • The Touch ID key reports KEY_POWER, as the built-in Touch ID key does.
  • usbhid accepts the keyboard's third HID interface (vendor page 0xff00, usage 0x4b), whose only input endpoint is bulk. Before this, Linux enumerated two of the three HID interfaces and logged "couldn't find an input interrupt endpoint" at every boot.
  • hid-apple-ta binds that interface with HID_CONNECT_DRIVER only, so there is no hidraw, hiddev or input device. It hands every input report, with its ID and actual length, to one in-kernel client. hid-apple leaves the interface alone.
  • The keyboard answers commands and sends sensor frames in input reports its descriptor does not declare (IDs 0x40-0x4f and 0xb0-0xbf, 8 bytes to about 10.8 KiB). hid-apple-ta's report_fixup() declares them as 16 KiB padding reports inside the vendor collection, so HID core keeps them and usbhid reads 16 KiB bulk transfers. It applies only to the exact 49-byte USB descriptor.
  • stac_ops.rs has request builders and reply parsers for 12 operations of the enclave's trusted-accessory service. stac_lifecycle.rs decides which operation runs next for one keyboard.
  • apple-sep registers as the hid-apple-ta client. When a keyboard comes up, or the stac service comes back online with one attached (at bring-up and after every wake), it reads three feature reports (protocol tag and version, accessory identity, connection nonce). It then asks the enclave for that accessory's pairing state (stac 0x02). Only those reads go to the keyboard. The step the lifecycle wants next (session, pairing, connection state) is logged as held and not run. Identities, nonces and keyboard answers are never logged.
  • For a keyboard the enclave reports as unpaired, apple-sep also asks for the enclave's host attestation object (stac 0x01) and logs its length and an 8-byte SHA-256 prefix. A pairing presents this object together with a certificate chain that covers it, so the fingerprint shows whether a chain issued for the same Mac under another OS covers this OS too.

Tests

  • KUnit: SEP suite 626 run, 624 pass, 2 skipped (apple_sep_stac 11, apple_sep_stac_client 3, apple_sep_stac_lifecycle 9, apple_sep_stac_ops 13).

  • drivers/hid builds clean with W=1; checkpatch clean apart from the MAINTAINERS note for new files.

  • J616s, keyboard 05ac:029f on USB, one-shot 7.3.0-aurora-fleet71stac2 (this branch at 09be5fe), SELinux enforcing:

    • Boot: usbhid binds interface 2; hid-apple-ta: "Touch ID channel, reports 0x20/0x21/0x22 (107/649/62 bytes)"; no hidraw node for it; keyboard interfaces 0/1 unchanged. No "couldn't find an input interrupt endpoint" line.
    • Client at boot (9.7 s) and again after unbinding and rebinding hid-apple-ta: "stac: keyboard identified; the enclave reports paired=false authorized=false", then "next step pairing held (read-only client)".
    • usbmon on the keyboard's bus across the rebind and three s2idle cycles: 3 control requests to the keyboard, all GET_REPORT feature 0x4b/0x41/0x4c on interface 2; 0 SET_REPORT, 0 OUT transfers.
    • Feature-report reads on the production kernel before and after the row are identical.
    • s2idle 3/3 (rtcwake), binding present after each wake; 0 AVC; 0 hid/usb/sep kernel warnings or errors.
    • Touch ID key press under evdev: KEY_POWER 1 then 0 on the keyboard's input device.
  • J616s, same keyboard, one-shot 7.3.0-aurora-fleet71stac3 (this branch at 1d3adb6), SELinux enforcing:

    • Client at boot and after a hid-apple-ta rebind: paired=false authorized=false, then "host attestation object: 97 bytes, sha256 ...". The fingerprint is the same in both runs and equals the SHA-256 prefix of the host object macOS sends to the keyboard on the same Mac, so the object is per device rather than per OS.
    • usbmon across the rebind and three s2idle cycles: GET_REPORT feature 0x41/0x4b/0x4c on interface 2 only; 0 SET_REPORT, 0 OUT transfers.
    • Feature-report reads on the production kernel before and after the row (0x4b, 0x41, 0x4c, 0x44, 0x45 x3) are identical.
    • s2idle 3/3, 0 AVC, 0 hid/usb/sep kernel warnings or errors.
  • stac_ta_wire.rs encodes and checks the keyboard side of a pairing: commands [id][u32 step][payload], replies [id][u32 step][u32 status][payload] (the pairing check answers [id][u32 step][u8 status][u8 paired][u8 1]), 532-byte fragment reports (20-byte header: step, kind, total, offset, chunk; at most 512 bytes; zero padded), the 3601-byte material reply and the manifest read.

  • Pairing runs only when root writes 1 to /sys/module/apple_sep/parameters/ta_pair (write-only, nothing pairs otherwise), and only for a keyboard the enclave reports unpaired. It relays keyboard and enclave (0x01, 0x03, 0x15, 0x05) in the order of a macOS pairing and stops at the first reply that does not check out. The host certificate chain comes from the per-machine firmware file apple/stac-host-chain.der (the Mac's own device attestation chain, never shipped in this tree); it must be two DER certificates whose leaf names the SHA-256 of this enclave's host object, checked before the first write to the keyboard.

Tests (b837072)

  • KUnit: SEP suite 635 run, 633 pass, 2 skipped; new apple_sep_stac_ta_wire (9): fragment layout for 1663- and 1505-byte objects, reply id/step/status, pairing-check flags, material slot bounds, manifest bounds, chain check.
  • hid-apple-ta, usbhid and apple-sep build clean.
  • Not yet run on hardware: the descriptor fixup, the 16 KiB bulk reads and the pairing relay.

Draft. Next: J616s rows for the pairing (keyboard state before/after, usbmon, AVC, enclave pairing state afterwards), then a session after a keyboard power cycle, fprintd through the keyboard sensor, and sleep/reconnect.

Pairing on hardware — the 0xfe fragment transport and the step-2 stall (through c6a59b9)

Five commits after b837072 carry the pairing relay from read-only identify to an active exchange, and three J616s rows take it onto hardware:

  • match command replies by their carried logical ID (2cf95c5): the keyboard carries a command's reply inside its declared input reports, keyed by the inner logical ID, not the HID report ID. Routing by the inner ID delivers replies to the waiting command. Proven on J616s: pairing advanced from a step-1 timeout to reaching step 2.
  • carry large pairing reports over the 0xfe transport (dd54996) and stream the frames (0d512a1): a body too large for one USB SET_REPORT is split into fixed 44-byte feature-0xfe frames — a 4-byte header [0xfe][logical id][u16 segment word] and a 40-byte chunk — with a FIRST/CONT/LAST segment word. The frames stream back-to-back (no between-frame wait), matching the macOS cadence.
  • carry the last 0xfe frame's tail forward (c6a59b9): the final frame stays a fixed 44 bytes; its bytes past the valid tail repeat the preceding frame's buffer rather than being zero-padded or trimmed.

J616s rows, keyboard 05ac:029f on USB, one-shot test kernels on this branch, SELinux enforcing, dead-man reboot back to production each time, keyboard reported unpaired before and after every row (its feature-report state is byte-identical before and after), 0 AVC and 0 hid/usb/sep warnings on every row:

  • step 1 cleared: with the reply-carrier fix, the host attestation object and certificate chain check against the live enclave host object, and the exchange reaches step 2 (the keyboard's host-object check).
  • the 0xfe transport works on the wire: the step-2 body goes out as four feature-0xfe frames; the keyboard accepts the FIRST and both CONT frames (each a full 40-byte chunk, SET_REPORT completes 0).
  • open: the keyboard STALLs (control SET_REPORT -EPIPE) the frame tagged LAST, and pairing stops at step 2. Three distinct LAST-frame encodings were built and tested — zero-padded to 44 bytes, trimmed to the valid length, and tail-carried to 44 bytes — and the device STALLs all three identically, while always accepting the three frames before it. usbmon confirms the tail-carry frame is byte-faithful to the intended layout. So the stall is not the LAST frame's length or payload; it points at the LAST segment-word encoding (the value is derived from a static reading and is not yet confirmed on the wire) or a precondition the relay has not reproduced. This is the next item; pairing does not complete yet.

Draft stays draft: pairing reaches step 2 on hardware but does not complete.

The accessory handshake on identify, and pairing from a cold keyboard (130bf14)

A Magic Keyboard with Touch ID that is powered on cold comes up before it has selected the trusted-accessory communication protocol. In that state it stalls the identity feature reads, so the attach identify failed (-EPIPE) and the keyboard never reached the state the pairing trigger requires. A keyboard the other OS had already handed off answered the same reads and identified, which hid the dependency: on a freshly power-cycled keyboard the read-only identify stalled -32 and the pairing trigger refused with EBUSY ("keyboard idle, not unpaired").

macOS performs a fixed handshake on every attach before it knows whether to pair: read the protocol tag and version, echo the tag back to select the protocol, read the identity, request the identity point, then read the fresh connection nonce. The protocol handoff is what brings a cold keyboard to where it answers its identity.

  • run the full accessory handshake on identify (130bf14): the identify path now runs that five-exchange handshake (GET 0x4b, SET 0x68, GET 0x41, SET 0x67 answered by IN 0x40, GET 0x4c) instead of three bare feature reads. The only writes are the two protocol-handshake reports; it changes no pairing or enclave state. The identify, pairing and session paths share one definition of the order, since the pairing and session relays already opened with the same five exchanges. A KUnit test drives the handshake over a recording channel and pins the exact order and the returned identity and nonce, plus a case that refuses a foreign protocol tag before any write.

Tests (130bf14)

  • KUnit: SEP suite 647 run, 645 pass, 2 skipped; apple_sep_stac_client now 6 (adds the handshake-order test and the foreign-tag refusal).
  • hid-apple-ta, usbhid and apple-sep build clean, no new warnings.

J616s row, keyboard 05ac:029f on USB, one-shot 7.3.0-aurora-fleet71stac14 on this branch at 130bf14, SELinux enforcing, dead-man reboot back to production, on a keyboard left idle by a cold power-cycle (feature reads before the row answered only the protocol tag 0x4b, confirming the idle state):

  • Identify succeeded from the idle keyboard: the boot client logged "keyboard identified; the enclave reports paired=false authorized=false" then "keyboard Unpaired". The same cold keyboard had stalled identify -32 on the prior kernel. After the row the keyboard answered all of 0x4b, 0x41, 0x4c, 0x44 and 0x45, where before it answered only 0x4b, so the handshake moves it out of the idle state and that persists.
  • Pairing then started and reached step 2, which it could not do before on a cold keyboard: "pairing the keyboard" then "pairing stopped: Transport(2, -32)". usbmon shows the four step-2 0xfe frames go out, the keyboard accepts the first three and stalls the frame tagged LAST (SET 0xfe submits 4, SET 0x60 submits 0, one control -32 stall). This is the same step-2 LAST-frame stall already documented through c6a59b9 (the LAST segment-word value or a host-object precondition); the handshake change does not touch it.
  • 0 AVC, 0 hid/usb/sep kernel warnings, enrolment survived the reboot, production kernel restored.

Draft stays draft: a cold keyboard now identifies and pairing reaches step 2, but step 2 does not complete yet.

The step-2 host object, and the command-sequence replay (e6b3a15)

A reverse-engineering report on the step-2 body settled what the keyboard checks as it stalls the LAST 0xfe frame. The 137-byte logical 0x60 body is a command sequence (4 bytes, little-endian), a length (4 bytes), a 97-byte SEC1 uncompressed P-384 host public point owned by the Mac's SEP, and a 32-byte zero tail. It carries no nonce, MAC, signature, certificate, keyboard identity or pairing counter: the host pairing ephemeral and the certificate chain do not exist yet at step 2, so there is no byte range signed or MACed over the body for the keyboard to check. The host point this branch sends already matches the one macOS sends on the same Mac (the full certificate-digest binding and the independent same-machine fingerprints agree), and the recorded length and zero tail match.

The remaining divergence is the command lifecycle. The keyboard numbers the host's commands within one physical attach: the identity request takes sequence 1, the pairing-state check the next. stac_ta_handshake() hard-coded the identity request at sequence 1 and ran twice, once on identify and again at the start of a pairing, so it re-sent sequence 1 within the attach before the pairing check went out at 2. That replay is what the keyboard rejects as it commits the step-2 command.

  • carry the live command sequence into the pairing check (e6b3a15): the attach handshake runs once, on identify. Its result (identity, point, nonce) and the next command sequence are kept in a per-attach context, cleared on detach; a later pairing or session reuses that context instead of running the handshake again. The pairing draws every command's sequence from the one counter, so on a fresh attach the identity request is 1 and the pairing check 2; the session reuses the recording's fixed message numbers for its later steps but takes the carried sequence for its pairing check. A KUnit test asserts the identity request is issued exactly once at the first sequence and the pairing check then carries the next sequence rather than a replayed first.

Tests (e6b3a15)

  • KUnit: SEP suite 648 run, 646 pass, 2 skipped; apple_sep_stac_client now 7 (adds the sequence-carry test, updates the handshake-order test for the returned context).
  • hid-apple-ta, usbhid and apple-sep build clean, no new warnings.
  • J616s row, keyboard 05ac:029f on USB, one-shot 7.3.0-aurora-fleet71stac15 on this branch at e6b3a15, SELinux enforcing, dead-man reboot back to production, on a cold-idle keyboard:
    • The attach handshake runs once per attach: usbmon on the keyboard's bus shows a single SET 0x67 (identity request) and a single SET 0x68 (protocol handoff), where before the handshake ran again at the start of the pairing. The identify client logged paired=false, and the keyboard answered all of 0x4b/0x41/0x4c/0x44/0x45 after the row.
    • Step 2 still stalls: the four 0xfe frames of the host-object command go out, the keyboard accepts the first three and stalls the frame tagged LAST (SET 0xfe 4, SET 0x60 0, one control -32), logical IN 0x42 does not arrive, and pairing stops at step 2 (Transport(2, -32)).
    • 0 AVC, 0 hid/usb/sep kernel warnings, enrolment survived the reboot, production kernel restored.

With a single identity request and the carried step-2 sequence both confirmed on the wire, the step-2 stall is independent of the command-sequence lifecycle and of the logical object's contents (which already match macOS on this Mac). The next item is the keyboard's 0xfe reassembly of the final frame or a connection-state precondition, not a signature or nonce field in the body.

DjDeveloperr and others added 11 commits November 13, 2026 16:56
… to power

The Touch ID key of a Magic Keyboard with Touch ID reports usage 0x0c0040,
Consumer Menu, in its main keyboard report (measured on a 0x029a over USB),
so it reaches user space as KEY_MENU, which desktops ignore. On the Macs
with a built-in Touch ID key that key is the power key.

Map it to KEY_POWER on the Magic Keyboards with Touch ID, with or without
the numeric keypad, over USB and Bluetooth. Desktops then offer their
power options for it, as they do for the built-in key.

Signed-off-by: Diljit Singh <[email protected]>
(cherry picked from commit ba99065d3e3a8c1785bbfaccddceeb4df81597f8)
The stac service pairs the enclave with an accessory that has its own
fingerprint sensor, such as a keyboard with Touch ID, and tracks whether
it is connected and enabled for biometric use. The host carries public
points, attestations, certificates and manifests between the two; it
holds no private or session key and cannot mark an accessory paired.

Add request builders and reply parsers for the operations recorded on a
J616s under macOS 27 while a keyboard was paired, enabled for Touch ID,
disconnected and reconnected: host attestation object (0x01), pairing
state (0x02), pairing begin (0x03) and finish (0x15), manifest (0x05),
connection state (0x09), paired list (0x0b) and authorize (0x14). Every
body length in that recording matches these layouts, and static asserts
pin them. Fixed slots are zero-padded, length fields are bounded and
checked against the body, flags accept only 0 and 1, and an accessory
that is authorised but not paired is rejected. Identities and points
print only their type name.

Nothing calls these yet. The accessory client that drives them comes
with its HID transport.

KUnit (apple_sep_stac_ops, 10 cases) builds each request at the
recorded length and checks exact, truncated, oversized and overflowing
replies.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
(cherry picked from commit a41ff13)
A second J616s recording under macOS 27 covered forgetting the keyboard,
pairing it again and switching it off and on. It adds three operations
to the stac set:

  0x04  unpair          16-byte identity, empty reply
  0x06  session begin   identity + the accessory's ephemeral point (81),
                        reply: the enclave's point, a length word and an
                        attestation (69 + 159 in the recording)
  0x07  session finish  identity, length word and the accessory's
                        material in a zero-padded 1536-byte slot (1556)

The session ran when the keyboard came back after a power cycle, after a
manifest with after_pairing clear. It did not run at pairing or when the
keyboard moved between USB and Bluetooth. The session-begin reply has
the same layout as the pairing-begin reply and uses its parser.

KUnit: apple_sep_stac_ops grows to 13 cases (unpair, session begin round
trip with truncated and short replies, session finish layout and bounds).
The SEP suite passes 609 of 611 with 2 skipped.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
(cherry picked from commit 0c6e256)
…face

The Magic Keyboards with Touch ID have a third HID interface, the Touch
ID channel (vendor page 0xff00, usage 0x4b). It declares a HID report
descriptor and three input reports of up to 650 bytes, but its only data
endpoint is a bulk IN with 64-byte packets; there is no interrupt IN.
usbhid_probe() refuses it ("couldn't find an input interrupt endpoint"),
so Linux enumerates two of the keyboard's three HID interfaces.

For the interfaces of these keyboards, accept a bulk IN endpoint when
there is no interrupt IN, and read input reports from it with a bulk URB
of the largest input report's size. Each report is one transfer ended by
a short packet. hid_irq_in() already resubmits the URB itself, and the
stall handling clears the halt on whatever pipe the URB uses, so both
work for bulk unchanged. Every other interface still needs an interrupt
IN endpoint.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The Touch ID channel of a Magic Keyboard with Touch ID carries pairing,
session and sensor traffic between the keyboard's sensor and the Secure
Enclave. The host relays it and must not expose it: the bodies are
opaque to the host, and a user-space reader has no business with them.

Add hid-apple-ta, which claims the channel's collection on USB,
starts it with HID_CONNECT_DRIVER only (no hidraw, hiddev or input
device) and passes each input report to one in-kernel client through
include/linux/hid-apple-ta.h. The collection's three input reports are
told apart by their declared lengths (107, 649 and 62 bytes on a
0x029f): the largest carries data, the middle one control replies, the
smallest events. A collection that does not have exactly three numbered
input reports of different lengths is refused. Reports of the wrong
length for their ID are dropped. The client can send feature and output
reports and read feature reports.

hid-apple now leaves this collection to hid-apple-ta. Without
hid-apple-ta nothing binds it: hid-generic stays away from devices that
hid-apple claims, so the channel is never exposed. The keyboard
collections are unchanged.

Over Bluetooth the channel shares one HID device with the keyboard and
is not handled yet.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Add the decision logic for one keyboard with Touch ID between the
enclave's stac service and the keyboard's channel. It takes events
(service online/offline, channel attached/detached, the keyboard's
identity and connection nonce, the enclave's pairing state, session and
connection acknowledgements, failures, grace expiry) and returns the
operations to run. It holds no key and never decides on its own that a
keyboard is paired or enabled.

It follows the J616s recordings under macOS 27:
- a connection the enclave has no session for (a new nonce after the
  keyboard was switched off and on, or a fresh enclave) runs the
  per-connection session before connection state 1;
- moving between USB and Bluetooth keeps the nonce and the session and
  only reports the link;
- a finished pairing leaves a usable channel, so no session follows it;
- a lost link reports connection state 0 and keeps the record until a
  grace period ends, then finalises the detach.

The sensor is offered to biometric operations only while connected and
enabled; any failure or loss of the enclave withdraws it.

KUnit: apple_sep_stac_lifecycle, 9 cases (boot with a paired keyboard,
keyboard before the service, transport switch, power cycle, grace
expiry, unpaired keyboard, enclave restart, failure, a second keyboard
during the grace period). SEP suite: 620 run, 618 pass, 2 skipped.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
…keyboards

Register the SEP driver as the one client of hid-apple-ta and drive the
trusted-accessory lifecycle as far as it goes without writing anything.

When a keyboard channel comes up, or the stac service comes back online
with one attached (after bring-up and after every wake), a shim reads
three feature reports from the keyboard: the protocol tag and version
(0x4b, "TACP" version 1), the accessory identity (0x41, 16 bytes) and the
connection nonce (0x4c, 16 bytes). The identity is the one the enclave
keys its accessory records by. The client then asks the enclave for the
pairing state of that accessory (stac 0x02) and feeds the answer to the
lifecycle.

Nothing is sent to the keyboard except those reads. Whatever the
lifecycle asks for next (the per-connection session, a pairing, a
connection state) is logged as held and not run. Input reports that
arrive meanwhile are counted and dropped. Neither the identity, the
nonce nor any other keyboard answer is logged.

The stac exchange now takes any opcode with a one-packet answer; the
power-state report runs through it unchanged. Exchanges are serialised.
APPLE_SEP depends on HID_APPLE_TA || !HID_APPLE_TA; without the
transport the client is a stub.

KUnit: apple_sep_stac 11 cases (3 new: the pairing-state request word,
one-packet answers, opcode matching), apple_sep_stac_client 3 cases.
SEP suite: 626 run, 624 pass, 2 skipped.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
… keyboards

A pairing presents the enclave's host attestation object (0x01) to the
keyboard together with a certificate chain that covers it. Whether a
chain issued to the other OS on the same Mac can serve here depends on
whether this OS gets the same object.

When the enclave reports an identified keyboard as unpaired, ask for the
object and log its length and the first eight bytes of its SHA-256. The
object itself is not logged and nothing is written to the keyboard.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The Touch ID interface of the Magic Keyboards with Touch ID declares three
input reports (0x20, 0x21, 0x22; 107, 649 and 62 bytes), but the keyboard
answers pairing commands and streams sensor frames in input reports it
does not declare: IDs 0x40-0x4f and 0xb0-0xbf, from 8 bytes to about
10.8 KiB. HID core drops a report whose ID is not in the descriptor before
any driver sees it, usbhid sizes its bulk input buffer from the largest
declared report (650 bytes), and the driver itself only passed on reports
of exactly the declared role lengths.

Declare those IDs in report_fixup(), inside the vendor collection, each
with 16383 bytes of padding fields so that with its ID it fills
HID_MAX_BUFFER_SIZE. usbhid then reads 16 KiB bulk transfers, HID core
finds a report for every ID the keyboard sends, and nothing is parsed out
of the padding. The fixup applies only to the exact 49-byte USB
descriptor.

The client callback now gets every report with its ID and actual length
instead of a role with a fixed length; the roles of the declared reports
stay for the probe log and apple_ta_report_len().

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The keyboard side of a trusted-accessory pairing, as our USB capture of a
macOS pairing shows it: every host command is a feature report
[id][u32 step][payload], answered by one input report
[id][u32 step][u32 status][payload], except the pairing check, which
answers [id][u32 step][u8 status][u8 paired][u8 1]. Objects too large for
a command (the host certificate chain, the enclave's pairing attestation)
go in 532-byte fragment reports: a 20-byte header of step, kind, total
length, offset and chunk length, then at most 512 bytes, zero padded. The
keyboard's material comes back in one 3601-byte report holding its
attestation and certificate in length-prefixed fixed slots; the manifest
read is [id][u32 length][manifest].

Add the encoders and checkers for these, and a check that a host
certificate chain is exactly two DER certificates whose leaf names the
SHA-256 of this enclave's host object, so a chain for another Mac is
refused before anything reaches a keyboard. KUnit tests cover the
fragment layout with the recorded object sizes (1663 and 1505 bytes),
reply id/step/status checks, the pairing-check flags, the bounds of the
material slots and the manifest, and the chain check.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Relay a pairing between the keyboard and the enclave's trusted-accessory
service, in the order a macOS pairing uses: tag and version echo,
identity and identity point, nonce, the pairing check with the enclave's
host object (0x01), the keyboard's pairing ephemeral point, 0x03, the
host certificate chain in fragments, the host object again, the
enclave's attestation in fragments, the host object with the enclave's
ephemeral point, then 0x15 with the keyboard's material and 0x05 with its
manifest. Any reply that does not check out, a nonzero status or a
timeout stops the exchange; the log names the step, never the bodies.

It runs only when root writes 1 to the write-only ta_pair module
parameter, and only for a keyboard the enclave reports unpaired. The host
certificate chain, the Mac's Apple-issued device attestation chain, comes
from the per-machine firmware file apple/stac-host-chain.der and must
certify this enclave's host object before the first write to the
keyboard. Afterwards the enclave is asked for the pairing state again and
the lifecycle is told the keyboard is paired.

The shim gains a command call that sends one feature report and waits for
the input report with the expected ID, and a feature-report read, both
under the client lock so the keyboard cannot detach mid-exchange.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide RyanTheTide changed the title Magic Keyboard with Touch ID: Touch ID channel on USB and a read-only trusted-accessory client Magic Keyboard with Touch ID: Touch ID channel on USB, trusted-accessory client and pairing relay Nov 13, 2026
RyanTheTide and others added 7 commits November 14, 2026 00:18
When a paired keyboard's connection is new to the enclave the lifecycle
asks for a per-connection session (State::Session -> BeginSession), then
a connection state (0x09) and, when a detach's grace period ends, a
detach finalise (0x08). The read-only client logged these as held.

Add the session relay behind a new write-only ta_session parameter
(0200, off by default), mirroring ta_pair end to end: a session C->Rust
callback through the shim, and the gate that lets the session,
connection-state and detach-finalise steps run rather than being held.
With it off the client keeps its read-only behaviour; the pairing path
is untouched.

The session confirms the pairing (0x02 and the keyboard's SET 0x60 ->
IN 0x42 check), hands the enclave the manifest with after_pairing clear,
then relays the two session halves: the keyboard's session ephemeral
point (SET 0x66 [u32 4] -> IN 0x47) into SessionBegin (0x06), the host
session material it answers with (SET 0x62) back for the keyboard's
session material (IN 0x46), and SessionFinish (0x07). The session report
IDs 0x66/0x47 are the pairing step 3's with a different body; they are
kept as separate named constants. ConnectionState (0x09) and Finalize
(0x08) are enclave-only, with no keyboard writes.

New wire constants cmd::SESSION_EPHEMERAL/SESSION_MATERIAL and
reply::SESSION_EPHEMERAL/SESSION_MATERIAL, a bounded length-prefixed
parse_session_material() for the IN 0x46 slot, and the Op::Finalize
(0x08) opcode with finalize_request(). KUnit covers the session command
shapes, the 0x47 point and 0x46 material replies, finalize_request and
the session gate; the 0x06/0x07 request and reply layouts and their
tests were already in place. Identities, points and material stay opaque
and unlogged.

Unbuilt draft for review; the session material reply (IN 0x46) is
modelled as one bounded SESSION_MATERIAL_SLOT length-prefixed slot from
the op7 request shape, the capture having only the 9 SET bodies, not the
IN report layout.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
A Magic Keyboard with Touch ID does not answer a trusted-accessory command
in an input report of its own ID. It carries the reply in one of the three
declared input reports (the control or data role): a one-byte length and
then the reply with its logical ID first,

  [carrier report ID][u8 reply_len][reply_id][u32 step][u32 status][payload]

HID core strips the carrier report ID, so the client was handed the carrier's
ID (e.g. 0x20) while it awaited the logical reply ID (e.g. 0x40). The two never
matched: every reply was counted as dropped and the command timed out, so no
pairing or session write got past its first reply ("pairing stopped:
Transport(1, -110)").

Match the awaited reply by its logical ID in the report body and hand the
client the reply alone, its ID first, as the command callers parse it. The
larger replies and sensor frames arriving under the undeclared 0x40-0x4f and
0xb0-0xbf report IDs are read where those are handled.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
A fresh keyboard pairing sends the host object, the certificate chain
and the enclave material as logical reports whose bodies run past the
few dozen bytes one USB feature SET takes on this interface. An unpaired
keyboard will not take those as one direct feature SET (it STALLs a
direct SET 0x60); only the already-paired session uses a direct SET. A
fresh pairing slices each large body into fixed 44-byte feature reports
on report ID 0xfe: [0xfe][logical id][u16 segment word][40-byte chunk],
the last chunk zero-padded. The segment word tags the first frame with
the frame count, continuations with their index and the last with its
valid-byte count. Between frames the keyboard asks the host to read one
feature report first.

Add the 0xfe encoder and the between-frame notification parser to
stac_ta_wire.rs with KUnit cover (the four fragmented pairing reports,
their exact frame counts and segment words, a fail-closed reassembler
and its rejection cases), and route the Unpaired pairing steps through
it in stac_client.rs. Small reports and the already-paired session path
keep their direct feature SETs.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
…n them

A fresh Touch ID pairing sends the large host objects (steps 2/4/5/6/7)
over the 0xfe USB feature transport, one logical report sliced into fixed
44-byte frames. ta_set_logical() sent the first frame and then blocked on a
per-frame "between-frame notification" input report before sending the next.

On a real keyboard (J616s, fleet71stac6) that notification never arrives:
after the FIRST frame (segment word 0x4004) is accepted, the keyboard sends
nothing and waits for the next frame, while the host waits for a reply that
will not come -- a deadlock that the 2 s command timeout reports as
"pairing stopped: Transport(2, -110)". The live usbmon capture shows the one
pending input URB sitting unfilled until it is unlinked (-2); no input report
crosses the bus between frames. The between-frame notification is only in the
macOS relay's static disassembly (t-6693 section 7, flagged pending and
unconfirmed), and the macOS wire capture streams the frames back to back far
faster than an input + GET round trip per frame would allow.

Stream the frames: send every non-final frame as a fire-and-forget feature
SET and await the single logical reply only after the last frame. The 0xfe
encoder and segment words are unchanged. parse_fragment_notification() is kept
for the documented macOS lower-transport path and the tests.

KUnit: apple_sep_stac_ta_wire 16/16 pass, 644 run / 642 pass / 2 skip / 0 fail.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
A logical pairing report whose body is too large for one feature SET is
sliced into fixed 44-byte 0xfe transport frames. The final frame holds
fewer valid bytes than a full chunk. Earlier code zero-padded the bytes
after the valid prefix, and a later attempt trimmed them off; a Magic
Keyboard with Touch ID stalls the final frame (EPIPE) in both forms,
which blocked the host-object check at step 2 of a fresh pairing.

The transport encoder emits the frames out of one reused fixed-length
buffer and rewrites only the final frame's valid prefix, so the bytes
after it are whatever the preceding full frame left there. The keyboard
commits the logical object on the final frame and accepts it only with
those carried bytes present. Reproduce the carry-forward explicitly
(the preceding frame starts one chunk earlier) instead of zero-filling
or trimming, keeping every frame at the fixed 44-byte length and the
segment words unchanged. Reassembly is unaffected: the keyboard reads
only the valid prefix of the final frame.

Extend the wire KUnit tests to pin the final frame's carry-forward
layout for the four-frame 137-byte step-2 report (exact segment words,
17 valid body bytes then the preceding frame's 23-byte tail) and to
require every frame to keep the fixed length.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
A Magic Keyboard with Touch ID that is powered on cold comes up before it
has selected the trusted-accessory communication protocol. In that state
it stalls the identity feature reads, so the attach identify failed
(-EPIPE) and the keyboard never reached the state the pairing trigger
requires; a keyboard the other OS had already handed off answered the
same reads and identified, which hid the dependency.

macOS performs a fixed handshake on every attach before it knows whether
to pair: read the protocol tag and version, echo the tag back to select
the protocol, read the identity, request the identity point, then read
the fresh connection nonce. The protocol handoff is what brings a cold
keyboard to where it answers its identity.

Run that handshake on identify instead of three bare reads. Factor the
sequence into one routine over a small channel trait so the identify,
pairing and session paths share a single definition of the order (the
pairing and session relays already opened with the same five exchanges);
the routine's only writes are the two protocol-handshake reports and it
changes no pairing or enclave state. Move the identify work's body to the
Rust side, which drives the reads and the handshake writes through the
existing command/read entry points under the client lock the work holds,
so the one callback replaces the previous identified/failed pair.

Add a KUnit test that drives the handshake over a recording channel and
pins the exact macOS order (GET 0x4b, SET 0x68, GET 0x41, SET 0x67
answered by IN 0x40, GET 0x4c) and the returned identity and nonce, plus
a case that refuses a foreign protocol tag before any write.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
The keyboard numbers the host's commands within one physical attach. The
identity request takes the first sequence; the pairing-state check the one
after it. The keyboard tracks that counter and rejects a command that
repeats a sequence it has already seen.

The attach handshake was a stateless routine that hard-coded the identity
request at the first sequence, and it ran twice: once on identify and
again at the start of a pairing (and a session). The second run re-sent
the identity request with the first sequence, a replay within the attach,
before the pairing check went out at the second sequence. The keyboard
accepted the earlier 0xfe frames of the step-2 host-object command and
then stalled the last one as it committed the logical command, which
stopped the pairing with a transport error.

Run the handshake once per attach. On identify, keep its result in a small
per-attach context: the identity, its point, the connection nonce, and the
next command sequence (one past the identity request's). A later pairing
or session reuses that context instead of running the handshake again, so
the identity request goes out once and the pairing check carries the live
sequence that follows it. The context is cleared on detach; the next
attach builds its own.

The pairing path draws every command's sequence from the one counter, so
on a fresh attach the identity request is 1, the pairing check 2, and the
remaining steps follow in order. The session reuses the recording's fixed
message numbers for its later steps (it uses a non-consecutive subset),
but takes the carried sequence for its pairing check.

Add a KUnit test that runs the handshake over the recording channel and
asserts the identity request is issued exactly once at the first sequence
and that the pairing check then carries the next sequence (2) rather than
a replayed first. Update the order test for the returned context.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

ta_pair and ta_session in stac_ta_shim.c are write-only module parameters, and they are the only way pairing and the per-connection session ever run. With a default boot and no manual step, the keyboard's Touch ID never comes up. The session belongs in the hid-apple-ta client lifecycle, run when the keyboard connects. Pairing should either follow from an unpaired report or go through a per-device interface that a pairing agent can drive. A global module parameter is not that interface. Until then this should stay a draft.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Closing: pairing and sessions only run via module parameters; the session belongs in the client lifecycle and pairing needs a per-device interface. The Touch ID key mapping can come back on its own.

@RyanTheTide RyanTheTide reopened this Nov 22, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Reopened and kept as a draft; the comment above lists what it still needs before review.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Closing: pairing and sessions only run via module parameters; the session belongs in the client lifecycle and pairing needs a per-device interface. The Touch ID key mapping can come back on its own.

RyanTheTide added a commit that referenced this pull request Nov 26, 2026
…and tighten docs

Finalize diag/{sse,hibe,skdl,stac,pnon} before publishing bringup_ended
and PHASE_READY on both normal and early-failure bring-up paths so readers
observing completion cannot transiently see "pending".

Also keep docs/sep-endpoints/{README,pnon,stac}.md strictly scoped to
capabilities and operations established on main (removing unmerged raw
opcode numbers and noting that earlier draft PR #1810 was closed).

Signed-off-by: Ryan Murray <[email protected]>
maxberggren added a commit to maxberggren/linux-aurora that referenced this pull request Nov 30, 2026
Wire the Secure Enclave stac (trusted accessory), scrd, and sbio subsystems
to hid-apple-ta so Magic Keyboards with Touch ID work alongside the onboard
Touch ID sensor (and in keyboard-only desktop Mac configurations) via
/dev/sep-bio and fprintd:

- Run per-connection sessions, connection-state tracking (0x09), and detach
  grace finalization (0x08) automatically in the client lifecycle.
- Wire pairing and hardware double-press authorization (SCRD
  AccessoryAuthorization preflight + stac 0x14) to the keyboard's per-device
  touchid/{pair,authorize,session} sysfs attributes.
- Add standby transport promotion for USB <-> Bluetooth switching,
  non-blocking detach wakeups, serialized SCRD/STAC exchanges, and full
  biometric/ACM buffer zeroization.
- Extend sbio verify and enrolment to support accessory image-processing init
  (biodevice type 2 + accessory UID + capture metadata) and keyboard-only
  device-view restore proof when no onboard SPI sensor is present.

Builds on the stac protocol, lifecycle, and pairing wire foundation from
aurora-silicon/linux#1810.

Co-authored-by: Ryan Murray <[email protected]>
Signed-off-by: Max Berggren <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants