[dice] Add trait DiceArtifacts to access BCC and CDI values

The added DiceArtifacts is implemented for both BccHandover and
OwnedDiceArtifacts. This allows us to access the dice artifacts in
an easy and uniform way.

[u8; CDI_SIZE] is used instead of the alias Cdi to facilitate a
future conversion from Cdi alias to Cdi type so that we can have
more controle over the memory security of CDI. More context in
b/268587826.

Bug: 267575445
Test: atest diced_utils_test diced_sample_inputs_test \
diced_vendor_test diced_open_dice_cbor_test \
libdiced_open_dice_nostd.integration_test \
libdiced_open_dice.integration_test diced_open_dice_cbor_test

Change-Id: Iabb87ad18f2f4a4d46283da045eb21a5776ad4b4
diff --git a/diced/open_dice/src/bcc.rs b/diced/open_dice/src/bcc.rs
index f343bc5..1575113 100644
--- a/diced/open_dice/src/bcc.rs
+++ b/diced/open_dice/src/bcc.rs
@@ -14,7 +14,7 @@
 
 //! This module mirrors the content in open-dice/include/dice/android/bcc.h
 
-use crate::dice::{Cdi, CdiValues, InputValues, CDI_SIZE};
+use crate::dice::{Cdi, CdiValues, DiceArtifacts, InputValues, CDI_SIZE};
 use crate::error::{check_result, DiceError, Result};
 use open_dice_bcc_bindgen::{
     BccConfigValues, BccFormatConfigDescriptor, BccHandoverMainFlow, BccHandoverParse, BccMainFlow,
@@ -127,11 +127,25 @@
 #[derive(Debug)]
 pub struct BccHandover<'a> {
     /// Attestation CDI.
-    pub cdi_attest: &'a Cdi,
+    cdi_attest: &'a [u8; CDI_SIZE],
     /// Sealing CDI.
-    pub cdi_seal: &'a Cdi,
+    cdi_seal: &'a [u8; CDI_SIZE],
     /// Boot Certificate Chain.
-    pub bcc: Option<&'a [u8]>,
+    bcc: Option<&'a [u8]>,
+}
+
+impl<'a> DiceArtifacts for BccHandover<'a> {
+    fn cdi_attest(&self) -> &[u8; CDI_SIZE] {
+        self.cdi_attest
+    }
+
+    fn cdi_seal(&self) -> &[u8; CDI_SIZE] {
+        self.cdi_seal
+    }
+
+    fn bcc(&self) -> Option<&[u8]> {
+        self.bcc
+    }
 }
 
 /// A BCC handover combines the BCC and CDIs in a single CBOR object.
diff --git a/diced/open_dice/src/dice.rs b/diced/open_dice/src/dice.rs
index 350237d..f5e7f71 100644
--- a/diced/open_dice/src/dice.rs
+++ b/diced/open_dice/src/dice.rs
@@ -62,13 +62,31 @@
 /// Array type of DICE ID.
 pub type DiceId = [u8; ID_SIZE];
 
+/// A trait for types that represent Dice artifacts, which include:
+///
+/// - Attestation CDI
+/// - Sealing CDI
+/// - Boot Certificate Chain
+///
+/// Types that implement this trait provide an access these artifacts.
+pub trait DiceArtifacts {
+    /// Returns a reference to the attestation CDI.
+    fn cdi_attest(&self) -> &[u8; CDI_SIZE];
+
+    /// Returns a reference to the sealing CDI.
+    fn cdi_seal(&self) -> &[u8; CDI_SIZE];
+
+    /// Returns a reference to the Boot Certificate Chain, if present.
+    fn bcc(&self) -> Option<&[u8]>;
+}
+
 /// CDI Values.
 #[derive(Debug, Zeroize, ZeroizeOnDrop, Default)]
 pub struct CdiValues {
     /// Attestation CDI.
-    pub cdi_attest: Cdi,
+    pub cdi_attest: [u8; CDI_SIZE],
     /// Sealing CDI.
-    pub cdi_seal: Cdi,
+    pub cdi_seal: [u8; CDI_SIZE],
 }
 
 /// Configuration descriptor for DICE input values.
diff --git a/diced/open_dice/src/lib.rs b/diced/open_dice/src/lib.rs
index c10626b..925b5e8 100644
--- a/diced/open_dice/src/lib.rs
+++ b/diced/open_dice/src/lib.rs
@@ -33,8 +33,8 @@
 };
 pub use dice::{
     derive_cdi_certificate_id, derive_cdi_private_key_seed, dice_main_flow, Cdi, CdiValues, Config,
-    DiceMode, Hash, Hidden, InlineConfig, InputValues, PrivateKeySeed, CDI_SIZE, HASH_SIZE,
-    HIDDEN_SIZE, ID_SIZE, PRIVATE_KEY_SEED_SIZE,
+    DiceArtifacts, DiceMode, Hash, Hidden, InlineConfig, InputValues, PrivateKeySeed, CDI_SIZE,
+    HASH_SIZE, HIDDEN_SIZE, ID_SIZE, PRIVATE_KEY_SEED_SIZE,
 };
 pub use error::{check_result, DiceError, Result};
 pub use ops::{generate_certificate, hash, kdf, sign, verify};
diff --git a/diced/open_dice/src/retry.rs b/diced/open_dice/src/retry.rs
index b7c1a71..76a214c 100644
--- a/diced/open_dice/src/retry.rs
+++ b/diced/open_dice/src/retry.rs
@@ -18,7 +18,9 @@
 //! std environment.
 
 use crate::bcc::{bcc_format_config_descriptor, bcc_main_flow};
-use crate::dice::{dice_main_flow, Cdi, CdiValues, InputValues, PRIVATE_KEY_SEED_SIZE};
+use crate::dice::{
+    dice_main_flow, Cdi, CdiValues, DiceArtifacts, InputValues, CDI_SIZE, PRIVATE_KEY_SEED_SIZE,
+};
 use crate::error::{DiceError, Result};
 use crate::ops::generate_certificate;
 use std::ffi::CStr;
@@ -30,9 +32,23 @@
 #[derive(Debug)]
 pub struct OwnedDiceArtifacts {
     /// CDI Values.
-    pub cdi_values: CdiValues,
+    cdi_values: CdiValues,
     /// Boot Certificate Chain.
-    pub bcc: Vec<u8>,
+    bcc: Vec<u8>,
+}
+
+impl DiceArtifacts for OwnedDiceArtifacts {
+    fn cdi_attest(&self) -> &[u8; CDI_SIZE] {
+        &self.cdi_values.cdi_attest
+    }
+
+    fn cdi_seal(&self) -> &[u8; CDI_SIZE] {
+        &self.cdi_values.cdi_seal
+    }
+
+    fn bcc(&self) -> Option<&[u8]> {
+        Some(&self.bcc)
+    }
 }
 
 /// Retries the given function with bigger output buffer size.