gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/pkg/crypto/measure.go (about)

     1  // Copyright 2017-2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package crypto
     6  
     7  import (
     8  	"io/ioutil"
     9  	"log"
    10  
    11  	tss "github.com/u-root/u-root/pkg/tss"
    12  )
    13  
    14  const (
    15  	// BlobPCR type in PCR 7
    16  	BlobPCR uint32 = 7
    17  	// BootConfigPCR type in PCR 8
    18  	BootConfigPCR uint32 = 8
    19  	// ConfigDataPCR type in PCR 8
    20  	ConfigDataPCR uint32 = 8
    21  	// NvramVarsPCR type in PCR 9
    22  	NvramVarsPCR uint32 = 9
    23  )
    24  
    25  // TryMeasureData measures a byte array with additional information
    26  func TryMeasureData(pcr uint32, data []byte, info string) error {
    27  	tpm, err := tss.NewTPM()
    28  	if err != nil {
    29  		log.Printf("Cannot open TPM: %v", err)
    30  		return err
    31  	}
    32  	log.Printf("Measuring blob: %v", info)
    33  	if err := tpm.Measure(data, pcr); err != nil {
    34  		return err
    35  	}
    36  	tpm.Close()
    37  	return nil
    38  }
    39  
    40  // TryMeasureFiles measures a variable amount of files
    41  func TryMeasureFiles(files ...string) error {
    42  	tpm, err := tss.NewTPM()
    43  	if err != nil {
    44  		return err
    45  	}
    46  	for _, file := range files {
    47  		log.Printf("Measuring file: %v", file)
    48  		data, err := ioutil.ReadFile(file)
    49  		if err != nil {
    50  			continue
    51  		}
    52  		if err := tpm.Measure(data, BlobPCR); err != nil {
    53  			return err
    54  		}
    55  	}
    56  	tpm.Close()
    57  	return nil
    58  }