From a419234e6fdf8a89fa4d64174908166aa6d8a036 Mon Sep 17 00:00:00 2001
From: hnnajh <hnnajh@amazon.com>
Date: Thu, 9 Nov 2023 16:28:53 -0800
Subject: [PATCH 01/10] Update buffer allocation strategy and maximum buffer
 size when using TCP as transport

---
 src/system/SystemConfig.h         | 10 ++++++++++
 src/transport/raw/MessageHeader.h | 29 ++++++++++++++++++++---------
 2 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/src/system/SystemConfig.h b/src/system/SystemConfig.h
index 15e6abdb5d4e83..2d0b0b6c3cf43e 100644
--- a/src/system/SystemConfig.h
+++ b/src/system/SystemConfig.h
@@ -358,6 +358,16 @@
 #define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE 15
 #endif /* CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE */
 
+/**
+ *  @def CHIP_CONFIG_TCP_SUPPORT
+ *
+ *  @brief
+ *      Indicates whether the current node supports TCP
+ */
+#ifndef CHIP_CONFIG_TCP_SUPPORT
+#define CHIP_CONFIG_TCP_SUPPORT 0
+#endif /* CHIP_CONFIG_TCP_SUPPORT */
+
 /**
  *  @def CHIP_SYSTEM_CONFIG_PACKETBUFFER_LWIP_PBUF_RAM
  *
diff --git a/src/transport/raw/MessageHeader.h b/src/transport/raw/MessageHeader.h
index 7c637fa782d957..b090e733869f0f 100644
--- a/src/transport/raw/MessageHeader.h
+++ b/src/transport/raw/MessageHeader.h
@@ -42,38 +42,49 @@
 
 namespace chip {
 
+static constexpr size_t kMaxTagLen = 16;
+
 namespace detail {
 // Figure out the max size of a packet we can allocate, including all headers.
-static constexpr size_t kMaxIPPacketSizeBytes       = 1280;
+static constexpr size_t kMaxUdpIPPacketSizeBytes       = 1280;
+static constexpr size_t kMaxTcpIPPacketSizeBytes       = 12800;
 static constexpr size_t kMaxUDPAndIPHeaderSizeBytes = 48;
 
-static_assert(kMaxIPPacketSizeBytes >= kMaxUDPAndIPHeaderSizeBytes + CHIP_SYSTEM_HEADER_RESERVE_SIZE,
+static_assert(kMaxUdpIPPacketSizeBytes >= kMaxUDPAndIPHeaderSizeBytes + CHIP_SYSTEM_HEADER_RESERVE_SIZE,
               "Matter headers and IP headers must fit in an MTU.");
 
 // Max space we have for our Application Payload and MIC, per spec.
 static constexpr size_t kMaxPerSpecApplicationPayloadAndMICSizeBytes =
-    kMaxIPPacketSizeBytes - kMaxUDPAndIPHeaderSizeBytes - CHIP_SYSTEM_HEADER_RESERVE_SIZE;
+    kMaxUdpIPPacketSizeBytes - kMaxUDPAndIPHeaderSizeBytes - CHIP_SYSTEM_HEADER_RESERVE_SIZE;
 
 // Max space we have for our Application Payload and MIC in our actual packet
 // buffers.  This is the size _excluding_ the header reserve.
 static constexpr size_t kMaxPacketBufferApplicationPayloadAndMICSizeBytes = System::PacketBuffer::kMaxSize;
 
-static constexpr size_t kMaxApplicationPayloadAndMICSizeBytes =
+static constexpr size_t kMaxApplicationUdpPayloadAndMICSizeBytes =
     min(kMaxPerSpecApplicationPayloadAndMICSizeBytes, kMaxPacketBufferApplicationPayloadAndMICSizeBytes);
 
-} // namespace detail
+static constexpr size_t kMaxApplicationTcpPayloadAndMICSizeBytes =
+    kMaxTcpIPPacketSizeBytes - CHIP_SYSTEM_HEADER_RESERVE_SIZE;
 
-static constexpr size_t kMaxTagLen = 16;
-
-static_assert(detail::kMaxApplicationPayloadAndMICSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message");
+static_assert(detail::kMaxApplicationUdpPayloadAndMICSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message");
+static_assert(detail::kMaxApplicationTcpPayloadAndMICSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message");
 
 // This is somewhat of an under-estimate, because in practice any time we have a
 // tag we will not have source/destination node IDs, but above we are including
 // those in the header sizes.
-static constexpr size_t kMaxAppMessageLen = detail::kMaxApplicationPayloadAndMICSizeBytes - kMaxTagLen;
+static constexpr size_t kMaxUdpAppMessageLen = kMaxApplicationUdpPayloadAndMICSizeBytes - kMaxTagLen;
+static constexpr size_t kMaxTcpAppMessageLen = kMaxApplicationTcpPayloadAndMICSizeBytes - kMaxTagLen;
+} // namespace detail
 
 static constexpr uint16_t kMsgUnicastSessionIdUnsecured = 0x0000;
 
+#if CHIP_CONFIG_TCP_SUPPORT
+static constexpr size_t kMaxAppMessageLen = detail::kMaxTcpAppMessageLen;
+#else
+static constexpr size_t kMaxAppMessageLen = detail::kMaxUdpAppMessageLen;
+#endif
+
 typedef int PacketHeaderFlags;
 
 namespace Header {

From 0bad67451848f7fe1af3b108d4a7eb87ba601ce8 Mon Sep 17 00:00:00 2001
From: hnnajh <hnnajh@amazon.com>
Date: Fri, 10 Nov 2023 11:35:04 -0800
Subject: [PATCH 02/10] Adding TCP server and client config flag

---
 src/system/SystemConfig.h         | 20 +++++++++++++++-----
 src/transport/raw/MessageHeader.h |  2 +-
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/system/SystemConfig.h b/src/system/SystemConfig.h
index 2d0b0b6c3cf43e..49c0935312bc23 100644
--- a/src/system/SystemConfig.h
+++ b/src/system/SystemConfig.h
@@ -359,14 +359,24 @@
 #endif /* CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE */
 
 /**
- *  @def CHIP_CONFIG_TCP_SUPPORT
+ *  @def CHIP_CONFIG_TCP_SUPPORT_CLIENT
  *
  *  @brief
- *      Indicates whether the current node supports TCP
+ *      Indicates whether the current node supports TCP Client
  */
