Android KeyStore & StrongBox
Mobile Cryptographic Implementation · Mobile Application Security track
11 min read
In this module
The Android KeyStore System
Android's KeyStore is a centralised, hardware-backed vault for generating and using cryptographic keys. The design rule is simple: key material never leaves the secure hardware. Apps ask for an operation (sign, encrypt, decrypt) and the hardware does the work internally, handing back only the result.
The application never touches raw key bytes. Even with the app process fully owned, an attacker can ask the hardware to perform operations but can't extract the key itself. Bind the key to user authentication and even that request needs a biometric or PIN first.
Generating Hardware-Backed Keys
Keys are generated through the KeyPairGenerator or KeyGenerator APIs with a KeyGenParameterSpec that constrains how the key can be used:
KeyPairGenerator kpg = KeyPairGenerator.getInstance( KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
kpg.initialize(new KeyGenParameterSpec.Builder( "my-auth-key", KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY) .setDigests(KeyProperties.DIGEST_SHA256) .setAlgorithmParameterSpec( new ECGenParameterSpec("secp256r1")) .setUserAuthenticationRequired(true) .setUserAuthenticationParameters( 0, KeyProperties.AUTH_BIOMETRIC_STRONG) .setIsStrongBoxBacked(true) // Request StrongBox .build());
KeyPair keyPair = kpg.generateKeyPair();The KeyGenParameterSpec is where you lock the key down. Permitted purposes, required digests, whether the user has to authenticate, whether StrongBox is needed — all decided here. Once the key exists, the hardware enforces those constraints and they can't be loosened later.
TEE vs StrongBox
Android supports two tiers of hardware key protection, each with different security properties:
TEE (Trusted Execution Environment)
The TEE is a walled-off slice of the main application processor. On ARM devices, ARM TrustZone does the splitting: a "normal world" running Android, and a "secure world" where the TEE lives.
Owning the kernel doesn't immediately get you the TEE — that isolation is real. But the TEE rides on the same physical silicon as the app processor, which exposes it to:
- Side-channel attacks that exploit shared hardware resources (cache, power analysis).
- Fault injection attacks targeting the shared silicon.
- TEE firmware vulnerabilities - several TEE implementations (Qualcomm QSEE, Trustonic Kinibi) have had critical vulnerabilities.
StrongBox
StrongBox, introduced in Android 9 (API level 28), requires a physically separate secure element with strict hardware requirements:
- Dedicated CPU: A separate processor that does not share execution resources with the main SoC.
- Secure storage: Non-volatile storage that is accessible only to the secure element.
- True random number generator (TRNG): its own hardware source of randomness, independent of the main processor's.
- Tamper resistance: Physical protections against side-channel analysis, fault injection, and decapsulation.
- Secure clock: Independent timekeeping for enforcing time-based access policies.
StrongBox implementations include embedded Secure Elements (eSE), discrete secure microcontrollers, and on some devices, the Titan M chip (Google Pixel).
The critical advantage of StrongBox over TEE is physical isolation. Because StrongBox runs on a separate chip, a complete compromise of the main application processor - including the TEE - does not give the attacker access to StrongBox-protected keys. This matters for high-value operations like financial transactions and device attestation.
Here's the whole stack, from the app down to the secure element, with an attacker of your choosing. Start with root against a TEE key and note that the attacker can still sign with it, then switch the key to Software-backed and watch extraction flip to yes. After that, try the three bench attacks against TEE and StrongBox.
Who can reach the key?
Pick an attacker and where the key lives. Each layer shows whether it stops them.
Full control of Android: root, kernel code, any app's identity.
- AppFalls
Calls KeyStore with an alias. Gets back signatures and ciphertext, never key bytes.
Root can read and patch the app's memory and files.
- Android frameworkFalls
The KeyStore API in the app and the keystore2 system service, which checks which app owns each key.
Root can impersonate any app and call keystore2 as it.
- KeyMint HALFalls
The interface to the secure hardware. Its normal-world side does no sensitive work; it passes requests on.
The HAL's normal-world side is just Android code, so root controls it.
- secure world, same processorTEE (KeyMint TA)Holdskey lives here
A trusted app in the application processor's secure world, often TrustZone running an OS such as Trusty.
Secure-world memory is walled off from the kernel by hardware.
- separate chipStrongBoxNot used
A separate secure element with its own CPU, secure storage, true random number generator and tamper resistance.
This key never reaches this layer.
Root or kernel against a TEE key
- Use the key
- Yes
- Extract the key
- No
- Use the key through the HAL while the device runs the attacker's code
- Extract the key: it never leaves the secure hardware
- Forge the auth tokens the secure hardware checks
A simplified model. Real outcomes depend on the device's TEE, secure element and patch level.
Key Attestation
Key attestation lets a remote server check that a key really lives in hardware, not in some software fake-out. Generate a key with attestation enabled and the KeyStore hands back a certificate chain:
- Key certificate: Contains the public key and an extension describing the key's properties (hardware backing, authentication requirements, device security state).
- Intermediate certificate(s): On older devices, issued from attestation keys the manufacturer injected at the factory. Newer devices use Remote Key Provisioning (RKP), where Google's servers certify short-lived attestation keys over the network; it's mandatory for devices launching with Android 16.
- Root certificate: A Google attestation root. There's more than one: Google publishes the current set, and a new ECDSA P-384 root started signing chains on 1 February 2026, so a verifier has to trust the whole published set rather than one pinned certificate.
The attestation extension (OID 1.3.6.1.4.1.11129.2.1.17) in the key certificate includes:
- Whether the key is in TEE or StrongBox.
- The required authentication type (none, biometric, PIN).
- The device's verified boot state (locked/unlocked bootloader).
- The OS version and security patch level.
- Whether the device is running in a debuggable state.
// Requesting key attestationKeyPairGenerator kpg = KeyPairGenerator.getInstance( KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
kpg.initialize(new KeyGenParameterSpec.Builder( "attested-key", KeyProperties.PURPOSE_SIGN) .setDigests(KeyProperties.DIGEST_SHA256) .setAttestationChallenge( serverChallenge) // Nonce from the server .build());
KeyPair keyPair = kpg.generateKeyPair();
// Retrieve the attestation certificate chainKeyStore ks = KeyStore.getInstance("AndroidKeyStore");ks.load(null);Certificate[] chain = ks.getCertificateChain("attested-key");// Send chain to server for verificationThe server walks the chain to one of Google's published attestation roots, checks every certificate against Google's revocation list, checks the nonce matches what it sent, and reads the attestation extension to confirm the key's properties. The result is cryptographic proof that the key sits in hardware. A compromised app can't fake this.
Below are four synthetic chains, decoded field by field. Compare where rootOfTrust sits on the StrongBox key and on the software-only one, then paste a different challenge into the server box and watch the replay check trip.
Key attestation inspector
Each chain is built as DER bytes in your browser and decoded again. Names, keys and signatures are made up: these aren't Google's roots or real device keys.
A signing key generated in StrongBox on a phone with a locked bootloader and a verified boot chain. The key needs a strong biometric for every use.
Certificate chain
- Key certificate (leaf)synthetic
- Subject:
- CN=Android Keystore Key
- Issuer:
- O=Example only, CN=Synthetic StrongBox attestation key
- Serial, validity:
- 1, 2026-11-01 to 2048-12-31
- Extensions:
- keyUsage, Android key attestation
↓ issued by - Intermediatesynthetic
- Subject:
- O=Example only, CN=Synthetic StrongBox attestation key
- Issuer:
- O=Example only, not Google, CN=Synthetic attestation root
- Serial, validity:
- 27404, 2026-05-01 to 2048-12-31
- Extensions:
- basicConstraints, keyUsage
↓ issued by - Rootsynthetic
- Subject:
- O=Example only, not Google, CN=Synthetic attestation root
- Issuer:
- O=Example only, not Google, CN=Synthetic attestation root
- Serial, validity:
- 8192, 2025-01-01 to 2049-12-31
- Extensions:
- basicConstraints, keyUsage
- Each issuer name matches the next certificate's subject, ending at a self-issued root
- The attestation extension decodes and passes the server's rules
- Signatures: these are placeholder bytes, so the chain isn't cryptographically verified
- The root against Google's published set of attestation roots
- Every certificate's serial against Google's attestation revocation list
A real server does all three. Real chains can also have more than one intermediate.
Server checks
Oldest patch level accepted: 2025-11 (12 months before this month).
Passes every check here
KeyDescription (OID 1.3.6.1.4.1.11129.2.1.17)
| Field | Value |
|---|---|
| attestationVersion | KeyMint 3.0 (300) |
| attestationSecurityLevel | StrongBox (2) |
| keyMintVersion | 300 |
| keyMintSecurityLevel | StrongBox (2) |
| attestationChallenge | 1d43e4770a3656e6f9822652f9f36dcb |
| uniqueId | (empty) |
| Tag | Field | Value |
|---|---|---|
| [1] | purpose | SIGN (2) |
| [2] | algorithm | EC (3) |
| [3] | keySize | 256 |
| [5] | digest | SHA_2_256 (4) |
| [10] | ecCurve | P_256 (1) |
| [504] | userAuthType | FINGERPRINT (2) |
| [702] | origin | GENERATED (0) |
| [704] | rootOfTrust | deviceLocked TRUE, verifiedBootState Verified (0) |
| [705] | osVersion | 16.0.0 (160000) |
| [706] | osPatchLevel | 2026-10 (202610) |
| [718] | vendorPatchLevel | 2026-10-01 (20261001) |
| [719] | bootPatchLevel | 2026-10-01 (20261001) |
| Field | Value |
|---|---|
| verifiedBootKey | 7e40a33ec1c515e2f6cd27587a79a684049e92fb3da0d0bc0e7b05fe9adb992c |
| deviceLocked | TRUE |
| verifiedBootState | Verified (0) |
| verifiedBootHash | 5f41667110150f50d3b9ec6ff0d753350c9603e20e193bf3fc52a01a41e5ce38 |
| Tag | Field | Value |
|---|---|---|
| [701] | creationDateTime | 2026-11-01 (ms since epoch) |
Only hardwareEnforced values come from the TEE or StrongBox. On a Software key both lists are written by the OS.
Show the extension's DER structure (216 bytes)
0 SEQUENCE (213) 3 INTEGER (2) 300 7 ENUMERATED (1) 2 10 INTEGER (2) 300 14 ENUMERATED (1) 2 17 OCTET STRING (16) 1d 43 e4 77 0a 36 56 e6 f9 82 26 52 … (16 bytes) 35 OCTET STRING (0) 37 SEQUENCE (12) 39 [701] (8) 43 INTEGER (6) 1793491200000 51 SEQUENCE (162) 54 [1] (5) 56 SET (3) 58 INTEGER (1) 2 61 [2] (3) 63 INTEGER (1) 3 66 [3] (4) 68 INTEGER (2) 256 72 [5] (5) 74 SET (3) 76 INTEGER (1) 4 79 [10] (3) 81 INTEGER (1) 1 84 [504] (3) 88 INTEGER (1) 2 91 [702] (3) 95 INTEGER (1) 0 98 [704] (76) 102 SEQUENCE (74) 104 OCTET STRING (32) 7e 40 a3 3e c1 c5 15 e2 f6 cd 27 58 … (32 bytes) 138 BOOLEAN (1) TRUE 141 ENUMERATED (1) 0 144 OCTET STRING (32) 5f 41 66 71 10 15 0f 50 d3 b9 ec 6f … (32 bytes) 178 [705] (5) 182 INTEGER (3) 160000 187 [706] (5) 191 INTEGER (3) 202610 196 [718] (6) 200 INTEGER (4) 20261001 206 [719] (6) 210 INTEGER (4) 20261001
Offset, then tag and content length. Tags in brackets are the AuthorizationList's explicit context tags.
Key attestation is one of the strongest tools for detecting compromised devices. A server can reject keys generated on devices with unlocked bootloaders, outdated security patches, or software-backed key storage. This is used by banking apps, enterprise MDM solutions, and DRM systems to refuse service on devices that do not meet their security requirements.
User Authentication Binding
The KeyStore can bind key usage to user authentication, meaning the key cannot perform cryptographic operations until the user provides a biometric or PIN:
Authentication-Per-Use
Every key operation demands a fresh authentication right before it runs. Strongest binding available, and the right call for high-stakes work like authorising a payment.
Time-Based Authentication
After authenticating, the key stays usable for a configurable window — say 30 seconds. Friendlier for the user, but anything happening inside that window goes through without a fresh check.
Unlocked Device Only
The weakest tier: the key just needs the device to be unlocked. Whatever method the user used to unlock (biometric, PIN, pattern) counts.
// Authentication-per-use with strong biometricnew KeyGenParameterSpec.Builder("payment-key", KeyProperties.PURPOSE_SIGN) .setUserAuthenticationRequired(true) .setUserAuthenticationParameters( 0, // 0 = every use KeyProperties.AUTH_BIOMETRIC_STRONG) .build();
// Time-based: valid for 30 seconds after authnew KeyGenParameterSpec.Builder("session-key", KeyProperties.PURPOSE_SIGN) .setUserAuthenticationRequired(true) .setUserAuthenticationParameters( 30, // seconds KeyProperties.AUTH_BIOMETRIC_STRONG | KeyProperties.AUTH_DEVICE_CREDENTIAL) .build();Enforcement happens in the TEE or StrongBox, not in app code. The secure hardware keeps an authentication token counter and checks for a valid, recent token before doing the work. The app sits outside that check entirely.
KeyMint HAL
KeyMint (formerly Keymaster) is the Hardware Abstraction Layer (HAL) that defines the interface between the Android framework and the secure hardware. It specifies the operations the hardware must support:
- Key generation (RSA, EC, AES, HMAC).
- Key import (with optional wrapping).
- Cryptographic operations (sign, verify, encrypt, decrypt).
- Key attestation.
- Secure key deletion.
KeyMint version 1 (Android 12+) added support for:
- Secure key import: Wrapping imported keys so that they are only accessible inside the secure hardware, never exposed in plaintext on the host.
- Device-unique attestation: Binding attestation to a specific device identity.
- Key upgrade: Migrating keys when the OS or patch level changes.
The HAL is a hard security boundary. Anything above it (the Android framework) runs in the normal world. Anything below runs in the TEE or StrongBox. Key bytes only ever cross encrypted.
Common Implementation Mistakes
Strong hardware is wasted if the surrounding code is sloppy. The usual ways developers blunt the KeyStore:
Using Software-Backed Keys
If the device can't do hardware-backed keys — uncommon on modern hardware, still possible — the KeyStore quietly falls back to software storage. Always check KeyInfo.getSecurityLevel() (on API 31 and later; older code uses the now-deprecated isInsideSecureHardware()) and refuse software-backed keys for anything sensitive.
Not Checking Attestation
Generating a key and assuming it's hardware-backed without verifying attestation means the server is trusting the client's word. A rooted device or emulator will happily produce software keys and lie about them. Only attestation gives you cryptographic proof.
Overly Broad Key Purposes
Granting PURPOSE_ENCRYPT and PURPOSE_SIGN when you only sign hands attackers extra primitives for free. Follow least privilege — list only the operations the key actually does, nothing more.
Ignoring Authentication Binding
Storing a signing key without setUserAuthenticationRequired(true) means any code in the app's process can use it. Pop the app via a WebView bug and you sign whatever you want, silently. Authentication binding pulls the user back into the loop, so a compromised app still can't act unilaterally.
Skipping Validity Periods
Without setKeyValidityStart() and setKeyValidityEnd(), a key lives forever. For session tokens or temporary authorisations, always pin a validity window.
Put the list to work. Each spec below compiles, generates a hardware-backed key, and has at least one of the mistakes above. Find them before you open the answer.
// A: signs payment requestsnew KeyGenParameterSpec.Builder("pay-sign", KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY) .setDigests(KeyProperties.DIGEST_SHA256) .build();
// B: encrypts cached account datanew KeyGenParameterSpec.Builder("cache-key", KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) .setBlockModes(KeyProperties.BLOCK_MODE_ECB) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7) .setRandomizedEncryptionRequired(false) .build();
// C: signs a one-day session token (RSA)new KeyGenParameterSpec.Builder("session", KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY | KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) .setDigests(KeyProperties.DIGEST_SHA256) .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PSS) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP) .build();Show the answer
A has no setUserAuthenticationRequired(true), so any code running in the app's process can sign payments without the user present. B uses ECB, which encrypts identical 16-byte blocks to identical ciphertext and leaks structure. The setRandomizedEncryptionRequired(false) line is the giveaway in a review: KeyStore refuses ECB encryption unless you switch that safety off. Use GCM and let the KeyStore pick the IV. C grants encrypt and decrypt to a key that only ever signs, and it has no setKeyValidityEnd(), so a token key meant for one day lives forever.