@@ -20,22 +20,17 @@ use caliptra_common::keyids::{
2020 KEY_ID_DPE_CDI , KEY_ID_DPE_PRIV_KEY , KEY_ID_EXPORTED_DPE_CDI , KEY_ID_TMP ,
2121} ;
2222use caliptra_drivers:: {
23- cprintln , hmac384_kdf, Array4x12 , Ecc384 , Ecc384PrivKeyIn , Ecc384PubKey , Ecc384Scalar ,
24- Ecc384Seed , Hmac384 , Hmac384Data , Hmac384Key , Hmac384Tag , KeyId , KeyReadArgs , KeyUsage ,
25- KeyVault , KeyWriteArgs , Sha384 , Sha384DigestOp , Trng ,
23+ hmac384_kdf, Array4x12 , Ecc384 , Ecc384PrivKeyIn , Ecc384PubKey , Ecc384Scalar , Ecc384Seed ,
24+ ExportedCdiEntry , ExportedCdiHandles , Hmac384 , Hmac384Data , Hmac384Key , Hmac384Tag , KeyId ,
25+ KeyReadArgs , KeyUsage , KeyVault , KeyWriteArgs , Sha384 , Sha384DigestOp , Trng ,
2626} ;
2727use crypto:: { AlgLen , Crypto , CryptoBuf , CryptoError , Digest , EcdsaPub , EcdsaSig , Hasher } ;
2828use dpe:: {
29- response:: DpeErrorCode , x509:: MeasurementData , ExportedCdiHandle , MAX_EXPORTED_CDI_SIZE ,
29+ response:: DpeErrorCode , x509:: MeasurementData , ExportedCdiHandle , U8Bool , MAX_EXPORTED_CDI_SIZE ,
3030} ;
3131use zerocopy:: IntoBytes ;
3232use zeroize:: Zeroize ;
3333
34- // Currently only can export CDI once, but in the future we may want to support multiple exported
35- // CDI handles at the cost of using more KeyVault slots.
36- pub const EXPORTED_HANDLES_NUM : usize = 1 ;
37- pub type ExportedCdiHandles = [ Option < ( KeyId , ExportedCdiHandle ) > ; EXPORTED_HANDLES_NUM ] ;
38-
3934pub struct DpeCrypto < ' a > {
4035 sha384 : & ' a mut Sha384 ,
4136 trng : & ' a mut Trng ,
@@ -156,9 +151,13 @@ impl<'a> DpeCrypto<'a> {
156151 & mut self ,
157152 exported_cdi_handle : & [ u8 ; MAX_EXPORTED_CDI_SIZE ] ,
158153 ) -> Option < <DpeCrypto < ' a > as crypto:: Crypto >:: Cdi > {
159- for cdi_slot in self . exported_cdi_slots . iter ( ) {
154+ for cdi_slot in self . exported_cdi_slots . entries . iter ( ) {
160155 match cdi_slot {
161- Some ( ( cdi, handle) ) if handle == exported_cdi_handle => return Some ( * cdi) ,
156+ ExportedCdiEntry {
157+ key,
158+ handle,
159+ active,
160+ } if active. get ( ) && handle == exported_cdi_handle => return Some ( * key) ,
162161 _ => ( ) ,
163162 }
164163 }
@@ -243,18 +242,30 @@ impl<'a> Crypto for DpeCrypto<'a> {
243242 // Currently we only use one slot for export CDIs.
244243 let cdi_slot = KEY_ID_EXPORTED_DPE_CDI ;
245244 // Copy the CDI slots to work around the borrow checker.
246- let mut slots_clone = * self . exported_cdi_slots ;
245+ let mut slots_clone = self . exported_cdi_slots . clone ( ) ;
247246
248- for slot in slots_clone. iter_mut ( ) {
247+ for slot in slots_clone. entries . iter_mut ( ) {
249248 match slot {
250249 // Matching existing slot
251- Some ( ( cached_cdi, handle) ) if * cached_cdi == cdi_slot => {
250+ ExportedCdiEntry {
251+ key,
252+ handle,
253+ active,
254+ } if active. get ( ) && * key == cdi_slot => {
252255 Err ( CryptoError :: ExportedCdiHandleDuplicateCdi ) ?
253256 }
254- // Empty slot
255- None => {
257+ ExportedCdiEntry {
258+ key,
259+ handle,
260+ active,
261+ } if !active. get ( ) => {
262+ // Empty slot
256263 let cdi = self . derive_cdi_inner ( algs, measurement, info, cdi_slot) ?;
257- * slot = Some ( ( cdi, exported_cdi_handle) ) ;
264+ * slot = ExportedCdiEntry {
265+ key : cdi,
266+ handle : exported_cdi_handle,
267+ active : U8Bool :: new ( true ) ,
268+ } ;
258269 // We need to update `self.exported_cdi_slots` with our mutation.
259270 * self . exported_cdi_slots = slots_clone;
260271 return Ok ( exported_cdi_handle) ;
@@ -298,10 +309,14 @@ impl<'a> Crypto for DpeCrypto<'a> {
298309 ) -> Result < ( Self :: PrivKey , EcdsaPub ) , CryptoError > {
299310 let cdi = {
300311 let mut cdi = None ;
301- for cdi_slot in self . exported_cdi_slots . iter ( ) {
312+ for cdi_slot in self . exported_cdi_slots . entries . iter ( ) {
302313 match cdi_slot {
303- Some ( ( stored_cdi, stored_handle) ) if stored_handle == exported_handle => {
304- cdi = Some ( * stored_cdi) ;
314+ ExportedCdiEntry {
315+ key,
316+ handle,
317+ active,
318+ } if active. get ( ) && handle == exported_handle => {
319+ cdi = Some ( * key) ;
305320 break ;
306321 }
307322 _ => ( ) ,
0 commit comments