App lock, PINs and brute-force lockout on Android
androidapp-lockpinpbkdf2lockoutbiometricflag-secureprivacy
Lessons from adding an app lock, a parent PIN, and "private conversations" to the Message SMS app.
Storing the PIN
Hash with PBKDF2 and a random salt; never store the PIN. Keep the maximum PIN length in one constant shared by UI and verifier.
Lockout that cannot be cheated
- Escalating lockout (for example 30 s doubling to 32 min).
- Use the monotonic clock. Store the lockout duration plus
SystemClock.elapsedRealtime()at arming. Wall-clock lockouts are shortened by changing the date. elapsedRealtimeresets at reboot, so on boot re-arm the full window rather than letting a reboot clear it.
What the lock must actually protect
- Do not compose protected screens behind the lock overlay. Composing the chat screen behind the overlay marked threads as read before the user authenticated.
- Gate notification deep links on "unlocked"; a tap must not bypass the lock.
FLAG_SECUREkeeps content out of Recents thumbnails and screenshots (API 33+ hassetRecentsScreenshotEnabled; useFLAG_SECUREbelow that).- Wait for the lockout state to resolve before auto-prompting biometrics.
Private conversations
- Hidden from every list query (main, archived, blocked, quarantine), global search, starred, and "mark all read".
- Opening the private space verifies the app-lock credential without unlocking the whole app.
- Notifications are redacted, not suppressed: a bare "New message" on the default channel (per-conversation channel names would leak the contact), no actions, no OTP chip,
VISIBILITY_PRIVATE. The redaction check must fail closed on errors. See Android notifications and PendingIntent pitfalls. - Accepted limit: an explicit
sms:deep link to a private thread opens it without the private gate; the whole-app lock is the hard shell.
See also
- Passphrase-encrypted export files for moving app data between phones
- Locking down an Android phone with a device-owner app