Debian Codegraph @

tpm/pcrs.go

raw
1 // Copyright (c) 2014, Google LLC All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package tpm
16
17 import (
18 "crypto/sha1"
19 "errors"
20 "fmt"
21 "io"
22 "strconv"
23
24 "github.com/google/go-tpm/tpmutil"
25 )
26
27 // setPCR sets a PCR value as selected in a given mask.
28 func (pm *pcrMask) setPCR(i int) error {
29 if i >= 24 || i < 0 {
30 return errors.New("can't set PCR " + strconv.Itoa(i))
31 }
32
33 (*pm)[i/8] |= 1 << uint(i%8)
34 return nil
35 }
36
37 // isPCRSet checks to see if a given PCR is included in this mask.
38 func (pm pcrMask) isPCRSet(i int) (bool, error) {
39 if i >= 24 || i < 0 {
40 return false, errors.New("can't check PCR " + strconv.Itoa(i))
41 }
42
43 n := byte(1 << uint(i%8))
44 return pm[i/8]&n == n, nil
45 }
46
47 // String returns a string representation of a pcrSelection
48 func (p pcrSelection) String() string {
49 return fmt.Sprintf("pcrSelection{Size: %x, Mask: % x}", p.Size, p.Mask)
50 }
51
52 // newPCRSelection creates a new pcrSelection for the given set of PCRs.
53 func newPCRSelection(pcrVals []int) (*pcrSelection, error) {
54 pcrs := &pcrSelection{Size: 3}
55 for _, v := range pcrVals {
56 if err := pcrs.Mask.setPCR(v); err != nil {
57 return nil, err
58 }
59 }
60
61 return pcrs, nil
62 }
63
64 // createPCRComposite composes a set of PCRs by prepending a pcrSelection and a
65 // length, then computing the SHA1 hash and returning its output.
66 func createPCRComposite(mask pcrMask, pcrs []byte) ([]byte, error) {
67 if len(pcrs)%PCRSize != 0 {
68 return nil, errors.New("pcrs must be a multiple of " + strconv.Itoa(PCRSize))
69 }
70
71 pcrc := pcrComposite{
72 Selection: pcrSelection{3, mask},
73 Values: pcrs,
74 }
75 b, err := tpmutil.Pack(pcrc)
76 if err != nil {
77 return nil, err
78 }
79
80 h := sha1.Sum(b)
81 return h[:], nil
82 }
83
84 // String returns a string representation of a pcrInfoLong.
85 func (pcri pcrInfoLong) String() string {
86 return fmt.Sprintf("pcrInfoLong{Tag: %x, LocAtCreation: %x, LocAtRelease: %x, PCRsAtCreation: %s, PCRsAtRelease: %s, DigestAtCreation: % x, DigestAtRelease: % x}", pcri.Tag, pcri.LocAtCreation, pcri.LocAtRelease, pcri.PCRsAtCreation, pcri.PCRsAtRelease, pcri.DigestAtCreation, pcri.DigestAtRelease)
87 }
88
89 // String returns a string representation of a pcrInfoShort.
90 func (pcri pcrInfoShort) String() string {
91 return fmt.Sprintf("pcrInfoShort{LocAtRelease: %x, PCRsAtRelease: %s, DigestAtRelease: % x}", pcri.LocAtRelease, pcri.PCRsAtRelease, pcri.DigestAtRelease)
92 }
93
94 // createPCRInfoLong creates a pcrInfoLong structure from a mask and some PCR
95 // values that match this mask, along with a TPM locality.
96 func createPCRInfoLong(loc Locality, mask pcrMask, pcrVals []byte) (*pcrInfoLong, error) {
97 d, err := createPCRComposite(mask, pcrVals)
98 if err != nil {
99 return nil, err
100 }
101
102 pcri := &pcrInfoLong{
103 Tag: tagPCRInfoLong,
104 LocAtCreation: loc,
105 LocAtRelease: loc,
106 PCRsAtCreation: pcrSelection{3, mask},
107 PCRsAtRelease: pcrSelection{3, mask},
108 }
109
110 copy(pcri.DigestAtRelease[:], d)
111 copy(pcri.DigestAtCreation[:], d)
112
113 return pcri, nil
114 }
115
116 // createPCRInfoShort creates a pcrInfoShort structure from a mask and some PCR
117 // values that match this mask, alon with a TPM locality
118 func createPCRInfoShort(loc Locality, mask pcrMask, pcrVals []byte) (*pcrInfoShort, error) {
119 d, err := createPCRComposite(mask, pcrVals)
120 if err != nil {
121 return nil, err
122 }
123
124 pcri := &pcrInfoShort{
125 PCRsAtRelease: pcrSelection{3, mask},
126 LocAtRelease: loc,
127 }
128 copy(pcri.DigestAtRelease[:], d)
129
130 return pcri, nil
131 }
132
133 // newPCRInfoLong creates and returns a pcrInfoLong structure for the given PCR
134 // values.
135 func newPCRInfoLong(rw io.ReadWriter, loc Locality, pcrNums []int) (*pcrInfoLong, error) {
136 var mask pcrMask
137 for _, pcr := range pcrNums {
138 if err := mask.setPCR(pcr); err != nil {
139 return nil, err
140 }
141 }
142
143 pcrVals, err := FetchPCRValues(rw, pcrNums)
144 if err != nil {
145 return nil, err
146 }
147
148 return createPCRInfoLong(loc, mask, pcrVals)
149 }
150
151 func newPCRInfoShort(rw io.ReadWriter, loc Locality, pcrNums []int) (*pcrInfoShort, error) {
152 var mask pcrMask
153 for _, pcr := range pcrNums {
154 if err := mask.setPCR(pcr); err != nil {
155 return nil, err
156 }
157 }
158 pcrVals, err := FetchPCRValues(rw, pcrNums)
159 if err != nil {
160 return nil, err
161 }
162 return createPCRInfoShort(loc, mask, pcrVals)
163 }
164
165 func newPCRInfo(rw io.ReadWriter, pcrNums []int) (*pcrInfo, error) {
166 var mask pcrMask
167 for _, pcr := range pcrNums {
168 if err := mask.setPCR(pcr); err != nil {
169 return nil, err
170 }
171 }
172
173 pcrVals, err := FetchPCRValues(rw, pcrNums)
174 if err != nil {
175 return nil, err
176 }
177 d, err := createPCRComposite(mask, pcrVals)
178 if err != nil {
179 return nil, err
180 }
181 pcri := &pcrInfo{
182 PcrSelection: pcrSelection{3, mask},
183 }
184 copy(pcri.DigestAtRelease[:], d)
185 copy(pcri.DigestAtCreation[:], d)
186
187 return pcri, nil
188 }
189
190 // newPCRInfoLongWithHashes creates and returns a pcrInfoLong structure for the
191 // given PCRs and hashes.
192 func newPCRInfoLongWithHashes(loc Locality, pcrs map[int][]byte) (*pcrInfoLong, error) {
193 var mask pcrMask
194 var hashes []byte
195 for index, hash := range pcrs {
196 if err := mask.setPCR(index); err != nil {
197 return nil, err
198 }
199 hashes = append(hashes, hash...)
200 }
201
202 return createPCRInfoLong(loc, mask, hashes)
203 }