Skip to content

Commit 80e6088

Browse files
committed
keystore: remove keystore_get_bip39_mnemonic
Is now readily implemented in Rust using bip39_mnemonic_from_seed() and copy_seed().
1 parent a781609 commit 80e6088

File tree

8 files changed

+32
-66
lines changed

8 files changed

+32
-66
lines changed

src/keystore.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -504,20 +504,6 @@ bool keystore_bip39_mnemonic_from_seed(
504504
return snprintf_result >= 0 && snprintf_result < (int)mnemonic_out_size;
505505
}
506506

507-
bool keystore_get_bip39_mnemonic(char* mnemonic_out, size_t mnemonic_out_size)
508-
{
509-
if (keystore_is_locked()) {
510-
return false;
511-
}
512-
uint8_t seed[KEYSTORE_MAX_SEED_LENGTH] = {0};
513-
UTIL_CLEANUP_32(seed);
514-
size_t seed_length = 0;
515-
if (!keystore_copy_seed(seed, &seed_length)) {
516-
return false;
517-
}
518-
return keystore_bip39_mnemonic_from_seed(seed, seed_length, mnemonic_out, mnemonic_out_size);
519-
}
520-
521507
bool keystore_bip39_mnemonic_to_seed(const char* mnemonic, uint8_t* seed_out, size_t* seed_len_out)
522508
{
523509
return bip39_mnemonic_to_bytes(NULL, mnemonic, seed_out, 32, seed_len_out) == WALLY_OK;

src/keystore.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ USE_RESULT bool keystore_bip39_mnemonic_from_seed(
125125
char* mnemonic_out,
126126
size_t mnemonic_out_size);
127127

128-
/**
129-
* @param[out] mnemonic_out resulting mnemonic
130-
* @param[in] mnemonic_out_size size of mnemonic_out. Should be at least 216 bytes (longest possible
131-
* 24 word phrase plus null terminator).
132-
* @return returns false if the keystore is not unlocked or the mnemonic does not fit.
133-
* The resulting string should be safely zeroed after use.
134-
*/
135-
USE_RESULT bool keystore_get_bip39_mnemonic(char* mnemonic_out, size_t mnemonic_out_size);
136-
137128
/**
138129
* Turn a bip39 mnemonic into a seed. Make sure to use UTIL_CLEANUP_32 to destroy it.
139130
* Output can be fed into `keystore_encrypt_and_store_seed` to create a keystore from the mnemonic.

src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use pb::response::Response;
2222
use crate::hal::Ui;
2323
use crate::workflow::{confirm, mnemonic, unlock};
2424

25-
use bitbox02::keystore;
25+
use crate::keystore;
2626

2727
/// Handle the ShowMnemonic API call. This shows the seed encoded as
2828
/// 12/18/24 BIP39 English words. Afterwards, for each word, the user

src/rust/bitbox02-rust/src/keystore.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#[cfg(feature = "ed25519")]
1616
pub mod ed25519;
1717

18+
use alloc::string::String;
1819
use alloc::vec::Vec;
1920

2021
use crate::bip32;
@@ -25,6 +26,11 @@ use util::bip32::HARDENED;
2526
use crate::hash::Sha512;
2627
use hmac::{digest::FixedOutput, Mac, SimpleHmac};
2728

29+
/// Returns the keystore's seed encoded as a BIP-39 mnemonic.
30+
pub fn get_bip39_mnemonic() -> Result<zeroize::Zeroizing<String>, ()> {
31+
keystore::bip39_mnemonic_from_seed(&keystore::copy_seed()?)
32+
}
33+
2834
/// Derives an xpub from the keystore seed at the given keypath.
2935
pub fn get_xpub(keypath: &[u32]) -> Result<bip32::Xpub, ()> {
3036
// Convert from C keystore to Rust by encoding the xpub in C and decoding it in Rust.
@@ -74,7 +80,17 @@ pub fn bip85_ln(index: u32) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
7480
mod tests {
7581
use super::*;
7682

77-
use bitbox02::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
83+
use bitbox02::testing::{mock_unlocked, mock_unlocked_using_mnemonic, TEST_MNEMONIC};
84+
85+
#[test]
86+
fn test_get_bip39_mnemonic() {
87+
keystore::lock();
88+
assert!(get_bip39_mnemonic().is_err());
89+
90+
mock_unlocked();
91+
92+
assert_eq!(get_bip39_mnemonic().unwrap().as_str(), TEST_MNEMONIC);
93+
}
7894

7995
#[test]
8096
fn test_get_xpub() {

src/rust/bitbox02-sys/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ const ALLOWLIST_FNS: &[&str] = &[
7272
"keystore_create_and_store_seed",
7373
"keystore_encode_xpub_at_keypath",
7474
"keystore_encrypt_and_store_seed",
75-
"keystore_get_bip39_mnemonic",
7675
"keystore_get_bip39_word",
7776
"keystore_get_ed25519_seed",
7877
"keystore_is_locked",

src/rust/bitbox02/src/keystore.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,6 @@ pub fn copy_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
122122
}
123123
}
124124

125-
pub fn get_bip39_mnemonic() -> Result<zeroize::Zeroizing<String>, ()> {
126-
let mut mnemonic = zeroize::Zeroizing::new([0u8; 256]);
127-
match unsafe {
128-
bitbox02_sys::keystore_get_bip39_mnemonic(mnemonic.as_mut_ptr(), mnemonic.len() as _)
129-
} {
130-
false => Err(()),
131-
true => Ok(zeroize::Zeroizing::new(
132-
crate::util::str_from_null_terminated(&mnemonic[..])
133-
.unwrap()
134-
.into(),
135-
)),
136-
}
137-
}
138-
139125
pub fn bip39_mnemonic_from_seed(seed: &[u8]) -> Result<zeroize::Zeroizing<String>, ()> {
140126
let mut mnemonic = zeroize::Zeroizing::new([0u8; 256]);
141127
match unsafe {
@@ -374,7 +360,7 @@ pub fn secp256k1_schnorr_sign(
374360
#[cfg(test)]
375361
mod tests {
376362
use super::*;
377-
use crate::testing::{mock_unlocked, mock_unlocked_using_mnemonic, TEST_MNEMONIC};
363+
use crate::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
378364
use util::bip32::HARDENED;
379365

380366
#[test]
@@ -442,19 +428,6 @@ mod tests {
442428
);
443429
}
444430

445-
#[test]
446-
fn test_get_bip39_mnemonic() {
447-
lock();
448-
assert!(get_bip39_mnemonic().is_err());
449-
450-
mock_unlocked();
451-
452-
assert_eq!(
453-
get_bip39_mnemonic().unwrap().as_ref() as &str,
454-
TEST_MNEMONIC
455-
);
456-
}
457-
458431
#[test]
459432
fn test_get_bip39_word() {
460433
assert!(get_bip39_word(2048).is_err());

test/unit-test/test_keystore.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -451,26 +451,23 @@ static void _test_keystore_lock(void** state)
451451
assert_true(keystore_is_locked());
452452
}
453453

454-
static void _test_keystore_get_bip39_mnemonic(void** state)
454+
static void _test_keystore_bip39_mnemonic_from_seed(void** state)
455455
{
456456
char mnemonic[300];
457-
_mock_unlocked(NULL, 0, NULL);
458-
assert_false(keystore_get_bip39_mnemonic(mnemonic, sizeof(mnemonic)));
459-
460-
_mock_unlocked(_mock_seed, sizeof(_mock_seed), NULL);
461-
assert_false(keystore_get_bip39_mnemonic(mnemonic, sizeof(mnemonic)));
462457

463-
_mock_unlocked(_mock_seed, sizeof(_mock_seed), _mock_bip39_seed);
464-
assert_true(keystore_get_bip39_mnemonic(mnemonic, sizeof(mnemonic)));
458+
assert_true(keystore_bip39_mnemonic_from_seed(
459+
_mock_seed, sizeof(_mock_seed), mnemonic, sizeof(mnemonic)));
465460
const char* expected_mnemonic =
466461
"baby mass dust captain baby mass mass dust captain baby mass dutch creek office smoke "
467462
"grid creek olive baby mass dust captain baby length";
468463
assert_string_equal(mnemonic, expected_mnemonic);
469464

470465
// Output buffer too short.
471-
assert_false(keystore_get_bip39_mnemonic(mnemonic, strlen(expected_mnemonic)));
466+
assert_false(keystore_bip39_mnemonic_from_seed(
467+
_mock_seed, sizeof(_mock_seed), mnemonic, strlen(expected_mnemonic)));
472468
// Just enough space to fit.
473-
assert_true(keystore_get_bip39_mnemonic(mnemonic, strlen(expected_mnemonic) + 1));
469+
assert_true(keystore_bip39_mnemonic_from_seed(
470+
_mock_seed, sizeof(_mock_seed), mnemonic, strlen(expected_mnemonic) + 1));
474471
}
475472

476473
static void _test_keystore_create_and_store_seed(void** state)
@@ -696,7 +693,7 @@ int main(void)
696693
cmocka_unit_test(_test_keystore_unlock),
697694
cmocka_unit_test(_test_keystore_unlock_bip39),
698695
cmocka_unit_test(_test_keystore_lock),
699-
cmocka_unit_test(_test_keystore_get_bip39_mnemonic),
696+
cmocka_unit_test(_test_keystore_bip39_mnemonic_from_seed),
700697
cmocka_unit_test(_test_keystore_create_and_store_seed),
701698
cmocka_unit_test(_test_keystore_get_ed25519_seed),
702699
cmocka_unit_test(_test_secp256k1_schnorr_sign),

test/unit-test/test_keystore_functional.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ static void _test_seeds(void** state)
9696

9797
static void _check_mnemonic(const char* expected)
9898
{
99+
uint8_t seed[KEYSTORE_MAX_SEED_LENGTH];
100+
size_t seed_len;
101+
99102
char mnemonic[300];
100-
assert_true(keystore_get_bip39_mnemonic(mnemonic, sizeof(mnemonic)));
103+
assert_true(keystore_copy_seed(seed, &seed_len));
104+
assert_true(keystore_bip39_mnemonic_from_seed(seed, seed_len, mnemonic, sizeof(mnemonic)));
101105
assert_string_equal(mnemonic, expected);
102106
}
103107

0 commit comments

Comments
 (0)