Move choice between prod and test storage to embedded_flash module
This way all users of storage can share the logic to choose between flash or RAM storage depending on the "std" feature. This is needed because the store_latency example assumes flash storage but is built when running `cargo test --features=std`.
This commit is contained in:
@@ -21,6 +21,7 @@ use crate::ctap::key_material;
|
||||
use crate::ctap::pin_protocol_v1::PIN_AUTH_LENGTH;
|
||||
use crate::ctap::status_code::Ctap2StatusCode;
|
||||
use crate::ctap::INITIAL_SIGNATURE_COUNTER;
|
||||
use crate::embedded_flash::{new_storage, Storage};
|
||||
#[cfg(feature = "with_ctap2_1")]
|
||||
use alloc::string::String;
|
||||
use alloc::vec;
|
||||
@@ -31,11 +32,6 @@ use cbor::cbor_array_vec;
|
||||
use core::convert::TryInto;
|
||||
use crypto::rng256::Rng256;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
type Storage = persistent_store::BufferStorage;
|
||||
#[cfg(not(feature = "std"))]
|
||||
type Storage = crate::embedded_flash::SyscallStorage;
|
||||
|
||||
// Those constants may be modified before compilation to tune the behavior of the key.
|
||||
//
|
||||
// The number of pages should be at least 3 and at most what the flash can hold. There should be no
|
||||
@@ -89,10 +85,7 @@ impl PersistentStore {
|
||||
///
|
||||
/// This should be at most one instance of persistent store per program lifetime.
|
||||
pub fn new(rng: &mut impl Rng256) -> PersistentStore {
|
||||
#[cfg(not(feature = "std"))]
|
||||
let storage = PersistentStore::new_prod_storage();
|
||||
#[cfg(feature = "std")]
|
||||
let storage = PersistentStore::new_test_storage();
|
||||
let storage = new_storage(NUM_PAGES);
|
||||
let mut store = PersistentStore {
|
||||
store: persistent_store::Store::new(storage).ok().unwrap(),
|
||||
};
|
||||
@@ -100,27 +93,6 @@ impl PersistentStore {
|
||||
store
|
||||
}
|
||||
|
||||
/// Creates a syscall storage in flash.
|
||||
#[cfg(not(feature = "std"))]
|
||||
fn new_prod_storage() -> Storage {
|
||||
Storage::new(NUM_PAGES).unwrap()
|
||||
}
|
||||
|
||||
/// Creates a buffer storage in RAM.
|
||||
#[cfg(feature = "std")]
|
||||
fn new_test_storage() -> Storage {
|
||||
const PAGE_SIZE: usize = 0x1000;
|
||||
let store = vec![0xff; NUM_PAGES * PAGE_SIZE].into_boxed_slice();
|
||||
let options = persistent_store::BufferOptions {
|
||||
word_size: 4,
|
||||
page_size: PAGE_SIZE,
|
||||
max_word_writes: 2,
|
||||
max_page_erases: 10000,
|
||||
strict_mode: true,
|
||||
};
|
||||
Storage::new(store, options)
|
||||
}
|
||||
|
||||
/// Initializes the store by creating missing objects.
|
||||
fn init(&mut self, rng: &mut impl Rng256) -> Result<(), Ctap2StatusCode> {
|
||||
// Generate and store the master keys if they are missing.
|
||||
|
||||
@@ -17,3 +17,36 @@ mod syscall;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub use self::syscall::SyscallStorage;
|
||||
|
||||
/// Storage definition for production.
|
||||
#[cfg(not(feature = "std"))]
|
||||
mod prod {
|
||||
pub type Storage = super::SyscallStorage;
|
||||
|
||||
pub fn new_storage(num_pages: usize) -> Storage {
|
||||
Storage::new(num_pages).unwrap()
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "std"))]
|
||||
pub use self::prod::{new_storage, Storage};
|
||||
|
||||
/// Storage definition for testing.
|
||||
#[cfg(feature = "std")]
|
||||
mod test {
|
||||
pub type Storage = persistent_store::BufferStorage;
|
||||
|
||||
pub fn new_storage(num_pages: usize) -> Storage {
|
||||
const PAGE_SIZE: usize = 0x1000;
|
||||
let store = vec![0xff; num_pages * PAGE_SIZE].into_boxed_slice();
|
||||
let options = persistent_store::BufferOptions {
|
||||
word_size: 4,
|
||||
page_size: PAGE_SIZE,
|
||||
max_word_writes: 2,
|
||||
max_page_erases: 10000,
|
||||
strict_mode: true,
|
||||
};
|
||||
Storage::new(store, options)
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "std")]
|
||||
pub use self::test::{new_storage, Storage};
|
||||
|
||||
Reference in New Issue
Block a user