Skip to content

Commit

Permalink
Move common helper code into the test helper library.
Browse files Browse the repository at this point in the history
  • Loading branch information
bluegate010 authored and jhand2 committed Dec 4, 2024
1 parent 6dc66ed commit cabaa7d
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 124 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

17 changes: 2 additions & 15 deletions rom/dev/tests/rom_integration_tests/test_warm_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ use caliptra_common::RomBootStatus::*;
use caliptra_drivers::CaliptraError;
use caliptra_hw_model::DeviceLifecycle;
use caliptra_hw_model::{BootParams, Fuses, HwModel, InitParams, SecurityState};
use caliptra_test::swap_word_bytes_inplace;
use openssl::sha::sha384;
use zerocopy::AsBytes;
use caliptra_test::image_pk_desc_hash;

use crate::helpers;

fn bytes_to_be_words_48(buf: &[u8; 48]) -> [u32; 12] {
let mut result: [u32; 12] = zerocopy::transmute!(*buf);
swap_word_bytes_inplace(&mut result);
result
}

#[test]
fn test_warm_reset_success() {
let security_state = *SecurityState::default()
Expand All @@ -38,13 +30,8 @@ fn test_warm_reset_success() {
},
)
.unwrap();
let vendor_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.vendor_pub_key_info.as_bytes(),
));

let owner_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.owner_pub_key_info.as_bytes(),
));
let (vendor_pk_desc_hash, owner_pk_desc_hash) = image_pk_desc_hash(&image.manifest);

let mut hw = caliptra_hw_model::new(
InitParams {
Expand Down
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ caliptra-image-gen.workspace = true
caliptra-image-crypto.workspace = true
caliptra-auth-man-gen.workspace = true
caliptra-image-serde.workspace = true
caliptra-test.workspace = true
caliptra-cfi-lib-git = { workspace = true, features = ["cfi-test"] }
openssl.workspace = true
sha2 = { version = "0.10.2", default-features = false, features = ["compress"] }
Expand Down
31 changes: 5 additions & 26 deletions runtime/tests/runtime_integration_tests/test_warm_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,8 @@ use caliptra_builder::{
use caliptra_error::CaliptraError;
use caliptra_hw_model::{BootParams, DeviceLifecycle, Fuses, HwModel, InitParams, SecurityState};
use caliptra_registers::mbox::enums::MboxStatusE;
use caliptra_test::image_pk_desc_hash;
use dpe::DPE_PROFILE;
use openssl::sha::sha384;
use zerocopy::AsBytes;

fn swap_word_bytes_inplace(words: &mut [u32]) {
for word in words.iter_mut() {
*word = word.swap_bytes()
}
}

fn bytes_to_be_words_48(buf: &[u8; 48]) -> [u32; 12] {
let mut result: [u32; 12] = zerocopy::transmute!(*buf);
swap_word_bytes_inplace(&mut result);
result
}

#[test]
fn test_rt_journey_pcr_validation() {
Expand All @@ -40,12 +27,8 @@ fn test_rt_journey_pcr_validation() {
},
)
.unwrap();
let vendor_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.vendor_pub_key_info.as_bytes(),
));
let owner_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.owner_pub_key_info.as_bytes(),
));

let (vendor_pk_desc_hash, owner_pk_desc_hash) = image_pk_desc_hash(&image.manifest);

let mut model = caliptra_hw_model::new(
InitParams {
Expand Down Expand Up @@ -107,12 +90,8 @@ fn test_mbox_busy_during_warm_reset() {
},
)
.unwrap();
let vendor_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.vendor_pub_key_info.as_bytes(),
));
let owner_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.owner_pub_key_info.as_bytes(),
));

let (vendor_pk_desc_hash, owner_pk_desc_hash) = image_pk_desc_hash(&image.manifest);

