BLE pairing: from STK to LTK
In a smart building system there are two BLE devices: SamSU-DoorLock (a door lock) and SamSU-SmartLamp (a lamp). A phone app first paired with both using Legacy Pairing (Just Works), and later reconnected with a stored key. A radio sniffer recorded everything: ble.pcap.
File format: classic pcap, linktype 256 (BLUETOOTH_LE_LL_WITH_PHDR): each frame = a 10-byte phdr (RF channel, signal, noise, AA errors, reference access address, flags) + a Link Layer packet (Access Address, 2-byte header, payload, CRC). All LL packets are dewhitened. The AA of packets on the advertising channel = 0x8E89BED6; CONNECT_IND (PDU type 5) gives the connection AA, the addresses of both sides and the address types.
Direction: in each connection event the master (phone) sends a packet first, then the slave (device), so the order of packets for each AA is master, slave, master, slave... (there are empty PDUs too; the SN bits indicate retransmission). In encrypted packets the MIC is 4 bytes, and the CCM nonce and AAD follow Core Spec Vol 6 Part E (nonce = 39-bit packet counter | direction bit | IV, AAD = the first header byte with the NESN/SN/MD bits set to zero). An empty PDU is not encrypted and does not increment the counter.
All multi-byte fields are sent LSB-first over the air; the inputs/outputs of c1, s1 and e() (AES-128), however, are big-endian numbers, so the bytes must be reversed (Core Spec Vol 3 Part H 2.2). The MIC confirms that you derived the key correctly.
Task: The value written to handle 0x002A («Owner Key») inside the encrypted ATT Write Request sent to the SamSU-DoorLock device on reconnection is the flag: BAYROQ{...} (the last 16 characters are lowercase hex). Flag-like values written to other handles are decoys.