mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-12-15 16:09:39 -05:00
measurement-reader: unify TPM & TDX sorting
This commit is contained in:
parent
253d201ff3
commit
d58b5f1c06
5 changed files with 114 additions and 150 deletions
|
|
@ -8,10 +8,6 @@ SPDX-License-Identifier: AGPL-3.0-only
|
|||
package tpm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
||||
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
||||
"github.com/edgelesssys/constellation/v2/measurement-reader/internal/sorted"
|
||||
tpmClient "github.com/google/go-tpm-tools/client"
|
||||
|
|
@ -25,26 +21,5 @@ func Measurements() ([]sorted.Measurement, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return sortMeasurements(m), nil
|
||||
}
|
||||
|
||||
func sortMeasurements(m measurements.M) []sorted.Measurement {
|
||||
keys := make([]uint32, 0, len(m))
|
||||
for idx := range m {
|
||||
keys = append(keys, idx)
|
||||
}
|
||||
sort.Slice(keys, func(i, j int) bool {
|
||||
return keys[i] < keys[j]
|
||||
})
|
||||
|
||||
var measurements []sorted.Measurement
|
||||
for _, idx := range keys {
|
||||
expected := m[idx].Expected
|
||||
measurements = append(measurements, sorted.Measurement{
|
||||
Index: fmt.Sprintf("PCR[%02d]", idx),
|
||||
Value: expected[:],
|
||||
})
|
||||
}
|
||||
|
||||
return measurements
|
||||
return sorted.SortMeasurements(m, sorted.TPM), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,78 +0,0 @@
|
|||
/*
|
||||
Copyright (c) Edgeless Systems GmbH
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package tpm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
||||
"github.com/edgelesssys/constellation/v2/measurement-reader/internal/sorted"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSortMeasurements(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
input measurements.M
|
||||
want []sorted.Measurement
|
||||
}{
|
||||
"pre sorted": {
|
||||
input: measurements.M{
|
||||
0: measurements.WithAllBytes(0x11, measurements.Enforce),
|
||||
1: measurements.WithAllBytes(0x22, measurements.Enforce),
|
||||
2: measurements.WithAllBytes(0x33, measurements.Enforce),
|
||||
},
|
||||
want: []sorted.Measurement{
|
||||
{
|
||||
Index: "PCR[00]",
|
||||
Value: bytes.Repeat([]byte{0x11}, 32),
|
||||
},
|
||||
{
|
||||
Index: "PCR[01]",
|
||||
Value: bytes.Repeat([]byte{0x22}, 32),
|
||||
},
|
||||
{
|
||||
Index: "PCR[02]",
|
||||
Value: bytes.Repeat([]byte{0x33}, 32),
|
||||
},
|
||||
},
|
||||
},
|
||||
"unsorted": {
|
||||
input: measurements.M{
|
||||
1: measurements.WithAllBytes(0x22, measurements.Enforce),
|
||||
0: measurements.WithAllBytes(0x11, measurements.Enforce),
|
||||
2: measurements.WithAllBytes(0x33, measurements.Enforce),
|
||||
},
|
||||
want: []sorted.Measurement{
|
||||
{
|
||||
Index: "PCR[00]",
|
||||
Value: bytes.Repeat([]byte{0x11}, 32),
|
||||
},
|
||||
{
|
||||
Index: "PCR[01]",
|
||||
Value: bytes.Repeat([]byte{0x22}, 32),
|
||||
},
|
||||
{
|
||||
Index: "PCR[02]",
|
||||
Value: bytes.Repeat([]byte{0x33}, 32),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
got := sortMeasurements(tc.input)
|
||||
for i := range got {
|
||||
assert.Equal(got[i].Index, tc.want[i].Index)
|
||||
assert.Equal(got[i].Value, tc.want[i].Value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue