From a203ff13df251ac5a172997c2210e0b4a3322e44 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Thu, 24 Sep 2020 10:06:03 +0200 Subject: [PATCH 1/4] Create new store and add storage API --- .github/workflows/cargo_fmt.yml | 6 ++ .github/workflows/persistent_store_test.yml | 26 ++++++ libraries/persistent_store/Cargo.toml | 11 +++ libraries/persistent_store/src/lib.rs | 19 +++++ libraries/persistent_store/src/storage.rs | 95 +++++++++++++++++++++ run_desktop_tests.sh | 9 ++ 6 files changed, 166 insertions(+) create mode 100644 .github/workflows/persistent_store_test.yml create mode 100644 libraries/persistent_store/Cargo.toml create mode 100644 libraries/persistent_store/src/lib.rs create mode 100644 libraries/persistent_store/src/storage.rs diff --git a/.github/workflows/cargo_fmt.yml b/.github/workflows/cargo_fmt.yml index a3cbd70..6e38687 100644 --- a/.github/workflows/cargo_fmt.yml +++ b/.github/workflows/cargo_fmt.yml @@ -62,6 +62,12 @@ jobs: command: fmt args: --manifest-path libraries/crypto/Cargo.toml --all -- --check + - name: Cargo format libraries/persistent_store + uses: actions-rs/cargo@v1 + with: + command: fmt + args: --manifest-path libraries/persistent_store/Cargo.toml --all -- --check + - name: Cargo format tools/heapviz uses: actions-rs/cargo@v1 with: diff --git a/.github/workflows/persistent_store_test.yml b/.github/workflows/persistent_store_test.yml new file mode 100644 index 0000000..1a1d942 --- /dev/null +++ b/.github/workflows/persistent_store_test.yml @@ -0,0 +1,26 @@ +--- +name: Persistent store tests +on: + push: + paths: + - 'libraries/peristent_store/**' + pull_request: + types: [opened, synchronize, reopened] + +jobs: + persistent_store_test: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + + - name: Unit testing of Persistent store library (release mode) + uses: actions-rs/cargo@v1 + with: + command: test + args: --manifest-path libraries/persistent_store/Cargo.toml --release --features std + + - name: Unit testing of Persistent store library (debug mode) + uses: actions-rs/cargo@v1 + with: + command: test + args: --manifest-path libraries/persistent_store/Cargo.toml --features std diff --git a/libraries/persistent_store/Cargo.toml b/libraries/persistent_store/Cargo.toml new file mode 100644 index 0000000..5fe7c9f --- /dev/null +++ b/libraries/persistent_store/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "persistent_store" +version = "0.1.0" +authors = ["Julien Cretin "] +license = "Apache-2.0" +edition = "2018" + +[dependencies] + +[features] +std = [] diff --git a/libraries/persistent_store/src/lib.rs b/libraries/persistent_store/src/lib.rs new file mode 100644 index 0000000..05425c3 --- /dev/null +++ b/libraries/persistent_store/src/lib.rs @@ -0,0 +1,19 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![cfg_attr(not(feature = "std"), no_std)] + +mod storage; + +pub use self::storage::{Storage, StorageError, StorageIndex, StorageResult}; diff --git a/libraries/persistent_store/src/storage.rs b/libraries/persistent_store/src/storage.rs new file mode 100644 index 0000000..c5d55c5 --- /dev/null +++ b/libraries/persistent_store/src/storage.rs @@ -0,0 +1,95 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// Represents a byte position in a storage +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct StorageIndex { + pub page: usize, + pub byte: usize, +} + +/// Represents a possible storage error +#[derive(Debug, PartialEq, Eq)] +pub enum StorageError { + /// Arguments are not correctly aligned + NotAligned, + + /// Arguments are out of bounds + OutOfBounds, + + /// Implementation-specific error + CustomError, +} + +pub type StorageResult = Result; + +/// Abstracts a flash storage +pub trait Storage { + /// The size of a word in bytes + fn word_size(&self) -> usize; + + /// The size of a page in bytes + fn page_size(&self) -> usize; + + /// The number of pages in the storage + fn num_pages(&self) -> usize; + + /// Maximum number of times a word can be written between page erasures + fn max_word_writes(&self) -> usize; + + /// Maximum number of times a page can be erased + fn max_page_erases(&self) -> usize; + + /// Reads a byte slice from the storage + /// + /// The `index` must designate `length` bytes in the storage. + fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult<&[u8]>; + + /// Writes a word slice to the storage + /// + /// The following pre-conditions must hold: + /// - The `index` must designate `value.len()` bytes in the storage. + /// - Both `index` and `value.len()` must be word-aligned. + /// - The written words should not have been written too many times since last page erasure. + fn write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()>; + + /// Erases a page of the storage + /// + /// The `page` must be in the storage. + fn erase_page(&mut self, page: usize) -> StorageResult<()>; +} + +impl StorageIndex { + /// Whether a slice fits in a storage page + fn is_valid(self, length: usize, storage: &impl Storage) -> bool { + let page_size = storage.page_size(); + self.page < storage.num_pages() && length <= page_size && self.byte <= page_size - length + } + + /// Returns the range of a valid slice + /// + /// The range starts at `self` with `length` bytes. + pub fn range( + self, + length: usize, + storage: &impl Storage, + ) -> StorageResult> { + if self.is_valid(length, storage) { + let start = self.page * storage.page_size() + self.byte; + Ok(start..start + length) + } else { + Err(StorageError::OutOfBounds) + } + } +} diff --git a/run_desktop_tests.sh b/run_desktop_tests.sh index 703e6e6..f2dff08 100755 --- a/run_desktop_tests.sh +++ b/run_desktop_tests.sh @@ -23,6 +23,9 @@ cd ../.. cd libraries/crypto cargo fmt --all -- --check cd ../.. +cd libraries/persistent_store +cargo fmt --all -- --check +cd ../.. cd tools/heapviz cargo fmt --all -- --check cd ../.. @@ -84,6 +87,9 @@ then cd libraries/crypto RUSTFLAGS='-C target-feature=+aes' cargo test --release --features std,derive_debug cd ../.. + cd libraries/persistent_store + cargo test --release --features std + cd ../.. cargo test --release --features std echo "Running unit tests on the desktop (debug mode)..." @@ -93,6 +99,9 @@ then cd libraries/crypto RUSTFLAGS='-C target-feature=+aes' cargo test --features std,derive_debug cd ../.. + cd libraries/persistent_store + cargo test --features std + cd ../.. cargo test --features std echo "Running unit tests on the desktop (release mode + CTAP1)..." From 7f826dde5385071b9d4e12f1592321c9c7331b9e Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Mon, 28 Sep 2020 12:44:56 +0200 Subject: [PATCH 2/4] Add dot at end of short comments --- libraries/persistent_store/src/storage.rs | 32 +++++++++++------------ 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libraries/persistent_store/src/storage.rs b/libraries/persistent_store/src/storage.rs index c5d55c5..f5ba7e9 100644 --- a/libraries/persistent_store/src/storage.rs +++ b/libraries/persistent_store/src/storage.rs @@ -12,51 +12,51 @@ // See the License for the specific language governing permissions and // limitations under the License. -/// Represents a byte position in a storage +/// Represents a byte position in a storage. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct StorageIndex { pub page: usize, pub byte: usize, } -/// Represents a possible storage error +/// Represents a possible storage error. #[derive(Debug, PartialEq, Eq)] pub enum StorageError { - /// Arguments are not correctly aligned + /// Arguments are not correctly aligned. NotAligned, - /// Arguments are out of bounds + /// Arguments are out of bounds. OutOfBounds, - /// Implementation-specific error + /// Implementation-specific error. CustomError, } pub type StorageResult = Result; -/// Abstracts a flash storage +/// Abstracts a flash storage. pub trait Storage { - /// The size of a word in bytes + /// The size of a word in bytes. fn word_size(&self) -> usize; - /// The size of a page in bytes + /// The size of a page in bytes. fn page_size(&self) -> usize; - /// The number of pages in the storage + /// The number of pages in the storage. fn num_pages(&self) -> usize; - /// Maximum number of times a word can be written between page erasures + /// Maximum number of times a word can be written between page erasures. fn max_word_writes(&self) -> usize; - /// Maximum number of times a page can be erased + /// Maximum number of times a page can be erased. fn max_page_erases(&self) -> usize; - /// Reads a byte slice from the storage + /// Reads a byte slice from the storage. /// /// The `index` must designate `length` bytes in the storage. fn read_slice(&self, index: StorageIndex, length: usize) -> StorageResult<&[u8]>; - /// Writes a word slice to the storage + /// Writes a word slice to the storage. /// /// The following pre-conditions must hold: /// - The `index` must designate `value.len()` bytes in the storage. @@ -64,20 +64,20 @@ pub trait Storage { /// - The written words should not have been written too many times since last page erasure. fn write_slice(&mut self, index: StorageIndex, value: &[u8]) -> StorageResult<()>; - /// Erases a page of the storage + /// Erases a page of the storage. /// /// The `page` must be in the storage. fn erase_page(&mut self, page: usize) -> StorageResult<()>; } impl StorageIndex { - /// Whether a slice fits in a storage page + /// Whether a slice fits in a storage page. fn is_valid(self, length: usize, storage: &impl Storage) -> bool { let page_size = storage.page_size(); self.page < storage.num_pages() && length <= page_size && self.byte <= page_size - length } - /// Returns the range of a valid slice + /// Returns the range of a valid slice. /// /// The range starts at `self` with `length` bytes. pub fn range( From ba151d92897e5fda97720b1299a14562d1e60d03 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Mon, 28 Sep 2020 13:14:33 +0200 Subject: [PATCH 3/4] Update copyright dates --- libraries/persistent_store/src/lib.rs | 2 +- libraries/persistent_store/src/storage.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/persistent_store/src/lib.rs b/libraries/persistent_store/src/lib.rs index 05425c3..846f956 100644 --- a/libraries/persistent_store/src/lib.rs +++ b/libraries/persistent_store/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/libraries/persistent_store/src/storage.rs b/libraries/persistent_store/src/storage.rs index f5ba7e9..55df177 100644 --- a/libraries/persistent_store/src/storage.rs +++ b/libraries/persistent_store/src/storage.rs @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2019-2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From 6d9c1ce1a4291f243d8bef3008a6c5556e2e9084 Mon Sep 17 00:00:00 2001 From: Julien Cretin Date: Mon, 28 Sep 2020 13:22:06 +0200 Subject: [PATCH 4/4] Update reproducible hashes --- .../reference_binaries_macos-10.15.sha256sum | 10 +++++----- .../reference_binaries_ubuntu-18.04.sha256sum | 10 +++++----- reproducible/reference_elf2tab_macos-10.15.txt | 16 ++++++++-------- reproducible/reference_elf2tab_ubuntu-18.04.txt | 16 ++++++++-------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/reproducible/reference_binaries_macos-10.15.sha256sum b/reproducible/reference_binaries_macos-10.15.sha256sum index 17fb89b..7fb1c68 100644 --- a/reproducible/reference_binaries_macos-10.15.sha256sum +++ b/reproducible/reference_binaries_macos-10.15.sha256sum @@ -1,9 +1,9 @@ dd5920dfb172d9371b29d019b6a37fae1a995bf9d814000944d9ef36bad31513 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin -e1b147fefa75befe6f280e8aec41fa62b02e5c01bb1c940736b1903b597afe13 target/nrf52840dk_merged.hex +1518f10da8515c321dcb19847701dcf15511e44d8a9b80576cb36e13258353fb target/nrf52840dk_merged.hex e4acfa602a5cc5d7c61d465f873918e8e0858628d0e5f8e0db26a7b7dd0b94d4 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin -99b69a33320a4c5218b9e5084f2ad56957e04465dbbb53b0ab1051a176ba73a0 target/nrf52840_dongle_merged.hex +ba5c294725ba98230427601ab9e09291b3de10d60a78a5e1078ac2357c991fb7 target/nrf52840_dongle_merged.hex c0ace9f13ef3fd18c576a735ae23b3956bf8dd346f20c6217086e748d6bad8a2 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin -3bde950d855ffa68eb334033388b292e98cdbf97993a57eca4524de101166f8b target/nrf52840_dongle_dfu_merged.hex +e33940f7277c16825e847762d23a4eea3ad8fe104f20b858ef339687c74e81bc target/nrf52840_dongle_dfu_merged.hex 06a38a0d6d356145467a73c765e28a945878f663664016f888393207097bfe10 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_mdk_dfu.bin -f3ed663311204ac709ed05dfb3d09ff283c3df7dbc5480ad3ec050523b0a2ed2 target/nrf52840_mdk_dfu_merged.hex -a2ee6798d20da62ed2c3ea7164620253d4b2319a3ed2f2dbc2973c1a5dd838a9 target/tab/ctap2.tab +152f6a7d0eba3ce7e9577bc90d6cb7466cf1eacb5e257ae1d8d09794b504ab38 target/nrf52840_mdk_dfu_merged.hex +49d5b242ae2d63b0d243cea3271c2847d074a5fd15798c728c841126cdfe86ee target/tab/ctap2.tab diff --git a/reproducible/reference_binaries_ubuntu-18.04.sha256sum b/reproducible/reference_binaries_ubuntu-18.04.sha256sum index edb0e5d..9f5fd36 100644 --- a/reproducible/reference_binaries_ubuntu-18.04.sha256sum +++ b/reproducible/reference_binaries_ubuntu-18.04.sha256sum @@ -1,9 +1,9 @@ 2426ee9a6c75e325537818081d45445d95468a4c0a77feacdc6133d7d9aa227a third_party/tock/target/thumbv7em-none-eabi/release/nrf52840dk.bin -0038c7256f2bb8406364ca2deb8b228363e524a076263e23625fa2a3b4b35df6 target/nrf52840dk_merged.hex +f8c54ac3f2e153e3cf6d4651663fc12df1f3d61fefd40f953b3181c4d9e8b43c target/nrf52840dk_merged.hex c53d1e1db72df25950fa6d28699a2d38757def0dcbeb0d09d2366481cf0149a6 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle.bin -183365121eab69bb15746e7907bb0d66b9cc766d26a6a0dab301583c4bb8c4c3 target/nrf52840_dongle_merged.hex +1fff9a157e779d92245420053147dc8e894ea281f4de2a524ab6d815614b050a target/nrf52840_dongle_merged.hex 233b5ba4459523759e3171cee83cdb3a383bbe65727c8ece64dfe5321d6ebe34 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_dongle_dfu.bin -95be6da19f67a9a85e83d97bdaaf5f062c82da00b9a767dd0bc49eaa2c74bb26 target/nrf52840_dongle_dfu_merged.hex +5597a6c3c4a9b1bd72d0d377b084b17627301043724fd1e67fe7d87f0e2fab25 target/nrf52840_dongle_dfu_merged.hex 1baaf518a74c6077cb936d9cf178b6dd0232e7562fa56174886b05b77886cc32 third_party/tock/target/thumbv7em-none-eabi/release/nrf52840_mdk_dfu.bin -39c94b1dd8e65438d481835663c4ec2cda99311011031403c9244ed5095230c7 target/nrf52840_mdk_dfu_merged.hex -b02eb9439df1f8a3c21eb29f39c3b72c0f709b05a4e8a968441e73678cfb55df target/tab/ctap2.tab +53801f162b40c861ab2612c2972fe363889c7c7c313e6f80c1c68ce2dc8049fb target/nrf52840_mdk_dfu_merged.hex +d7146c0b30cc08a34531895c37420bdd36056ffcb5f537b92ee34876a42557a6 target/tab/ctap2.tab diff --git a/reproducible/reference_elf2tab_macos-10.15.txt b/reproducible/reference_elf2tab_macos-10.15.txt index 48959df..5432207 100644 --- a/reproducible/reference_elf2tab_macos-10.15.txt +++ b/reproducible/reference_elf2tab_macos-10.15.txt @@ -6,8 +6,8 @@ Min RAM size from segments in ELF: 20 bytes Number of writeable flash regions: 0 Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes. Entry point is in .text section - Adding .text section. Offset: 128 (0x80). Length: 187280 (0x2db90) bytes. - Adding .stack section. Offset: 187408 (0x2dc10). Length: 16384 (0x4000) bytes. + Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes. + Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes. Searching for .rel.X sections to add. TBF Header: version: 2 0x2 @@ -30,8 +30,8 @@ Min RAM size from segments in ELF: 20 bytes Number of writeable flash regions: 0 Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes. Entry point is in .text section - Adding .text section. Offset: 128 (0x80). Length: 187280 (0x2db90) bytes. - Adding .stack section. Offset: 187408 (0x2dc10). Length: 16384 (0x4000) bytes. + Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes. + Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes. Searching for .rel.X sections to add. TBF Header: version: 2 0x2 @@ -54,8 +54,8 @@ Min RAM size from segments in ELF: 20 bytes Number of writeable flash regions: 0 Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes. Entry point is in .text section - Adding .text section. Offset: 128 (0x80). Length: 187280 (0x2db90) bytes. - Adding .stack section. Offset: 187408 (0x2dc10). Length: 16384 (0x4000) bytes. + Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes. + Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes. Searching for .rel.X sections to add. TBF Header: version: 2 0x2 @@ -78,8 +78,8 @@ Min RAM size from segments in ELF: 20 bytes Number of writeable flash regions: 0 Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes. Entry point is in .text section - Adding .text section. Offset: 128 (0x80). Length: 187280 (0x2db90) bytes. - Adding .stack section. Offset: 187408 (0x2dc10). Length: 16384 (0x4000) bytes. + Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes. + Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes. Searching for .rel.X sections to add. TBF Header: version: 2 0x2 diff --git a/reproducible/reference_elf2tab_ubuntu-18.04.txt b/reproducible/reference_elf2tab_ubuntu-18.04.txt index 2b48605..5432207 100644 --- a/reproducible/reference_elf2tab_ubuntu-18.04.txt +++ b/reproducible/reference_elf2tab_ubuntu-18.04.txt @@ -6,8 +6,8 @@ Min RAM size from segments in ELF: 20 bytes Number of writeable flash regions: 0 Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes. Entry point is in .text section - Adding .text section. Offset: 128 (0x80). Length: 187264 (0x2db80) bytes. - Adding .stack section. Offset: 187392 (0x2dc00). Length: 16384 (0x4000) bytes. + Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes. + Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes. Searching for .rel.X sections to add. TBF Header: version: 2 0x2 @@ -30,8 +30,8 @@ Min RAM size from segments in ELF: 20 bytes Number of writeable flash regions: 0 Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes. Entry point is in .text section - Adding .text section. Offset: 128 (0x80). Length: 187264 (0x2db80) bytes. - Adding .stack section. Offset: 187392 (0x2dc00). Length: 16384 (0x4000) bytes. + Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes. + Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes. Searching for .rel.X sections to add. TBF Header: version: 2 0x2 @@ -54,8 +54,8 @@ Min RAM size from segments in ELF: 20 bytes Number of writeable flash regions: 0 Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes. Entry point is in .text section - Adding .text section. Offset: 128 (0x80). Length: 187264 (0x2db80) bytes. - Adding .stack section. Offset: 187392 (0x2dc00). Length: 16384 (0x4000) bytes. + Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes. + Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes. Searching for .rel.X sections to add. TBF Header: version: 2 0x2 @@ -78,8 +78,8 @@ Min RAM size from segments in ELF: 20 bytes Number of writeable flash regions: 0 Adding .crt0_header section. Offset: 64 (0x40). Length: 64 (0x40) bytes. Entry point is in .text section - Adding .text section. Offset: 128 (0x80). Length: 187264 (0x2db80) bytes. - Adding .stack section. Offset: 187392 (0x2dc00). Length: 16384 (0x4000) bytes. + Adding .text section. Offset: 128 (0x80). Length: 186976 (0x2da60) bytes. + Adding .stack section. Offset: 187104 (0x2dae0). Length: 16384 (0x4000) bytes. Searching for .rel.X sections to add. TBF Header: version: 2 0x2