diff --git a/broadcast/asymmetricClient.go b/broadcast/asymmetricClient.go index d1e67eb9f61e434de1017c8c9b418c9dbe04eb30..0c65ceefa830db776415427b056d255762f4dd2b 100644 --- a/broadcast/asymmetricClient.go +++ b/broadcast/asymmetricClient.go @@ -44,11 +44,16 @@ func (bc *broadcastClient) BroadcastAsymmetric(pk multicastRSA.PrivateKey, paylo cMixParams.DebugTag = asymmCMixSendTag } + sizedPayload, err := NewSizedBroadcast(bc.net.GetMaxMessageLength(), encryptedPayload) + if err != nil { + return id.Round(0), ephemeral.Id{}, err + } + return bc.net.Send( - bc.channel.ReceptionID, fp, service, encryptedPayload, mac, cMixParams) + bc.channel.ReceptionID, fp, service, sizedPayload, mac, cMixParams) } // MaxAsymmetricPayloadSize returns the maximum size for an asymmetric broadcast payload. func (bc *broadcastClient) MaxAsymmetricPayloadSize() int { - return bc.net.GetMaxMessageLength() // TODO: this is wrong, use math from xx_network/crypto layer + return bc.channel.MaxAsymmetricPayloadSize() } diff --git a/broadcast/asymmetricClient_test.go b/broadcast/asymmetricClient_test.go index b26e86f7cf143e7711964580aa9d891cd32de20f..356030c7595dea1d8281da4d5b4827fc2414a9e3 100644 --- a/broadcast/asymmetricClient_test.go +++ b/broadcast/asymmetricClient_test.go @@ -19,7 +19,7 @@ import ( ) // Tests that symmetricClient adheres to the Symmetric interface. -var _ Asymmetric = (*asymmetricClient)(nil) +var _ Channel = (*broadcastClient)(nil) // Tests that symmetricClient adheres to the Symmetric interface. var _ Client = (cmix.Client)(nil) @@ -27,21 +27,22 @@ var _ Client = (cmix.Client)(nil) func Test_asymmetricClient_Smoke(t *testing.T) { cMixHandler := newMockCmixHandler() rngGen := fastRNG.NewStreamGenerator(1000, 10, csprng.NewSystemRNG) - pk, err := rsa.GenerateKey(rngGen.GetStream(), 8192) + pk, err := rsa.GenerateKey(rngGen.GetStream(), 4096) if err != nil { t.Fatalf("Failed to generate priv key: %+v", err) } - channel := crypto.Asymmetric{ + channel := crypto.Channel{ ReceptionID: id.NewIdFromString("ReceptionID", id.User, t), Name: "MyChannel", Description: "This is my channel about stuff.", Salt: cMixCrypto.NewSalt(csprng.NewSystemRNG(), 32), RsaPubKey: pk.GetPublic(), } + payloadSize := channel.MaxAsymmetricPayloadSize() const n = 5 cbChans := make([]chan []byte, n) - clients := make([]Asymmetric, n) + clients := make([]Channel, n) for i := range clients { cbChan := make(chan []byte, 10) cb := func(payload []byte, _ receptionID.EphemeralIdentity, @@ -49,7 +50,10 @@ func Test_asymmetricClient_Smoke(t *testing.T) { cbChan <- payload } - s := NewAsymmetricClient(channel, cb, newMockCmix(cMixHandler), rngGen) + s, err := NewBroadcastChannel(channel, cb, newMockCmix(cMixHandler), rngGen, Param{Method: Asymmetric}) + if err != nil { + t.Errorf("Failed to create broadcast channel: %+v", err) + } cbChans[i] = cbChan clients[i] = s @@ -63,7 +67,7 @@ func Test_asymmetricClient_Smoke(t *testing.T) { // Send broadcast from each client for i := range clients { - payload := make([]byte, newMockCmix(cMixHandler).GetMaxMessageLength()) + payload := make([]byte, payloadSize) copy(payload, fmt.Sprintf("Hello from client %d of %d.", i, len(clients))) @@ -88,7 +92,7 @@ func Test_asymmetricClient_Smoke(t *testing.T) { } // Broadcast payload - _, _, err := clients[i].Broadcast(pk, payload, cmix.GetDefaultCMIXParams()) + _, _, err := clients[i].BroadcastAsymmetric(pk, payload, cmix.GetDefaultCMIXParams()) if err != nil { t.Errorf("Client %d failed to send broadcast: %+v", i, err) } @@ -102,7 +106,7 @@ func Test_asymmetricClient_Smoke(t *testing.T) { clients[i].Stop() } - payload := make([]byte, newMockCmix(cMixHandler).GetMaxMessageLength()) + payload := make([]byte, payloadSize) copy(payload, "This message should not get through.") // Start waiting on channels and error if anything is received @@ -120,7 +124,7 @@ func Test_asymmetricClient_Smoke(t *testing.T) { } // Broadcast payload - _, _, err = clients[0].Broadcast(pk, payload, cmix.GetDefaultCMIXParams()) + _, _, err = clients[0].BroadcastAsymmetric(pk, payload, cmix.GetDefaultCMIXParams()) if err != nil { t.Errorf("Client 0 failed to send broadcast: %+v", err) } diff --git a/broadcast/processor.go b/broadcast/processor.go index e715887eb3da0ff2f6a0a32993791e79dfaa766b..905fc470a98a5f640920ce6099f2fea6801355c0 100644 --- a/broadcast/processor.go +++ b/broadcast/processor.go @@ -34,7 +34,12 @@ func (p *processor) Process(msg format.Message, var err error switch p.method { case Asymmetric: - payload, err = p.c.DecryptAsymmetric(msg.GetContents()) + unsizedPayload, err := DecodeSizedBroadcast(msg.GetContents()) + if err != nil { + jww.ERROR.Printf("Failed to decode sized broadcast: %+v", err) + return + } + payload, err = p.c.DecryptAsymmetric(unsizedPayload) if err != nil { jww.ERROR.Printf(errDecrypt, p.c.ReceptionID, p.c.Name, err) return diff --git a/broadcast/processor_test.go b/broadcast/processor_test.go index c7b56e05c92a57d47a2f9c8640ff09668db7d470..9e48f372e92663eea061a8a7d72e0fbe13d58eee 100644 --- a/broadcast/processor_test.go +++ b/broadcast/processor_test.go @@ -29,7 +29,7 @@ func Test_processor_Process(t *testing.T) { if err != nil { t.Errorf("Failed to generate RSA key: %+v", err) } - s := &crypto.Symmetric{ + s := &crypto.Channel{ ReceptionID: id.NewIdFromString("channel", id.User, t), Name: "MyChannel", Description: "This is my channel that I channel stuff on.", @@ -42,15 +42,16 @@ func Test_processor_Process(t *testing.T) { cbChan <- payload } - p := &symmetricProcessor{ - s: s, - cb: cb, + p := &processor{ + c: s, + cb: cb, + method: Symmetric, } msg := format.NewMessage(4092) payload := make([]byte, msg.ContentsSize()) _, _ = rng.Read(payload) - encryptedPayload, mac, fp := p.s.Encrypt(payload, rng) + encryptedPayload, mac, fp := p.c.EncryptSymmetric(payload, rng) msg.SetContents(encryptedPayload) msg.SetMac(mac) msg.SetKeyFP(fp) diff --git a/broadcast/symmetricClient_test.go b/broadcast/symmetricClient_test.go index 4b6f60ac89fb1b7a06e5ce1a3fa7398720848250..d26610c54635791fbcb117802fa5de578dc7970e 100644 --- a/broadcast/symmetricClient_test.go +++ b/broadcast/symmetricClient_test.go @@ -25,7 +25,7 @@ import ( ) // Tests that symmetricClient adheres to the Symmetric interface. -var _ Symmetric = (*symmetricClient)(nil) +var _ Channel = (*broadcastClient)(nil) // Tests that symmetricClient adheres to the Symmetric interface. var _ Client = (cmix.Client)(nil) @@ -36,7 +36,7 @@ func Test_symmetricClient_Smoke(t *testing.T) { // Initialise objects used by all clients cMixHandler := newMockCmixHandler() rngGen := fastRNG.NewStreamGenerator(1000, 10, csprng.NewSystemRNG) - channel := crypto.Symmetric{ + channel := crypto.Channel{ ReceptionID: id.NewIdFromString("ReceptionID", id.User, t), Name: "MyChannel", Description: "This is my channel about stuff.", @@ -47,7 +47,7 @@ func Test_symmetricClient_Smoke(t *testing.T) { // Set up callbacks, callback channels, and the symmetric clients const n = 5 cbChans := make([]chan []byte, n) - clients := make([]Symmetric, n) + clients := make([]Channel, n) for i := range clients { cbChan := make(chan []byte, 10) cb := func(payload []byte, _ receptionID.EphemeralIdentity, @@ -55,8 +55,10 @@ func Test_symmetricClient_Smoke(t *testing.T) { cbChan <- payload } - s := NewSymmetricClient(channel, cb, newMockCmix(cMixHandler), rngGen) - + s, err := NewBroadcastChannel(channel, cb, newMockCmix(cMixHandler), rngGen, Param{Method: Symmetric}) + if err != nil { + t.Errorf("Failed to create broadcast channel: %+v", err) + } cbChans[i] = cbChan clients[i] = s diff --git a/go.mod b/go.mod index 3d5655db5d01e0006b8de82f205a5e8380f4ca70..45cbd29359501276d3da18b7745794d21cdaaa8c 100644 --- a/go.mod +++ b/go.mod @@ -13,11 +13,11 @@ require ( github.com/spf13/viper v1.7.1 gitlab.com/elixxir/bloomfilter v0.0.0-20211222005329-7d931ceead6f gitlab.com/elixxir/comms v0.0.4-0.20220323190139-9ed75f3a8b2c - gitlab.com/elixxir/crypto v0.0.7-0.20220513133141-46faee2d8fca + gitlab.com/elixxir/crypto v0.0.7-0.20220516144816-71049ce09e4b gitlab.com/elixxir/ekv v0.1.7 gitlab.com/elixxir/primitives v0.0.3-0.20220330212736-cce83b5f948f gitlab.com/xx_network/comms v0.0.4-0.20220315161313-76acb14429ac - gitlab.com/xx_network/crypto v0.0.5-0.20220502201458-dabab1ef2982 + gitlab.com/xx_network/crypto v0.0.5-0.20220516143655-14f9153096ce gitlab.com/xx_network/primitives v0.0.4-0.20220324193139-b292d1ae6e7e go.uber.org/ratelimit v0.2.0 golang.org/x/crypto v0.0.0-20220128200615-198e4374d7ed diff --git a/go.sum b/go.sum index fa944cd47a49236f96fe5c009e233166ae38951b..4a738e6d8a959874976a8053d6ffe015b5e1216d 100644 --- a/go.sum +++ b/go.sum @@ -323,6 +323,8 @@ gitlab.com/elixxir/crypto v0.0.7-0.20220510191648-70e5e956d3d5 h1:uzzFrmqx0CnqQ7 gitlab.com/elixxir/crypto v0.0.7-0.20220510191648-70e5e956d3d5/go.mod h1:cJF80ad9YCR+UcOlZNzfDVBAQqGEEhhs3y5taMEvXaE= gitlab.com/elixxir/crypto v0.0.7-0.20220513133141-46faee2d8fca h1:0EINaZCe/0dPbQD2msOj8fwKpZ5lnZ57Y6zUZm85rEo= gitlab.com/elixxir/crypto v0.0.7-0.20220513133141-46faee2d8fca/go.mod h1:cJF80ad9YCR+UcOlZNzfDVBAQqGEEhhs3y5taMEvXaE= +gitlab.com/elixxir/crypto v0.0.7-0.20220516144816-71049ce09e4b h1:HZlcbi+rTr1MACtLoNUJDV2iTvy7JtkRjWWaPF45bcM= +gitlab.com/elixxir/crypto v0.0.7-0.20220516144816-71049ce09e4b/go.mod h1:L3duHa+GppnsY8x22/ixrEdQSM7WXa1ORWPS90HZ/JI= gitlab.com/elixxir/ekv v0.1.6 h1:M2hUSNhH/ChxDd+s8xBqSEKgoPtmE6hOEBqQ73KbN6A= gitlab.com/elixxir/ekv v0.1.6/go.mod h1:e6WPUt97taFZe5PFLPb1Dupk7tqmDCTQu1kkstqJvw4= gitlab.com/elixxir/ekv v0.1.7 h1:OW2z+N4QCqqMFzouAwFTWWMKz0Y/PDhyYReN7gQ5NiQ= @@ -353,6 +355,8 @@ gitlab.com/xx_network/crypto v0.0.5-0.20220317171841-084640957d71 h1:N2+Jja4xNg6 gitlab.com/xx_network/crypto v0.0.5-0.20220317171841-084640957d71/go.mod h1:/SJf+R75E+QepdTLh0H1/udsovxx2Q5ru34q1v0umKk= gitlab.com/xx_network/crypto v0.0.5-0.20220502201458-dabab1ef2982 h1:iUMwO/lIeOEmqNTMPtr9d7H6Y0cGe4DaHhd+ISCts6E= gitlab.com/xx_network/crypto v0.0.5-0.20220502201458-dabab1ef2982/go.mod h1:/SJf+R75E+QepdTLh0H1/udsovxx2Q5ru34q1v0umKk= +gitlab.com/xx_network/crypto v0.0.5-0.20220516143655-14f9153096ce h1:zkPtJLJUpoWDYD4z30nmKU8OvnIOnBvSu7e6q6WhY5A= +gitlab.com/xx_network/crypto v0.0.5-0.20220516143655-14f9153096ce/go.mod h1:/SJf+R75E+QepdTLh0H1/udsovxx2Q5ru34q1v0umKk= gitlab.com/xx_network/primitives v0.0.0-20200803231956-9b192c57ea7c/go.mod h1:wtdCMr7DPePz9qwctNoAUzZtbOSHSedcK++3Df3psjA= gitlab.com/xx_network/primitives v0.0.0-20200804183002-f99f7a7284da/go.mod h1:OK9xevzWCaPO7b1wiluVJGk7R5ZsuC7pHY5hteZFQug= gitlab.com/xx_network/primitives v0.0.2/go.mod h1:cs0QlFpdMDI6lAo61lDRH2JZz+3aVkHy+QogOB6F/qc=