-#ifndef CHIP_CONFIG_TCP_SUPPORT
-#define CHIP_CONFIG_TCP_SUPPORT 0
-#endif /* CHIP_CONFIG_TCP_SUPPORT */
+#ifndef CHIP_CONFIG_TCP_SUPPORT_CLIENT
+#define CHIP_CONFIG_TCP_SUPPORT_CLIENT 0
+#endif /* CHIP_CONFIG_TCP_SUPPORT_CLIENT */
+
+/**
+ *  @def CHIP_CONFIG_TCP_SUPPORT_SERVER
+ *
+ *  @brief
+ *      Indicates whether the current node supports TCP Server
+ */
+#ifndef CHIP_CONFIG_TCP_SUPPORT_SERVER
+#define CHIP_CONFIG_TCP_SUPPORT_SERVER 0
+#endif /* CHIP_CONFIG_TCP_SUPPORT_SERVER */
 
 /**
  *  @def CHIP_SYSTEM_CONFIG_PACKETBUFFER_LWIP_PBUF_RAM
diff --git a/src/transport/raw/MessageHeader.h b/src/transport/raw/MessageHeader.h
index b090e733869f0f..267faa110cebb3 100644
--- a/src/transport/raw/MessageHeader.h
+++ b/src/transport/raw/MessageHeader.h
@@ -79,7 +79,7 @@ static constexpr size_t kMaxTcpAppMessageLen = kMaxApplicationTcpPayloadAndMICSi
 
 static constexpr uint16_t kMsgUnicastSessionIdUnsecured = 0x0000;
 
-#if CHIP_CONFIG_TCP_SUPPORT
+#if CHIP_CONFIG_TCP_SUPPORT_SERVER || CHIP_CONFIG_TCP_SUPPORT_CLIENT
 static constexpr size_t kMaxAppMessageLen = detail::kMaxTcpAppMessageLen;
 #else
 static constexpr size_t kMaxAppMessageLen = detail::kMaxUdpAppMessageLen;

From 72421bdea3525651b28d72af3355ed08d8b791fd Mon Sep 17 00:00:00 2001
From: hnnajh <hnnajh@amazon.com>
Date: Tue, 28 Nov 2023 17:16:29 -0800
Subject: [PATCH 03/10] Fix the size limits to be per session instead of one
 for all sessions

---
 src/transport/SecureMessageCodec.cpp |  4 ++--
 src/transport/SecureMessageCodec.h   |  3 ++-
 src/transport/SessionManager.cpp     | 13 +++++++++++-
 src/transport/raw/MessageHeader.h    | 31 ++++++++++------------------
 4 files changed, 27 insertions(+), 24 deletions(-)

diff --git a/src/transport/SecureMessageCodec.cpp b/src/transport/SecureMessageCodec.cpp
index 9c08d535ff57a3..ecf965e063f4db 100644
--- a/src/transport/SecureMessageCodec.cpp
+++ b/src/transport/SecureMessageCodec.cpp
@@ -37,11 +37,11 @@ using System::PacketBufferHandle;
 namespace SecureMessageCodec {
 
 CHIP_ERROR Encrypt(const CryptoContext & context, CryptoContext::ConstNonceView nonce, PayloadHeader & payloadHeader,
-                   PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf)
+                   PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf, size_t inputMaxLength)
 {
     VerifyOrReturnError(!msgBuf.IsNull(), CHIP_ERROR_INVALID_ARGUMENT);
     VerifyOrReturnError(!msgBuf->HasChainedBuffer(), CHIP_ERROR_INVALID_MESSAGE_LENGTH);
-    VerifyOrReturnError(msgBuf->TotalLength() <= kMaxAppMessageLen, CHIP_ERROR_MESSAGE_TOO_LONG);
+    VerifyOrReturnError(msgBuf->TotalLength() <= inputMaxLength, CHIP_ERROR_MESSAGE_TOO_LONG);
 
     static_assert(std::is_same<decltype(msgBuf->TotalLength()), uint16_t>::value,
                   "Addition to generate payloadLength might overflow");
diff --git a/src/transport/SecureMessageCodec.h b/src/transport/SecureMessageCodec.h
index f074e792b59d21..6b82d52d0f3d5b 100644
--- a/src/transport/SecureMessageCodec.h
+++ b/src/transport/SecureMessageCodec.h
@@ -47,10 +47,11 @@ namespace SecureMessageCodec {
  * @param msgBuf        The message buffer that contains the unencrypted message. If
  *                      the operation is successful, this buffer will be mutated to contain
  *                      the encrypted message.
+ * @param inputMaxLength Max size for input
  * @return A CHIP_ERROR value consistent with the result of the encryption operation
  */
 CHIP_ERROR Encrypt(const CryptoContext & context, CryptoContext::ConstNonceView nonce, PayloadHeader & payloadHeader,
-                   PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf);
+                   PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf, size_t inputMaxLength);
 
 /**
  * @brief
diff --git a/src/transport/SessionManager.cpp b/src/transport/SessionManager.cpp
index 085fda948629c7..288d42e167f2f8 100644
--- a/src/transport/SessionManager.cpp
+++ b/src/transport/SessionManager.cpp
@@ -201,7 +201,18 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
         packetHeader.SetSessionId(keyContext->GetKeyHash());
         CryptoContext::NonceStorage nonce;
         CryptoContext::BuildNonce(nonce, packetHeader.GetSecurityFlags(), packetHeader.GetMessageCounter(), sourceNodeId);
-        CHIP_ERROR err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message);
+        CHIP_ERROR err = CHIP_NO_ERROR;
+        SecureSession * session = sessionHandle->AsSecureSession();
+        if (session == nullptr)
+        {
+            return CHIP_ERROR_NOT_CONNECTED;
+        }
+        if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp) {
+            // support large payloads
+            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message, kLargePayloadMaxSizeBytes);
+        } else {
+            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message, kMaxAppMessageLen);
+        }
         keyContext->Release();
         ReturnErrorOnFailure(err);
 
diff --git a/src/transport/raw/MessageHeader.h b/src/transport/raw/MessageHeader.h
index 267faa110cebb3..8b4165b6bc78ac 100644
--- a/src/transport/raw/MessageHeader.h
+++ b/src/transport/raw/MessageHeader.h
@@ -42,49 +42,40 @@
 
 namespace chip {
 
-static constexpr size_t kMaxTagLen = 16;
-
 namespace detail {
 // Figure out the max size of a packet we can allocate, including all headers.
-static constexpr size_t kMaxUdpIPPacketSizeBytes       = 1280;
-static constexpr size_t kMaxTcpIPPacketSizeBytes       = 12800;
+static constexpr size_t kMaxIPPacketSizeBytes       = 1280;
 static constexpr size_t kMaxUDPAndIPHeaderSizeBytes = 48;
 
-static_assert(kMaxUdpIPPacketSizeBytes >= kMaxUDPAndIPHeaderSizeBytes + CHIP_SYSTEM_HEADER_RESERVE_SIZE,
+static_assert(kMaxIPPacketSizeBytes >= kMaxUDPAndIPHeaderSizeBytes + CHIP_SYSTEM_HEADER_RESERVE_SIZE,
               "Matter headers and IP headers must fit in an MTU.");
 
 // Max space we have for our Application Payload and MIC, per spec.
 static constexpr size_t kMaxPerSpecApplicationPayloadAndMICSizeBytes =
-    kMaxUdpIPPacketSizeBytes - kMaxUDPAndIPHeaderSizeBytes - CHIP_SYSTEM_HEADER_RESERVE_SIZE;
+    kMaxIPPacketSizeBytes - kMaxUDPAndIPHeaderSizeBytes - CHIP_SYSTEM_HEADER_RESERVE_SIZE;
 
 // Max space we have for our Application Payload and MIC in our actual packet
 // buffers.  This is the size _excluding_ the header reserve.
 static constexpr size_t kMaxPacketBufferApplicationPayloadAndMICSizeBytes = System::PacketBuffer::kMaxSize;
 
-static constexpr size_t kMaxApplicationUdpPayloadAndMICSizeBytes =
+static constexpr size_t kMaxApplicationPayloadAndMICSizeBytes =
     min(kMaxPerSpecApplicationPayloadAndMICSizeBytes, kMaxPacketBufferApplicationPayloadAndMICSizeBytes);
 
-static constexpr size_t kMaxApplicationTcpPayloadAndMICSizeBytes =
-    kMaxTcpIPPacketSizeBytes - CHIP_SYSTEM_HEADER_RESERVE_SIZE;
+} // namespace detail
 
-static_assert(detail::kMaxApplicationUdpPayloadAndMICSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message");
-static_assert(detail::kMaxApplicationTcpPayloadAndMICSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message");
+static constexpr size_t kMaxTagLen = 16;
+
+static_assert(detail::kMaxApplicationPayloadAndMICSizeBytes > kMaxTagLen, "Need to be able to fit our tag in a message");
 
 // This is somewhat of an under-estimate, because in practice any time we have a
 // tag we will not have source/destination node IDs, but above we are including
 // those in the header sizes.
-static constexpr size_t kMaxUdpAppMessageLen = kMaxApplicationUdpPayloadAndMICSizeBytes - kMaxTagLen;
-static constexpr size_t kMaxTcpAppMessageLen = kMaxApplicationTcpPayloadAndMICSizeBytes - kMaxTagLen;
-} // namespace detail
+static constexpr size_t kMaxAppMessageLen = detail::kMaxApplicationPayloadAndMICSizeBytes - kMaxTagLen;
+// large payload limit
+static constexpr size_t kLargePayloadMaxSizeBytes   = 128000;
 
 static constexpr uint16_t kMsgUnicastSessionIdUnsecured = 0x0000;
 
-#if CHIP_CONFIG_TCP_SUPPORT_SERVER || CHIP_CONFIG_TCP_SUPPORT_CLIENT
-static constexpr size_t kMaxAppMessageLen = detail::kMaxTcpAppMessageLen;
-#else
-static constexpr size_t kMaxAppMessageLen = detail::kMaxUdpAppMessageLen;
-#endif
-
 typedef int PacketHeaderFlags;
 
 namespace Header {

From 976cb7064b552f970dfd9db2fb178aef5c781350 Mon Sep 17 00:00:00 2001
From: hnnajh <hnnajh@amazon.com>
Date: Tue, 28 Nov 2023 18:05:10 -0800
Subject: [PATCH 04/10] remove TCP flags

---
 src/system/SystemConfig.h          | 20 --------------------
 src/transport/SecureMessageCodec.h |  2 +-
 2 files changed, 1 insertion(+), 21 deletions(-)

diff --git a/src/system/SystemConfig.h b/src/system/SystemConfig.h
index 49c0935312bc23..15e6abdb5d4e83 100644
--- a/src/system/SystemConfig.h
+++ b/src/system/SystemConfig.h
@@ -358,26 +358,6 @@
 #define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE 15
 #endif /* CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE */
 
