Skip to content

Instantly share code, notes, and snippets.

@Munken
Created June 8, 2021 19:34
Show Gist options
  • Save Munken/7596dba69590f99ab387af967580d3c8 to your computer and use it in GitHub Desktop.
Save Munken/7596dba69590f99ab387af967580d3c8 to your computer and use it in GitHub Desktop.
b = bytes.fromhex("02c5f205f48bf0954a0c0e631fee98d4bb1b0199af485adf00000004111011121314151617226062636465666768")
print(b.hex())
for i, B in enumerate(b):
print("{:02d} 0x{:02x}".format(i, B))
print(b)
dtype = b[0]
options = int.from_bytes(b[1:2], byteorder='little')
extended = int.from_bytes(b[2:3], byteorder='little')
opt_names = (
"MAC sequence num cap.",
"RXOnCapability",
"Reserved",
"Reserved",
"PANId request",
"GP Security Request",
"Fixed location",
"Extended Options field"
)
extended_names = (
("SecurityLevel capabilities", 0, 1),
("KeyType", 2, 4),
("GPD Key present", 5, 5),
("GPD Key encryption", 6, 6),
("GPD outgoing present", 7, 7),
)
has_extended = options & 0b10000000 != 0
has_key = False
has_counter = False
for i, name in enumerate(opt_names):
flag = options & (0b1 << i)
flag = flag != 0
print("{:30s} {}".format(name, flag))
print()
if has_extended:
for name, l, h in extended_names:
mask = 0
for j in range(l, h+1):
mask |= 0b1 << j
v = (extended & mask) >> l
print("{:30s} {:b}".format(name, v))
has_key = extended & 0b00010000 != 0
has_counter = extended & 0b10000000 != 0
print()
print("Has key {}".format(has_key))
offset = 3 if has_extended else 2
if has_key:
key = int.from_bytes(b[offset:offset+16], byteorder='little')
mic = int.from_bytes(b[offset+16:offset+20], byteorder='little')
print("Key 0x{:032x}".format(key))
print("MIC 0x{:08x}".format(mic))
offset += 20
print()
print("Has counter {}".format(has_key))
if has_counter:
counter = int.from_bytes(b[offset:offset+4], byteorder='little')
print("Counter 0x{:08x}".format(counter))
offset += 4
app_info = int.from_bytes(b[offset:offset+1], byteorder='little')
print("App info 0x{:02x}".format(app_info))
offset += 1
print()
data_length = int.from_bytes(b[offset:offset+1], byteorder='little')
print("Optional data length 0x{:02x}".format(data_length))
offset += 1
for i in range(data_length):
v = int.from_bytes(b[offset+i:offset+i+1], byteorder='little')
print("Value {:02d} 0x{:02x}".format(i, v))
@Munken
Copy link
Author

Munken commented Jun 8, 2021

Output:

00 0x02
01 0xc5
02 0xf2
03 0x05
04 0xf4
05 0x8b
06 0xf0
07 0x95
08 0x4a
09 0x0c
10 0x0e
11 0x63
12 0x1f
13 0xee
14 0x98
15 0xd4
16 0xbb
17 0x1b
18 0x01
19 0x99
20 0xaf
21 0x48
22 0x5a
23 0xdf
24 0x00
25 0x00
26 0x00
27 0x04
28 0x11
29 0x10
30 0x11
31 0x12
32 0x13
33 0x14
34 0x15
35 0x16
36 0x17
37 0x22
38 0x60
39 0x62
40 0x63
41 0x64
42 0x65
43 0x66
44 0x67
45 0x68


MAC sequence num cap.          True
RXOnCapability                 False
Reserved                       True
Reserved                       False
PANId request                  False
GP Security Request            False
Fixed location                 True
Extended Options field         True

SecurityLevel capabilities     10
KeyType                        100
GPD Key present                1
GPD Key encryption             1
GPD outgoing present           1

Has key                        True
Key                            0x011bbbd498ee1f630e0c4a95f08bf405
MIC                            0x5a48af99

Has counter                    True
Counter                        0x000000df
App info                       0x04

Optional data length           0x11
Value 00                       0x10
Value 01                       0x11
Value 02                       0x12
Value 03                       0x13
Value 04                       0x14
Value 05                       0x15
Value 06                       0x16
Value 07                       0x17
Value 08                       0x22
Value 09                       0x60
Value 10                       0x62
Value 11                       0x63
Value 12                       0x64
Value 13                       0x65
Value 14                       0x66
Value 15                       0x67
Value 16                       0x68

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment