Skip to content

Commit

Permalink
Merge pull request #59 from Ruoyu-y/update_api
Browse files Browse the repository at this point in the history
golang: update api
  • Loading branch information
rihuita authored Nov 25, 2024
2 parents 862206e + 6478a55 commit 299fd3a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/golang/cctrusted_vm/cmd/app/imr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var imrCmd = &cobra.Command{
if err != nil {
return err
}
report, err := sdk.GetCCReport("", "", nil)
report, err := sdk.GetCCReport(nil, nil, nil)
if err != nil {
return err
}
Expand Down
9 changes: 4 additions & 5 deletions src/golang/cctrusted_vm/cmd/app/report.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"encoding/base64"
"encoding/binary"
"math"
"math/rand"
Expand Down Expand Up @@ -31,14 +30,14 @@ var reportCmd = &cobra.Command{
},
}

func makeNonce() string {
func makeNonce() []byte {
num := uint64(rand.Int63n(math.MaxInt64))
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, num)
return base64.StdEncoding.EncodeToString(b)
return b
}

func makeUserData() string {
func makeUserData() []byte {
b := []byte("demo user data")
return base64.StdEncoding.EncodeToString(b)
return b
}
16 changes: 8 additions & 8 deletions src/golang/cctrusted_vm/cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (

type Device interface {
ProbeDevice() error
Report(nonce, userData string, extraArgs map[string]any) (evidence_api.CcReport, error)
Report(nonce, userData []byte, extraArgs map[string]any) (evidence_api.CcReport, error)
Name() string
CCType() evidence_api.CC_Type
Version() evidence_api.DeviceVersion
Expand All @@ -27,7 +27,7 @@ type GenericDevice struct {
Device
}

func (d *GenericDevice) Report(nonce, userData string, extraArgs map[string]any) (evidence_api.CcReport, error) {
func (d *GenericDevice) Report(nonce, userData []byte, extraArgs map[string]any) (evidence_api.CcReport, error) {
var err error
if _, err = os.Stat(TSM_PREFIX); os.IsNotExist(err) {
return evidence_api.CcReport{}, errors.New("Configfs TSM is not supported in the current environment.")
Expand All @@ -36,18 +36,18 @@ func (d *GenericDevice) Report(nonce, userData string, extraArgs map[string]any)
// concatenate nonce and userData
// check if the data is base64 encoded, if yes, decode before doing hash
hasher := sha512.New()
if nonce != "" {
val, err := base64.StdEncoding.DecodeString(nonce)
if nonce != nil {
val, err := base64.StdEncoding.DecodeString(string(nonce))
if err != nil {
hasher.Write([]byte(nonce))
hasher.Write(nonce)
} else {
hasher.Write(val)
}
}
if userData != "" {
val, err := base64.StdEncoding.DecodeString(userData)
if userData != nil {
val, err := base64.StdEncoding.DecodeString(string(userData))
if err != nil {
hasher.Write([]byte(userData))
hasher.Write(userData)
} else {
hasher.Write(val)
}
Expand Down
4 changes: 2 additions & 2 deletions src/golang/cctrusted_vm/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *SDK) DumpCCReport(reportBytes []byte) error {
// GetCCMeasurement implements evidence_api.EvidenceAPI.
func (s *SDK) GetCCMeasurement(index int, alg evidence_api.TCG_ALG) (evidence_api.TcgDigest, error) {
emptyRet := evidence_api.TcgDigest{}
report, err := s.GetCCReport("", "", nil)
report, err := s.GetCCReport(nil, nil, nil)
if err != nil {
return emptyRet, err
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func (s *SDK) internelEventlog() (*evidence_api.EventLogger, error) {
}

// Report implements EvidenceAPI.
func (s *SDK) GetCCReport(nonce, userData string, extraArgs map[string]any) (evidence_api.Report, error) {
func (s *SDK) GetCCReport(nonce, userData []byte, extraArgs map[string]any) (evidence_api.Report, error) {
if s.cvm == nil {
return nil, errors.New("no available cvm in sdk")
}
Expand Down
2 changes: 1 addition & 1 deletion src/golang/cctrusted_vm/tdx/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (t *TDXDevice) initDevice() error {
}

// Report implements cctrusted_vm.Device, get CC report
func (t *TDXDevice) Report(nonce, userData string, extraArgs map[string]any) (evidence_api.CcReport, error) {
func (t *TDXDevice) Report(nonce, userData []byte, extraArgs map[string]any) (evidence_api.CcReport, error) {
var resp evidence_api.CcReport
var err error

Expand Down
4 changes: 2 additions & 2 deletions src/golang/cctrusted_vm/tdx/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestReport15(t *testing.T) {
fmt.Println(res)
assert.Equal(t, true, res)

nonce := "IXUKoBO1UM3c1wopN4sY"
userData := "MTIzNDU2NzgxMjM0NTY3ODEyMzQ1Njc4MTIzNDU2NzgxMjM0NTY3ODEyMzQ1Njc4"
nonce := []byte{"IXUKoBO1UM3c1wopN4sY"}
userData := []byte{"MTIzNDU2NzgxMjM0NTY3ODEyMzQ1Njc4MTIzNDU2NzgxMjM0NTY3ODEyMzQ1Njc4"}
tdreport, err := device.TdReport(nonce, userData)
assert.Nil(t, err)
t.Log(tdreport)
Expand Down
12 changes: 6 additions & 6 deletions src/golang/cctrusted_vm/tdx/quote_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type QuoteHandler interface {
Quote([tdx.TD_REPORT_LEN]byte) ([]byte, error)
// TdReport gets the td report of the td vm, where nonce ad userData
// are encoded in base64
TdReport(nonce, userData string) ([tdx.TD_REPORT_LEN]byte, error)
TdReport(nonce, userData []byte) ([tdx.TD_REPORT_LEN]byte, error)
}

var _ QuoteHandler = (*QuoteHandler15)(nil)
Expand All @@ -34,7 +34,7 @@ type QuoteHandler15 struct {
}

// TdReport implements QuoteHandler.
func (q *QuoteHandler15) TdReport(nonce, userData string) ([tdx.TD_REPORT_LEN]byte, error) {
func (q *QuoteHandler15) TdReport(nonce, userData []byte) ([tdx.TD_REPORT_LEN]byte, error) {
tdreport := [tdx.TD_REPORT_LEN]uint8{}
var err error
var file *os.File
Expand All @@ -55,18 +55,18 @@ func (q *QuoteHandler15) TdReport(nonce, userData string) ([tdx.TD_REPORT_LEN]by
// check if the data is base64 encoded, if yes, decode before doing hash
hasher := sha512.New()
if len(nonce) > 0 {
nonceDecoded, err := base64.StdEncoding.DecodeString(nonce)
nonceDecoded, err := base64.StdEncoding.DecodeString(string(nonce))
if err != nil {
hasher.Write([]byte(nonce))
hasher.Write(nonce)
} else {
hasher.Write(nonceDecoded)
}
}

if len(userData) > 0 {
userDataDecoded, err := base64.StdEncoding.DecodeString(userData)
userDataDecoded, err := base64.StdEncoding.DecodeString(string(userData))
if err != nil {
hasher.Write([]byte(userData))
hasher.Write(userData)
} else {
hasher.Write(userDataDecoded)
}
Expand Down

0 comments on commit 299fd3a

Please sign in to comment.