Debian Codegraph @

client/pcr.go

raw
1 package client
2
3 import (
4 "crypto"
5 "fmt"
6 "io"
7 "math"
8
9 pb "github.com/google/go-tpm-tools/proto/tpm"
10 "github.com/google/go-tpm/legacy/tpm2"
11 )
12
13 // NumPCRs is set to the spec minimum of 24, as that's all go-tpm supports.
14 const NumPCRs = 24
15
16 // We hard-code SHA256 as the policy session hash algorithms. Note that this
17 // differs from the PCR hash algorithm (which selects the bank of PCRs to use)
18 // and the Public area Name algorithm. We also chose this for compatibility with
19 // github.com/google/go-tpm/legacy/tpm2, as it hardcodes the nameAlg as SHA256 in
20 // several places. Two constants are used to avoid repeated conversions.
21 const (
22 SessionHashAlg = crypto.SHA256
23 SessionHashAlgTpm = tpm2.AlgSHA256
24 )
25
26 // CertifyHashAlgTpm is the hard-coded algorithm used in certify PCRs.
27 const CertifyHashAlgTpm = tpm2.AlgSHA256
28
29 // AllocatedPCRs returns a list of selections corresponding to the TPM's implemented PCRs.
30 func AllocatedPCRs(rw io.ReadWriter) ([]tpm2.PCRSelection, error) {
31 caps, moreData, err := tpm2.GetCapability(rw, tpm2.CapabilityPCRs, math.MaxUint32, 0)
32 if err != nil {
33 return nil, fmt.Errorf("listing implemented PCR banks: %w", err)
34 }
35 if moreData {
36 return nil, fmt.Errorf("extra data from GetCapability")
37 }
38 var sels []tpm2.PCRSelection
39 for _, cap := range caps {
40 sel, ok := cap.(tpm2.PCRSelection)
41 if !ok {
42 return nil, fmt.Errorf("unexpected data from GetCapability")
43 }
44 // skip empty (unallocated) PCR selections
45 if len(sel.PCRs) == 0 {
46 continue
47 }
48 sels = append(sels, sel)
49 }
50 return sels, nil
51 }
52
53 // ReadPCRs fetches all the PCR values specified in sel, making multiple calls
54 // to the TPM if necessary.
55 func ReadPCRs(rw io.ReadWriter, sel tpm2.PCRSelection) (*pb.PCRs, error) {
56 pl := pb.PCRs{
57 Hash: pb.HashAlgo(sel.Hash),
58 Pcrs: map[uint32][]byte{},
59 }
60
61 for i := 0; i < len(sel.PCRs); i += 8 {
62 end := min(i+8, len(sel.PCRs))
63 pcrSel := tpm2.PCRSelection{
64 Hash: sel.Hash,
65 PCRs: sel.PCRs[i:end],
66 }
67
68 pcrMap, err := tpm2.ReadPCRs(rw, pcrSel)
69 if err != nil {
70 return nil, err
71 }
72
73 for pcr, val := range pcrMap {
74 pl.Pcrs[uint32(pcr)] = val
75 }
76 }
77
78 return &pl, nil
79 }
80
81 // ReadAllPCRs fetches all the PCR values from all implemented PCR banks.
82 func ReadAllPCRs(rw io.ReadWriter) ([]*pb.PCRs, error) {
83 sels, err := AllocatedPCRs(rw)
84 if err != nil {
85 return nil, err
86 }
87
88 allPcrs := make([]*pb.PCRs, len(sels))
89 for i, sel := range sels {
90 allPcrs[i], err = ReadPCRs(rw, sel)
91 if err != nil {
92 return nil, fmt.Errorf("reading bank %x PCRs: %w", sel.Hash, err)
93 }
94 }
95 return allPcrs, nil
96 }
97
98 // SealOpts specifies the PCR values that should be used for Seal().
99 type SealOpts struct {
100 // Current seals data to the current specified PCR selection.
101 Current tpm2.PCRSelection
102 // Target predictively seals data to the given specified PCR values.
103 Target *pb.PCRs
104 }
105
106 // UnsealOpts specifies the options that should be used for Unseal().
107 // Currently, it specifies the PCRs that need to pass certification in order to
108 // successfully unseal.
109 // CertifyHashAlgTpm is the hard-coded algorithm that must be used with
110 // UnsealOpts.
111 type UnsealOpts struct {
112 // CertifyCurrent certifies that a selection of current PCRs have the same
113 // value when sealing.
114 CertifyCurrent tpm2.PCRSelection
115 // CertifyExpected certifies that the TPM had a specific set of PCR values when sealing.
116 CertifyExpected *pb.PCRs
117 }
118
119 // FullPcrSel will return a full PCR selection based on the total PCR number
120 // of the TPM with the given hash algo.
121 func FullPcrSel(hash tpm2.Algorithm) tpm2.PCRSelection {
122 sel := tpm2.PCRSelection{Hash: hash}
123 for i := 0; i < NumPCRs; i++ {
124 sel.PCRs = append(sel.PCRs, int(i))
125 }
126 return sel
127 }
128
129 func mergePCRSelAndProto(rw io.ReadWriter, sel tpm2.PCRSelection, proto *pb.PCRs) (*pb.PCRs, error) {
130 if proto == nil || len(proto.GetPcrs()) == 0 {
131 return ReadPCRs(rw, sel)
132 }
133 if len(sel.PCRs) == 0 {
134 return proto, nil
135 }
136 if sel.Hash != tpm2.Algorithm(proto.Hash) {
137 return nil, fmt.Errorf("current hash (%v) differs from target hash (%v)",
138 sel.Hash, tpm2.Algorithm(proto.Hash))
139 }
140
141 // At this point, both sel and proto are non-empty.
142 // Verify no overlap in sel and proto PCR indexes.
143 overlap := make([]int, 0)
144 targetMap := proto.GetPcrs()
145 for _, pcrVal := range sel.PCRs {
146 if _, found := targetMap[uint32(pcrVal)]; found {
147 overlap = append(overlap, pcrVal)
148 }
149 }
150 if len(overlap) != 0 {
151 return nil, fmt.Errorf("found PCR overlap: %v", overlap)
152 }
153
154 currentPcrs, err := ReadPCRs(rw, sel)
155 if err != nil {
156 return nil, err
157 }
158
159 for pcr, val := range proto.GetPcrs() {
160 currentPcrs.Pcrs[pcr] = val
161 }
162 return currentPcrs, nil
163 }