@@ -30,10 +30,22 @@ use serde::{Serialize, Deserialize};
3030
3131#[ cfg( all( target_env = "sgx" , feature = "sgxstd" ) ) ]
3232use std:: os:: fortanix_sgx:: arch;
33+
3334#[ cfg( all( target_env = "sgx" , not( feature = "sgxstd" ) ) ) ]
3435mod arch;
35- use core:: { convert:: TryFrom , num:: TryFromIntError , slice} ;
3636
37+ // Compatibility layer before the `EVERIFYREPORT2` is upstreamed
38+ #[ cfg( all( target_env = "sgx" , feature = "sgxstd" ) ) ]
39+ #[ path ="arch.rs" ]
40+ mod non_std_arch;
41+
42+ #[ cfg( all( target_env = "sgx" , feature = "sgxstd" ) ) ]
43+ use non_std_arch:: { Align256 , everifyreport2} ;
44+
45+ #[ cfg( all( target_env = "sgx" , not( feature = "sgxstd" ) ) ) ]
46+ use arch:: { Align256 , everifyreport2} ;
47+
48+ use core:: { convert:: TryFrom , num:: TryFromIntError , slice} ;
3749
3850#[ cfg( feature = "serde" ) ]
3951mod array_64 {
@@ -305,6 +317,7 @@ pub enum ErrorCode {
305317 PageAttributesMismatch = 19 ,
306318 PageNotModifiable = 20 ,
307319 PageNotDebuggable = 21 ,
320+ InvalidReportMacStruct = 28 ,
308321 InvalidCpusvn = 32 ,
309322 InvalidIsvsvn = 64 ,
310323 UnmaskedEvent = 128 ,
@@ -702,7 +715,7 @@ impl Report {
702715 /// implementation of the verifying function.
703716 ///
704717 /// Care should be taken that `check_mac` prevents timing attacks,
705- /// in particular that the comparison happens in constant time.
718+ /// in particular that the comparison happens in constant time.
706719 #[ cfg( target_env = "sgx" ) ]
707720 pub fn verify < F , R > ( & self , check_mac : F ) -> R
708721 where
@@ -808,6 +821,100 @@ impl Default for Keypolicy {
808821 }
809822}
810823
824+ struct_def ! {
825+ /// Rust definition of `REPORTTYPE` from `REPORTMACSTRUCT`.
826+ ///
827+ /// Ref: Intel® Trust Domain CPU Architectural Extensions, table 2-4.
828+ /// Version: 343754-002US, MAY 2021
829+ /// Link: <https://cdrdv2.intel.com/v1/dl/getContent/733582>
830+ #[ repr( C , align( 4 ) ) ]
831+ #[ derive( Clone , Debug , Default , Eq , PartialEq ) ]
832+ pub struct TeeReportType {
833+ /// Trusted Execution Environment(TEE) type:
834+ /// 0x00: SGX Legacy REPORT TYPE
835+ /// 0x7F-0x01: Reserved
836+ /// 0x80: Reserved
837+ /// 0x81: TEE Report type 2
838+ /// 0xFF-0x82: Reserved
839+ pub report_type: u8 ,
840+ /// TYPE-specific subtype, Stage1: value is 0
841+ pub subtype: u8 ,
842+ /// TYPE-specific version, Stage1: value is 0
843+ pub version: u8 ,
844+ pub reserved: u8 ,
845+ }
846+ }
847+
848+ impl TeeReportType {
849+ pub const UNPADDED_SIZE : usize = 4 ;
850+ }
851+
852+ /// SHA384 hash size in bytes
853+ pub const HASH_384_SIZE : usize = 48 ;
854+ /// SHA384 hash
855+ pub type Sha384Hash = [ u8 ; HASH_384_SIZE ] ;
856+
857+ pub const CPU_SVN_SIZE : usize = 16 ;
858+ pub const REPORT_MAC_STRUCT_SIZE : usize = 256 ;
859+ pub const REPORT_MAC_STRUCT_RESERVED1_BYTES : usize = 12 ;
860+ pub const REPORT_MAC_STRUCT_RESERVED2_BYTES : usize = 32 ;
861+ pub const REPORT_DATA_SIZE : usize = 64 ;
862+
863+ /// Message SHA 256 HASH Code - 32 bytes
864+ pub const TEE_MAC_SIZE : usize = 32 ;
865+
866+
867+ struct_def ! {
868+ /// Rust definition of `REPORTMACSTRUCT`, used by TDX `TDREPORT_STRUCT`
869+ /// and the future 256BITSGX
870+ ///
871+ /// Ref: Intel® Trust Domain CPU Architectural Extensions, table 2-5.
872+ /// Version: 343754-002US, MAY 2021
873+ /// Link TDX: <https://cdrdv2.intel.com/v1/dl/getContent/733582>
874+ /// Link 256BITSGX: <https://cdrdv2-public.intel.com/851355/319433-057-architecture-instruction-set-extensions-programming-reference.pdf>
875+ #[ repr( C , align( 256 ) ) ]
876+ #[ cfg_attr(
877+ feature = "large_array_derive" ,
878+ derive( Clone , Debug , Eq , PartialEq )
879+ ) ]
880+ pub struct ReportMac {
881+ /// ( 0) TEE Report type
882+ pub report_type: TeeReportType ,
883+ /// ( 4) Reserved, must be zero
884+ pub reserved1: [ u8 ; REPORT_MAC_STRUCT_RESERVED1_BYTES ] ,
885+ /// ( 16) Security Version of the CPU
886+ pub cpu_svn: [ u8 ; CPU_SVN_SIZE ] ,
887+ /// ( 32) SHA384 of TEE_TCB_INFO for TEEs
888+ pub tee_tcb_info_hash: Sha384Hash ,
889+ /// ( 80) SHA384 of TEE_INFO
890+ pub tee_info_hash: Sha384Hash ,
891+ /// (128) Data provided by the user
892+ pub report_data: [ u8 ; REPORT_DATA_SIZE ] ,
893+ /// (192) Reserved, must be zero
894+ pub reserved2: [ u8 ; REPORT_MAC_STRUCT_RESERVED2_BYTES ] ,
895+ /// (224) The Message Authentication Code over this structure
896+ pub mac: [ u8 ; TEE_MAC_SIZE ] ,
897+ }
898+ }
899+
900+ impl ReportMac {
901+ pub const UNPADDED_SIZE : usize = 256 ;
902+
903+ #[ cfg( target_env = "sgx" ) ]
904+ pub fn verify ( & self ) -> Result < ( ) , ErrorCode > {
905+ everifyreport2 ( self . as_ref ( ) )
906+ // Same as `egetkey` reasoning: unwrap is okay here
907+ . map_err ( |e| ErrorCode :: try_from ( e) . unwrap ( ) )
908+ }
909+ }
910+
911+ #[ cfg( target_env = "sgx" ) ]
912+ impl AsRef < Align256 < [ u8 ; ReportMac :: UNPADDED_SIZE ] > > for ReportMac {
913+ fn as_ref ( & self ) -> & Align256 < [ u8 ; Self :: UNPADDED_SIZE ] > {
914+ unsafe { & * ( self as * const _ as * const _ ) }
915+ }
916+ }
917+
811918#[ test]
812919fn test_eq ( ) {
813920 let mut a = Keyrequest :: default ( ) ;
0 commit comments