Skip to content

Commit fc4c3c7

Browse files
committed
wip intermediate
So C tests can be shown to pass with the Rust impl.
1 parent 3ab9b5b commit fc4c3c7

File tree

5 files changed

+43
-58
lines changed

5 files changed

+43
-58
lines changed

src/keystore.c

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -210,67 +210,12 @@ static keystore_error_t _get_and_decrypt_seed(
210210
return KEYSTORE_OK;
211211
}
212212

213-
static bool _verify_seed(
214-
const char* password,
215-
const uint8_t* expected_seed,
216-
size_t expected_seed_len)
217-
{
218-
uint8_t decrypted_seed[KEYSTORE_MAX_SEED_LENGTH] = {0};
219-
size_t seed_len;
220-
UTIL_CLEANUP_32(decrypted_seed);
221-
if (_get_and_decrypt_seed(password, decrypted_seed, &seed_len, NULL) != KEYSTORE_OK) {
222-
return false;
223-
}
224-
if (expected_seed_len != seed_len) {
225-
return false;
226-
}
227-
if (!MEMEQ(expected_seed, decrypted_seed, seed_len)) {
228-
return false;
229-
}
230-
return true;
231-
}
232-
233213
keystore_error_t keystore_encrypt_and_store_seed(
234214
const uint8_t* seed,
235215
size_t seed_length,
236216
const char* password)
237217
{
238-
if (memory_is_initialized()) {
239-
return KEYSTORE_ERR_MEMORY;
240-
}
241-
keystore_lock();
242-
if (!_validate_seed_length(seed_length)) {
243-
return KEYSTORE_ERR_SEED_SIZE;
244-
}
245-
if (securechip_init_new_password(password)) {
246-
return KEYSTORE_ERR_SECURECHIP;
247-
}
248-
uint8_t secret[32] = {0};
249-
UTIL_CLEANUP_32(secret);
250-
if (securechip_stretch_password(password, secret)) {
251-
return KEYSTORE_ERR_SECURECHIP;
252-
}
253-
254-
size_t encrypted_seed_len = seed_length + 64;
255-
uint8_t encrypted_seed[encrypted_seed_len];
256-
UTIL_CLEANUP_32(encrypted_seed);
257-
if (!cipher_aes_hmac_encrypt(seed, seed_length, encrypted_seed, &encrypted_seed_len, secret)) {
258-
return KEYSTORE_ERR_ENCRYPT;
259-
}
260-
if (encrypted_seed_len > 255) { // sanity check, can't happen
261-
Abort("keystore_encrypt_and_store_seed");
262-
}
263-
uint8_t encrypted_seed_len_u8 = (uint8_t)encrypted_seed_len;
264-
if (!memory_set_encrypted_seed_and_hmac(encrypted_seed, encrypted_seed_len_u8)) {
265-
return KEYSTORE_ERR_MEMORY;
266-
}
267-
if (!_verify_seed(password, seed, seed_length)) {
268-
if (!memory_reset_hww()) {
269-
return KEYSTORE_ERR_MEMORY;
270-
}
271-
return KEYSTORE_ERR_MEMORY;
272-
}
273-
return KEYSTORE_OK;
218+
return rust_keystore_encrypt_and_store_seed(rust_util_bytes(seed, seed_length), password);
274219
}
275220

276221
keystore_error_t keystore_create_and_store_seed(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 Shift Cryptosecurity AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
extern crate alloc;
16+
use crate::util::Bytes;
17+
18+
use bitbox02::keystore::keystore_error_t;
19+
use bitbox02_rust::keystore::Error;
20+
21+
#[no_mangle]
22+
pub unsafe extern "C" fn rust_keystore_encrypt_and_store_seed(
23+
seed: Bytes,
24+
password: *const core::ffi::c_char,
25+
) -> u8 {
26+
let password_str = core::ffi::CStr::from_ptr(password).to_str().unwrap();
27+
match bitbox02_rust::keystore::encrypt_and_store_seed(seed.as_ref(), password_str) {
28+
Ok(()) => 0,
29+
Err(err) => match err {
30+
// In C this err didn't exist and ERR_MEMORY was used.
31+
Error::AlreadyInitialized => keystore_error_t::KEYSTORE_ERR_MEMORY as _,
32+
Error::Memory => keystore_error_t::KEYSTORE_ERR_MEMORY as _,
33+
Error::SeedSize => keystore_error_t::KEYSTORE_ERR_SEED_SIZE as _,
34+
Error::SecureChip => keystore_error_t::KEYSTORE_ERR_SECURECHIP as _,
35+
Error::IncorrectPassword => keystore_error_t::KEYSTORE_ERR_INCORRECT_PASSWORD as _,
36+
},
37+
}
38+
}

src/rust/bitbox02-rust-c/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ mod p256;
3636
mod sha2;
3737
#[cfg(feature = "firmware")]
3838
mod workflow;
39+
#[cfg(feature = "firmware")]
40+
mod keystore;
3941

4042
#[cfg(feature = "firmware")]
4143
mod der;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn get_and_decrypt_seed(password: &str) -> Result<zeroize::Zeroizing<Vec<u8>>, E
5151

5252
pub fn encrypt_and_store_seed(seed: &[u8], password: &str) -> Result<(), Error> {
5353
if memory::is_initialized() {
54-
return Err(Error::AlreadyInitialized);
54+
return Err(Error::Memory);
5555
}
5656
keystore::lock();
5757
validate_seed_length(seed.len())?;

src/rust/bitbox02/src/keystore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use alloc::vec::Vec;
1919

2020
use core::convert::TryInto;
2121

22-
use bitbox02_sys::keystore_error_t;
22+
pub use bitbox02_sys::keystore_error_t;
2323

2424
pub const BIP39_WORDLIST_LEN: u16 = bitbox02_sys::BIP39_WORDLIST_LEN as u16;
2525
pub const EC_PUBLIC_KEY_LEN: usize = bitbox02_sys::EC_PUBLIC_KEY_LEN as _;

0 commit comments

Comments
 (0)