Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Nov 11, 2024
1 parent bec4fa1 commit 4a5d44a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 99 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### CHANGES

- `pkg/crypto/hashing`: doOnce encoding.HexEncode -> ToString method
- `pkg/network/message`: delete GetRand, GetRandMessageSizeBytes

<!-- ... -->

Expand Down
6 changes: 2 additions & 4 deletions pkg/network/message/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
/*
NETWORK MESSAGE FORMAT
E( K, P(HLMR) || HLMR || L(M) || M || L(R) || R )
E( K, P(HM) || HM || M )
where
HLMR = H( K, L(M) || M || L(R) || R )
HM = H( K, M )
where
H - HMAC
K - network key
M - message bytes
L - length
P - proof of work
E - encrypt
R - rand bytes
Scheme: https://github.com/number571/go-peer/blob/master/images/go-peer_layer1_net_message.jpg
*/
Expand Down
36 changes: 9 additions & 27 deletions pkg/network/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ import (
"github.com/number571/go-peer/pkg/crypto/hashing"
"github.com/number571/go-peer/pkg/crypto/keybuilder"
"github.com/number571/go-peer/pkg/crypto/puzzle"
"github.com/number571/go-peer/pkg/crypto/random"
"github.com/number571/go-peer/pkg/crypto/symmetric"
"github.com/number571/go-peer/pkg/encoding"
"github.com/number571/go-peer/pkg/payload"
"github.com/number571/go-peer/pkg/payload/joiner"
)

const (
// IV + Proof + Hash + (2xPayload32Head)=[len(data)+len(void)] + Payload32Head=[head]
// 16 + 8 + 48 + 2x4 + 4 = 84 additional bytes to origin message
// IV + Proof + Hash + Payload32Head=[head]
// 16 + 8 + 48 + 4 = 76 additional bytes to origin message
CMessageHeadSize = 0 +
1*symmetric.CCipherBlockSize +
1*encoding.CSizeUint64 +
1*hashing.CHasherSize +
2*encoding.CSizeUint32 +
1*encoding.CSizeUint32
)

Expand All @@ -34,23 +31,19 @@ var (
)

type sMessage struct {
fEncd []byte // E( K, P(HLMR) || HLMR || L(M) || M || L(R) || R )
fHash []byte // HLMR = H( K, L(M) || M || L(R) || R )
fRand []byte // R
fProof uint64 // P(HLMR)
fEncd []byte // E( K, P(HM) || HM || M )
fHash []byte // HM = H( K, M )
fProof uint64 // P(HM)
fPayload payload.IPayload32 // M
}

func NewMessage(pSett IConstructSettings, pPld payload.IPayload32) IMessage {
prng := random.NewRandom()
sett := pSett.GetSettings()

randBytes := prng.GetBytes(prng.GetUint64() % (pSett.GetRandMessageSizeBytes() + 1))
bytesJoiner := joiner.NewBytesJoiner32([][]byte{pPld.ToBytes(), randBytes})
pldBytes := pPld.ToBytes()

keyBuilder := keybuilder.NewKeyBuilder(0, []byte{}) // the network_key must have good entropy
key := keyBuilder.Build(sett.GetNetworkKey(), symmetric.CCipherKeySize)
hash := hashing.NewHMACHasher(key, bytesJoiner).ToBytes()
hash := hashing.NewHMACHasher(key, pldBytes).ToBytes()

proof := puzzle.NewPoWPuzzle(sett.GetWorkSizeBits()).ProofBytes(hash, pSett.GetParallel())
proofBytes := encoding.Uint64ToBytes(proof)
Expand All @@ -61,12 +54,11 @@ func NewMessage(pSett IConstructSettings, pPld payload.IPayload32) IMessage {
[][]byte{
proofBytes[:],
hash,
bytesJoiner,
pldBytes,
},
[]byte{},
)),
fHash: hash,
fRand: randBytes,
fProof: proof,
fPayload: pPld,
}
Expand Down Expand Up @@ -102,25 +94,19 @@ func LoadMessage(pSett ISettings, pData interface{}) (IMessage, error) {
return nil, ErrInvalidProofOfWork
}

bytesSlice, err := joiner.LoadBytesJoiner32(dBytes[cHashIndex:])
if err != nil || len(bytesSlice) != 2 {
return nil, ErrDecodeBytesJoiner
}

newHash := hashing.NewHMACHasher(key, dBytes[cHashIndex:]).ToBytes()
if !bytes.Equal(hash, newHash) {
return nil, ErrInvalidAuthHash
}

payload := payload.LoadPayload32(bytesSlice[0])
payload := payload.LoadPayload32(dBytes[cHashIndex:])
if payload == nil {
return nil, ErrDecodePayload
}

return &sMessage{
fEncd: msgBytes,
fHash: hash,
fRand: bytesSlice[1],
fProof: proof,
fPayload: payload,
}, nil
Expand All @@ -134,10 +120,6 @@ func (p *sMessage) GetHash() []byte {
return p.fHash
}

func (p *sMessage) GetRand() []byte {
return p.fRand
}

func (p *sMessage) GetPayload() payload.IPayload32 {
return p.fPayload
}
Expand Down
70 changes: 4 additions & 66 deletions pkg/network/message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/number571/go-peer/pkg/crypto/symmetric"
"github.com/number571/go-peer/pkg/encoding"
"github.com/number571/go-peer/pkg/payload"
"github.com/number571/go-peer/pkg/payload/joiner"
)

