Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shorter handle representation. #68

Merged
merged 5 commits into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions drbg/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ func (s *Seed) MarshalBinary() ([]byte, error) {
return s.value[:], nil
}

// MarshalJSON serializes the seed to JSON
// Implements the json.Marshaller interface
func (s *Seed) MarshalJSON() ([]byte, error) {
// MarshalText serializes the seed to textual representation
func (s *Seed) MarshalText() ([]byte, error) {
return json.Marshal(s.value)
}

// UnmarshalJSON restores the seed from a JSON representation.
func (s *Seed) UnmarshalJSON(data []byte) error {
// UnmarshalText restores the seed from a Text representation.
func (s *Seed) UnmarshalText(data []byte) error {
if err := json.Unmarshal(data, &s.value); err != nil {
return err
}
Expand Down
39 changes: 39 additions & 0 deletions libtalek/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libtalek
import (
"encoding/binary"
"errors"
"fmt"
"io"

"github.com/agl/ed25519"
Expand Down Expand Up @@ -184,3 +185,41 @@ func (h *Handle) retrieveResponse(args *common.ReadArgs, reply *common.ReadReply
}
return nil
}

// MarshalText is a compact textual representation of a handle
func (h *Handle) MarshalText() ([]byte, error) {
s1, err := h.Seed1.MarshalBinary()
if err != nil {
return nil, err
}
s2, err := h.Seed2.MarshalBinary()
if err != nil {
return nil, err
}
txt := fmt.Sprintf("%x.%x.%x.%x.%d", s1, s2, *h.SharedSecret, *h.SigningPublicKey, h.Seqno)
return []byte(txt), nil
}

// UnmarshalText restores a handle from its compact textual representation
func (h *Handle) UnmarshalText(text []byte) error {
var s1, s2, ss, pk []byte
if n, err := fmt.Sscanf(string(text), "%x.%x.%x.%x.%d", &s1, &s2, &ss, &pk, &h.Seqno); n < 4 || err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

n<5?

if err != nil {
return err
}
return errors.New("invalid handle")
}
h.SharedSecret = new([32]byte)
copy(h.SharedSecret[:], ss)
h.SigningPublicKey = new([32]byte)
copy(h.SigningPublicKey[:], pk)
h.Seed1 = &drbg.Seed{}
h.Seed2 = &drbg.Seed{}
if err := h.Seed1.UnmarshalBinary(s1); err != nil {
return err
}
if err := h.Seed2.UnmarshalBinary(s2); err != nil {
return err
}
return nil
}
21 changes: 21 additions & 0 deletions libtalek/handle_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package libtalek

import (
"bytes"
"crypto/rand"
"fmt"
"testing"
Expand Down Expand Up @@ -39,6 +40,26 @@ func TestGeneratePoll(t *testing.T) {
fmt.Printf("... done \n")
}

func TestSerialization(t *testing.T) {
topic, _ := NewTopic()
h := topic.Handle

txt, err := h.MarshalText()
if err != nil {
t.Fatalf("Error serializing: %v\n", err)
}
fmt.Printf("Serialized handle looks like %s\n", txt)

h2, _ := NewHandle()
err = h2.UnmarshalText(txt)
if err != nil {
t.Fatalf("Could not deserialize: %v\n", err)
}
if !bytes.Equal(h.SharedSecret[:], h2.SharedSecret[:]) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check other fields?

t.Fatalf("serialization log info!")
}
}

func BenchmarkGeneratePollN10K(b *testing.B) {
HelperBenchmarkGeneratePoll(b, 10000/4)
}
Expand Down
20 changes: 20 additions & 0 deletions libtalek/topic.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package libtalek

import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"

"github.com/agl/ed25519"
"github.com/privacylab/talek/common"
Expand Down Expand Up @@ -111,3 +113,21 @@ func (t *Topic) encrypt(plaintext []byte, nonce *[24]byte) ([]byte, error) {
digest := ed25519.Sign(t.SigningPrivateKey, buf)
return append(buf, digest[:]...), nil
}

// MarshalText is a compact textual representation of a topic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test?

func (t *Topic) MarshalText() ([]byte, error) {
handle, err := t.Handle.MarshalText()
if err != nil {
return nil, err
}
txt := fmt.Sprintf("%x.", *t.SigningPrivateKey)
return append([]byte(txt), handle...), nil
}

// UnmarshalText restores a topic from its compact textual representation
func (t *Topic) UnmarshalText(text []byte) error {
parts := bytes.SplitN(text, []byte("."), 1)
t.SigningPrivateKey = new([64]byte)
copy(t.SigningPrivateKey[:], parts[0])
return t.Handle.UnmarshalText(parts[1])
}