Skip to content

Commit a449d21

Browse files
committed
Merge branch 'rm-wally'
2 parents d6d0aa2 + aa3703c commit a449d21

File tree

19 files changed

+37
-130
lines changed

19 files changed

+37
-130
lines changed

.gitmodules

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
[submodule "external/cryptoauthlib"]
22
path = external/cryptoauthlib
33
url = https://github.com/BitBoxSwiss/cryptoauthlib.git
4-
[submodule "external/libwally-core"]
5-
path = external/libwally-core
6-
url = https://github.com/BitBoxSwiss/libwally-core.git
74
[submodule "tools/ttf2ugui"]
85
path = tools/ttf2ugui
96
url = https://github.com/BitBoxSwiss/ttf2ugui
@@ -13,3 +10,6 @@
1310
[submodule "external/embedded-swd"]
1411
path = external/embedded-swd
1512
url = https://github.com/BitBoxSwiss/embedded-swd
13+
[submodule "external/secp256k1-zkp"]
14+
path = external/secp256k1-zkp
15+
url = https://github.com/BitBoxSwiss/secp256k1-zkp.git

external/CMakeLists.txt

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
include(ExternalProject)
22

3+
# Tell `add_library()` to default to STATIC.
4+
# Needed because secp256k1-zkp/CMakeLists.txt sets it to ON.
5+
set(BUILD_SHARED_LIBS OFF)
6+
37
if(CMAKE_CROSSCOMPILING)
48
set(CONFIGURE_FLAGS
59
--host=${CMAKE_SYSTEM_PROCESSOR}-none-eabi
@@ -14,71 +18,19 @@ string(REPLACE "-mfloat-abi=softfp" "" MODIFIED_C_FLAGS_TMP ${CMAKE_C_FLAGS})
1418
string(REPLACE "-mfpu=fpv4-sp-d16" "" MODIFIED_C_FLAGS ${MODIFIED_C_FLAGS_TMP})
1519

1620
#----------------------
17-
# wally-core
18-
19-
# configure flags for secp256k1 bundled in libwally core, to reduce memory consumption
20-
set(LIBWALLY_SECP256k1_FLAGS --with-ecmult-window=2 --with-ecmult-gen-precision=2 --enable-ecmult-static-precomputation --enable-module-schnorrsig --enable-module-ecdsa-adaptor)
21-
set(LIBWALLY_CONFIGURE_FLAGS --enable-static --disable-shared --disable-tests ${LIBWALLY_SECP256k1_FLAGS})
22-
if(SANITIZE_ADDRESS)
23-
set(LIBWALLY_CFLAGS "-fsanitize=address")
24-
endif()
25-
if(SANITIZE_UNDEFINED)
26-
set(LIBWALLY_CFLAGS "${LIBWALLY_CFLAGS} -fsanitize=undefined")
27-
endif()
28-
# _DEFAULT_SOURCE enables the BSD explicit_bzero function referenced by libwally.
29-
set(LIBWALLY_CFLAGS "\
30-
${LIBWALLY_CFLAGS} \
31-
${MODIFIED_C_FLAGS} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}} \
32-
-D_DEFAULT_SOURCE \
33-
-fno-strict-aliasing \
34-
")
35-
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
36-
string(APPEND LIBWALLY_CFLAGS " -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
37-
endif()
3821

39-
# Hide some warnings
40-
set(LIBWALLY_CFLAGS "${LIBWALLY_CFLAGS} -Wno-cast-qual -Wno-cast-align \
41-
-Wno-missing-prototypes -Wno-redundant-decls \
42-
-Wno-switch-default -Wno-missing-declarations \
43-
-Wno-array-bounds -Wno-unused-label -Wno-sign-compare -Wno-type-limits \
44-
")
45-
if(CMAKE_CROSSCOMPILING)
46-
set(LIBWALLY_LDFLAGS --specs=nosys.specs)
47-
endif()
48-
set(LIBWALLY_LDFLAGS "${LIBWALLY_LDFLAGS} ${CMAKE_C_LINK_FLAGS}")
22+
## secp256k1-zkp
4923

50-
ExternalProject_Add(libwally-core
51-
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libwally-core
52-
STEP_TARGETS build-libwally
53-
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core
54-
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core tools/autogen.sh
55-
COMMAND ${CMAKE_COMMAND} -E env
56-
"CFLAGS=${LIBWALLY_CFLAGS}"
57-
"LDFLAGS=${LIBWALLY_LDFLAGS}"
58-
${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/configure
59-
${CONFIGURE_FLAGS}
60-
${LIBWALLY_CONFIGURE_FLAGS}
61-
INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}
62-
COMMAND ${CMAKE_COMMAND} -E copy
63-
${CMAKE_CURRENT_BINARY_DIR}/libwally-core/src/libwally-core-build/src/.libs/libwallycore.a
64-
${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libwallycore.a
65-
COMMAND ${CMAKE_COMMAND} -E copy
66-
${CMAKE_CURRENT_BINARY_DIR}/libwally-core/src/libwally-core-build/src/secp256k1/.libs/libsecp256k1.a
67-
${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libsecp256k1.a
24+
# Override
25+
set(SECP256K1_ENABLE_MODULE_RECOVERY ON) # needed only in Rust unit tests.
26+
set(SECP256K1_ECMULT_WINDOW_SIZE 2 CACHE STRING "Window size for ecmult precomputation for verification, specified as integer in range [2..24]. \"AUTO\" is a reasonable setting for desktop machines (currently 15). [default=AUTO]" FORCE)
27+
set(SECP256K1_ECMULT_GEN_PREC_BITS 2 CACHE STRING "Precision bits to tune the precomputed table size for signing, specified as integer 2, 4 or 8. \"AUTO\" is a reasonable setting for desktop machines (currently 4). [default=AUTO]" FORCE)
28+
# Suppress all warnings in this directory, we don't have control over them.
29+
set_directory_properties(PROPERTIES
30+
COMPILE_OPTIONS "-w"
31+
DIRECTORY secp256k1-zkp
6832
)
69-
70-
add_library(wallycore STATIC IMPORTED GLOBAL)
71-
set_property(TARGET wallycore
72-
PROPERTY IMPORTED_LOCATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libwallycore.a)
73-
set_target_properties(wallycore PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/include)
74-
set_target_properties(wallycore PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/include)
75-
76-
add_library(secp256k1 STATIC IMPORTED GLOBAL)
77-
set_property(TARGET secp256k1
78-
PROPERTY IMPORTED_LOCATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libsecp256k1.a)
79-
set_target_properties(secp256k1 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/src/secp256k1/include)
80-
set_target_properties(secp256k1 PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/src/secp256k1/include)
81-
33+
add_subdirectory(secp256k1-zkp)
8234

8335
if(CMAKE_CROSSCOMPILING)
8436
# Cortex Microcontroller Software Interface Standard

external/libwally-core

Lines changed: 0 additions & 1 deletion
This file was deleted.

external/secp256k1-zkp

Submodule secp256k1-zkp added at edc3c3e

src/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ else()
235235
set(RUSTFLAGS "--remap-path-prefix=${CMAKE_CURRENT_SOURCE_DIR}/rust=src --remap-path-prefix=$ENV{HOME}=")
236236
endif()
237237

238-
# Use libsecp256k1 that we link ourselves (included via external/libwally-core).
238+
# Use libsecp256k1 that we link ourselves.
239239
# See https://github.com/rust-bitcoin/rust-secp256k1/tree/7c8270a8506e31731e540fab7ee1abde1f48314e/secp256k1-sys#linking-to-external-symbols
240240
set(RUSTFLAGS "${RUSTFLAGS} --cfg=rust_secp_no_symbol_renaming")
241241

@@ -277,7 +277,7 @@ add_custom_target(rust-cbindgen
277277

278278
# Test rust crates that contain business logic. Avoid testing crates that depend on hardware.
279279
if(NOT CMAKE_CROSSCOMPILING)
280-
set(RUSTFLAGS_TESTS ${RUSTFLAGS} -L${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} -lbitbox_merged -lwallycore -lsecp256k1 -lfatfs -lhardware-fakes)
280+
set(RUSTFLAGS_TESTS ${RUSTFLAGS} -L${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} -lbitbox_merged -lsecp256k1 -lfatfs -lhardware-fakes)
281281

282282
# Since we build with all features we need to use a separate build directory.
283283
# Otherwise we invalidate the result from the normal compilation that uses a
@@ -293,7 +293,7 @@ if(NOT CMAKE_CROSSCOMPILING)
293293
${CARGO} test $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:-v> --all-features --target-dir ${RUST_BINARY_DIR}/all-features ${RUST_CARGO_FLAGS} -- --nocapture --test-threads 1
294294
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rust/
295295
)
296-
add_dependencies(rust-test generate-protobufs bitbox_merged libwally-core fatfs)
296+
add_dependencies(rust-test generate-protobufs bitbox_merged fatfs)
297297

298298
add_custom_target(rust-clippy
299299
COMMAND
@@ -520,8 +520,7 @@ if(CMAKE_CROSSCOMPILING)
520520
target_link_libraries(${elf} PRIVATE "-Wl,-Map=\"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${firmware}.map\" -T\"${CMAKE_SOURCE_DIR}/firmware.ld\"")
521521
target_link_libraries(${elf} PRIVATE -Wl,--defsym=STACK_SIZE=${STACK_SIZE} -Wl,-defsym=HEAP_SIZE=${HEAP_SIZE})
522522

523-
add_dependencies(${elf} libwally-core)
524-
target_link_libraries(${elf} PRIVATE wallycore secp256k1)
523+
target_link_libraries(${elf} PRIVATE secp256k1)
525524
target_link_libraries(${elf} PRIVATE ${QTOUCHLIB_A} ${QTOUCHLIB_B} ${QTOUCHLIB_T})
526525

527526
# Select the smaller version of libc called nano.

src/common_main.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "screen.h"
2525
#include "securechip/securechip.h"
2626
#include "util.h"
27-
#include <wally_core.h>
2827

2928
extern void __attribute__((noreturn)) __stack_chk_fail(void);
3029
void __attribute__((noreturn)) __stack_chk_fail(void)
@@ -52,22 +51,6 @@ static const securechip_interface_functions_t _securechip_interface_functions =
5251
.random_32_bytes = random_32_bytes,
5352
};
5453

55-
static void _wally_patched_bzero(void* ptr, size_t len)
56-
{
57-
util_zero(ptr, len);
58-
}
59-
60-
static bool _setup_wally(void)
61-
{
62-
static struct wally_operations _ops = {0};
63-
_ops.struct_size = sizeof(struct wally_operations);
64-
if (wally_get_operations(&_ops) != WALLY_OK) {
65-
return false;
66-
}
67-
_ops.bzero_fn = _wally_patched_bzero;
68-
return wally_set_operations(&_ops) == WALLY_OK;
69-
}
70-
7154
void common_main(void)
7255
{
7356
mpu_bitbox02_init();
@@ -76,10 +59,6 @@ void common_main(void)
7659
AbortAutoenter("memory_setup failed");
7760
}
7861

79-
if (!_setup_wally()) {
80-
AbortAutoenter("_setup_wally failed");
81-
}
82-
8362
/* Enable/configure SmartEEPROM. */
8463
smarteeprom_bb02_config();
8564

src/factorysetup.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
#include <secp256k1.h>
3636
#include <ui/oled/oled.h>
3737

38-
#include <wally_crypto.h>
39-
4038
#define BUFFER_SIZE_DOWN 1024
4139
#define BUFFER_SIZE_UP 1024
4240

@@ -387,7 +385,7 @@ static void _api_msg(const uint8_t* input, size_t in_len, uint8_t* output, size_
387385
result = ERR_INVALID_INPUT;
388386
break;
389387
}
390-
uint8_t msg32[SHA256_LEN] = {0};
388+
uint8_t msg32[32] = {0};
391389
_attestation_sighash(attestation_device_pubkey, msg32);
392390
bool matches_a_root_pubkey = false;
393391
for (size_t pubkey_idx = 0; pubkey_idx < sizeof(_root_pubkey_bytes) / ROOT_PUBKEY_SIZE;

src/keystore.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ bool keystore_unlock_bip39(const char* mnemonic_passphrase)
464464
return false;
465465
}
466466

467-
uint8_t bip39_seed[BIP39_SEED_LEN_512] = {0};
467+
uint8_t bip39_seed[64] = {0};
468468
UTIL_CLEANUP_64(bip39_seed);
469469
rust_derive_bip39_seed(
470470
rust_util_bytes(seed, seed_length),

src/keystore.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
#include <stdint.h>
2323

2424
#include <secp256k1.h>
25-
#include <wally_bip32.h>
26-
#include <wally_bip39.h> // for BIP39_WORDLIST_LEN
27-
#include <wally_crypto.h> // for EC_PUBLIC_KEY_LEN
2825

2926
#define KEYSTORE_MAX_SEED_LENGTH (32)
3027
#define KEYSTORE_U2F_SEED_LENGTH SHA256_LEN

src/rust/bitbox02-rust/src/workflow/mnemonic.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ use sha2::{Digest, Sha256};
2929

3030
const NUM_RANDOM_WORDS: u8 = 5;
3131

32+
/// Number of words in the BIP-39 wordlist.
33+
const BIP39_WORDLIST_LEN: u16 = 2048;
34+
3235
fn as_str_vec(v: &[zeroize::Zeroizing<String>]) -> Vec<&str> {
3336
v.iter().map(|s| s.as_str()).collect()
3437
}
@@ -56,7 +59,7 @@ fn create_random_unique_words(word: &str, length: u8) -> (u8, Vec<zeroize::Zeroi
5659
// A random word everywhere else.
5760
// Loop until we get a unique word, we don't want repeated words in the list.
5861
loop {
59-
let idx = rand16() % bitbox02::keystore::BIP39_WORDLIST_LEN;
62+
let idx = rand16() % BIP39_WORDLIST_LEN;
6063
if picked_indices.contains(&idx) {
6164
continue;
6265
};
@@ -319,7 +322,7 @@ pub async fn get(
319322
.await;
320323

321324
// Provide all bip39 words to restrict the keyboard entry.
322-
let bip39_wordlist: Vec<u16> = (0..bitbox02::keystore::BIP39_WORDLIST_LEN).collect();
325+
let bip39_wordlist: Vec<u16> = (0..BIP39_WORDLIST_LEN).collect();
323326

324327
let mut word_idx: usize = 0;
325328
let mut entered_words = vec![zeroize::Zeroizing::new(String::new()); num_words];
@@ -430,7 +433,7 @@ mod tests {
430433

431434
fn bruteforce_lastword(mnemonic: &[&str]) -> Vec<zeroize::Zeroizing<String>> {
432435
let mut result = Vec::new();
433-
for i in 0..bitbox02::keystore::BIP39_WORDLIST_LEN {
436+
for i in 0..BIP39_WORDLIST_LEN {
434437
let word = bitbox02::keystore::get_bip39_word(i).unwrap();
435438
let mut m = mnemonic.to_vec();
436439
m.push(&word);

0 commit comments

Comments
 (0)