Skip to content

Commit 718637d

Browse files
committed
apitypes: depend on the standard library only
1 parent 39e6555 commit 718637d

3 files changed

Lines changed: 66 additions & 5 deletions

File tree

apitypes/apitypes_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2026 Edgeless Systems GmbH
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package apitypes
5+
6+
import (
7+
"go/build"
8+
"strings"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
// TestNoExternalImports asserts that apitypes depends on the standard library only.
15+
func TestNoExternalImports(t *testing.T) {
16+
// Mode 0 only scans the directory for import statements, it does not resolve them.
17+
pkg, err := build.ImportDir(".", 0)
18+
require.NoError(t, err)
19+
20+
for _, imp := range pkg.Imports {
21+
// Standard library import paths have no dot in their first segment, because that segment cannot be a domain name.
22+
first, _, _ := strings.Cut(imp, "/")
23+
require.NotContains(t, first, ".", "apitypes must not import %q", imp)
24+
}
25+
}

apitypes/attest.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ package apitypes
55

66
import (
77
"bytes"
8+
"crypto/sha256"
89
"encoding/asn1"
910
"encoding/json"
1011
"fmt"
11-
12-
"github.com/edgelesssys/contrast/internal/history"
1312
)
1413

1514
// ReportDataSize is the size of the SNP/TDX REPORTDATA fields.
@@ -98,14 +97,14 @@ type CoordinatorState struct {
9897
// intended for use with application-level verification.
9998
func ConstructReportData(nonce []byte, transitionDigest []byte, state *CoordinatorState) [ReportDataSize]byte {
10099
// reportdata = sha256(nonce || sha256(transition) || sha256(root-ca) || sha256(mesh-ca))
101-
rootCADigest := history.Digest(state.RootCA)
102-
meshCADigest := history.Digest(state.MeshCA)
100+
rootCADigest := sha256.Sum256(state.RootCA)
101+
meshCADigest := sha256.Sum256(state.MeshCA)
103102

104103
reportdata := append([]byte{}, nonce...)
105104
reportdata = append(reportdata, transitionDigest...)
106105
reportdata = append(reportdata, rootCADigest[:]...)
107106
reportdata = append(reportdata, meshCADigest[:]...)
108-
hash32 := history.Digest(reportdata)
107+
hash32 := sha256.Sum256(reportdata)
109108

110109
var hash64 [64]byte
111110
copy(hash64[:], hash32[:])

apitypes/reportdata_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2026 Edgeless Systems GmbH
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package apitypes
5+
6+
import (
7+
"encoding/hex"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
// TestConstructReportDataGolden pins the byte layout of the report data digest.
14+
//
15+
// The digest is baked into attestation reports and reproduced independently by every client,
16+
// so any change here silently breaks verification against already-deployed Coordinators.
17+
// If this test fails, the change needs a new API version, not a new test value.
18+
func TestConstructReportDataGolden(t *testing.T) {
19+
nonce := make([]byte, 32)
20+
for i := range nonce {
21+
nonce[i] = byte(i)
22+
}
23+
transitionDigest := make([]byte, 32)
24+
for i := range transitionDigest {
25+
transitionDigest[i] = byte(0xa0 + i)
26+
}
27+
state := &CoordinatorState{
28+
RootCA: []byte("root-ca"),
29+
MeshCA: []byte("mesh-ca"),
30+
}
31+
32+
got := ConstructReportData(nonce, transitionDigest, state)
33+
34+
want := "ec25193fbfa21fb46964de80adca8e7d70222ad41e67c77a696e2f188c02a0f2" +
35+
"0000000000000000000000000000000000000000000000000000000000000000"
36+
assert.Equal(t, want, hex.EncodeToString(got[:]))
37+
}

0 commit comments

Comments
 (0)