In the code of KeyStoreUsage from the android developer samples I see the following snippet:
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
KeyStore.Entry entry = ks.getEntry(alias, null);
if (!(entry instanceof PrivateKeyEntry)) {
Log.w(TAG, "Not an instance of a PrivateKeyEntry");
return null;
}
Signature s = Signature.getInstance("SHA256withECDSA");
s.initSign(((PrivateKeyEntry) entry).getPrivateKey());
s.update(data);
byte[] signature = s.sign();
What is the benefit of storing keys in hardware, when all the crypto operations take place in normal memory? As I understand the architecture of android the apps are sandboxed so it would be secure, if the device is not rooted. But if it is rooted the unencrypted private key could be extracted from memory, right?
Is there any possibility in android devices (besides using UICC or dedicated microSD-HSM) to do all the crypto operations in the same hardware where the keys are stored, so the private key remains save even if the device is rooted?
EDIT
As it seems there's a little misunderstanding regarding my question, I'll try to clarify it a little bit. I'm well aware that normally hardware-backed keys are not exportable, I've already been working with HSM-Appliances from Utimaco and Luna. I just wondered about key-security in Android, because the documentation is not clear to me. On the one hand it states that keys are safe and hardware-backed (which means not extractable to me), on the other I find code-snippets like the one above which clearly indicate that the keys are extractable. So I wondered if I got things completely wrong and there is a standard way (not SEEK) in newer android versions to store keys in hardware in a way that they are not extractable and let the hardware do all the crypto-operations.
EDIT 2
Well, I feel a little silly now. After doing more research I found that I had a really narrow understanding how things work (and some misunderstandings due to the fact I'm no native speaker). As Steve already said the PrivateKey-objects are only handles, not the keys themselves. The JCE-provider is responsible how to deal with the keys referenced by these handles. In case of hardware-backed keys the keys will stay in hardware and all the operations that look like they are done in memory (on the first look) will be delegated to the hardware. So Steve's answer is right. Please bear with me for making things a little complicated...