Debian Codegraph @

tests/test_privacy.py

raw symbols SCIP
1 from bluetooth_data_tools import get_cipher_for_irk, resolve_private_address
2
3 # A realistic, non-zero 128-bit IRK. The RPA vectors below were generated by
4 # encrypting ``b"\x00" * 13 + prand`` with this key under AES-ECB and appending
5 # the top 24 bits of the result as the hash — the same construction the resolver
6 # verifies. They are computed independently of the production helper so a
7 # byte-order or slicing regression in ``resolve_private_address`` is caught
8 # rather than mirrored.
9 IRK = bytes.fromhex("ec0234a357c8ad05341010a60a397d9b")
10
11 # (address, prand) pairs that resolve against ``IRK``. Each first octet has its
12 # top two bits set to 0b01, marking it as a resolvable private address.
13 VALID_RPAS = (
14 "40:01:02:b2:12:da",
15 "55:aa:33:1d:27:08",
16 "7f:ff:fe:7e:0e:08",
17 "40:00:00:45:0c:63",
18 )
19
20
21 def test_resolve_private_address():
22 cipher = get_cipher_for_irk(b"\x00" * 16)
23
24 assert resolve_private_address(cipher, "40:01:02:0a:c4:a6")
25 assert resolve_private_address(cipher, "40:02:03:d2:74:ce")
26 assert not resolve_private_address(cipher, "40:00:00:d2:74:ce")
27 assert not resolve_private_address(cipher, "00:01:ff:a0:3a:76")
28
29
30 def test_resolve_with_nonzero_irk():
31 """A realistic non-zero IRK resolves its own generated RPAs."""
32 cipher = get_cipher_for_irk(IRK)
33 for address in VALID_RPAS:
34 assert resolve_private_address(cipher, address), address
35
36
37 def test_resolve_is_case_insensitive():
38 """Uppercase hex in the address resolves the same as lowercase."""
39 cipher = get_cipher_for_irk(IRK)
40 assert resolve_private_address(cipher, "40:01:02:B2:12:DA")
41 assert resolve_private_address(cipher, "40:01:02:b2:12:da")
42
43
44 def test_resolve_rejects_wrong_hash():
45 """An RPA-typed address with a non-matching hash does not resolve."""
46 cipher = get_cipher_for_irk(IRK)
47 assert not resolve_private_address(cipher, "40:01:02:00:00:00")
48
49
50 def test_resolve_rejects_wrong_irk():
51 """A valid RPA does not resolve against a different IRK."""
52 cipher = get_cipher_for_irk(b"\x11" * 16)
53 assert not resolve_private_address(cipher, "40:01:02:b2:12:da")
54
55
56 def test_resolve_rejects_non_rpa_address_types():
57 """Only addresses whose top two bits are 0b01 are treated as RPAs.
58
59 The hash half is left identical to a known-good RPA; flipping only the
60 address-type bits in the first octet must short-circuit to ``False`` before
61 the hash is ever compared.
62 """
63 cipher = get_cipher_for_irk(IRK)
64 # 0x00 -> 0b00 (public / non-resolvable), 0x80 -> 0b10 (random static
65 # reserved), 0xc0 -> 0b11 (static random). None are resolvable.
66 assert not resolve_private_address(cipher, "00:01:02:b2:12:da")
67 assert not resolve_private_address(cipher, "80:01:02:b2:12:da")
68 assert not resolve_private_address(cipher, "c0:01:02:b2:12:da")
69
70
71 def test_resolve_full_rpa_type_range():
72 """Every first octet in 0x40..0x7f is an RPA candidate (checked, not rejected).
73
74 A candidate with a deliberately wrong hash returns ``False`` via the hash
75 comparison rather than the address-type gate, while values outside the range
76 are rejected by the gate. This pins the 0x40/0x7f boundary.
77 """
78 cipher = get_cipher_for_irk(IRK)
79 for first in range(0x40, 0x80):
80 address = f"{first:02x}:00:00:00:00:00"
81 # Wrong hash, but still inside the RPA range -> reaches the comparison.
82 assert not resolve_private_address(cipher, address), address
83 # Just outside the range on either side.
84 assert not resolve_private_address(cipher, "3f:00:00:00:00:00")
85 assert not resolve_private_address(cipher, "80:00:00:00:00:00")
86
87
88 def test_cipher_is_reusable_across_addresses():
89 """One cipher resolves many addresses — the consumer's steady-state pattern.
90
91 ``resolve_private_address`` creates a fresh encryptor per call, so a single
92 ``Cipher`` built once per IRK must stay usable across an unbounded number of
93 resolutions (and re-resolving the same address stays stable).
94 """
95 cipher = get_cipher_for_irk(IRK)
96 for _ in range(3):
97 for address in VALID_RPAS:
98 assert resolve_private_address(cipher, address), address
99 assert not resolve_private_address(cipher, "40:01:02:00:00:00")