/
precalc.go
420 lines (359 loc) · 9.94 KB
/
precalc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package tpmtool
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"io/ioutil"
"os"
"github.com/9elements/tpmtool/pkg/tpm"
"github.com/koding/multiconfig"
)
// TPMInterface is a global TPM interface
var TPMInterface tpm.TPM
// CurrentPCRMap is the current used PCR map and a copy of the default map
var CurrentPCRMap map[int][]byte
// TPM1DefaultPCRMap is the TPM 1.2 default PCR map after a power cycle without
// any measurements done
var TPM1DefaultPCRMap = map[int][]byte{
0: make([]byte, 20),
1: make([]byte, 20),
2: make([]byte, 20),
3: make([]byte, 20),
4: make([]byte, 20),
5: make([]byte, 20),
6: make([]byte, 20),
7: make([]byte, 20),
8: make([]byte, 20),
9: make([]byte, 20),
10: make([]byte, 20),
11: make([]byte, 20),
12: make([]byte, 20),
13: make([]byte, 20),
14: make([]byte, 20),
15: make([]byte, 20),
16: make([]byte, 20),
17: []byte{'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
18: []byte{'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
19: []byte{'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
20: []byte{'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
21: []byte{'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
22: []byte{'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f'},
23: make([]byte, 20),
}
func getMap() map[int][]byte {
if TPMInterface.Info().Specification == tpm.TPM12 {
return TPM1DefaultPCRMap
}
return nil
}
func hashSum(data []byte, algoID tpm.IAlgHash) ([]byte, error) {
switch algoID {
case tpm.TPMAlgSha:
hash := sha1.Sum(data)
return hash[:], nil
case tpm.TPMAlgSha256:
hash := sha256.Sum256(data)
return hash[:], nil
case tpm.TPMAlgSha384:
hash := sha512.Sum384(data)
return hash[:], nil
case tpm.TPMAlgSha512:
hash := sha512.Sum512(data)
return hash[:], nil
case tpm.TPMAlgSm3s256:
return nil, errors.New("Not implemented yet")
}
return nil, errors.New("Hash algorithm not implemented yet")
}
// StaticPCR populates a static PCR into the map
func StaticPCR(pcrIndex int, hash []byte) {
CurrentPCRMap[pcrIndex] = hash
}
// DynamicPCR gets the current PCR and populates it into the map
func DynamicPCR(pcrIndex int) error {
hash, err := TPMInterface.ReadPCR(uint32(pcrIndex))
if err != nil {
return err
}
CurrentPCRMap[pcrIndex] = hash
return nil
}
// ExtendPCR extends a hash into a current PCR
func ExtendPCR(pcrIndex int, hash []byte, algoID tpm.IAlgHash) error {
hash, err := hashSum(append(CurrentPCRMap[pcrIndex], hash...), algoID)
if err != nil {
return err
}
CurrentPCRMap[pcrIndex] = hash
return nil
}
// MeasurePCR measures a file into a PCR
func MeasurePCR(pcrIndex int, filePath string, algoID tpm.IAlgHash) error {
file, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
var fileHash []byte
fileHash, err = hashSum(file, algoID)
if err != nil {
return err
}
hash, err := hashSum(append(CurrentPCRMap[pcrIndex], fileHash...), algoID)
if err != nil {
return err
}
CurrentPCRMap[pcrIndex] = hash
return nil
}
// FirmwareLogPCR uses the firmware ACPI log for extending PCRs
func FirmwareLogPCR(pcrIndex int, firmware tpm.FirmwareType) error {
tpmSpec := TPMInterface.Info().Specification
tcpaLog, err := tpm.ParseLog(firmware, tpmSpec)
if err != nil {
return err
}
for _, event := range tcpaLog.PcrList {
if event.PcrEventType() == uint32(tpm.EvNoAction) {
continue
}
if event.PcrIndex() == pcrIndex {
for _, digest := range *event.Digests() {
if event.PcrIndex() == pcrIndex {
err := ExtendPCR(event.PcrIndex(), digest.Digest, digest.DigestAlg)
if err != nil {
return err
}
}
}
}
}
return nil
}
// LuksPCR extends the hash of a LUKS device into a current PCR
func LuksPCR(pcrIndex int, devicePath string, algoID tpm.IAlgHash) error {
deviceFD, err := os.Open(devicePath)
if err != nil {
return err
}
defer deviceFD.Close()
luksHeader := make([]byte, Luks1HeaderLength)
_, err = deviceFD.Read(luksHeader)
if err != nil {
return err
}
hash, err := hashSum(append(CurrentPCRMap[pcrIndex], luksHeader...), algoID)
if err != nil {
return err
}
CurrentPCRMap[pcrIndex] = hash
return nil
}
func runCalculations(calculations []PreCalculation, pcrIndex int) error {
for _, calculation := range calculations {
if calculation.Method == Static && len(calculations) > 1 {
return errors.New("Static type: More calculation defined than possible")
}
if calculation.Method == Dynamic && len(calculations) > 1 {
return errors.New("Dynamic type: More calculation defined than possible")
}
}
// For TPM 2.0 we need to get Algo per PCR bank
var algoID = tpm.TPMAlgError
if TPMInterface.Info().Specification == tpm.TPM12 {
algoID = tpm.TPMAlgSha
}
CurrentPCRMap[pcrIndex] = getMap()[pcrIndex]
for _, calculation := range calculations {
switch calculation.Method {
case Static:
hash := calculation.Hash
if hash == "" {
return errors.New("Static type: No hash defined")
}
StaticPCR(pcrIndex, []byte(hash))
case Dynamic:
return DynamicPCR(pcrIndex)
case Extend:
if len(calculation.Hashes) <= 0 {
return errors.New("Extend type: No hashes defined")
}
for _, hash := range calculation.Hashes {
if err := ExtendPCR(pcrIndex, []byte(hash), algoID); err != nil {
return err
}
}
case Measure:
if len(calculation.FilePaths) <= 0 {
return errors.New("Measure type: No paths defined")
}
for _, path := range calculation.FilePaths {
if err := MeasurePCR(pcrIndex, path, algoID); err != nil {
return err
}
}
case FirmwareLog:
if calculation.Firmware == "" {
return errors.New("FirmwareLog type: Firmware not set")
}
if err := FirmwareLogPCR(pcrIndex, calculation.Firmware); err != nil {
return err
}
case Luks:
if calculation.DevicePath == "" {
return errors.New("Luks type: No path defined")
}
return LuksPCR(pcrIndex, calculation.DevicePath, algoID)
default:
return errors.New(string(calculation.Method) + " not implemented")
}
}
return nil
}
func executeConfig(sealingConfig *TPM1SealingConfig) error {
if sealingConfig.Pcr0 != nil {
if err := runCalculations(sealingConfig.Pcr0, 0); err != nil {
return err
}
}
if sealingConfig.Pcr1 != nil {
if err := runCalculations(sealingConfig.Pcr1, 1); err != nil {
return err
}
}
if sealingConfig.Pcr2 != nil {
if err := runCalculations(sealingConfig.Pcr2, 2); err != nil {
return err
}
}
if sealingConfig.Pcr3 != nil {
if err := runCalculations(sealingConfig.Pcr3, 3); err != nil {
return err
}
}
if sealingConfig.Pcr4 != nil {
if err := runCalculations(sealingConfig.Pcr4, 4); err != nil {
return err
}
}
if sealingConfig.Pcr5 != nil {
if err := runCalculations(sealingConfig.Pcr5, 5); err != nil {
return err
}
}
if sealingConfig.Pcr6 != nil {
if err := runCalculations(sealingConfig.Pcr6, 6); err != nil {
return err
}
}
if sealingConfig.Pcr7 != nil {
if err := runCalculations(sealingConfig.Pcr7, 7); err != nil {
return err
}
}
if sealingConfig.Pcr8 != nil {
if err := runCalculations(sealingConfig.Pcr8, 8); err != nil {
return err
}
}
if sealingConfig.Pcr9 != nil {
if err := runCalculations(sealingConfig.Pcr9, 9); err != nil {
return err
}
}
if sealingConfig.Pcr10 != nil {
if err := runCalculations(sealingConfig.Pcr10, 10); err != nil {
return err
}
}
if sealingConfig.Pcr11 != nil {
if err := runCalculations(sealingConfig.Pcr11, 11); err != nil {
return err
}
}
if sealingConfig.Pcr12 != nil {
if err := runCalculations(sealingConfig.Pcr12, 12); err != nil {
return err
}
}
if sealingConfig.Pcr13 != nil {
if err := runCalculations(sealingConfig.Pcr13, 13); err != nil {
return err
}
}
if sealingConfig.Pcr14 != nil {
if err := runCalculations(sealingConfig.Pcr14, 14); err != nil {
return err
}
}
if sealingConfig.Pcr15 != nil {
if err := runCalculations(sealingConfig.Pcr15, 15); err != nil {
return err
}
}
if sealingConfig.Pcr16 != nil {
if err := runCalculations(sealingConfig.Pcr16, 16); err != nil {
return err
}
}
if sealingConfig.Pcr17 != nil {
if err := runCalculations(sealingConfig.Pcr17, 17); err != nil {
return err
}
}
if sealingConfig.Pcr18 != nil {
if err := runCalculations(sealingConfig.Pcr18, 18); err != nil {
return err
}
}
if sealingConfig.Pcr19 != nil {
if err := runCalculations(sealingConfig.Pcr19, 19); err != nil {
return err
}
}
if sealingConfig.Pcr20 != nil {
if err := runCalculations(sealingConfig.Pcr20, 20); err != nil {
return err
}
}
if sealingConfig.Pcr21 != nil {
if err := runCalculations(sealingConfig.Pcr21, 21); err != nil {
return err
}
}
if sealingConfig.Pcr22 != nil {
if err := runCalculations(sealingConfig.Pcr22, 22); err != nil {
return err
}
}
if sealingConfig.Pcr23 != nil {
if err := runCalculations(sealingConfig.Pcr23, 23); err != nil {
return err
}
}
return nil
}
// PreCalculate calculates a PCR map by a given sealing configuration
// doing different types of calculations in the right order
func PreCalculate(tpmInterface tpm.TPM, sealingConfigPath string) (map[int][]byte, error) {
TPMInterface = tpmInterface
// Initialize the default values
var sealingConf *TPM1SealingConfig
CurrentPCRMap = make(map[int][]byte)
if TPMInterface.Info().Specification == tpm.TPM12 {
sealingConf = new(TPM1SealingConfig)
} else {
return nil, errors.New("TPM spec not implemented yet")
}
config := multiconfig.NewWithPath(sealingConfigPath)
if config == nil {
return nil, errors.New("Couldn't load config from disk")
}
if err := config.Load(sealingConf); err != nil {
return nil, err
}
if err := executeConfig(sealingConf); err != nil {
return nil, err
}
return CurrentPCRMap, nil
}