Fixed cursor movements, consistent parameter names, onion encode function

This commit is contained in:
David Vennik
2022-12-23 16:30:35 +00:00
parent 9f56a69c77
commit 740baaeeea
8 changed files with 55 additions and 32 deletions

View File

@@ -125,7 +125,7 @@ func NoisePad(l int) (noise []byte) {
end = l
}
seed = sha256.Single(seed[:])
copy(noise[cursor:end], seed[:])
copy(noise[cursor:end], seed[:end-cursor])
cursor = end
}
return

View File

@@ -23,7 +23,7 @@ var (
//
// The Decode function wipes the original message data for security as the
// private keys inside it are no longer needed and any secret should only have
// one storage so it doesn't appear in any GC later.
// one storage, so it doesn't appear in any GC later.
type Type struct {
Header, Payload *prv.Key
types.Onion

14
pkg/wire/encode.go Normal file
View File

@@ -0,0 +1,14 @@
package wire
import (
"github.com/Indra-Labs/indra/pkg/slice"
"github.com/Indra-Labs/indra/pkg/types"
)
func EncodeOnion(on types.Onion) (b slice.Bytes) {
b = make(slice.Bytes, on.Len())
var sc slice.Cursor
c := &sc
on.Encode(b, c)
return
}

View File

@@ -46,20 +46,19 @@ func (x *Type) Len() int {
x.Onion.Len()
}
func (x *Type) Encode(o slice.Bytes, c *slice.Cursor) {
copy(o[*c:c.Inc(magicbytes.Len)], Magic)
func (x *Type) Encode(b slice.Bytes, c *slice.Cursor) {
copy(b[*c:c.Inc(magicbytes.Len)], Magic)
port := slice.NewUint16()
slice.EncodeUint16(port, int(x.Port))
copy(o[*c:c.Inc(slice.Uint16Len)], port)
copy(o[*c:c.Inc(sha256.Len)], x.Ciphers[0][:])
copy(o[*c:c.Inc(sha256.Len)], x.Ciphers[1][:])
copy(o[*c:c.Inc(sha256.Len)], x.Ciphers[1][:])
copy(b[*c:c.Inc(slice.Uint16Len)], port)
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][:])
bytesLen := slice.NewUint32()
slice.EncodeUint32(bytesLen, len(x.Bytes))
copy(o[*c:c.Inc(slice.Uint32Len)], bytesLen)
copy(o[*c:c.Inc(len(x.Bytes))], x.Bytes)
x.Onion.Encode(o, c)
copy(b[*c:c.Inc(slice.Uint32Len)], bytesLen)
copy(b[*c:c.Inc(len(x.Bytes))], x.Bytes)
x.Onion.Encode(b, c)
}
func (x *Type) Decode(b slice.Bytes, c *slice.Cursor) (e error) {

View File

@@ -87,7 +87,7 @@ func (x *Type) 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[MinLen:])
ciph.Encipher(blk, n, b[checkStart+MinLen:])
// Get the hash of the message and truncate it to the checksum at the
// start of the message. Every layer of the onion has a Message and an
// onion inside it, the Message takes care of the encryption. This saves
@@ -109,6 +109,8 @@ func (x *Type) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
if !magicbytes.CheckMagic(b, Magic) {
return magicbytes.WrongMagic(x, b, Magic)
}
chek := b[*c:c.Inc(4)]
start := int(*c)
var n nonce.IV
copy(n[:], b[c.Inc(magicbytes.Len):c.Inc(nonce.IVLen)])
copy(x.Nonce[:], n[:])
@@ -122,15 +124,23 @@ func (x *Type) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
" length field, got: %d expected %d",
length, len(b[*c:]))
}
hash := sha256.Single(b[start : start+length])
if string(hash[:4]) != string(chek) {
return fmt.Errorf("message decode fail checksum")
}
// Snip out bytes for this layer from the remainder, until the length
// indicated by the length prefix. Cursor will now be at the beginning
// of the next layer's messages.
x.Bytes = b[*c:c.Inc(length)]
// A further step is required which decrypts the remainder of the bytes
// after finding the private key corresponding to the Cloak and
// FromPubKey.
return
}
// Decrypt requires the ToPriv key to be located from the Cloak, using the
// Decrypt requires the prv.Key to be located from the Cloak, using the
// FromPub key to derive the shared secret, and then decrypts the rest of the
// message.
func (x *Type) Decrypt(b slice.Bytes, c *slice.Cursor) {
ciph.Encipher(ciph.GetBlock(x.ToPriv, x.FromPub), x.Nonce, b[*c:])
func (x *Type) Decrypt(prk *prv.Key) {
ciph.Encipher(ciph.GetBlock(prk, x.FromPub), x.Nonce, x.Bytes)
}

View File

@@ -139,9 +139,9 @@ func SendPurchase(nBytes uint64, client node.Node,
func SendExit(payload slice.Bytes, port uint16, 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
@@ -149,9 +149,9 @@ func SendExit(payload slice.Bytes, port uint16, client node.Node,
// 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),
ecdh.Compute(replies[2], client.PayloadKey),
ecdh.Compute(replies[1], hop[4].PayloadKey),
ecdh.Compute(replies[0], hop[3].PayloadKey),
}
return OnionSkins{}.
@@ -162,10 +162,10 @@ func SendExit(payload slice.Bytes, port uint16, client node.Node,
Message(address.FromPubKey(hop[2].HeaderKey), set.Next()).
Exit(port, ciphers, payload).
Return(hop[3].IP).
Message(address.FromPubKey(hop[3].HeaderKey), rtns[0]).
Message(address.FromPubKey(hop[3].HeaderKey), replies[0]).
Return(hop[4].IP).
Message(address.FromPubKey(hop[4].HeaderKey), rtns[1]).
Message(address.FromPubKey(hop[4].HeaderKey), replies[1]).
Return(client.IP).
Message(address.FromPubKey(client.HeaderKey), rtns[2]).
Message(address.FromPubKey(client.HeaderKey), replies[2]).
Assemble()
}

View File

@@ -53,7 +53,7 @@ func (x *Type) Decode(b slice.Bytes, c *slice.Cursor) (e error) {
if len(b) < MinLen {
return magicbytes.TooShort(len(b), MinLen, string(Magic))
}
ipLen := b[0]
x.IP = net.IP(b[1 : 1+ipLen])
ipLen := b[*c]
x.IP = net.IP(b[c.Inc(1):c.Inc(int(ipLen))])
return
}

View File

@@ -11,13 +11,13 @@ var (
// URL is the git URL for the repository.
URL = "github.com/Indra-Labs/indra"
// GitRef is the gitref, as in refs/heads/branchname.
GitRef = "refs/heads/ind-bootstrap"
GitRef = "refs/heads/main"
// ParentGitCommit is the commit hash of the parent HEAD.
ParentGitCommit = "ec634cba58726396fa6aac6634f82d499e714f81"
ParentGitCommit = "9db68c9cd407557312cd491805a0c9e8332f38fd"
// BuildTime stores the time when the current binary was built.
BuildTime = "2022-12-23T12:28:36Z"
BuildTime = "2022-12-23T16:30:35Z"
// SemVer lists the (latest) git tag on the build.
SemVer = "v0.0.216"
SemVer = "v0.0.217"
// 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 = 216
Patch = 217
)
// Version returns a pretty printed version information string.