From 1ac7f1bad7596bc0cc489d85de8bdf5d195b99a3 Mon Sep 17 00:00:00 2001
From: Rasmus Dahlberg <rasmus.dahlberg@kau.se>
Date: Mon, 31 May 2021 23:56:49 +0200
Subject: added types and fixed an error

- The wrong signature header was used for leaves
- Added SigIdent marshalling
- Added a wrapper that uses the log's hash function
---
 types/ascii.go      | 21 ++++++++++++++-------
 types/ascii_test.go |  6 +++---
 types/util.go       | 13 +++++++++++++
 3 files changed, 30 insertions(+), 10 deletions(-)
 create mode 100644 types/util.go

(limited to 'types')

diff --git a/types/ascii.go b/types/ascii.go
index 88b372e..565e52d 100644
--- a/types/ascii.go
+++ b/types/ascii.go
@@ -200,7 +200,7 @@ func (l *Leaf) MarshalASCII(w io.Writer) error {
 	if err := writeASCII(w, Checksum, hex.EncodeToString(l.Checksum[:])); err != nil {
 		return fmt.Errorf("writeASCII: %v", err)
 	}
-	if err := writeASCII(w, Signature, hex.EncodeToString(l.Signature[:])); err != nil {
+	if err := writeASCII(w, SignatureOverMessage, hex.EncodeToString(l.Signature[:])); err != nil {
 		return fmt.Errorf("writeASCII: %v", err)
 	}
 	if err := writeASCII(w, KeyHash, hex.EncodeToString(l.KeyHash[:])); err != nil {
@@ -220,16 +220,23 @@ func (sth *SignedTreeHead) MarshalASCII(w io.Writer) error {
 		return fmt.Errorf("writeASCII: %v", err)
 	}
 	for _, sigident := range sth.SigIdent {
-		if err := writeASCII(w, Signature, hex.EncodeToString(sigident.Signature[:])); err != nil {
-			return fmt.Errorf("writeASCII: %v", err)
-		}
-		if err := writeASCII(w, KeyHash, hex.EncodeToString(sigident.KeyHash[:])); err != nil {
-			return fmt.Errorf("writeASCII: %v", err)
+		if err := sigident.MarshalASCII(w); err != nil {
+			return fmt.Errorf("MarshalASCII: %v", err)
 		}
 	}
 	return nil
 }
 
+func (si *SigIdent) MarshalASCII(w io.Writer) error {
+	if err := writeASCII(w, Signature, hex.EncodeToString(si.Signature[:])); err != nil {
+		return fmt.Errorf("writeASCII: %v", err)
+	}
+	if err := writeASCII(w, KeyHash, hex.EncodeToString(si.KeyHash[:])); err != nil {
+		return fmt.Errorf("writeASCII: %v", err)
+	}
+	return nil
+}
+
 func (p *ConsistencyProof) MarshalASCII(w io.Writer) error {
 	if err := writeASCII(w, NewSize, strconv.FormatUint(p.NewSize, 10)); err != nil {
 		return fmt.Errorf("writeASCII: %v", err)
@@ -385,7 +392,7 @@ func (req *LeafRequest) UnmarshalASCII(r io.Reader) error {
 	if req.Checksum, err = msg.GetHash(Checksum); err != nil {
 		return fmt.Errorf("GetHash(Checksum): %v", err)
 	}
-	if req.Signature, err = msg.GetSignature(Signature); err != nil {
+	if req.Signature, err = msg.GetSignature(SignatureOverMessage); err != nil {
 		return fmt.Errorf("GetSignature: %v", err)
 	}
 	if req.VerificationKey, err = msg.GetVerificationKey(VerificationKey); err != nil {
diff --git a/types/ascii_test.go b/types/ascii_test.go
index 74a1e37..92732f9 100644
--- a/types/ascii_test.go
+++ b/types/ascii_test.go
@@ -106,12 +106,12 @@ func TestLeafMarshalASCII(t *testing.T) {
 		// Leaf 1
 		ShardHint, Delim, 123, EOL,
 		Checksum, Delim, testBuffer32[:], EOL,
-		Signature, Delim, testBuffer64[:], EOL,
+		SignatureOverMessage, Delim, testBuffer64[:], EOL,
 		KeyHash, Delim, testBuffer32[:], EOL,
 		// Leaf 2
 		ShardHint, Delim, 456, EOL,
 		Checksum, Delim, testBuffer32[:], EOL,
-		Signature, Delim, testBuffer64[:], EOL,
+		SignatureOverMessage, Delim, testBuffer64[:], EOL,
 		KeyHash, Delim, testBuffer32[:], EOL,
 	))
 	buf := bytes.NewBuffer(nil)
@@ -399,7 +399,7 @@ func TestLeafRequestUnmarshalASCII(t *testing.T) {
 				"%s%s%d%s"+"%s%s%x%s"+"%s%s%x%s"+"%s%s%x%s"+"%s%s%s%s",
 				ShardHint, Delim, 123, EOL,
 				Checksum, Delim, testBuffer32[:], EOL,
-				Signature, Delim, testBuffer64[:], EOL,
+				SignatureOverMessage, Delim, testBuffer64[:], EOL,
 				VerificationKey, Delim, testBuffer32[:], EOL,
 				DomainHint, Delim, "example.com", EOL,
 			)),
diff --git a/types/util.go b/types/util.go
new file mode 100644
index 0000000..dc8ccba
--- /dev/null
+++ b/types/util.go
@@ -0,0 +1,13 @@
+package types
+
+import (
+	"crypto/sha256"
+)
+
+func Hash(buf []byte) *[HashSize]byte {
+	var ret [HashSize]byte
+	hash := sha256.New()
+	hash.Write(buf)
+	copy(ret[:], hash.Sum(nil))
+	return &ret
+}
-- 
cgit v1.2.3