Fossil Hybrid HR: some code cleanups

This commit is contained in:
Andreas Shimokawa
2025-07-21 23:36:12 +02:00
parent 62dbbea218
commit a490288b14
@@ -24,7 +24,6 @@ import java.nio.ByteOrder;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import java.util.zip.CRC32; import java.util.zip.CRC32;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
@@ -41,23 +40,17 @@ import nodomain.freeyourgadget.gadgetbridge.util.CRC32C;
import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils; import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
public abstract class FileEncryptedGetRequest extends FossilRequest implements FileEncryptedInterface{ public abstract class FileEncryptedGetRequest extends FossilRequest implements FileEncryptedInterface {
private byte majorHandle; private final byte majorHandle;
private byte minorHandle; private final byte minorHandle;
private FossilHRWatchAdapter adapter; private final FossilHRWatchAdapter adapter;
int fileSize;
private ByteBuffer fileBuffer; private ByteBuffer fileBuffer;
private byte[] fileData; private byte[] fileData;
private boolean finished = false; private boolean finished = false;
private Cipher cipher; private Cipher cipher;
private SecretKeySpec keySpec; private SecretKeySpec keySpec;
private byte[] originalIv; private byte[] originalIv;
int fileSize;
private int packetCount = 0; private int packetCount = 0;
private int ivIncrementor = 0x1f; private int ivIncrementor = 0x1f;
@@ -101,7 +94,7 @@ public abstract class FileEncryptedGetRequest extends FossilRequest implements F
originalIv[7]++; originalIv[7]++;
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) { } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace(); log(e.getMessage());
} }
} }
@@ -109,7 +102,7 @@ public abstract class FileEncryptedGetRequest extends FossilRequest implements F
return adapter; return adapter;
} }
private byte[] incrementIV(byte[] iv, int amount){ private byte[] incrementIV(byte[] iv, int amount) {
byte[] incrementedIv = new byte[iv.length]; byte[] incrementedIv = new byte[iv.length];
System.arraycopy(iv, 0, incrementedIv, 0, iv.length); System.arraycopy(iv, 0, incrementedIv, 0, iv.length);
ByteBuffer buffer = ByteBuffer.wrap(incrementedIv); ByteBuffer buffer = ByteBuffer.wrap(incrementedIv);
@@ -160,7 +153,8 @@ public abstract class FileEncryptedGetRequest extends FossilRequest implements F
ByteBuffer buffer = ByteBuffer.wrap(value); ByteBuffer buffer = ByteBuffer.wrap(value);
buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.order(ByteOrder.LITTLE_ENDIAN);
short handle = buffer.getShort(1); short minorHandle = buffer.get(1);
short majorHandle = buffer.get(2);
if (this.minorHandle != minorHandle) { if (this.minorHandle != minorHandle) {
throw new RuntimeException("minor handle: " + minorHandle + " expected: " + this.minorHandle); throw new RuntimeException("minor handle: " + minorHandle + " expected: " + this.minorHandle);
} }
@@ -185,24 +179,24 @@ public abstract class FileEncryptedGetRequest extends FossilRequest implements F
} else if (characteristic.getUuid().toString().equals("3dda0004-957f-7d4a-34a6-74696673696d")) { } else if (characteristic.getUuid().toString().equals("3dda0004-957f-7d4a-34a6-74696673696d")) {
try { try {
byte[] result = null; byte[] result = null;
if(packetCount == 1) { if (packetCount == 1) {
for(int testIvSummand = 0x1e; testIvSummand < 0x30; testIvSummand++){ for (int testIvSummand = 0x1e; testIvSummand < 0x30; testIvSummand++) {
byte[] iv = incrementIV(originalIv, testIvSummand); byte[] iv = incrementIV(originalIv, testIvSummand);
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv)); cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
result = cipher.doFinal(value); result = cipher.doFinal(value);
int currentLength = fileBuffer.position() + result.length - 1; int currentLength = fileBuffer.position() + result.length - 1;
byte expectedByte = (currentLength == fileSize) ? (byte)0x81 : (byte)0x01; // 0x81 indicated the last payload byte expectedByte = (currentLength == fileSize) ? (byte) 0x81 : (byte) 0x01; // 0x81 indicated the last payload
if(result[0] == expectedByte){ if (result[0] == expectedByte) {
this.ivIncrementor = testIvSummand; this.ivIncrementor = testIvSummand;
log("iv summand: " + testIvSummand); log("iv summand: " + testIvSummand);
break; break;
} }
log("no iv summand found"); log("no iv summand found");
} }
}else{ } else {
byte[] iv = incrementIV(originalIv, ivIncrementor * packetCount); byte[] iv = incrementIV(originalIv, ivIncrementor * packetCount);
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv)); cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
@@ -216,18 +210,14 @@ public abstract class FileEncryptedGetRequest extends FossilRequest implements F
if ((result[0] & 0x80) == 0x80) { if ((result[0] & 0x80) == 0x80) {
this.fileData = fileBuffer.array(); this.fileData = fileBuffer.array();
} }
} catch (BadPaddingException | IllegalBlockSizeException | InvalidAlgorithmParameterException | InvalidKeyException e) { } catch (BadPaddingException | IllegalBlockSizeException |
e.printStackTrace(); InvalidAlgorithmParameterException | InvalidKeyException e) {
log(e.getMessage());
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
} }
@Override
public UUID getRequestUUID() {
return UUID.fromString("3dda0003-957f-7d4a-34a6-74696673696d");
}
@Override @Override
public byte[] getStartSequence() { public byte[] getStartSequence() {
return new byte[]{1}; return new byte[]{1};