-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add treeID, index, and depth to HashLeaf and HashEmtpy. The fuffills the security requirements in #670.
- Loading branch information
Showing
19 changed files
with
238 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2016 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package coniks provides hashing for maps. | ||
package coniks | ||
|
||
import ( | ||
"crypto" | ||
"encoding/binary" | ||
|
||
"github.com/google/trillian/merkle" | ||
) | ||
|
||
// Domain separation prefixes | ||
var ( | ||
leafIdentifier = []byte("L") | ||
emptyIdentifier = []byte("E") | ||
) | ||
|
||
// Default is the standard CONIKS hasher. | ||
var Default = New(crypto.SHA512_256) | ||
|
||
// hasher implements the sparse merkel tree hashing algorithm specified in the CONIKS paper. | ||
type hasher struct { | ||
crypto.Hash | ||
} | ||
|
||
// New creates a new merkle.TreeHasher using the passed in hash function. | ||
func New(h crypto.Hash) merkle.MapHasher { | ||
return &hasher{Hash: h} | ||
} | ||
|
||
// EmptyRoot returns the root of an empty tree. | ||
func (m *hasher) EmptyRoot() []byte { | ||
panic("EmptyRoot() not defined for coniks.Hasher") | ||
} | ||
|
||
// HashEmpty returns the hash of an empty branch at a given depth. | ||
// A depth of 0 indicates the hash of an empty leaf. | ||
// Empty branches within the tree are plain interior nodes e1 = H(e0, e0) etc. | ||
func (m *hasher) HashEmpty(treeID int64, index []byte, height int) []byte { | ||
depth := m.BitLen() - height | ||
|
||
h := m.New() | ||
h.Write(emptyIdentifier) | ||
binary.Write(h, binary.BigEndian, uint64(treeID)) | ||
h.Write(index) // TODO block out the bits that are not part of the index. | ||
binary.Write(h, binary.BigEndian, uint32(depth)) | ||
return h.Sum(nil) | ||
} | ||
|
||
// HashLeaf calculate the merkle tree leaf value: | ||
// H(Identifier || treeID || depth || index || dataHash) | ||
func (m *hasher) HashLeaf(treeID int64, index []byte, height int, leaf []byte) []byte { | ||
depth := m.BitLen() - height | ||
|
||
h := m.New() | ||
h.Write(leafIdentifier) | ||
binary.Write(h, binary.BigEndian, uint64(treeID)) | ||
h.Write(index) // TODO block out the bits that are not part of the index. | ||
binary.Write(h, binary.BigEndian, uint32(depth)) | ||
h.Write(leaf) | ||
return h.Sum(nil) | ||
} | ||
|
||
// HashChildren returns the inner Merkle tree node hash of the the two child nodes l and r. | ||
// The hashed structure is H(l || r). | ||
func (m *hasher) HashChildren(l, r []byte) []byte { | ||
h := m.New() | ||
h.Write(l) | ||
h.Write(r) | ||
return h.Sum(nil) | ||
} | ||
|
||
// BitLen returns the number of bits in the hash function. | ||
func (m *hasher) BitLen() int { | ||
return m.Size() * 8 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2016 Google Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package coniks | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"testing" | ||
) | ||
|
||
// h2b converts a hex string into a bytes string | ||
func h2b(h string) []byte { | ||
b, err := hex.DecodeString(h) | ||
if err != nil { | ||
panic("invalid hex string") | ||
} | ||
return b | ||
} | ||
|
||
func TestVectors(t *testing.T) { | ||
for _, tc := range []struct { | ||
treeID int64 | ||
index []byte | ||
depth int | ||
leaf []byte | ||
want []byte | ||
}{ | ||
{treeID: 0, index: []byte("foo"), depth: 128, leaf: []byte("leaf"), want: h2b("2d6c9f648b61e786e18bcba49d1dc62dee2020cec168f0d2c9e47a7bd4633f02")}, | ||
} { | ||
height := Default.BitLen() - tc.depth | ||
if got, want := Default.HashLeaf(tc.treeID, tc.index, height, tc.leaf), tc.want; !bytes.Equal(got, want) { | ||
t.Errorf("HashLeaf(%v, %s, %v, %s): %x, want %x", tc.treeID, tc.index, tc.depth, tc.leaf, got, want) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.