page.title=Android Keystore System @jd:body

In this document

  1. Security Features
  2. Choosing Between a Keychain or the Android Keystore Provider
  3. Using Android Keystore Provider
    1. Generating a New Private Key
    2. Working with Keystore Entries
    3. Listing Entries
    4. Signing and Verifying Data
  4. Supported Algorithms

Blog articles

  1. Unifying Key Store Access in ICS

The Android Keystore system lets you store cryptographic keys in a container to make it more difficult to extract from the device. Once keys are in the keystore, they can be used for cryptographic operations with the key material remaining non-exportable. Moreover, it offers facilities to restrict when and how keys can be used, such as requiring user authentication for key use or restricting keys to be used only in certain cryptographic modes. See Security Features section for more information.

The Keystore system is used by the {@link android.security.KeyChain} API as well as the Android Keystore provider feature that was introduced in Android 4.3 (API level 18). This document goes over when and how to use the Android Keystore provider.

Security Features

Android Keystore system protects key material from unauthorized use. Firstly, Android Keystore mitigates unauthorized use of key material outside of the Android device by preventing extraction of the key material from application processes and from the Android device as a whole. Secondly, Android KeyStore mitigates unauthorized use of key material on the Android device by making apps specify authorized uses of their keys and then enforcing these restrictions outside of the apps' processes.

Extraction Prevention

Key material of Android Keystore keys is protected from extraction using two security measures:

Key Use Authorizations

To mitigate unauthorized use of keys on the Android device, Android Keystore lets apps specify authorized uses of their keys when generating or importing the keys. Once a key is generated or imported, its authorizations can not be changed. Authorizations are then enforced by the Android Keystore whenever the key is used. This is an advanced security feature which is generally useful only if your requirements are that a compromise of your application process after key generation/import (but not before or during) cannot lead to unauthorized uses of the key.

Supported key use authorizations fall into the following categories:

