Fix a number of kitkat compatibility bugs

This commit is contained in:
Alexander Bakker 2018-06-06 22:03:17 +02:00
parent 3f01a0a3da
commit d27d54f811
2 changed files with 13 additions and 3 deletions

View file

@ -1,5 +1,7 @@
package me.impy.aegis.crypto; package me.impy.aegis.crypto;
import android.os.Build;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -9,6 +11,7 @@ import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays; import java.util.Arrays;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
@ -18,6 +21,7 @@ import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import org.spongycastle.crypto.generators.SCrypt; import org.spongycastle.crypto.generators.SCrypt;
@ -58,7 +62,13 @@ public class CryptoUtils {
// generate the nonce if none is given // generate the nonce if none is given
// we are not allowed to do this ourselves as "setRandomizedEncryptionRequired" is set to true // we are not allowed to do this ourselves as "setRandomizedEncryptionRequired" is set to true
if (nonce != null) { if (nonce != null) {
GCMParameterSpec spec = new GCMParameterSpec(CRYPTO_AEAD_TAG_SIZE * 8, nonce); AlgorithmParameterSpec spec;
// apparently kitkat doesn't support GCMParameterSpec
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
spec = new IvParameterSpec(nonce);
} else {
spec = new GCMParameterSpec(CRYPTO_AEAD_TAG_SIZE * 8, nonce);
}
cipher.init(opmode, key, spec); cipher.init(opmode, key, spec);
} else { } else {
cipher.init(opmode, key); cipher.init(opmode, key);

View file

@ -44,9 +44,9 @@ public abstract class Slot implements Serializable {
CryptResult res = CryptoUtils.decrypt(_encryptedMasterKey, cipher, _encryptedMasterKeyParams); CryptResult res = CryptoUtils.decrypt(_encryptedMasterKey, cipher, _encryptedMasterKeyParams);
SecretKey key = new SecretKeySpec(res.getData(), CryptoUtils.CRYPTO_AEAD); SecretKey key = new SecretKeySpec(res.getData(), CryptoUtils.CRYPTO_AEAD);
return new MasterKey(key); return new MasterKey(key);
} catch (AEADBadTagException e) { } catch (BadPaddingException e) {
throw new SlotIntegrityException(e); throw new SlotIntegrityException(e);
} catch (IOException | BadPaddingException | IllegalBlockSizeException e) { } catch (IOException | IllegalBlockSizeException e) {
throw new SlotException(e); throw new SlotException(e);
} }
} }