From 2e723aa1a4b95f2042df26c8d3587258d5bff46d Mon Sep 17 00:00:00 2001 From: David Vennik Date: Tue, 20 Dec 2022 12:17:55 +0000 Subject: [PATCH] Fixed ping and return to have next hop IP in previous hop message wrapper --- pkg/client/client_test.go | 6 ++--- pkg/client/session.go | 29 ++++++++--------------- pkg/key/address/keychain.go | 8 ++++--- pkg/node/node.go | 12 +++++----- pkg/wire/layers.go | 4 ++-- pkg/wire/onion.go | 47 +++++++++++++++++++++---------------- pkg/wire/onionskins.go | 15 ++++-------- version.go | 8 +++---- 8 files changed, 61 insertions(+), 68 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 668beb01..a0684f00 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -68,7 +68,7 @@ package client // // log.I.Ln(len(ci.Hops)) // for i := range ci.Hops { // // progress through the hops in reverse -// rm := &wire.Forward{ +// rm := &wire.HeaderKey{ // IP: ci.Hops[len(ci.Hops)-i-1].IP, // Message: lastMsg, // } @@ -76,7 +76,7 @@ package client // ep := message.EP{ // To: address. // FromPubKey(ci.Hops[len(ci.Hops)-i-1].Key), -// From: cl.Sessions[i].KeyRoller.Next(), +// From: cl.Sessions[i].KeySet.Next(), // Length: len(rmm), // Data: rmm, // } @@ -134,7 +134,7 @@ package client // t.Error(e) // t.FailNow() // } -// var rm *wire.Forward +// var rm *wire.HeaderKey // var msg wire.Message // if msg, e = wire.Deserialize(f.Data); check(e) { // t.Error(e) diff --git a/pkg/client/session.go b/pkg/client/session.go index 24369c6f..728971af 100644 --- a/pkg/client/session.go +++ b/pkg/client/session.go @@ -11,10 +11,9 @@ import ( // with new credit, and the current state of the encryption. type Session struct { nonce.ID - Remaining uint64 - Forward, Return *address.SendEntry - ReceiveEntry *address.ReceiveEntry - KeyRoller *signer.KeySet + Remaining uint64 + HeaderKey, PayloadKey *address.SendEntry + *signer.KeySet } type Sessions []*Session @@ -49,15 +48,15 @@ func (s Sessions) Find(t nonce.ID) (se *Session) { // // Purchasing a session the seller returns a token, based on a requested data // allocation, -func NewSession(id nonce.ID, rem uint64, fwd, rtn *address.SendEntry, - re *address.ReceiveEntry, kr *signer.KeySet) (s *Session) { +func NewSession(id nonce.ID, rem uint64, hdr, pld *address.SendEntry, + kr *signer.KeySet) (s *Session) { s = &Session{ - ID: id, - Remaining: rem, - Forward: fwd, - ReceiveEntry: re, - KeyRoller: kr, + ID: id, + Remaining: rem, + HeaderKey: hdr, + PayloadKey: pld, + KeySet: kr, } return } @@ -78,11 +77,3 @@ func (s *Session) SubtractBytes(b uint64) bool { s.Remaining -= b return true } - -func (s *Session) SetSendEntry(se *address.SendEntry) { - s.Forward = se -} - -func (s *Session) SetReceiveEntry(re *address.ReceiveEntry) { - s.ReceiveEntry = re -} diff --git a/pkg/key/address/keychain.go b/pkg/key/address/keychain.go index dce6ce04..f03cd08e 100644 --- a/pkg/key/address/keychain.go +++ b/pkg/key/address/keychain.go @@ -6,6 +6,7 @@ import ( "github.com/Indra-Labs/indra/pkg/key/prv" "github.com/Indra-Labs/indra/pkg/key/pub" + "github.com/Indra-Labs/indra/pkg/nonce" ) // SendEntry tracks the received signing keys to be used for messages with a @@ -91,15 +92,16 @@ func (sc *SendCache) Delete(k pub.Bytes) (e error) { return } -// ReceiveEntry tracks the details of a receiver key and their related IP -// address. +// ReceiveEntry tracks the details of a receiver key and their ID. type ReceiveEntry struct { + nonce.ID *Receiver time.Time } func NewReceiveEntry(priv *prv.Key) *ReceiveEntry { return &ReceiveEntry{ + ID: nonce.NewID(), Receiver: NewReceiver(priv), Time: time.Now(), } @@ -128,7 +130,7 @@ func (rc *ReceiveCache) Len() int { } func (rc *ReceiveCache) Add(r *Receiver) { - re := &ReceiveEntry{Receiver: r, Time: time.Now()} + re := NewReceiveEntry(r.Key) rc.ReceiveEntries = append(rc.ReceiveEntries, re) rc.Index = append(rc.Index, pub.Derive(r.Key).ToBytes()) return diff --git a/pkg/node/node.go b/pkg/node/node.go index e6190316..b53018dd 100644 --- a/pkg/node/node.go +++ b/pkg/node/node.go @@ -25,7 +25,7 @@ var ( type Node struct { nonce.ID net.IP - Forward, Return *pub.Key + HeaderKey, PayloadKey *pub.Key ifc.Transport } @@ -34,11 +34,11 @@ type Node struct { func New(ip net.IP, fwd, rtn *pub.Key, tpt ifc.Transport) (n *Node, id nonce.ID) { id = nonce.NewID() n = &Node{ - ID: id, - IP: ip, - Transport: tpt, - Forward: fwd, - Return: rtn, + ID: id, + IP: ip, + Transport: tpt, + HeaderKey: fwd, + PayloadKey: rtn, } return } diff --git a/pkg/wire/layers.go b/pkg/wire/layers.go index 731e0a29..4d56a607 100644 --- a/pkg/wire/layers.go +++ b/pkg/wire/layers.go @@ -16,8 +16,8 @@ type OnionSkins []Onion func (o OnionSkins) Message(to *address.Sender, from *prv.Key) OnionSkins { return append(o, &Message{To: to, From: from}) } -func (o OnionSkins) Confirmation(ciph sha256.Hash, id nonce.ID) OnionSkins { - return append(o, &Confirmation{Cipher: ciph, ID: id}) +func (o OnionSkins) Confirmation(id nonce.ID) OnionSkins { + return append(o, &Confirmation{ID: id}) } func (o OnionSkins) Forward(ip net.IP) OnionSkins { return append(o, &Forward{IP: ip}) diff --git a/pkg/wire/onion.go b/pkg/wire/onion.go index 47b9eac5..b986cdf1 100644 --- a/pkg/wire/onion.go +++ b/pkg/wire/onion.go @@ -19,17 +19,18 @@ import ( // an increment of their liveness score. By using this scheme, when nodes are // offline their scores will fall to zero after a time whereas live nodes will // have steadily increasing scores from successful pings. -func Ping(ciph sha256.Hash, id nonce.ID, nodes [3]node.Node, +func Ping(id nonce.ID, client node.Node, hop [3]node.Node, set signer.KeySet) Onion { return OnionSkins{}. - Message(address.FromPubKey(nodes[0].Forward), set.Next()). - Forward(nodes[0].IP). - Message(address.FromPubKey(nodes[1].Forward), set.Next()). - Forward(nodes[1].IP). - Message(address.FromPubKey(nodes[2].Forward), set.Next()). - Forward(nodes[2].IP). - Confirmation(ciph, id). + Message(address.FromPubKey(hop[0].HeaderKey), set.Next()). + Forward(hop[1].IP). + Message(address.FromPubKey(hop[1].HeaderKey), set.Next()). + Forward(hop[2].IP). + Message(address.FromPubKey(hop[2].HeaderKey), set.Next()). + Forward(client.IP). + Message(address.FromPubKey(client.HeaderKey), set.Next()). + Confirmation(id). Assemble() } @@ -46,20 +47,26 @@ func Ping(ciph sha256.Hash, id nonce.ID, nodes [3]node.Node, // This message's last layer is a Confirmation, which allows the client to know // that the key was successfully delivered to the Return relays that will be // used in the Purchase. -func SendReturn(id nonce.ID, ciph sha256.Hash, hdr, pld *prv.Key, - nodes [5]node.Node, set signer.KeySet) Onion { +// +// The first hop (0) is the destination of the first layer, 1 is second, 2 is +// the return relay, 3 is the first return, 4 is the second return, and client +// is the client. +func SendReturn(idCipher sha256.Hash, id nonce.ID, hdr, pld *prv.Key, + client node.Node, hop [5]node.Node, set signer.KeySet) Onion { return OnionSkins{}. - Message(address.FromPubKey(nodes[0].Forward), set.Next()). - Forward(nodes[0].IP). - Message(address.FromPubKey(nodes[1].Forward), set.Next()). - Forward(nodes[1].IP). - Message(address.FromPubKey(nodes[2].Forward), set.Next()). + Message(address.FromPubKey(hop[0].HeaderKey), set.Next()). + Forward(hop[1].IP). + Message(address.FromPubKey(hop[1].HeaderKey), set.Next()). + Forward(hop[2].IP). + Message(address.FromPubKey(hop[2].HeaderKey), set.Next()). Cipher(hdr, pld). - Message(address.FromPubKey(nodes[3].Forward), set.Next()). - Forward(nodes[1].IP). - Message(address.FromPubKey(nodes[4].Forward), set.Next()). - Forward(nodes[2].IP). - Confirmation(ciph, id). + Forward(hop[3].IP). + Message(address.FromPubKey(hop[3].HeaderKey), set.Next()). + Forward(hop[4].IP). + Message(address.FromPubKey(hop[4].HeaderKey), set.Next()). + Forward(client.IP). + Message(address.FromPubKey(client.HeaderKey), set.Next()). + Confirmation(id). Assemble() } diff --git a/pkg/wire/onionskins.go b/pkg/wire/onionskins.go index 82081bf4..12732d77 100644 --- a/pkg/wire/onionskins.go +++ b/pkg/wire/onionskins.go @@ -1,7 +1,6 @@ package wire import ( - "crypto/aes" "crypto/cipher" "net" @@ -114,7 +113,6 @@ func (on *Message) Encode(o slice.Bytes, c *slice.Cursor) { // randomly selected, so they will generally be a much smaller subset versus the // current full set of Session s currently open. type Confirmation struct { - Cipher sha256.Hash nonce.ID } @@ -128,13 +126,8 @@ func (cf *Confirmation) Len() int { func (cf *Confirmation) Encode(o slice.Bytes, c *slice.Cursor) { copy(o[*c:c.Inc(MagicLen)], ConfirmationMagic) - // Generate block cipher from confirmation Cipher. - block, _ := aes.NewCipher(cf.Cipher[:]) - start, end := *c, c.Inc(nonce.IDLen) // Copy in the ID. - copy(o[start:end], cf.ID[:]) - // Encrypt the ID to the cipher. - ciph.Encipher(block, nonce.New(), o[start:end]) + copy(o[*c:c.Inc(nonce.IDLen)], cf.ID[:]) } // Forward is just an IP address and a wrapper for another message. @@ -324,10 +317,10 @@ func (se *Session) Len() int { } func (se *Session) Encode(o slice.Bytes, c *slice.Cursor) { - fwd, rtn := se.HeaderKey.ToBytes(), se.PayloadKey.ToBytes() + hdr, pld := se.HeaderKey.ToBytes(), se.PayloadKey.ToBytes() copy(o[*c:c.Inc(MagicLen)], SessionMagic) - copy(o[*c:c.Inc(pub.KeyLen)], fwd[:]) - copy(o[*c:c.Inc(pub.KeyLen)], rtn[:]) + copy(o[*c:c.Inc(pub.KeyLen)], hdr[:]) + copy(o[*c:c.Inc(pub.KeyLen)], pld[:]) se.Onion.Encode(o, c) } diff --git a/version.go b/version.go index a875e664..4281088b 100644 --- a/version.go +++ b/version.go @@ -13,11 +13,11 @@ var ( // GitRef is the gitref, as in refs/heads/branchname. GitRef = "refs/heads/main" // ParentGitCommit is the commit hash of the parent HEAD. - ParentGitCommit = "ac3e6aaf8f848db2fb18df2b9e6e02c0e0eb2934" + ParentGitCommit = "61bd600f319bb33fcb56488ca715a812e36c66a0" // BuildTime stores the time when the current binary was built. - BuildTime = "2022-12-20T11:11:58Z" + BuildTime = "2022-12-20T12:17:55Z" // SemVer lists the (latest) git tag on the build. - SemVer = "v0.0.193" + SemVer = "v0.0.194" // PathBase is the path base returned from runtime caller. PathBase = "/home/loki/src/github.com/Indra-Labs/indra/" // Major is the major number from the tag. @@ -25,7 +25,7 @@ var ( // Minor is the minor number from the tag. Minor = 0 // Patch is the patch version number from the tag. - Patch = 193 + Patch = 194 ) // Version returns a pretty printed version information string.