Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
recovery: pin the PRF input and the CTAP hmac-secret salt (#781)
nyblnetb75675e1e6fe
diff --git a/kernel/fixtures/README.md b/kernel/fixtures/README.mdindex 19e8c91e..6b116a2e 100644--- a/kernel/fixtures/README.md+++ b/kernel/fixtures/README.md@@ -36,6 +36,24 @@ bytes end to end. Reference: `kernel/src/recovery.ts`. Rig:scalar alone while kx is unruled, or with kx appended under modes A and C. Onimport, each scalar must be in 1..n−1 AND its public point d·G must equal thepublic key in the directory-signed binding. A swapped blob fails closed.+- **The PRF input is the UTF-8 bytes of `bento/id/prf/v1`.** A host that+ speaks CTAP `hmac-secret` directly, with no WebAuthn PRF API (libfido2 on+ desktop), must send `prf.hmacSecretSalt`: SHA-256("WebAuthn PRF" ‖ 0x00 ‖+ input), per WebAuthn L3 §10.1.4. Then the same security key returns the same+ 32 bytes in a browser and on the desktop, and a blob sealed on one opens on+ the other. The `prf.wrong` values are the easy mistakes. (home-desktop)++ Apply that hash EXACTLY ONCE, and only where nothing else does (security §34):++ | Path | Who hashes | What you pass |+ |---|---|---|+ | CTAP `hmac-secret` directly (libfido2: Linux, any host driving the key itself) | **you** | `prf.hmacSecretSalt` |+ | a browser's WebAuthn PRF extension | the browser | the input (`prf.inputHex`), never the salt |+ | Windows `webauthn.dll` with the PRF extension | Windows | the input, never the salt |+ | a platform's own WebAuthn PRF API (Apple AuthenticationServices, Android Credential Manager) | the platform | the input, never the salt |++ Passing the salt to an API that hashes again gives the `SHA-256` of the salt:+ a third value, and a blob nothing else can open.- **The pepper layer** is the directory's (share implements it). Its rows existso the server code can be checked against the same bytes.diff --git a/kernel/fixtures/recovery-v1.json b/kernel/fixtures/recovery-v1.jsonindex 41b518fd..bb38e811 100644--- a/kernel/fixtures/recovery-v1.json+++ b/kernel/fixtures/recovery-v1.json@@ -222,6 +222,17 @@},"prfKek": "cb2d620d31cc776d953b939c82d8196096591589f3a5bcc12ac21218b9406f06"},+ "prf": {+ "note": "The PRF extension evaluates input = UTF-8(prfInput). A host speaking CTAP hmac-secret directly (no WebAuthn PRF API) sends hmacSecretSalt = SHA-256(UTF-8(\"WebAuthn PRF\") ‖ 0x00 ‖ input) (WebAuthn L3 §10.1.4), so the authenticator returns the same 32 bytes a browser gets. Browsers apply this themselves: never twice.",+ "prfInput": "bento/id/prf/v1",+ "inputHex": "62656e746f2f69642f7072662f7631",+ "hmacSecretSalt": "f99670d5db28529b25c8a9ec469959b60006b8d6625066cc77f075dab7ea3b77",+ "wrong": {+ "noSeparator": "9bd7977da368bf160952ba577eb62abcb1b4e105c29f0abc2ba3e41f2c09b240",+ "rawInputHash": "0348893aa98ff0466b2e8bba1ebf6f13f6e7d5e773b2f8ab83d79e44bd02720d",+ "rawInput": "62656e746f2f69642f7072662f7631"+ }+ },"blobs": {"keys": {"accountD": "e5526afbbded1a62046b9985419c6c5023b6f341313ed2f31642c4000c5fbad4",diff --git a/kernel/src/recovery.ts b/kernel/src/recovery.tsindex 4aae6539..cfc992ad 100644--- a/kernel/src/recovery.ts+++ b/kernel/src/recovery.ts@@ -105,6 +105,27 @@ export async function recoveryVerifier(code: Uint8Array, saltV: Uint8Array, argoreturn argon2(code, saltV)}+/**+ * The PRF extension's FIXED input (identity-design §2f), as UTF-8 bytes: every+ * ceremony evaluates exactly this, so one passkey always yields one PRF output.+ */+export const PRF_INPUT = 'bento/id/prf/v1'++/**+ * What a host that speaks CTAP directly (libfido2 hmac-secret, no WebAuthn PRF+ * API) must send as the hmac-secret salt so the authenticator returns the SAME+ * 32 bytes a browser's PRF extension would. WebAuthn Level 3 §10.1.4:+ * salt = SHA-256(UTF-8("WebAuthn PRF") ‖ 0x00 ‖ input). Forget the 0x00, or+ * send the raw input, and the blob you seal can never be opened by a browser+ * (and vice versa). Browsers do this themselves; never apply it twice.+ */+export async function prfHmacSecretSalt(input: Uint8Array = te.encode(PRF_INPUT)): Promise<Uint8Array> {+ const label = te.encode('WebAuthn PRF')+ const msg = new Uint8Array(label.length + 1 + input.length)+ msg.set(label); msg[label.length] = 0; msg.set(input, label.length + 1)+ return new Uint8Array(await crypto.subtle.digest('SHA-256', msg as BufferSource))+}+/** The PRF blob's key: HKDF-SHA-256(PRF output, 32-byte salt, "bento/id/acct-wrap/v1"). */export async function prfKek(prfOutput: Uint8Array, salt: Uint8Array): Promise<Uint8Array> {checkLen(prfOutput, 32, 'PRF output'); checkLen(salt, 32, 'salt')diff --git a/scripts/gen-recovery-vectors.ts b/scripts/gen-recovery-vectors.tsindex 95a8cb42..58c65820 100644--- a/scripts/gen-recovery-vectors.ts+++ b/scripts/gen-recovery-vectors.ts@@ -16,7 +16,7 @@ import { join, dirname } from 'node:path'import { fileURLToPath } from 'node:url'import {ARGON2, formatRecoveryCode, parseRecoveryCode, recoveryWrapKey, recoveryVerifier, prfKek,- sealPrfBlob, sealRecoveryInner, pepperKeys, pepperVerifierTag, pepperSeal, publicKeyOf, type Argon2id,+ sealPrfBlob, sealRecoveryInner, pepperKeys, pepperVerifierTag, pepperSeal, publicKeyOf, PRF_INPUT, prfHmacSecretSalt, type Argon2id,} from '../kernel/src/recovery.ts'const OUT = join(dirname(fileURLToPath(import.meta.url)), '..', 'kernel/fixtures/recovery-v1.json')@@ -92,6 +92,26 @@ const derivations = {}derivations.prfKek = hex(await prfKek(bytes(32, (i) => 0xa0 + i), bytes(32, (i) => 0x70 + i)))+// --- the PRF input, and the CTAP hmac-secret salt for hosts without a PRF API -------------+// WebAuthn L3 §10.1.4. The two "wrong" values are the easy mistakes, published so+// a host can see it has made one rather than silently sealing unopenable blobs.+const sha256hex = async (b: Uint8Array) => hex(new Uint8Array(await crypto.subtle.digest('SHA-256', b as BufferSource)))+const prfInputBytes = new TextEncoder().encode(PRF_INPUT)+const label = new TextEncoder().encode('WebAuthn PRF')+const prf = {+ note: 'The PRF extension evaluates input = UTF-8(prfInput). A host speaking CTAP hmac-secret directly ' ++ '(no WebAuthn PRF API) sends hmacSecretSalt = SHA-256(UTF-8("WebAuthn PRF") ‖ 0x00 ‖ input) (WebAuthn L3 §10.1.4), ' ++ 'so the authenticator returns the same 32 bytes a browser gets. Browsers apply this themselves: never twice.',+ prfInput: PRF_INPUT,+ inputHex: hex(prfInputBytes),+ hmacSecretSalt: hex(await prfHmacSecretSalt()),+ wrong: {+ noSeparator: await sha256hex(Uint8Array.from([...label, ...prfInputBytes])),+ rawInputHash: await sha256hex(prfInputBytes),+ rawInput: hex(prfInputBytes),+ },+}+// --- whole blobs (end to end) --------------------------------------------------------------// Test keys are DETERMINISTIC (fixed scalars), so regenerating this file changes// nothing unless a rule changes: porters can build against stable bytes.@@ -146,6 +166,7 @@ const fixture = {codes,parse,derivations,+ prf,blobs,scalars,}diff --git a/scripts/test-recovery.ts b/scripts/test-recovery.tsindex 362195f4..da332218 100644--- a/scripts/test-recovery.ts+++ b/scripts/test-recovery.ts@@ -21,7 +21,7 @@ import { fileURLToPath } from 'node:url'import {ARGON2, formatRecoveryCode, parseRecoveryCode, recoveryWrapKey, recoveryVerifier, prfKek,sealPrfBlob, openPrfBlob, sealRecoveryInner, openRecoveryInner, unpackScalars,- pepperKeys, pepperVerifierTag, pepperSeal, pepperOpen, publicKeyOf, type Argon2id,+ pepperKeys, pepperVerifierTag, pepperSeal, pepperOpen, publicKeyOf, PRF_INPUT, prfHmacSecretSalt, type Argon2id,} from '../kernel/src/recovery.ts'let failures = 0@@ -74,6 +74,21 @@ console.log('\nderivations')ok(hex(await prfKek(unhex(d.prf.output), unhex(d.prf.salt))) === d.prfKek, 'PRF KEK = HKDF(PRF output, salt, "bento/id/acct-wrap/v1")')}+console.log('\nthe PRF input, and the CTAP hmac-secret salt (WebAuthn L3 §10.1.4)')+{+ const v = F.prf+ ok(v.prfInput === PRF_INPUT && v.prfInput === 'bento/id/prf/v1', 'the fixed PRF input is "bento/id/prf/v1" (identity-design §2f)')+ ok(v.inputHex === Buffer.from('bento/id/prf/v1', 'utf8').toString('hex'), 'the input is its UTF-8 bytes')+ const salt = hex(await prfHmacSecretSalt())+ ok(salt === v.hmacSecretSalt, 'hmac-secret salt = SHA-256("WebAuthn PRF" ‖ 0x00 ‖ input)')+ // independent of the reference: the spec's formula, written out with node's own hash+ const { createHash } = await import('node:crypto')+ const byHand = createHash('sha256').update(Buffer.concat([Buffer.from('WebAuthn PRF'), Buffer.from([0]), Buffer.from('bento/id/prf/v1')])).digest('hex')+ ok(byHand === v.hmacSecretSalt, "and node's own SHA-256 of the spec's bytes agrees")+ ok(new Set([v.hmacSecretSalt, v.wrong.noSeparator, v.wrong.rawInputHash, v.wrong.rawInput]).size === 4,+ 'each easy mistake (no 0x00, hashing the raw input, the raw input) gives a different salt')+}+console.log('\nwhole blobs, end to end'){const b = F.blobs, k = b.keys, i = b.prf.inputs