From c084319478a98631f2a21feb2cc6cfc2ad0785b4 Mon Sep 17 00:00:00 2001
From: jbhusson <jonah@elixxir.io>
Date: Wed, 20 Jul 2022 11:22:51 -0400
Subject: [PATCH] Asymmetric should only encrypt one payload & fill rest with
 random bytes

---
 broadcast/asymmetric.go      | 27 ++++++++-------------------
 broadcast/broadcastClient.go |  2 +-
 broadcast/processor.go       | 19 ++++---------------
 3 files changed, 13 insertions(+), 35 deletions(-)

diff --git a/broadcast/asymmetric.go b/broadcast/asymmetric.go
index 9f913b8c2..ed731ff21 100644
--- a/broadcast/asymmetric.go
+++ b/broadcast/asymmetric.go
@@ -11,7 +11,6 @@ import (
 	"github.com/pkg/errors"
 	"gitlab.com/elixxir/client/cmix"
 	"gitlab.com/elixxir/client/cmix/message"
-	"gitlab.com/elixxir/primitives/format"
 	"gitlab.com/xx_network/crypto/multicastRSA"
 	"gitlab.com/xx_network/primitives/id"
 	"gitlab.com/xx_network/primitives/id/ephemeral"
@@ -37,26 +36,14 @@ func (bc *broadcastClient) BroadcastAsymmetric(pk multicastRSA.PrivateKey, paylo
 		return 0, ephemeral.Id{}, errors.New(errNetworkHealth)
 	}
 
-	if len(payload) != bc.maxAsymmetricPayload() {
+	if len(payload) != bc.MaxAsymmetricPayloadSize() {
 		return 0, ephemeral.Id{},
 			errors.Errorf(errPayloadSize, len(payload), bc.maxAsymmetricPayload())
 	}
 
-	numParts := bc.maxParts()
-	size := bc.channel.MaxAsymmetricPayloadSize()
-	var mac []byte
-	var fp format.Fingerprint
-	var sequential []byte
-	for i := 0; i < numParts; i++ {
-		// Encrypt payload to send using asymmetric channel
-		var encryptedPayload []byte
-		var err error
-		encryptedPayload, mac, fp, err = bc.channel.EncryptAsymmetric(payload[:size], pk, bc.rng.GetStream())
-		if err != nil {
-			return 0, ephemeral.Id{}, errors.WithMessage(err, "Failed to encrypt asymmetric broadcast message")
-		}
-		payload = payload[size:]
-		sequential = append(sequential, encryptedPayload...)
+	encryptedPayload, mac, fp, err := bc.channel.EncryptAsymmetric(payload, pk, bc.rng.GetStream())
+	if err != nil {
+		return 0, ephemeral.Id{}, errors.WithMessage(err, "Failed to encrypt asymmetric broadcast message")
 	}
 
 	// Create service object to send message
@@ -69,10 +56,12 @@ func (bc *broadcastClient) BroadcastAsymmetric(pk multicastRSA.PrivateKey, paylo
 		cMixParams.DebugTag = asymmCMixSendTag
 	}
 
-	sizedPayload, err := NewSizedBroadcast(bc.net.GetMaxMessageLength(), sequential)
+	sizedPayload := make([]byte, bc.net.GetMaxMessageLength())
+	_, err = bc.rng.GetStream().Read(sizedPayload)
 	if err != nil {
-		return id.Round(0), ephemeral.Id{}, err
+		return 0, ephemeral.Id{}, errors.WithMessage(err, "Failed to add random data to sized broadcast")
 	}
+	copy(sizedPayload[:len(encryptedPayload)], encryptedPayload)
 
 	return bc.net.Send(
 		bc.channel.ReceptionID, fp, service, sizedPayload, mac, cMixParams)
diff --git a/broadcast/broadcastClient.go b/broadcast/broadcastClient.go
index d38975b53..c80f4a50e 100644
--- a/broadcast/broadcastClient.go
+++ b/broadcast/broadcastClient.go
@@ -102,5 +102,5 @@ func (bc *broadcastClient) MaxPayloadSize() int {
 }
 
 func (bc *broadcastClient) MaxAsymmetricPayloadSize() int {
-	return bc.maxAsymmetricPayload()
+	return bc.channel.MaxAsymmetricPayloadSize()
 }
diff --git a/broadcast/processor.go b/broadcast/processor.go
index ec9ab810d..b747eac00 100644
--- a/broadcast/processor.go
+++ b/broadcast/processor.go
@@ -36,24 +36,13 @@ func (p *processor) Process(msg format.Message,
 	switch p.method {
 	case Asymmetric:
 		// We use sized broadcast to fill any remaining bytes in the cmix payload, decode it here
-		unsizedPayload, err := DecodeSizedBroadcast(msg.GetContents())
+		encPartSize := p.c.RsaPubKey.Size() // Size of each chunk returned by multicast RSA encryption
+		encodedMessage := msg.GetContents()[:encPartSize]
+		payload, err = p.c.DecryptAsymmetric(encodedMessage)
 		if err != nil {
-			jww.ERROR.Printf("Failed to decode sized broadcast: %+v", err)
+			jww.ERROR.Printf(errDecrypt, p.c.ReceptionID, p.c.Name, err)
 			return
 		}
-		encPartSize := p.c.RsaPubKey.Size()           // Size of each chunk returned by multicast RSA encryption
-		numParts := len(unsizedPayload) / encPartSize // Number of chunks in the payload
-		// Iterate through & decrypt each chunk, appending to aggregate payload
-		for i := 0; i < numParts; i++ {
-			var decrypted []byte
-			decrypted, err = p.c.DecryptAsymmetric(unsizedPayload[:encPartSize])
-			if err != nil {
-				jww.ERROR.Printf(errDecrypt, p.c.ReceptionID, p.c.Name, err)
-				return
-			}
-			unsizedPayload = unsizedPayload[encPartSize:]
-			payload = append(payload, decrypted...)
-		}
 
 	case Symmetric:
 		payload, err = p.c.DecryptSymmetric(msg.GetContents(), msg.GetMac(), msg.GetKeyFP())
-- 
GitLab