Merge remote-tracking branch 'agent/benma-agent/stage0-reject-zero-signatures'
What changed, and why it matters
This update hardens the BitBox02 hardware wallet's bootloader so it refuses to accept a firmware update signed with an all-zero signature. Previously, the cryptographic verification routine could treat a zero-filled signature as valid under some conditions, which might let a malicious or malformed firmware image pass the signature check. The fix explicitly scans every byte of each signature to ensure it is not all zeros, and it re-reads the signature bytes fresh during each verification pass to guard against subtle compiler or hardware-level tampering.
Treat this as a security-hardening fix and include it in the next firmware release. Review whether other signature verification paths in the bootloader or main firmware apply the same all-zero-signature check. Run the new unit tests in CI and consider a firmware security advisory if prior releases could accept zero signatures.
Security signals we found
Rejects all-zero ECDSA signatures that may verify as valid
Adds volatile signature read to mitigate LTO-based read elimination
ORs all signature bytes without early exit to ensure full-byte inspection
Adds unit tests covering zero signatures, byte coverage, and inter-pass tampering
Evidence from the diff
In src/bootloader/stage0/stage1_sigcheck.c, _check_signatures() now reads the raw ECDSA verification status into status first, then independently computes whether the signature bytes are non-zero by OR-ing all bytes of the signature into signature_bits. A signature is accepted only if pukcc_ecdsa_verify() returns zero (success) AND the signature is non-zero. The signature pointer is declared volatile to prevent link-time optimization (LTO) from caching reads across the two verification passes (forward and reverse). Unit tests were added to verify that all-zero signatures are rejected, that every byte of the signature is checked, and that a signature modified between the forward and reverse passes is rejected.
Changed components
BitBox02 firmware bootloader stage0 signature verificationsrc/bootloader/stage0/stage1_sigcheck.cPUKCC ECDSA verify wrapper usageInspect captured patch +81 / −2
### src/bootloader/stage0/stage1_sigcheck.c
@@ -164,12 +164,23 @@ static sigcheck_masks_t _check_signatures(
const uint8_t key_idx = reverse ? (uint8_t)(BB02_STAGE1_ROOT_KEY_COUNT - 1U - i) : i;
const uint8_t key_bit = (uint8_t)(1U << key_idx);
const size_t signature_offset = (size_t)key_idx * BB02_STAGE1_SIGNATURE_LEN;
- const uint8_t valid = _is_zero_u8(pukcc_ecdsa_verify(
+ const uint8_t status = pukcc_ecdsa_verify(
pubkeys[key_idx],
&signatures[signature_offset],
digest,
BB02_STAGE1_SIGNED_DIGEST_LEN,
- curve_p256));
+ curve_p256);
+
+ // Force fresh reads in each verification pass under LTO.
+ const volatile uint8_t* signature = &signatures[signature_offset];
+ uint8_t signature_bits = 0;
+ // OR all bytes to detect an all-zero signature without early exit.
+ for (size_t j = 0; j < BB02_STAGE1_SIGNATURE_LEN; j++) {
+ signature_bits |= signature[j];
+ }
+ // Reject all-zero signatures even if PUKCC verification succeeds.
+ const uint8_t nonzero = (uint8_t)(_is_zero_u8(signature_bits) ^ 1U);
+ const uint8_t valid = (uint8_t)(_is_zero_u8(status) & nonzero);
const uint8_t invalid = (uint8_t)(valid ^ 1U);
masks.valid_mask |= (uint8_t)((uint8_t)(0U - valid) & key_bit);
masks.invalid_mask |= (uint8_t)((uint8_t)(0U - invalid) & key_bit);
### test/unit-test/test_stage0_sigcheck.c
@@ -37,6 +37,7 @@ static uint8_t _sha_update_count;
static uint8_t _sha_finish_count;
static bool _accept_any_digest;
static bool _assert_message_not_header_prefix;
+static bool _zero_first_signature_in_reverse_pass;
static uint8_t _forward_valid_mask;
static uint8_t _reverse_valid_mask;
static uint8_t _call_count;
@@ -192,6 +193,7 @@ static void _reset_image(void)
_header.stage1_marketing_version_len = 3;
memcpy(_header.stage1_marketing_version, "dev", 3);
_fill_body();
+ _fill_signature_fixture();
}
static void _reset_script(uint8_t forward_valid_mask, uint8_t reverse_valid_mask)
@@ -208,6 +210,7 @@ static void _reset_script(uint8_t forward_valid_mask, uint8_t reverse_valid_mask
_sha_finish_count = 0;
_accept_any_digest = false;
_assert_message_not_header_prefix = false;
+ _zero_first_signature_in_reverse_pass = false;
_forward_valid_mask = forward_valid_mask;
_reverse_valid_mask = reverse_valid_mask;
_call_count = 0;
@@ -331,6 +334,10 @@ uint8_t __wrap_pukcc_ecdsa_verify(
(void)curve;
assert_true(_call_count < sizeof(_call_order));
+ if (_zero_first_signature_in_reverse_pass && _call_count == BB02_STAGE1_ROOT_KEY_COUNT) {
+ memset(_signatures(), 0, BB02_STAGE1_SIGNATURE_LEN);
+ }
+
const uint8_t key_idx = _key_index(public_key);
assert_ptr_equal(signature, &_signatures()[(uint32_t)key_idx * BB02_STAGE1_SIGNATURE_LEN]);
assert_int_equal(message_len, BB02_STAGE1_SIGNED_DIGEST_LEN);
@@ -538,6 +545,63 @@ static void test_stage1_sigcheck_image_ok_rejects_inconsistent_passes(void** sta
_assert_result(0x07, 0x03, secfalse_u32);
}
+static void test_stage1_sigcheck_image_ok_requires_nonzero_verified_signatures(void** state)
+{
+ (void)state;
+ for (uint8_t nonzero_mask = 0; nonzero_mask < 8; nonzero_mask++) {
+ for (uint8_t verified_mask = 0; verified_mask < 8; verified_mask++) {
+ _reset_image();
+ uint8_t valid_count = 0;
+ for (uint8_t i = 0; i < BB02_STAGE1_ROOT_KEY_COUNT; i++) {
+ const uint8_t key_bit = (uint8_t)(1u << i);
+ if ((nonzero_mask & key_bit) == 0) {
+ memset(
+ &_signatures()[i * BB02_STAGE1_SIGNATURE_LEN],
+ 0,
+ BB02_STAGE1_SIGNATURE_LEN);
+ } else if ((verified_mask & key_bit) != 0) {
+ valid_count++;
+ }
+ }
+ _reset_script(verified_mask, verified_mask);
+
+ const secbool_u32 expected =
+ valid_count >= BB02_STAGE1_SIGNATURE_THRESHOLD ? sectrue_u32 : secfalse_u32;
+ assert_int_equal(stage1_sigcheck_image_ok(&_header, _pubkeys), expected);
+ _assert_sha_calls();
+ _assert_call_order();
+ }
+ }
+}
+
+static void test_stage1_sigcheck_image_ok_checks_all_signature_bytes(void** state)
+{
+ (void)state;
+ for (size_t i = 0; i < BB02_STAGE1_SIGNATURE_LEN; i++) {
+ _reset_image();
+ memset(_signatures(), 0, BB02_STAGE1_HEADER_SIGNATURES_LEN);
+ _signatures()[i] = 0x80;
+ _signatures()[BB02_STAGE1_SIGNATURE_LEN] = 1;
+ _reset_script(0x07, 0x07);
+
+ assert_int_equal(stage1_sigcheck_image_ok(&_header, _pubkeys), sectrue_u32);
+ _assert_sha_calls();
+ _assert_call_order();
+ }
+}
+
+static void test_stage1_sigcheck_image_ok_rejects_changed_signature_between_passes(void** state)
+{
+ (void)state;
+ _reset_image();
+ _reset_script(0x07, 0x07);
+ _zero_first_signature_in_reverse_pass = true;
+
+ assert_int_equal(stage1_sigcheck_image_ok(&_header, _pubkeys), secfalse_u32);
+ _assert_sha_calls();
+ _assert_call_order();
+}
+
static void test_stage1_sigcheck_image_ok_rejects_changed_body(void** state)
{
(void)state;
@@ -624,6 +688,7 @@ static void test_stage1_sigcheck_image_ok_accepts_larger_header_len(void** state
_header.header_len = BB02_STAGE1_HEADER_LEN + EXTRA_HEADER_LEN;
_header.image_len = _header.header_len + BODY_LEN;
_fill_body();
+ _fill_signature_fixture();
_reset_script(0x07, 0x07);
assert_int_equal(stage1_sigcheck_image_ok(&_header, _pubkeys), sectrue_u32);
@@ -678,6 +743,9 @@ int main(void)
cmocka_unit_test(test_stage1_sigcheck_image_ok_accepts_two_signatures),
cmocka_unit_test(test_stage1_sigcheck_image_ok_accepts_three_signatures),
cmocka_unit_test(test_stage1_sigcheck_image_ok_rejects_inconsistent_passes),
+ cmocka_unit_test(test_stage1_sigcheck_image_ok_requires_nonzero_verified_signatures),
+ cmocka_unit_test(test_stage1_sigcheck_image_ok_checks_all_signature_bytes),
+ cmocka_unit_test(test_stage1_sigcheck_image_ok_rejects_changed_signature_between_passes),
cmocka_unit_test(test_stage1_sigcheck_image_ok_rejects_changed_body),
cmocka_unit_test(test_stage1_sigcheck_image_ok_rejects_changed_metadata),
cmocka_unit_test(test_stage1_sigcheck_image_ok_rejects_changed_header_version),Why this scored 77/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.