Skip to content

Commit

Permalink
parsesnp: add slog
Browse files Browse the repository at this point in the history
  • Loading branch information
malt3 committed Dec 20, 2023
1 parent 1d68e9d commit 3e7956e
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions tools/parsesnp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/hex"
"fmt"
"io"
"log"
"log/slog"
"os"

"github.com/google/go-sev-guest/abi"
Expand Down Expand Up @@ -45,13 +45,26 @@ type Report struct {
}

func main() {
if err := run(); err != nil {
os.Exit(1)
}
}

func run() (retErr error) {
logger := slog.Default()
defer func() {
if retErr != nil {
logger.Error(retErr.Error())
}
}()

data, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("failed to read input: %v", err)
return fmt.Errorf("failed to read input: %w", err)
}
data, err = hex.DecodeString(string(data))
if err != nil {
log.Fatalf("failed to decode input: %v", err)
return fmt.Errorf("failed to decode input: %w", err)
}

var r Report
Expand Down Expand Up @@ -79,11 +92,11 @@ func main() {

signerInfo, err := abi.ParseSignerInfo(binary.LittleEndian.Uint32(data[0x48:0x4C]))
if err != nil {
log.Printf("failed to parse signer info: %v", err)
return fmt.Errorf("failed to parse signer info: %w", err)
}
r.SignerInfo = abi.ComposeSignerInfo(signerInfo)
if err := mbz(data, 0x4C, 0x50); err != nil {
log.Printf("%v", err)
return err
}
fmt.Printf("SignerInfo: %v\n", r.SignerInfo)
r.ReportData = clone(data[0x50:0x90])
Expand All @@ -102,7 +115,7 @@ func main() {
fmt.Printf("ReportIdMa: %x\n", r.ReportIdMa)
r.ReportedTcb = binary.LittleEndian.Uint64(data[0x180:0x188])
if err := mbz(data, 0x188, 0x1A0); err != nil {
fmt.Printf("%v", err)
return err
}

r.ChipId = clone(data[0x1A0:0x1E0])
Expand All @@ -116,7 +129,7 @@ func main() {
r.CurrentMajor = uint32(data[0x1EA])
fmt.Printf("CurrentMajor: %d\n", r.CurrentMajor)
if err := mbz(data, 0x1EB, 0x1EC); err != nil {
fmt.Printf("%v", err)
return err
}
r.CommittedBuild = uint32(data[0x1EC])
fmt.Printf("CommittedBuild: %d\n", r.CommittedBuild)
Expand All @@ -125,20 +138,21 @@ func main() {
r.CommittedMajor = uint32(data[0x1EE])
fmt.Printf("CommittedMajor: %d\n", r.CommittedMajor)
if err := mbz(data, 0x1EF, 0x1F0); err != nil {
fmt.Printf("%v", err)
return err
}
r.LaunchTcb = binary.LittleEndian.Uint64(data[0x1F0:0x1F8])
if err := mbz(data, 0x1F8, signatureOffset); err != nil {
fmt.Printf("%v", err)
return err
}

if r.SignatureAlgo == abi.SignEcdsaP384Sha384 {
if err := mbz(data, signatureOffset+abi.EcdsaP384Sha384SignatureSize, abi.ReportSize); err != nil {
fmt.Printf("%v", err)
return err
}
}
r.Signature = clone(data[signatureOffset:abi.ReportSize])
fmt.Printf("Signature: %x\n", r.Signature)
return nil
}

func clone(b []byte) []byte {
Expand Down

0 comments on commit 3e7956e

Please sign in to comment.