@@ -30,7 +30,7 @@ import (
3030 ar "github.com/Fraunhofer-AISEC/cmc/attestationreport"
3131)
3232
33- func performImaPrecomputation (pcr int , bootAggregate []byte , paths []string , strip string , imaTemplate string ) ([]* ar.ReferenceValue , error ) {
33+ func performImaPrecomputation (pcr int , bootAggregate []byte , paths []string , strip , prepend string , imaTemplate string ) ([]* ar.ReferenceValue , error ) {
3434
3535 refvals := make ([]* ar.ReferenceValue , 0 )
3636 fileCh := make (chan string , 100 )
@@ -57,9 +57,9 @@ func performImaPrecomputation(pcr int, bootAggregate []byte, paths []string, str
5757 go func () {
5858 defer wg .Done ()
5959 for path := range fileCh {
60- refval , err := precomputeImaEntry (path , strip , imaTemplate , pcr , true )
60+ refval , err := precomputeImaEntry (path , strip , prepend , imaTemplate , pcr , true )
6161 if err != nil {
62- log .Debugf ("error hashing %q: %v" , path , err )
62+ log .Errorf ("error hashing %q: %v" , path , err )
6363 continue
6464 }
6565 log .Tracef ("%s: %s" , refval .SubType , hex .EncodeToString (refval .Sha256 ))
@@ -151,27 +151,27 @@ func precomputeImaBootAggregate(hash []byte, template string, pcr int, optional
151151 return r , nil
152152}
153153
154- func precomputeImaEntry (path , strip , template string , pcr int , optional bool ) (* ar.ReferenceValue , error ) {
154+ func precomputeImaEntry (path , strip , prepend , template string , pcr int , optional bool ) (* ar.ReferenceValue , error ) {
155155
156156 fileHash , err := hashFile (path )
157157 if err != nil {
158158 return nil , fmt .Errorf ("failed to hash file: %w" , err )
159159 }
160160
161- strippedPath := stripPrefix (path , strip )
161+ hashedPath := modifyPath (path , strip , prepend )
162162
163- tmpl , err := precomputeImaTemplate (fileHash , strippedPath , template )
163+ tmpl , err := precomputeImaTemplate (fileHash , hashedPath , template )
164164 if err != nil {
165165 return nil , fmt .Errorf ("failed to precompute ima template: %w" , err )
166166 }
167167
168168 // Create reference value
169169 r := & ar.ReferenceValue {
170170 Type : "TPM Reference Value" ,
171- SubType : filepath .Base (strippedPath ),
171+ SubType : filepath .Base (hashedPath ),
172172 Index : pcr ,
173173 Sha256 : tmpl ,
174- Description : strippedPath ,
174+ Description : hashedPath ,
175175 Optional : optional ,
176176 }
177177
@@ -224,12 +224,10 @@ func hashFile(path string) ([]byte, error) {
224224 return h .Sum (nil ), nil
225225}
226226
227- func stripPrefix (s , prefix string ) string {
227+ func modifyPath (s , prefix , prepend string ) string {
228228 if s == "" {
229229 return ""
230230 }
231- if prefix != "" && strings .HasPrefix (s , prefix ) {
232- return s [len (prefix ):]
233- }
234- return s
231+ s = strings .TrimPrefix (s , prefix )
232+ return prepend + s
235233}
0 commit comments