Skip to content

Commit fa2791e

Browse files
committed
keystore: port keystore_get_u2f_seed to Rust
1 parent 5c0907f commit fa2791e

File tree

6 files changed

+27
-35
lines changed

6 files changed

+27
-35
lines changed

src/keystore.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -532,21 +532,6 @@ bool keystore_secp256k1_sign(
532532
return true;
533533
}
534534

535-
bool keystore_get_u2f_seed(uint8_t* seed_out)
536-
{
537-
if (keystore_is_locked()) {
538-
return false;
539-
}
540-
uint8_t bip39_seed[64] = {0};
541-
UTIL_CLEANUP_64(bip39_seed);
542-
if (!keystore_copy_bip39_seed(bip39_seed)) {
543-
return false;
544-
}
545-
const uint8_t message[] = "u2f";
546-
rust_hmac_sha256(bip39_seed, 64, message, sizeof(message), seed_out);
547-
return true;
548-
}
549-
550535
#ifdef TESTING
551536
void keystore_mock_unlocked(const uint8_t* seed, size_t seed_len, const uint8_t* bip39_seed)
552537
{

src/keystore.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,6 @@ USE_RESULT bool keystore_secp256k1_sign(
179179
uint8_t* sig_compact_out,
180180
int* recid_out);
181181

182-
/**
183-
* Get the seed to be used for u2f
184-
* @param seed_out Buffer for seed, must be KEYSTORE_U2F_SEED_LENGTH
185-
* @return true if succes
186-
*/
187-
USE_RESULT bool keystore_get_u2f_seed(uint8_t* seed_out);
188-
189182
#ifdef TESTING
190183
/**
191184
* convenience to mock the keystore state (locked, seed) in tests.

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ pub fn secp256k1_schnorr_sign(
183183
Ok(sig.serialize())
184184
}
185185

186+
/// Get the seed to be used for u2f
187+
#[cfg(feature = "app-u2f")]
188+
pub fn get_u2f_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
189+
let bip39_seed = keystore::copy_bip39_seed()?;
190+
191+
let mut engine = HmacEngine::<bitcoin::hashes::sha256::Hash>::new(&bip39_seed);
192+
// Null-terminator for backwards compatibility from the time when this was coded in C.
193+
engine.input(b"u2f\0");
194+
Ok(zeroize::Zeroizing::new(
195+
Hmac::from_engine(engine).to_byte_array().to_vec(),
196+
))
197+
}
198+
186199
/// # Safety
187200
///
188201
/// keypath pointer has point to a buffer of length `keypath_len` uint32 elements.
@@ -202,6 +215,18 @@ pub unsafe extern "C" fn rust_secp256k1_get_private_key(
202215
}
203216
}
204217

218+
#[cfg(feature = "app-u2f")]
219+
#[unsafe(no_mangle)]
220+
pub extern "C" fn rust_keystore_get_u2f_seed(mut seed_out: util::bytes::BytesMut) -> bool {
221+
match get_u2f_seed() {
222+
Ok(seed) => {
223+
seed_out.as_mut().copy_from_slice(&seed);
224+
true
225+
}
226+
Err(_) => false,
227+
}
228+
}
229+
205230
#[cfg(test)]
206231
mod tests {
207232
use super::*;
@@ -506,7 +531,7 @@ mod tests {
506531
test.expected_xpub,
507532
);
508533
assert_eq!(
509-
hex::encode(keystore::get_u2f_seed().unwrap()),
534+
hex::encode(get_u2f_seed().unwrap()),
510535
test.expected_u2f_seed_hex,
511536
);
512537
}

src/rust/bitbox02-sys/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const ALLOWLIST_FNS: &[&str] = &[
7373
"keystore_create_and_store_seed",
7474
"keystore_encrypt_and_store_seed",
7575
"keystore_get_bip39_word",
76-
"keystore_get_u2f_seed",
7776
"keystore_is_locked",
7877
"keystore_lock",
7978
"keystore_mock_unlocked",

src/rust/bitbox02/src/keystore.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,6 @@ pub fn encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error>
220220
}
221221
}
222222

223-
// Currently only used in the functional tests below.
224-
#[cfg(feature = "testing")]
225-
pub fn get_u2f_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
226-
let mut seed = zeroize::Zeroizing::new([0u8; 32].to_vec());
227-
match unsafe { bitbox02_sys::keystore_get_u2f_seed(seed.as_mut_ptr()) } {
228-
true => Ok(seed),
229-
false => Err(()),
230-
}
231-
}
232-
233223
#[cfg(test)]
234224
mod tests {
235225
use super::*;

src/u2f.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ USE_RESULT static bool _keyhandle_gen(
257257
uint8_t hmac_in[U2F_APPID_SIZE + U2F_NONCE_LENGTH];
258258
uint8_t seed[32];
259259
UTIL_CLEANUP_32(seed);
260-
if (!keystore_get_u2f_seed(seed)) {
260+
if (!rust_keystore_get_u2f_seed(rust_util_bytes_mut(seed, sizeof(seed)))) {
261261
return false;
262262
}
263263

0 commit comments

Comments
 (0)