0013 · Touch ID on macOS: Swift shim with CryptoKit, Secure Enclave key without entitlements, and mlock postponed
- Status: accepted
- Date: 2026-11-02
Context
The design (§4.3) calls for unlocking klavd with Touch ID: a P-256 key in the Secure Enclave, with the biometryCurrentSet ACL, wraps the device's local key (spec §12.2). In addition:
- the password must be asked for again every N days (14 by default) and after a restart;
- the binary is signed and notarized;
unsafeis only allowed in the native bridge, with// SAFETY:comments.
The spec (§12.1) and ADR 0011 left the mlock decision to this ADR.
Decision
- Swift shim with CryptoKit, in its own crate,
crates/klav-touchid(Apache-2.0).build.rscompilesswift/shim.swiftwithswiftc -emit-library -staticand links it with CoreGraphics, CryptoKit, Foundation, LocalAuthentication and Security. The system Swift libraries (/usr/lib/swift, in the shared cache) are linked with absolute paths, withoutrpath.- Without macOS, without
swiftcor withKLAV_NO_SWIFT=1, nothing is compiled and every function returnsUnavailable. That way CI on Linux and a Mac without Xcode keep working, andklavduses the password. - It is the only crate with
unsafetoday. It repeats the workspace lints withunsafe_code = "warn"(allowed only when the shim is compiled) andclippy::undocumented_unsafe_blocks = "deny". - The C interface uses caller buffers with fixed sizes (65-byte points, 32-byte secrets) plus the handle with capacity and length. No memory is freed across languages.
- Secure Enclave key stored as
dataRepresentation(SecureEnclave.P256.KeyAgreement.PrivateKey), not in the Keychain.- That handle is encrypted by the Secure Enclave itself and only works on this Mac.
- It doesn't need
keychain-access-groups, a provisioning profile or an app bundle. This was checked on this machine: an unsigned binary, an ad hoc one and the one signed with Developer ID and hardened runtime all create the key, reload it and wrap without a dialog. - Rejected alternative:
SecKeyCreateRandomKeywithkSecAttrTokenIDSecureEnclaveandkSecAttrIsPermanent. It stores the key in the data protection Keychain, which requires the keychain group entitlement and, with it, a provisioning profile. - Rejected alternative: the
security-frameworkandobjc2-*crates, which add dependencies and don't avoid that problem.
- ECDH in the shim, everything else in
klav-core.- When wrapping, CryptoKit creates an ephemeral P-256 pair and computes the ECDH with the enclave's public key. There is no dialog.
- When unwrapping, it first authenticates with
LAContext.evaluatePolicyand then the enclave computes the ECDH with that context. Authenticating first makes it possible to tell "the user canceled" apart from "the key no longer works". - The shim only returns the shared secret. HKDF and the XChaCha20-Poly1305 sealing of the local key (type
0x42) belong toklav-core(spec §12.3, vectorllavero.json). - No new cryptographic dependency enters Rust: P-256 lives in CryptoKit, the system library.
- Policy, bound in the AAD:
password_at_ms(last password unlock) andboot_time_s(boot, throughsysctl kern.boottime) go in the AAD of type0x42;klavdrequires the password if 14 days have passed or if the boot changed;- editing
touchid.jsonto extend the deadline makes the blob stop opening; - every password unlock rewraps with a new ephemeral pair.
- Automatic enrollment and opt-out:
- the first password unlock sets up Touch ID, without a dialog, if it is available (design §4.3);
klavd --no-touch-idorKLAV_NO_TOUCH_ID=1turn it off;klav unlock --passwordand--password-stdinuse the password, and scripts never wait for a fingerprint.
- Errors:
- canceling or failing the fingerprint gives
biometry_failedand keeps the enrollment; - if authentication works but the key doesn't (the fingerprints changed,
biometryCurrentSet), or the file doesn't open, the enrollment is deleted and the response isbiometry_unavailable; - in any of those cases
klavasks for the password, which sets up Touch ID again.
- canceling or failing the fingerprint gives
- Lock on screen lock:
klavdpollsCGSessionCopyCurrentDictionary()(CGSSessionScreenIsLocked) every 2 s. With no graphical session (SSH) it is treated as unlocked. - Signing and notarization (
scripts/macos/):build-sign.shbuilds in release and signsklavandklavdwith the Developer ID identity (SZ3F58F8LX), hardened runtime and a timestamp. No entitlements are needed. It packages them in a zip.notarize.shsubmits it withnotarytoolusing a Keychain profile (KLAV_NOTARY_PROFILE). There are never credentials in the repository or in argv.- Command-line binaries can't be stapled; Gatekeeper checks the ticket online.
mlock: postponed.- The attacker who can read swap or a memory dump is root or the same user, who can already read
klavd's live memory (out of scope, threat model §6). - macOS encrypts swap.
- Doing it properly requires
unsafeormemsec/regionon every secret type inklav-coreand turning off core dumps. - It will be revisited with the desktop app or if the model changes. Meanwhile, the defense is
zeroize/secrecyand a short window: lock on idle, on sleep and on screen lock.
- The attacker who can read swap or a memory dump is root or the same user, who can already read
Consequences
- Not tested automatically: the Touch ID dialog from a daemon without a bundle needs a finger.
- The
touch_id_round_triptest is marked#[ignore]; it runs withcargo test -p klav-touchid -- --ignored. - Enrollment, wrapping and policy are tested without a dialog, on macOS and with vectors.
- The
- Building
klavdwith Touch ID requires the Xcode Command Line Tools. Without them, the binary works only with the password andbuild.rswarns about it. - A
touchid.jsoncopied to another Mac is useless: the handle only works with its own Secure Enclave, and the AAD binds the account and the device.