The user wants an overview of securely handling secrets in client applications. I need to cover concepts like envelope encryption and how to use KMS with KEK and DEK. Since clients can't securely store long-term secrets, I should suggest using per-user tokens and hardware-backed keys instead. It's also important to share best practices for different platforms and discuss the threat model, including reverse engineering and jailbreak risks. Additionally, certificate pinning and remote configuration will be crucial to mention. I'll ensure this is practical and includes templates and examples.Creating a key management guide
I need to focus on rotating DEKs frequently while KEKs should be rotated rarely. It's essential to implement key versioning and store key IDs, along with metadata. I'll cover the compromise playbook, including detection signals, responses like revoking tokens, and the necessity for re-authentication. I'll mention using envelope encryption for local data and detail best practices for offline encryption. Since the user is looking for structured guidance, I’ll emphasize never shipping secrets and suggest alternative storage patterns.Explaining API keys and encryption
I need to explain how to properly handle API keys, emphasizing the use of public identifiers alongside backend-signed requests. It's crucial to use app attestation to reduce abuse and not solely rely on secrecy. I’ll create a structured markdown guide with sections that cover principles, threat models, and what secrets are safe in client applications. Additionally, I'll detail the design for envelope encryption in client-stored data, including generating DEKs per object, using AEAD for encryption, and securely storing KEKs. The edge cases will also consider multi-user scenarios and trade-offs for key management.Detailing keychain and keystore usage
I need to specify how keychains and keystores work across different platforms. For iOS, I should highlight using Keychain with Secure Enclave keys and the importance of avoiding iCloud sync for secrets. On Android, I'd mention Jetpack Security, the Android Keystore, and backup exclusions. For macOS and Windows, it's Keychain and DPAPI, while Linux uses libsecret or KWallet. I also want to note that web storage should lean on WebCrypto with user passphrases and emphasize token and key rotation methods, including emergency plans.Developing a compromise playbook
I need to outline a compromise playbook that addresses common events like reverse engineering or API key leaks. The steps should include triage, containment, eradication, recovery, and postmortem analysis. For embedded API keys, I’ll suggest rotating them, implementing app attestation to block misuse, and moving secrets to the server. For token theft, I'll emphasize revoking sessions and enforcing re-authentication. It’s also crucial to communicate effectively throughout the process. Lastly, I should remind the user that envelope encryption protects data at rest, not API secrets. I'll keep it concise and helpful while structuring the information clearly.
0) First principle: “secrets in client apps” mostly don’t exist
If a value ships in a client binary (mobile/desktop/web) or can be fetched by the client without a user secret, assume an attacker can extract it (reverse engineering, hooking, rooted/jailbroken devices, TLS interception on compromised devices, memory inspection).
So:
Never rely on a client-held secret to protect a backend API. Use user auth + server-side authorization.
Treat “API keys in apps” as public identifiers, not secrets. If you must ship one, scope it tightly and add abuse controls (rate limiting, per-device/per-user quotas, attestation).
What you can protect in clients:
User/session credentials (refresh tokens, private keys) bound to a device.
User data at rest (offline cache, local DB, downloaded files).
End-to-end encryption keys if your product requires E2EE (with explicit UX and recovery model).
The rest of this assumes you’re protecting local secrets / local encrypted data, not trying to hide a backend master key in an app.
1) Envelope encryption (client-side) pattern
Goal: Encrypt data with a fast random key (DEK), and protect that DEK with a better-protected key (KEK).
Recommended construction
DEK (Data Encryption Key): random 256-bit key per file/record/blob (or per logical bucket).
Encrypt data: use an AEAD mode:
AES-256-GCM or ChaCha20-Poly1305
Store nonce/iv + ciphertext + tag
Use AAD to bind metadata (record id, user id, schema version) to prevent swapping attacks.
KEK (Key Encryption Key): stored in OS secure storage (Keychain/Keystore/DPAPI/etc.) or derived from a user secret.
Wrap (encrypt) the DEK with the KEK:
Prefer a standard key-wrap scheme (e.g., AES-KW) or AEAD wrapping (AES-GCM with distinct nonce).
Persist alongside the ciphertext:
wrapped_dek, kek_key_id/version, dek_alg, data_alg, nonce, aad, etc.
No secrets in logs/crash reports/analytics events.
If you tell me the platform(s) (iOS/Android/web/desktop) and what the “secret” is (API key vs refresh token vs E2EE key vs local DB encryption), I can propose a concrete design (storage flags, key hierarchy, record format, and a rotation + incident runbook tailored to it).