let mut model = caliptra_hw_model::new(
InitParams {
Expand Down
20 changes: 20 additions & 0 deletions test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ use caliptra_builder::{
FwId, ImageOptions,
};
use caliptra_hw_model::{BootParams, DefaultHwModel, HwModel, InitParams};
use zerocopy::AsBytes;

pub mod crypto;
pub mod derive;
mod redact;
mod unwrap_single;
pub mod x509;

use caliptra_image_types::ImageManifest;
use openssl::sha::sha384;
pub use redact::{redact_cert, RedactOpts};
pub use unwrap_single::UnwrapSingle;

Expand All @@ -28,6 +31,23 @@ pub fn swap_word_bytes_inplace(words: &mut [u32]) {
}
}

pub fn bytes_to_be_words_48(buf: &[u8; 48]) -> [u32; 12] {
let mut result: [u32; 12] = zerocopy::transmute!(*buf);
swap_word_bytes_inplace(&mut result);
result
}

// Returns the vendor and owner public key descriptor hashes from the image.
pub fn image_pk_desc_hash(manifest: &ImageManifest) -> ([u32; 12], [u32; 12]) {
let vendor_pk_desc_hash =
bytes_to_be_words_48(&sha384(manifest.preamble.vendor_pub_key_info.as_bytes()));

let owner_pk_desc_hash =
bytes_to_be_words_48(&sha384(manifest.preamble.owner_pub_key_info.as_bytes()));

(vendor_pk_desc_hash, owner_pk_desc_hash)
}

// Run a test which boots ROM -> FMC -> test_bin. If test_bin_name is None,
// run the production runtime image.
pub fn run_test(
Expand Down
15 changes: 2 additions & 13 deletions test/tests/caliptra_integration_tests/fake_collateral_boot_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use caliptra_common::mailbox_api::{
use caliptra_hw_model::{BootParams, HwModel, InitParams};
use caliptra_test::{
derive::{DoeInput, DoeOutput, LDevId},
swap_word_bytes, swap_word_bytes_inplace,
image_pk_desc_hash, swap_word_bytes,
x509::{DiceFwid, DiceTcbInfo},
};
use openssl::sha::sha384;
Expand Down Expand Up @@ -42,12 +42,6 @@ fn get_idevid_pubkey() -> openssl::pkey::PKey<openssl::pkey::Public> {
csr.public_key().unwrap()
}

fn bytes_to_be_words_48(buf: &[u8; 48]) -> [u32; 12] {
let mut result: [u32; 12] = zerocopy::transmute!(*buf);
swap_word_bytes_inplace(&mut result);
result
}

// [CAP2][TODO] This test is disabled because it needs to be updated.
//#[test]
fn fake_boot_test() {
Expand All @@ -64,13 +58,8 @@ fn fake_boot_test() {
},
)
.unwrap();
let vendor_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.vendor_pub_key_info.as_bytes(),
));

let owner_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.owner_pub_key_info.as_bytes(),
));
let (vendor_pk_desc_hash, owner_pk_desc_hash) = image_pk_desc_hash(&image.manifest);

let mut hw = caliptra_hw_model::new(
InitParams {
Expand Down
20 changes: 5 additions & 15 deletions test/tests/caliptra_integration_tests/jtag_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@ use caliptra_builder::{firmware, get_elf_path, ImageOptions};

use caliptra_api_types::DeviceLifecycle;
use caliptra_hw_model::{BootParams, Fuses, HwModel, InitParams, SecurityState};
use caliptra_test::swap_word_bytes_inplace;
use openssl::sha::sha384;
use caliptra_test::image_pk_desc_hash;
use std::io::{BufRead, BufReader, Write};
use std::process::{ChildStdin, Command, Stdio};
use zerocopy::AsBytes;

fn bytes_to_be_words_48(buf: &[u8; 48]) -> [u32; 12] {
let mut result: [u32; 12] = zerocopy::transmute!(*buf);
swap_word_bytes_inplace(&mut result);
result
}

#[derive(PartialEq, Debug)]
enum RegAccess {
Expand Down Expand Up @@ -95,14 +87,12 @@ fn gdb_test() {
},
)
.unwrap();
let vendor_pk_desc_hash = sha384(image.manifest.preamble.vendor_pub_key_info.as_bytes());
let owner_pk_desc_hash = sha384(image.manifest.preamble.owner_pub_key_info.as_bytes());
let vendor_pk_desc_hash_words = bytes_to_be_words_48(&vendor_pk_desc_hash);
let owner_pk_desc_hash_words = bytes_to_be_words_48(&owner_pk_desc_hash);

let (vendor_pk_desc_hash, owner_pk_desc_hash) = image_pk_desc_hash(&image.manifest);

let fuses = Fuses {
key_manifest_pk_hash: vendor_pk_desc_hash_words,
owner_pk_hash: owner_pk_desc_hash_words,
key_manifest_pk_hash: vendor_pk_desc_hash,
owner_pk_hash: owner_pk_desc_hash,
fmc_key_manifest_svn: 0b1111111,
lms_verify: true,
..Default::default()
Expand Down
11 changes: 3 additions & 8 deletions test/tests/caliptra_integration_tests/smoke_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ use caliptra_drivers::CaliptraError;
use caliptra_hw_model::{BootParams, HwModel, InitParams, SecurityState};
use caliptra_hw_model_types::{RandomEtrngResponses, RandomNibbles};
use caliptra_test::derive::{PcrRtCurrentInput, RtAliasKey};
use caliptra_test::{derive, redact_cert, run_test, RedactOpts, UnwrapSingle};
use caliptra_test::{
bytes_to_be_words_48,
derive::{DoeInput, DoeOutput, FmcAliasKey, IDevId, LDevId, Pcr0, Pcr0Input},
swap_word_bytes, swap_word_bytes_inplace,
swap_word_bytes,
x509::{DiceFwid, DiceTcbInfo},
};
use caliptra_test::{derive, redact_cert, run_test, RedactOpts, UnwrapSingle};
use openssl::nid::Nid;
use openssl::sha::{sha384, Sha384};
use rand::rngs::StdRng;
Expand Down Expand Up @@ -134,12 +135,6 @@ fn test_golden_ldevid_pubkey_matches_generated() {
.public_eq(&ldevid_pubkey));
}

fn bytes_to_be_words_48(buf: &[u8; 48]) -> [u32; 12] {
let mut result: [u32; 12] = zerocopy::transmute!(*buf);
swap_word_bytes_inplace(&mut result);
result
}

#[test]
fn smoke_test() {
let security_state = *SecurityState::default()
Expand Down
26 changes: 5 additions & 21 deletions test/tests/caliptra_integration_tests/warm_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@ use caliptra_builder::{
};
use caliptra_common::mailbox_api::CommandId;
use caliptra_hw_model::{mbox_write_fifo, BootParams, HwModel, InitParams, SecurityState};
use caliptra_test::swap_word_bytes_inplace;
use openssl::sha::sha384;
use zerocopy::AsBytes;

fn bytes_to_be_words_48(buf: &[u8; 48]) -> [u32; 12] {
let mut result: [u32; 12] = zerocopy::transmute!(*buf);
swap_word_bytes_inplace(&mut result);
result
}
use caliptra_test::image_pk_desc_hash;

#[test]
fn warm_reset_basic() {
Expand All @@ -35,12 +27,8 @@ fn warm_reset_basic() {
},
)
.unwrap();
let vendor_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.vendor_pub_key_info.as_bytes(),
));
let owner_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.owner_pub_key_info.as_bytes(),
));

