Remove old Nano S hack to save 32 bytes of stack
What changed, and why it matters
This commit is a straightforward code cleanup in Ledger's Bitcoin app. It removes an old memory-saving trick used for the discontinued Nano S device and replaces it with a cleaner, more readable approach that uses a dedicated local variable to hold the final hash. There is no security-relevant change: the same hash is computed and compared in the same way, just stored in a different place.
No security action required. This is a benign refactor; standard code review is sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
In get_merkle_preimage.c, the previous code called crypto_hash_digest() with the address of hash_context.acc (an internal field of the cx_sha256_t context) as the output buffer, avoiding a separate 32-byte stack variable. The patch introduces a local final_hash[32] buffer, writes the digest there, and compares that buffer against the expected hash. The functional behavior is identical; only the storage location of the computed digest changes. The commit message explicitly frames this as a clarity/optimization cleanup, not a security fix.
Changed components
src/handler/lib/get_merkle_preimage.cInspect captured patch +3 / −4
diff --git a/src/handler/lib/get_merkle_preimage.c b/src/handler/lib/get_merkle_preimage.c
index 8a7e149..fc1766a 100644
--- a/src/handler/lib/get_merkle_preimage.c
+++ b/src/handler/lib/get_merkle_preimage.c
@@ -110,11 +110,10 @@ int call_get_merkle_preimage(dispatcher_context_t *dispatcher_context,
bytes_remaining -= n_bytes;
}
- // hack: we pass the address of the final accumulator inside cx_sha256_t, so we don't need
- // an additional variable in the stack to store the final hash.
- crypto_hash_digest(&hash_context.header, (uint8_t *) &hash_context.acc, 32);
+ uint8_t final_hash[32];
+ crypto_hash_digest(&hash_context.header, final_hash, 32);
- if (memcmp(hash_context.acc, hash, 32) != 0) {
+ if (memcmp(final_hash, hash, 32) != 0) {
PRINTF("Hash mismatch.\n");
return -10;
}
Why this scored 15/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.