Skip to content

iOS: a Secure Enclave approval key, so payments and logins can be signed on an iPhone - #137

Draft
dbejarano820 wants to merge 2 commits into
mainfrom
dbejarano820/125-ios-secure-enclave-key
Draft

dbejarano820 wants to merge 2 commits into
mainfrom
dbejarano820/125-ios-secure-enclave-key

Conversation

@dbejarano820

Copy link
Copy Markdown
Contributor

What and why

Closes #125

On iOS the approval key failed closed. This puts a real key in the Secure Enclave, behind the same contract as the Android Keystore key: P-256, never exported, Face ID or Touch ID for each signature, no passcode fallback, and lost when a face or a finger is added or removed. Payments and logins still stay off on an iPhone for now, because the vault and the wallet have no hardware-wrapped key on iOS yet (docs/devices.md says so).

What the module does (apps/mobile/modules/hermit-approval-key/ios):

  • The key. SecKeyCreateRandomKey with kSecAttrTokenIDSecureEnclave, P-256, stored under the tag hermit.approval-key.v1. Its access control is [.privateKeyUsage, .biometryCurrentSet], accessible only WhenPasscodeSetThisDeviceOnly. A key whose attributes don't say Secure Enclave and 256 bits is never used. If the Secure Enclave can't make the key, enrollment rejects with no_secure_hardware and leaves nothing behind (for example on the Simulator). No software key is ever made.
  • What is signed. The exact UTF-8 bytes of the message, with .ecdsaSignatureMessageX962SHA256. That gives ECDSA SHA-256 in DER, the same as Android's SHA256withECDSA, so the cage needs no conversion. The public key is the X9.63 point behind the P-256 SubjectPublicKeyInfo header (ApprovalKeyFormat.spki), so it reads the same as Android's getEncoded().
  • One prompt, one signature. Each enroll and sign uses a fresh LAContext with reuse duration 0, an empty fallback title (no "Enter Passcode" button), and is invalidated afterwards. At most one prompt is open at a time (busy). sign refuses before any prompt when the message isn't an approval (bad_message), has expired (expired), there's no key (lost), or the biometric set changed (lost).
  • The prompt's words. ApprovalText.swift is a port of ApprovalText.kt: the module's own strings, picked by locale code, with every fact read from the message being signed. JavaScript passes no display text.
  • lost without a prompt. At enrollment the module saves evaluatedPolicyDomainState in a Keychain item on this device only. state compares it with the current set, and a different or missing set means lost. The Secure Enclave enforces this on its own as well: .biometryCurrentSet stops the key from signing after any change.
  • States and errors. state returns no_biometric when Face ID or Touch ID can't be used (a lockout still counts as enrolled), then none, lost or ready with level: "secure_enclave". Errors from the prompt map to the existing codes: cancelled, lockout, no_biometric, lost and failed.

On the JS side, ApprovalKeyLevel gains secure_enclave (APPROVAL_KEY_LEVELS in core). The runtime pins it, and the approval key screen has a line for it in both locales. Android is unchanged.

Vision fit

Hermit asks for your fingerprint before money moves, and the same promise now holds on an iPhone: the key that approves a payment or a login lives in hardware and signs only behind your face or finger.

Non-negotiables

  • This keeps every non-negotiable in AGENTS.md as it is.

  • Or it changes one, and this is the accepted RFC: #

  • 1, 2 and 3 (data stays on the phone, no telemetry, no network in packages): no network code, no dependency, nothing leaves the phone. The key and the biometric-set record are ThisDeviceOnly Keychain items.

  • 4 (the cage decides): nothing skips decide() or the log. The cage still verifies every signature against the pinned key (verifySignedApproval) and spends its nonce. The module only signs.

  • 5 (untrusted text): unchanged. Taint still refuses money and logins before any sheet opens.

  • 6 (money and logins need the sheet and the biometric): this is the hard part, and it holds in hardware. The access control allows biometry only, from the current set, with no passcode, and needs a new match for each signature. The approval stays bound to argsHash, the request id, the nonce and the expiry, because those are in the signed bytes. A software key is never made.

  • 7 (sheets show only code-written words): the system prompt's text comes from ApprovalText.swift, the module's own strings plus the facts from the signed payload with hidden characters stripped. No caller text is used.

  • 8 (testnet only): unchanged.

  • 9 (copy): one new key, HM-APPR-01.key.level.secure_enclave, in en and es-419, with no dashes. The prompt strings still say fingerprint, as on Android. Face ID wording is iOS: permission prompts in both languages, and copy that says Face ID instead of fingerprint #128.

  • 10 (tests first, made-up data): see below. The vector comes from a throwaway software key.

  • 11 to 13: n/a.

