Skip to content

Commit b360294

Browse files
authored
Merge pull request #30 from Bluetooth-Devices/fix-remove-cipher-update
feat: remove cipher update
2 parents 9675913 + 21b9390 commit b360294

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

src/bthome_ble/bthome_v2_encryption.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def decrypt_payload(
2626
print("CryptData:", payload.hex())
2727
print("Mic:", mic.hex())
2828
cipher = AES.new(key, AES.MODE_CCM, nonce=nonce, mac_len=4)
29-
cipher.update(b"\x11")
3029
print()
3130
print("Starting Decryption data")
3231
try:
@@ -71,7 +70,6 @@ def encrypt_payload(
7170
"""Encrypt payload."""
7271
nonce = b"".join([mac, uuid16, sw_version, count_id]) # 6+2+1+4 = 13 bytes
7372
cipher = AES.new(key, AES.MODE_CCM, nonce=nonce, mac_len=4)
74-
cipher.update(b"\x11")
7573
ciphertext, mic = cipher.encrypt_and_digest(data)
7674
print("MAC:", mac.hex())
7775
print("Binkey:", key.hex())
@@ -96,7 +94,7 @@ def main() -> None:
9694
count_id = bytes(bytearray.fromhex("00112233")) # count id (change every message)
9795
mac = binascii.unhexlify("5448E68F80A5") # MAC
9896
uuid16 = b"\xD2\xFC"
99-
sw_version = b"\x42"
97+
sw_version = b"\x41"
10098
bindkey = binascii.unhexlify("231d39c1d7cc1ab1aee224cd096db932")
10199

102100
payload = encrypt_payload(

src/bthome_ble/parser.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ def _decrypt_bthome(self, data: bytes, bthome_mac: bytes, sw_version: int) -> by
493493
# nonce: mac [6], uuid16 [2 (v1) or 3 (v2)], count_id [4]
494494
nonce = b"".join([bthome_mac, uuid, count_id])
495495
cipher = AES.new(self.bindkey, AES.MODE_CCM, nonce=nonce, mac_len=4)
496-
cipher.update(b"\x11")
496+
if sw_version == 1:
497+
cipher.update(b"\x11")
497498

498499
# decrypt the data
499500
try:

tests/test_parser_v2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def test_bindkey_wrong():
221221
def test_bindkey_correct():
222222
"""Test BTHome parser with correct encryption key."""
223223
bindkey = "231d39c1d7cc1ab1aee224cd096db932"
224-
data_string = b"\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\xb7\xce\xd8\xe5"
224+
data_string = b"\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\x78\x23\x72\x14"
225225
advertisement = bytes_to_service_info(
226226
data_string,
227227
local_name="TEST DEVICE",
@@ -276,7 +276,7 @@ def test_bindkey_correct():
276276
def test_bindkey_verified_can_be_unset():
277277
"""Test BTHome parser with wrong encryption key."""
278278
bindkey = "814aac74c4f17b6c1581e1ab87816b99"
279-
data_string = b'\x41\xfb\xa45\xe4\xd3\xc3\x12\xfb\x00\x11"3W\xd9\n\x99'
279+
data_string = b"\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\x78\x23\x72\x14"
280280
advertisement = bytes_to_service_info(
281281
data_string,
282282
local_name="ATC_8D18B2",

tests/test_v2_encryption.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_encryption_example():
1010
count_id = bytes(bytearray.fromhex("00112233")) # count id (change every message)
1111
mac = binascii.unhexlify("5448E68F80A5") # MAC
1212
uuid16 = b"\xd2\xfc"
13-
sw_version = b"\x42"
13+
sw_version = b"\x41"
1414
bindkey = binascii.unhexlify("231d39c1d7cc1ab1aee224cd096db932")
1515

1616
encrypted_payload = encrypt_payload(
@@ -23,7 +23,7 @@ def test_encryption_example():
2323
)
2424
assert (
2525
encrypted_payload
26-
== b"\xd2\xfc\x42\x69\x6c\x8c\x91\x85\x6f\x00\x11\x22\x33\x04\x5b\xbc\x2a"
26+
== b"\xd2\xfc\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\x78\x23\x72\x14"
2727
)
2828
assert decrypt_aes_ccm(key=bindkey, mac=mac, data=encrypted_payload) == {
2929
"humidity": 50.55,

0 commit comments

Comments
 (0)