Skip to content
Snippets Groups Projects
Commit 4327eb29 authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

added version numbers to both transmission and reception message types

parent 8a1622c4
No related branches found
No related tags found
3 merge requests!117Release,!116Dev,!109added version numbers to both transmission and reception message types
...@@ -33,7 +33,7 @@ func TestCollator_collate(t *testing.T) { ...@@ -33,7 +33,7 @@ func TestCollator_collate(t *testing.T) {
buff := bytes.NewBuffer(expectedData) buff := bytes.NewBuffer(expectedData)
for i := 0; i < messageCount; i++ { for i := 0; i < messageCount; i++ {
msgParts[i] = newResponseMessagePart(msgPayloadSize + 4) msgParts[i] = newResponseMessagePart(msgPayloadSize + 5)
msgParts[i].SetMaxParts(uint8(messageCount)) msgParts[i].SetMaxParts(uint8(messageCount))
msgParts[i].SetPartNum(uint8(i)) msgParts[i].SetPartNum(uint8(i))
msgParts[i].SetContents(buff.Next(msgPayloadSize)) msgParts[i].SetContents(buff.Next(msgPayloadSize))
...@@ -88,7 +88,7 @@ func TestCollator_collate_UnmarshalError(t *testing.T) { ...@@ -88,7 +88,7 @@ func TestCollator_collate_UnmarshalError(t *testing.T) {
// Error path: max reported parts by payload larger then set in collator // Error path: max reported parts by payload larger then set in collator
func TestCollator_collate_MaxPartsError(t *testing.T) { func TestCollator_collate_MaxPartsError(t *testing.T) {
payloadBytes := []byte{0xFF, 0xFF, 0xFF, 0xFF} payloadBytes := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
c := newCollator(1) c := newCollator(1)
payload, collated, err := c.collate(payloadBytes) payload, collated, err := c.collate(payloadBytes)
...@@ -105,7 +105,7 @@ func TestCollator_collate_MaxPartsError(t *testing.T) { ...@@ -105,7 +105,7 @@ func TestCollator_collate_MaxPartsError(t *testing.T) {
// Error path: the message part number is greater than the max number of parts. // Error path: the message part number is greater than the max number of parts.
func TestCollator_collate_PartNumTooLargeError(t *testing.T) { func TestCollator_collate_PartNumTooLargeError(t *testing.T) {
payloadBytes := []byte{25, 5, 5, 5} payloadBytes := []byte{25, 5, 5, 5, 5}
c := newCollator(5) c := newCollator(5)
payload, collated, err := c.collate(payloadBytes) payload, collated, err := c.collate(payloadBytes)
...@@ -122,7 +122,7 @@ func TestCollator_collate_PartNumTooLargeError(t *testing.T) { ...@@ -122,7 +122,7 @@ func TestCollator_collate_PartNumTooLargeError(t *testing.T) {
// Error path: a message with the part number already exists. // Error path: a message with the part number already exists.
func TestCollator_collate_PartExistsError(t *testing.T) { func TestCollator_collate_PartExistsError(t *testing.T) {
payloadBytes := []byte{1, 5, 0, 1, 20} payloadBytes := []byte{0, 1, 5, 0, 1, 20}
c := newCollator(5) c := newCollator(5)
payload, collated, err := c.collate(payloadBytes) payload, collated, err := c.collate(payloadBytes)
if err != nil { if err != nil {
......
...@@ -64,7 +64,7 @@ func TestManager_StartProcesses(t *testing.T) { ...@@ -64,7 +64,7 @@ func TestManager_StartProcesses(t *testing.T) {
DhPubKey: m.store.E2e().GetDHPublicKey(), DhPubKey: m.store.E2e().GetDHPublicKey(),
} }
tag := "Test tag" tag := "Test tag"
payload := make([]byte, 132) payload := make([]byte, 131)
rand.New(rand.NewSource(42)).Read(payload) rand.New(rand.NewSource(42)).Read(payload)
callback, callbackChan := createReceiveComm() callback, callbackChan := createReceiveComm()
...@@ -150,7 +150,7 @@ func TestManager_StartProcesses_Stop(t *testing.T) { ...@@ -150,7 +150,7 @@ func TestManager_StartProcesses_Stop(t *testing.T) {
DhPubKey: m.store.E2e().GetDHPublicKey(), DhPubKey: m.store.E2e().GetDHPublicKey(),
} }
tag := "Test tag" tag := "Test tag"
payload := make([]byte, 132) payload := make([]byte, 131)
rand.New(rand.NewSource(42)).Read(payload) rand.New(rand.NewSource(42)).Read(payload)
callback, callbackChan := createReceiveComm() callback, callbackChan := createReceiveComm()
......
...@@ -23,7 +23,7 @@ func TestManager_receiveTransmissionHandler(t *testing.T) { ...@@ -23,7 +23,7 @@ func TestManager_receiveTransmissionHandler(t *testing.T) {
DhPubKey: m.store.E2e().GetDHPublicKey(), DhPubKey: m.store.E2e().GetDHPublicKey(),
} }
tag := "Test tag" tag := "Test tag"
payload := make([]byte, 132) payload := make([]byte, 131)
rand.New(rand.NewSource(42)).Read(payload) rand.New(rand.NewSource(42)).Read(payload)
callback, callbackChan := createReceiveComm() callback, callbackChan := createReceiveComm()
...@@ -91,7 +91,7 @@ func TestManager_receiveTransmissionHandler_FingerPrintError(t *testing.T) { ...@@ -91,7 +91,7 @@ func TestManager_receiveTransmissionHandler_FingerPrintError(t *testing.T) {
DhPubKey: m.store.E2e().GetGroup().NewInt(42), DhPubKey: m.store.E2e().GetGroup().NewInt(42),
} }
tag := "Test tag" tag := "Test tag"
payload := make([]byte, 132) payload := make([]byte, 131)
rand.New(rand.NewSource(42)).Read(payload) rand.New(rand.NewSource(42)).Read(payload)
callback, callbackChan := createReceiveComm() callback, callbackChan := createReceiveComm()
...@@ -128,7 +128,7 @@ func TestManager_receiveTransmissionHandler_ProcessMessageError(t *testing.T) { ...@@ -128,7 +128,7 @@ func TestManager_receiveTransmissionHandler_ProcessMessageError(t *testing.T) {
DhPubKey: m.store.E2e().GetDHPublicKey(), DhPubKey: m.store.E2e().GetDHPublicKey(),
} }
tag := "Test tag" tag := "Test tag"
payload := make([]byte, 132) payload := make([]byte, 131)
rand.New(rand.NewSource(42)).Read(payload) rand.New(rand.NewSource(42)).Read(payload)
callback, callbackChan := createReceiveComm() callback, callbackChan := createReceiveComm()
...@@ -167,7 +167,7 @@ func TestManager_receiveTransmissionHandler_TagFpError(t *testing.T) { ...@@ -167,7 +167,7 @@ func TestManager_receiveTransmissionHandler_TagFpError(t *testing.T) {
DhPubKey: m.store.E2e().GetDHPublicKey(), DhPubKey: m.store.E2e().GetDHPublicKey(),
} }
tag := "Test tag" tag := "Test tag"
payload := make([]byte, 132) payload := make([]byte, 131)
rand.New(rand.NewSource(42)).Read(payload) rand.New(rand.NewSource(42)).Read(payload)
msg, _, _, _, err := m.makeTransmitCmixMessage(partner, payload, tag, 8, 32, msg, _, _, _, err := m.makeTransmitCmixMessage(partner, payload, tag, 8, 32,
......
...@@ -16,20 +16,23 @@ import ( ...@@ -16,20 +16,23 @@ import (
const ( const (
partNumLen = 1 partNumLen = 1
maxPartsLen = 1 maxPartsLen = 1
responseMinSize = partNumLen + maxPartsLen + sizeSize responseMinSize = receptionMessageVersionLen + partNumLen + maxPartsLen + sizeSize
receptionMessageVersion = 0
receptionMessageVersionLen = 1
) )
/* /*
+-----------------------------------------+ +---------------------------------------------------+
| CMIX Message Contents | | CMIX Message Contents |
+---------+----------+---------+----------+ +---------+----------+---------+---------+----------+
| partNum | maxParts | size | contents | | version | maxParts | size | partNum | contents |
| 1 bytes | 1 byte | 2 bytes | variable | | 1 bytes | 1 byte | 2 bytes | 1 bytes | variable |
+------------+----------+---------+-------+ +------------+----------+---------+------+----------+
*/ */
type responseMessagePart struct { type responseMessagePart struct {
data []byte // Serial of all contents data []byte // Serial of all contents
version []byte // Version of the message
partNum []byte // Index of message in a series of messages partNum []byte // Index of message in a series of messages
maxParts []byte // The number of parts in this message. maxParts []byte // The number of parts in this message.
size []byte // Size of the contents size []byte // Size of the contents
...@@ -46,7 +49,9 @@ func newResponseMessagePart(externalPayloadSize int) responseMessagePart { ...@@ -46,7 +49,9 @@ func newResponseMessagePart(externalPayloadSize int) responseMessagePart {
externalPayloadSize, responseMinSize) externalPayloadSize, responseMinSize)
} }
return mapResponseMessagePart(make([]byte, externalPayloadSize)) rmp := mapResponseMessagePart(make([]byte, externalPayloadSize))
rmp.version[0] = receptionMessageVersion
return rmp
} }
// mapResponseMessagePart builds a message part mapped to the passed in data. // mapResponseMessagePart builds a message part mapped to the passed in data.
...@@ -54,9 +59,10 @@ func newResponseMessagePart(externalPayloadSize int) responseMessagePart { ...@@ -54,9 +59,10 @@ func newResponseMessagePart(externalPayloadSize int) responseMessagePart {
func mapResponseMessagePart(data []byte) responseMessagePart { func mapResponseMessagePart(data []byte) responseMessagePart {
return responseMessagePart{ return responseMessagePart{
data: data, data: data,
partNum: data[:partNumLen], version: data[:receptionMessageVersionLen],
maxParts: data[partNumLen : maxPartsLen+partNumLen], partNum: data[receptionMessageVersionLen:receptionMessageVersionLen+partNumLen],
size: data[maxPartsLen+partNumLen : responseMinSize], maxParts: data[receptionMessageVersionLen+partNumLen : receptionMessageVersionLen+maxPartsLen+partNumLen],
size: data[receptionMessageVersionLen+maxPartsLen+partNumLen : responseMinSize],
contents: data[responseMinSize:], contents: data[responseMinSize:],
} }
} }
......
...@@ -21,10 +21,11 @@ func Test_newResponseMessagePart(t *testing.T) { ...@@ -21,10 +21,11 @@ func Test_newResponseMessagePart(t *testing.T) {
payloadSize := prng.Intn(2000) payloadSize := prng.Intn(2000)
expected := responseMessagePart{ expected := responseMessagePart{
data: make([]byte, payloadSize), data: make([]byte, payloadSize),
version: make([]byte, receptionMessageVersionLen),
partNum: make([]byte, partNumLen), partNum: make([]byte, partNumLen),
maxParts: make([]byte, maxPartsLen), maxParts: make([]byte, maxPartsLen),
size: make([]byte, sizeSize), size: make([]byte, sizeSize),
contents: make([]byte, payloadSize-partNumLen-maxPartsLen-sizeSize), contents: make([]byte, payloadSize-partNumLen-maxPartsLen-sizeSize-receptionMessageVersionLen),
} }
rmp := newResponseMessagePart(payloadSize) rmp := newResponseMessagePart(payloadSize)
...@@ -50,13 +51,14 @@ func Test_newResponseMessagePart_PayloadSizeError(t *testing.T) { ...@@ -50,13 +51,14 @@ func Test_newResponseMessagePart_PayloadSizeError(t *testing.T) {
// Happy path. // Happy path.
func Test_mapResponseMessagePart(t *testing.T) { func Test_mapResponseMessagePart(t *testing.T) {
prng := rand.New(rand.NewSource(42)) prng := rand.New(rand.NewSource(42))
expectedVersion := uint8(0)
expectedPartNum := uint8(prng.Uint32()) expectedPartNum := uint8(prng.Uint32())
expectedMaxParts := uint8(prng.Uint32()) expectedMaxParts := uint8(prng.Uint32())
size := []byte{uint8(prng.Uint64()), uint8(prng.Uint64())} size := []byte{uint8(prng.Uint64()), uint8(prng.Uint64())}
expectedContents := make([]byte, prng.Intn(2000)) expectedContents := make([]byte, prng.Intn(2000))
prng.Read(expectedContents) prng.Read(expectedContents)
var data []byte var data []byte
data = append(data, expectedPartNum, expectedMaxParts) data = append(data, expectedVersion, expectedPartNum, expectedMaxParts)
data = append(data, size...) data = append(data, size...)
data = append(data, expectedContents...) data = append(data, expectedContents...)
......
...@@ -26,7 +26,7 @@ func TestManager_GetMaxTransmissionPayloadSize(t *testing.T) { ...@@ -26,7 +26,7 @@ func TestManager_GetMaxTransmissionPayloadSize(t *testing.T) {
m := newTestManager(0, false, t) m := newTestManager(0, false, t)
cmixPrimeSize := m.store.Cmix().GetGroup().GetP().ByteLen() cmixPrimeSize := m.store.Cmix().GetGroup().GetP().ByteLen()
e2ePrimeSize := m.store.E2e().GetGroup().GetP().ByteLen() e2ePrimeSize := m.store.E2e().GetGroup().GetP().ByteLen()
expectedSize := 2*cmixPrimeSize - e2ePrimeSize - format.KeyFPLen - format.MacLen - format.RecipientIDLen - transmitPlMinSize expectedSize := 2*cmixPrimeSize - e2ePrimeSize - format.KeyFPLen - format.MacLen - format.RecipientIDLen - transmitPlMinSize - transmitMessageVersionSize
testSize := m.GetMaxTransmissionPayloadSize() testSize := m.GetMaxTransmissionPayloadSize()
if expectedSize != testSize { if expectedSize != testSize {
...@@ -260,7 +260,7 @@ func TestManager_makeTransmitCmixMessage(t *testing.T) { ...@@ -260,7 +260,7 @@ func TestManager_makeTransmitCmixMessage(t *testing.T) {
DhPubKey: m.store.E2e().GetGroup().NewInt(42), DhPubKey: m.store.E2e().GetGroup().NewInt(42),
} }
tag := "Test tag" tag := "Test tag"
payload := make([]byte, 132) payload := make([]byte, 131)
rand.New(rand.NewSource(42)).Read(payload) rand.New(rand.NewSource(42)).Read(payload)
maxMsgs := uint8(8) maxMsgs := uint8(8)
timeNow := netTime.Now() timeNow := netTime.Now()
......
...@@ -19,19 +19,23 @@ import ( ...@@ -19,19 +19,23 @@ import (
) )
/* /*
+-----------------------------------------------------------------+ +------------------------------------------------------------------------------+
| CMIX Message Contents | | CMIX Message Contents |
+------------+-------------------------------------------- --------+ +------------+-------------------------------------------- --------+
| pubKey | payload (transmitMessagePayload) | | Version | pubKey | payload (transmitMessagePayload) |
| pubKeySize | externalPayloadSize - pubKeySize | | 1 byte | pubKeySize | externalPayloadSize - pubKeySize |
+------------+----------+---------+----------+---------+----------+ +------------+------------+----------+---------+----------+---------+----------+
| Tag FP | nonce | maxParts | size | contents | | Tag FP | nonce | maxParts | size | contents |
| 16 bytes | 8 bytes | 1 byte | 2 bytes | variable | | 16 bytes | 8 bytes | 1 byte | 2 bytes | variable |
+----------+---------+----------+---------+----------+ +----------+---------+----------+---------+----------+
*/ */
const transmitMessageVersion = 0
const transmitMessageVersionSize = 1
type transmitMessage struct { type transmitMessage struct {
data []byte // Serial of all contents data []byte // Serial of all contents
version []byte
pubKey []byte pubKey []byte
payload []byte // The encrypted payload containing reception ID and contents payload []byte // The encrypted payload containing reception ID and contents
} }
...@@ -45,7 +49,10 @@ func newTransmitMessage(externalPayloadSize, pubKeySize int) transmitMessage { ...@@ -45,7 +49,10 @@ func newTransmitMessage(externalPayloadSize, pubKeySize int) transmitMessage {
externalPayloadSize, pubKeySize) externalPayloadSize, pubKeySize)
} }
return mapTransmitMessage(make([]byte, externalPayloadSize), pubKeySize) tm := mapTransmitMessage(make([]byte, externalPayloadSize), pubKeySize)
tm.version[0] = transmitMessageVersion
return tm
} }
// mapTransmitMessage builds a message mapped to the passed in data. It is // mapTransmitMessage builds a message mapped to the passed in data. It is
...@@ -53,8 +60,9 @@ func newTransmitMessage(externalPayloadSize, pubKeySize int) transmitMessage { ...@@ -53,8 +60,9 @@ func newTransmitMessage(externalPayloadSize, pubKeySize int) transmitMessage {
func mapTransmitMessage(data []byte, pubKeySize int) transmitMessage { func mapTransmitMessage(data []byte, pubKeySize int) transmitMessage {
return transmitMessage{ return transmitMessage{
data: data, data: data,
pubKey: data[:pubKeySize], version: data[:transmitMessageVersionSize],
payload: data[pubKeySize:], pubKey: data[transmitMessageVersionSize:transmitMessageVersionSize+pubKeySize],
payload: data[transmitMessageVersionSize+pubKeySize:],
} }
} }
...@@ -79,6 +87,11 @@ func (m transmitMessage) GetPubKey(grp *cyclic.Group) *cyclic.Int { ...@@ -79,6 +87,11 @@ func (m transmitMessage) GetPubKey(grp *cyclic.Group) *cyclic.Int {
return grp.NewIntFromBytes(m.pubKey) return grp.NewIntFromBytes(m.pubKey)
} }
// Version returns the version of the message.
func (m transmitMessage) Version() uint8 {
return m.version[0]
}
// GetPubKeySize returns the length of the public key. // GetPubKeySize returns the length of the public key.
func (m transmitMessage) GetPubKeySize() int { func (m transmitMessage) GetPubKeySize() int {
return len(m.pubKey) return len(m.pubKey)
......
...@@ -27,8 +27,9 @@ func Test_newTransmitMessage(t *testing.T) { ...@@ -27,8 +27,9 @@ func Test_newTransmitMessage(t *testing.T) {
pubKeySize := prng.Intn(externalPayloadSize) pubKeySize := prng.Intn(externalPayloadSize)
expected := transmitMessage{ expected := transmitMessage{
data: make([]byte, externalPayloadSize), data: make([]byte, externalPayloadSize),
version: make([]byte, transmitMessageVersionSize),
pubKey: make([]byte, pubKeySize), pubKey: make([]byte, pubKeySize),
payload: make([]byte, externalPayloadSize-pubKeySize), payload: make([]byte, externalPayloadSize-pubKeySize-transmitMessageVersionSize),
} }
m := newTransmitMessage(externalPayloadSize, pubKeySize) m := newTransmitMessage(externalPayloadSize, pubKeySize)
...@@ -60,7 +61,9 @@ func Test_mapTransmitMessage(t *testing.T) { ...@@ -60,7 +61,9 @@ func Test_mapTransmitMessage(t *testing.T) {
prng.Read(pubKey) prng.Read(pubKey)
payload := make([]byte, externalPayloadSize-pubKeySize) payload := make([]byte, externalPayloadSize-pubKeySize)
prng.Read(payload) prng.Read(payload)
version := make([]byte, 1)
var data []byte var data []byte
data = append(data, version...)
data = append(data, pubKey...) data = append(data, pubKey...)
data = append(data, payload...) data = append(data, payload...)
m := mapTransmitMessage(data, pubKeySize) m := mapTransmitMessage(data, pubKeySize)
...@@ -137,7 +140,8 @@ func TestTransmitMessage_SetPayload_GetPayload_GetPayloadSize(t *testing.T) { ...@@ -137,7 +140,8 @@ func TestTransmitMessage_SetPayload_GetPayload_GetPayloadSize(t *testing.T) {
prng := rand.New(rand.NewSource(42)) prng := rand.New(rand.NewSource(42))
externalPayloadSize := prng.Intn(2000) externalPayloadSize := prng.Intn(2000)
pubKeySize := prng.Intn(externalPayloadSize) pubKeySize := prng.Intn(externalPayloadSize)
payload := make([]byte, externalPayloadSize-pubKeySize) payloadSize := externalPayloadSize - pubKeySize-transmitMessageVersionSize
payload := make([]byte, payloadSize)
prng.Read(payload) prng.Read(payload)
m := newTransmitMessage(externalPayloadSize, pubKeySize) m := newTransmitMessage(externalPayloadSize, pubKeySize)
...@@ -149,7 +153,7 @@ func TestTransmitMessage_SetPayload_GetPayload_GetPayloadSize(t *testing.T) { ...@@ -149,7 +153,7 @@ func TestTransmitMessage_SetPayload_GetPayload_GetPayloadSize(t *testing.T) {
"\nexpected: %+v\nreceived: %+v", payload, testPayload) "\nexpected: %+v\nreceived: %+v", payload, testPayload)
} }
payloadSize := externalPayloadSize - pubKeySize
if payloadSize != m.GetPayloadSize() { if payloadSize != m.GetPayloadSize() {
t.Errorf("GetContentsSize() returned incorrect content size."+ t.Errorf("GetContentsSize() returned incorrect content size."+
"\nexpected: %d\nreceived: %d", payloadSize, m.GetPayloadSize()) "\nexpected: %d\nreceived: %d", payloadSize, m.GetPayloadSize())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment