Add crypto Erase to eStorageD.

The goal is to erase the keys that are used to decrypt the drive. After
the keys are erased it will not be possible to decrypt the drive, even
if the password can be recalled. The data is forever inaccessible.

Testing:

$ systemctl stop emmc.service

$ /eStoraged -b /dev/mmcblk0 &

$ busctl call xyz.openbmc_project.eStoraged.mmcblk0 /xyz/openbmc_project/storage/mmcblk0 xyz.openbmc_project.Inventory.Item.Volume FormatLuks ays 3 1 2 3 xyz.openbmc_project.Inventory.Item.Volume.FilesystemType.ext4

$ busctl call xyz.openbmc_project.eStoraged.mmcblk0 /xyz/openbmc_project/storage/mmcblk0 xyz.openbmc_project.Inventory.Item.Volume Lock

$ busctl call xyz.openbmc_project.eStoraged.mmcblk0 /xyz/openbmc_project/storage/mmcblk0 xyz.openbmc_project.Inventory.Item.Volume Erase s xyz.openbmc_project.Inventory.Item.Volume.EraseMethod.CryptoErase

$ busctl call xyz.openbmc_project.eStoraged.mmcblk0 /xyz/openbmc_project/storage/mmcblk0 xyz.openbmc_project.Inventory.Item.Volume Unlock ay 3 1 2 3
Call failed: The operation failed internally.

Signed-off-by: John Edward Broadbent <jebr@google.com>
Change-Id: I3221e82a92c1b555e2379b19c9e1d5b6e4b02f9b
diff --git a/src/erase/cryptoErase.cpp b/src/erase/cryptoErase.cpp
new file mode 100644
index 0000000..d99f6bf
--- /dev/null
+++ b/src/erase/cryptoErase.cpp
@@ -0,0 +1,88 @@
+#include "cryptErase.hpp"
+#include "cryptsetupInterface.hpp"
+#include "erase.hpp"
+
+#include <libcryptsetup.h>
+
+#include <phosphor-logging/lg2.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
+#include <memory>
+#include <string>
+#include <string_view>
+
+namespace estoraged
+{
+using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
+using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
+
+CryptErase::CryptErase(
+    std::string_view devPathIn,
+    std::unique_ptr<estoraged::CryptsetupInterface> inCryptIface) :
+    Erase(devPathIn),
+    cryptIface(std::move(inCryptIface))
+{}
+
+void CryptErase::doErase()
+{
+    /* get cryptHandle */
+    CryptHandle cryptHandle(std::string(devPath).c_str());
+    if (cryptHandle.get() == nullptr)
+    {
+        lg2::error("Failed to initialize crypt device", "REDFISH_MESSAGE_ID",
+                   std::string("OpenBMC.0.1.EraseFailure"));
+        throw ResourceNotFound();
+    }
+    /* cryptLoad */
+    if (cryptIface.get()->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr) !=
+        0)
+    {
+        lg2::error("Failed to load the key slots for destruction",
+                   "REDFISH_MESSAGE_ID",
+                   std::string("OpenBMC.0.1.EraseFailure"));
+        throw ResourceNotFound();
+    }
+
+    /* find key slots */
+    int nKeySlots = cryptIface.get()->cryptKeySlotMax(CRYPT_LUKS2);
+    if (nKeySlots < 0)
+    {
+        lg2::error("Failed to find the max keyslots", "REDFISH_MESSAGE_ID",
+                   std::string("OpenBMC.0.1.EraseFailure"));
+        throw ResourceNotFound();
+    }
+
+    if (nKeySlots == 0)
+    {
+        lg2::error("Max keyslots should never be zero", "REDFISH_MESSAGE_ID",
+                   std::string("OpenBMC.0.1.EraseFailure"));
+        throw ResourceNotFound();
+    }
+
+    /* destory working keyslots */
+    bool keySlotIssue = false;
+    for (int i = 0; i < nKeySlots; i++)
+    {
+        crypt_keyslot_info ki =
+            cryptIface.get()->cryptKeySlotStatus(cryptHandle.get(), i);
+
+        if (ki == CRYPT_SLOT_ACTIVE || ki == CRYPT_SLOT_ACTIVE_LAST)
+        {
+            if (cryptIface.get()->cryptKeyslotDestroy(cryptHandle.get(), i) !=
+                0)
+            {
+                lg2::error(
+                    "Estoraged erase failed to destroy keyslot, continuing",
+                    "REDFISH_MESSAGE_ID",
+                    std::string("eStorageD.1.0.EraseFailure"));
+                keySlotIssue = true;
+            }
+        }
+    }
+    if (keySlotIssue)
+    {
+        throw InternalFailure();
+    }
+}
+
+} // namespace estoraged