chore(python): fix `code_entry_code` encoding in trezorlib [no changelog]
What changed, and why it matters
This commit fixes a bug in the Trezor Python library where a numeric pairing code was converted to raw bytes instead of being formatted as a six-digit ASCII string. The change aligns the client-side code with the expected protocol format used by the Trezor device, likely preventing authentication failures or mismatches during the CPace pairing flow.
Treat as a correctness fix with possible security implications for device pairing integrity. Users relying on CPace-based pairing (e.g., Trezor Suite or third-party tools using trezorlib) should update the Python library. No immediate emergency response is indicated absent further vendor guidance.
Security signals we found
Incorrect encoding of a shared-secret input in a cryptographic handshake
Risk of client-device shared-secret mismatch in CPace pairing
Potential downgrade of effective code entropy if binary encoding collides with decimal interpretations
No changelog entry suggests routine fix rather than disclosed security issue
Evidence from the diff
In python/src/trezorlib/client.py, the CPace key generation step previously used code.to_bytes(6, ‘big’) to encode an integer pairing code. This produces a 6-byte binary representation whose value depends on the integer’s magnitude (e.g., 123456 becomes b’\x00\x01\xe2@’), not the zero-padded decimal string ‘123456’. The patch changes the encoding to f”{code:06}”.encode(“ascii”), which yields a fixed 6-character ASCII decimal string. This matches the typical CPace protocol expectation that the code is a human-readable, zero-padded PIN-like string. A mismatch could cause the shared secret derived on the client to differ from the one computed on the device, breaking the pairing/authentication handshake.
Changed components
python/src/trezorlib/client.pyTrezor Python client library CPace pairing code pathInspect captured patch +1 / −1
diff --git a/python/src/trezorlib/client.py b/python/src/trezorlib/client.py
index feaad5c52..85158da95 100644
--- a/python/src/trezorlib/client.py
+++ b/python/src/trezorlib/client.py
@@ -167,7 +167,7 @@ class TrezorClient:
cpace.random_bytes = os.urandom
assert cpace_trezor_msg.cpace_trezor_public_key is not None
cpace.generate_keys_and_secret(
- code.to_bytes(6, "big"), cpace_trezor_msg.cpace_trezor_public_key
+ f"{code:06}".encode("ascii"), cpace_trezor_msg.cpace_trezor_public_key
)
sha_ctx = sha256(cpace.shared_secret)
tag = sha_ctx.digest()
Why this scored 46/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.