HardwareKeyStore

Non-exportable ECDSA P-256 signing keys generated inside the platform's secure hardware: StrongBox or the trusted execution environment on Android, and the Secure Enclave on Apple platforms. Private keys never leave the hardware; callers receive the public key and signatures only.

By default generate fails with KeychainUnavailableException.Reason.Unsupported when no secure hardware is available (emulators, simulators, unsigned macOS tools, desktop JVM). Opt in to software-backed keys with HardwareKeySpec.allowSoftwareKeys; HardwareKeyInfo.securityLevel always reports the level actually obtained.

Signatures use ECDSA with SHA-256 over the supplied data and are DER-encoded (X9.62), so they verify with SHA256withECDSA on the JVM and ecdsaSignatureMessageX962SHA256 on Apple platforms. Keys are isolated by the store's service/account namespace, like the other stores.

val keys = HardwareKeyStore.forCurrentPlatform(serviceName = "my-app", accountName = "user-123")
val key = keys.generate("device-binding")
upload(key.publicKeyDer)
val signature: ByteArray? = keys.sign("device-binding", challenge)

Types

Link copied to clipboard
object Companion

Platform-specific factories for creating an application-scoped key store.

Functions

Link copied to clipboard
abstract fun clear()

Deletes every key in this store's service/account namespace.

Link copied to clipboard
abstract fun delete(alias: String)

Deletes the key under alias. Deleting a missing key succeeds silently as long as the backend itself is available.

Link copied to clipboard
abstract fun generate(alias: String, spec: HardwareKeySpec = HardwareKeySpec()): HardwareKeyInfo

Generates a new key under alias, replacing any existing key with that alias.

Link copied to clipboard
abstract fun info(alias: String): HardwareKeyInfo?

Returns the public information of the key under alias.

Link copied to clipboard
abstract fun sign(alias: String, data: ByteArray): ByteArray?

Signs data with the private key under alias using ECDSA with SHA-256.