Merge "Improve the AndroidKeyStore-backed HMAC state machine."

This commit is contained in:
Alex Klyubin
2015-04-02 21:06:27 +00:00
committed by Gerrit Code Review
2 changed files with 18 additions and 10 deletions

View File

@@ -264,8 +264,6 @@ public abstract class KeyStoreCipherSpi extends CipherSpi implements KeyStoreCry
@Override @Override
protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output,
int outputOffset) throws ShortBufferException { int outputOffset) throws ShortBufferException {
ensureKeystoreOperationInitialized();
byte[] outputCopy = engineUpdate(input, inputOffset, inputLen); byte[] outputCopy = engineUpdate(input, inputOffset, inputLen);
if (outputCopy == null) { if (outputCopy == null) {
return 0; return 0;

View File

@@ -78,7 +78,11 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp
} }
mKeyAliasInKeyStore = ((KeyStoreSecretKey) key).getAlias(); mKeyAliasInKeyStore = ((KeyStoreSecretKey) key).getAlias();
if (mKeyAliasInKeyStore == null) {
throw new InvalidKeyException("Key's KeyStore alias not known");
}
engineReset(); engineReset();
ensureKeystoreOperationInitialized();
} }
@Override @Override
@@ -90,8 +94,18 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp
} }
mOperationHandle = null; mOperationHandle = null;
mChunkedStreamer = null; mChunkedStreamer = null;
}
private void ensureKeystoreOperationInitialized() {
if (mChunkedStreamer != null) {
return;
}
if (mKeyAliasInKeyStore == null) {
throw new IllegalStateException("Not initialized");
}
KeymasterArguments keymasterArgs = new KeymasterArguments(); KeymasterArguments keymasterArgs = new KeymasterArguments();
keymasterArgs.addInt(KeymasterDefs.KM_TAG_ALGORITHM, KeyStoreKeyConstraints.Algorithm.HMAC);
keymasterArgs.addInt(KeymasterDefs.KM_TAG_DIGEST, mDigest); keymasterArgs.addInt(KeymasterDefs.KM_TAG_DIGEST, mDigest);
OperationResult opResult = mKeyStore.begin(mKeyAliasInKeyStore, OperationResult opResult = mKeyStore.begin(mKeyAliasInKeyStore,
@@ -105,10 +119,10 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp
} else if (opResult.resultCode != KeyStore.NO_ERROR) { } else if (opResult.resultCode != KeyStore.NO_ERROR) {
throw KeymasterUtils.getCryptoOperationException(opResult.resultCode); throw KeymasterUtils.getCryptoOperationException(opResult.resultCode);
} }
mOperationToken = opResult.token; if (opResult.token == null) {
if (mOperationToken == null) {
throw new CryptoOperationException("Keystore returned null operation token"); throw new CryptoOperationException("Keystore returned null operation token");
} }
mOperationToken = opResult.token;
mOperationHandle = opResult.operationHandle; mOperationHandle = opResult.operationHandle;
mChunkedStreamer = new KeyStoreCryptoOperationChunkedStreamer( mChunkedStreamer = new KeyStoreCryptoOperationChunkedStreamer(
new KeyStoreCryptoOperationChunkedStreamer.MainDataStream( new KeyStoreCryptoOperationChunkedStreamer.MainDataStream(
@@ -122,9 +136,7 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp
@Override @Override
protected void engineUpdate(byte[] input, int offset, int len) { protected void engineUpdate(byte[] input, int offset, int len) {
if (mChunkedStreamer == null) { ensureKeystoreOperationInitialized();
throw new IllegalStateException("Not initialized");
}
byte[] output; byte[] output;
try { try {
@@ -139,9 +151,7 @@ public abstract class KeyStoreHmacSpi extends MacSpi implements KeyStoreCryptoOp
@Override @Override
protected byte[] engineDoFinal() { protected byte[] engineDoFinal() {
if (mChunkedStreamer == null) { ensureKeystoreOperationInitialized();
throw new IllegalStateException("Not initialized");
}
byte[] result; byte[] result;
try { try {