remove remaining libwally code/header uses
What changed, and why it matters
This commit removes the last remaining uses of the libwally library from the BitBox02 firmware and replaces its constants (like SHA256_LEN and BIP39_WORDLIST_LEN) with local definitions. It is a code cleanup and dependency-removal change, not a security fix or vulnerability patch.
No security action required. Treat as ordinary refactoring; verify that the inlined constants match the previously imported libwally definitions and that no remaining libwally symbols or build dependencies exist.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch deletes libwally header includes, removes wallycore from simulator/unit-test CMake targets, drops wally_get/set_operations setup in common_main.c, and inlines or redefines constants such as SHA256_LEN (32), HMAC_SHA256_LEN (32), BIP39_WORDLIST_LEN (2048), EC_PUBLIC_KEY_LEN (33), BIP39_SEED_LEN_512 (64), and BIP32_INITIAL_HARDENED_CHILD (0x80000000). No functional behavior changes are visible in the diff.
Changed components
src/common_main.csrc/factorysetup.csrc/keystore.csrc/keystore.hsrc/rust/bitbox02-rust/src/workflow/mnemonic.rssrc/rust/bitbox02-sys/build.rssrc/rust/bitbox02-sys/wrapper.hsrc/rust/bitbox02/src/keystore.rssrc/u2f.csrc/u2f/u2f_app.ctest/simulator/CMakeLists.txttest/unit-test/CMakeLists.txttest/unit-test/test_keystore_antiklepto.cInspect captured patch +14 / −55
diff --git a/src/common_main.c b/src/common_main.c
index 002254c..3825de6 100644
--- a/src/common_main.c
+++ b/src/common_main.c
@@ -24,7 +24,6 @@
#include "screen.h"
#include "securechip/securechip.h"
#include "util.h"
-#include <wally_core.h>
extern void __attribute__((noreturn)) __stack_chk_fail(void);
void __attribute__((noreturn)) __stack_chk_fail(void)
@@ -52,22 +51,6 @@ static const securechip_interface_functions_t _securechip_interface_functions =
.random_32_bytes = random_32_bytes,
};
-static void _wally_patched_bzero(void* ptr, size_t len)
-{
- util_zero(ptr, len);
-}
-
-static bool _setup_wally(void)
-{
- static struct wally_operations _ops = {0};
- _ops.struct_size = sizeof(struct wally_operations);
- if (wally_get_operations(&_ops) != WALLY_OK) {
- return false;
- }
- _ops.bzero_fn = _wally_patched_bzero;
- return wally_set_operations(&_ops) == WALLY_OK;
-}
-
void common_main(void)
{
mpu_bitbox02_init();
@@ -76,10 +59,6 @@ void common_main(void)
AbortAutoenter("memory_setup failed");
}
- if (!_setup_wally()) {
- AbortAutoenter("_setup_wally failed");
- }
-
/* Enable/configure SmartEEPROM. */
smarteeprom_bb02_config();
diff --git a/src/factorysetup.c b/src/factorysetup.c
index 856ee52..24185c2 100644
--- a/src/factorysetup.c
+++ b/src/factorysetup.c
@@ -35,8 +35,6 @@
#include <secp256k1.h>
#include <ui/oled/oled.h>
-#include <wally_crypto.h>
-
#define BUFFER_SIZE_DOWN 1024
#define BUFFER_SIZE_UP 1024
@@ -387,7 +385,7 @@ static void _api_msg(const uint8_t* input, size_t in_len, uint8_t* output, size_
result = ERR_INVALID_INPUT;
break;
}
- uint8_t msg32[SHA256_LEN] = {0};
+ uint8_t msg32[32] = {0};
_attestation_sighash(attestation_device_pubkey, msg32);
bool matches_a_root_pubkey = false;
for (size_t pubkey_idx = 0; pubkey_idx < sizeof(_root_pubkey_bytes) / ROOT_PUBKEY_SIZE;
diff --git a/src/keystore.c b/src/keystore.c
index 350f052..b0e5c64 100644
--- a/src/keystore.c
+++ b/src/keystore.c
@@ -464,7 +464,7 @@ bool keystore_unlock_bip39(const char* mnemonic_passphrase)
return false;
}
- uint8_t bip39_seed[BIP39_SEED_LEN_512] = {0};
+ uint8_t bip39_seed[64] = {0};
UTIL_CLEANUP_64(bip39_seed);
rust_derive_bip39_seed(
rust_util_bytes(seed, seed_length),
diff --git a/src/keystore.h b/src/keystore.h
index b038897..cf7517d 100644
--- a/src/keystore.h
+++ b/src/keystore.h
@@ -22,9 +22,6 @@
#include <stdint.h>
#include <secp256k1.h>
-#include <wally_bip32.h>
-#include <wally_bip39.h> // for BIP39_WORDLIST_LEN
-#include <wally_crypto.h> // for EC_PUBLIC_KEY_LEN
#define KEYSTORE_MAX_SEED_LENGTH (32)
#define KEYSTORE_U2F_SEED_LENGTH SHA256_LEN
diff --git a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
index c815c67..614c894 100644
--- a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
+++ b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
@@ -29,6 +29,9 @@ use sha2::{Digest, Sha256};
const NUM_RANDOM_WORDS: u8 = 5;
+/// Number of words in the BIP-39 wordlist.
+const BIP39_WORDLIST_LEN: u16 = 2048;
+
fn as_str_vec(v: &[zeroize::Zeroizing<String>]) -> Vec<&str> {
v.iter().map(|s| s.as_str()).collect()
}
@@ -56,7 +59,7 @@ fn create_random_unique_words(word: &str, length: u8) -> (u8, Vec<zeroize::Zeroi
// A random word everywhere else.
// Loop until we get a unique word, we don't want repeated words in the list.
loop {
- let idx = rand16() % bitbox02::keystore::BIP39_WORDLIST_LEN;
+ let idx = rand16() % BIP39_WORDLIST_LEN;
if picked_indices.contains(&idx) {
continue;
};
@@ -319,7 +322,7 @@ pub async fn get(
.await;
// Provide all bip39 words to restrict the keyboard entry.
- let bip39_wordlist: Vec<u16> = (0..bitbox02::keystore::BIP39_WORDLIST_LEN).collect();
+ let bip39_wordlist: Vec<u16> = (0..BIP39_WORDLIST_LEN).collect();
let mut word_idx: usize = 0;
let mut entered_words = vec![zeroize::Zeroizing::new(String::new()); num_words];
@@ -430,7 +433,7 @@ mod tests {
fn bruteforce_lastword(mnemonic: &[&str]) -> Vec<zeroize::Zeroizing<String>> {
let mut result = Vec::new();
- for i in 0..bitbox02::keystore::BIP39_WORDLIST_LEN {
+ for i in 0..BIP39_WORDLIST_LEN {
let word = bitbox02::keystore::get_bip39_word(i).unwrap();
let mut m = mnemonic.to_vec();
m.push(&word);
diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs
index c2f4185..550f5f7 100644
--- a/src/rust/bitbox02-sys/build.rs
+++ b/src/rust/bitbox02-sys/build.rs
@@ -157,7 +157,6 @@ const ALLOWLIST_FNS: &[&str] = &[
"ui_screen_stack_pop_all",
"ui_screen_stack_push",
"util_format_datetime",
- "wally_free_string",
"communication_mode_ble_enabled",
];
@@ -213,8 +212,6 @@ pub fn main() -> Result<(), &'static str> {
"../../usb/class/hid",
"../../usb/class/hid/hww",
"../../usb/class/hid/u2f",
- // $WALLY_INCLUDES
- "../../../external/libwally-core/include",
// $SECP256k1_INCLUDES
"../../../external/libwally-core/src/secp256k1/include",
];
diff --git a/src/rust/bitbox02-sys/wrapper.h b/src/rust/bitbox02-sys/wrapper.h
index 4c41da3..ada97b3 100644
--- a/src/rust/bitbox02-sys/wrapper.h
+++ b/src/rust/bitbox02-sys/wrapper.h
@@ -50,9 +50,6 @@
#include <ui/screen_stack.h>
#include <ui/ugui/ugui.h>
#include <util.h>
-#include <wally_bip39.h>
-#include <wally_core.h>
-#include <wally_crypto.h>
#if defined(TESTING)
#include <fake_memory.h>
diff --git a/src/rust/bitbox02/src/keystore.rs b/src/rust/bitbox02/src/keystore.rs
index 861edd0..bf86ce6 100644
--- a/src/rust/bitbox02/src/keystore.rs
+++ b/src/rust/bitbox02/src/keystore.rs
@@ -24,8 +24,8 @@ use core::convert::TryInto;
use bitbox02_sys::keystore_error_t;
-pub const BIP39_WORDLIST_LEN: u16 = bitbox02_sys::BIP39_WORDLIST_LEN as u16;
-pub const EC_PUBLIC_KEY_LEN: usize = bitbox02_sys::EC_PUBLIC_KEY_LEN as _;
+/// Length of a compressed secp256k1 pubkey.
+const EC_PUBLIC_KEY_LEN: usize = 33;
pub const MAX_SEED_LENGTH: usize = bitbox02_sys::KEYSTORE_MAX_SEED_LENGTH as usize;
pub fn is_locked() -> bool {
diff --git a/src/u2f.c b/src/u2f.c
index b58bd51..140304f 100644
--- a/src/u2f.c
+++ b/src/u2f.c
@@ -34,7 +34,6 @@
#include <usb/usb_processing.h>
#include <utils_assert.h>
#include <version.h>
-#include <wally_crypto.h>
#ifndef TESTING
#include <hal_timer.h>
@@ -49,6 +48,8 @@ typedef struct {
#define APDU_LEN(A) (uint32_t)(((A).lc1 << 16) + ((A).lc2 << 8) + ((A).lc3))
#define U2F_KEYHANDLE_LEN (U2F_NONCE_LENGTH + SHA256_LEN)
+#define SHA256_LEN 32
+#define HMAC_SHA256_LEN 32
#if (U2F_EC_KEY_SIZE != SHA256_LEN) || (U2F_EC_KEY_SIZE != U2F_NONCE_LENGTH)
#error "Incorrect macro values for u2f"
diff --git a/src/u2f/u2f_app.c b/src/u2f/u2f_app.c
index 66be8f5..f424160 100644
--- a/src/u2f/u2f_app.c
+++ b/src/u2f/u2f_app.c
@@ -7,7 +7,6 @@
#include <stddef.h>
#include <stdio.h>
-#include <wally_bip39.h>
#define APPID_BOGUS_CHROMIUM "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
#define APPID_BOGUS_FIREFOX "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
diff --git a/test/simulator/CMakeLists.txt b/test/simulator/CMakeLists.txt
index 5dbfa69..1ef583a 100644
--- a/test/simulator/CMakeLists.txt
+++ b/test/simulator/CMakeLists.txt
@@ -84,7 +84,6 @@ target_include_directories(
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/asf4-drivers/hal/utils/include
${CMAKE_SOURCE_DIR}/external/fatfs/source
- $<TARGET_PROPERTY:wallycore,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES>
)
@@ -102,7 +101,6 @@ target_include_directories(
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/asf4-drivers/hal/utils/include
${CMAKE_SOURCE_DIR}/external/fatfs/source
- $<TARGET_PROPERTY:wallycore,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES>
)
@@ -117,7 +115,6 @@ target_include_directories(
add_dependencies(bitbox_objects-simulator
rust-cbindgen
- wallycore
secp256k1
fatfs
)
@@ -129,9 +126,6 @@ target_link_libraries(bitbox-simulator PRIVATE ${LIBBITBOX02_RUST} "-lm")
target_compile_definitions(bitbox_objects-simulator PUBLIC "PRODUCT_BITBOX_MULTI=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1")
target_compile_definitions(bitbox_objects-simulator PUBLIC TESTING _UNIT_TEST_)
-# Since wallycore is an external projects we need to specify the dependency
-add_dependencies(bitbox_objects-simulator libwally-core)
-
target_compile_definitions(bitbox-simulator PUBLIC "PRODUCT_BITBOX_MULTI=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1")
target_compile_definitions(bitbox-simulator PUBLIC TESTING _UNIT_TEST_)
@@ -139,7 +133,6 @@ target_link_libraries(bitbox-simulator
PUBLIC
secp256k1
PRIVATE
- wallycore
fatfs
)
diff --git a/test/unit-test/CMakeLists.txt b/test/unit-test/CMakeLists.txt
index 43d0424..adb5eab 100644
--- a/test/unit-test/CMakeLists.txt
+++ b/test/unit-test/CMakeLists.txt
@@ -103,7 +103,6 @@ target_include_directories(
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/asf4-drivers/hal/utils/include
${CMAKE_SOURCE_DIR}/external/fatfs/source
- $<TARGET_PROPERTY:wallycore,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES>
${CMOCKA_INCLUDE_DIRS}
@@ -123,7 +122,6 @@ target_include_directories(
${CMAKE_SOURCE_DIR}/external
${CMAKE_SOURCE_DIR}/external/asf4-drivers/hal/utils/include
${CMAKE_SOURCE_DIR}/external/fatfs/source
- $<TARGET_PROPERTY:wallycore,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES>
)
@@ -139,7 +137,6 @@ target_include_directories(
add_dependencies(bitbox_objects
rust-cbindgen
- wallycore
secp256k1
fatfs
)
@@ -151,9 +148,6 @@ target_link_libraries(bitbox PRIVATE ${LIBBITBOX02_RUST} "-lm")
target_compile_definitions(bitbox_objects PUBLIC "PRODUCT_BITBOX_MULTI=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1")
target_compile_definitions(bitbox_objects PUBLIC TESTING _UNIT_TEST_)
-# Since wallycore is an external projects we need to specify the dependency
-add_dependencies(bitbox_objects libwally-core)
-
target_compile_definitions(bitbox PUBLIC "PRODUCT_BITBOX_MULTI=1" "APP_BTC=1" "APP_LTC=1" "APP_U2F=1" "APP_ETH=1")
target_compile_definitions(bitbox PUBLIC TESTING _UNIT_TEST_)
@@ -162,7 +156,6 @@ target_link_libraries(bitbox
secp256k1
${CMOCKA_LDFLAGS}
PRIVATE
- wallycore
fatfs
)
diff --git a/test/unit-test/test_keystore_antiklepto.c b/test/unit-test/test_keystore_antiklepto.c
index 095783d..6f8552d 100644
--- a/test/unit-test/test_keystore_antiklepto.c
+++ b/test/unit-test/test_keystore_antiklepto.c
@@ -42,6 +42,8 @@ static uint8_t _mock_bip39_seed[64] = {
0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
};
+#define BIP32_INITIAL_HARDENED_CHILD 0x80000000
+
static void _test_keystore_antiklepto(void** state)
{
keystore_mock_unlocked(_mock_seed, sizeof(_mock_seed), _mock_bip39_seed);
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.