diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCrypto.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCrypto.java index 32c2fab91..38658959e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCrypto.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiCrypto.java @@ -248,9 +248,9 @@ public class HuaweiCrypto { } } - public static byte[] encrypt(byte encryptMethod, byte[] message, byte[] key, byte[] iv) throws CryptoException { + public static byte[] encrypt(boolean useGCM, byte[] message, byte[] key, byte[] iv) throws CryptoException { try { - if (encryptMethod == 0x01) + if (useGCM) return CryptoUtils.encryptAES_GCM_NoPad(message, key, iv, null); return CryptoUtils.encryptAES_CBC_Pad(message, key, iv); } catch (InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | BadPaddingException | InvalidKeyException | IllegalArgumentException e) { @@ -258,9 +258,9 @@ public class HuaweiCrypto { } } - public static byte[] decrypt(byte encryptMethod, byte[] message, byte[] key, byte[] iv) throws CryptoException { + public static byte[] decrypt(boolean useGCM, byte[] message, byte[] key, byte[] iv) throws CryptoException { try { - if (encryptMethod == 0x01) + if (useGCM) return CryptoUtils.decryptAES_GCM_NoPad(message, key, iv, null); return CryptoUtils.decryptAES_CBC_Pad(message, key, iv); } catch (InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | BadPaddingException | InvalidKeyException | IllegalArgumentException e) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java index bccc420ef..7eff8c12b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/huawei/HuaweiTLV.java @@ -291,7 +291,11 @@ public class HuaweiTLV { byte[] serializedTLV = serialize(); byte[] key = paramsProvider.getSecretKey(); byte[] nonce = paramsProvider.getIv(); - byte[] encryptedTLV = HuaweiCrypto.encrypt(paramsProvider.getEncryptMethod(), serializedTLV, key, nonce); + byte[] encryptedTLV = HuaweiCrypto.encrypt( + paramsProvider.getEncryptMethod() == 0x01 || paramsProvider.getDeviceSupportType() == 0x04, + serializedTLV, + key, + nonce); return new HuaweiTLV() .put(CryptoTags.encryption, (byte) 0x01) .put(CryptoTags.initVector, nonce) @@ -300,7 +304,11 @@ public class HuaweiTLV { public void decrypt(ParamsProvider paramsProvider) throws CryptoException, HuaweiPacket.MissingTagException { byte[] key = paramsProvider.getSecretKey(); - byte[] decryptedTLV = HuaweiCrypto.decrypt(paramsProvider.getEncryptMethod(), getBytes(CryptoTags.cipherText), key, getBytes(CryptoTags.initVector)); + byte[] decryptedTLV = HuaweiCrypto.decrypt( + paramsProvider.getEncryptMethod() == 0x01 || paramsProvider.getDeviceSupportType() == 0x04, + getBytes(CryptoTags.cipherText), + key, + getBytes(CryptoTags.initVector)); this.valueMap = new ArrayList<>(); parse(decryptedTLV); }