diff options
author | Rasmus Dahlberg <rasmus@mullvad.net> | 2022-04-25 00:43:06 +0200 |
---|---|---|
committer | Rasmus Dahlberg <rasmus@mullvad.net> | 2022-04-25 00:43:06 +0200 |
commit | 528a53f7f76f08af5902f4cfa8235380b3434ba0 (patch) | |
tree | 662b7834d5ce15627554e9307a4e00f7364fba11 /pkg/types/tree_head.go | |
parent | 4fc0ff2ec2f48519ee245d6d7edee1921cb3b8bc (diff) |
drafty types refactor with simple ascii packagergdd/sketch
types.go compiles but that is about it, here be dragons. Pushing so
that we can get an idea of what this refactor would roughly look like.
Diffstat (limited to 'pkg/types/tree_head.go')
-rw-r--r-- | pkg/types/tree_head.go | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/pkg/types/tree_head.go b/pkg/types/tree_head.go deleted file mode 100644 index de62526..0000000 --- a/pkg/types/tree_head.go +++ /dev/null @@ -1,90 +0,0 @@ -package types - -import ( - "crypto" - "crypto/ed25519" - "encoding/binary" - "fmt" - "io" - - "git.sigsum.org/sigsum-go/pkg/ascii" - "git.sigsum.org/sigsum-go/pkg/hex" -) - -type TreeHead struct { - Timestamp uint64 `ascii:"timestamp"` - TreeSize uint64 `ascii:"tree_size"` - RootHash Hash `ascii:"root_hash"` -} - -type SignedTreeHead struct { - TreeHead - Signature Signature `ascii:"signature"` -} - -type CosignedTreeHead struct { - SignedTreeHead - Cosignature []Signature `ascii:"cosignature"` - KeyHash []Hash `ascii:"key_hash"` -} - -func (th *TreeHead) toBinary() []byte { - b := make([]byte, 48) - binary.BigEndian.PutUint64(b[0:8], th.Timestamp) - binary.BigEndian.PutUint64(b[8:16], th.TreeSize) - copy(b[16:48], th.RootHash[:]) - return b -} - -func (th *TreeHead) ToBinary(keyHash *Hash) []byte { - namespace := fmt.Sprintf("tree_head:v0:%s@sigsum.org", hex.Serialize(keyHash[:])) // length 88 - b := make([]byte, 6+4+88+4+0+4+6+4+HashSize) - - copy(b[0:6], "SSHSIG") - i := 6 - i += putSSHString(b[i:], namespace) - i += putSSHString(b[i:], "") - i += putSSHString(b[i:], "sha256") - i += putSSHString(b[i:], string((*HashFn(th.toBinary()))[:])) - - return b -} - -func (th *TreeHead) Sign(s crypto.Signer, kh *Hash) (*SignedTreeHead, error) { - sig, err := s.Sign(nil, th.ToBinary(kh), crypto.Hash(0)) - if err != nil { - return nil, fmt.Errorf("types: failed signing tree head") - } - - sth := &SignedTreeHead{ - TreeHead: *th, - } - copy(sth.Signature[:], sig) - return sth, nil -} - -func (sth *SignedTreeHead) ToASCII(w io.Writer) error { - return ascii.StdEncoding.Serialize(w, sth) -} - -func (sth *SignedTreeHead) FromASCII(r io.Reader) error { - return ascii.StdEncoding.Deserialize(r, sth) -} - -func (sth *SignedTreeHead) Verify(key *PublicKey, kh *Hash) bool { - return ed25519.Verify(ed25519.PublicKey(key[:]), sth.TreeHead.ToBinary(kh), sth.Signature[:]) -} - -func (cth *CosignedTreeHead) ToASCII(w io.Writer) error { - return ascii.StdEncoding.Serialize(w, cth) -} - -func (cth *CosignedTreeHead) FromASCII(r io.Reader) error { - if err := ascii.StdEncoding.Deserialize(r, cth); err != nil { - return err - } - if len(cth.Cosignature) != len(cth.KeyHash) { - return fmt.Errorf("types: mismatched cosignature count") - } - return nil -} |