purchase message encryption fully working now

This commit is contained in:
David Vennik
2023-01-03 13:05:02 +00:00
parent 02ec0429c5
commit 4b59e5ba7c
4 changed files with 11 additions and 44 deletions

View File

@@ -9,7 +9,6 @@ import (
"github.com/Indra-Labs/indra/pkg/ciph"
"github.com/Indra-Labs/indra/pkg/ifc"
"github.com/Indra-Labs/indra/pkg/key/address"
"github.com/Indra-Labs/indra/pkg/key/ecdh"
"github.com/Indra-Labs/indra/pkg/key/prv"
"github.com/Indra-Labs/indra/pkg/key/pub"
"github.com/Indra-Labs/indra/pkg/key/signer"
@@ -98,7 +97,6 @@ func (cl *Client) runner() (out bool) {
break
case b := <-cl.Node.Receive():
// process received message
// log.I.Ln("received message")
var onion types.Onion
var e error
c := slice.NewCursor()
@@ -180,20 +178,16 @@ func (cl *Client) forward(on *forward.OnionSkin, b slice.Bytes, c *slice.Cursor)
if on.AddrPort.String() == cl.Node.AddrPort.String() {
// it is for us, we want to unwrap the next
// part.
log.I.Ln("processing new message", *c, cl.AddrPort.String())
b = append(b[*c:], slice.NoisePad(int(*c))...)
cl.Node.Send(b)
} else {
// we need to forward this message onion.
log.I.Ln("forwarding")
cl.Send(on.AddrPort, b)
}
}
func (cl *Client) layer(on *layer.OnionSkin, b slice.Bytes, c *slice.Cursor) {
// this is probably an encrypted layer for us.
log.I.Ln("decrypting onion skin")
// log.I.S(on, b[*c:].ToBytes())
rcv := cl.ReceiveCache.FindCloaked(on.Cloak)
if rcv == nil {
log.I.Ln("no matching key found from cloaked key")
@@ -201,7 +195,6 @@ func (cl *Client) layer(on *layer.OnionSkin, b slice.Bytes, c *slice.Cursor) {
}
on.Decrypt(rcv.Key, b, c)
b = append(b[*c:], slice.NoisePad(int(*c))...)
// log.I.S(b.ToBytes())
cl.Node.Send(b)
}
@@ -228,13 +221,10 @@ func (cl *Client) purchase(on *purchase.OnionSkin, b slice.Bytes, c *slice.Curso
copy(rb[*cur:cur.Inc(ReverseHeaderLen)], header[:ReverseHeaderLen])
start := *cur
se.Encode(rb, cur)
log.I.S(rb[start:].ToBytes())
// on.Ciphers[0], on.Ciphers[2] = on.Ciphers[2], on.Ciphers[0]
// on.Nonces[0], on.Nonces[2] = on.Nonces[2], on.Nonces[0]
for i := range on.Ciphers {
blk := ciph.BlockFromHash(on.Ciphers[i])
ciph.Encipher(blk, nonce.IV{}, rb[start:])
log.I.S(on.Ciphers[i], nonce.IV{}, rb[start:].ToBytes())
ciph.Encipher(blk, on.Nonces[2-i], rb[start:])
}
cl.Node.Send(rb)
}
@@ -242,26 +232,20 @@ func (cl *Client) purchase(on *purchase.OnionSkin, b slice.Bytes, c *slice.Curso
func (cl *Client) reverse(on *reverse.OnionSkin, b slice.Bytes,
c *slice.Cursor) {
// log.I.S(on)
var e error
var onion types.Onion
if on.AddrPort.String() == cl.Node.AddrPort.String() {
// log.I.S("it's for us", on.AddrPort.String(),
// b[*c:].ToBytes())
if onion, e = wire.PeelOnion(b, c); check(e) {
return
}
switch on1 := onion.(type) {
case *layer.OnionSkin:
// log.I.F("in reverse %x %s", on1.Nonce[:], on.AddrPort.String())
// log.I.S(on1)
start := *c - ReverseLayerLen
first := *c
second := first + ReverseLayerLen
last := second + ReverseLayerLen
// log.I.S(b[start:first].ToBytes(),
// b[first:second].ToBytes(),
// b[second:last].ToBytes(),
// )
rcv := cl.ReceiveCache.FindCloaked(on1.Cloak)
// We need to find the PayloadKey to match.
ses := cl.Sessions.FindPub(rcv.Pub)
@@ -272,37 +256,22 @@ func (cl *Client) reverse(on *reverse.OnionSkin, b slice.Bytes,
ciph.Encipher(blk, on1.Nonce,
b[*c:c.Inc(2*ReverseLayerLen)])
blk = ciph.GetBlock(ses.PayloadPrv, hdrPub)
log.I.S(ecdh.Compute(ses.PayloadPrv, hdrPub),
on1.Nonce)
ciph.Encipher(blk, nonce.IV{}, b[*c:])
log.I.S("!!!!!!!", b[*c:].ToBytes())
// if b[start:].String() !=
// reverse.MagicString {
// cl.Node.Send(b[*c:])
// }
// log.I.S(cl.AddrPort.String(),
// // b.ToBytes()[:ReverseHeaderLen],
// b.ToBytes(), // [ReverseHeaderLen:],
// )
ciph.Encipher(blk, on1.Nonce, b[*c:])
// shift the header segment upwards and pad the
// remainder.
copy(b[start:first], b[first:second])
copy(b[first:second], b[second:last])
copy(b[second:last], slice.NoisePad(ReverseLayerLen))
if b[start:start+2].String() != reverse.MagicString {
log.I.S(b[last:].ToBytes())
cl.Node.Send(b[last:])
break
}
log.I.S(b[start:].ToBytes())
cl.Node.Send(b[start:])
default:
return
}
} else {
// we need to forward this message onion.
log.I.Ln("reversing", cl.Node.AddrPort.String(),
on.AddrPort.String())
cl.Send(on.AddrPort, b)
}

View File

@@ -237,7 +237,7 @@ func TestSendPurchase(t *testing.T) {
b := wire.EncodeOnion(o)
hop[0].Send(b)
go func() {
time.Sleep(time.Second)
time.Sleep(time.Second * 2)
quit.Q()
// t.Error("sendpurchase got stuck")
}()

View File

@@ -50,7 +50,7 @@ type OnionSkin struct {
func (x *OnionSkin) String() string {
return fmt.Sprintf("\n\tnonce: %x\n\tto: %x,\n\tfrom: %x,\n",
x.To.GetCloak(), pub.Derive(x.From).ToBytes(), x.Nonce)
x.Nonce, x.To.ToBytes(), x.From.ToBytes())
}
func (x *OnionSkin) Inner() types.Onion { return x.Onion }
@@ -61,10 +61,8 @@ func (x *OnionSkin) Len() int {
func (x *OnionSkin) Encode(b slice.Bytes, c *slice.Cursor) {
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
// Generate a new nonce and copy it in.
n := nonce.New()
// log.I.S("encryption nonce", n)
copy(b[*c:c.Inc(nonce.IVLen)], n[:])
copy(b[*c:c.Inc(nonce.IVLen)], x.Nonce[:])
// Derive the cloaked key and copy it in.
// log.I.S("public key", x.To.ToBytes())
to := x.To.GetCloak()
@@ -83,7 +81,7 @@ func (x *OnionSkin) Encode(b slice.Bytes, c *slice.Cursor) {
if blk = ciph.GetBlock(x.From, x.To.Key); check(e) {
panic(e)
}
ciph.Encipher(blk, n, b[start:])
ciph.Encipher(blk, x.Nonce, b[start:])
}
// Decode decodes a received OnionSkin. The entire remainder of the message is

View File

@@ -10,9 +10,9 @@ var (
// GitRef is the gitref, as in refs/heads/branchname.
GitRef = "refs/heads/protocol"
// ParentGitCommit is the commit hash of the parent HEAD.
ParentGitCommit = "b0d92d763598ea5dde4cbd0ca53675c19d18de8b"
ParentGitCommit = "4b88cee2d030cce07e98000ca69ce20372a6c61d"
// BuildTime stores the time when the current binary was built.
BuildTime = "2023-01-02T17:21:40Z"
BuildTime = "2023-01-03T13:05:02Z"
// SemVer lists the (latest) git tag on the release.
SemVer = "v0.1.2"
// PathBase is the path base returned from runtime caller.