SPB Git

Repository Public

CRYPTOGRAPHY.md 6.7 KB

# Cryptography

SP 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/.

# Key hierarchy

text
Face ID (current biometric set)
        │ unlocks
        ▼
Keychain item ── master key K (256 bits, random)
        │
        ├─ HKDF-SHA256(K, salt = vault id, "spvault/keywrap/v1")  ──► wraps each card key
        ├─ HKDF-SHA256(K, salt = vault id, "spvault/manifest/v1") ──► manifest.bin
        └─ HKDF-SHA256(K, salt = vault id, "spvault/search/v1")   ──► search.idx

card key Kc (256 bits, random, one per card)
        ├─ record  (cards/<id>/meta)
        └─ photographs (front, back, thumbnails)

recovery passphrase ── PBKDF2-HMAC-SHA256 ──► wraps K in recovery.key   (optional)
backup passphrase   ── PBKDF2-HMAC-SHA256 ── HKDF ──► .spvault records
Item Choice
Cipher AES-256-GCM (CryptoKit.AES.GCM), 96-bit nonce, 128-bit tag
Nonce AES.GCM.Nonce() — fresh from the system CSPRNG for every seal; never derived, never reused by construction
Master key SymmetricKey(size: .bits256), generated on the device at vault creation
Per-card key SymmetricKey(size: .bits256), generated when the card is saved, wrapped (AES-256-GCM) by the key-wrap sub-key
Sub-keys HKDF-SHA256, salt = the 16 bytes of the vault id, one info string per purpose
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
Randomness SecRandomCopyBytes / CryptoKit

# Sealed blob format (SPV1)

Every encrypted file is one sealed blob:

text
offset  size  field
0       4     magic "SPV1"
4       1     format version (1)
5       1     kind: 1 wrapped key · 2 record · 3 image · 4 manifest · 5 search index · 6 recovery wrap · 7 backup record
6       12    nonce
18      n     ciphertext
18+n    16    GCM tag

Additional 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:

  • any altered byte, in the header, the nonce, the ciphertext or the tag, fails authentication;
  • a blob cannot be moved to another card, another file name or another vault;
  • a blob of one kind is never accepted as another kind;
  • an unknown version is refused before any decryption.

A card whose files fail authentication is reported as damaged in the Security Center. It is never silently dropped or "repaired".

# Where the master key lives

KeychainBiometricKeyProtector stores the 32 bytes as a generic-password item with:

  • kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly — never leaves the device, not in backups, gone if the passcode is removed;
  • 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;
  • no passcode fallback (localizedFallbackTitle = "", biometry-only access control).

Unlocking 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.

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.

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.

# In memory

While 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.

# At rest

  • Vault directory: Application Support/SPVault/, excluded from iCloud and computer backups (isExcludedFromBackup).
  • Every file is written with Data.WritingOptions.completeFileProtection and the app carries the com.apple.developer.default-data-protection = NSFileProtectionComplete entitlement.
  • 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.
  • Deleting a card removes its wrapped key first, then its directory: once the key is gone the remaining ciphertext is undecryptable.
  • vault.json is the only clear file: format version, vault id, creation date and the recovery KDF parameters. It holds nothing about the cards.

# Recovery passphrase

Optional, 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.

# What is never stored

There 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.

Files main
Menu

Theme