@@ -155,6 +155,14 @@ impl fmt::Display for TdInfoStruct {
155155 }
156156}
157157
158+ const MEM_PAGE_ADD : [ u8 ; 16 ] = [
159+ b'M' , b'E' , b'M' , b'.' , b'P' , b'A' , b'G' , b'E' , b'.' , b'A' , b'D' , b'D' , 0 , 0 , 0 , 0 ,
160+ ] ;
161+ const MR_EXTEND : [ u8 ; 16 ] = [
162+ b'M' , b'R' , b'.' , b'E' , b'X' , b'T' , b'E' , b'N' , b'D' , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
163+ ] ;
164+ const MRTD_EXTENSION_BUFFER_PADDING : [ u8 ; 104 ] = [ 0 ; 104 ] ;
165+
158166impl TdInfoStruct {
159167 pub fn pack ( self , buffer : & mut [ u8 ; size_of :: < TdInfoStruct > ( ) ] ) -> usize {
160168 buffer. zeroize ( ) ;
@@ -283,10 +291,6 @@ impl TdInfoStruct {
283291
284292 desc_offset += size_of :: < TdxMetadataDescriptor > ( ) ;
285293
286- let mut buffer128: [ u8 ; MRTD_EXTENSION_BUFFER_SIZE ] = [ 0 ; MRTD_EXTENSION_BUFFER_SIZE ] ; // used by page add
287- let mut buffer3_128: [ [ u8 ; MRTD_EXTENSION_BUFFER_SIZE ] ; 3 ] =
288- [ [ 0 ; MRTD_EXTENSION_BUFFER_SIZE ] ; 3 ] ; // used by mr extend
289-
290294 let mut sha384hasher = Sha384 :: new ( ) ;
291295
292296 for _i in 0 ..descriptor. number_of_section_entry {
@@ -320,37 +324,49 @@ impl TdInfoStruct {
320324 panic ! ( "Invalid type value!\n " ) ;
321325 }
322326
323- let nr_pages = sec. memory_data_size / PAGE_SIZE ;
327+ raw_image_file
328+ . seek ( SeekFrom :: Start ( sec. data_offset as u64 ) )
329+ . expect ( "Seek cursor to sec.data_offset" ) ;
324330
325- for iter in 0 ..nr_pages {
326- if sec. attributes & TDX_METADATA_ATTRIBUTES_EXTEND_MEM_PAGE_ADD == 0 {
327- // Use TDCALL [TDH.MEM.PAGE.ADD]
328- fill_buffer128_with_mem_page_add (
329- & mut buffer128,
330- sec. memory_address + iter * PAGE_SIZE ,
331- ) ;
331+ let mut section_data = vec ! [ 0u8 ; sec. raw_data_size as usize ] ;
332332
333- sha384hasher. update ( buffer128) ;
333+ raw_image_file
334+ . read_exact ( & mut section_data)
335+ . expect ( "Read from sec.data_offset" ) ;
336+
337+ section_data. resize_with ( sec. memory_data_size as usize , Default :: default) ;
338+
339+ let mut page_addr = sec. memory_address ;
340+
341+ for page in section_data. chunks_exact ( PAGE_SIZE as usize ) {
342+ // Use TDCALL [TDH.MEM.PAGE.ADD]
343+ if sec. attributes & TDX_METADATA_ATTRIBUTES_EXTEND_MEM_PAGE_ADD == 0 {
344+ // Byte 0 through 15 contain the ASCII string 'MEM.PAGE.ADD' and padding.
345+ sha384hasher. update ( MEM_PAGE_ADD ) ;
346+ // Byte 16 through 23 contain the GPA (in little-endian format).
347+ sha384hasher. update ( page_addr. to_le_bytes ( ) ) ;
348+ // 0 padding to 128 byte buffer.
349+ sha384hasher. update ( MRTD_EXTENSION_BUFFER_PADDING ) ;
334350 }
335351
336- // check attributes
352+ // Use TDCALL [TDH.MR.EXTEND]
337353 if sec. attributes & TDX_METADATA_ATTRIBUTES_EXTENDMR != 0 {
338- // Use TDCALL [TDH.MR.EXTEND]
339- let granularity = TDH_MR_EXTEND_GRANULARITY ;
340- let iteration = PAGE_SIZE / granularity;
341- for chunk_iter in 0 ..iteration {
342- fill_buffer3_128_with_mr_extend (
343- & mut buffer3_128,
344- sec. memory_address + iter * PAGE_SIZE + chunk_iter * granularity,
345- raw_image_file,
346- sec. data_offset as u64 + iter * PAGE_SIZE + chunk_iter * granularity,
347- ) ;
348-
349- sha384hasher. update ( buffer3_128[ 0 ] ) ;
350- sha384hasher. update ( buffer3_128[ 1 ] ) ;
351- sha384hasher. update ( buffer3_128[ 2 ] ) ;
354+ let mut chunk_addr = page_addr;
355+
356+ for chunk in page. chunks_exact ( TDH_MR_EXTEND_GRANULARITY as usize ) {
357+ // Byte 0 through 15 contain the ASCII string 'MR.EXTEND' and padding.
358+ sha384hasher. update ( MR_EXTEND ) ;
359+ // Byte 16 through 23 contain the GPA (in little-endian format).
360+ sha384hasher. update ( chunk_addr. to_le_bytes ( ) ) ;
361+ // 0 padding to 128 byte buffer.
362+ sha384hasher. update ( MRTD_EXTENSION_BUFFER_PADDING ) ;
363+
364+ // Hash 256 bytes of chunk data
365+ sha384hasher. update ( chunk) ;
366+ chunk_addr += TDH_MR_EXTEND_GRANULARITY ;
352367 }
353368 }
369+ page_addr += PAGE_SIZE ;
354370 }
355371 }
356372 let hash = sha384hasher. finalize ( ) ;
@@ -490,28 +506,6 @@ fn fill_buffer128_with_mem_page_add(buf: &mut [u8; MRTD_EXTENSION_BUFFER_SIZE],
490506 . copy_from_slice ( gpa. to_le_bytes ( ) . as_ref ( ) ) ;
491507}
492508
493- fn fill_buffer3_128_with_mr_extend (
494- buf : & mut [ [ u8 ; MRTD_EXTENSION_BUFFER_SIZE ] ; 3 ] ,
495- gpa : u64 ,
496- file : & mut File ,
497- data_offset : u64 ,
498- ) {
499- buf[ 0 ] . zeroize ( ) ;
500- buf[ 1 ] . zeroize ( ) ;
501- buf[ 2 ] . zeroize ( ) ;
502-
503- // Byte 0 through 8 contain the ASCII string 'MR.EXTEND'.
504- // Byte 16 through 23 contain the GPA (in little-endian format).
505- // All the other bytes contain 0. The other two extension buffers contain the chunk’s content.
506- buf[ 0 ] [ 0 ..MR_EXTEND_ASCII_SIZE ] . copy_from_slice ( "MR.EXTEND" . as_bytes ( ) ) ;
507- buf[ 0 ] [ MR_EXTEND_GPA_OFFSET ..MR_EXTEND_GPA_OFFSET + MR_EXTEND_GPA_SIZE ]
508- . copy_from_slice ( gpa. to_le_bytes ( ) . as_ref ( ) ) ;
509-
510- file. seek ( SeekFrom :: Start ( data_offset) ) . unwrap ( ) ;
511- file. read_exact ( & mut buf[ 1 ] ) . unwrap ( ) ;
512- file. read_exact ( & mut buf[ 2 ] ) . unwrap ( ) ;
513- }
514-
515509fn fill_buffer3_128_with_mr_extend_tdvf (
516510 buf : & mut [ [ u8 ; MRTD_EXTENSION_BUFFER_SIZE ] ; 3 ] ,
517511 gpa : u64 ,
0 commit comments