1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 21:06:50 +01:00

[Huawei] Change bond request behaviour

This commit is contained in:
Damien 'Psolyca' Gaignon 2024-02-05 20:16:23 +01:00
parent 05c11cbd14
commit bb5fe00643
No known key found for this signature in database
GPG Key ID: 9E9404E5D9E11843
3 changed files with 38 additions and 22 deletions

View File

@ -226,8 +226,9 @@ public class HuaweiCrypto {
return Arrays.copyOfRange(finalMixedKeyHash, 0, 16);
}
public byte[] encryptBondingKey(byte[] data, String mac, byte[] iv) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IllegalArgumentException {
byte[] encryptionKey = createSecretKey(mac);
public byte[] encryptBondingKey(byte encryptMethod, byte[] data, byte[] encryptionKey, byte[] iv) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, IllegalArgumentException {
if (encryptMethod == 0x01)
return CryptoUtils.encryptAES_GCM_NoPad(data, encryptionKey, iv, null);
return CryptoUtils.encryptAES_CBC_Pad(data, encryptionKey, iv);
}

View File

@ -469,26 +469,21 @@ public class DeviceConfig {
public Request(
ParamsProvider paramsProvider,
byte[] clientSerial,
String mac,
HuaweiCrypto huaweiCrypto
) throws CryptoException {
byte[] key,
byte[] iv
) {
super(paramsProvider);
this.serviceId = DeviceConfig.id;
this.commandId = id;
byte[] iv = paramsProvider.getIv();
try {
this.tlv = new HuaweiTLV()
.put(0x01)
.put(0x03, (byte) 0x00)
.put(0x05, clientSerial)
.put(0x06, huaweiCrypto.encryptBondingKey(paramsProvider.getSecretKey(), mac, iv))
.put(0x06, key)
.put(0x07, iv);
this.isEncrypted = false;
this.complete = true;
} catch (InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | BadPaddingException | InvalidKeyException e) {
throw new CryptoException("Bonding key creation exception", e);
}
}
}

View File

@ -19,11 +19,20 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiCrypto;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.DeviceConfig;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class GetBondRequest extends Request {
private static final Logger LOG = LoggerFactory.getLogger(GetBondRequest.class);
@ -39,14 +48,25 @@ public class GetBondRequest extends Request {
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
try {
byte[] iv = paramsProvider.getIv();
huaweiCrypto = new HuaweiCrypto(paramsProvider.getAuthVersion());
byte[] encryptionKey;
if (paramsProvider.getDeviceSupportType() == 0x02) { //HiChainLite
encryptionKey = paramsProvider.getFirstKey();
} else {
encryptionKey = huaweiCrypto.createSecretKey(supportProvider.getDeviceMac());
}
byte[] key = huaweiCrypto.encryptBondingKey(paramsProvider.getEncryptMethod(), paramsProvider.getSecretKey(), encryptionKey, iv);
LOG.debug("key: " + GB.hexdump(key));
return new DeviceConfig.Bond.Request(
paramsProvider,
supportProvider.getSerial(),
supportProvider.getDeviceMac(),
huaweiCrypto
key,
iv
).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);
} catch (HuaweiPacket.CryptoException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | IllegalBlockSizeException |
BadPaddingException e) {
throw new RequestCreationException(e.toString());
}
}