java-topology/defects/trezor-0002/test/trezor-0002-test.py

65 lines
2.4 KiB
Python

"""
Unit test for trezor-0002: THP crypto module logs encryption keys verbatim (CWE-312).
The original code logs AES-GCM encryption/decryption keys, handshake keys (self.k),
and session keys (key_receive, key_send) via log.debug() in debug builds.
This test verifies that the patched version does not include key material in log output.
"""
import re
# Simulate the log output lines from the original code
ORIGINAL_LOG_LINES = [
'enc (key: a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4, nonce: 0)',
'dec (key: a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4, nonce: 1)',
'th1 - enc (key: a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4, nonce: 0, handshake_hash deadbeef)',
'th2 - dec (key: a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4, nonce: 1)',
'th2 - dec (key: a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4, nonce: 0)',
'(key_receive: aabbccdd, key_send: eeff0011)',
]
# Simulated log output from patched code
PATCHED_LOG_LINES = [
'enc (key: <redacted>, nonce: 0)',
'dec (key: <redacted>, nonce: 1)',
# th1/th2/key_receive/key_send lines are removed entirely
]
# Pattern to detect hex key material (at least 16 hex chars that look like a key)
KEY_PATTERN = re.compile(r'(?:key[_\s:]*|hash[_\s:]*)([0-9a-fA-F]{16,})')
def test_original_leaks_keys():
"""Original log lines contain key material."""
leaked = 0
for line in ORIGINAL_LOG_LINES:
if KEY_PATTERN.search(line):
leaked += 1
assert leaked >= 4, f"Expected at least 4 key leaks, found {leaked}"
print(f"PASS: original leaks {leaked} key values")
def test_patched_no_key_leaks():
"""Patched log lines must not contain key material."""
for line in PATCHED_LOG_LINES:
match = KEY_PATTERN.search(line)
assert match is None, f"Key material found in patched output: {line}"
print("PASS: patched output contains no key material")
def test_handshake_key_logs_removed():
"""The th1/th2 handshake key logs and session key logs are completely removed."""
for line in PATCHED_LOG_LINES:
assert "key_receive" not in line, f"key_receive found: {line}"
assert "key_send" not in line, f"key_send found: {line}"
assert "handshake_hash" not in line, f"handshake_hash found: {line}"
print("PASS: handshake and session key logs removed")
if __name__ == "__main__":
test_original_leaks_keys()
test_patched_no_key_leaks()
test_handshake_key_logs_removed()
print("\nAll tests PASS")