Skip to content

Commit 84aef71

Browse files
committed
Merge branch 'port_verify_seed'
2 parents 27c4cce + 7dd42bf commit 84aef71

File tree

4 files changed

+29
-38
lines changed

4 files changed

+29
-38
lines changed

src/keystore.c

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -192,39 +192,6 @@ static keystore_error_t _get_and_decrypt_seed(
192192
return KEYSTORE_OK;
193193
}
194194

195-
static bool _verify_seed(
196-
const uint8_t* encryption_key,
197-
const uint8_t* expected_seed,
198-
size_t expected_seed_len)
199-
{
200-
uint8_t encrypted_seed_and_hmac[96];
201-
UTIL_CLEANUP_32(encrypted_seed_and_hmac);
202-
uint8_t encrypted_len;
203-
if (!memory_get_encrypted_seed_and_hmac(encrypted_seed_and_hmac, &encrypted_len)) {
204-
return false;
205-
}
206-
if (encrypted_len < 49) {
207-
Abort("_verify_seed: underflow / zero size");
208-
}
209-
size_t decrypted_len = encrypted_len - 48;
210-
uint8_t decrypted[decrypted_len];
211-
bool password_correct = cipher_aes_hmac_decrypt(
212-
encrypted_seed_and_hmac, encrypted_len, decrypted, &decrypted_len, encryption_key);
213-
if (!password_correct) {
214-
return false;
215-
}
216-
if (expected_seed_len != decrypted_len) {
217-
util_zero(decrypted, sizeof(decrypted));
218-
return false;
219-
}
220-
if (!MEMEQ(expected_seed, decrypted, expected_seed_len)) {
221-
util_zero(decrypted, sizeof(decrypted));
222-
return false;
223-
}
224-
util_zero(decrypted, sizeof(decrypted));
225-
return true;
226-
}
227-
228195
static keystore_error_t _hash_seed(const uint8_t* seed, size_t seed_len, uint8_t* out)
229196
{
230197
uint8_t salted_key[32] = {0};
@@ -362,7 +329,9 @@ keystore_error_t keystore_encrypt_and_store_seed(
362329
if (!memory_set_encrypted_seed_and_hmac(encrypted_seed, encrypted_seed_len_u8)) {
363330
return KEYSTORE_ERR_MEMORY;
364331
}
365-
if (!_verify_seed(secret, seed, seed_length)) {
332+
if (!rust_keystore_verify_seed(
333+
rust_util_bytes(secret, sizeof(secret)),
334+
rust_util_bytes(seed, seed_length))) {
366335
if (!memory_reset_hww()) {
367336
return KEYSTORE_ERR_MEMORY;
368337
}

src/rust/bitbox02-rust/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ erc20_params = { path = "../erc20_params", optional = true }
3333
binascii = { version = "0.1.4", default-features = false, features = ["encode"] }
3434
bitbox02-noise = {path = "../bitbox02-noise"}
3535
streaming-silent-payments = { path = "../streaming-silent-payments", optional = true }
36+
bitbox-aes = { path = "../bitbox-aes" }
3637
hex = { workspace = true }
3738
sha2 = { workspace = true }
3839
sha3 = { workspace = true, optional = true }
@@ -65,9 +66,6 @@ version = "0.13.1"
6566
default-features = false
6667
features = ["derive"]
6768

68-
[dev-dependencies]
69-
bitbox-aes = { path = "../bitbox-aes" }
70-
7169
[features]
7270
ed25519 = [
7371
"dep:bip32-ed25519",

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ pub fn is_locked() -> bool {
4646
keystore::_is_locked()
4747
}
4848

49+
fn verify_seed(encryption_key: &[u8], expected_seed: &[u8]) -> bool {
50+
if encryption_key.len() != 32 {
51+
return false;
52+
}
53+
54+
let cipher = match bitbox02::memory::get_encrypted_seed_and_hmac() {
55+
Ok(cipher) => cipher,
56+
Err(_) => return false,
57+
};
58+
let decrypted = match bitbox_aes::decrypt_with_hmac(encryption_key, &cipher) {
59+
Ok(decrypted) => decrypted,
60+
Err(_) => return false,
61+
};
62+
63+
decrypted.as_slice() == expected_seed
64+
}
65+
4966
pub fn unlock(password: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, Error> {
5067
keystore::_unlock(password)
5168
}
@@ -291,6 +308,14 @@ pub unsafe extern "C" fn rust_keystore_stretch_retained_seed_encryption_key(
291308
}
292309
}
293310

311+
#[unsafe(no_mangle)]
312+
pub extern "C" fn rust_keystore_verify_seed(
313+
encryption_key: util::bytes::Bytes,
314+
expected_seed: util::bytes::Bytes,
315+
) -> bool {
316+
verify_seed(encryption_key.as_ref(), expected_seed.as_ref())
317+
}
318+
294319
fn bip85_entropy(keypath: &[u32]) -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
295320
let priv_key = secp256k1_get_private_key_twice(keypath)?;
296321

src/rust/bitbox02/src/memory.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ pub fn get_attestation_pubkey_and_certificate(
9898
}
9999
}
100100

101-
#[cfg(feature = "testing")]
102101
pub fn get_encrypted_seed_and_hmac() -> Result<alloc::vec::Vec<u8>, ()> {
103102
let mut out = vec![0u8; 96];
104103
let mut len = 0u8;

0 commit comments

Comments
 (0)