From fb54ddda01cdb03ff1e23609167d2b7404ab3b9c Mon Sep 17 00:00:00 2001 From: Niamh Nikali <niamh@elixxir.io> Date: Fri, 28 Jun 2019 13:29:19 -0700 Subject: [PATCH] Fix compile against new libraries --- api/client.go | 6 ++-- api/client_test.go | 21 ++++++----- api/mockserver.go | 6 ++-- bindings/client.go | 11 +++--- bots/bots.go | 21 ++++------- bots/bots_test.go | 16 ++++----- cmd/root.go | 15 ++++---- crypto/decrypt.go | 18 +++++----- crypto/encrypt.go | 34 +++++++----------- crypto/encryptdecrypt_test.go | 68 +++++++++++++++++------------------ glide.yaml | 4 +-- io/collate.go | 24 +++++++------ io/collate_test.go | 10 +++--- io/interface.go | 7 ++-- io/messaging.go | 57 ++++++++++++----------------- keyStore/e2eKey.go | 5 +-- keyStore/keyManager.go | 13 +++---- keyStore/keyStack_test.go | 10 +++--- keyStore/keyStore.go | 3 +- parse/message.go | 29 ++++++++++----- parse/partition.go | 15 ++++++-- parse/partition_test.go | 19 ++++++---- rekey/rekey.go | 17 +++++---- rekey/rekey_test.go | 21 ++++++----- 24 files changed, 228 insertions(+), 222 deletions(-) diff --git a/api/client.go b/api/client.go index 40c45875e..0209d0d09 100644 --- a/api/client.go +++ b/api/client.go @@ -31,7 +31,6 @@ import ( "gitlab.com/elixxir/crypto/large" "gitlab.com/elixxir/crypto/registration" "gitlab.com/elixxir/crypto/signature" - "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/id" "gitlab.com/elixxir/primitives/switchboard" "google.golang.org/grpc/credentials" @@ -411,10 +410,9 @@ func (cl *Client) SetRateLimiting(limit uint32) { (cl.comm).(*io.Messaging).TransmitDelay = time.Duration(limit) * time.Millisecond } -func (cl *Client) Listen(user *id.User, outerType format.CryptoType, - messageType int32, newListener switchboard.Listener) string { +func (cl *Client) Listen(user *id.User, messageType int32, newListener switchboard.Listener) string { listenerId := cl.sess.GetSwitchboard(). - Register(user, outerType, messageType, newListener) + Register(user, messageType, newListener) globals.Log.INFO.Printf("Listening now: user %v, message type %v, id %v", user, messageType, listenerId) return listenerId diff --git a/api/client_test.go b/api/client_test.go index 0a3531d3d..319b4c01a 100644 --- a/api/client_test.go +++ b/api/client_test.go @@ -38,7 +38,10 @@ func TestRegistrationGob(t *testing.T) { t.Error(err) } - testClient.Connect(RegGWAddresses[:], "", "", "") + err = testClient.Connect(RegGWAddresses[:], "", "", "") + if err != nil { + t.Error(err) + } // populate a gob in the store grp := getGroup() @@ -215,7 +218,7 @@ func TestRegisterUserE2E(t *testing.T) { key, action := km.PopKey() if key == nil { t.Errorf("TransmissionKeys map returned nil") - } else if key.GetOuterType() != format.E2E { + } else if key.GetOuterType() != parse.E2E { t.Errorf("Key type expected 'E2E', got %s", key.GetOuterType()) } else if action != keyStore.None { @@ -226,7 +229,7 @@ func TestRegisterUserE2E(t *testing.T) { key, action = km.PopRekey() if key == nil { t.Errorf("TransmissionReKeys map returned nil") - } else if key.GetOuterType() != format.Rekey { + } else if key.GetOuterType() != parse.Rekey { t.Errorf("Key type expected 'Rekey', got %s", key.GetOuterType()) } else if action != keyStore.None { @@ -248,7 +251,7 @@ func TestRegisterUserE2E(t *testing.T) { key = session.GetKeyStore().GetRecvKey(fp) if key == nil { t.Errorf("ReceptionKeys map returned nil for Key") - } else if key.GetOuterType() != format.E2E { + } else if key.GetOuterType() != parse.E2E { t.Errorf("Key type expected 'E2E', got %s", key.GetOuterType()) } @@ -260,7 +263,7 @@ func TestRegisterUserE2E(t *testing.T) { key = session.GetKeyStore().GetRecvKey(fp) if key == nil { t.Errorf("ReceptionKeys map returned nil for ReKey") - } else if key.GetOuterType() != format.Rekey { + } else if key.GetOuterType() != parse.Rekey { t.Errorf("Key type expected 'Rekey', got %s", key.GetOuterType()) } @@ -314,7 +317,7 @@ func TestRegisterUserE2E_CheckAllKeys(t *testing.T) { key, action := km.PopKey() if key == nil { t.Errorf("TransmissionKeys map returned nil") - } else if key.GetOuterType() != format.E2E { + } else if key.GetOuterType() != parse.E2E { t.Errorf("Key type expected 'E2E', got %s", key.GetOuterType()) } @@ -342,7 +345,7 @@ func TestRegisterUserE2E_CheckAllKeys(t *testing.T) { key, action := km.PopRekey() if key == nil { t.Errorf("TransmissionReKeys map returned nil") - } else if key.GetOuterType() != format.Rekey { + } else if key.GetOuterType() != parse.Rekey { t.Errorf("Key type expected 'Rekey', got %s", key.GetOuterType()) } @@ -376,7 +379,7 @@ func TestRegisterUserE2E_CheckAllKeys(t *testing.T) { key := session.GetKeyStore().GetRecvKey(fp) if key == nil { t.Errorf("ReceptionKeys map returned nil for Key") - } else if key.GetOuterType() != format.E2E { + } else if key.GetOuterType() != parse.E2E { t.Errorf("Key type expected 'E2E', got %s", key.GetOuterType()) } @@ -395,7 +398,7 @@ func TestRegisterUserE2E_CheckAllKeys(t *testing.T) { key := session.GetKeyStore().GetRecvKey(fp) if key == nil { t.Errorf("ReceptionKeys map returned nil for Rekey") - } else if key.GetOuterType() != format.Rekey { + } else if key.GetOuterType() != parse.Rekey { t.Errorf("Key type expected 'Rekey', got %s", key.GetOuterType()) } diff --git a/api/mockserver.go b/api/mockserver.go index 9ff19efa8..7a859b103 100644 --- a/api/mockserver.go +++ b/api/mockserver.go @@ -9,9 +9,9 @@ package api import ( "gitlab.com/elixxir/client/cmixproto" + "gitlab.com/elixxir/client/parse" pb "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/crypto/large" - "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/id" "sync" ) @@ -40,8 +40,8 @@ func (m APIMessage) GetMessageType() int32 { return int32(cmixproto.Type_NO_TYPE) } -func (m APIMessage) GetCryptoType() format.CryptoType { - return format.None +func (m APIMessage) GetCryptoType() parse.CryptoType { + return parse.None } func (m APIMessage) Pack() []byte { diff --git a/bindings/client.go b/bindings/client.go index d4437fdbb..0a10155ee 100644 --- a/bindings/client.go +++ b/bindings/client.go @@ -12,7 +12,6 @@ import ( "gitlab.com/elixxir/client/globals" "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/crypto/cyclic" - "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/id" "gitlab.com/elixxir/primitives/switchboard" "io" @@ -73,7 +72,7 @@ func (cl *Client) Listen(userId []byte, messageType int32, newListener Listener) listener := &listenerProxy{proxy: newListener} - return cl.client.Listen(typedUserId, format.None, messageType, listener) + return cl.client.Listen(typedUserId, messageType, listener) } // Pass the listener handle that Listen() returned to delete the listener @@ -170,11 +169,11 @@ func (cl *Client) Send(m Message, encrypt bool) error { sender := id.NewUserFromBytes(m.GetSender()) recipient := id.NewUserFromBytes(m.GetRecipient()) - var cryptoType format.CryptoType + var cryptoType parse.CryptoType if encrypt { - cryptoType = format.E2E + cryptoType = parse.E2E } else { - cryptoType = format.Unencrypted + cryptoType = parse.Unencrypted } return cl.client.Send(&parse.Message{ @@ -182,7 +181,7 @@ func (cl *Client) Send(m Message, encrypt bool) error { MessageType: m.GetMessageType(), Body: m.GetPayload(), }, - CryptoType: cryptoType, + InferredType: cryptoType, Sender: sender, Receiver: recipient, }) diff --git a/bots/bots.go b/bots/bots.go index 2db7183ef..bafb4768b 100644 --- a/bots/bots.go +++ b/bots/bots.go @@ -6,7 +6,6 @@ import ( "gitlab.com/elixxir/client/io" "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/client/user" - "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/id" "gitlab.com/elixxir/primitives/switchboard" ) @@ -62,24 +61,18 @@ func InitBots(s user.Session, m io.Communications) { messaging = m l := session.GetSwitchboard() - l.Register(UdbID, - format.None, int32(cmixproto.Type_UDB_PUSH_KEY_RESPONSE), + l.Register(UdbID, int32(cmixproto.Type_UDB_PUSH_KEY_RESPONSE), &pushKeyResponseListener) - l.Register(UdbID, - format.None, int32(cmixproto.Type_UDB_GET_KEY_RESPONSE), + l.Register(UdbID, int32(cmixproto.Type_UDB_GET_KEY_RESPONSE), &getKeyResponseListener) - l.Register(UdbID, - format.None, int32(cmixproto.Type_UDB_REGISTER_RESPONSE), + l.Register(UdbID, int32(cmixproto.Type_UDB_REGISTER_RESPONSE), ®isterResponseListener) - l.Register(UdbID, - format.None, int32(cmixproto.Type_UDB_SEARCH_RESPONSE), + l.Register(UdbID, int32(cmixproto.Type_UDB_SEARCH_RESPONSE), &searchResponseListener) l.Register(id.ZeroID, - format.None, int32(cmixproto.Type_NICKNAME_REQUEST), - &nicknameRequestListener) + int32(cmixproto.Type_NICKNAME_REQUEST), &nicknameRequestListener) l.Register(id.ZeroID, - format.None, int32(cmixproto.Type_NICKNAME_RESPONSE), - &nicknameResponseListener) + int32(cmixproto.Type_NICKNAME_RESPONSE), &nicknameResponseListener) } // sendCommand sends a command to the udb. This doesn't block. @@ -87,7 +80,7 @@ func InitBots(s user.Session, m io.Communications) { // listener. func sendCommand(botID *id.User, command []byte) error { return messaging.SendMessage(session, botID, - format.Unencrypted, command) + parse.Unencrypted, command) } // Nickname Lookup function diff --git a/bots/bots_test.go b/bots/bots_test.go index 1cb0b5127..c1244c17a 100644 --- a/bots/bots_test.go +++ b/bots/bots_test.go @@ -32,7 +32,7 @@ type dummyMessaging struct { // SendMessage to the server func (d *dummyMessaging) SendMessage(sess user.Session, recipientID *id.User, - cryptoType format.CryptoType, + cryptoType parse.CryptoType, message []byte) error { jww.INFO.Printf("Sending: %s", string(message)) return nil @@ -41,7 +41,7 @@ func (d *dummyMessaging) SendMessage(sess user.Session, // SendMessage without partitions to the server func (d *dummyMessaging) SendMessageNoPartition(sess user.Session, recipientID *id.User, - cryptoType format.CryptoType, + cryptoType parse.CryptoType, message []byte) error { jww.INFO.Printf("Sending: %s", string(message)) return nil @@ -127,8 +127,8 @@ func TestNicknameFunctions(t *testing.T) { MessageType: int32(cmixproto.Type_NICKNAME_REQUEST), Body: []byte{}, }, - CryptoType: format.Unencrypted, - Receiver: session.GetCurrentUser().User, + InferredType: parse.Unencrypted, + Receiver: session.GetCurrentUser().User, } session.GetSwitchboard().Speak(msg) @@ -153,8 +153,8 @@ func TestNicknameFunctions(t *testing.T) { MessageType: int32(cmixproto.Type_NICKNAME_RESPONSE), Body: []byte(session.GetCurrentUser().Nick), }, - CryptoType: format.Unencrypted, - Receiver: session.GetCurrentUser().User, + InferredType: parse.Unencrypted, + Receiver: session.GetCurrentUser().User, } session.GetSwitchboard().Speak(msg) } @@ -164,7 +164,7 @@ type errorMessaging struct{} // SendMessage that just errors out func (e *errorMessaging) SendMessage(sess user.Session, recipientID *id.User, - cryptoType format.CryptoType, + cryptoType parse.CryptoType, message []byte) error { return errors.New("This is an error") } @@ -172,7 +172,7 @@ func (e *errorMessaging) SendMessage(sess user.Session, // SendMessage no partition that just errors out func (e *errorMessaging) SendMessageNoPartition(sess user.Session, recipientID *id.User, - cryptoType format.CryptoType, + cryptoType parse.CryptoType, message []byte) error { return errors.New("This is an error") } diff --git a/cmd/root.go b/cmd/root.go index d310b7f5c..a5d06e082 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,7 +22,6 @@ import ( "gitlab.com/elixxir/client/user" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/large" - "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/id" "gitlab.com/elixxir/primitives/switchboard" "io/ioutil" @@ -319,15 +318,15 @@ var rootCmd = &cobra.Command{ // the integration test // Normal text messages text := TextListener{} - client.Listen(id.ZeroID, format.None, int32(cmixproto.Type_TEXT_MESSAGE), + client.Listen(id.ZeroID, int32(cmixproto.Type_TEXT_MESSAGE), &text) // Channel messages channel := ChannelListener{} - client.Listen(id.ZeroID, format.None, + client.Listen(id.ZeroID, int32(cmixproto.Type_CHANNEL_MESSAGE), &channel) // All other messages fallback := FallbackListener{} - client.Listen(id.ZeroID, format.None, int32(cmixproto.Type_NO_TYPE), + client.Listen(id.ZeroID, int32(cmixproto.Type_NO_TYPE), &fallback) // Do calculation for dummy messages if the flag is set @@ -336,9 +335,9 @@ var rootCmd = &cobra.Command{ (time.Duration(float64(1000000000) * (float64(1.0) / dummyFrequency))) } - cryptoType := format.Unencrypted + cryptoType := parse.Unencrypted if end2end { - cryptoType = format.E2E + cryptoType = parse.E2E } // Only send a message if we have a message to send (except dummy messages) @@ -368,7 +367,7 @@ var rootCmd = &cobra.Command{ MessageType: int32(cmixproto.Type_TEXT_MESSAGE), Body: wireOut, }, - CryptoType: cryptoType, + InferredType: cryptoType, Receiver: recipientId, }) } @@ -397,7 +396,7 @@ var rootCmd = &cobra.Command{ MessageType: int32(cmixproto.Type_TEXT_MESSAGE), Body: api.FormatTextMessage(message), }, - CryptoType: cryptoType, + InferredType: cryptoType, Receiver: recipientId} client.Send(message) diff --git a/crypto/decrypt.go b/crypto/decrypt.go index 9b5e62ca9..a90367266 100644 --- a/crypto/decrypt.go +++ b/crypto/decrypt.go @@ -21,12 +21,11 @@ import ( func E2EDecrypt(grp *cyclic.Group, key *cyclic.Int, msg *format.Message) error { // First thing to do is check MAC - if !hash.VerifyHMAC(msg.SerializePayload(), - msg.GetMAC(), key.Bytes()) { + if !hash.VerifyHMAC(msg.Contents.Get(), msg.GetMAC(), key.Bytes()) { return errors.New("HMAC verification failed for E2E message") } var iv [e2e.AESBlockSize]byte - fp := msg.GetKeyFingerprint() + fp := msg.GetKeyFP() copy(iv[:], fp[:e2e.AESBlockSize]) // decrypt the timestamp in the associated data decryptedTimestamp, err := e2e.DecryptAES256WithIV( @@ -38,11 +37,11 @@ func E2EDecrypt(grp *cyclic.Group, key *cyclic.Int, // to mobile developers on the bindings to interact with the timestamps msg.SetTimestamp(decryptedTimestamp) // Decrypt e2e - decryptedPayload, err := e2e.Decrypt(grp, key, msg.SerializePayload()) + decryptedPayload, err := e2e.Decrypt(grp, key, msg.Contents.Get()) if err != nil { return errors.New("Failed to decrypt E2E message: " + err.Error()) } - msg.SetSplitPayload(decryptedPayload) + msg.Contents.Set(decryptedPayload) return nil } @@ -53,12 +52,11 @@ func E2EDecrypt(grp *cyclic.Group, key *cyclic.Int, func E2EDecryptUnsafe(grp *cyclic.Group, key *cyclic.Int, msg *format.Message) error { // First thing to do is check MAC - if !hash.VerifyHMAC(msg.SerializePayload(), - msg.GetMAC(), key.Bytes()) { + if !hash.VerifyHMAC(msg.Contents.Get(), msg.GetMAC(), key.Bytes()) { return errors.New("HMAC verification failed for E2E message") } var iv [e2e.AESBlockSize]byte - fp := msg.GetKeyFingerprint() + fp := msg.GetKeyFP() copy(iv[:], fp[:e2e.AESBlockSize]) // decrypt the timestamp in the associated data decryptedTimestamp, err := e2e.DecryptAES256WithIV( @@ -70,7 +68,7 @@ func E2EDecryptUnsafe(grp *cyclic.Group, key *cyclic.Int, // to mobile developers on the bindings to interact with the timestamps msg.SetTimestamp(decryptedTimestamp) // Decrypt e2e - decryptedPayload := e2e.DecryptUnsafe(grp, key, msg.SerializePayload()) - msg.SetSplitPayload(decryptedPayload) + decryptedPayload := e2e.DecryptUnsafe(grp, key, msg.Contents.Get()) + msg.Contents.Set(decryptedPayload) return nil } diff --git a/crypto/encrypt.go b/crypto/encrypt.go index 0324491a1..39a30f0bf 100644 --- a/crypto/encrypt.go +++ b/crypto/encrypt.go @@ -13,7 +13,6 @@ import ( "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/e2e" "gitlab.com/elixxir/crypto/hash" - "gitlab.com/elixxir/crypto/verification" "gitlab.com/elixxir/primitives/format" ) @@ -31,17 +30,7 @@ func CMIXEncrypt(session user.Session, //TODO: Add KMAC generation here } - fp := msg.GetKeyFingerprint() - // Calculate MIC - recipientMicList := [][]byte{ - msg.GetRecipientID(), - fp[:], - msg.GetTimestamp(), - msg.GetMAC(), - } - mic := verification.GenerateMIC(recipientMicList, uint64(format.AD_RMIC_LEN)) - msg.SetRecipientMIC(mic) - return cmix.ClientEncryptDecrypt(true, session.GetGroup(), msg, salt, baseKeys) + return cmix.ClientEncrypt(session.GetGroup(), msg, salt, baseKeys) } // E2EEncrypt uses the E2E key to encrypt msg @@ -51,24 +40,27 @@ func CMIXEncrypt(session user.Session, func E2EEncrypt(grp *cyclic.Group, key *cyclic.Int, keyFP format.Fingerprint, msg *format.Message) { - msg.SetKeyFingerprint(keyFP) + msg.SetKeyFP(keyFP) // Encrypt the timestamp using key // Timestamp bytes were previously stored // and GO only uses 15 bytes, so use those var iv [e2e.AESBlockSize]byte copy(iv[:], keyFP[:e2e.AESBlockSize]) - encryptedTimestamp, _ := + encryptedTimestamp, err := e2e.EncryptAES256WithIV(key.Bytes(), iv, - msg.GetTimestamp()[:15]) + msg.GetTimestamp()) + if err != nil { + panic(err) + } msg.SetTimestamp(encryptedTimestamp) // E2E encrypt the msg - encPayload, err := e2e.Encrypt(grp, key, msg.GetPayload()) + encPayload, err := e2e.Encrypt(grp, key, msg.Contents.Get()) if err != nil { globals.Log.ERROR.Panicf(err.Error()) } - msg.SetPayload(encPayload) + msg.Contents.Set(encPayload) // MAC is HMAC(key, ciphertext) // Currently, the MAC doesn't include any of the associated data @@ -85,7 +77,7 @@ func E2EEncrypt(grp *cyclic.Group, func E2EEncryptUnsafe(grp *cyclic.Group, key *cyclic.Int, keyFP format.Fingerprint, msg *format.Message) { - msg.SetKeyFingerprint(keyFP) + msg.SetKeyFP(keyFP) // Encrypt the timestamp using key // Timestamp bytes were previously stored @@ -94,12 +86,12 @@ func E2EEncryptUnsafe(grp *cyclic.Group, copy(iv[:], keyFP[:e2e.AESBlockSize]) encryptedTimestamp, _ := e2e.EncryptAES256WithIV(key.Bytes(), iv, - msg.GetTimestamp()[:15]) + msg.GetTimestamp()) msg.SetTimestamp(encryptedTimestamp) // E2E encrypt the msg - encPayload := e2e.EncryptUnsafe(grp, key, msg.GetPayload()) - msg.SetPayload(encPayload) + encPayload := e2e.EncryptUnsafe(grp, key, msg.Contents.Get()) + msg.Contents.Set(encPayload) // MAC is HMAC(key, ciphertext) // Currently, the MAC doesn't include any of the associated data diff --git a/crypto/encryptdecrypt_test.go b/crypto/encryptdecrypt_test.go index 6b0bf119b..ba3b0c252 100644 --- a/crypto/encryptdecrypt_test.go +++ b/crypto/encryptdecrypt_test.go @@ -89,13 +89,17 @@ func TestFullEncryptDecrypt(t *testing.T) { sender := id.NewUserFromUint(38, t) recipient := id.NewUserFromUint(29, t) msg := format.NewMessage() - msg.SetSender(sender) msg.SetRecipient(recipient) msgPayload := []byte("help me, i'm stuck in an" + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory") - msg.SetPayloadData(msgPayload) + // Normally, msgPayload would be the right length due to padding + msgPayload = append(msgPayload, make([]byte, + format.ContentsLen-len(msgPayload))...) + msg.Contents.Set(msgPayload) now := time.Now() nowBytes, _ := now.MarshalBinary() + // Normally, nowBytes would be the right length due to AES encryption + nowBytes = append(nowBytes, make([]byte, format.TimestampLen - len(nowBytes))...) msg.SetTimestamp(nowBytes) key := grp.NewInt(42) @@ -114,8 +118,9 @@ func TestFullEncryptDecrypt(t *testing.T) { // This block imitates what the server does during the realtime var encryptedNet *pb.Slot { - payload := grp.NewIntFromBytes(encMsg.SerializePayload()) - assocData := grp.NewIntFromBytes(encMsg.SerializeAssociatedData()) + // FIXME(nen) This is almost certainly wrong now... + payload := grp.NewIntFromBytes(encMsg.GetPayloadA()) + assocData := grp.NewIntFromBytes(encMsg.GetPayloadB()) // Multiply payload and associated data by transmission key only grp.Mul(payload, serverTransmissionKey, payload) // Multiply associated data only by transmission key @@ -123,14 +128,14 @@ func TestFullEncryptDecrypt(t *testing.T) { encryptedNet = &pb.Slot{ SenderID: sender.Bytes(), Salt: salt, - MessagePayload: payload.LeftpadBytes(uint64(format.TOTAL_LEN)), - AssociatedData: assocData.LeftpadBytes(uint64(format.TOTAL_LEN)), + MessagePayload: payload.LeftpadBytes(uint64(format.TotalLen)), + AssociatedData: assocData.LeftpadBytes(uint64(format.TotalLen)), } } decMsg := format.NewMessage() - decMsg.Payload = format.DeserializePayload(encryptedNet.MessagePayload) - decMsg.AssociatedData = format.DeserializeAssociatedData(encryptedNet.AssociatedData) + decMsg.SetPayloadA(encryptedNet.MessagePayload) + decMsg.SetPayloadB(encryptedNet.AssociatedData) // E2E Decryption err := E2EDecrypt(grp, key, decMsg) @@ -139,17 +144,13 @@ func TestFullEncryptDecrypt(t *testing.T) { t.Errorf("E2EDecrypt returned error: %v", err.Error()) } - if *decMsg.GetSender() != *sender { - t.Errorf("Sender differed from expected: Got %q, expected %q", - decMsg.GetRecipient(), sender) - } if *decMsg.GetRecipient() != *recipient { t.Errorf("Recipient differed from expected: Got %q, expected %q", decMsg.GetRecipient(), sender) } - if !bytes.Equal(decMsg.GetPayloadData(), msgPayload) { + if !bytes.Equal(decMsg.Contents.Get(), msgPayload) { t.Errorf("Decrypted payload differed from expected: Got %q, "+ - "expected %q", decMsg.GetPayloadData(), msgPayload) + "expected %q", decMsg.Contents.Get(), msgPayload) } } @@ -166,9 +167,7 @@ func TestFullEncryptDecrypt_Unsafe(t *testing.T) { " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory" + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory" + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory") - // Need to take up space of SenderID - msg.SetSenderID(msgPayload[:format.MP_SID_LEN]) - msg.SetPayloadData(msgPayload[format.MP_SID_LEN:]) + msg.Contents.Set(msgPayload[format.ContentsLen:]) now := time.Now() nowBytes, _ := now.MarshalBinary() msg.SetTimestamp(nowBytes) @@ -189,8 +188,8 @@ func TestFullEncryptDecrypt_Unsafe(t *testing.T) { // This block imitates what the server does during the realtime var encryptedNet *pb.Slot { - payload := grp.NewIntFromBytes(encMsg.SerializePayload()) - assocData := grp.NewIntFromBytes(encMsg.SerializeAssociatedData()) + payload := grp.NewIntFromBytes(encMsg.GetPayloadA()) + assocData := grp.NewIntFromBytes(encMsg.GetPayloadB()) // Multiply payload and associated data by transmission key only grp.Mul(payload, serverTransmissionKey, payload) // Multiply associated data only by transmission key @@ -198,14 +197,14 @@ func TestFullEncryptDecrypt_Unsafe(t *testing.T) { encryptedNet = &pb.Slot{ SenderID: sender.Bytes(), Salt: salt, - MessagePayload: payload.LeftpadBytes(uint64(format.TOTAL_LEN)), - AssociatedData: assocData.LeftpadBytes(uint64(format.TOTAL_LEN)), + MessagePayload: payload.LeftpadBytes(uint64(format.TotalLen)), + AssociatedData: assocData.LeftpadBytes(uint64(format.TotalLen)), } } decMsg := format.NewMessage() - decMsg.AssociatedData = format.DeserializeAssociatedData(encryptedNet.AssociatedData) - decMsg.Payload = format.DeserializePayload(encryptedNet.MessagePayload) + decMsg.SetPayloadA(encryptedNet.MessagePayload) + decMsg.SetPayloadB(encryptedNet.AssociatedData) // E2E Decryption err := E2EDecryptUnsafe(grp, key, decMsg) @@ -218,26 +217,26 @@ func TestFullEncryptDecrypt_Unsafe(t *testing.T) { t.Errorf("Recipient differed from expected: Got %q, expected %q", decMsg.GetRecipient(), sender) } - if !bytes.Equal(decMsg.GetPayload(), msgPayload) { + if !bytes.Equal(decMsg.Contents.Get(), msgPayload) { t.Errorf("Decrypted payload differed from expected: Got %q, "+ - "expected %q", decMsg.GetPayload(), msgPayload) + "expected %q", decMsg.Contents.Get(), msgPayload) } } // Test that E2EEncrypt panics if the payload is too big (can't be padded) func TestE2EEncrypt_Panic(t *testing.T) { grp := session.GetGroup() - sender := id.NewUserFromUint(38, t) recipient := id.NewUserFromUint(29, t) msg := format.NewMessage() - msg.SetSender(sender) msg.SetRecipient(recipient) msgPayload := []byte("help me, i'm stuck in an" + + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory" + + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory" + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory" + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory" + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory" + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory") - msg.SetPayloadData(msgPayload) + msg.Contents.Set(msgPayload) now := time.Now() nowBytes, _ := now.MarshalBinary() msg.SetTimestamp(nowBytes) @@ -261,14 +260,12 @@ func TestE2EEncrypt_Panic(t *testing.T) { // Test that E2EDecrypt and E2EDecryptUnsafe handle errors correctly func TestE2EDecrypt_Errors(t *testing.T) { grp := session.GetGroup() - sender := id.NewUserFromUint(38, t) recipient := id.NewUserFromUint(29, t) msg := format.NewMessage() - msg.SetSender(sender) msg.SetRecipient(recipient) msgPayload := []byte("help me, i'm stuck in an" + " EnterpriseTextLabelDescriptorSetPipelineStateFactoryBeanFactory") - msg.SetPayloadData(msgPayload) + msg.Contents.Set(msgPayload) now := time.Now() nowBytes, _ := now.MarshalBinary() msg.SetTimestamp(nowBytes) @@ -284,8 +281,8 @@ func TestE2EDecrypt_Errors(t *testing.T) { // Copy message badMsg := format.NewMessage() - badMsg.Payload = format.DeserializePayload(msg.SerializePayload()) - badMsg.AssociatedData = format.DeserializeAssociatedData(msg.SerializeAssociatedData()) + badMsg.SetPayloadA(msg.GetPayloadA()) + badMsg.SetPayloadB(msg.GetPayloadB()) // Corrupt MAC to make decryption fail badMsg.SetMAC([]byte("sakfaskfajskasfkkaskfanjjnaf")) @@ -336,10 +333,11 @@ func TestE2EDecrypt_Errors(t *testing.T) { badMsg.SetTimestamp(msg.GetTimestamp()) // Corrupt payload to make decryption fail - badMsg.SetPayload([]byte("sakomnsfjeiknheuijhgfyaistuajhfaiuojfkhufijsahufiaij")) + badMsg.Contents.Set([]byte( + "sakomnsfjeiknheuijhgfyaistuajhfaiuojfkhufijsahufiaij")) // Calculate new MAC to avoid failing on that verification again - newMAC := hash.CreateHMAC(badMsg.SerializePayload(), key.Bytes()) + newMAC := hash.CreateHMAC(badMsg.Contents.Get(), key.Bytes()) badMsg.SetMAC(newMAC) // E2E Decryption returns error diff --git a/glide.yaml b/glide.yaml index 049ea320f..0f45aa617 100644 --- a/glide.yaml +++ b/glide.yaml @@ -4,7 +4,7 @@ import: version: master vcs: git - package: gitlab.com/elixxir/crypto - version: master + version: updateMessageFormat repo: git@gitlab.com:elixxir/crypto vcs: git - package: gitlab.com/elixxir/comms @@ -12,7 +12,7 @@ import: repo: git@gitlab.com:elixxir/comms vcs: git - package: gitlab.com/elixxir/primitives - version: master + version: removeOuterType repo: git@gitlab.com:elixxir/primitives vcs: git - package: github.com/mitchellh/go-homedir diff --git a/io/collate.go b/io/collate.go index 99a538a23..fb7774cae 100644 --- a/io/collate.go +++ b/io/collate.go @@ -12,6 +12,7 @@ import ( "gitlab.com/elixxir/client/globals" "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/primitives/format" + "gitlab.com/elixxir/primitives/id" "sync" "time" ) @@ -48,8 +49,11 @@ func NewCollator() *Collator { func (mb *Collator) AddMessage(message *format.Message, timeout time.Duration) *parse.Message { - payload := message.GetPayloadData() - sender := message.GetSender() + payload := message.Contents.Get() + // There's currently no mechanism for knowing who sent an unencrypted + // message, I think? + // Let's just try ZeroID for now... + sender := id.ZeroID recipient := message.GetRecipient() partition, err := parse.ValidatePartition(payload) @@ -66,10 +70,10 @@ func (mb *Collator) AddMessage(message *format.Message, } msg := parse.Message{ - TypedBody: *typedBody, - CryptoType: format.Unencrypted, - Sender: sender, - Receiver: recipient, + TypedBody: *typedBody, + InferredType: parse.Unencrypted, + Sender: sender, + Receiver: recipient, } return &msg @@ -131,10 +135,10 @@ func (mb *Collator) AddMessage(message *format.Message, } msg := parse.Message{ - TypedBody: *typedBody, - CryptoType: format.Unencrypted, - Sender: sender, - Receiver: recipient, + TypedBody: *typedBody, + InferredType: parse.Unencrypted, + Sender: sender, + Receiver: recipient, } delete(mb.pendingMessages, key) diff --git a/io/collate_test.go b/io/collate_test.go index 34c424f07..0e71dcea7 100644 --- a/io/collate_test.go +++ b/io/collate_test.go @@ -22,7 +22,7 @@ func TestCollator_AddMessage(t *testing.T) { pendingMessages: make(map[PendingMessageKey]*multiPartMessage), } var bodies [][]byte - for length := 5; length < 20*format.MP_PAYLOAD_LEN; length += 20 { + for length := 5; length < 20*format.TotalLen; length += 20 { newBody := make([]byte, length) _, err := rand.Read(newBody) if err != nil { @@ -40,9 +40,8 @@ func TestCollator_AddMessage(t *testing.T) { for j := range partitions { fm := format.NewMessage() - fm.SetSender(id.NewUserFromUint(5, t)) fm.SetRecipient(id.NewUserFromUint(6, t)) - fm.SetPayloadData(partitions[j]) + fm.Contents.Set(partitions[j]) result = collator.AddMessage(fm, time.Minute) } @@ -66,7 +65,7 @@ func TestCollator_AddMessage_Timeout(t *testing.T) { pendingMessages: make(map[PendingMessageKey]*multiPartMessage), } //enough for four partitions, probably - body := make([]byte, 3*format.MP_PAYLOAD_LEN) + body := make([]byte, 3*format.ContentsLen) partitions, err := parse.Partition(body, []byte{88}) if err != nil { t.Errorf("Error partitioning messages: %v", err.Error()) @@ -74,9 +73,8 @@ func TestCollator_AddMessage_Timeout(t *testing.T) { var result *parse.Message for i := range partitions { fm := format.NewMessage() - fm.SetSender(id.NewUserFromUint(5, t)) fm.SetRecipient(id.NewUserFromUint(6, t)) - fm.SetPayload(partitions[i]) + fm.Contents.Set(partitions[i]) result = collator.AddMessage(fm, 80*time.Millisecond) if result != nil { diff --git a/io/interface.go b/io/interface.go index 043e3ceb0..c8015f92f 100644 --- a/io/interface.go +++ b/io/interface.go @@ -8,8 +8,8 @@ package io import ( + "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/client/user" - "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/id" "time" ) @@ -17,12 +17,13 @@ import ( // Communication interface implements send/receive functionality with the server type Communications interface { // SendMessage to the server + // TODO(nen) Can we get rid of the crypto type param here? SendMessage(session user.Session, recipientID *id.User, - cryptoType format.CryptoType, message []byte) error + cryptoType parse.CryptoType, message []byte) error // SendMessage without partitions to the server // This is used to send rekey messages SendMessageNoPartition(session user.Session, recipientID *id.User, - cryptoType format.CryptoType, message []byte) error + cryptoType parse.CryptoType, message []byte) error // MessageReceiver thread to get new messages MessageReceiver(session user.Session, delay time.Duration) } diff --git a/io/messaging.go b/io/messaging.go index 04a137678..1fa1e6b58 100644 --- a/io/messaging.go +++ b/io/messaging.go @@ -74,7 +74,7 @@ func NewMessenger() *Messaging { // w.r.t. generating message IDs for multi-part messages.) func (m *Messaging) SendMessage(session user.Session, recipientID *id.User, - cryptoType format.CryptoType, + cryptoType parse.CryptoType, message []byte) error { // FIXME: We should really bring the plaintext parts of the NewMessage logic // into this module, then have an EncryptedMessage type that is sent to/from @@ -85,7 +85,6 @@ func (m *Messaging) SendMessage(session user.Session, // TBD: Is there a really good reason why we'd ever have more than one user // in this library? why not pass a sender object instead? globals.Log.DEBUG.Printf("Sending message to %q: %q", *recipientID, message) - userID := session.GetCurrentUser().User parts, err := parse.Partition([]byte(message), m.nextId()) if err != nil { @@ -102,12 +101,11 @@ func (m *Messaging) SendMessage(session user.Session, } for i := range parts { message := format.NewMessage() - message.SetSender(userID) message.SetRecipient(recipientID) // The timestamp will be encrypted later // NOTE: This sets 15 bytes, not 16 message.SetTimestamp(nowBytes) - message.SetPayloadData(parts[i]) + message.Contents.Set(parts[i]) err = m.send(session, cryptoType, message, false) if err != nil { return fmt.Errorf("SendMessage send() error: %v", err.Error()) @@ -121,13 +119,12 @@ func (m *Messaging) SendMessage(session user.Session, // message, where a new public key will take up the whole message func (m *Messaging) SendMessageNoPartition(session user.Session, recipientID *id.User, - cryptoType format.CryptoType, + cryptoType parse.CryptoType, message []byte) error { size := len(message) - if size > format.TOTAL_LEN { + if size > format.TotalLen { return fmt.Errorf("SendMessageNoPartition() error: message to be sent is too big") } - userID := session.GetCurrentUser().User now := time.Now() // GO Timestamp binary serialization is 15 bytes, which // allows the encrypted timestamp to fit in 16 bytes @@ -141,15 +138,7 @@ func (m *Messaging) SendMessageNoPartition(session user.Session, // The timestamp will be encrypted later // NOTE: This sets 15 bytes, not 16 msg.SetTimestamp(nowBytes) - // If message is bigger than payload size - // use SenderID space to send it - if size > format.MP_PAYLOAD_LEN { - msg.SetSenderID(message[:format.MP_SID_END]) - msg.SetPayloadData(message[format.MP_SID_END:]) - } else { - msg.SetSender(userID) - msg.SetPayloadData(message) - } + msg.Contents.Set(message) globals.Log.DEBUG.Printf("Sending message to %v: %x", *recipientID, message) err = m.send(session, cryptoType, msg, true) if err != nil { @@ -160,7 +149,7 @@ func (m *Messaging) SendMessageNoPartition(session user.Session, // send actually sends the message to the server func (m *Messaging) send(session user.Session, - cryptoType format.CryptoType, + cryptoType parse.CryptoType, message *format.Message, rekey bool) error { // Enable transmission blocking if enabled @@ -173,14 +162,14 @@ func (m *Messaging) send(session user.Session, } // Check message type - if cryptoType == format.E2E { + if cryptoType == parse.E2E { handleE2ESending(session, message, rekey) } else { - padded, err := e2e.Pad(message.GetPayload(), format.TOTAL_LEN) + padded, err := e2e.Pad(message.Contents.Get(), format.ContentsLen) if err != nil { return err } - message.SetPayload(padded) + message.Contents.Set(padded) e2e.SetUnencrypted(message) } @@ -190,8 +179,8 @@ func (m *Messaging) send(session user.Session, msgPacket := &pb.Slot{ SenderID: session.GetCurrentUser().User.Bytes(), - MessagePayload: encMsg.SerializePayload(), - AssociatedData: encMsg.SerializeAssociatedData(), + MessagePayload: encMsg.GetPayloadA(), + AssociatedData: encMsg.GetPayloadB(), Salt: salt, KMACs: make([][]byte, 0), } @@ -242,8 +231,8 @@ func handleE2ESending(session user.Session, MessageType: int32(cmixproto.Type_REKEY_TRIGGER), Body: []byte{}, }, - CryptoType: format.None, - Receiver: recipientID, + InferredType: parse.None, + Receiver: recipientID, } go session.GetSwitchboard().Speak(rekeyMsg) } @@ -302,7 +291,7 @@ func (m *Messaging) MessageReceiver(session user.Session, delay time.Duration) { func handleE2EReceiving(session user.Session, message *format.Message) (bool, error) { - keyFingerprint := message.GetKeyFingerprint() + keyFingerprint := message.GetKeyFP() // Lookup reception key recpKey := session.GetKeyStore(). @@ -312,7 +301,7 @@ func handleE2EReceiving(session user.Session, if recpKey == nil { // TODO Handle sending error message to SW return rekey, fmt.Errorf("E2EKey for matching fingerprint not found, can't process message") - } else if recpKey.GetOuterType() == format.Rekey { + } else if recpKey.GetOuterType() == parse.Rekey { // If key type is rekey, the message is a rekey from partner rekey = true } @@ -335,15 +324,15 @@ func handleE2EReceiving(session user.Session, // Send rekey message to switchboard if rekey { partner := recpKey.GetManager().GetPartner() - partnerPubKey := message.SerializePayload() + partnerPubKey := message.Contents.Get() rekeyMsg := &parse.Message{ Sender: partner, TypedBody: parse.TypedBody{ MessageType: int32(cmixproto.Type_NO_TYPE), Body: partnerPubKey, }, - CryptoType: format.Rekey, - Receiver: session.GetCurrentUser().User, + InferredType: parse.Rekey, + Receiver: session.GetCurrentUser().User, } go session.GetSwitchboard().Speak(rekeyMsg) } @@ -392,10 +381,8 @@ func (m *Messaging) receiveMessagesFromGateway(session user.Session, } msg := format.NewMessage() - msg.AssociatedData = format.DeserializeAssociatedData( - newMessage.AssociatedData) - msg.Payload = format.DeserializePayload(newMessage. - MessagePayload) + msg.SetPayloadA(newMessage.MessagePayload) + msg.SetPayloadB(newMessage.AssociatedData) var err error = nil var rekey bool @@ -405,9 +392,9 @@ func (m *Messaging) receiveMessagesFromGateway(session user.Session, rekey, err = handleE2EReceiving(session, msg) } else { // If message is non E2E, need to unpad payload - unpadded, err = e2e.Unpad(msg.SerializePayload()) + unpadded, err = e2e.Unpad(msg.Contents.Get()) if err == nil { - msg.SetSplitPayload(unpadded) + msg.Contents.Set(unpadded) } } diff --git a/keyStore/e2eKey.go b/keyStore/e2eKey.go index d45808f60..3c6226477 100644 --- a/keyStore/e2eKey.go +++ b/keyStore/e2eKey.go @@ -1,6 +1,7 @@ package keyStore import ( + "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/hash" "gitlab.com/elixxir/primitives/format" @@ -14,7 +15,7 @@ type E2EKey struct { key *cyclic.Int // Designation of crypto type - outer format.CryptoType + outer parse.CryptoType // keyNum is needed by Key Manager // to keep track of which receiving keys @@ -33,7 +34,7 @@ func (e2ekey *E2EKey) GetKey() *cyclic.Int { } // Get key type, E2E or Rekey -func (e2ekey *E2EKey) GetOuterType() format.CryptoType { +func (e2ekey *E2EKey) GetOuterType() parse.CryptoType { return e2ekey.outer } diff --git a/keyStore/keyManager.go b/keyStore/keyManager.go index e34eb4646..669e72d9f 100644 --- a/keyStore/keyManager.go +++ b/keyStore/keyManager.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/binary" "encoding/gob" + "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/e2e" "gitlab.com/elixxir/primitives/format" @@ -88,11 +89,11 @@ func NewManager(baseKey *cyclic.Int, km.ttl = ttl km.numKeys = numKeys km.numReKeys = numReKeys - for i, _ := range km.recvKeysState { + for i := range km.recvKeysState { km.recvKeysState[i] = new(uint64) *km.recvKeysState[i] = 0 } - for i, _ := range km.recvReKeysState { + for i := range km.recvReKeysState { km.recvReKeysState[i] = new(uint64) *km.recvReKeysState[i] = 0 } @@ -283,7 +284,7 @@ func (km *KeyManager) GenerateKeys(grp *cyclic.Group, userID *id.User, e2ekey := new(E2EKey) e2ekey.key = key e2ekey.manager = km - e2ekey.outer = format.E2E + e2ekey.outer = parse.E2E km.sendKeys.Push(e2ekey) } @@ -295,7 +296,7 @@ func (km *KeyManager) GenerateKeys(grp *cyclic.Group, userID *id.User, e2ekey := new(E2EKey) e2ekey.key = key e2ekey.manager = km - e2ekey.outer = format.Rekey + e2ekey.outer = parse.Rekey km.sendReKeys.Push(e2ekey) } // Add KeyManager to KeyStore map @@ -317,7 +318,7 @@ func (km *KeyManager) GenerateKeys(grp *cyclic.Group, userID *id.User, e2ekey := new(E2EKey) e2ekey.key = key e2ekey.manager = km - e2ekey.outer = format.E2E + e2ekey.outer = parse.E2E e2ekey.keyNum = uint32(i) keyFP := e2ekey.KeyFingerprint() km.recvKeysFingerprint = append(km.recvKeysFingerprint, keyFP) @@ -333,7 +334,7 @@ func (km *KeyManager) GenerateKeys(grp *cyclic.Group, userID *id.User, e2ekey := new(E2EKey) e2ekey.key = key e2ekey.manager = km - e2ekey.outer = format.Rekey + e2ekey.outer = parse.Rekey e2ekey.keyNum = uint32(i) keyFP := e2ekey.KeyFingerprint() km.recvReKeysFingerprint = append(km.recvReKeysFingerprint, keyFP) diff --git a/keyStore/keyStack_test.go b/keyStore/keyStack_test.go index a61db9971..8d4085f87 100644 --- a/keyStore/keyStack_test.go +++ b/keyStore/keyStack_test.go @@ -1,9 +1,9 @@ package keyStore import ( + "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/large" - "gitlab.com/elixxir/primitives/format" "testing" "time" ) @@ -32,7 +32,7 @@ func TestKeyStack(t *testing.T) { for i := 0; i < 100; i++ { key := new(E2EKey) - key.outer = format.E2E + key.outer = parse.E2E key.key = grp.NewInt(int64(i + 2)) key.manager = nil expectedKeys[99-i] = key @@ -57,7 +57,7 @@ func TestKeyStack_Panic(t *testing.T) { for i := 0; i < 10; i++ { key := new(E2EKey) - key.outer = format.E2E + key.outer = parse.E2E key.key = grp.NewInt(int64(i + 2)) key.manager = nil expectedKeys[9-i] = key @@ -88,7 +88,7 @@ func TestKeyStack_Delete(t *testing.T) { for i := 0; i < 100; i++ { key := new(E2EKey) - key.outer = format.E2E + key.outer = parse.E2E key.key = grp.NewInt(int64(i + 2)) key.manager = nil expectedKeys[99-i] = key @@ -123,7 +123,7 @@ func TestKeyStack_Concurrent(t *testing.T) { for i := 0; i < 100; i++ { key := new(E2EKey) - key.outer = format.E2E + key.outer = parse.E2E key.key = grp.NewInt(int64(i + 2)) key.manager = nil expectedKeys[99-i] = key diff --git a/keyStore/keyStore.go b/keyStore/keyStore.go index 85392d0e3..8c92dccc6 100644 --- a/keyStore/keyStore.go +++ b/keyStore/keyStore.go @@ -3,6 +3,7 @@ package keyStore import ( "bytes" "encoding/gob" + "gitlab.com/elixxir/client/parse" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/e2e" "gitlab.com/elixxir/primitives/format" @@ -68,7 +69,7 @@ func (m *inKeyMap) Pop(fingerprint format.Fingerprint) *E2EKey { m.Delete(fingerprint) // Update Key Manager Receiving State key.GetManager().updateRecvState( - key.GetOuterType() == format.Rekey, + key.GetOuterType() == parse.Rekey, key.keyNum) return key } diff --git a/parse/message.go b/parse/message.go index fd34ef7a1..6b7b2ee21 100644 --- a/parse/message.go +++ b/parse/message.go @@ -8,7 +8,6 @@ package parse import ( "crypto/sha256" - "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/id" ) @@ -19,10 +18,24 @@ type MessageHash [MessageHashLen]byte type Message struct { TypedBody - CryptoType format.CryptoType - Sender *id.User - Receiver *id.User - Nonce []byte + // The crypto type is inferred from the message's contents + InferredType CryptoType + Sender *id.User + Receiver *id.User + Nonce []byte +} + +type CryptoType int32 + +const ( + None CryptoType = iota + Unencrypted + Rekey + E2E +) + +func (CryptoType) String() string { + return "yo, there's probably a crypto type that needs to get got around here" } // Interface used to standardize message definitions @@ -38,7 +51,7 @@ type MessageInterface interface { // Return the message's inner type GetMessageType() int32 // Returns the message's outer type - GetCryptoType() format.CryptoType + GetCryptoType() CryptoType // Return the message fully serialized including the type prefix // Does this really belong in the interface? Pack() []byte @@ -78,8 +91,8 @@ func (m *Message) GetMessageType() int32 { return m.MessageType } -func (m *Message) GetCryptoType() format.CryptoType { - return m.CryptoType +func (m *Message) GetCryptoType() CryptoType { + return m.InferredType } func (m *Message) Pack() []byte { diff --git a/parse/partition.go b/parse/partition.go index 23afa2467..4adaeba1a 100644 --- a/parse/partition.go +++ b/parse/partition.go @@ -17,6 +17,14 @@ import ( "sync" ) +// TODO(nen) How the fuck is this supposed to work, +// now that there are two types of padding? +// This might be incorrect, and over-partition the message, +// so I need to come back here and verify this is correct +func getMaxMessageLength() int { + return format.ContentsLen - format.PadMinLen - e2e.MinPaddingLen +} + // TODO is there a better way to generate unique message IDs locally? func IDCounter() func() []byte { // 32 bits to put a smaller upper bound on the varint size on the wire @@ -52,7 +60,7 @@ func GetMaxIndex(body []byte, id []byte) int32 { if bodyLen > 0 { bodyLen-- } - maxIndex := bodyLen / (format.MP_PAYLOAD_LEN - e2e.MinPaddingLen - len(id) - IndexLength) + maxIndex := bodyLen / (getMaxMessageLength() - len(id) - IndexLength) return int32(maxIndex) } @@ -75,7 +83,7 @@ func Partition(body []byte, id []byte) ([][]byte, error) { var lastPartitionLength int partitionReadIdx := 0 for i := range partitions { - maxPartitionLength := format.MP_PAYLOAD_LEN - e2e.MinPaddingLen + maxPartitionLength := getMaxMessageLength() partitions[i], lastPartitionLength = makePartition(maxPartitionLength, body[partitionReadIdx:], id, byte(i), byte(maxIndex)) partitionReadIdx += lastPartitionLength @@ -128,7 +136,8 @@ func makePartition(maxLength int, body []byte, id []byte, i byte, func Assemble(partitions [][]byte) ([]byte, error) { // this will allocate a bit more capacity than needed but not so much that // it breaks the bank - result := make([]byte, 0, int(format.MP_PAYLOAD_LEN)*len(partitions)) + result := make([]byte, 0, int(format.ContentsLen-format.PadMinLen)* + len(partitions)) for i := range partitions { result = append(result, partitions[i]...) diff --git a/parse/partition_test.go b/parse/partition_test.go index d6dec915f..363573911 100644 --- a/parse/partition_test.go +++ b/parse/partition_test.go @@ -61,7 +61,9 @@ func TestPartitionShort(t *testing.T) { // in sum, contains the whole message. func TestPartitionLong(t *testing.T) { id := []byte{0xa2, 0x54} - randomBytes := randomString(0, 300) + // This should be about the right length + // With the other length, the test panicked with out of bounds + randomBytes := randomString(0, getMaxMessageLength()*2-12) actual, err := Partition(randomBytes, id) if err != nil { @@ -75,7 +77,7 @@ func TestPartitionLong(t *testing.T) { expected[0] = append(expected[0], 0, 1) // part of random string expected[0] = append(expected[0], - randomBytes[:format.MP_PAYLOAD_LEN-4-e2e.MinPaddingLen]...) + randomBytes[:getMaxMessageLength()-4]...) // id expected[1] = append(expected[1], id...) @@ -83,7 +85,7 @@ func TestPartitionLong(t *testing.T) { expected[1] = append(expected[1], 1, 1) // other part of random string expected[1] = append(expected[1], - randomBytes[format.MP_PAYLOAD_LEN-4-e2e.MinPaddingLen:]...) + randomBytes[getMaxMessageLength()-4:]...) for i := range actual { if !bytes.Equal(actual[i], expected[i]) { @@ -155,8 +157,12 @@ func TestOnlyAssemble(t *testing.T) { partitions := make([][]byte, len(messageChunks)) for i := range partitions { - e2e.Pad([]byte(messageChunks[i]), format.MP_PAYLOAD_LEN) - partitions[i] = append(partitions[i], messageChunks[i]...) + paddedChunk, err := e2e.Pad([]byte(messageChunks[i]), + format.ContentsLen) + if err != nil { + t.Error(err) + } + partitions[i] = append(partitions[i], paddedChunk...) } assembled, err := Assemble(partitions) @@ -449,7 +455,8 @@ func TestValidatePartition(t *testing.T) { for i := range validPayloads { result, err := ValidatePartition(validPayloads[i]) if err != nil { - t.Errorf("Payload %v was incorrectly invalidated: %v", i, err.Error()) + t.Fatalf("Payload %v was incorrectly invalidated: %v", i, + err.Error()) } if !bytes.Equal(result.ID, expectedIDs[i]) { t.Errorf("Payload %v's ID was parsed incorrectly. Got %v, "+ diff --git a/rekey/rekey.go b/rekey/rekey.go index 7caffa001..453d035e3 100644 --- a/rekey/rekey.go +++ b/rekey/rekey.go @@ -92,13 +92,18 @@ func InitRekey(s user.Session, m io.Communications) { l := session.GetSwitchboard() l.Register(s.GetCurrentUser().User, - format.None, int32(cmixproto.Type_REKEY_TRIGGER), + int32(cmixproto.Type_REKEY_TRIGGER), &rekeyTriggerList) + // TODO(nen) Wouldn't it be possible to register these listeners based + // solely on the inner type? maybe the switchboard can rebroadcast + // messages that have a type that includes the outer type if that's not + // possible + // in short, switchboard should be the package that includes outer l.Register(id.ZeroID, - format.Rekey, int32(cmixproto.Type_NO_TYPE), + int32(cmixproto.Type_NO_TYPE), &rekeyList) l.Register(id.ZeroID, - format.None, int32(cmixproto.Type_REKEY_CONFIRM), + int32(cmixproto.Type_REKEY_CONFIRM), &rekeyConfirmList) } @@ -236,8 +241,8 @@ func rekeyProcess(rt rekeyType, partner *id.User, data []byte) error { // Directly send raw publicKey bytes, without any message type // This ensures that the publicKey fits in a single message, which // is sent with E2E encryption using a send Rekey, and without padding - return messaging.SendMessageNoPartition(session, partner, format.E2E, - pubKey.GetKey().LeftpadBytes(uint64(format.TOTAL_LEN))) + return messaging.SendMessageNoPartition(session, partner, parse.E2E, + pubKey.GetKey().LeftpadBytes(uint64(format.TotalLen))) case Rekey: // Send rekey confirm message with hash of the baseKey h, _ := hash.NewCMixHash() @@ -246,7 +251,7 @@ func rekeyProcess(rt rekeyType, partner *id.User, data []byte) error { MessageType: int32(cmixproto.Type_REKEY_CONFIRM), Body: h.Sum(nil), }) - return messaging.SendMessage(session, partner, format.None, msg) + return messaging.SendMessage(session, partner, parse.None, msg) } return nil } diff --git a/rekey/rekey_test.go b/rekey/rekey_test.go index 6dacf9d2c..8ff44adbe 100644 --- a/rekey/rekey_test.go +++ b/rekey/rekey_test.go @@ -12,7 +12,6 @@ import ( "gitlab.com/elixxir/crypto/e2e" "gitlab.com/elixxir/crypto/hash" "gitlab.com/elixxir/crypto/signature" - "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/id" "os" "testing" @@ -28,7 +27,7 @@ type dummyMessaging struct { // SendMessage to the server func (d *dummyMessaging) SendMessage(sess user.Session, recipientID *id.User, - cryptoType format.CryptoType, + cryptoType parse.CryptoType, message []byte) error { d.listener <- message return nil @@ -37,7 +36,7 @@ func (d *dummyMessaging) SendMessage(sess user.Session, // SendMessage without partitions to the server func (d *dummyMessaging) SendMessageNoPartition(sess user.Session, recipientID *id.User, - cryptoType format.CryptoType, + cryptoType parse.CryptoType, message []byte) error { d.listener <- message return nil @@ -125,7 +124,7 @@ func TestRekeyTrigger(t *testing.T) { MessageType: int32(cmixproto.Type_REKEY_TRIGGER), Body: partnerPubKey.Bytes(), }, - CryptoType: format.None, + InferredType: parse.None, Receiver: partnerID, } session.GetSwitchboard().Speak(msg) @@ -156,7 +155,7 @@ func TestRekeyTrigger(t *testing.T) { MessageType: int32(cmixproto.Type_REKEY_TRIGGER), Body: partnerPubKey.Bytes(), }, - CryptoType: format.None, + InferredType: parse.None, Receiver: partnerID, } session.GetSwitchboard().Speak(msg) @@ -179,7 +178,7 @@ func TestRekeyConfirm(t *testing.T) { MessageType: int32(cmixproto.Type_REKEY_CONFIRM), Body: baseKey.Bytes(), }, - CryptoType: format.None, + InferredType: parse.None, Receiver: session.GetCurrentUser().User, } session.GetSwitchboard().Speak(msg) @@ -198,7 +197,7 @@ func TestRekeyConfirm(t *testing.T) { MessageType: int32(cmixproto.Type_REKEY_CONFIRM), Body: h.Sum(nil), }, - CryptoType: format.None, + InferredType: parse.None, Receiver: session.GetCurrentUser().User, } session.GetSwitchboard().Speak(msg) @@ -224,7 +223,7 @@ func TestRekeyConfirm(t *testing.T) { MessageType: int32(cmixproto.Type_REKEY_CONFIRM), Body: h.Sum(nil), }, - CryptoType: format.None, + InferredType: parse.None, Receiver: session.GetCurrentUser().User, } session.GetSwitchboard().Speak(msg) @@ -255,7 +254,7 @@ func TestRekey(t *testing.T) { MessageType: int32(cmixproto.Type_NO_TYPE), Body: partnerPubKey.GetKey().Bytes(), }, - CryptoType: format.Rekey, + InferredType: parse.Rekey, Receiver: session.GetCurrentUser().User, } session.GetSwitchboard().Speak(msg) @@ -309,7 +308,7 @@ func TestRekey_Errors(t *testing.T) { MessageType: int32(cmixproto.Type_REKEY_TRIGGER), Body: partnerPubKey.Bytes(), }, - CryptoType: format.None, + InferredType: parse.None, Receiver: partnerID, } session.GetSwitchboard().Speak(msg) @@ -326,7 +325,7 @@ func TestRekey_Errors(t *testing.T) { MessageType: int32(cmixproto.Type_NO_TYPE), Body: []byte{}, }, - CryptoType: format.Rekey, + InferredType: parse.Rekey, Receiver: session.GetCurrentUser().User, } session.GetSwitchboard().Speak(msg) -- GitLab