var (
Expand Down Expand Up @@ -101,32 +100,9 @@ func TestMessage(t *testing.T) {
return
}

voidBytes := msg.GetRand()
if len(voidBytes) > tcLimitVoid {
t.Error("got length void bytes > limit")
return
}

if (len(msg.ToBytes()) - len(voidBytes)) != len(msg.GetPayload().GetBody())+CMessageHeadSize {
t.Error("invalid message size in bytes with void")
return
}

payloadSize := encoding.Uint32ToBytes(uint32(len(pld.ToBytes())))
voidSize := encoding.Uint32ToBytes(uint32(len(voidBytes)))
payloadRandBytes := bytes.Join(
[][]byte{
payloadSize[:],
pld.ToBytes(),
voidSize[:],
voidBytes,
},
[]byte{},
)

keyBuilder := keybuilder.NewKeyBuilder(0, []byte{}) // the network_key must have good entropy
key := keyBuilder.Build(tcNetworkKey, symmetric.CCipherKeySize)
newHash := hashing.NewHMACHasher(key, payloadRandBytes).ToBytes()
newHash := hashing.NewHMACHasher(key, pld.ToBytes()).ToBytes()
if !bytes.Equal(msg.GetHash(), newHash) {
t.Error("payload hash not equal hash of message")
return
Expand Down Expand Up @@ -244,46 +220,12 @@ func TestMessage(t *testing.T) {
t.Error("success load invalid message 2")
return
}

if _, err := LoadMessage(sett.GetSettings(), tNewInvalidMessage3(sett, pld).ToBytes()); err == nil {
t.Error("success load invalid message 3")
return
}
}

func tNewInvalidMessage1(pSett IConstructSettings, pPld payload.IPayload32) IMessage {
bytesJoiner := joiner.NewBytesJoiner32([][]byte{pPld.ToBytes()})
sett := pSett.GetSettings()

keyBuilder := keybuilder.NewKeyBuilder(0, []byte{}) // the network_key must have good entropy
key := keyBuilder.Build(tcNetworkKey, symmetric.CCipherKeySize)
hash := hashing.NewHMACHasher(key, bytesJoiner).ToBytes()

proof := puzzle.NewPoWPuzzle(sett.GetWorkSizeBits()).ProofBytes(hash, pSett.GetParallel())
proofBytes := encoding.Uint64ToBytes(proof)

cipher := symmetric.NewCipher(key)
return &sMessage{
fEncd: cipher.EncryptBytes(bytes.Join(
[][]byte{
proofBytes[:],
hash,
bytesJoiner,
},
[]byte{},
)),
fHash: hash,
fProof: proof,
fPayload: pPld,
}
}

func tNewInvalidMessage2(pSett IConstructSettings, pPld payload.IPayload32) IMessage {
prng := random.NewRandom()
sett := pSett.GetSettings()

voidBytes := prng.GetBytes(prng.GetUint64() % (pSett.GetRandMessageSizeBytes() + 1))
bytesJoiner := joiner.NewBytesJoiner32([][]byte{pPld.ToBytes(), voidBytes})
bytesJoiner := pPld.ToBytes()

keyBuilder := keybuilder.NewKeyBuilder(0, []byte{}) // the network_key must have good entropy
key := keyBuilder.Build(tcNetworkKey, symmetric.CCipherKeySize)
Expand All @@ -305,18 +247,15 @@ func tNewInvalidMessage2(pSett IConstructSettings, pPld payload.IPayload32) IMes
[]byte{},
)),
fHash: hash,
fRand: voidBytes,
fProof: proof,
fPayload: pPld,
}
}

func tNewInvalidMessage3(pSett IConstructSettings, pPld payload.IPayload32) IMessage {
prng := random.NewRandom()
func tNewInvalidMessage2(pSett IConstructSettings, pPld payload.IPayload32) IMessage {
sett := pSett.GetSettings()

voidBytes := prng.GetBytes(prng.GetUint64() % (pSett.GetRandMessageSizeBytes() + 1))
bytesJoiner := joiner.NewBytesJoiner32([][]byte{nil, voidBytes})
bytesJoiner := []byte{}

keyBuilder := keybuilder.NewKeyBuilder(0, []byte{}) // the network_key must have good entropy
key := keyBuilder.Build(tcNetworkKey, symmetric.CCipherKeySize)
Expand All @@ -336,7 +275,6 @@ func tNewInvalidMessage3(pSett IConstructSettings, pPld payload.IPayload32) IMes
[]byte{},
)),
fHash: hash,
fRand: voidBytes,
fProof: proof,
fPayload: pPld,
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/network/message/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type IMessage interface {
types.IConverter

GetHash() []byte
GetRand() []byte
GetProof() uint64

// payload = head(32bit) || body(Nbit)
Expand All @@ -19,7 +18,6 @@ type IMessage interface {
type IConstructSettings interface {
GetSettings() ISettings
GetParallel() uint64
GetRandMessageSizeBytes() uint64
}

type ISettings interface {
Expand Down

0 comments on commit 4a5d44a

Please sign in to comment.