Debian Codegraph @

src/bluetooth_data_tools/privacy.py

raw symbols SCIP
1 """Helpers for resolving a private address if you know its identity resolving key.
2
3 This process uses 128bit IRK as encryption key for ECB AES.
4
5 One half of the address stores a random 24bit number (prand).
6 This is encrypted to produce a "hash": the least significant 24
7 bits of the AES output. That hash should match the other half of
8 the MAC address.
9
10 See https://www.mdpi.com/2227-7390/10/22/4346
11 """
12
13 import binascii
14 import hmac
15
16 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
17
18 PADDING = b"\x00" * 13
19
20
21 def get_cipher_for_irk(irk: bytes) -> Cipher:
22 return Cipher(algorithms.AES(irk), modes.ECB()) # noqa: S305
23
24
25 def resolve_private_address(
26 cipher: Cipher,
27 address: str,
28 ) -> bool:
29 rpa = binascii.unhexlify(address.replace(":", ""))
30
31 if rpa[0] & 0xC0 != 0x40:
32 # Not an RPA
33 return False
34
35 pt = PADDING + rpa[:3]
36
37 encryptor = cipher.encryptor()
38 ct = encryptor.update(pt) + encryptor.finalize()
39
40 return hmac.compare_digest(ct[13:], rpa[3:])