-/**
- *  @def CHIP_CONFIG_TCP_SUPPORT_CLIENT
- *
- *  @brief
- *      Indicates whether the current node supports TCP Client
- */
-#ifndef CHIP_CONFIG_TCP_SUPPORT_CLIENT
-#define CHIP_CONFIG_TCP_SUPPORT_CLIENT 0
-#endif /* CHIP_CONFIG_TCP_SUPPORT_CLIENT */
-
-/**
- *  @def CHIP_CONFIG_TCP_SUPPORT_SERVER
- *
- *  @brief
- *      Indicates whether the current node supports TCP Server
- */
-#ifndef CHIP_CONFIG_TCP_SUPPORT_SERVER
-#define CHIP_CONFIG_TCP_SUPPORT_SERVER 0
-#endif /* CHIP_CONFIG_TCP_SUPPORT_SERVER */
-
 /**
  *  @def CHIP_SYSTEM_CONFIG_PACKETBUFFER_LWIP_PBUF_RAM
  *
diff --git a/src/transport/SecureMessageCodec.h b/src/transport/SecureMessageCodec.h
index 6b82d52d0f3d5b..315a29d711c85d 100644
--- a/src/transport/SecureMessageCodec.h
+++ b/src/transport/SecureMessageCodec.h
@@ -51,7 +51,7 @@ namespace SecureMessageCodec {
  * @return A CHIP_ERROR value consistent with the result of the encryption operation
  */
 CHIP_ERROR Encrypt(const CryptoContext & context, CryptoContext::ConstNonceView nonce, PayloadHeader & payloadHeader,
-                   PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf, size_t inputMaxLength);
+                   PacketHeader & packetHeader, System::PacketBufferHandle & msgBuf, size_t inputMaxLength = kMaxAppMessageLen);
 
 /**
  * @brief

From 94d1d8c38b6bbc6b47e6ed0989666a2f30600bbe Mon Sep 17 00:00:00 2001
From: hnnajh <hnnajh@amazon.com>
Date: Mon, 4 Dec 2023 11:34:40 -0800
Subject: [PATCH 05/10] address comments

---
 src/transport/SessionManager.cpp  | 7 ++-----
 src/transport/raw/MessageHeader.h | 2 +-
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/transport/SessionManager.cpp b/src/transport/SessionManager.cpp
index 288d42e167f2f8..43c477b879e292 100644
--- a/src/transport/SessionManager.cpp
+++ b/src/transport/SessionManager.cpp
@@ -203,13 +203,10 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
         CryptoContext::BuildNonce(nonce, packetHeader.GetSecurityFlags(), packetHeader.GetMessageCounter(), sourceNodeId);
         CHIP_ERROR err = CHIP_NO_ERROR;
         SecureSession * session = sessionHandle->AsSecureSession();
-        if (session == nullptr)
-        {
-            return CHIP_ERROR_NOT_CONNECTED;
-        }
+        VerifyOrReturnError(session == nullptr, CHIP_ERROR_NOT_CONNECTED);
         if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp) {
             // support large payloads
-            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message, kLargePayloadMaxSizeBytes);
+            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message, kLargePayloadMaxMessageSizeBytes);
         } else {
             err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message, kMaxAppMessageLen);
         }
diff --git a/src/transport/raw/MessageHeader.h b/src/transport/raw/MessageHeader.h
index 8b4165b6bc78ac..d76045e291340a 100644
--- a/src/transport/raw/MessageHeader.h
+++ b/src/transport/raw/MessageHeader.h
@@ -72,7 +72,7 @@ static_assert(detail::kMaxApplicationPayloadAndMICSizeBytes > kMaxTagLen, "Need
 // those in the header sizes.
 static constexpr size_t kMaxAppMessageLen = detail::kMaxApplicationPayloadAndMICSizeBytes - kMaxTagLen;
 // large payload limit
-static constexpr size_t kLargePayloadMaxSizeBytes   = 128000;
+static constexpr size_t kLargePayloadMaxMessageSizeBytes   = 128000;
 
 static constexpr uint16_t kMsgUnicastSessionIdUnsecured = 0x0000;
 

From 200dea3d1c3fd56d5b9d99374ca8c69512654ce2 Mon Sep 17 00:00:00 2001
From: "Restyled.io" <commits@restyled.io>
Date: Mon, 4 Dec 2023 19:39:08 +0000
Subject: [PATCH 06/10] Restyled by clang-format

---
 src/transport/SessionManager.cpp  | 15 ++++++++++-----
 src/transport/raw/MessageHeader.h |  2 +-
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/transport/SessionManager.cpp b/src/transport/SessionManager.cpp
index 43c477b879e292..13a6e3897ea9c9 100644
--- a/src/transport/SessionManager.cpp
+++ b/src/transport/SessionManager.cpp
@@ -201,14 +201,19 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
         packetHeader.SetSessionId(keyContext->GetKeyHash());
         CryptoContext::NonceStorage nonce;
         CryptoContext::BuildNonce(nonce, packetHeader.GetSecurityFlags(), packetHeader.GetMessageCounter(), sourceNodeId);
-        CHIP_ERROR err = CHIP_NO_ERROR;
+        CHIP_ERROR err          = CHIP_NO_ERROR;
         SecureSession * session = sessionHandle->AsSecureSession();
         VerifyOrReturnError(session == nullptr, CHIP_ERROR_NOT_CONNECTED);
-        if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp) {
+        if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp)
+        {
             // support large payloads
-            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message, kLargePayloadMaxMessageSizeBytes);
-        } else {
-            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message, kMaxAppMessageLen);
+            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
+                                              kLargePayloadMaxMessageSizeBytes);
+        }
+        else
+        {
+            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
+                                              kMaxAppMessageLen);
         }
         keyContext->Release();
         ReturnErrorOnFailure(err);
diff --git a/src/transport/raw/MessageHeader.h b/src/transport/raw/MessageHeader.h
index d76045e291340a..58a62ccb7a6e27 100644
--- a/src/transport/raw/MessageHeader.h
+++ b/src/transport/raw/MessageHeader.h
@@ -72,7 +72,7 @@ static_assert(detail::kMaxApplicationPayloadAndMICSizeBytes > kMaxTagLen, "Need
 // those in the header sizes.
 static constexpr size_t kMaxAppMessageLen = detail::kMaxApplicationPayloadAndMICSizeBytes - kMaxTagLen;
 // large payload limit
-static constexpr size_t kLargePayloadMaxMessageSizeBytes   = 128000;
+static constexpr size_t kLargePayloadMaxMessageSizeBytes = 128000;
 
 static constexpr uint16_t kMsgUnicastSessionIdUnsecured = 0x0000;
 

From 84adb59259cf62da2a446bd37ce67182b10ab5bb Mon Sep 17 00:00:00 2001
From: hnnajh <hnnajh@amazon.com>
Date: Mon, 4 Dec 2023 17:21:19 -0800
Subject: [PATCH 07/10] Fix unit tests

---
 src/transport/SessionManager.cpp | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/src/transport/SessionManager.cpp b/src/transport/SessionManager.cpp
index 13a6e3897ea9c9..4f8cd1305494d7 100644
--- a/src/transport/SessionManager.cpp
+++ b/src/transport/SessionManager.cpp
@@ -202,18 +202,23 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
         CryptoContext::NonceStorage nonce;
         CryptoContext::BuildNonce(nonce, packetHeader.GetSecurityFlags(), packetHeader.GetMessageCounter(), sourceNodeId);
         CHIP_ERROR err          = CHIP_NO_ERROR;
-        SecureSession * session = sessionHandle->AsSecureSession();
-        VerifyOrReturnError(session == nullptr, CHIP_ERROR_NOT_CONNECTED);
-        if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp)
-        {
-            // support large payloads
-            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
-                                              kLargePayloadMaxMessageSizeBytes);
-        }
-        else
-        {
+        if (sessionHandle->GetSessionType() == Transport::Session::SessionType::kSecure) {
+            SecureSession * session = sessionHandle->AsSecureSession();
+            VerifyOrReturnError(session == nullptr, CHIP_ERROR_NOT_CONNECTED);
+            if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp)
+            {
+                // support large payloads
+                err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
+                                                kLargePayloadMaxMessageSizeBytes);
+            }
+            else
+            {
+                err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
+                                                kMaxAppMessageLen);
+            }
+        } else {
             err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
-                                              kMaxAppMessageLen);
+                                                kMaxAppMessageLen);
         }
         keyContext->Release();
         ReturnErrorOnFailure(err);

From 15f187ace8d91ef1890a36b3cd87255a93edcc66 Mon Sep 17 00:00:00 2001
From: "Restyled.io" <commits@restyled.io>
Date: Tue, 5 Dec 2023 01:21:46 +0000
Subject: [PATCH 08/10] Restyled by clang-format

---
 src/transport/SessionManager.cpp | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/src/transport/SessionManager.cpp b/src/transport/SessionManager.cpp
index 4f8cd1305494d7..bea1901384d786 100644
--- a/src/transport/SessionManager.cpp
+++ b/src/transport/SessionManager.cpp
@@ -201,24 +201,27 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
         packetHeader.SetSessionId(keyContext->GetKeyHash());
         CryptoContext::NonceStorage nonce;
         CryptoContext::BuildNonce(nonce, packetHeader.GetSecurityFlags(), packetHeader.GetMessageCounter(), sourceNodeId);
-        CHIP_ERROR err          = CHIP_NO_ERROR;
-        if (sessionHandle->GetSessionType() == Transport::Session::SessionType::kSecure) {
+        CHIP_ERROR err = CHIP_NO_ERROR;
+        if (sessionHandle->GetSessionType() == Transport::Session::SessionType::kSecure)
+        {
             SecureSession * session = sessionHandle->AsSecureSession();
             VerifyOrReturnError(session == nullptr, CHIP_ERROR_NOT_CONNECTED);
             if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp)
             {
                 // support large payloads
                 err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
-                                                kLargePayloadMaxMessageSizeBytes);
+                                                  kLargePayloadMaxMessageSizeBytes);
             }
             else
             {
                 err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
-                                                kMaxAppMessageLen);
+                                                  kMaxAppMessageLen);
             }
-        } else {
+        }
+        else
+        {
             err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
-                                                kMaxAppMessageLen);
+                                              kMaxAppMessageLen);
         }
         keyContext->Release();
         ReturnErrorOnFailure(err);

From efd7902a0c49ced84bbd08af678be40f1f0bd821 Mon Sep 17 00:00:00 2001
From: hnnajh <hnnajh@amazon.com>
Date: Tue, 5 Dec 2023 16:23:34 -0800
Subject: [PATCH 09/10] fix group messaging

---
 src/transport/SessionManager.cpp | 36 ++++++++++++--------------------
 1 file changed, 13 insertions(+), 23 deletions(-)

diff --git a/src/transport/SessionManager.cpp b/src/transport/SessionManager.cpp
index bea1901384d786..6ada97a587e859 100644
--- a/src/transport/SessionManager.cpp
+++ b/src/transport/SessionManager.cpp
@@ -201,28 +201,8 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
         packetHeader.SetSessionId(keyContext->GetKeyHash());
         CryptoContext::NonceStorage nonce;
         CryptoContext::BuildNonce(nonce, packetHeader.GetSecurityFlags(), packetHeader.GetMessageCounter(), sourceNodeId);
-        CHIP_ERROR err = CHIP_NO_ERROR;
-        if (sessionHandle->GetSessionType() == Transport::Session::SessionType::kSecure)
-        {
-            SecureSession * session = sessionHandle->AsSecureSession();
-            VerifyOrReturnError(session == nullptr, CHIP_ERROR_NOT_CONNECTED);
-            if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp)
-            {
-                // support large payloads
-                err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
-                                                  kLargePayloadMaxMessageSizeBytes);
-            }
-            else
-            {
-                err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
-                                                  kMaxAppMessageLen);
-            }
-        }
-        else
-        {
-            err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
-                                              kMaxAppMessageLen);
-        }
+        CHIP_ERROR err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
+            kMaxAppMessageLen);
         keyContext->Release();
         ReturnErrorOnFailure(err);
 
@@ -258,7 +238,17 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
         NodeId sourceNodeId = session->GetLocalScopedNodeId().GetNodeId();
         CryptoContext::BuildNonce(nonce, packetHeader.GetSecurityFlags(), messageCounter, sourceNodeId);
 
-        ReturnErrorOnFailure(SecureMessageCodec::Encrypt(session->GetCryptoContext(), nonce, payloadHeader, packetHeader, message));
+        if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp)
+        {
+            // support large payloads
+            ReturnErrorOnFailure(SecureMessageCodec::Encrypt(session->GetCryptoContext(), nonce, payloadHeader, packetHeader, message,
+                kLargePayloadMaxMessageSizeBytes));
+        }
+        else
+        {
+            ReturnErrorOnFailure(SecureMessageCodec::Encrypt(session->GetCryptoContext(), nonce, payloadHeader, packetHeader, message,
+                kMaxAppMessageLen));
+        }
 
 #if CHIP_PROGRESS_LOGGING
         destination = session->GetPeerNodeId();

From 1c2c52c060baa3d15cf0f5c9f4b4477ec2d789ae Mon Sep 17 00:00:00 2001
From: "Restyled.io" <commits@restyled.io>
Date: Fri, 19 Jan 2024 23:58:11 +0000
Subject: [PATCH 10/10] Restyled by clang-format

---
 src/transport/SessionManager.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/transport/SessionManager.cpp b/src/transport/SessionManager.cpp
index 6ada97a587e859..5431dc52f60d05 100644
--- a/src/transport/SessionManager.cpp
+++ b/src/transport/SessionManager.cpp
@@ -201,8 +201,8 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
         packetHeader.SetSessionId(keyContext->GetKeyHash());
         CryptoContext::NonceStorage nonce;
         CryptoContext::BuildNonce(nonce, packetHeader.GetSecurityFlags(), packetHeader.GetMessageCounter(), sourceNodeId);
-        CHIP_ERROR err = SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message,
-            kMaxAppMessageLen);
+        CHIP_ERROR err =
+            SecureMessageCodec::Encrypt(CryptoContext(keyContext), nonce, payloadHeader, packetHeader, message, kMaxAppMessageLen);
         keyContext->Release();
         ReturnErrorOnFailure(err);
 
@@ -241,13 +241,13 @@ CHIP_ERROR SessionManager::PrepareMessage(const SessionHandle & sessionHandle, P
         if (session->GetPeerAddress().GetTransportType() == Transport::Type::kTcp)
         {
             // support large payloads
-            ReturnErrorOnFailure(SecureMessageCodec::Encrypt(session->GetCryptoContext(), nonce, payloadHeader, packetHeader, message,
-                kLargePayloadMaxMessageSizeBytes));
+            ReturnErrorOnFailure(SecureMessageCodec::Encrypt(session->GetCryptoContext(), nonce, payloadHeader, packetHeader,
+                                                             message, kLargePayloadMaxMessageSizeBytes));
         }
         else
         {
-            ReturnErrorOnFailure(SecureMessageCodec::Encrypt(session->GetCryptoContext(), nonce, payloadHeader, packetHeader, message,
-                kMaxAppMessageLen));
+            ReturnErrorOnFailure(SecureMessageCodec::Encrypt(session->GetCryptoContext(), nonce, payloadHeader, packetHeader,
+                                                             message, kMaxAppMessageLen));
         }
 
 #if CHIP_PROGRESS_LOGGING