Skip to content

Commit d58be1c

Browse files
FiloSottilegopherbot
authored andcommitted
sumdb/tlog: set the hash of the empty tree according to RFC 6962
Updates FiloSottile/sunlight#14 Change-Id: I712ea53fd3a17b66ec310d8f48de44416d0054cc Reviewed-on: https://go-review.googlesource.com/c/mod/+/590715 Reviewed-by: Russ Cox <[email protected]> Auto-Submit: Filippo Valsorda <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Joedian Reid <[email protected]>
1 parent 232e49f commit d58be1c

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

sumdb/client.go

+8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ func (c *Client) initWork() {
142142
c.verifiers = note.VerifierList(verifier)
143143
c.name = verifier.Name()
144144

145+
if c.latest.N == 0 {
146+
c.latest.Hash, err = tlog.TreeHash(0, nil)
147+
if err != nil {
148+
c.initErr = err
149+
return
150+
}
151+
}
152+
145153
data, err := c.ops.ReadConfig(c.name + "/latest")
146154
if err != nil {
147155
c.initErr = err

sumdb/tlog/tlog.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,22 @@ func (f HashReaderFunc) ReadHashes(indexes []int64) ([]Hash, error) {
234234
return f(indexes)
235235
}
236236

237+
// emptyHash is the hash of the empty tree, per RFC 6962, Section 2.1.
238+
// It is the hash of the empty string.
239+
var emptyHash = Hash{
240+
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
241+
0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
242+
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
243+
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
244+
}
245+
237246
// TreeHash computes the hash for the root of the tree with n records,
238247
// using the HashReader to obtain previously stored hashes
239248
// (those returned by StoredHashes during the writes of those n records).
240249
// TreeHash makes a single call to ReadHash requesting at most 1 + log₂ n hashes.
241-
// The tree of size zero is defined to have an all-zero Hash.
242250
func TreeHash(n int64, r HashReader) (Hash, error) {
243251
if n == 0 {
244-
return Hash{}, nil
252+
return emptyHash, nil
245253
}
246254
indexes := subTreeIndex(0, n, nil)
247255
hashes, err := r.ReadHashes(indexes)

sumdb/tlog/tlog_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package tlog
66

77
import (
88
"bytes"
9+
"crypto/sha256"
910
"fmt"
1011
"testing"
1112
)
@@ -267,3 +268,13 @@ func TestTilePath(t *testing.T) {
267268
}
268269
}
269270
}
271+
272+
func TestEmptyTree(t *testing.T) {
273+
h, err := TreeHash(0, nil)
274+
if err != nil {
275+
t.Fatal(err)
276+
}
277+
if h != sha256.Sum256(nil) {
278+
t.Fatalf("TreeHash(0) = %x, want SHA-256('')", h)
279+
}
280+
}

0 commit comments

Comments
 (0)