Working tested purchase onionskin

This commit is contained in:
David Vennik
2022-12-24 16:32:00 +00:00
parent c6e54ef36f
commit 414d6533cd
6 changed files with 60 additions and 31 deletions

View File

@@ -20,6 +20,7 @@ import (
"github.com/Indra-Labs/indra/pkg/wire/delay"
"github.com/Indra-Labs/indra/pkg/wire/exit"
"github.com/Indra-Labs/indra/pkg/wire/forward"
"github.com/Indra-Labs/indra/pkg/wire/purchase"
"github.com/Indra-Labs/indra/pkg/wire/reply"
"github.com/Indra-Labs/indra/pkg/wire/response"
"github.com/Indra-Labs/indra/pkg/wire/session"
@@ -198,7 +199,36 @@ func TestOnionSkins_Message(t *testing.T) {
}
func TestOnionSkins_Purchase(t *testing.T) {
log2.CodeLoc = true
var e error
prvs, pubs := GetCipherSet(t)
ciphers := GenCiphers(prvs, pubs)
p := rand.Uint64()
on := OnionSkins{}.
Purchase(p, prvs, pubs).
Assemble()
onb := EncodeOnion(on)
c := slice.NewCursor()
var onex types.Onion
if onex, e = PeelOnion(onb, c); check(e) {
t.FailNow()
}
var pr *purchase.OnionSkin
var ok bool
if pr, ok = onex.(*purchase.OnionSkin); !ok {
t.Error("did not unwrap expected type")
t.FailNow()
}
if pr.NBytes != p {
t.Error("NBytes did not unwrap correctly")
t.FailNow()
}
for i := range pr.Ciphers {
if pr.Ciphers[i] != ciphers[i] {
t.Errorf("cipher %d did not unwrap correctly", i)
t.FailNow()
}
}
}
func TestOnionSkins_Reply(t *testing.T) {

View File

@@ -61,7 +61,7 @@ func (x *OnionSkin) Encode(b slice.Bytes, c *slice.Cursor) {
func (x *OnionSkin) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
if len(b[*c:]) < MinLen-magicbytes.Len {
return magicbytes.TooShort(len(b[*c:]), MinLen, string(Magic))
return magicbytes.TooShort(len(b[*c:]), MinLen-magicbytes.Len, string(Magic))
}
x.Port = uint16(slice.DecodeUint16(b[*c:c.Inc(slice.Uint16Len)]))
for i := range x.Ciphers {

View File

@@ -72,10 +72,12 @@ func (o OnionSkins) Message(to *address.Sender, from *prv.Key) OnionSkins {
Onion: &noop.OnionSkin{},
})
}
func (o OnionSkins) Purchase(nBytes uint64, ciphers [3]sha256.Hash) OnionSkins {
func (o OnionSkins) Purchase(nBytes uint64, prvs [3]*prv.Key,
pubs [3]*pub.Key, ) OnionSkins {
return append(o, &purchase.OnionSkin{
NBytes: nBytes,
Ciphers: ciphers,
Ciphers: GenCiphers(prvs, pubs),
Onion: &noop.OnionSkin{},
})
}

View File

@@ -2,13 +2,11 @@ package wire
import (
"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"
"github.com/Indra-Labs/indra/pkg/node"
"github.com/Indra-Labs/indra/pkg/nonce"
"github.com/Indra-Labs/indra/pkg/sha256"
"github.com/Indra-Labs/indra/pkg/slice"
"github.com/Indra-Labs/indra/pkg/types"
)
@@ -88,34 +86,31 @@ func SendKeys(id nonce.ID, hdr, pld *pub.Key,
func SendPurchase(nBytes uint64, client node.Node,
hop [5]node.Node, set signer.KeySet) types.Onion {
var rtns [3]*prv.Key
for i := range rtns {
rtns[i] = set.Next()
var replies [3]*prv.Key
for i := range replies {
replies[i] = set.Next()
}
// The ciphers represent the combination of the same From key and the
// payload keys combined, which the receiver knows means the header uses
// HeaderKey and the message underneath use a different cipher in place
// of the HeaderKey, the PayloadKey it corresponds to.
ciphers := [3]sha256.Hash{
ecdh.Compute(rtns[2], client.PayloadKey),
ecdh.Compute(rtns[1], hop[4].PayloadKey),
ecdh.Compute(rtns[0], hop[3].PayloadKey),
}
var prvs [3]*prv.Key
var pubs [3]*pub.Key
prvs[0] = replies[2]
prvs[1] = replies[1]
prvs[2] = replies[0]
pubs[0] = client.PayloadKey
pubs[1] = hop[4].PayloadKey
pubs[2] = hop[3].PayloadKey
return OnionSkins{}.
Message(address.FromPubKey(hop[0].HeaderKey), set.Next()).
Forward(hop[1].AddrPort).
Message(address.FromPubKey(hop[1].HeaderKey), set.Next()).
Forward(hop[2].AddrPort).
Message(address.FromPubKey(hop[2].HeaderKey), set.Next()).
Purchase(nBytes, ciphers).
Purchase(nBytes, prvs, pubs).
Reply(hop[3].AddrPort).
Message(address.FromPubKey(hop[3].HeaderKey), rtns[0]).
Message(address.FromPubKey(hop[3].HeaderKey), replies[0]).
Reply(hop[4].AddrPort).
Message(address.FromPubKey(hop[4].HeaderKey), rtns[1]).
Message(address.FromPubKey(hop[4].HeaderKey), replies[1]).
Reply(client.AddrPort).
Message(address.FromPubKey(client.HeaderKey), rtns[2]).
Message(address.FromPubKey(client.HeaderKey), replies[2]).
Assemble()
}

View File

@@ -39,18 +39,20 @@ func (x *OnionSkin) Encode(b slice.Bytes, c *slice.Cursor) {
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
value := slice.NewUint64()
slice.EncodeUint64(value, x.NBytes)
copy(b[*c:c.Inc(slice.Uint64Len)], value)
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[0][:])
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[1][:])
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[1][:])
copy(b[*c:c.Inc(sha256.Len)], x.Ciphers[2][:])
x.Onion.Encode(b, c)
}
func (x *OnionSkin) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
if len(b[*c:]) < MinLen-magicbytes.Len {
return magicbytes.TooShort(len(b[*c:]), MinLen, string(Magic))
return magicbytes.TooShort(len(b[*c:]),
MinLen-magicbytes.Len, MagicString)
}
x.NBytes = slice.DecodeUint64(
b[c.Inc(magicbytes.Len):c.Inc(slice.Uint64Len)])
b[*c:c.Inc(slice.Uint64Len)])
for i := range x.Ciphers {
bytes := b[*c:c.Inc(sha256.Len)]
copy(x.Ciphers[i][:], bytes)

View File

@@ -10,11 +10,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 = "40301abeb501a452ae64f976dce001fa0fa0c85c"
ParentGitCommit = "43c16838313ca26b3704c96e7d40b8ced95a5101"
// BuildTime stores the time when the current binary was built.
BuildTime = "2022-12-24T16:12:09Z"
BuildTime = "2022-12-24T16:32:00Z"
// SemVer lists the (latest) git tag on the build.
SemVer = "v0.0.233"
SemVer = "v0.0.234"
// 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.
@@ -22,7 +22,7 @@ var (
// Minor is the minor number from the tag.
Minor = 0
// Patch is the patch version number from the tag.
Patch = 233
Patch = 234
)
// Version returns a pretty printed version information string.