| 1 |
package tpm2 |
| 2 |
|
| 3 |
// pcrSelectionFormatter is a Platform TPM Profile-specific interface for |
| 4 |
// formatting TPM PCR selections. |
| 5 |
// This interface isn't (yet) part of the go-tpm public interface. After we |
| 6 |
// add a second implementation, we should consider making it public. |
| 7 |
type pcrSelectionFormatter interface { |
| 8 |
// PCRs returns the TPM PCR selection bitmask associated with the given PCR indices. |
| 9 |
PCRs(pcrs ...uint) []byte |
| 10 |
} |
| 11 |
|
| 12 |
// PCClientCompatible is a pcrSelectionFormatter that formats PCR selections |
| 13 |
// suitable for use in PC Client PTP-compatible TPMs (the vast majority): |
| 14 |
// https://trustedcomputinggroup.org/resource/pc-client-platform-tpm-profile-ptp-specification/ |
| 15 |
// PC Client mandates at least 24 PCRs but does not provide an upper limit. |
| 16 |
var PCClientCompatible pcrSelectionFormatter = pcClient{} |
| 17 |
|
| 18 |
type pcClient struct{} |
| 19 |
|
| 20 |
// The TPM requires all PCR selections to be at least big enough to select all |
| 21 |
// the PCRs in the minimum PCR allocation. |
| 22 |
const pcClientMinimumPCRCount = 24 |
| 23 |
|
| 24 |
func (pcClient) PCRs(pcrs ...uint) []byte { |
| 25 |
// Find the biggest PCR we selected. |
| 26 |
maxPCR := uint(0) |
| 27 |
for _, pcr := range pcrs { |
| 28 |
if pcr > maxPCR { |
| 29 |
maxPCR = pcr |
| 30 |
} |
| 31 |
} |
| 32 |
selectionSize := maxPCR/8 + 1 |
| 33 |
|
| 34 |
// Enforce the minimum PCR selection size. |
| 35 |
if selectionSize < (pcClientMinimumPCRCount / 8) { |
| 36 |
selectionSize = (pcClientMinimumPCRCount / 8) |
| 37 |
} |
| 38 |
|
| 39 |
// Allocate a byte array to store the bitfield, that has at least |
| 40 |
// enough bits to store our selections. |
| 41 |
selection := make([]byte, selectionSize) |
| 42 |
for _, pcr := range pcrs { |
| 43 |
// The PCR selection mask is byte-wise little-endian: |
| 44 |
// select[0] contains bits representing the selection of PCRs 0 through 7 |
| 45 |
// select[1] contains PCRs 8 through 15, and so on. |
| 46 |
byteIdx := pcr / 8 |
| 47 |
// Within the byte, the PCR selection is bit-wise big-endian: |
| 48 |
// bit 0 of select[0] contains the selection of PCR 0 |
| 49 |
// bit 1 of select[0] contains the selection of PCR 1, and so on. |
| 50 |
bitIdx := pcr % 8 |
| 51 |
|
| 52 |
selection[byteIdx] |= (1 << bitIdx) |
| 53 |
} |
| 54 |
return selection |
| 55 |
} |