mirror of
https://github.com/number571/go-peer.git
synced 2026-09-14 11:05:42 +05:00
update
This commit is contained in:
parent
bec4fa1ca5
commit
4a5d44abbd
@ -9,6 +9,7 @@
|
||||
### CHANGES
|
||||
|
||||
- `pkg/crypto/hashing`: doOnce encoding.HexEncode -> ToString method
|
||||
- `pkg/network/message`: delete GetRand, GetRandMessageSizeBytes
|
||||
|
||||
<!-- ... -->
|
||||
|
||||
|
||||
@ -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
|
||||
*/
|
||||
|
||||
@ -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
|
||||
)
|
||||
|
||||
@ -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)
|
||||
@ -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,
|
||||
}
|
||||
@ -102,17 +94,12 @@ 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
|
||||
}
|
||||
@ -120,7 +107,6 @@ func LoadMessage(pSett ISettings, pData interface{}) (IMessage, error) {
|
||||
return &sMessage{
|
||||
fEncd: msgBytes,
|
||||
fHash: hash,
|
||||
fRand: bytesSlice[1],
|
||||
fProof: proof,
|
||||
fPayload: payload,
|
||||
}, nil
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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 (
|
||||
@ -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
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -336,7 +275,6 @@ func tNewInvalidMessage3(pSett IConstructSettings, pPld payload.IPayload32) IMes
|
||||
[]byte{},
|
||||
)),
|
||||
fHash: hash,
|
||||
fRand: voidBytes,
|
||||
fProof: proof,
|
||||
fPayload: pPld,
|
||||
}
|
||||
|
||||
@ -9,7 +9,6 @@ type IMessage interface {
|
||||
types.IConverter
|
||||
|
||||
GetHash() []byte
|
||||
GetRand() []byte
|
||||
GetProof() uint64
|
||||
|
||||
// payload = head(32bit) || body(Nbit)
|
||||
@ -19,7 +18,6 @@ type IMessage interface {
|
||||
type IConstructSettings interface {
|
||||
GetSettings() ISettings
|
||||
GetParallel() uint64
|
||||
GetRandMessageSizeBytes() uint64
|
||||
}
|
||||
|
||||
type ISettings interface {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user