1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-08-23 15:51:47 +02:00

[Huawei] Add HiChainLite firstKey handling

This commit is contained in:
Damien 'Psolyca' Gaignon 2024-02-05 19:09:55 +01:00
parent 270212a771
commit 05c11cbd14
No known key found for this signature in database
GPG Key ID: 9E9404E5D9E11843
3 changed files with 39 additions and 6 deletions

View File

@ -119,7 +119,11 @@ public class HuaweiCrypto {
.put(message) .put(message)
.array(); .array();
byte[] digestStep1 = CryptoUtils.calcHmacSha256(msgToDigest, nonce); byte[] digestStep1 = CryptoUtils.calcHmacSha256(msgToDigest, nonce);
return CryptoUtils.calcHmacSha256(digestStep1, nonce); byte[] challenge = ByteBuffer.allocate(0x40)
.put(CryptoUtils.calcHmacSha256(digestStep1, nonce))
.put(digestStep1)
.array();
return challenge;
} }
public byte[] computeDigestHiChainLite(byte[] message, byte[] key, byte[] nonce) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, UnsupportedEncodingException { public byte[] computeDigestHiChainLite(byte[] message, byte[] key, byte[] nonce) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, UnsupportedEncodingException {
@ -138,7 +142,11 @@ public class HuaweiCrypto {
} else { } else {
digestStep1 = CryptoUtils.calcHmacSha256(msgToDigest, nonce); digestStep1 = CryptoUtils.calcHmacSha256(msgToDigest, nonce);
} }
return CryptoUtils.calcHmacSha256(digestStep1, nonce); byte[] challenge = ByteBuffer.allocate(0x40)
.put(CryptoUtils.calcHmacSha256(digestStep1, nonce))
.put(digestStep1)
.array();
return challenge;
} }
public byte[] digestChallenge(byte[] secretKey, byte[] nonce) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, UnsupportedEncodingException { public byte[] digestChallenge(byte[] secretKey, byte[] nonce) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, UnsupportedEncodingException {
@ -150,7 +158,11 @@ public class HuaweiCrypto {
.put(secretKey) .put(secretKey)
.put(MESSAGE_CHALLENGE) .put(MESSAGE_CHALLENGE)
.array(); .array();
return CryptoUtils.calcHmacSha256(key, nonce); byte[] challenge = ByteBuffer.allocate(0x40)
.put(CryptoUtils.calcHmacSha256(key, nonce))
.put(key)
.array();
return challenge;
} }
return computeDigestHiChainLite(MESSAGE_CHALLENGE, secretKey, nonce); return computeDigestHiChainLite(MESSAGE_CHALLENGE, secretKey, nonce);
} }
@ -166,7 +178,11 @@ public class HuaweiCrypto {
.put(secretKey) .put(secretKey)
.put(MESSAGE_RESPONSE) .put(MESSAGE_RESPONSE)
.array(); .array();
return CryptoUtils.calcHmacSha256(key, nonce); byte[] challenge = ByteBuffer.allocate(0x40)
.put(CryptoUtils.calcHmacSha256(key, nonce))
.put(key)
.array();
return challenge;
} }
return computeDigestHiChainLite(MESSAGE_RESPONSE, secretKey, nonce); return computeDigestHiChainLite(MESSAGE_RESPONSE, secretKey, nonce);
} }

View File

@ -58,6 +58,7 @@ public class HuaweiPacket {
protected byte interval; protected byte interval;
protected byte authAlgo; protected byte authAlgo;
protected byte encryptMethod; protected byte encryptMethod;
protected byte[] firstKey;
public void setAuthVersion(byte authVersion) { public void setAuthVersion(byte authVersion) {
this.authVersion = authVersion; this.authVersion = authVersion;
@ -153,6 +154,14 @@ public class HuaweiPacket {
public byte getEncryptMethod () { public byte getEncryptMethod () {
return this.encryptMethod; return this.encryptMethod;
} }
public void setFirstKey(byte[] firstKey) {
this.firstKey = firstKey;
}
public byte[] getFirstKey() {
return firstKey;
}
} }
public static abstract class ParseException extends Exception { public static abstract class ParseException extends Exception {

View File

@ -74,8 +74,13 @@ public class GetAuthRequest extends Request {
.putShort(authVersion) .putShort(authVersion)
.put(clientNonce) .put(clientNonce)
.array(); .array();
byte[] challenge = huaweiCrypto.digestChallenge(key, doubleNonce); ByteBuffer digestedChallenge = ByteBuffer.wrap(huaweiCrypto.digestChallenge(key, doubleNonce));
byte[] challenge = new byte[0x20];
digestedChallenge.get(challenge, 0x00, 0x20);
LOG.debug("challenge: " + GB.hexdump(challenge)); LOG.debug("challenge: " + GB.hexdump(challenge));
byte[] firstKey = new byte[0x10];
digestedChallenge.get(firstKey, 0x00, 0x10);
paramsProvider.setFirstKey(firstKey);
if (challenge == null) if (challenge == null)
throw new RequestCreationException("Challenge null"); throw new RequestCreationException("Challenge null");
return new DeviceConfig.Auth.Request(paramsProvider, challenge, nonce).serialize(); return new DeviceConfig.Auth.Request(paramsProvider, challenge, nonce).serialize();
@ -94,7 +99,10 @@ public class GetAuthRequest extends Request {
throw new ResponseTypeMismatchException(receivedPacket, DeviceConfig.Auth.Response.class); throw new ResponseTypeMismatchException(receivedPacket, DeviceConfig.Auth.Response.class);
try { try {
byte[] expectedAnswer = huaweiCrypto.digestResponse(key, doubleNonce); ByteBuffer digestedChallenge = ByteBuffer.wrap(huaweiCrypto.digestResponse(key, doubleNonce));
byte[] expectedAnswer = new byte[0x20];
digestedChallenge.get(expectedAnswer, 0x00, 0x20);
LOG.debug("challenge: " + GB.hexdump(expectedAnswer));
if (expectedAnswer == null) if (expectedAnswer == null)
throw new ResponseParseException("Challenge null"); throw new ResponseParseException("Challenge null");
byte[] actualAnswer = ((DeviceConfig.Auth.Response) receivedPacket).challengeResponse; byte[] actualAnswer = ((DeviceConfig.Auth.Response) receivedPacket).challengeResponse;