Most Android devices implement an Android Hardware-backed Keystore. The Rich Execution Environment (REE) applications, i.e., the unsecure ones, use a hardware root of trust and an application in the Trusted Execution Environment (TEE). Usually, as all the cryptographic operations occur only in the trusted part, these keys should be safe.
Three researchers from the Tel-Aviv university demonstrated that it is not necessarily the case. ARM’s TrustZone is one of the most used TEEs. Each vendor must write its own Trusted Application (TA) that executes in the TrustZone for its key store. The researchers reverse-engineered the Samsung implementation for S8, S9, S20, and S21. They succeeded in breaching the keys protected by the key store.
The breach is not due to a vulnerability in TrustZone. It is due to design errors in the TA.
When REE requests to generate a new key, the TA returns a wrapped key, i.e., a key encrypted with a key stored in the root of trust. In a simplified explanation, the wrapped key is the newly generated key AES-CGM-encrypted with an IV provided by the REE application and a Hardware-Derived Key (HDK) derived from some information supplied by the REE application and the hardware root of trust key.
In other words, the REE application provides the IV and some data that generate the HDK. AES-CGM is a stream cipher (uses AES CTR), and thus it is sensitive to IV reuse. With a streamcipher, you must never reuse an IV with the same key. Else, it is easy to retrieve the encrypted message with a known ciphertext. In this case, the attacker has access to the IV used to encrypt the wrapped key and can provide the same `seed` for generating the HDK. Game over!
In S20 and S21, the key derivation function adds some randomness for each new HDK. The attacker cannot anymore generate the same HDK. Unfortunately, the S20 andS21 TA contains the old derivation function. The researchers found a way to downgrade to the S9 HDK. Once more, game over!
- Never reuse an IV with a streamcipher. Do not trust the user to generate a new IV, do it yourself.
- A Trusted Execution Environment does not protect from a weak/wicked “trusted” application.
- If not necessary, remove all unused software from the implementation. You reduce the attack surface.
A. Shakevsky, E. Ronen, and A. Wool, “Trust Dies in Darkness: Shedding Light on Samsung’s TrustZone Keymaster Design,” 208, 2022. Available: http://eprint.iacr.org/2022/208