@@ -64,7 +64,7 @@ pub struct Header {
6464 pub flags : u32 ,
6565 pub vendor : Vendor ,
6666 pub date : Date ,
67- pub size : u32 , // in dwords, dword size is 32bit
67+ pub manifest_len : u32 , // in dwords, dword size is 32bit
6868 pub magic : [ u8 ; 4 ] ,
6969 // NOTE: only for Gen 2 ME firmware
7070 pub entries : u32 ,
@@ -93,79 +93,106 @@ impl Display for Header {
9393 }
9494}
9595
96+ impl Header {
97+ /// Get the header length including signature
98+ pub fn header_len ( & self ) -> usize {
99+ self . header_len as usize * 4
100+ }
101+
102+ /// Get the length of the manifest including its data
103+ pub fn manifest_len ( & self ) -> usize {
104+ self . manifest_len as usize * 4
105+ }
106+
107+ /// Get the length of the data after the header
108+ pub fn data_len ( & self ) -> usize {
109+ let mlen = self . manifest_len ( ) ;
110+ let hlen = self . header_len ( ) ;
111+ mlen - hlen
112+ }
113+ }
114+
96115#[ derive( Immutable , IntoBytes , FromBytes , Serialize , Deserialize , Clone , Copy , Debug ) ]
97116#[ repr( C ) ]
98- pub struct Manifest {
99- pub header : Header ,
117+ pub struct Signature {
100118 #[ serde( with = "serde_bytes" ) ]
101119 pub rsa_pub_key : [ u8 ; 0x100 ] ,
102120 pub rsa_pub_exp : u32 ,
103121 #[ serde( with = "serde_bytes" ) ]
104122 pub rsa_sig : [ u8 ; 0x100 ] ,
105123}
106124
125+ #[ derive( Serialize , Deserialize , Clone , Debug ) ]
126+ #[ repr( C ) ]
127+ pub struct Manifest {
128+ pub header : Header ,
129+ pub signature : Signature ,
130+ pub mdata : Vec < u8 > ,
131+ }
132+
107133pub const MANIFEST_SIZE : usize = core:: mem:: size_of :: < Manifest > ( ) ;
108134
109135impl < ' a > Manifest {
110136 pub fn new ( data : & ' a [ u8 ] ) -> Result < Self , String > {
111- let ( manifest , _ ) = match Self :: read_from_prefix ( data) {
137+ let ( header , slice ) = match Header :: read_from_prefix ( data) {
112138 Ok ( r) => r,
113139 Err ( e) => {
114140 let err = format ! ( "Manifest cannot be parsed: {e:?}" ) ;
115141 return Err ( err) ;
116142 }
117143 } ;
118144
119- if manifest . header . magic != MANIFEST2_MAGIC_BYTES {
145+ if header. magic != MANIFEST2_MAGIC_BYTES {
120146 let err = format ! (
121147 "Manifest magic not found: wanted {MANIFEST2_MAGIC_BYTES:02x?} ({MANIFEST2_MAGIC}), got {:02x?}" ,
122- manifest . header. magic
148+ header. magic
123149 ) ;
124150 return Err ( err) ;
125151 }
126152
127- Ok ( manifest)
128- }
129-
130- /// Get the header length
131- pub fn header_len ( & self ) -> usize {
132- self . header . header_len as usize * 4
133- }
134-
135- /// Get the size of the manifest and its data
136- pub fn size ( & self ) -> usize {
137- self . header . size as usize * 4
138- }
153+ let ( signature, _) = match Signature :: read_from_prefix ( slice) {
154+ Ok ( r) => r,
155+ Err ( e) => {
156+ let err = format ! ( "Signature cannot be parsed: {e:?}" ) ;
157+ return Err ( err) ;
158+ }
159+ } ;
139160
140- /// Get the length of the data after the manifest
141- pub fn data_len ( & self ) -> usize {
142- let mlen = self . size ( ) ;
143- let hlen = self . header_len ( ) ;
144- mlen - hlen
161+ // The manifest carries additional data after its header and signature.
162+ // Note that header_len includes the signature.
163+ let header_len = header. header_len ( ) ;
164+ let size = header. manifest_len ( ) ;
165+ let mdata = & data[ header_len..size] ;
166+
167+ Ok ( Self {
168+ header,
169+ signature,
170+ mdata : mdata. to_vec ( ) ,
171+ } )
145172 }
146173
147174 /// Get the MD5 hash over the RSA public key and exponent.
148- pub fn hash_key ( self : Self ) -> Vec < u8 > {
149- let k = self . rsa_pub_key . as_bytes ( ) ;
150- let e = self . rsa_pub_exp ;
175+ pub fn hash_key ( & self ) -> Vec < u8 > {
176+ let k = self . signature . rsa_pub_key . as_bytes ( ) ;
177+ let e = self . signature . rsa_pub_exp ;
151178 let ke = [ k, & e. to_le_bytes ( ) ] . concat ( ) ;
152179 md5:: compute ( ke) . to_vec ( )
153180 }
154181
155- /// Verify the manifest. Pass extra bytes from after the manifest.
156- pub fn verify ( & self , ebytes : & [ u8 ] ) -> bool {
182+ /// Verify the manifest.
183+ pub fn verify ( & self ) -> bool {
157184 use num_bigint:: BigUint ;
158185 use sha2:: { Digest , Sha256 } ;
159186
160- let modulus = BigUint :: from_bytes_le ( & self . rsa_pub_key ) ;
161- let exponent = BigUint :: from ( self . rsa_pub_exp ) ;
162- let signature = BigUint :: from_bytes_le ( & self . rsa_sig ) ;
187+ let modulus = BigUint :: from_bytes_le ( & self . signature . rsa_pub_key ) ;
188+ let exponent = BigUint :: from ( self . signature . rsa_pub_exp ) ;
189+ let signature = BigUint :: from_bytes_le ( & self . signature . rsa_sig ) ;
163190 let sb = signature. modpow ( & exponent, & modulus) . to_bytes_be ( ) ;
164191
165192 let header = self . header . as_bytes ( ) ;
166193 let mut hasher = Sha256 :: new ( ) ;
167194 hasher. update ( & header) ;
168- hasher. update ( & ebytes ) ;
195+ hasher. update ( & self . mdata ) ;
169196 let hash = hasher. finalize ( ) ;
170197 let hb = hash. as_bytes ( ) ;
171198
@@ -177,7 +204,7 @@ impl<'a> Manifest {
177204impl Display for Manifest {
178205 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
179206 let h = self . header ;
180- let exp = self . rsa_pub_exp ;
207+ let exp = self . signature . rsa_pub_exp ;
181208 write ! ( f, "{h}, RSA exp {exp}" )
182209 }
183210}
0 commit comments