factorysetup: remove use of wally_get_secp_context()
What changed, and why it matters
This commit changes how the BitBox02 factory-setup code accesses the secp256k1 cryptographic library. Previously it borrowed a shared context via a libwally helper; now it creates and destroys its own context with no special flags. The change is a code-quality and dependency-cleanup move. There is no direct evidence in the commit that it fixes an exploitable vulnerability, but it removes a potential source of shared-state or context-lifetime issues during device programming.
Treat as routine maintenance. Review whether wally_get_secp_context() is still used elsewhere and whether any remaining callers assume a context with precomputed tables. No urgent action required based on this commit alone.
Security signals we found
Use-after-free / double-free risk reduced by explicit context creation and scoped cleanup
Shared mutable context dependency removed
No input validation changes or bounds-check modifications
No mention of vulnerability, CVE, bug bounty, or security advisory in commit metadata
Evidence from the diff
In src/factorysetup.c, the OP_SET_CERTIFICATE handler is updated to call secp256k1_context_create(SECP256K1_CONTEXT_NONE) and clean it up with a GCC cleanup destructor instead of using wally_get_secp_context(). The commit also moves a helper _free() and adds a matching _destroy() helper. The new context is then reused for both signature parsing and public-key parsing, replacing repeated calls to wally_get_secp_context(). The commit message cites upstream secp256k1 deprecation of context flags other than SECP256K1_CONTEXT_NONE. No security bug, CVE, or advisory is mentioned in the commit or supplied references.
Changed components
src/factorysetup.cOP_SET_CERTIFICATE factory-setup commandsecp256k1 ECDSA signature verification during attestation certificate installationInspect captured patch +21 / −12
diff --git a/src/factorysetup.c b/src/factorysetup.c
index 204bf00..856ee52 100644
--- a/src/factorysetup.c
+++ b/src/factorysetup.c
@@ -301,6 +301,20 @@ static bool _rtt_receive(uint8_t* msg_out, size_t* len_out)
}
}
+static void _free(uint8_t** buf)
+{
+ free(*buf);
+ *buf = NULL;
+}
+
+static void _destroy(secp256k1_context** ctx)
+{
+ if (*ctx) {
+ secp256k1_context_destroy(*ctx);
+ *ctx = NULL;
+ }
+}
+
/**
* Computes the hash which is signed by the root key.
* @param[in] attestation_device_pubkey 64 bytes P-256 pubkey.
@@ -352,7 +366,7 @@ static void _api_msg(const uint8_t* input, size_t in_len, uint8_t* output, size_
out_len = 2 + 64;
break;
}
- case OP_SET_CERTIFICATE:
+ case OP_SET_CERTIFICATE: {
if (in_len != 1 + 64 + 64 + 32) {
result = ERR_INVALID_INPUT;
break;
@@ -364,7 +378,10 @@ static void _api_msg(const uint8_t* input, size_t in_len, uint8_t* output, size_
const uint8_t* root_pubkey_identifier = input + 1 + pubkey_size + certificate_size;
// Verify sig
- secp256k1_context* ctx = wally_get_secp_context();
+
+ secp256k1_context* __attribute__((__cleanup__(_destroy))) ctx =
+ secp256k1_context_create(SECP256K1_CONTEXT_NONE);
+
secp256k1_ecdsa_signature sig = {0};
if (!secp256k1_ecdsa_signature_parse_compact(ctx, &sig, certificate)) {
result = ERR_INVALID_INPUT;
@@ -377,10 +394,7 @@ static void _api_msg(const uint8_t* input, size_t in_len, uint8_t* output, size_
pubkey_idx++) {
secp256k1_pubkey pubkey;
if (!secp256k1_ec_pubkey_parse(
- wally_get_secp_context(),
- &pubkey,
- _root_pubkey_bytes[pubkey_idx],
- ROOT_PUBKEY_SIZE)) {
+ ctx, &pubkey, _root_pubkey_bytes[pubkey_idx], ROOT_PUBKEY_SIZE)) {
Abort("Invalid root pubkey");
}
@@ -404,6 +418,7 @@ static void _api_msg(const uint8_t* input, size_t in_len, uint8_t* output, size_
}
screen_print_debug("DONE", 0);
break;
+ }
case OP_SC_ROLLKEYS:
if (!securechip_reset_keys()) {
screen_print_debug("resetting securechip keys: failed", 0);
@@ -438,12 +453,6 @@ static void _api_msg(const uint8_t* input, size_t in_len, uint8_t* output, size_
*output_len = out_len;
}
-static void _free(uint8_t** buf)
-{
- free(*buf);
- *buf = NULL;
-}
-
static uint8_t _ble_firmware_checksum(const uint8_t* buf, size_t buf_len)
{
uint8_t res = 0;
Why this scored 19/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.