aboutsummaryrefslogtreecommitdiff
path: root/log_parameters.go
diff options
context:
space:
mode:
authorRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-02-25 14:36:35 +0100
committerRasmus Dahlberg <rasmus.dahlberg@kau.se>2021-02-25 14:36:35 +0100
commitc05c22ddbc771e7713849cae40f9d91bfafa0503 (patch)
treeb97d11ab2a914806e6f671f9aff1cab9767b2eab /log_parameters.go
parentc9b4b43654f0ff26207cc63449f13298cd3c56e8 (diff)
major refactor based on README.md and TODOs
Updated types, improved units tests, isolated most test data to have it in one place, renamed and created new files to improve readability, and fixed a bunch of minor TODOs.
Diffstat (limited to 'log_parameters.go')
-rw-r--r--log_parameters.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/log_parameters.go b/log_parameters.go
new file mode 100644
index 0000000..86ac0cc
--- /dev/null
+++ b/log_parameters.go
@@ -0,0 +1,71 @@
+package stfe
+
+import (
+ "crypto"
+ "fmt"
+ "time"
+
+ "crypto/rand"
+
+ "github.com/system-transparency/stfe/types"
+)
+
+// LogParameters is a collection of log parameters
+type LogParameters struct {
+ LogId *types.Namespace // log identifier
+ LogIdBytes []byte // serialized log id
+ TreeId int64 // used internally by Trillian
+ Prefix string // e.g., "test" for <base>/test
+ MaxRange int64 // max entries per get-entries request
+ Submitters *types.NamespacePool // trusted submitters
+ Witnesses *types.NamespacePool // trusted witnesses
+ Deadline time.Duration // gRPC deadline
+ Interval time.Duration // cosigning sth frequency
+ HashType crypto.Hash // hash function used by Trillian
+ Signer crypto.Signer // access to Ed25519 private key
+}
+
+// NewLogParameters creates newly initialized log parameters
+func NewLogParameters(signer crypto.Signer, logId *types.Namespace, treeId int64, prefix string, submitters, witnesses *types.NamespacePool, maxRange int64, interval, deadline time.Duration) (*LogParameters, error) {
+ logIdBytes, err := types.Marshal(*logId)
+ if err != nil {
+ return nil, fmt.Errorf("Marshal failed for log identifier: %v", err)
+ }
+ return &LogParameters{
+ LogId: logId,
+ TreeId: treeId,
+ Prefix: prefix,
+ MaxRange: maxRange,
+ Submitters: submitters,
+ Witnesses: witnesses,
+ Deadline: deadline,
+ Interval: interval,
+ HashType: crypto.SHA256,
+ Signer: signer,
+ LogIdBytes: logIdBytes,
+ }, nil
+}
+
+// SignTreeHeadV1 signs a TreeHeadV1 structure
+func (lp *LogParameters) SignTreeHeadV1(th *types.TreeHeadV1) (*types.StItem, error) {
+ serialized, err := types.Marshal(*th)
+ if err != nil {
+ return nil, fmt.Errorf("Marshal failed for TreeHeadV1: %v", err)
+ }
+ sig, err := lp.Signer.Sign(rand.Reader, serialized, crypto.Hash(0))
+ if err != nil {
+ return nil, fmt.Errorf("Sign failed: %v", err)
+ }
+ lastSthTimestamp.Set(float64(time.Now().Unix()), string(lp.LogIdBytes))
+ lastSthSize.Set(float64(th.TreeSize), string(lp.LogIdBytes))
+ return &types.StItem{
+ Format: types.StFormatSignedTreeHeadV1,
+ SignedTreeHeadV1: &types.SignedTreeHeadV1{
+ TreeHead: *th,
+ Signature: types.SignatureV1{
+ Namespace: *lp.LogId,
+ Signature: sig,
+ },
+ },
+ }, nil
+}