@@ -12,7 +12,7 @@ use caliptra_builder::{
1212 firmware:: { APP_WITH_UART , FMC_WITH_UART } ,
1313 ImageOptions ,
1414} ;
15- use caliptra_common:: mailbox_api:: { InvokeDpeReq , MailboxReq , MailboxReqHeader } ;
15+ use caliptra_common:: mailbox_api:: { InvokeDpeReq , InvokeDpeResp , MailboxReq , MailboxReqHeader } ;
1616use caliptra_drivers:: CaliptraError ;
1717use caliptra_hw_model:: { HwModel , ModelError } ;
1818use caliptra_runtime:: { RtBootStatus , DPE_SUPPORT , VENDOR_ID , VENDOR_SKU } ;
@@ -23,9 +23,9 @@ use cms::{
2323} ;
2424use dpe:: {
2525 commands:: {
26- CertifyKeyCommand , CertifyKeyFlags , CertifyKeyP384Cmd , Command , DeriveContextCmd ,
27- DeriveContextFlags , GetCertificateChainCmd , GetProfileCmd , InitCtxCmd , RotateCtxCmd ,
28- RotateCtxFlags , SignFlags , SignP384Cmd ,
26+ CertifyKeyCommand , CertifyKeyFlags , CertifyKeyP384Cmd , Command , CommandHdr ,
27+ DeriveContextCmd , DeriveContextFlags , GetCertificateChainCmd , GetProfileCmd , InitCtxCmd ,
28+ RotateCtxCmd , RotateCtxFlags , SignFlags , SignP384Cmd ,
2929 } ,
3030 context:: ContextHandle ,
3131 error:: DpeErrorCode ,
@@ -532,3 +532,62 @@ fn test_certify_key_with_max_contexts() {
532532 ) ;
533533 }
534534}
535+
536+ #[ test]
537+ fn test_invoke_dpe_derive_context_without_svn ( ) {
538+ let mut model = run_rt_test ( RuntimeTestArgs :: default ( ) ) ;
539+
540+ model. step_until ( |m| {
541+ m. soc_ifc ( ) . cptra_boot_status ( ) . read ( ) == u32:: from ( RtBootStatus :: RtReadyForCommands )
542+ } ) ;
543+
544+ let derive_ctx_cmd = DeriveContextCmd {
545+ handle : ContextHandle :: default ( ) ,
546+ data : TciMeasurement ( [ 0 ; TCI_SIZE ] ) ,
547+ flags : DeriveContextFlags :: EXPORT_CDI | DeriveContextFlags :: CREATE_CERTIFICATE ,
548+ tci_type : 0 ,
549+ target_locality : 0 ,
550+ svn : 0 ,
551+ } ;
552+
553+ let mut cmd_data = [ 0u8 ; InvokeDpeReq :: DATA_MAX_SIZE ] ;
554+ let cmd_hdr = CommandHdr :: new ( DpeProfile :: P384Sha384 , Command :: DERIVE_CONTEXT ) ;
555+ let cmd_hdr_buf = cmd_hdr. as_bytes ( ) ;
556+ cmd_data[ ..cmd_hdr_buf. len ( ) ] . copy_from_slice ( cmd_hdr_buf) ;
557+ let dpe_cmd_buf = derive_ctx_cmd. as_bytes ( ) ;
558+ // Strip the last 4 bytes (svn u32 field) to simulate caller without SVN
559+ let dpe_cmd_buf_no_svn = & dpe_cmd_buf[ ..dpe_cmd_buf. len ( ) - 4 ] ;
560+ cmd_data[ cmd_hdr_buf. len ( ) ..cmd_hdr_buf. len ( ) + dpe_cmd_buf_no_svn. len ( ) ]
561+ . copy_from_slice ( dpe_cmd_buf_no_svn) ;
562+ let mut mbox_cmd = MailboxReq :: InvokeDpeCommand ( InvokeDpeReq {
563+ hdr : MailboxReqHeader { chksum : 0 } ,
564+ data : cmd_data,
565+ data_size : ( cmd_hdr_buf. len ( ) + dpe_cmd_buf_no_svn. len ( ) ) as u32 ,
566+ } ) ;
567+ mbox_cmd. populate_chksum ( ) . unwrap ( ) ;
568+
569+ let resp = model. mailbox_execute (
570+ u32:: from ( CommandId :: INVOKE_DPE ) ,
571+ mbox_cmd. as_bytes ( ) . unwrap ( ) ,
572+ ) ;
573+ let resp = resp. unwrap ( ) . expect ( "We should have received a response" ) ;
574+
575+ assert ! ( resp. len( ) <= std:: mem:: size_of:: <InvokeDpeResp >( ) ) ;
576+ let mut resp_hdr = InvokeDpeResp :: default ( ) ;
577+ resp_hdr. as_mut_bytes ( ) [ ..resp. len ( ) ] . copy_from_slice ( & resp) ;
578+
579+ assert ! ( caliptra_common:: checksum:: verify_checksum(
580+ resp_hdr. hdr. chksum,
581+ 0x0 ,
582+ & resp[ core:: mem:: size_of_val( & resp_hdr. hdr. chksum) ..] ,
583+ ) ) ;
584+
585+ let resp_bytes = & resp_hdr. data [ ..resp_hdr. data_size as usize ] ;
586+ let parsed_resp =
587+ Response :: try_read_from_bytes ( & Command :: DeriveContext ( & derive_ctx_cmd) , resp_bytes)
588+ . unwrap ( ) ;
589+
590+ let Response :: DeriveContextExportedCdi ( _) = parsed_resp else {
591+ panic ! ( "expected derive context exported cdi resp!" ) ;
592+ } ;
593+ }
0 commit comments