mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-25 03:16:51 +01:00
[Huawei] Change bond request behaviour
This commit is contained in:
parent
05c11cbd14
commit
bb5fe00643
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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(0x07, iv);
|
||||
this.isEncrypted = false;
|
||||
this.complete = true;
|
||||
} catch (InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | BadPaddingException | InvalidKeyException e) {
|
||||
throw new CryptoException("Bonding key creation exception", e);
|
||||
}
|
||||
this.tlv = new HuaweiTLV()
|
||||
.put(0x01)
|
||||
.put(0x03, (byte) 0x00)
|
||||
.put(0x05, clientSerial)
|
||||
.put(0x06, key)
|
||||
.put(0x07, iv);
|
||||
this.isEncrypted = false;
|
||||
this.complete = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user