SPB Git

Repository Public

CRYPTOGRAPHY.md 6.7 KB 91 lines markdown

1# Cryptography23SP Vault uses only Apple's audited primitives: CryptoKit (AES-GCM, HKDF, SHA-256), CommonCrypto (PBKDF2), the Security framework (Keychain, `SecRandomCopyBytes`) and LocalAuthentication. There is no home-made algorithm. The code is in `SPVault/Core/Cryptography/`, `SPVault/Core/Biometrics/`, `SPVault/Core/Storage/` and `SPVault/Engines/Backup/`.45## Key hierarchy67```8Face ID (current biometric set)9        │ unlocks10        ▼11Keychain item ── master key K (256 bits, random)12        │13        ├─ HKDF-SHA256(K, salt = vault id, "spvault/keywrap/v1")  ──► wraps each card key14        ├─ HKDF-SHA256(K, salt = vault id, "spvault/manifest/v1") ──► manifest.bin15        └─ HKDF-SHA256(K, salt = vault id, "spvault/search/v1")   ──► search.idx1617card key Kc (256 bits, random, one per card)18        ├─ record  (cards/<id>/meta)19        └─ photographs (front, back, thumbnails)2021recovery passphrase ── PBKDF2-HMAC-SHA256 ──► wraps K in recovery.key   (optional)22backup passphrase   ── PBKDF2-HMAC-SHA256 ── HKDF ──► .spvault records23```2425| Item | Choice |26|---|---|27| Cipher | AES-256-GCM (`CryptoKit.AES.GCM`), 96-bit nonce, 128-bit tag |28| Nonce | `AES.GCM.Nonce()` — fresh from the system CSPRNG for every seal; never derived, never reused by construction |29| Master key | `SymmetricKey(size: .bits256)`, generated on the device at vault creation |30| Per-card key | `SymmetricKey(size: .bits256)`, generated when the card is saved, wrapped (AES-256-GCM) by the key-wrap sub-key |31| Sub-keys | HKDF-SHA256, salt = the 16 bytes of the vault id, one `info` string per purpose |32| Passphrase KDF | PBKDF2-HMAC-SHA256 (`CCKeyDerivationPBKDF`), 600 000 iterations, 32-byte random salt, 32-byte output. Files with fewer than 100 000 iterations are refused on read |33| Randomness | `SecRandomCopyBytes` / CryptoKit |3435## Sealed blob format (SPV1)3637Every encrypted file is one sealed blob:3839```40offset  size  field410       4     magic "SPV1"424       1     format version (1)435       1     kind: 1 wrapped key · 2 record · 3 image · 4 manifest · 5 search index · 6 recovery wrap · 7 backup record446       12    nonce4518      n     ciphertext4618+n    16    GCM tag47```4849Additional authenticated data = `magic | version | kind | context`, where the context is `vault id | card id | file name` (or `vault id | "manifest"`, etc.). Consequences, each covered by a unit test:5051- any altered byte, in the header, the nonce, the ciphertext or the tag, fails authentication;52- a blob cannot be moved to another card, another file name or another vault;53- a blob of one kind is never accepted as another kind;54- an unknown version is refused before any decryption.5556A card whose files fail authentication is reported as damaged in the Security Center. It is never silently dropped or "repaired".5758## Where the master key lives5960`KeychainBiometricKeyProtector` stores the 32 bytes as a generic-password item with:6162- `kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly` — never leaves the device, not in backups, gone if the passcode is removed;63- `SecAccessControl` with `.biometryCurrentSet` — every read needs a successful match against the biometric set that existed when the item was written. Enrolling or removing a face invalidates the item;64- no passcode fallback (`localizedFallbackTitle = ""`, biometry-only access control).6566Unlocking *is* reading this item: `Vault.unlock()` calls `SecItemCopyMatching` with an `LAContext`, and nothing can be decrypted without the bytes it returns. There is no flag, password or debug path that opens the vault in a Release build.6768**Secure Enclave.** The access control of this Keychain item is enforced by the Secure Enclave (it holds the class keys and performs the biometric match). The master key itself is a symmetric key, which the Secure Enclave cannot generate or hold as a non-exportable key (it only holds P-256 private keys). Wrapping the master key with an enclave-held P-256 key (ECIES) was considered and left out of 1.0: it adds a second mechanism that cannot be tested without a physical iPhone, for a marginal gain over a `biometryCurrentSet` Keychain item. It is listed as future work in `SECURITY_AUDIT.md`.6970**Simulator.** The simulator has no passcode and no Secure Enclave, so the item above cannot be created there. A `SimulatorKeyProtector`, compiled only for the simulator (`#if targetEnvironment(simulator)`), keeps the key in a plain simulator Keychain item and gates the read with a real `LAContext` evaluation. A `ScriptedKeyProtector` exists in Debug builds for tests. Neither is present in a Release device binary.7172## In memory7374While unlocked, `Vault` holds the master key, the decrypted records, the search index and decoded images (an `NSCache`). `Vault.lock()` drops all of them; it runs when the scene enters the background. Swift and iOS give no guarantee that freed memory is overwritten at once, so this is a best effort, stated as such: the references are released immediately, the PBKDF2 output buffer is zeroed with `memset_s`, and decrypted photographs are never written to disk.7576## At rest7778- Vault directory: `Application Support/SPVault/`, excluded from iCloud and computer backups (`isExcludedFromBackup`).79- Every file is written with `Data.WritingOptions.completeFileProtection` and the app carries the `com.apple.developer.default-data-protection = NSFileProtectionComplete` entitlement.80- Writes are atomic. A new card is written into a staging directory and appears through one rename; leftovers of an interrupted save are purged at the next unlock.81- Deleting a card removes its wrapped key first, then its directory: once the key is gone the remaining ciphertext is undecryptable.82- `vault.json` is the only clear file: format version, vault id, creation date and the recovery KDF parameters. It holds nothing about the cards.8384## Recovery passphrase8586Optional, chosen at onboarding. `recovery.key` is the master key sealed under PBKDF2(passphrase). After a change of the biometric set, the passphrase unwraps the master key, which is then stored again under the new biometric set. This file can be attacked offline by someone who extracts the device's file system, which is why the passphrase policy requires at least 12 characters and a "Good" estimate, and why the KDF cost is 600 000 iterations. Without a recovery passphrase, a biometric reset leaves the `.spvault` backup as the only way back.8788## What is never stored8990There is no field for a security code (CVV/CVC/CID), a PIN or magnetic-stripe data. `SensitiveDataPolicy` refuses such labels and track-data patterns on every path (editor, OCR proposals, restore), and the scanner blacks out the security code in the pixels of a bank card's back before the photograph is encrypted.91
Files main
Menu

Theme