diff options
Diffstat (limited to 'executables')
-rw-r--r-- | executables/hex_checksum-v2.py | 23 | ||||
-rw-r--r-- | executables/hex_checksum.py | 40 |
2 files changed, 63 insertions, 0 deletions
diff --git a/executables/hex_checksum-v2.py b/executables/hex_checksum-v2.py new file mode 100644 index 0000000..71409f1 --- /dev/null +++ b/executables/hex_checksum-v2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +import sys + +def compute_psa_checksum(data: bytes) -> bytes: + var_e = 0x0000 # Correct seed from firmware logic + for i in range(len(data)): + var_e ^= (data[i] + i) & 0xFFFF + return var_e.to_bytes(2, 'big') # 2-byte checksum, big-endian + +def add_checksum(hexstring: str) -> str: + try: + data = bytes.fromhex(hexstring.strip()) + except ValueError: + return "Invalid hex input!" + checksum = compute_psa_checksum(data) + return (data + checksum).hex() + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: ./hex_checksum.py <hexstring_without_checksum>") + sys.exit(1) + result = add_checksum(sys.argv[1]) + print(result) diff --git a/executables/hex_checksum.py b/executables/hex_checksum.py new file mode 100644 index 0000000..72bb7b6 --- /dev/null +++ b/executables/hex_checksum.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +import sys + +""" +PSA Checksum Calculator + +This script calculates the 2-byte checksum used in PSA multicast protocol messages. +The checksum algorithm is based on reverse-engineered firmware behavior. + +How it works: +------------- +- Initialize a 16-bit value `var_e` to 0x0000 +- For each byte in the payload: + - Add the byte's index to its value (i.e., byte + index) + - XOR the result into `var_e` +- The result is a 2-byte checksum (big endian: high byte first, low byte second) + +This checksum is appended to the payload to form a valid message. +""" + +def compute_psa_checksum(data: bytes) -> bytes: + var_e = 0x0000 + for i in range(len(data)): + var_e ^= (data[i] + i) & 0xFFFF + return var_e.to_bytes(2, 'big') + +def add_checksum(hexstring: str) -> str: + try: + data = bytes.fromhex(hexstring.strip()) + except ValueError: + return "Invalid hex input!" + checksum = compute_psa_checksum(data) + return (data + checksum).hex() + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: ./hex_checksum.py <hexstring_without_checksum>") + sys.exit(1) + result = add_checksum(sys.argv[1]) + print(result) |