Created
June 8, 2021 19:34
-
-
Save Munken/7596dba69590f99ab387af967580d3c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: