Not so hardware Android Keystore

So you thought that your keys generated with an Android Keystore are securely stored in some hardware magic box? Not even close. You can find them on your drive.

The headline above is something of a clickbait, but there is a lot of truth in it as well. TL;DR Your generated keys, on most of the devices, are really stored on your drive besides other files. You CAN’T store anything inside your Keystore. Even generated keys. But you can feel safe about them.
Use Keystore to encrypt the data and be happy.

Now we can take a closer look. You can find your private keys here:

The private key is stored in a file that has <app_id>_USRCERT_<key_alias>

There is a method KeyInfo.isInsideSecurityHardware() and it even has a name that means – “is inside security hardware”. Moreover, it returns true on most of the Android Devices above API 23.
Let’s take a look at the documentation:

Returns <code>true</code> if the key resides inside secure hardware (e.g., Trusted Execution Environment (TEE) or Secure Element (SE)). Key material of such keys is available in plaintext only inside the secure hardware and is not exposed outside of it

Above looks good – the key resides inside secure hardware but what is really matter is that line Trusted Execution Environment (TEE) OR Secure Element (SE). You have to understand the difference between the two of them.

  • Trusted execution environment (TEE) is a secure area of the main processor. It guarantees code and data loaded inside to be protected with respect to confidentiality and integrity. This is just a part of your processor without any storage.
  • Secure Element (SE) is a tamper-resistant secrets store, much like a smart-card or SIM-card. Its main purpose is to store cryptographic secrets in such a way that illicit use is hard or impossible to do.

Now you know that SE is something that can store your secret keys. Don’t bother it. On 99% you don’t have access to it. I know that the newest Google Pixel phones have “Titan” chipset as a Secure Element, and I don’t have knowledge about any other devices.

To access SE you have to have API level 28 (Android 9) and you should look information about StrongBox Keymaster which have

  • Its own CPU.
  • Secure storage.
  • A true random number generator.
  • Additional mechanisms to resist package tampering and unauthorized sideloading of apps.

There is a method KeyStore.setIsStrongBoxBacked() that will allow you to use SE and throw an exception if it isn’t available.

Let’s go back to the TEE. Now, when we know that this doesn’t store our keys in some secure magic box, how we can feel safe? These keys aren’t stored as normal files but as encrypted blobs. Even when you have access to them you can’t do much with them outside the TEE world. It’s just sad that Google doesn’t say clearly what does this mean that your keys are hardware-backed. For most of the people Google documentation is enough but when you build an app where security is “a matter of life and death” there is a lack of information.

F-Secure Labs did a great job analyzing the security of the Android Keystore and you can find results below and sleep well.

Last, but not least – please read the article of Marc Obrador Sureda. That had a lot of self-denial to look for the answers what is the difference between “StrongBox” and “hardware-backed” keys? and inspired me to write this article.

Have a great day!
See you on The Code Side
Artur Latoszewski

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.