Skip to content

Commit 7b104fe

Browse files
committed
Merge branch 'keystore-sizes'
2 parents f2a76cb + 01d3c58 commit 7b104fe

File tree

5 files changed

+12
-11
lines changed

5 files changed

+12
-11
lines changed

src/keystore.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
static bool _is_unlocked_device = false;
3939
// Must be defined if is_unlocked is true. Length of the seed store in `_retained_seed`. See also:
4040
// `_validate_seed_length()`.
41-
static uint8_t _seed_length = 0;
41+
static size_t _seed_length = 0;
4242
// Must be defined if is_unlocked is true. ONLY ACCESS THIS WITH _get_seed()
4343
static uint8_t _retained_seed[KEYSTORE_MAX_SEED_LENGTH] = {0};
4444

@@ -78,7 +78,7 @@ static const uint8_t* _get_seed(void)
7878
return _retained_seed;
7979
}
8080

81-
bool keystore_copy_seed(uint8_t* seed_out, uint32_t* length_out)
81+
bool keystore_copy_seed(uint8_t* seed_out, size_t* length_out)
8282
{
8383
if (_get_seed() == NULL) {
8484
return false;
@@ -236,7 +236,7 @@ static bool _verify_seed(
236236

237237
keystore_error_t keystore_encrypt_and_store_seed(
238238
const uint8_t* seed,
239-
uint32_t seed_length,
239+
size_t seed_length,
240240
const char* password)
241241
{
242242
if (memory_is_initialized()) {
@@ -268,7 +268,8 @@ keystore_error_t keystore_encrypt_and_store_seed(
268268
if (encrypted_seed_len > 255) { // sanity check, can't happen
269269
Abort("keystore_encrypt_and_store_seed");
270270
}
271-
if (!memory_set_encrypted_seed_and_hmac(encrypted_seed, encrypted_seed_len)) {
271+
uint8_t encrypted_seed_len_u8 = (uint8_t)encrypted_seed_len;
272+
if (!memory_set_encrypted_seed_and_hmac(encrypted_seed, encrypted_seed_len_u8)) {
272273
return KEYSTORE_ERR_MEMORY;
273274
}
274275
if (!_verify_seed(password, seed, seed_length)) {

src/keystore.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void keystore_mock_unlocked(const uint8_t* seed, size_t seed_len, const uint8_t*
6060
* @param[out] length_out The seed length.
6161
* @return true if the seed was still retained.
6262
*/
63-
USE_RESULT bool keystore_copy_seed(uint8_t* seed_out, uint32_t* length_out);
63+
USE_RESULT bool keystore_copy_seed(uint8_t* seed_out, size_t* length_out);
6464

6565
/**
6666
* Restores a seed.
@@ -69,7 +69,7 @@ USE_RESULT bool keystore_copy_seed(uint8_t* seed_out, uint32_t* length_out);
6969
* @param[in] password The password with which we encrypt the seed.
7070
*/
7171
USE_RESULT keystore_error_t
72-
keystore_encrypt_and_store_seed(const uint8_t* seed, uint32_t seed_length, const char* password);
72+
keystore_encrypt_and_store_seed(const uint8_t* seed, size_t seed_length, const char* password);
7373

7474
/**
7575
Generates the seed, mixes it with host_entropy, and stores it encrypted with the

src/rust/bitbox02/src/keystore.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ pub fn create_and_store_seed(password: &SafeInputString, host_entropy: &[u8]) ->
108108

109109
pub fn copy_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
110110
let mut seed = zeroize::Zeroizing::new([0u8; MAX_SEED_LENGTH]);
111-
let mut seed_len: u32 = 0;
111+
let mut seed_len: usize = 0;
112112
match unsafe { bitbox02_sys::keystore_copy_seed(seed.as_mut_ptr(), &mut seed_len) } {
113-
true => Ok(zeroize::Zeroizing::new(seed[..seed_len as usize].to_vec())),
113+
true => Ok(zeroize::Zeroizing::new(seed[..seed_len].to_vec())),
114114
false => Err(()),
115115
}
116116
}
@@ -283,7 +283,7 @@ pub fn encrypt_and_store_seed(seed: &[u8], password: &SafeInputString) -> Result
283283
match unsafe {
284284
bitbox02_sys::keystore_encrypt_and_store_seed(
285285
seed.as_ptr(),
286-
seed.len() as _,
286+
seed.len(),
287287
password.as_cstr(),
288288
)
289289
} {

test/unit-test/test_keystore.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ static void _test_keystore_create_and_unlock_twice(void** state)
364364
static void _expect_seeded(bool seeded)
365365
{
366366
uint8_t seed[KEYSTORE_MAX_SEED_LENGTH];
367-
uint32_t len;
367+
size_t len;
368368
assert_int_equal(seeded, keystore_copy_seed(seed, &len));
369369
}
370370

test/unit-test/test_keystore_functional.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void _test_seeds(void** state)
6464
_smarteeprom_reset();
6565
assert_true(keystore_is_locked());
6666
uint8_t read_seed[KEYSTORE_MAX_SEED_LENGTH];
67-
uint32_t read_seed_len;
67+
size_t read_seed_len;
6868
assert_false(keystore_copy_seed(read_seed, &read_seed_len));
6969

7070
will_return(__wrap_memory_is_initialized, true);

0 commit comments

Comments
 (0)