I'm trying to store a SecretKey in the Android KeyStore, and I'm even directly following the documentation found here: KeyProtection: AES Key for Encryption / Description in GCM Mode, which still doesn't work.
I seem to get the following exception when I run this:
java.security.KeyStoreException: unsupported protection parameter
This exception is specifically raised when I call keyStore.setEntry(...).
I'm running Android 10 on a Samsung Galaxy S10, if that makes any difference.
@RequiresApi(23)
private static KeyStore.ProtectionParameter getProtectionParameter() {
return new KeyProtection.Builder(KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.build();
}
@RequiresApi(23)
private static SecretKey getSecretKey() throws Exception {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null);
if (keyStore.containsAlias(KEY_ALIAS)) {
final KeyStore.SecretKeyEntry secretKeyEntry = (KeyStore.SecretKeyEntry) keyStore
.getEntry(KEY_ALIAS, null);
return secretKeyEntry.getSecretKey();
}
final KeyGenerator keyGenerator = KeyGenerator
.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
final KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(KEY_ALIAS,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.build();
keyGenerator.init(keyGenParameterSpec);
SecretKey k = keyGenerator.generateKey();
keyStore.setEntry(
KEY_ALIAS,
new KeyStore.SecretKeyEntry(k),
getProtectionParameter()
);
return getSecretKey();
}