Kmp Secure Storage
ASecurityUse when storing access tokens, refresh tokens, API keys or other secrets from Kotlin Multiplatform shared code — Android Keystore, a replacement for deprecated EncryptedSharedPreferences, iOS Keychain (SecItemAdd, SecItemCopyMatching, kSecAttrAccessible), wiping data on logout, biometric-gated secrets, Android 17 Keystore key limits — or when tokens sit in SharedPreferences, DataStore or NSUserDefaults, survive logout or reinstall, appear in logs, key creation fails with ERROR_TOO_MANY_KEYS,...
- 17 stars
- 0 votes
- 0 copies
- 0 views
- Added December 3, 2026
Works with
Security analysis
100/100Pro scans all 3 files and shows the line behind each finding
npx -y skills add Kushpawan/kmp-mobile-skills --skill kmp-secure-storage --agent claude-codeAre you the author of Kmp Secure Storage?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/kushpawan-kmp-secure-storage)---
name: kmp-secure-storage
description: Use when storing access tokens, refresh tokens, API keys or other secrets from Kotlin Multiplatform shared code — Android Keystore, a replacement for deprecated EncryptedSharedPreferences, iOS Keychain (SecItemAdd, SecItemCopyMatching, kSecAttrAccessible), wiping data on logout, biometric-gated secrets, Android 17 Keystore key limits — or when tokens sit in SharedPreferences, DataStore or NSUserDefaults, survive logout or reinstall, appear in logs, key creation fails with ERROR_TOO_MANY_KEYS, or Keychain calls fail with -34018 or -25308.
license: Apache-2.0
compatibility: Kotlin Multiplatform project (Android + iOS). Android Keystore needs a device or emulator to test; iOS Keychain steps need macOS with Xcode.
metadata:
category: data
platforms: android, ios
---
# Secure storage for secrets in KMP
Secrets never touch plain preferences, a database column, a log line or a backup. Shared code sees
one small `SecureStore` interface; Android backs it with a non-exportable Android Keystore key, iOS
with the Keychain. Writes fail loudly instead of falling back to plaintext, reads fail closed, and
logout wipes everything the session left behind.
## When to use
- Storing session tokens, refresh tokens, per-user API keys or encryption keys from shared code.
- Replacing `EncryptedSharedPreferences` / `MasterKey` (Jetpack Security crypto, deprecated).
- Fixing: tokens in `SharedPreferences`, DataStore or `NSUserDefaults` in plain text; a session that
survives logout or an iOS reinstall; tokens in Logcat or the Xcode console; Keychain status
`-34018` (`errSecMissingEntitlement`) or `-25308` (`errSecInteractionNotAllowed`).
- Adding biometric unlock in front of a stored secret.
- Key generation failing with `KeyStoreException` / `ERROR_TOO_MANY_KEYS` on Android 17, or a design
that creates a Keystore key per record.
## When not to use
- Non-secret settings (theme, last tab) — DataStore or multiplatform-settings.
- Encrypting a whole database or large files — a dedicated solution (for example SQLCipher); this
skill covers small values.
- A full security review of the app — **mobile-security-review**.
- Where the token is *used* (bearer auth, refresh) — **kmp-data-layer**.
- Signing users back in silently after an Android device restore — Google's `restore-credentials`
skill (Credential Manager restore keys); this skill makes sure the old ciphertext is not restored.
## Step 0 — Read the project first
```bash
grep -nE "security-crypto|tink|datastore|multiplatform-settings|kvault|keychain" gradle/libs.versions.toml
grep -rnE "EncryptedSharedPreferences|MasterKey|KeyGenParameterSpec|SecItemAdd|KeychainSettings|NSUserDefaults|getSharedPreferences" --include=*.kt --include=*.swift .
grep -rniE "(token|secret|password|api_?key)" --include=*.kt --include=*.swift . | grep -iE "putString|setString|setObject|prefs\[|edit \{|forKey" | head -20
grep -rn "allowBackup\|fullBackupContent\|dataExtractionRules" --include=AndroidManifest.xml .
```
| Finding | Then |
|---|---|
| A secure store abstraction already exists | Use it. Check it against **Common mistakes**; fix gaps in place. |
| `EncryptedSharedPreferences` | Works today but deprecated (Jetpack Security crypto 1.1.0-alpha07+). Keep it running; migrate with step 8 when you touch it. |
| multiplatform-settings `KeychainSettings` on iOS | A reasonable iOS backend (marked experimental by the library); pair it with an encrypted Android implementation. |
| Tokens in plain prefs, DataStore, `NSUserDefaults` or a Room table | Treat as a defect: migrate (step 8). |
| `allowBackup="true"` without exclusion rules | Add rules (step 7). |
| A Keystore alias built from an id (one key per note, message or user) | Replace with one master key (step 3); Android 17 caps keys per app. |
## Workflow
### 1. Decide what is secret
| Secret — store with this skill | Not a storage problem |
|---|---|
| Access and refresh tokens, session cookies | Build-time API keys shipped in the binary (extractable anyway; restrict them server-side) |
| Per-user API keys, OAuth client secrets issued at runtime | User preferences |
| Keys used to encrypt other local data | Cached server data — that belongs in the database |
Never store the user's password; store the tokens the password bought.
### 2. One interface in `commonMain`
```kotlin
// commonMain — core/security/SecureStore.kt
interface SecureStore {
/** Null when absent. Throws [SecureStoreException] when the store is temporarily unreadable. */
suspend fun read(key: String): String?
/** Throws [SecureStoreException] if the value cannot be stored securely. Never stores plaintext. */
suspend fun write(key: String, value: String)
suspend fun delete(key: String)
/** Removes every value this store owns. */
suspend fun clear()
}
class SecureStoreException(message: String, cause: Throwable? = null) : Exception(message, cause)
```
The contract matters more than the code: **absent** (null), **unreadable right now** (exception —
for example an iOS device not yet unlocked after a reboot) and **stored** are three different
states. Treating "unreadable" as "absent" signs users out after every reboot.
### 3. Android — Keystore key + DataStore
AES-256-GCM key generated inside the Android Keystore (never exportable), a fresh IV per write,
ciphertext stored Base64-encoded in a dedicated Preferences DataStore file that is excluded from
backup. Full implementation, failure handling and tests:
[references/android-keystore.md](references/android-keystore.md).
**One master key, not a key per record.** The Keystore is shared by every app on the device, and
Android 17 enforces a per-app limit: 50,000 keys for apps targeting Android 17 (API 37), 200,000 for
others. Past it, key creation fails with a `KeyStoreException` whose `getNumericErrorCode()` is
`ERROR_TOO_MANY_KEYS` (target 37) or `ERROR_INCORRECT_USAGE`. Encrypt every value with the one
app-wide alias (plus, if needed, one separate alias for biometric-bound secrets — step 9); if you need
per-record keys, derive or wrap them under the master key (for example a Tink keyset) instead of
minting Keystore entries. Map a key-creation failure to `SecureStoreException`; check on a device how
it surfaces for your `minSdk`, since it can arrive wrapped in another exception.
Do not add `androidx.security:security-crypto` to new code: all of its APIs are deprecated "in
favour of existing platform APIs and direct use of Android Keystore".
### 4. iOS — Keychain via `platform.Security`
One `kSecClassGenericPassword` item per key under the app's service name, accessibility
`kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly` (readable by background work after the first
unlock, never synced to iCloud, never restored to another device). Update first, add on
`errSecItemNotFound`. Full Kotlin/Native implementation with correct Core Foundation memory
handling: [references/ios-keychain.md](references/ios-keychain.md).
### 5. Bind per platform
```kotlin
// androidMain — di/PlatformModule.android.kt
single<SecureStore> { AndroidSecureStore(context = androidContext()) }
// iosMain — di/PlatformModule.ios.kt
single<SecureStore> { KeychainSecureStore(service = "com.example.app.session") }
```
A single instance per process. See **kmp-koin-di** for platform modules.
### 6. Build the token store on top
```kotlin
// commonMain — core/session/SecureSessionTokenStore.kt
data class SessionTokens(val accessToken: String, val refreshToken: String) {
override fun toString(): String = "SessionTokens(***)" // a stray log line must not leak them
}
class SecureSessionTokenStore(private val secureStore: SecureStore) : SessionTokenStore {
private val mutex = Mutex() // the pair changes together, never half-written
override suspend fun read(): SessionTokens? = mutex.withLock {
val access = secureStore.read(KEY_ACCESS) ?: return@withLock null
val refresh = secureStore.read(KEY_REFRESH) ?: return@withLock null
SessionTokens(access, refresh)
}
override suspend fun write(tokens: SessionTokens) = mutex.withLock {
secureStore.write(KEY_ACCESS, tokens.accessToken)
secureStore.write(KEY_REFRESH, tokens.refreshToken)
}
override suspend fun clear() = mutex.withLock {
secureStore.delete(KEY_ACCESS)
secureStore.delete(KEY_REFRESH)
}
private companion object {
const val KEY_ACCESS = "access_token"
const val KEY_REFRESH = "refresh_token"
}
}
```
`SessionTokenStore` is the interface the bearer-auth setup reads (**kmp-data-layer**). If a write
of the pair fails half-way, delete what was written and rethrow — a sign-in that cannot be stored
securely must fail, not continue with tokens only in memory.
### 7. Wipe everything on logout — and on reinstall
Logout is a checklist, not one call. Run every step even if one fails, and log failures by name only:
```kotlin
// commonMain — core/session/SessionDataCleaner.kt
interface SessionDataCleaner {
val name: String // for logs: anonymous objects and lambdas have no useful class name
suspend fun clearSessionData()
}
class LogoutUseCase(private val cleaners: List<SessionDataCleaner>, private val log: (String) -> Unit) {
suspend operator fun invoke() {
cleaners.forEach { cleaner ->
try {
cleaner.clearSessionData()
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
log("Session cleaner '${cleaner.name}' failed: ${e::class.simpleName}")
}
}
}
}
```
Register a cleaner for each of: the secure store, user tables in the database, in-memory caches and
session scopes, the HTTP client's cached bearer token
(`client.authProvider<BearerAuthProvider>()?.clearToken()`), scheduled background work, and the
push-notification token. If a feature holds **unsynced offline writes**, decide before wiping —
block logout with a warning or discard explicitly; never lose them silently.
- **iOS reinstall:** Keychain items are not removed when the app is deleted (long-standing behaviour,
not a documented guarantee), while `NSUserDefaults` are. On launch, if a "has launched" flag in
`NSUserDefaults` is missing, clear the store before reading tokens — code in
[references/ios-keychain.md](references/ios-keychain.md). The first release that adds this signs
existing users out once, because they have no flag yet.
- **Android backup:** Keystore keys are never backed up, so restored ciphertext cannot be decrypted on
a new device. Exclude the secure DataStore file from cloud backup and device transfer
(`dataExtractionRules` on API 31+, `fullBackupContent` below) — rules in
[references/android-keystore.md](references/android-keystore.md).
### 8. Migrate secrets out of plain storage
1. Read the old value.
2. Write it to `SecureStore`, then read it back and compare.
3. Only then delete the old copy.
4. If the secure write is refused (for example the iOS device is locked), keep the old copy and
retry on the next launch — do not sign the user out.
Run the migration once per launch before the first authenticated request, behind the same mutex as
token reads.
### 9. Optional — biometrics in front of a secret
- Android: a Keystore key with `setUserAuthenticationRequired(true)` and
`setUserAuthenticationParameters(0, KeyProperties.AUTH_BIOMETRIC_STRONG)`; unlock the `Cipher`
through `BiometricPrompt.CryptoObject` (androidx.biometric).
- iOS: an item protected by `SecAccessControlCreateWithFlags(..., kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, kSecAccessControlBiometryCurrentSet, ...)`
in `kSecAttrAccessControl` (instead of `kSecAttrAccessible`), plus `NSFaceIDUsageDescription` in
`Info.plist`. The read blocks while the prompt shows — never call it on the main thread.
- Success, cancel, lockout and "biometrics changed" are four different outcomes. Only success
unlocks; a new enrolment invalidates the key or item (Android throws
`KeyPermanentlyInvalidatedException`) — fall back to a full sign-in, never to the unlocked path.
- Do not gate the refresh token that background sync needs; gate a separate secret or the UI.
**Showing a secret on screen** (a recovery code, an API key the user copies): set `FLAG_SECURE` on
that window on Android — for apps targeting Android 17, `setContentCaptureEnabled(false)` no longer
stops content capture, so `FLAG_SECURE` is the only control — and cover the app-switcher snapshot on
iOS. **mobile-security-review** has the Compose and SwiftUI code.
### 10. Test with a fake; test the real stores on devices
```kotlin
// commonTest — core/security/InMemorySecureStore.kt
class InMemorySecureStore : SecureStore {
private val values = mutableMapOf<String, String>()
var failWrites = false
override suspend fun read(key: String): String? = values[key]
override suspend fun write(key: String, value: String) {
if (failWrites) throw SecureStoreException("write refused")
values[key] = value
}
override suspend fun delete(key: String) { values.remove(key) }
override suspend fun clear() = values.clear()
}
```
- Test token store, logout and migration logic against the fake, including `failWrites = true`.
- Android Keystore is not available in JVM host tests — test `AndroidSecureStore` in instrumented tests.
- Observed: a Kotlin/Native iOS test executable runs without an app bundle or entitlements, and
every Keychain call returns `-34018` (`errSecMissingEntitlement`). Test the real Keychain store from
the app (a debug screen or app-hosted UI test), not from `iosSimulatorArm64Test`.
## Quick reference
| Concern | Android | iOS |
|---|---|---|
| Key / item | One app-wide AES-256-GCM key in `AndroidKeyStore` (per-app key cap on Android 17) | `kSecClassGenericPassword`, service + account |
| Storage | Dedicated Preferences DataStore file, Base64(IV + ciphertext) | Keychain item `kSecValueData` |
| Accessibility | Keystore default; optional user authentication | `kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly` |
| Backup | Exclude the file from `cloud-backup` and `device-transfer` | `ThisDeviceOnly` items never leave the device |
| Reinstall | Data removed with the app | Items survive — clear on first launch |
| Logout | `clear()` + delete the key | `SecItemDelete` with service only |
| Failure | Encryption error → exception, never plaintext | Non-success `OSStatus` → exception |
| Tests | Instrumented | App-hosted; unentitled test binaries get `-34018` |
## Verify
Report each check as `PASS`, `FAIL` or `NOT RUN` (with the reason).
```bash
./gradlew :shared:allTests # token store, logout, migration against the fake
./gradlew :composeApp:connectedDebugAndroidTest # AndroidSecureStore on a device or emulator (task name varies)
grep -rniE "(token|secret|password)" --include=*.kt --include=*.swift . | grep -iE "println|NSLog|Log\.[dviwe]|\.d\(|\.i\(" | head # secrets near log calls
grep -rnE "getSharedPreferences|NSUserDefaults" --include=*.kt --include=*.swift . | head # review every hit
```
Manual checks on both platforms — each one a separate line in the report:
1. Sign in, kill the app, relaunch: still signed in.
2. Sign out, relaunch: signed out, and no user data visible offline.
3. iOS: sign in, delete the app, reinstall: signed out.
4. iOS: sign in, reboot, start background work before unlocking: no forced sign-out after unlock.
5. Android: back up and restore to another device (or `adb shell bmgr`): the app starts signed out, without crashing.
## Common mistakes
| Mistake | Why it bites | Fix |
|---|---|---|
| Tokens in `SharedPreferences`, DataStore, `NSUserDefaults` or Room | Readable from backups, rooted or jailbroken devices | `SecureStore` |
| Encryption failure falls back to storing plaintext | Silently defeats the whole store on the devices where the Keystore misbehaves | Throw; the caller fails the sign-in |
| Undecryptable value returned as-is | Garbage sent as a bearer token | Delete it and return null |
| "Keychain locked" treated as "no token" | Users signed out after every reboot | Distinguish absent from unreadable (exception) |
| `kSecAttrAccessibleWhenUnlocked` for tokens used by background work | Background refresh fails while the screen is locked | `AfterFirstUnlockThisDeviceOnly` |
| Non-`ThisDeviceOnly` accessibility | Tokens sync via iCloud Keychain or restore onto another device | `...ThisDeviceOnly` |
| Keychain not cleared after reinstall | A deleted-and-reinstalled app is already signed in | First-launch flag in `NSUserDefaults` |
| Secure DataStore file included in Android backup | Restore on a new device yields ciphertext without a key | Exclusion rules |
| `data class` holding tokens, logged by accident | `toString()` prints the secrets | Override `toString()` |
| Logout only deletes the tokens | Cached user data, scheduled jobs and the HTTP client's cached token survive | Cleaner registry |
| `SecItemAdd` without update-first | `errSecDuplicateItem` (-25299) on the second write | Update, then add on `errSecItemNotFound` |
| `CFBridgingRetain` without a matching release | Secret bytes leak for the process lifetime | Release every bridged value (reference shows the pattern) |
| New code on `EncryptedSharedPreferences` | Deprecated; no further fixes | Keystore + DataStore |
| A Keystore key per record or per user item | Android 17 per-app key cap: creation fails with `ERROR_TOO_MANY_KEYS` once reached | One master key; wrap per-record keys under it |
| `setContentCaptureEnabled(false)` to hide a secret on screen | Ignored from target 37 | `FLAG_SECURE` on that window |
## Related skills
- **kmp-data-layer** — bearer auth that reads these tokens; clearing the HTTP client's token cache.
- **kmp-koin-di** — platform bindings for the store.
- **kmp-room-database** — user tables to wipe on logout.
- **mobile-security-review** — the wider review: transport, logging, backups, exported components.
- **kmp-dependency-security** — vetting crypto and storage libraries before adding them.
- Google `restore-credentials` skill (silent sign-in after device restore): <https://github.com/android/skills/tree/main/identity/restore-credentials>
- Android 17 per-app Keystore limit: <https://developer.android.com/about/versions/17/behavior-changes-all>
- OWASP MASVS (MASVS-STORAGE-1 "The app securely stores sensitive data", MASVS-STORAGE-2 "The app prevents leakage of sensitive data"): <https://mas.owasp.org/MASVS/>; weaknesses MASWE-0001 (unencrypted private storage), MASWE-0003 (keys outside the platform keystore), MASWE-0006 (not excluded from backup): <https://mas.owasp.org/MASWE/>
- Android Keystore: <https://developer.android.com/privacy-and-security/keystore>
- Apple Keychain services: <https://developer.apple.com/documentation/security/keychain-services>
- multiplatform-settings: <https://github.com/russhwolf/multiplatform-settings>
Files in this skill
- SKILL.md
- references/android-keystore.md
- references/ios-keychain.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.