Add get() function to CryptHandle class

This commit modifies the CryptHandle class so that the crypt_device
struct is managed internally. This way, the caller does not need to
provide it's own crypt_device pointer. The caller can use the new get()
function when it needs access to the crypt_device struct.

Signed-off-by: John Wedig <johnwedig@google.com>
Change-Id: I82c2f96d74cc2714de5a656432cbaa2f6ee1244a
diff --git a/include/cryptsetupInterface.hpp b/include/cryptsetupInterface.hpp
index 9e26da9..25c69d0 100644
--- a/include/cryptsetupInterface.hpp
+++ b/include/cryptsetupInterface.hpp
@@ -154,27 +154,32 @@
   public:
     /** @brief Constructor for CryptHandle
      *
-     *  @param[out] cd - pointer to crypt_device*, to be allocated
      *  @param[in] device - path to device file
      */
-    CryptHandle(struct crypt_device** cd, const char* device) :
-        handle(init(cd, device))
+    explicit CryptHandle(const char* device) : handle(init(device))
     {}
 
+    /** @brief Get a pointer to the crypt_device struct. */
+    struct crypt_device* get()
+    {
+        return *handle;
+    }
+
+  private:
     /** @brief Allocate and initialize the crypt_device struct
      *
-     *  @param[out] cd - pointer to crypt_device*, to be allocated
      *  @param[in] device - path to device file
      */
-    struct crypt_device* init(struct crypt_device** cd, const char* device)
+    struct crypt_device* init(const char* device)
     {
-        int retval = crypt_init(cd, device);
+        struct crypt_device* cryptDev;
+        int retval = crypt_init(&cryptDev, device);
         if (retval < 0)
         {
             return nullptr;
         }
 
-        return *cd;
+        return cryptDev;
     }
 
     /** @brief Free the crypt_device struct
diff --git a/src/estoraged.cpp b/src/estoraged.cpp
index 4e64ca6..18e4248 100644
--- a/src/estoraged.cpp
+++ b/src/estoraged.cpp
@@ -29,17 +29,16 @@
     std::string msg = "OpenBMC.0.1.DriveFormat";
     lg2::info("Starting format", "REDFISH_MESSAGE_ID", msg);
 
-    struct crypt_device* cryptDev;
-    CryptHandle cryptHandle(&cryptDev, devPath.c_str());
-    if (*cryptHandle.handle == nullptr)
+    CryptHandle cryptHandle(devPath.c_str());
+    if (cryptHandle.get() == nullptr)
     {
         lg2::error("Failed to initialize crypt device", "REDFISH_MESSAGE_ID",
                    std::string("OpenBMC.0.1.FormatFail"));
         throw EncryptionError();
     }
 
-    formatLuksDev(cryptDev, password);
-    activateLuksDev(cryptDev, password);
+    formatLuksDev(cryptHandle.get(), password);
+    activateLuksDev(cryptHandle.get(), password);
 
     createFilesystem();
     mountFilesystem();
@@ -104,16 +103,15 @@
     std::string msg = "OpenBMC.0.1.DriveUnlock";
     lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg);
 
-    struct crypt_device* cryptDev;
-    CryptHandle cryptHandle(&cryptDev, devPath.c_str());
-    if (*cryptHandle.handle == nullptr)
+    CryptHandle cryptHandle(devPath.c_str());
+    if (cryptHandle.get() == nullptr)
     {
         lg2::error("Failed to initialize crypt device", "REDFISH_MESSAGE_ID",
                    std::string("OpenBMC.0.1.UnlockFail"));
         throw EncryptionError();
     }
 
-    activateLuksDev(cryptDev, password);
+    activateLuksDev(cryptHandle.get(), password);
     mountFilesystem();
 }