Skip to content
Snippets Groups Projects
Commit 14a57593 authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

continued progress

parent 2b011c28
No related branches found
No related tags found
No related merge requests found
Showing
with 290 additions and 15 deletions
package main
package cmix
import (
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/xx_network/primitives/id"
"time"
)
const currentKeyVersion = 0
type key struct {
k *cyclic.Int
}
// loads the key for the given node id from the versioned keystore
func loadKey(kv *versioned.KV, id *id.ID) (*key, error) {
k := &key{}
key := keyKey(id)
obj, err := kv.Get(key)
if err != nil {
return nil, err
}
err = k.unmarshal(obj.Data)
if err != nil {
return nil, err
}
return k, nil
}
// saves the key as the key for the given node ID in the passed keystore
func (k *key) save(kv *versioned.KV, id *id.ID) error {
now := time.Now()
data, err := k.marshal()
if err != nil {
return err
}
obj := versioned.Object{
Version: currentKeyVersion,
Timestamp: now,
Data: data,
}
key := keyKey(id)
return kv.Set(key, &obj)
}
// deletes the key from the versioned keystore
func (k *key) delete(kv *versioned.KV, id *id.ID) error {
key := keyKey(id)
return kv.Delete(key)
}
// makes a binary representation of the given key in the keystore
func (k *key) marshal() ([]byte, error) {
return k.k.GobEncode()
}
// resets the data of the key from the binary representation of the key passed in
func (k *key) unmarshal(b []byte) error {
k.k = &cyclic.Int{}
return k.k.GobDecode(b)
}
// generates the key used in the keystore for the given key
func keyKey(id *id.ID) string {
return "nodeKey:" + id.String()
}
package cmix
import (
"gitlab.com/elixxir/client/globals"
"gitlab.com/elixxir/crypto/cmix"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/hash"
"gitlab.com/elixxir/primitives/format"
)
type RoundKeys []*cyclic.Int
// Encrypts the given message for CMIX
// Panics if the passed message format
func (rk RoundKeys) Encrypt(grp *cyclic.Group, msg format.Message,
salt []byte) (format.Message, [][]byte, error) {
ecrMsg := cmix.ClientEncrypt(grp, msg, salt, rk)
h, err := hash.NewCMixHash()
if err != nil {
globals.Log.ERROR.Printf("Cound not get hash for KMAC generation: %+v", h)
}
KMAC := cmix.GenerateKMACs(salt, rk, h)
return ecrMsg, KMAC
}
package cmix
import "testing"
func TestRoundKeys_Encrypt_Consistancy(t *testing.T) {
}
package cmix
import (
"encoding/json"
"github.com/pkg/errors"
"gitlab.com/elixxir/client/storage"
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/xx_network/comms/connect"
"gitlab.com/xx_network/primitives/id"
"sync"
"time"
)
const currentStoreVersion = 0
const storeKey = "cmixKeyStore"
type Store struct {
nodes map[id.ID]*key
kv *versioned.KV
mux sync.RWMutex
}
// returns a new cmix storage object
func NewStore(kv *versioned.KV) *Store {
return &Store{
nodes: make(map[id.ID]*key),
kv: kv,
}
}
// loads the cmix storage object
func LoadStore(kv *versioned.KV) (*Store, error) {
s := NewStore(kv)
obj, err := kv.Get(storeKey)
if err != nil {
return nil, err
}
err = s.unmarshal(obj.Data)
if err != nil {
return nil, err
}
return s, nil
}
// adds the key for a round to the cmix storage object. Saves the updated list
// of nodes and the key to disk
func (s *Store) Add(nid *id.ID, k *cyclic.Int) error {
s.mux.Lock()
defer s.mux.Unlock()
nodekey := &key{k: k}
err := nodekey.save(s.kv, nid)
if err != nil {
return err
}
s.nodes[*nid] = nodekey
return s.save()
}
// removes the key from the cmix storage object. Saves an updates node list to
//
func (s *Store) Remove(nid *id.ID, k *cyclic.Int) error {
s.mux.Lock()
defer s.mux.Unlock()
nodekey, ok := s.nodes[*nid]
if !ok {
return errors.New("Cannot remove, no key with given ID found")
}
err := nodekey.delete(s.kv, nid)
if err != nil {
return err
}
delete(s.nodes, *nid)
return nil
}
//Returns a RoundKeys for the topology and a list of nodes it did not have a key for
func (s *Store) GetRoundKeys(topology *connect.Circuit) (RoundKeys, []*id.ID) {
s.mux.RLock()
defer s.mux.RUnlock()
var missingNodes []*id.ID
rk := RoundKeys(make([]*cyclic.Int, topology.Len()))
for i := 0; i < topology.Len(); i++ {
nid := topology.GetNodeAtIndex(i)
k, ok := s.nodes[*nid]
if !ok {
missingNodes = append(missingNodes, nid)
} else {
rk[i] = k.k
}
}
return rk, missingNodes
}
// stores the cmix store
func (s *Store) save() error {
now := time.Now()
data, err := s.marshal()
if err != nil {
return err
}
obj := versioned.Object{
Version: currentStoreVersion,
Timestamp: now,
Data: data,
}
return s.kv.Set(storeKey, &obj)
}
// builds a byte representation of the store
func (s *Store) marshal() ([]byte, error) {
nodes := make([]id.ID, len(s.nodes))
index := 0
for nid, _ := range s.nodes {
nodes[index] = nid
}
return json.Marshal(&nodes)
}
// restores the data for a store from the byte representation of the store
func (s *Store) unmarshal(b []byte) error {
var nodes []id.ID
err := json.Unmarshal(b, &nodes)
if err != nil {
return err
}
for _, nid := range nodes {
k, err := loadKey(s.s, &nid)
if err != nil {
return errors.WithMessagef(err, "could not load node key for %s", &nid)
}
s.nodes[nid] = k
}
return nil
}
......@@ -9,6 +9,7 @@ package storage
import (
"encoding/json"
"gitlab.com/elixxir/client/globals"
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/xx_network/primitives/id"
"time"
)
......@@ -42,7 +43,7 @@ func (s *Session) saveContacts() error {
if err != nil {
return err
}
obj := VersionedObject{
obj := versioned.Object{
Version: currentContactVersion,
Timestamp: time.Now(),
Data: data,
......@@ -59,7 +60,7 @@ func (s *Session) updateContact(record *Contact) error {
// GetContactByEmail reads contact information from disk
func (s *Session) GetContactByEmail(email string) (*Contact, error) {
key := MakeKeyWithPrefix("Contact", email)
key := versioned.MakeKeyWithPrefix("Contact", email)
obj, err := s.Get(key)
if err != nil {
......@@ -85,12 +86,12 @@ func (s *Session) SetContactByEmail(email string, record *Contact) error {
return err
}
key := MakeKeyWithPrefix("Contact", email)
key := versioned.MakeKeyWithPrefix("Contact", email)
data, err := json.Marshal(record)
if err != nil {
return err
}
obj := VersionedObject{
obj := versioned.Object{
Version: currentContactVersion,
Timestamp: time.Now(),
Data: data,
......@@ -122,6 +123,6 @@ func (s *Session) DeleteContactByID(ID *id.ID) error {
return err
}
key := MakeKeyWithPrefix("Contact", record.Email)
key := versioned.MakeKeyWithPrefix("Contact", record.Email)
return s.Delete(key)
}
......@@ -7,6 +7,7 @@
package storage
import (
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/elixxir/ekv"
"gitlab.com/xx_network/primitives/id"
"reflect"
......@@ -16,7 +17,7 @@ import (
// Show that all fields of a searched user record get stored
func TestSession_Contact(t *testing.T) {
store := make(ekv.Memstore)
session := &Session{kv: NewVersionedKV(store)}
session := &Session{kv: versioned.NewKV(store)}
session.loadAllContacts()
expectedRecord := &Contact{
......
package e2e
import (
"gitlab.com/elixxir/client/storage"
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/elixxir/crypto/cyclic"
)
......@@ -10,5 +10,5 @@ type context struct {
grp *cyclic.Group
kv *storage.Session
kv *versioned.KV
}
File moved
File moved
File moved
File moved
File moved
File moved
......@@ -3,7 +3,7 @@ package e2e
import (
"encoding/json"
"errors"
"gitlab.com/elixxir/client/storage"
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/elixxir/crypto/csprng"
"gitlab.com/elixxir/crypto/cyclic"
dh "gitlab.com/elixxir/crypto/diffieHellman"
......@@ -116,7 +116,7 @@ func (s *Session) save() error {
return err
}
obj := storage.VersionedObject{
obj := versioned.Object{
Version: currentSessionVersion,
Timestamp: now,
Data: data,
......
......@@ -4,10 +4,10 @@ import (
"encoding/base64"
"encoding/json"
"github.com/pkg/errors"
"gitlab.com/elixxir/client/storage"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/xx_network/primitives/id"
"sync"
jww "github.com/spf13/jwalterweatherman"
"time"
)
......@@ -68,7 +68,7 @@ func (sb *sessionBuff) save() error {
return err
}
obj := storage.VersionedObject{
obj := versioned.Object{
Version: currentSessionBuffVersion,
Timestamp: now,
Data: data,
......
File moved
File moved
File moved
......@@ -2,8 +2,10 @@ package e2e
import (
"gitlab.com/elixxir/client/storage"
"gitlab.com/elixxir/client/storage/versioned"
"gitlab.com/elixxir/crypto/csprng"
dh "gitlab.com/elixxir/crypto/diffieHellman"
"gitlab.com/elixxir/ekv"
"testing"
)
......@@ -19,7 +21,7 @@ func TestSession_generate_noPrivateKeyReceive(t *testing.T) {
ctx := &context{
fa: &fps,
grp: grp,
kv: storage.InitMem(t),
kv: versioned.NewKV(make(ekv.Memstore)),
}
//build the session
......@@ -80,7 +82,7 @@ func TestSession_generate_PrivateKeySend(t *testing.T) {
ctx := &context{
fa: &fps,
grp: grp,
kv: storage.InitMem(t),
kv: versioned.NewKV(make(ekv.Memstore)),
}
//build the session
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment