Skip to content

Commit

Permalink
Override STR.SerializeWithAd of the STR struct for signing
Browse files Browse the repository at this point in the history
  • Loading branch information
vqhuy authored and arlolra committed Jan 20, 2017
1 parent 0bedfc6 commit deb0140
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
2 changes: 1 addition & 1 deletion client/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestUnmarshalSampleMessage(t *testing.T) {
msg, _ := keyserver.MarshalResponse(res)
response := UnmarshalResponse(protocol.RegistrationType, []byte(msg))
str := response.DirectoryResponse.(*protocol.DirectoryProof).STR
if str.Ad == nil || !bytes.Equal(str.Ad.Serialize(), str.Policies.Serialize()) {
if !bytes.Equal(d.LatestSTR().Serialize(), str.Serialize()) {
t.Error("Cannot unmarshal Associate Data properly")
}
}
15 changes: 12 additions & 3 deletions merkletree/str.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,26 @@ func NewSTR(key sign.PrivateKey, ad AssocData, m *MerkleTree, epoch uint64, prev
return str
}

// Serialize serializes the signed tree root into
// a specified format for signing.
// Serialize serializes the signed tree root
// and its associated data into a specified format for signing.
// One should use this function for signing as well as
// verifying the signature.
// Any composition struct of SignedTreeRoot with
// a specific AssocData should override this method.
func (str *SignedTreeRoot) Serialize() []byte {
return append(str.SerializeInternal(), str.Ad.Serialize()...)
}

// SerializeInternal serializes the signed tree root into
// a specified format.
func (str *SignedTreeRoot) SerializeInternal() []byte {
var strBytes []byte
strBytes = append(strBytes, utils.ULongToBytes(str.Epoch)...) // t - epoch number
if str.Epoch > 0 {
strBytes = append(strBytes, utils.ULongToBytes(str.PreviousEpoch)...) // t_prev - previous epoch number
}
strBytes = append(strBytes, str.TreeHash...) // root
strBytes = append(strBytes, str.PreviousSTRHash...) // previous STR hash
strBytes = append(strBytes, str.Ad.Serialize()...)
return strBytes
}

Expand Down
2 changes: 1 addition & 1 deletion merkletree/str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func TestVerifyHashChain(t *testing.T) {

keyPrefix := "key"
valuePrefix := []byte("value")
pad, err := NewPAD(TestAd{""}, signKey, vrfPrivKey1, 10)
pad, err := NewPAD(TestAd{"abc"}, signKey, vrfPrivKey1, 10)
if err != nil {
t.Fatal(err)
}
Expand Down
26 changes: 6 additions & 20 deletions protocol/str.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package protocol

import (
"encoding/json"

"github.com/coniks-sys/coniks-go/merkletree"
)
import "github.com/coniks-sys/coniks-go/merkletree"

// DirSTR disambiguates merkletree.SignedTreeRoot's AssocData interface,
// for the purpose of exporting and unmarshalling.
Expand All @@ -21,22 +17,12 @@ func NewDirSTR(str *merkletree.SignedTreeRoot) *DirSTR {
}
}

// Serialize overrides merkletree.SignedTreeRoot.Serialize
func (str *DirSTR) Serialize() []byte {
return append(str.SerializeInternal(), str.Policies.Serialize()...)
}

// VerifyHashChain wraps merkletree.SignedTreeRoot.VerifyHashChain
func (str *DirSTR) VerifyHashChain(savedSTR *DirSTR) bool {
return str.SignedTreeRoot.VerifyHashChain(savedSTR.SignedTreeRoot)
}

// UnmarshalJSON fills in the unexported Ad interface from the underlying
// merkletree.SignedTreeRoot. This is necessary since, for now, Serialize
// and VerifyHashChain dispatch to methods which dereference it.
func (str *DirSTR) UnmarshalJSON(m []byte) error {
// Use an alias to avoid an infinite loop
type DirSTR2 DirSTR
str2 := &DirSTR2{}
if err := json.Unmarshal(m, str2); err != nil {
return err
}
str2.Ad = str2.Policies
*str = DirSTR(*str2)
return nil
}

0 comments on commit deb0140

Please sign in to comment.