Checklist

  • It closes one issue, linked above, or it's a small fix (a typo, a broken link, a wrong comment). A feature has an accepted proposal.
  • I wrote the failing test first. It fails without this change and passes with it.
  • These pass: pnpm check:privacy, pnpm typecheck, pnpm lint, pnpm test and node scripts/guard/check.mjs --patterns scripts/guard/patterns.txt.
  • No new network call, telemetry, analytics, ads or crash reporting. (A new request in an app adapter is explained below, with its host, skill and risk.)
  • No new dependency, or the reason for each one is below.
  • Every UI string is in packages/copy, in es-419 and en, with matching keys and placeholders, and no em or en dashes.
  • Tests, fixtures and screenshots use made-up data only.

Tests written first, and the reason each one failed:

  • packages/runtime/test/approval-key.test.ts, "an iPhone's Secure Enclave key is pinned with its level and reads back as ready". It failed with expected { kind: 'not_set_up' } to deeply equal { kind: 'ready', …}, because the pin rejected the new level.
  • packages/copy/test/copy.test.ts, "every place an approval key can be kept has its line on the approval key screen". It failed because APPROVAL_KEY_LEVELS didn't exist yet.
  • packages/cage/test/signed-approval.test.ts, "an iPhone's Secure Enclave key". This is a known vector made once on a Mac with a throwaway software P-256 SecKey, using the module's own export (ApprovalKeyFormat.spki) and signature calls. The public key and the enrollment and payment signatures verify as they come. A changed amount is mismatch, and the changed payload with the old signature is bad_signature. This test passed without any cage change: it shows the iOS formats already match what the cage verifies, so no conversion is needed.
  • ApprovalText.swift was checked against every case in ApprovalTextTest.kt with a local swiftc harness, and all of them pass. That harness isn't committed, because the pod has no test target.

Model path

  • This doesn't touch the model path: packages/model, agent, skills or eval, the prompt, the router, tool descriptions, normalizers, examples or the model file.
  • It does. The eval numbers from before and after are below, run as docs/evals.md says. No held-out case was tuned on.

Screenshots

  • No UI change.
  • UI change. Each changed screen is below, in both themes and both languages, with made-up data.

There is one new line on the approval key screen, and only on an iPhone: "Kept in this iPhone's Secure Enclave." / "Guardada en el Secure Enclave de este iPhone." The screenshots are still to come from a phone run, so none are below yet.

New dependencies and network calls

n/a. The module uses Apple's Security and LocalAuthentication frameworks, which make no network calls.

Still needs a phone test

Not run on a device yet:

  1. Set up the approval key on an iPhone with Face ID: the screen says ready, with the Secure Enclave line.
  2. Sign an approval once, then see a changed payload refused. The vault and the wallet are off on iOS, so payment and login sheets can't be reached in the app yet. This step waits on them, or on a build that exercises sign through the normal sheet.
  3. Add an alternate appearance (or a finger), and the screen says lost. Set the key up again and it's ready.
  4. Cancel the prompt, and nothing is pinned.
  5. Check what the Face ID prompt shows of the reason text.

AI agents

  • No AI agent wrote any part of this.
  • An AI agent helped. Which one, and what it did: Claude Code (Claude Opus 5.5) wrote the Swift module, the port of ApprovalText, the tests and this description. It ran the gates and an unsigned Release build for a generic iOS device. It did not run anything on a phone.
  • I read and ran everything in this pull request, and I can explain it.

@juancguerrerodev

Copy link
Copy Markdown
Contributor

Holding this one until it's been checked on an iPhone, as the description says. It's the approval key for payments and logins, so the phone test is the one that counts. Merge it yourself once it passes, or say so here and I'll merge it. 🦀

…ned on an iPhone (#125)

The iOS approval key module no longer fails closed. It makes a P-256 key
in the Secure Enclave with access control [.privateKeyUsage,
.biometryCurrentSet], WhenPasscodeSetThisDeviceOnly: Face ID or Touch ID
for every signature, no passcode fallback, never a software key. It signs
the exact UTF-8 message with ecdsaSignatureMessageX962SHA256 (DER, as
Android's SHA256withECDSA) and reports the public key as a P-256
SubjectPublicKeyInfo, so the cage verifies it unchanged.

state() reports lost without a prompt when the enrolled biometric set
changed. The prompt's words come from ApprovalText.swift, a port of
ApprovalText.kt. ApprovalKeyLevel gains secure_enclave, with its line on
the approval key screen in both locales.

Signed-off-by: Daniel Bejarano <dbejarano820@gmail.com>
…fingerprint (#125)

ApprovalText.swift was a port of Android's text and its enrollment line said
fingerprint. It now names this iPhone's biometric from LAContext.biometryType,
as the app's copy does (common.biometric.*): Face ID, Touch ID, or both names
when the phone doesn't say. The payment and password prompts had no biometric
word and are unchanged.

Signed-off-by: Daniel Bejarano <dbejarano820@gmail.com>
@dbejarano820
dbejarano820 force-pushed the dbejarano820/125-ios-secure-enclave-key branch from 9997284 to 0093200 Compare November 30, 2026 22:45
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.

iOS: a Secure Enclave approval key, so payments and logins can work on an iPhone

2 participants