| 1 |
"""Example showing encoding and decoding of BTHome v2 advertisement""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import binascii |
| 6 |
|
| 7 |
from cryptography.hazmat.primitives.ciphers.aead import AESCCM |
| 8 |
|
| 9 |
|
| 10 |
def parse_value(data: bytes) -> dict[str, float]: |
| 11 |
"""Parse decrypted payload to readable BTHome data""" |
| 12 |
vlength = len(data) |
| 13 |
if vlength >= 3: |
| 14 |
temp = round(int.from_bytes(data[1:3], "little", signed=False) * 0.01, 2) |
| 15 |
humi = round(int.from_bytes(data[4:6], "little", signed=False) * 0.01, 2) |
| 16 |
print("Temperature:", temp, "Humidity:", humi) |
| 17 |
return {"temperature": temp, "humidity": humi} |
| 18 |
elif vlength == 2: |
| 19 |
motion = data[1] |
| 20 |
print("Motion:", motion) |
| 21 |
return {"Motion": motion} |
| 22 |
print("MsgLength:", vlength, "HexValue:", data.hex()) |
| 23 |
return {} |
| 24 |
|
| 25 |
|
| 26 |
def decrypt_payload( |
| 27 |
payload: bytes, mic: bytes, key: bytes, nonce: bytes |
| 28 |
) -> dict[str, float] | None: |
| 29 |
"""Decrypt payload.""" |
| 30 |
print("Nonce:", nonce.hex()) |
| 31 |
print("Ciphertext:", payload.hex()) |
| 32 |
print("MIC:", mic.hex()) |
| 33 |
cipher = AESCCM(key, tag_length=4) |
| 34 |
print() |
| 35 |
print("Starting Decryption data") |
| 36 |
try: |
| 37 |
data = cipher.decrypt(nonce, payload + mic, None) |
| 38 |
except ValueError as error: |
| 39 |
print() |
| 40 |
print("Decryption failed:", error) |
| 41 |
return None |
| 42 |
print("Decryption succeeded, decrypted data:", data.hex()) |
| 43 |
print() |
| 44 |
return parse_value(data=data) |
| 45 |
|
| 46 |
|
| 47 |
def decrypt_aes_ccm(key: bytes, mac: bytes, data: bytes) -> dict[str, float] | None: |
| 48 |
"""Decrypt AES CCM.""" |
| 49 |
print("MAC:", mac.hex()) |
| 50 |
print("Bindkey:", key.hex()) |
| 51 |
adslength = len(data) |
| 52 |
if adslength > 12 and data[0] == 0xD2 and data[1] == 0xFC: |
| 53 |
pkt = data[: data[0] + 1] |
| 54 |
uuid = pkt[0:2] |
| 55 |
sw_version = pkt[2:3] |
| 56 |
encrypted_data = pkt[3:-8] |
| 57 |
count_id = pkt[-8:-4] |
| 58 |
mic = pkt[-4:] |
| 59 |
|
| 60 |
nonce = b"".join([mac, uuid, sw_version, count_id]) |
| 61 |
return decrypt_payload(encrypted_data, mic, key, nonce) |
| 62 |
else: |
| 63 |
print("Error: format packet!") |
| 64 |
return None |
| 65 |
|
| 66 |
|
| 67 |
def encrypt_payload( |
| 68 |
data: bytes, |
| 69 |
mac: bytes, |
| 70 |
uuid16: bytes, |
| 71 |
sw_version: bytes, |
| 72 |
count_id: bytes, |
| 73 |
key: bytes, |
| 74 |
) -> bytes: |
| 75 |
"""Encrypt payload.""" |
| 76 |
nonce = b"".join([mac, uuid16, sw_version, count_id]) |
| 77 |
cipher = AESCCM(key, tag_length=4) |
| 78 |
result = cipher.encrypt(nonce, data, None) |
| 79 |
ciphertext = result[:-4] |
| 80 |
mic = result[-4:] |
| 81 |
print("MAC:", mac.hex()) |
| 82 |
print("Binkey:", key.hex()) |
| 83 |
print("Data:", data.hex()) |
| 84 |
print("Nonce:", nonce.hex()) |
| 85 |
print("Ciphertext:", ciphertext.hex()) |
| 86 |
print("count_id:", count_id.hex()) |
| 87 |
print("MIC:", mic.hex()) |
| 88 |
return b"".join([uuid16, sw_version, ciphertext, count_id, mic]) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
def main() -> None: |
| 95 |
"""Example to encrypt and decrypt BTHome payload.""" |
| 96 |
print() |
| 97 |
print("====== Test encode -----------------------------------------") |
| 98 |
data = bytes(bytearray.fromhex("02CA0903BF13")) |
| 99 |
parse_value(data) |
| 100 |
|
| 101 |
print() |
| 102 |
print("Preparing data for encryption") |
| 103 |
count_id = 1122867 |
| 104 |
count_id_bytes = count_id.to_bytes(4, "little") |
| 105 |
mac = binascii.unhexlify("5448E68F80A5") |
| 106 |
uuid16 = b"\xd2\xfc" |
| 107 |
sw_version = b"\x41" |
| 108 |
bindkey = binascii.unhexlify("231d39c1d7cc1ab1aee224cd096db932") |
| 109 |
|
| 110 |
payload = encrypt_payload( |
| 111 |
data=data, |
| 112 |
mac=mac, |
| 113 |
uuid16=uuid16, |
| 114 |
sw_version=sw_version, |
| 115 |
count_id=count_id_bytes, |
| 116 |
key=bindkey, |
| 117 |
) |
| 118 |
print() |
| 119 |
print("Encrypted data:", payload.hex()) |
| 120 |
print() |
| 121 |
print("====== Test decode -----------------------------------------") |
| 122 |
decrypt_aes_ccm(key=bindkey, mac=mac, data=payload) |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__": |
| 126 |
main() |