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/leaf.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/leaf.go')
-rw-r--r-- | pkg/types/leaf.go | 118 |
1 files changed, 0 insertions, 118 deletions
diff --git a/pkg/types/leaf.go b/pkg/types/leaf.go deleted file mode 100644 index 2ba9299..0000000 --- a/pkg/types/leaf.go +++ /dev/null @@ -1,118 +0,0 @@ -package types - -import ( - "crypto" - "crypto/ed25519" - "encoding/binary" - "fmt" - "io" - - "git.sigsum.org/sigsum-go/pkg/ascii" -) - -type Statement struct { - ShardHint uint64 `ascii:"shard_hint"` - Checksum Hash `ascii:"checksum"` -} - -type Leaf struct { - Statement - Signature Signature `ascii:"signature"` - KeyHash Hash `ascii:"key_hash"` -} - -type Leaves []Leaf - -func (s *Statement) ToBinary() []byte { - namespace := fmt.Sprintf("tree_leaf:v0:%d@sigsum.org", s.ShardHint) - b := make([]byte, 6+4+len(namespace)+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(s.Checksum[:])) - - return b -} - -func (s *Statement) Sign(signer crypto.Signer) (*Signature, error) { - sig, err := signer.Sign(nil, s.ToBinary(), crypto.Hash(0)) - if err != nil { - return nil, fmt.Errorf("types: failed signing statement") - } - - var signature Signature - copy(signature[:], sig) - return &signature, nil -} - -func (s *Statement) Verify(key *PublicKey, sig *Signature) bool { - return ed25519.Verify(ed25519.PublicKey(key[:]), s.ToBinary(), sig[:]) -} - -func (l *Leaf) ToBinary() []byte { - b := make([]byte, 136) - binary.BigEndian.PutUint64(b[0:8], l.ShardHint) - copy(b[8:40], l.Checksum[:]) - copy(b[40:104], l.Signature[:]) - copy(b[104:136], l.KeyHash[:]) - return b -} - -func (l *Leaf) FromBinary(b []byte) error { - if len(b) != 136 { - return fmt.Errorf("types: invalid leaf size: %d", len(b)) - } - - l.ShardHint = binary.BigEndian.Uint64(b[0:8]) - copy(l.Checksum[:], b[8:40]) - copy(l.Signature[:], b[40:104]) - copy(l.KeyHash[:], b[104:136]) - return nil -} - -func (l *Leaf) ToASCII(w io.Writer) error { - return ascii.StdEncoding.Serialize(w, l) -} - -func (l *Leaf) FromASCII(r io.Reader) error { - return ascii.StdEncoding.Deserialize(r, l) -} - -func (l *Leaves) FromASCII(r io.Reader) error { - leaves := &struct { - ShardHint []uint64 `ascii:"shard_hint"` - Checksum []Hash `ascii:"checksum"` - Signature []Signature `ascii:"signature"` - KeyHash []Hash `ascii:"key_hash"` - }{} - - if err := ascii.StdEncoding.Deserialize(r, leaves); err != nil { - return err - } - n := len(leaves.ShardHint) - if n != len(leaves.Checksum) { - return fmt.Errorf("types: mismatched leaf field counts") - } - if n != len(leaves.Signature) { - return fmt.Errorf("types: mismatched leaf field counts") - } - if n != len(leaves.KeyHash) { - return fmt.Errorf("types: mismatched leaf field counts") - } - - *l = make([]Leaf, 0, n) - for i := 0; i < n; i++ { - *l = append(*l, Leaf{ - Statement: Statement{ - ShardHint: leaves.ShardHint[i], - Checksum: leaves.Checksum[i], - }, - Signature: leaves.Signature[i], - KeyHash: leaves.KeyHash[i], - }) - } - return nil -} |