From 58f5d1e11da9cf84dca72e1da49c34133e3303e3 Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Thu, 4 Feb 2021 20:05:34 +0000
Subject: [PATCH] Updates to make everything live in storage

---
 api/client.go                 |  4 +---
 auth/confirm.go               |  2 +-
 auth/manager.go               |  7 +------
 cmd/root.go                   | 18 ++++--------------
 interfaces/networkManager.go  |  1 -
 interfaces/params/E2E.go      |  9 +++++----
 interfaces/params/E2E_test.go |  6 ------
 keyExchange/rekey.go          |  2 +-
 keyExchange/trigger.go        |  2 +-
 network/manager.go            | 12 ++++++------
 storage/e2e/session.go        | 16 ++++++++--------
 storage/e2e/store.go          | 22 ++++++++++++++++++++++
 storage/e2e/store_test.go     |  2 ++
 13 files changed, 52 insertions(+), 51 deletions(-)

diff --git a/api/client.go b/api/client.go
index 17649e531..776eec62d 100644
--- a/api/client.go
+++ b/api/client.go
@@ -242,9 +242,7 @@ func Login(storageDir string, password []byte, parameters params.Network) (*Clie
 	}
 
 	//initilize the auth tracker
-	authE2EParams := parameters.E2EParams
-	c.auth = auth.NewManager(c.switchboard, c.storage, c.network,
-		authE2EParams)
+	c.auth = auth.NewManager(c.switchboard, c.storage, c.network)
 
 	return c, nil
 }
