Skip to content

Commit

Permalink
Add new library crate with structures exposed by attest task / API
Browse files Browse the repository at this point in the history
  • Loading branch information
flihp committed Dec 3, 2023
1 parent 8f4cd75 commit 9b9e57b
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 1 deletion.
79 changes: 79 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]

members = [
"attest",
"dice-cert-check",
"dice-cert-tmpl",
"dice-mfg",
Expand Down Expand Up @@ -31,9 +32,10 @@ salty = { version = "0.3", default-features = false }
serde = { version = "1", default-features = false }
serde-big-array = "0.5"
serde_json = { version = "1", features = ["std", "alloc"] }
serde_with = { version = "3.3", default-features = false }
serialport = { git = "https://github.com/jgallagher/serialport-rs", branch = "illumos-support" }
sha2 = "0.10"
sha3 = "0.10"
sha3 = { version = "0.10", default-features = false }
string-error = "0.1"
tempfile = { version = "3", default-features = false }
thiserror = "1.0.50"
Expand Down
10 changes: 10 additions & 0 deletions attest/Cargo.toml
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
86 changes: 86 additions & 0 deletions attest/src/lib.rs
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],
}
}
}

0 comments on commit 9b9e57b

Please sign in to comment.