page.title=Android Keystore System @jd:body
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.
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()}.
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.
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 {@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}To generate the key, use a {@link javax.crypto.KeyGenerator} with {@link android.security.keystore.KeyGenParameterSpec}.
Using the {@code AndroidKeyStore} provider takes place through all the standard {@link java.security.KeyStore} APIs.
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}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}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:
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+ |
Algorithm | Supported (API Levels) | Notes |
---|---|---|
AES | 23+ | Supported sizes: 128, 192, 256 |
HmacSHA1 | 23+ |
|
HmacSHA224 | 23+ |
|
HmacSHA256 | 23+ |
|
HmacSHA384 | 23+ |
|
HmacSHA512 | 23+ |
|
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) |
Algorithm | Supported (API Levels) | Notes |
---|---|---|
DSA | 19–22 | |
EC | 23+ |
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+ |
|
Algorithm | Supported (API Levels) | Notes |
---|---|---|
HmacSHA1 | 23+ | |
HmacSHA224 | 23+ | |
HmacSHA256 | 23+ | |
HmacSHA384 | 23+ | |
HmacSHA512 | 23+ |
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+ |
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} |