diff --git a/auth/confirm.go b/auth/confirm.go
index d6c048594..d5b5c8def 100644
--- a/auth/confirm.go
+++ b/auth/confirm.go
@@ -105,7 +105,7 @@ func ConfirmRequestAuth(partner contact.Contact, rng io.Reader,
 	// messages does not occur
 
 	//create local relationship
-	p := net.GetE2EParams()
+	p := storage.E2e().GetE2ESessionParams()
 	if err := storage.E2e().AddPartner(partner.ID, partner.DhPubKey, newPrivKey,
 		p, p); err != nil {
 		storage.Auth().Fail(partner.ID)
diff --git a/auth/manager.go b/auth/manager.go
index 83857a0d8..08f75d92d 100644
--- a/auth/manager.go
+++ b/auth/manager.go
@@ -28,14 +28,13 @@ type Manager struct {
 }
 
 func NewManager(sw interfaces.Switchboard, storage *storage.Session,
-	net interfaces.NetworkManager, e2eParams params.E2ESessionParams) *Manager {
+	net interfaces.NetworkManager) *Manager {
 	m := &Manager{
 		requestCallbacks: newCallbackMap(),
 		confirmCallbacks: newCallbackMap(),
 		rawMessages:      make(chan message.Receive, 1000),
 		storage:          storage,
 		net:              net,
-		params:           e2eParams,
 	}
 
 	sw.RegisterChannel("Auth", switchboard.AnyUser(), message.Raw, m.rawMessages)
@@ -92,7 +91,3 @@ func (m *Manager) AddSpecificConfirmCallback(id *id.ID, cb interfaces.ConfirmCal
 func (m *Manager) RemoveSpecificConfirmCallback(id *id.ID) {
 	m.confirmCallbacks.RemoveSpecific(id)
 }
-
-func (m *Manager) GetE2EParams() params.E2ESessionParams {
-	return m.params
-}
diff --git a/cmd/root.go b/cmd/root.go
index 5ad3638bb..f1b3d0d98 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -213,6 +213,10 @@ func createClient() *api.Client {
 	}
 
 	netParams := params.GetDefaultNetwork()
+	netParams.E2EParams.MinKeys = uint16(viper.GetUint("e2eMinKeys"))
+	netParams.E2EParams.MaxKeys = uint16(viper.GetUint("e2eMaxKeys"))
+	netParams.E2EParams.NumRekeys = uint16(
+		viper.GetUint("e2eNumReKeys"))
 	netParams.ForceHistoricalRounds = viper.GetBool("forceHistoricalRounds")
 
 	client, err := api.OpenClient(storeDir, []byte(pass), netParams)
@@ -233,10 +237,6 @@ func initClient() *api.Client {
 	netParams.E2EParams.MaxKeys = uint16(viper.GetUint("e2eMaxKeys"))
 	netParams.E2EParams.NumRekeys = uint16(
 		viper.GetUint("e2eNumReKeys"))
-	netParams.E2EParams.TTLParams.TTLScalar = float64(
-		viper.GetUint("e2eTTLScalar"))
-	netParams.E2EParams.TTLParams.MinNumKeys = uint16(
-		viper.GetUint("e2eTTLMinNumKeys"))
 	netParams.ForceHistoricalRounds = viper.GetBool("forceHistoricalRounds")
 
 	//load the client
@@ -647,16 +647,6 @@ func init() {
 		"", uint(defaultE2EParams.NumRekeys),
 		"Number of rekeys held in memory at once")
 	viper.BindPFlag("e2eNumReKeys", rootCmd.Flags().Lookup("e2eNumReKeys"))
-	rootCmd.Flags().Float64P("e2eTTLScalar",
-		"", defaultE2EParams.TTLParams.TTLScalar,
-		"Time to live key retrigger setting")
-	viper.BindPFlag("e2eTTLScalar", rootCmd.Flags().Lookup("e2eTTLScalar"))
-	rootCmd.Flags().UintP("e2eTTLMinNumKeys",
-		"", uint(defaultE2EParams.TTLParams.MinNumKeys),
-		"Minimum number of keys used in the TTL")
-	viper.BindPFlag("e2eTTLMinNumKeys",
-		rootCmd.Flags().Lookup("e2eTTLMinNumKeys"))
-
 }
 
 // initConfig reads in config file and ENV variables if set.
diff --git a/interfaces/networkManager.go b/interfaces/networkManager.go
index b7cbd7383..541dc5b14 100644
--- a/interfaces/networkManager.go
+++ b/interfaces/networkManager.go
@@ -25,7 +25,6 @@ type NetworkManager interface {
 	GetHealthTracker() HealthTracker
 	Follow() (stoppable.Stoppable, error)
 	CheckGarbledMessages()
-	GetE2EParams() params.E2ESessionParams
 }
 
 //for use in key exchange which needs to be callable inside of network
diff --git a/interfaces/params/E2E.go b/interfaces/params/E2E.go
index cb328ed01..b0ae16cba 100644
--- a/interfaces/params/E2E.go
+++ b/interfaces/params/E2E.go
@@ -89,9 +89,10 @@ func GetDefaultE2ESessionParams() E2ESessionParams {
 		MinKeys:   minKeys,
 		MaxKeys:   maxKeys,
 		NumRekeys: numReKeys,
-		TTLParams: e2e.TTLParams{
-			TTLScalar:  ttlScalar,
-			MinNumKeys: threshold,
-		},
 	}
 }
+
+func (p E2ESessionParams) String() string {
+	return fmt.Sprintf("Params{ MinKeys: %d, MaxKeys: %d, NumRekeys: %d }",
+		p.MinKeys, p.MaxKeys, p.NumRekeys)
+}
diff --git a/interfaces/params/E2E_test.go b/interfaces/params/E2E_test.go
index a62d30466..1f4b599bd 100644
--- a/interfaces/params/E2E_test.go
+++ b/interfaces/params/E2E_test.go
@@ -79,10 +79,4 @@ func Test_GetDefaultParams(t *testing.T) {
 	if p.NumRekeys != numReKeys {
 		t.Errorf("MinKeys mismatch\r\tGot: %d\r\tExpected: %d", p.NumRekeys, numReKeys)
 	}
-	if p.TTLScalar != ttlScalar {
-		t.Errorf("MinKeys mismatch\r\tGot: %v\r\tExpected: %v", p.TTLScalar, ttlScalar)
-	}
-	if p.MinNumKeys != threshold {
-		t.Errorf("MinKeys mismatch\r\tGot: %d\r\tExpected: %d", p.MinNumKeys, threshold)
-	}
 }
diff --git a/keyExchange/rekey.go b/keyExchange/rekey.go
index 389caaad3..deeec7dbb 100644
--- a/keyExchange/rekey.go
+++ b/keyExchange/rekey.go
@@ -49,7 +49,7 @@ func trigger(instance *network.Instance, sendE2E interfaces.SendE2E,
 		fmt.Printf("in new session triggered\n")
 		//create the session, pass a nil private key to generate a new one
 		negotiatingSession = manager.NewSendSession(nil,
-			session.GetE2EParams())
+			sess.E2e().GetE2ESessionParams())
 		//move the state of the triggering session forward
 		session.SetNegotiationStatus(e2e.NewSessionCreated)
 		fmt.Printf("after setting session: %v\n", negotiatingSession.NegotiationStatus())
diff --git a/keyExchange/trigger.go b/keyExchange/trigger.go
index f109388de..544cde8ee 100644
--- a/keyExchange/trigger.go
+++ b/keyExchange/trigger.go
@@ -81,7 +81,7 @@ func handleTrigger(sess *storage.Session, net interfaces.NetworkManager,
 
 	//create the new session
 	session, duplicate := partner.NewReceiveSession(PartnerPublicKey,
-		oldSession.GetE2EParams(), oldSession)
+		sess.E2e().GetE2ESessionParams(), oldSession)
 	// new session being nil means the session was a duplicate. This is possible
 	// in edge cases where the partner crashes during operation. The session
 	// creation in this case ignores the new session, but the confirmation
diff --git a/network/manager.go b/network/manager.go
index 51e10f29d..15d14dda2 100644
--- a/network/manager.go
+++ b/network/manager.go
@@ -46,8 +46,7 @@ type manager struct {
 	message *message.Manager
 
 	//atomic denotes if the network is running
-	running   *uint32
-	e2eParams params.E2ESessionParams
+	running *uint32
 }
 
 // NewManager builds a new reception manager object using inputted key fields
@@ -64,6 +63,11 @@ func NewManager(session *storage.Session, switchboard *switchboard.Switchboard,
 
 	running := uint32(0)
 
+	// Note: These are not loaded/stored in E2E Store, but the
+	// E2E Session Params are a part of the network parameters, so we
+	// set them here when they are needed on startup
+	session.E2e().SetE2ESessionParams(params.E2EParams)
+
 	//create manager object
 	m := manager{
 		param:   params,
@@ -155,7 +159,3 @@ func (m *manager) GetInstance() *network.Instance {
 func (m *manager) CheckGarbledMessages() {
 	m.message.CheckGarbledMessages()
 }
-
-func (m *manager) GetE2EParams() params.E2ESessionParams {
-	return m.e2eParams
-}
diff --git a/storage/e2e/session.go b/storage/e2e/session.go
index f5d37b2d4..573bd5ce7 100644
--- a/storage/e2e/session.go
+++ b/storage/e2e/session.go
@@ -17,9 +17,10 @@ import (
 	"gitlab.com/elixxir/client/storage/versioned"
 	"gitlab.com/elixxir/crypto/cyclic"
 	dh "gitlab.com/elixxir/crypto/diffieHellman"
-	"gitlab.com/elixxir/crypto/e2e"
 	"gitlab.com/elixxir/crypto/hash"
+	"gitlab.com/xx_network/crypto/randomness"
 	"gitlab.com/xx_network/primitives/id"
+	"math/big"
 	"sync"
 	"testing"
 	"time"
@@ -548,14 +549,17 @@ func (s *Session) generate(kv *versioned.KV) *versioned.KV {
 	kv = kv.Prefix(makeSessionPrefix(s.GetID()))
 
 	p := s.e2eParams
+	h, _ := hash.NewCMixHash()
 
 	//generate ttl and keying info
-	keysTTL, numKeys := e2e.GenerateKeyTTL(s.baseKey.GetLargeInt(),
-		p.MinKeys, p.MaxKeys, p.TTLParams)
+	numKeys := uint32(randomness.RandInInterval(big.NewInt(
+		int64(p.MaxKeys-p.MinKeys)),
+		s.baseKey.Bytes(), h).Int64() + int64(p.MinKeys))
+	keysTTL := uint32(p.NumRekeys)
 
 	//ensure that enough keys are remaining to rekey
 	if numKeys-uint32(keysTTL) < uint32(p.NumRekeys) {
-		numKeys = uint32(keysTTL + p.NumRekeys)
+		numKeys = uint32(keysTTL) + uint32(p.NumRekeys)
 	}
 
 	s.ttl = uint32(keysTTL)
@@ -595,7 +599,3 @@ func (s *Session) getUnusedKeys() []*Key {
 func makeSessionPrefix(sid SessionID) string {
 	return fmt.Sprintf(sessionPrefix, sid)
 }
-
-func (s *Session) GetE2EParams() params.E2ESessionParams {
-	return s.e2eParams
-}
diff --git a/storage/e2e/store.go b/storage/e2e/store.go
index 9144d1bc3..cff582cc6 100644
--- a/storage/e2e/store.go
+++ b/storage/e2e/store.go
@@ -47,6 +47,8 @@ type Store struct {
 	*fingerprints
 
 	*context
+
+	e2eParams params.E2ESessionParams
 }
 
 func NewStore(grp *cyclic.Group, kv *versioned.KV, privKey *cyclic.Int,
@@ -77,6 +79,8 @@ func NewStore(grp *cyclic.Group, kv *versioned.KV, privKey *cyclic.Int,
 			rng:  rng,
 			myID: myID,
 		},
+
+		e2eParams: params.GetDefaultE2ESessionParams(),
 	}
 
 	err := utility.StoreCyclicKey(kv, pubKey, pubKeyKey)
@@ -120,6 +124,8 @@ func LoadStore(kv *versioned.KV, myID *id.ID, rng *fastRNG.StreamGenerator) (*St
 			myID: myID,
 			grp:  grp,
 		},
+
+		e2eParams: params.GetDefaultE2ESessionParams(),
 	}
 
 	obj, err := kv.Get(storeKey)
@@ -268,6 +274,22 @@ func (s *Store) unmarshal(b []byte) error {
 	return nil
 }
 
+// GetE2ESessionParams returns a copy of the session params object
+func (s *Store) GetE2ESessionParams() params.E2ESessionParams {
+	s.mux.RLock()
+	defer s.mux.RUnlock()
+	jww.DEBUG.Printf("Using Session Params: %s", s.e2eParams)
+	return s.e2eParams
+}
+
+// SetE2ESessionParams overwrites the current session params
+func (s *Store) SetE2ESessionParams(newParams params.E2ESessionParams) {
+	s.mux.Lock()
+	defer s.mux.Unlock()
+	jww.DEBUG.Printf("Setting Session Params: %s", newParams)
+	s.e2eParams = newParams
+}
+
 type fingerprints struct {
 	toKey map[format.Fingerprint]*Key
 	mux   sync.RWMutex
diff --git a/storage/e2e/store_test.go b/storage/e2e/store_test.go
index 5d4f1c00f..0d7f760b9 100644
--- a/storage/e2e/store_test.go
+++ b/storage/e2e/store_test.go
@@ -31,6 +31,7 @@ func TestNewStore(t *testing.T) {
 	kv := versioned.NewKV(make(ekv.Memstore))
 	fingerprints := newFingerprints()
 	rng := fastRNG.NewStreamGenerator(12, 3, csprng.NewSystemRNG)
+	e2eP := params.GetDefaultE2ESessionParams()
 	expectedStore := &Store{
 		managers:     make(map[id.ID]*Manager),
 		dhPrivateKey: privKey,
@@ -44,6 +45,7 @@ func TestNewStore(t *testing.T) {
 			rng:  rng,
 			myID: &id.ID{},
 		},
+		e2eParams: e2eP,
 	}
 	expectedData, err := expectedStore.marshal()
 	if err != nil {
-- 
GitLab