Implement decryptPayload and invoke it when unflattening the packet

Change-Id: I52b283c9390e43c89b84d0617cebaf4938dad881
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/message_parsers.cpp b/message_parsers.cpp
index d43009f..6cdd694 100644
--- a/message_parsers.cpp
+++ b/message_parsers.cpp
@@ -165,9 +165,6 @@
         ((header->payloadType & PAYLOAD_AUTH_MASK) ? true : false);
 
     auto payloadLen = endian::from_ipmi(header->payloadLength);
-    message->payload.assign(inPacket.begin() + sizeof(SessionHeader_t),
-                            inPacket.begin() + sizeof(SessionHeader_t) +
-                            payloadLen);
 
     if (message->isPacketAuthenticated)
     {
@@ -179,6 +176,21 @@
         }
     }
 
+    // Decrypt the payload if the payload is encrypted
+    if (message->isPacketEncrypted)
+    {
+        // Assign the decrypted payload to the IPMI Message
+        message->payload = internal::decryptPayload(inPacket,
+                                                    *(message.get()),
+                                                    payloadLen);
+    }
+    else
+    {
+        message->payload.assign(inPacket.begin() + sizeof(SessionHeader_t),
+                                inPacket.begin() + sizeof(SessionHeader_t) +
+                                payloadLen);
+    }
+
     return message;
 }
 
@@ -310,6 +322,18 @@
     packet.insert(packet.end(), integrityData.begin(), integrityData.end());
 }
 
+std::vector<uint8_t> decryptPayload(const std::vector<uint8_t>& packet,
+                                    const Message& message,
+                                    size_t payloadLen)
+{
+    auto session = (std::get<session::Manager&>(singletonPool).getSession(
+                        message.bmcSessionID)).lock();
+
+    return session->getCryptAlgo()->decryptPayload(packet,
+                                                  sizeof(SessionHeader_t),
+                                                  payloadLen);
+}
+
 } // namespace internal
 
 } // namespace ipmi20parser