-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new library crate with structures exposed by attest task / API
- Loading branch information
Showing
4 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "attest" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
hubpack.workspace = true | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_with = { workspace = true, features = ["macros"] } | ||
sha3.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#![cfg_attr(not(any(test, feature = "std")), no_std)] | ||
|
||
use hubpack::SerializedSize; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_with::serde_as; | ||
use sha3::{ | ||
digest::{core_api::OutputSizeUser, typenum::Unsigned}, | ||
Sha3_256Core, | ||
}; | ||
|
||
/// ArrayBuf is the type we use as a base for types that are constant sized | ||
/// byte buffers. | ||
#[serde_as] | ||
#[derive( | ||
Clone, Copy, Debug, Deserialize, PartialEq, Serialize, SerializedSize, | ||
)] | ||
pub struct ArrayBuf<const N: usize>(#[serde_as(as = "[_; N]")] pub [u8; N]); | ||
|
||
impl<const N: usize> Default for ArrayBuf<N> { | ||
fn default() -> Self { | ||
Self([0u8; N]) | ||
} | ||
} | ||
|
||
impl<const N: usize> From<[u8; N]> for ArrayBuf<N> { | ||
fn from(item: [u8; N]) -> Self { | ||
Self(item) | ||
} | ||
} | ||
|
||
pub const SHA3_256_DIGEST_SIZE: usize = | ||
<Sha3_256Core as OutputSizeUser>::OutputSize::USIZE; | ||
|
||
pub type Sha3_256Digest = ArrayBuf<SHA3_256_DIGEST_SIZE>; | ||
|
||
/// Measurement is an enum that can hold any of the hash algorithms that we support | ||
#[derive( | ||
Clone, Copy, Debug, Deserialize, PartialEq, Serialize, SerializedSize, | ||
)] | ||
pub enum Measurement { | ||
Sha3_256(Sha3_256Digest), | ||
} | ||
|
||
impl Default for Measurement { | ||
fn default() -> Self { | ||
Measurement::Sha3_256(Sha3_256Digest::default()) | ||
} | ||
} | ||
|
||
/// Log is the collection of measurements recorded | ||
#[serde_as] | ||
#[derive(Serialize, SerializedSize)] | ||
pub struct Log<const N: usize> { | ||
index: u32, | ||
#[serde_as(as = "[_; N]")] | ||
measurements: [Measurement; N], | ||
} | ||
|
||
impl<const N: usize> Log<N> { | ||
pub fn is_full(&self) -> bool { | ||
self.index as usize == N | ||
} | ||
|
||
pub fn push(&mut self, measurement: Measurement) -> bool { | ||
if !self.is_full() { | ||
self.measurements[self.index as usize] = measurement; | ||
self.index += 1; | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
|
||
impl<const N: usize> Default for Log<N> { | ||
fn default() -> Self { | ||
Self { | ||
index: 0, | ||
measurements: [Measurement::default(); N], | ||
} | ||
} | ||
} |