Debian Codegraph @

internal/pcrs.go

raw
1 // Package internal contains private helper functions needed in client and server
2 package internal
3
4 import (
5 "bytes"
6 "crypto"
7 "encoding/hex"
8 "fmt"
9 "io"
10
11 pb "github.com/google/go-tpm-tools/proto/tpm"
12 "github.com/google/go-tpm/legacy/tpm2"
13 "github.com/google/go-tpm/tpmutil"
14 )
15
16 const minPCRIndex = uint32(0)
17
18 func maxPCRIndex(p *pb.PCRs) uint32 {
19 high := minPCRIndex
20 for idx := range p.GetPcrs() {
21 if idx > high {
22 high = idx
23 }
24 }
25 return high
26 }
27
28 // FormatPCRs writes a multiline representation of the PCR values to w.
29 func FormatPCRs(w io.Writer, p *pb.PCRs) error {
30 if _, err := fmt.Fprintf(w, "%v:\n", p.Hash); err != nil {
31 return err
32 }
33 for idx := minPCRIndex; idx <= maxPCRIndex(p); idx++ {
34 if val, ok := p.GetPcrs()[idx]; ok {
35 if _, err := fmt.Fprintf(w, " %2d: 0x%X\n", idx, val); err != nil {
36 return err
37 }
38 }
39 }
40 return nil
41 }
42
43 // CheckSubset verifies if the pcrs PCRs are a valid "subset" of the provided
44 // "superset" of PCRs. The PCR values must match (if present), and all PCRs must
45 // be present in the superset. This function will return an error containing the
46 // first missing or mismatched PCR number.
47 func CheckSubset(subset, superset *pb.PCRs) error {
48 if subset.GetHash() != superset.GetHash() {
49 return fmt.Errorf("PCR hash algo not matching: %v, %v", subset.GetHash(), superset.GetHash())
50 }
51 for pcrNum, pcrVal := range subset.GetPcrs() {
52 if expectedVal, ok := superset.GetPcrs()[pcrNum]; ok {
53 if !bytes.Equal(expectedVal, pcrVal) {
54 return fmt.Errorf("PCR %d mismatch: expected %v, got %v",
55 pcrNum, hex.EncodeToString(expectedVal), hex.EncodeToString(pcrVal))
56 }
57 } else {
58 return fmt.Errorf("PCR %d mismatch: value missing from the superset PCRs", pcrNum)
59 }
60 }
61 return nil
62 }
63
64 // PCRSelection returns the corresponding tpm2.PCRSelection for the PCR data.
65 func PCRSelection(p *pb.PCRs) tpm2.PCRSelection {
66 sel := tpm2.PCRSelection{Hash: tpm2.Algorithm(p.GetHash())}
67
68 for pcrNum := range p.GetPcrs() {
69 sel.PCRs = append(sel.PCRs, int(pcrNum))
70 }
71 return sel
72 }
73
74 // SamePCRSelection checks if the Pcrs has the same PCRSelection as the
75 // provided given tpm2.PCRSelection (including the hash algorithm).
76 func SamePCRSelection(p *pb.PCRs, sel tpm2.PCRSelection) bool {
77 if tpm2.Algorithm(p.GetHash()) != sel.Hash {
78 return false
79 }
80 if len(p.GetPcrs()) != len(sel.PCRs) {
81 return false
82 }
83 for _, pcr := range sel.PCRs {
84 if _, ok := p.Pcrs[uint32(pcr)]; !ok {
85 return false
86 }
87 }
88 return true
89 }
90
91 // PCRSessionAuth calculates the authorization value for the given PCRs.
92 func PCRSessionAuth(p *pb.PCRs, hashAlg crypto.Hash) []byte {
93 // Start with all zeros, we only use a single policy command on our session.
94 oldDigest := make([]byte, hashAlg.Size())
95 ccPolicyPCR, _ := tpmutil.Pack(tpm2.CmdPolicyPCR)
96
97 // Extend the policy digest, see TPM2_PolicyPCR in Part 3 of the spec.
98 hash := hashAlg.New()
99 hash.Write(oldDigest)
100 hash.Write(ccPolicyPCR)
101 hash.Write(encodePCRSelection(PCRSelection(p)))
102 hash.Write(PCRDigest(p, hashAlg))
103 newDigest := hash.Sum(nil)
104 return newDigest[:]
105 }
106
107 // PCRDigest computes the digest of the Pcrs. Note that the digest hash
108 // algorithm may differ from the PCRs' hash (which denotes the PCR bank).
109 func PCRDigest(p *pb.PCRs, hashAlg crypto.Hash) []byte {
110 hash := hashAlg.New()
111 for i := uint32(0); i < 24; i++ {
112 if pcrValue, exists := p.GetPcrs()[i]; exists {
113 hash.Write(pcrValue)
114 }
115 }
116 return hash.Sum(nil)
117 }
118
119 // Encode a tpm2.PCRSelection as if it were a TPML_PCR_SELECTION
120 func encodePCRSelection(sel tpm2.PCRSelection) []byte {
121 // Encode count, pcrSelections.hash and pcrSelections.sizeofSelect fields
122 buf, _ := tpmutil.Pack(uint32(1), sel.Hash, byte(3))
123 // Encode pcrSelect bitmask
124 pcrBits := make([]byte, 3)
125 for _, pcr := range sel.PCRs {
126 byteNum := pcr / 8
127 bytePos := 1 << uint(pcr%8)
128 pcrBits[byteNum] |= byte(bytePos)
129 }
130
131 return append(buf, pcrBits...)
132 }