fix(crypto): copy only the digest length in `blake2s_Final()`
What changed, and why it matters
This commit fixes a memory safety bug in the Trezor firmware's implementation of the BLAKE2s hashing function. The finalization routine was copying as many bytes as the caller's output buffer could hold, rather than the actual hash digest length. If a caller passed a buffer larger than 32 bytes, the function would read past the end of its internal 32-byte buffer, potentially leaking nearby stack data into the output. The fix makes BLAKE2s behave consistently with the already-correct BLAKE2b implementation.
Apply the patch and run the updated unit tests. Review all call sites of `blake2s_Final()` in the firmware and host tooling to confirm no code relies on the previous over-copy behavior. Consider auditing other inherited upstream crypto code for similar `memcpy` length mismatches.
Security signals we found
Out-of-bounds stack read in cryptographic finalization routine
Potential information disclosure of adjacent stack memory
Inconsistency between blake2b_Final and blake2s_Final copy behavior
Regression test added to prevent reintroduction
Evidence from the diff
In crypto/blake2s.c, blake2s_Final() previously executed memcpy(out, buffer, outlen), where outlen is the size of the caller-supplied buffer and buffer is a 32-byte stack-local array. When outlen > 32, this produced an out-of-bounds stack read. The patch changes the copy length to S->outlen, the configured digest length, matching blake2b_Final(). Tests were added to verify that both BLAKE2b and BLAKE2s finalization write only the digest length and leave the remainder of a larger output buffer untouched.
Changed components
crypto/blake2s.ccrypto/tests/test_check.cInspect captured patch +27 / −1
### crypto/blake2s.c
@@ -287,7 +287,7 @@ int blake2s_Final(blake2s_state *S, void *out, size_t outlen) {
for (i = 0; i < 8; ++i) /* Output full hash to temp buffer */
store32(buffer + sizeof(S->h[i]) * i, S->h[i]);
- memcpy(out, buffer, outlen);
+ memcpy(out, buffer, S->outlen);
memzero(buffer, sizeof(buffer));
return 0;
}
### crypto/tests/test_check.c
@@ -6408,6 +6408,19 @@ START_TEST(test_blake2b) {
0);
ck_assert_int_eq(blake2b_Final(&ctx, digest, sizeof(digest)), 0);
ck_assert_mem_eq(digest, fromhex(tests[i].hash), BLAKE2B_DIGEST_LENGTH);
+
+ // Test that `blake2b_Final` writes only the digest length
+ uint8_t out[2 * BLAKE2B_DIGEST_LENGTH];
+ memset(out, 0xcc, sizeof(out));
+ ck_assert_int_eq(
+ blake2b_InitKey(&ctx, BLAKE2B_DIGEST_LENGTH, key, sizeof(key)), 0);
+ ck_assert_int_eq(blake2b_Update(&ctx, fromhex(tests[i].msg), msg_len), 0);
+ ck_assert_int_eq(blake2b_Final(&ctx, out, BLAKE2B_DIGEST_LENGTH - 1), -1);
+ ck_assert_int_eq(blake2b_Final(&ctx, out, sizeof(out)), 0);
+ ck_assert_mem_eq(out, fromhex(tests[i].hash), BLAKE2B_DIGEST_LENGTH);
+ for (size_t j = BLAKE2B_DIGEST_LENGTH; j < sizeof(out); j++) {
+ ck_assert_uint_eq(out[j], 0xcc);
+ }
}
}
END_TEST
@@ -6521,6 +6534,19 @@ START_TEST(test_blake2s) {
0);
ck_assert_int_eq(blake2s_Final(&ctx, digest, sizeof(digest)), 0);
ck_assert_mem_eq(digest, fromhex(tests[i].hash), BLAKE2S_DIGEST_LENGTH);
+
+ // Test that `blake2s_Final` writes only the digest length
+ uint8_t out[2 * BLAKE2S_DIGEST_LENGTH];
+ memset(out, 0xcc, sizeof(out));
+ ck_assert_int_eq(
+ blake2s_InitKey(&ctx, BLAKE2S_DIGEST_LENGTH, key, sizeof(key)), 0);
+ ck_assert_int_eq(blake2s_Update(&ctx, fromhex(tests[i].msg), msg_len), 0);
+ ck_assert_int_eq(blake2s_Final(&ctx, out, BLAKE2S_DIGEST_LENGTH - 1), -1);
+ ck_assert_int_eq(blake2s_Final(&ctx, out, sizeof(out)), 0);
+ ck_assert_mem_eq(out, fromhex(tests[i].hash), BLAKE2S_DIGEST_LENGTH);
+ for (size_t j = BLAKE2S_DIGEST_LENGTH; j < sizeof(out); j++) {
+ ck_assert_uint_eq(out[j], 0xcc);
+ }
}
}
END_TESTWhy this scored 60/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.