let (vendor_pk_desc_hash, owner_pk_desc_hash) = image_pk_desc_hash(&image.manifest);

let mut hw = caliptra_hw_model::new(
InitParams {
Expand Down Expand Up @@ -99,12 +87,8 @@ fn warm_reset_during_fw_load() {
},
)
.unwrap();
let vendor_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.vendor_pub_key_info.as_bytes(),
));
let owner_pk_desc_hash = bytes_to_be_words_48(&sha384(
image.manifest.preamble.owner_pub_key_info.as_bytes(),
));

let (vendor_pk_desc_hash, owner_pk_desc_hash) = image_pk_desc_hash(&image.manifest);

let mut hw = caliptra_hw_model::new(
InitParams {
Expand Down
7 changes: 0 additions & 7 deletions test/tests/fips_test_suite/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use caliptra_builder::{version, ImageOptions};
use caliptra_common::mailbox_api::*;
use caliptra_drivers::FipsTestHook;
use caliptra_hw_model::{BootParams, DefaultHwModel, HwModel, InitParams, ModelError, ShaAccMode};
use caliptra_test::swap_word_bytes_inplace;
use dpe::{
commands::*,
response::{
Expand Down Expand Up @@ -423,12 +422,6 @@ pub fn verify_output_inhibited<T: HwModel>(hw: &mut T) {
verify_sha_engine_output_inhibited(hw);
}

pub fn bytes_to_be_words_48(buf: &[u8; 48]) -> [u32; 12] {
let mut result: [u32; 12] = zerocopy::transmute!(*buf);
swap_word_bytes_inplace(&mut result);
result
}

pub fn hook_code_read<T: HwModel>(hw: &mut T) -> u8 {
((hw.soc_ifc().cptra_dbg_manuf_service_reg().read() & HOOK_CODE_MASK) >> HOOK_CODE_OFFSET) as u8
}
Expand Down
23 changes: 4 additions & 19 deletions test/tests/fips_test_suite/fw_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use caliptra_image_types::SHA384_DIGEST_WORD_SIZE;
use caliptra_image_types::{
FwImageType, ImageBundle, VENDOR_ECC_MAX_KEY_COUNT, VENDOR_LMS_MAX_KEY_COUNT,
};
use openssl::sha::sha384;
use caliptra_test::image_pk_desc_hash;

use common::*;
use zerocopy::AsBytes;
Expand Down Expand Up @@ -1195,27 +1195,12 @@ fn fw_load_bad_pub_key_flow(fw_image: ImageBundle, exp_error_code: u32) {
// Generate pub key hashes and set fuses
// Use a fresh image (will NOT be loaded)
let pk_hash_src_image = build_fw_image(ImageOptions::default());
let vendor_pk_desc_hash = sha384(
pk_hash_src_image
.manifest
.preamble
.vendor_pub_key_info
.as_bytes(),
);
let owner_pk_desc_hash = sha384(
pk_hash_src_image
.manifest
.preamble
.owner_pub_key_info
.as_bytes(),
);
let vendor_pk_desc_hash_words = bytes_to_be_words_48(&vendor_pk_desc_hash);
let owner_pk_desc_hash_words = bytes_to_be_words_48(&owner_pk_desc_hash);
let (vendor_pk_desc_hash, owner_pk_desc_hash) = image_pk_desc_hash(&pk_hash_src_image.manifest);

let fuses = Fuses {
life_cycle: DeviceLifecycle::Production,
key_manifest_pk_hash: vendor_pk_desc_hash_words,
owner_pk_hash: owner_pk_desc_hash_words,
key_manifest_pk_hash: vendor_pk_desc_hash,
owner_pk_hash: owner_pk_desc_hash,
lms_verify: true,
..Default::default()
};
Expand Down

0 comments on commit cabaa7d

Please sign in to comment.