1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
"""Tests for the parser of BLE advertisements in BTHome V2 format."""
import binascii
from bthome_ble.bthome_v2_encryption import decrypt_aes_ccm, encrypt_payload
def test_encryption_example_1():
"""Test BTHome V2 encryption example."""
data = bytes(bytearray.fromhex("02CA0903BF13")) # BTHome data (not encrypted)
count_id = bytes(bytearray.fromhex("00112233")) # count id (change every message)
mac = binascii.unhexlify("5448E68F80A5") # MAC
uuid16 = b"\xd2\xfc"
sw_version = b"\x41"
bindkey = binascii.unhexlify("231d39c1d7cc1ab1aee224cd096db932")
encrypted_payload = encrypt_payload(
data=data,
mac=mac,
uuid16=uuid16,
sw_version=sw_version,
count_id=count_id,
key=bindkey,
)
assert (
encrypted_payload
== b"\xd2\xfc\x41\xa4\x72\x66\xc9\x5f\x73\x00\x11\x22\x33\x78\x23\x72\x14"
)
assert decrypt_aes_ccm(key=bindkey, mac=mac, data=encrypted_payload) == {
"humidity": 50.55,
"temperature": 25.06,
}
def test_encryption_example_2():
"""Test BTHome V2 encryption example."""
data = bytes(bytearray.fromhex("2101")) # BTHome data (not encrypted)
count_id = bytes(bytearray.fromhex("00112233")) # count id (change every message)
mac = binascii.unhexlify("5448E68F80A5") # MAC
uuid16 = b"\xd2\xfc"
sw_version = b"\x41"
bindkey = binascii.unhexlify("231d39c1d7cc1ab1aee224cd096db932")
encrypted_payload = encrypt_payload(
data=data,
mac=mac,
uuid16=uuid16,
sw_version=sw_version,
count_id=count_id,
key=bindkey,
)
print(encrypted_payload.hex())
assert encrypted_payload == b"\xd2\xfc\x41\x87\xb9\x00\x11\x22\x33\x72\xb0\x23\x1f"
assert decrypt_aes_ccm(key=bindkey, mac=mac, data=encrypted_payload) == {
"Motion": 1,
}
|