I am studying using Android KeyStore for Marshmallow and above.
I would like to simultaneously verify data integrity and authentication of my data using HMAC.
How do I achieve this?
I am currently generating an encryption / decryption key as follows: -
mKeyStore = KeyStore.getInstance(keyStoreName); mKeyStore.load(mKeyStoreLoadStoreParameter); if (mKeyStore.containsAlias(keyStoreAlias)) { mSecretKey = (SecretKey) mKeyStore.getKey(keyStoreAlias, KEY_STORE_PASSWORD); } else { final KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, keyStoreName); final int keyPurpose = KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT; keyGenerator.init( new KeyGenParameterSpec.Builder(keyStoreAlias, keyPurpose) .setKeySize(KEY_STORE_KEY_SIZE) .setBlockModes(KeyProperties.BLOCK_MODE_GCM) .setRandomizedEncryptionRequired(true) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) .build()); mSecretKey = keyGenerator.generateKey();
I found this sample to generate
HMAC <
SecretKey key = ...; // HMAC key of algorithm "HmacSHA512". KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); keyStore.load(null); keyStore.setEntry( "key1", new KeyStore.SecretKeyEntry(key), new KeyProtection.Builder(KeyProperties.PURPOSE_SIGN).build()); // Key imported, obtain a reference to it. SecretKey keyStoreKey = (SecretKey) keyStore.getKey("key1", null); // The original key can now be discarded. Mac mac = Mac.getInstance("HmacSHA512"); mac.init(keyStoreKey);
However, how can I use this when encrypting / decrypting my data?
EXPLANATION
I have a number of decisions / decisions that need to be made when implementing security / cryptography in any Android application.
one). Do I use cryptography of any type Yes or No? 2). If so, then ... I should try to reach the βmostβ safe solution.
If I am going to use cryptography, then I need to provide the following.
but). I store passwords / secret keys in a "Safe Place", for example. Android Key Store. b) I use the "strongest" cryptography. from). I would like to simultaneously check both the integrity of the data and the authentication of my data, for example. I would like to determine if my encrypted data has been changed.
As I understand what I read about HMAC, they provide this functionality. I would like to know how I encode the use of HMAC in my Android application to ensure data integrity and authentication of my data.
android cryptography keystore
Hector
source share