As an additional security measure, for keys whose key material is inside secure hardware (see {@link android.security.keystore.KeyInfo#isInsideSecureHardware() KeyInfo.isInsideSecurityHardware()}) some key use authorizations may be enforced by secure hardware, depending on the Android device. Cryptographic and user authentication authorizations are likely to be enforced by secure hardware. Temporal validity interval authorizations are unlikely to be enforced by the secure hardware because it normally does not have an independent secure real-time clock.

Whether a key's user authentication authorization is enforced by the secure hardware can be queried using {@link android.security.keystore.KeyInfo#isUserAuthenticationRequirementEnforcedBySecureHardware() KeyInfo.isUserAuthenticationRequirementEnforcedBySecureHardware()}.

Choosing Between a Keychain or the Android Keystore Provider

Use the {@link android.security.KeyChain} API when you want system-wide credentials. When an app requests the use of any credential through the {@link android.security.KeyChain} API, users get to choose, through a system-provided UI, which of the installed credentials an app can access. This allows several apps to use the same set of credentials with user consent.

Use the Android Keystore provider to let an individual app store its own credentials that only the app itself can access. This provides a way for apps to manage credentials that are usable only by itself while providing the same security benefits that the {@link android.security.KeyChain} API provides for system-wide credentials. This method requires no user interaction to select the credentials.

Using Android Keystore Provider

To use this feature, you use the standard {@link java.security.KeyStore} and {@link java.security.KeyPairGenerator} or {@link javax.crypto.KeyGenerator} classes along with the {@code AndroidKeyStore} provider introduced in Android 4.3 (API level 18).

{@code AndroidKeyStore} is registered as a {@link java.security.KeyStore} type for use with the {@link java.security.KeyStore#getInstance(String) KeyStore.getInstance(type)} method and as a provider for use with the {@link java.security.KeyPairGenerator#getInstance(String, String) KeyPairGenerator.getInstance(algorithm, provider)} and {@link javax.crypto.KeyGenerator#getInstance(String, String) KeyGenerator.getInstance(algorithm, provider)} methods.

Generating a New Private Key

Generating a new {@link java.security.PrivateKey} requires that you also specify the initial X.509 attributes that the self-signed certificate will have. You can use {@link java.security.KeyStore#setKeyEntry(String, java.security.Key, char[], java.security.cert.Certificate[]) KeyStore.setKeyEntry} to replace the certificate at a later time with a certificate signed by a Certificate Authority (CA).

To generate the key, use a {@link java.security.KeyPairGenerator} with {@link android.security.KeyPairGeneratorSpec}:

{@sample development/samples/ApiDemos/src/com/example/android/apis/security/KeyStoreUsage.java generate}

Generating a New Secret Key

To generate the key, use a {@link javax.crypto.KeyGenerator} with {@link android.security.keystore.KeyGenParameterSpec}.

Working with Keystore Entries

Using the {@code AndroidKeyStore} provider takes place through all the standard {@link java.security.KeyStore} APIs.

Listing Entries

List entries in the keystore by calling the {@link java.security.KeyStore#aliases()} method:

{@sample development/samples/ApiDemos/src/com/example/android/apis/security/KeyStoreUsage.java list}

Signing and Verifying Data

Sign data by fetching the {@link java.security.KeyStore.Entry} from the keystore and using the {@link java.security.Signature} APIs, such as {@link java.security.Signature#sign()}:

{@sample development/samples/ApiDemos/src/com/example/android/apis/security/KeyStoreUsage.java sign}

Similarly, verify data with the {@link java.security.Signature#verify(byte[])} method:

{@sample development/samples/ApiDemos/src/com/example/android/apis/security/KeyStoreUsage.java verify}

Requiring User Authentication For Key Use

When generating or importing a key into the {@code AndroidKeyStore} you can specify that the key is only authorized to be used if the user has been authenticated. The user is authenticated using a subset of their secure lock screen credentials (pattern/PIN/password, fingerprint).

This is an advanced security feature which is generally useful only if your requirements are that a compromise of your application process after key generation/import (but not before or during) cannot bypass the requirement for the user to be authenticated to use the key.

When a key is authorized to be used only if the user has been authenticated, it is configured to operate in one of the two modes:

Supported Algorithms

Cipher

Algorithm Supported (API Levels) Notes
AES/CBC/NoPadding 23+
AES/CBC/PKCS7Padding 23+
AES/CTR/NoPadding 23+
AES/ECB/NoPadding 23+
AES/ECB/PKCS7Padding 23+
AES/GCM/NoPadding 23+ Only 12-byte long IVs supported.
RSA/ECB/NoPadding 18+
RSA/ECB/PKCS1Padding 18+
RSA/ECB/OAEPWithSHA-1AndMGF1Padding 23+
RSA/ECB/OAEPWithSHA-224AndMGF1Padding 23+
RSA/ECB/OAEPWithSHA-256AndMGF1Padding 23+
RSA/ECB/OAEPWithSHA-384AndMGF1Padding 23+
RSA/ECB/OAEPWithSHA-512AndMGF1Padding 23+
RSA/ECB/OAEPPadding 23+

KeyGenerator

Algorithm Supported (API Levels) Notes
AES 23+ Supported sizes: 128, 192, 256
HmacSHA1 23+
  • Supported sizes: 8--1024 (inclusive), must be multiple of 8
  • Default size: 160
HmacSHA224 23+
  • Supported sizes: 8--1024 (inclusive), must be multiple of 8
  • Default size: 224
HmacSHA256 23+
  • Supported sizes: 8--1024 (inclusive), must be multiple of 8
  • Default size: 256
HmacSHA384 23+
  • Supported sizes: 8--1024 (inclusive), must be multiple of 8
  • Default size: 384
HmacSHA512 23+
  • Supported sizes: 8--1024 (inclusive), must be multiple of 8
  • Default size: 512

KeyFactory

Algorithm Supported (API Levels) Notes
EC 23+ Supported key specs: {@link android.security.keystore.KeyInfo} (private key only), {@link java.security.spec.ECPublicKeySpec} (public key only), {@link java.security.spec.X509EncodedKeySpec} (public key only)
RSA 23+ Supported key specs: {@link android.security.keystore.KeyInfo} (private key only), {@link java.security.spec.RSAPublicKeySpec} (public key only), {@link java.security.spec.X509EncodedKeySpec} (public key only)

KeyStore

KeyStore supports the same key types as {@code KeyPairGenerator} and {@code KeyGenerator}.

KeyPairGenerator

Algorithm Supported (API Levels) Notes
DSA 19–22
EC 23+
  • Supported sizes: 224, 256, 384, 521
  • Supported named curves: P-224 (secp224r1), P-256 (aka secp256r1 and prime256v1), P-384 (aka secp384r1), P-521 (aka secp521r1)

Prior to API Level 23, EC keys can be generated using KeyPairGenerator of algorithm "RSA" initialized {@link android.security.KeyPairGeneratorSpec} whose key type is set to "EC" using {@link android.security.KeyPairGeneratorSpec.Builder#setKeyType(String)}. EC curve name cannot be specified using this method -- a NIST P-curve is automatically chosen based on the requested key size.

RSA 18+
  • Supported sizes: 512, 768, 1024, 2048, 3072, 4096
  • Supported public exponents: 3, 65537
  • Default public exponent: 65537

Mac

Algorithm Supported (API Levels) Notes
HmacSHA1 23+
HmacSHA224 23+
HmacSHA256 23+
HmacSHA384 23+
HmacSHA512 23+

Signature

Algorithm Supported (API Levels) Notes
MD5withRSA 18+
NONEwithECDSA 23+
NONEwithRSA 18+
SHA1withDSA 19–22
SHA1withECDSA 19+
SHA1withRSA 18+
SHA1withRSA/PSS 23+
SHA224withDSA 20–22
SHA224withECDSA 20+
SHA224withRSA 20+
SHA224withRSA/PSS 23+
SHA256withDSA 19–22
SHA256withECDSA 19+
SHA256withRSA 18+
SHA256withRSA/PSS 23+
SHA384withDSA 19–22
SHA384withECDSA 19+
SHA384withRSA 18+
SHA384withRSA/PSS 23+
SHA512withDSA 19–22
SHA512withECDSA 19+
SHA512withRSA 18+
SHA512withRSA/PSS 23+

SecretKeyFactory

Algorithm Supported (API Levels) Notes
AES 23+ Supported key specs: {@link android.security.keystore.KeyInfo}
HmacSHA1 23+ Supported key specs: {@link android.security.keystore.KeyInfo}
HmacSHA224 23+ Supported key specs: {@link android.security.keystore.KeyInfo}
HmacSHA256 23+ Supported key specs: {@link android.security.keystore.KeyInfo}
HmacSHA384 23+ Supported key specs: {@link android.security.keystore.KeyInfo}
HmacSHA512 23+ Supported key specs: {@link android.security.keystore.KeyInfo}