Use common typedef for binary data 15/159915/24
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 13 Nov 2017 16:29:49 +0000 (17:29 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Tue, 28 Nov 2017 13:59:20 +0000 (14:59 +0100)
Change-Id: I8a47b1f6fb3718608a2011e50b79b8e6f466414d

32 files changed:
rmi/common.h
rmi/key.h
server/engine/encryption/cryptsetup-engine.cpp
server/engine/encryption/cryptsetup-engine.h
server/engine/encryption/dmcrypt-engine.cpp
server/engine/encryption/dmcrypt-engine.h
server/engine/encryption/ecryptfs-engine.cpp
server/engine/encryption/ecryptfs-engine.h
server/engine/encryption/ext4-engine.cpp
server/engine/encryption/ext4-engine.h
server/ext4-tool.cpp
server/ext4-tool.h
server/external-encryption.cpp
server/external-encryption.h
server/file-footer.cpp
server/file-footer.h
server/internal-encryption.cpp
server/internal-encryption.h
server/key-manager/anti-forensics.cpp
server/key-manager/anti-forensics.h
server/key-manager/encrypted-key.cpp
server/key-manager/encrypted-key.h
server/key-manager/key-store.cpp
server/key-manager/key-store.h
server/key-server.cpp
server/key-server.h
server/luks.cpp
server/upgrade-support.cpp
server/upgrade-support.h
tests/CMakeLists.txt
tests/af.cpp
tests/dmcrypt-engine.cpp

index 4a634b6..c5481a3 100644 (file)
@@ -22,6 +22,8 @@
 #ifndef __ODE_COMMON_H__
 #define __ODE_COMMON_H__
 
+#include <vector>
+
 namespace ode {
 
 // TODO make it an enum when klay starts to support enum serialization in rmi
@@ -41,6 +43,8 @@ const int Unknown = -10;
 
 };
 
+typedef std::vector<unsigned char> BinaryData;
+
 } // namespace ode
 
 #endif // __ODE_COMMON_H__
index 1ae7012..3628aef 100644 (file)
--- a/rmi/key.h
+++ b/rmi/key.h
@@ -18,7 +18,6 @@
 #define __ODE_KEY_H__
 
 #include <string>
-#include <vector>
 
 #include "common.h"
 
@@ -34,7 +33,6 @@ public:
            DEFAULT_256BIT = 1,
            DEFAULT_512BIT = 2,
        };
-       typedef std::vector<unsigned char> KeyData; // TODO change it to something else outside of this class
 
        virtual ~Key() {}
 
index e325f2c..0b96898 100644 (file)
@@ -36,7 +36,7 @@ std::string mappingPath(const std::string& name)
        return mappings_dir + name;
 }
 
-void forkAndWrite(const char *const* argv, const CryptsetupEngine::data *d)
+void forkAndWrite(const char *const* argv, const BinaryData *d)
 {
        // pipe
        int pipefd[2];
@@ -108,7 +108,7 @@ CryptsetupEngine::~CryptsetupEngine()
 {
 }
 
-void CryptsetupEngine::format(DeviceType type, const data &key)
+void CryptsetupEngine::format(DeviceType type, const BinaryData &key)
 {
        switch (type) {
        case DeviceType::LUKS:
@@ -148,7 +148,9 @@ void CryptsetupEngine::format(DeviceType type, const data &key)
        }
 }
 
-std::string CryptsetupEngine::open(DeviceType type, const std::string &name, const data &key)
+std::string CryptsetupEngine::open(DeviceType type,
+                                                                  const std::string &name,
+                                                                  const BinaryData &key)
 {
        // create new mapping
        switch (type) {
index 263d7ba..1ce40c7 100644 (file)
 #define __CRYPTSETUP_ENGINE_H__
 
 #include <string>
-#include <vector>
 
 #include <klay/exception.h>
 
+#include "rmi/common.h"
+
 namespace ode {
 
 class CryptsetupEngine final {
@@ -60,13 +61,12 @@ public:
        CryptsetupEngine &operator=(const CryptsetupEngine &) = delete;
        CryptsetupEngine &operator=(CryptsetupEngine &&) = delete;
 
-       // TODO make it common among engines
-       typedef std::vector<unsigned char> data;
-
-       void format(DeviceType type, const data &key);
+       void format(DeviceType type, const BinaryData &key);
 
        // create new mapping, returns mapping path
-       std::string open(DeviceType type, const std::string &name, const data &key);
+       std::string open(DeviceType type,
+                                        const std::string &name,
+                                        const BinaryData &key);
 
        static void close(const std::string &name);
 
index f6165f3..4d57c54 100644 (file)
@@ -58,7 +58,7 @@ blkcnt_t getBlockCount(const std::string &src)
        return (blkcnt_t)size;
 }
 
-const std::string convertToHex(const DMCryptEngine::data &binary)
+const std::string convertToHex(const BinaryData &binary)
 {
        std::stringstream hex;
 
@@ -84,7 +84,10 @@ void initDMIoctl(char *buf, size_t size, const std::string &name, unsigned flags
        ::strncpy(io->name, name.c_str(), sizeof(io->name) - 1);
 }
 
-const std::string createCryptoBlkDev(const std::string &realBlkDev, const std::string &mountName, const DMCryptEngine::data &key, std::string cryptoTypeName)
+const std::string createCryptoBlkDev(const std::string &realBlkDev,
+                                                                        const std::string &mountName,
+                                                                        const BinaryData &key,
+                                                                        std::string cryptoTypeName)
 {
        auto blockCount = getBlockCount(realBlkDev);
        std::string cryptoBlkDev;
@@ -194,11 +197,11 @@ void destroyCryptoBlkDev(const std::string &cryptoBlkDev)
        }
 }
 
-ode::DMCryptEngine::data sanitizeKey(const ode::DMCryptEngine::data &key)
+BinaryData sanitizeKey(const BinaryData &key)
 {
        if (key.size() < DM_KEY_MIN_LEN_BYTE)
                throw runtime::Exception("Size of key smaller than minimum 32B");
-       ode::DMCryptEngine::data sanitized(key);
+       BinaryData sanitized(key);
        sanitized.resize(DM_KEY_MIN_LEN_BYTE);
        return sanitized;
 }
@@ -242,7 +245,7 @@ DMCryptEngine::~DMCryptEngine()
 {
 }
 
-void DMCryptEngine::mount(const DMCryptEngine::data &key, unsigned int options)
+void DMCryptEngine::mount(const BinaryData &key, unsigned int options)
 {
        // create crypto type device mapping layer to mount the encrypted partition.
        auto cryptoBlkDev = createCryptoBlkDev(source, DM_DEFAULT_LABEL_NAME, sanitizeKey(key), DM_DEFAULT_CRYPTO_NAME);
@@ -268,7 +271,7 @@ bool DMCryptEngine::isMounted()
        return mounted;
 }
 
-void DMCryptEngine::encrypt(const DMCryptEngine::data &key, unsigned int options)
+void DMCryptEngine::encrypt(const BinaryData &key, unsigned int options)
 {
        // Force filesystem check via fcsf might be able to avoid fail situation during encryption.
        Ext4Tool ext4Source(source);
@@ -309,7 +312,7 @@ void DMCryptEngine::encrypt(const DMCryptEngine::data &key, unsigned int options
        progress.done();
 }
 
-void DMCryptEngine::decrypt(const DMCryptEngine::data &key, unsigned int options)
+void DMCryptEngine::decrypt(const BinaryData &key, unsigned int options)
 {
        // create crypto type device mapping layer to mount the plain partition
        // should be encrypted here.
index fed5f94..0320267 100644 (file)
 #ifndef __DMCRYPT_ENGINE_H__
 #define __DMCRYPT_ENGINE_H__
 
-#include <vector>
 #include <string>
 #include <atomic>
 
 #include "progress-bar.h"
+#include "rmi/common.h"
 
 namespace ode {
 
@@ -45,14 +45,12 @@ public:
                return destination;
        }
 
-       typedef std::vector<unsigned char> data;
-
-       void mount(const data &key, unsigned int options);
+       void mount(const BinaryData &key, unsigned int options);
        void umount();
        bool isMounted();
 
-       void encrypt(const data &key, unsigned int options);
-       void decrypt(const data &key, unsigned int options);
+       void encrypt(const BinaryData &key, unsigned int options);
+       void decrypt(const BinaryData &key, unsigned int options);
 
        unsigned int getSupportedOptions();
 
index 3fa613d..9afe2b8 100644 (file)
@@ -268,7 +268,7 @@ void copyInPlace(const std::string& source, const std::string& destination,
        }
 }
 
-void ecryptfsMount(const std::string &source, const std::string &destination, const std::vector<unsigned char> &key, unsigned int options)
+void ecryptfsMount(const std::string &source, const std::string &destination, const BinaryData &key, unsigned int options)
 {
        EcryptfsPayload payload(EcryptfsPayload::Type::PasswordToken);
        std::string mountOption;
@@ -342,7 +342,7 @@ EcryptfsEngine::~EcryptfsEngine()
 {
 }
 
-void EcryptfsEngine::mount(const data &key, unsigned int options)
+void EcryptfsEngine::mount(const BinaryData &key, unsigned int options)
 {
        ecryptfsMount(source, destination, key, options);
 }
@@ -366,7 +366,7 @@ bool EcryptfsEngine::isMounted()
        return false;
 }
 
-void EcryptfsEngine::encrypt(const data &key, unsigned int options)
+void EcryptfsEngine::encrypt(const BinaryData &key, unsigned int options)
 {
        if (!isEnoughToCopyInPlace(source, getDecryptedSize)) {
                throw runtime::Exception("No space to encryption");
@@ -413,7 +413,7 @@ void EcryptfsEngine::encrypt(const data &key, unsigned int options)
        progress.done();
 }
 
-void EcryptfsEngine::decrypt(const data &key, unsigned int options)
+void EcryptfsEngine::decrypt(const BinaryData &key, unsigned int options)
 {
        if (!isEnoughToCopyInPlace(destination, getEncryptedSize)) {
                throw runtime::Exception("No space to encryption");
index 5e83dea..3d97ad2 100644 (file)
 #ifndef __ECRYPTFS_ENGINE_H__
 #define __ECRYPTFS_ENGINE_H__
 
-#include <vector>
 #include <string>
 #include <atomic>
 
 #include "progress-bar.h"
+#include "rmi/common.h"
 
 namespace ode {
 
@@ -45,14 +45,12 @@ public:
                return destination;
        }
 
-       typedef std::vector<unsigned char> data;
-
-       void mount(const data& key, unsigned int);
+       void mount(const BinaryData& key, unsigned int);
        void umount();
        bool isMounted();
 
-       void encrypt(const data& key, unsigned int);
-       void decrypt(const data& key, unsigned int);
+       void encrypt(const BinaryData& key, unsigned int);
+       void decrypt(const BinaryData& key, unsigned int);
 
        unsigned int getSupportedOptions();
 
index 7f771c0..6934340 100755 (executable)
@@ -73,14 +73,14 @@ namespace ode {
 
 namespace {
 
-const Ext4Engine::data generateKeyDescriptor(const Ext4Engine::data& key)
+const BinaryData generateKeyDescriptor(const BinaryData& key)
 {
        auto hash = KeyGenerator::SHA512(KeyGenerator::SHA512(key));
        hash.resize(EXT4_KEY_DESCRIPTOR_SIZE);
        return hash;
 }
 
-const std::string convertToHex(const Ext4Engine::data &binary)
+const std::string convertToHex(const BinaryData& binary)
 {
        std::stringstream hex;
 
@@ -91,14 +91,14 @@ const std::string convertToHex(const Ext4Engine::data &binary)
        return hex.str();
 }
 
-Ext4Engine::data sanitizeKey(const Ext4Engine::data &key)
+BinaryData sanitizeKey(const BinaryData& key)
 {
-       Ext4Engine::data sanitized(key);
+       BinaryData sanitized(key);
        sanitized.resize(EXT4_MAX_KEY_SIZE);
        return sanitized;
 }
 
-void addKeyToKeyring(const Ext4Engine::data& key)
+void addKeyToKeyring(const BinaryData& key)
 {
        struct ext4_encryption_key payload;
        std::string keyDescriptor;
@@ -140,7 +140,7 @@ int intLog2(int arg)
        return l;
 }
 
-void setPolicy(const std::string& path, const Ext4Engine::data& key)
+void setPolicy(const std::string& path, const BinaryData& key)
 {
        struct ext4_encryption_policy policy;
        int pad = 4;
@@ -151,7 +151,7 @@ void setPolicy(const std::string& path, const Ext4Engine::data& key)
                throw runtime::Exception("invalid path");
        }
 
-       Ext4Engine::data descriptor = generateKeyDescriptor(key);
+       BinaryData descriptor = generateKeyDescriptor(key);
 
        policy.version = 0;
        policy.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
@@ -296,7 +296,7 @@ Ext4Engine::~Ext4Engine()
 {
 }
 
-void Ext4Engine::mount(const Ext4Engine::data& key, unsigned int options)
+void Ext4Engine::mount(const BinaryData& key, unsigned int options)
 {
        std::string encryptedPath(destination + "/" ENCRYPTION_DIR);
 
@@ -329,7 +329,7 @@ bool Ext4Engine::isMounted()
        return mounted;
 }
 
-void Ext4Engine::encrypt(const Ext4Engine::data& key, unsigned int options)
+void Ext4Engine::encrypt(const BinaryData& key, unsigned int options)
 {
        if (!isEnoughToCopyInPlace(destination)) {
                throw runtime::Exception("No space to encryption");
@@ -339,7 +339,7 @@ void Ext4Engine::encrypt(const Ext4Engine::data& key, unsigned int options)
                throw runtime::Exception("Mount error - " + runtime::GetSystemErrorMessage());
        }
 
-       Ext4Engine::data sanitizedKey = sanitizeKey(key);
+       BinaryData sanitizedKey = sanitizeKey(key);
        addKeyToKeyring(sanitizedKey);
 
        runtime::File encrypted(destination + "/" ENCRYPTION_DIR);
@@ -365,7 +365,7 @@ void Ext4Engine::encrypt(const Ext4Engine::data& key, unsigned int options)
        progress.done();
 }
 
-void Ext4Engine::decrypt(const Ext4Engine::data& key, unsigned int options)
+void Ext4Engine::decrypt(const BinaryData& key, unsigned int options)
 {
        if (!isEnoughToCopyInPlace(destination)) {
                throw runtime::Exception("No space to encryption");
index dfd848d..c72a23a 100644 (file)
 #ifndef __EXT4_ENGINE_H__
 #define __EXT4_ENGINE_H__
 
-#include <vector>
 #include <string>
 #include <atomic>
 
 #include "progress-bar.h"
+#include "rmi/common.h"
 
 namespace ode {
 
@@ -45,14 +45,12 @@ public:
                return destination;
        }
 
-       typedef std::vector<unsigned char> data;
-
-       void mount(const data &key, unsigned int options);
+       void mount(const BinaryData &key, unsigned int options);
        void umount();
        bool isMounted();
 
-       void encrypt(const data &key, unsigned int options);
-       void decrypt(const data &key, unsigned int options);
+       void encrypt(const BinaryData &key, unsigned int options);
+       void decrypt(const BinaryData &key, unsigned int options);
 
        unsigned int getSupportedOptions();
 
index a70a1c6..22fde77 100644 (file)
@@ -146,7 +146,7 @@ void Ext4Tool::readInfo()
        unsigned int descBlockCount = divCeilSafely(groupDescCount, descPerBlock);
 
        // read first meta block
-       data group_desc(descBlockCount * blockSize);
+       BinaryData group_desc(descBlockCount * blockSize);
        device.lseek((firstDataBlock + 1) * blockSize, SEEK_SET);
        device.read(group_desc.data(), (descBlockCount * blockSize));
 
@@ -172,7 +172,7 @@ void Ext4Tool::readInfo()
                unsigned int blk = (((struct odeExtGroupDesc *)(((unsigned char *)(group_desc.data())) + i * descSize))->blockBitmap);
 
                try {
-                       data block_bitmap(blockSize);
+                       BinaryData block_bitmap(blockSize);
                        device.lseek(blk * blockSize, SEEK_SET);
                        device.read(block_bitmap.data(), blockSize);
 
index bbf2ce8..711d093 100644 (file)
 #define __EXT4_TOOL_H__
 
 #include <string>
-#include <vector>
 
 #include <klay/filesystem.h>
 
+#include "rmi/common.h"
+
 namespace ode {
 
 class Ext4Tool final {
@@ -34,8 +35,6 @@ public:
        Ext4Tool &operator=(const Ext4Tool &) = delete;
        Ext4Tool &operator=(Ext4Tool &&) = delete;
 
-       typedef std::vector<unsigned char> data;
-
        unsigned int getBlockSize();
        unsigned int getTotalBlockCount();
        bool isUsedBlock(unsigned int blockIndex);
@@ -48,7 +47,7 @@ private:
        void readInfo();
        std::string source;
        unsigned int blockSize, totalBlockCount;
-       data bitmap;
+       BinaryData bitmap;
 };
 
 } // namespace ode
index fe1169b..0a8d0bb 100644 (file)
@@ -229,7 +229,7 @@ int ExternalEncryptionServer::mount()
                return error::NoData;
        }
 
-       Key::KeyData key = mountKey;
+       BinaryData key = mountKey;
        mountKey.clear();
 
        if (getState() != State::Encrypted) {
@@ -288,7 +288,7 @@ int ExternalEncryptionServer::encrypt(const std::string &password, unsigned int
                return error::NoSuchDevice;
        }
 
-       Key::KeyData masterKey;
+       BinaryData masterKey;
        int ret = keyServer.get(engine->getSource(), password, masterKey);
        if (ret != error::None)
                return ret;
@@ -327,7 +327,7 @@ int ExternalEncryptionServer::decrypt(const std::string &password)
                return error::NoSuchDevice;
        }
 
-       Key::KeyData masterKey;
+       BinaryData masterKey;
        int ret = keyServer.get(engine->getSource(), password, masterKey);
        if (ret != error::None)
                return ret;
index 0d614b4..bb4d9cc 100644 (file)
@@ -61,7 +61,7 @@ private:
        ServerContext& server;
 
        std::unique_ptr<EXTERNAL_ENGINE> engine;
-       Key::KeyData mountKey;
+       BinaryData mountKey;
        KeyServer& keyServer;
 };
 
index 008c55c..b81c14f 100644 (file)
@@ -31,7 +31,7 @@ namespace {
 
 const std::string getFileName(const std::string &key)
 {
-       KeyGenerator::data hash = KeyGenerator::SHA256(KeyGenerator::data(key.begin(), key.end()));
+       BinaryData hash = KeyGenerator::SHA256(BinaryData(key.begin(), key.end()));
        std::stringstream fileName;
 
        fileName << "/opt/etc/.ode_";
@@ -56,14 +56,14 @@ bool FileFooter::exist(const std::string &key)
        return file.exists();
 }
 
-const FileFooter::data FileFooter::read(const std::string &key)
+const BinaryData FileFooter::read(const std::string &key)
 {
        std::string fileName(getFileName(key));
 
        INFO(SINK, "Footer file : " + fileName);
 
        runtime::File file(fileName);
-       data value(file.size());
+       BinaryData value(file.size());
 
        file.open(O_RDONLY);
        file.read(value.data(), value.size());
@@ -71,7 +71,7 @@ const FileFooter::data FileFooter::read(const std::string &key)
        return value;
 }
 
-void FileFooter::write(const std::string &key, const data &value)
+void FileFooter::write(const std::string &key, const BinaryData &value)
 {
        std::string fileName(getFileName(key));
 
index 8400162..0f3f447 100644 (file)
 #ifndef __FILE_FOOTER_H__
 #define __FILE_FOOTER_H__
 
-#include <vector>
 #include <string>
 
+#include "rmi/common.h"
+
 namespace ode {
 
 class FileFooter final {
 public:
        FileFooter() = delete;
 
-       typedef std::vector<unsigned char> data;
-
        static bool exist(const std::string &key);
-       static const data read(const std::string &key);
-       static void write(const std::string &key, const data &value);
+       static const BinaryData read(const std::string &key);
+       static void write(const std::string &key, const BinaryData &value);
        static void clear(const std::string &key);
 };
 
index 6a5dd36..0dfaf71 100644 (file)
@@ -279,7 +279,7 @@ int InternalEncryptionServer::mount()
                return error::NoData;
        }
 
-       Key::KeyData key = mountKey;
+       BinaryData key = mountKey;
        mountKey.clear();
 
        if (getState() != State::Encrypted) {
@@ -340,7 +340,7 @@ int InternalEncryptionServer::encrypt(const std::string& password, unsigned int
                return error::NoSuchDevice;
        }
 
-       Key::KeyData masterKey;
+       BinaryData masterKey;
        int ret = keyServer.get(engine->getSource(), password, masterKey);
        if (ret != error::None)
                return ret;
@@ -400,7 +400,7 @@ int InternalEncryptionServer::decrypt(const std::string& password)
                return error::NoSuchDevice;
        }
 
-       Key::KeyData masterKey;
+       BinaryData masterKey;
        int ret = keyServer.get(engine->getSource(), password, masterKey);
        if (ret != error::None)
                return ret;
index cdb0236..480fd8c 100644 (file)
@@ -58,7 +58,7 @@ private:
        ServerContext& server;
 
        std::unique_ptr<INTERNAL_ENGINE> engine;
-       Key::KeyData mountKey;
+       BinaryData mountKey;
        KeyServer& keyServer;
 };
 
index 4f32bce..b49b8ed 100644 (file)
@@ -84,14 +84,15 @@ static void diffuse(unsigned char *src, unsigned char *dst, size_t size)
        }
 }
 
-AntiForensics::data AntiForensics::AFSplit(const AntiForensics::data &src,
-               uint32_t blockSize, uint32_t blockNumbers)
+BinaryData AntiForensics::AFSplit(const BinaryData &src,
+                                                                 uint32_t blockSize,
+                                                                 uint32_t blockNumbers)
 {
        if (blockSize != src.size())
                throw runtime::Exception("Source size check failed");
 
-       data buf(blockSize + 1, 0);
-       data dst(blockSize * blockNumbers, 0);
+       BinaryData buf(blockSize + 1, 0);
+       BinaryData dst(blockSize * blockNumbers, 0);
 
        uint32_t i;
        for (i = 0; i < blockNumbers - 1; i++) {
@@ -105,14 +106,15 @@ AntiForensics::data AntiForensics::AFSplit(const AntiForensics::data &src,
        return dst;
 }
 
-AntiForensics::data AntiForensics::AFMerge(const AntiForensics::data &src,
-               uint32_t blockSize, uint32_t blockNumbers)
+BinaryData AntiForensics::AFMerge(const BinaryData &src,
+                                                                 uint32_t blockSize,
+                                                                 uint32_t blockNumbers)
 {
        if (blockSize * blockNumbers != src.size())
                throw runtime::Exception("Source size check failed");
 
-       data buf(blockSize, 0);
-       data dst(blockSize, 0);
+       BinaryData buf(blockSize, 0);
+       BinaryData dst(blockSize, 0);
 
        uint32_t i;
        for (i = 0; i < blockNumbers - 1; i++) {
index 9b0dec8..cd17e5a 100644 (file)
@@ -17,7 +17,8 @@
 #define __ANTI_FORENSICS_H__
 
 #include <stdint.h>
-#include <vector>
+
+#include "rmi/common.h"
 
 namespace ode {
 
@@ -25,10 +26,12 @@ class AntiForensics final {
 public:
        AntiForensics() = delete;
 
-       typedef std::vector<unsigned char> data;
-
-       static data AFSplit(const data &src, uint32_t blockSize, uint32_t blockNumbers);
-       static data AFMerge(const data &src, uint32_t blockSize, uint32_t blockNumbers);
+       static BinaryData AFSplit(const BinaryData &src,
+                                                         uint32_t blockSize,
+                                                         uint32_t blockNumbers);
+       static BinaryData AFMerge(const BinaryData &src,
+                                                         uint32_t blockSize,
+                                                         uint32_t blockNumbers);
 };
 
 } // namespace ode
index fdf43ee..3161499 100644 (file)
@@ -32,7 +32,7 @@ const size_t PW_DIGEST_ITERATIONS = 1000;
 const size_t LUKS_STRIPES = 3;
 } // anonymous namespace
 
-EncryptedKey::EncryptedKey(const Key::KeyData& masterKey,
+EncryptedKey::EncryptedKey(const BinaryData& masterKey,
                                                   const std::string& password)
 {
        /*
@@ -58,9 +58,9 @@ EncryptedKey::EncryptedKey(const Key::KeyData& masterKey,
        encrypt(masterKey, password);
 }
 
-Key::KeyData EncryptedKey::decrypt(const std::string& password) const
+BinaryData EncryptedKey::decrypt(const std::string& password) const
 {
-       Key::KeyData pwData(password.begin(), password.end());
+       BinaryData pwData(password.begin(), password.end());
 
        auto derivedPassword = KeyGenerator::PBKDF(pwData,
                                                                                           store.getPasswordSalt(),
@@ -84,13 +84,13 @@ Key::KeyData EncryptedKey::decrypt(const std::string& password) const
                return masterKeyCandidate;
 
        // TODO consider using an exception when we have more than one type
-       return Key::KeyData();
+       return BinaryData();
 }
 
-void EncryptedKey::encrypt(const Key::KeyData& masterKey,
+void EncryptedKey::encrypt(const BinaryData& masterKey,
                                                   const std::string& password)
 {
-       Key::KeyData pwData(password.begin(), password.end());
+       BinaryData pwData(password.begin(), password.end());
 
        auto passwordSalt = KeyGenerator::RNG(store.getPasswordSaltLength());
        store.setPasswordSalt(passwordSalt);
index 9344035..a203cdf 100644 (file)
@@ -32,13 +32,13 @@ namespace ode {
 
 class EncryptedKey {
 public:
-       EncryptedKey(const Key::KeyData& masterKey, const std::string& password);
-       explicit EncryptedKey(const FileFooter::data& footer) : store(footer) {}
+       EncryptedKey(const BinaryData& masterKey, const std::string& password);
+       explicit EncryptedKey(const BinaryData& footer) : store(footer) {}
 
-       void encrypt(const Key::KeyData& masterKey, const std::string& password);
-       Key::KeyData decrypt(const std::string& password) const;
+       void encrypt(const BinaryData& masterKey, const std::string& password);
+       BinaryData decrypt(const std::string& password) const;
 
-       FileFooter::data serialize() const { return store.serialize(); }
+       BinaryData serialize() const { return store.serialize(); }
 private:
        KeyStore store;
 };
index d5f8e38..497461b 100644 (file)
@@ -31,23 +31,23 @@ KeyStore::KeyStore()
 {
 }
 
-KeyStore::KeyStore(const data &raw)
+KeyStore::KeyStore(const BinaryData &raw)
 {
        if (raw.size() <= sizeof(luks)) {
                throw runtime::Exception("the size of raw data is not enough");
        }
 
        ::memcpy(&luks, raw.data(), sizeof(luks));
-       keyMaterial = data(raw.begin() + sizeof(luks), raw.end());
+       keyMaterial = BinaryData(raw.begin() + sizeof(luks), raw.end());
 }
 
 KeyStore::~KeyStore()
 {
 }
 
-const KeyStore::data KeyStore::serialize() const
+const BinaryData KeyStore::serialize() const
 {
-       data result((unsigned char *)&luks,
+       BinaryData result((unsigned char *)&luks,
                                ((unsigned char *)&luks) + sizeof(LUKSHeader));
        result.insert(result.end(), keyMaterial.begin(), keyMaterial.end());
 
@@ -100,7 +100,7 @@ void KeyStore::setMasterKeyLength(unsigned int length)
        luks.keyBytes = ::htonl(length);
 }
 
-void KeyStore::setEncryptedMasterKey(const data key)
+void KeyStore::setEncryptedMasterKey(const BinaryData key)
 {
        keyMaterial = key;
        luks.keyslot[0].active = ::htonl(0x00AC71F3);
@@ -114,15 +114,15 @@ void KeyStore::setEncryptedMasterKey(const data key)
        luks.payloadOffset = ::htonl(luks.payloadOffset);
 }
 
-const KeyStore::data& KeyStore::getEncryptedMasterKey() const
+const BinaryData& KeyStore::getEncryptedMasterKey() const
 {
        return keyMaterial;
 }
 
-const KeyStore::data KeyStore::getPasswordSalt() const
+const BinaryData KeyStore::getPasswordSalt() const
 {
        unsigned char *buf = (unsigned char *)luks.keyslot[0].passwordSalt;
-       return data(buf, buf + sizeof(luks.keyslot[0].passwordSalt));
+       return BinaryData(buf, buf + sizeof(luks.keyslot[0].passwordSalt));
 }
 
 unsigned int KeyStore::getPasswordIteration() const
@@ -135,7 +135,7 @@ unsigned int KeyStore::getPasswordSaltLength() const
        return sizeof(luks.keyslot[0].passwordSalt);
 }
 
-void KeyStore::setPasswordSalt(const data salt)
+void KeyStore::setPasswordSalt(const BinaryData salt)
 {
        ::memcpy(luks.keyslot[0].passwordSalt, salt.data(),
                                sizeof(luks.keyslot[0].passwordSalt));
@@ -146,16 +146,16 @@ void KeyStore::setPasswordIteration(unsigned int count)
        luks.keyslot[0].passwordIteration = ::htonl(count);
 }
 
-const KeyStore::data KeyStore::getMasterKeyDigest() const
+const BinaryData KeyStore::getMasterKeyDigest() const
 {
        unsigned char *buf = (unsigned char *)luks.mkDigest;
-       return data(buf, buf + sizeof(luks.mkDigest));
+       return BinaryData(buf, buf + sizeof(luks.mkDigest));
 }
 
-const KeyStore::data KeyStore::getMasterKeyDigestSalt() const
+const BinaryData KeyStore::getMasterKeyDigestSalt() const
 {
        unsigned char *buf = (unsigned char *)luks.mkDigestSalt;
-       return data(buf, buf + sizeof(luks.mkDigestSalt));
+       return BinaryData(buf, buf + sizeof(luks.mkDigestSalt));
 }
 
 unsigned int KeyStore::getMasterKeyDigestIteration() const
@@ -173,12 +173,12 @@ unsigned int KeyStore::getMasterKeyDigestSaltLength() const
        return sizeof(luks.mkDigestSalt);
 }
 
-void KeyStore::setMasterKeyDigest(const data digest)
+void KeyStore::setMasterKeyDigest(const BinaryData digest)
 {
        ::memcpy(luks.mkDigest, digest.data(), sizeof(luks.mkDigest));
 }
 
-void KeyStore::setMasterKeyDigestSalt(const data salt)
+void KeyStore::setMasterKeyDigestSalt(const BinaryData salt)
 {
        ::memcpy(luks.mkDigestSalt, salt.data(), sizeof(luks.mkDigestSalt));
 }
index e50c129..3f4d57f 100644 (file)
 #ifndef __KEY_STORE_H__
 #define __KEY_STORE_H__
 
-#include <vector>
 #include <string>
 
 #include <klay/filesystem.h>
 
 #include "luks.h"
+#include "rmi/common.h"
 
 namespace ode {
 
 class KeyStore final {
 public:
-       typedef std::vector<unsigned char> data;
-
        KeyStore();
-       KeyStore(const data &raw);
+       KeyStore(const BinaryData &raw);
        KeyStore(const KeyStore &) = delete;
        KeyStore(KeyStore &&) = delete;
        ~KeyStore();
@@ -39,7 +37,7 @@ public:
        KeyStore &operator=(const KeyStore &) = delete;
        KeyStore &operator=(KeyStore &&) = delete;
 
-       const data serialize() const;
+       const BinaryData serialize() const;
 
        const std::string getCipherName() const;
        const std::string getCipherMode() const;
@@ -51,27 +49,27 @@ public:
        unsigned int getMasterKeyLength() const;
        void setMasterKeyLength(unsigned int length);
 
-       const data& getEncryptedMasterKey() const;
-       void setEncryptedMasterKey(const data salt);
+       const BinaryData& getEncryptedMasterKey() const;
+       void setEncryptedMasterKey(const BinaryData salt);
 
-       const data getPasswordSalt() const;
+       const BinaryData getPasswordSalt() const;
        unsigned int getPasswordIteration() const;
        unsigned int getPasswordSaltLength() const;
-       void setPasswordSalt(const data salt);
+       void setPasswordSalt(const BinaryData salt);
        void setPasswordIteration(unsigned int count);
 
-       const data getMasterKeyDigest() const;
-       const data getMasterKeyDigestSalt() const;
+       const BinaryData getMasterKeyDigest() const;
+       const BinaryData getMasterKeyDigestSalt() const;
        unsigned int getMasterKeyDigestLength() const;
        unsigned int getMasterKeyDigestSaltLength() const;
        unsigned int getMasterKeyDigestIteration() const;
-       void setMasterKeyDigest(const data digest);
-       void setMasterKeyDigestSalt(const data salt);
+       void setMasterKeyDigest(const BinaryData digest);
+       void setMasterKeyDigestSalt(const BinaryData salt);
        void setMasterKeyDigestIteration(unsigned int count);
 
 private:
        LUKSHeader luks;
-       data keyMaterial;
+       BinaryData keyMaterial;
 };
 
 } // namespace ode
index 7cf3cec..65ec714 100644 (file)
@@ -70,14 +70,14 @@ int KeyServer::init(const std::string& dev,
                                        const std::string& password,
                                        int params)
 {
-       KeyData dummy;
+       BinaryData dummy;
        return initAndGet(dev, password, params, dummy);
 }
 
 int KeyServer::initAndGet(const std::string& dev,
                                                  const std::string& password,
                                                  int params,
-                                                 KeyData& masterKey)
+                                                 BinaryData& masterKey)
 {
        if (dev.empty() || password.empty() || KEY_SIZE.find(params) == KEY_SIZE.end())
                return error::InvalidParameter;
@@ -150,7 +150,7 @@ int KeyServer::verifyPassword(const std::string& dev,
 
 int KeyServer::get(const std::string& dev,
                                   const std::string& password,
-                                  KeyData& masterKey) const
+                                  BinaryData& masterKey) const
 {
        if (dev.empty() || password.empty())
                return error::InvalidParameter;
index 763282d..db77521 100644 (file)
@@ -34,7 +34,7 @@ public:
        int initAndGet(const std::string& dev,
                                   const std::string& password,
                                   int params,
-                                  KeyData& masterKey);
+                                  BinaryData& masterKey);
        int remove(const std::string& dev, const std::string& password);
        int changePassword(const std::string& dev,
                                           const std::string& curPW,
@@ -42,7 +42,7 @@ public:
        int verifyPassword(const std::string& dev, const std::string& password);
        int get(const std::string& dev,
                        const std::string& password,
-                       KeyData& masterKey) const;
+                       BinaryData& masterKey) const;
        void removePassword(const std::string& dev);
 
 private:
index 8fcbeeb..ffbec87 100644 (file)
@@ -105,7 +105,7 @@ int LuksServer::format(bool sync,
                                           const std::string& password)
 {
        return execute(sync, Luks::Format, [=](){
-               Key::KeyData key;
+               BinaryData key;
                int ret = keyServer.initAndGet(device, password, Key::DEFAULT_512BIT, key);
                if (ret != error::None)
                        return ret;
@@ -124,7 +124,7 @@ int LuksServer::open(bool sync,
        return execute(sync, Luks::Open, [=](){
                CryptsetupEngine engine(device);
 
-               Key::KeyData key;
+               BinaryData key;
                int ret = keyServer.get(device, password, key);
                if (ret != error::None)
                        return ret;
index 0676337..bea13da 100644 (file)
 
 #include <string>
 #include <algorithm>
-#include <vector>
 #include <memory>
 #include <mutex>
 
 #include <klay/filesystem.h>
 #include <klay/exception.h>
+#include <rmi/common.h>
 
 namespace ode {
 
@@ -58,11 +58,9 @@ public:
 
        static KeyStoragePlugin& Instance();
 
-       typedef std::vector<unsigned char> Data;
-
-       Data store(const Data& key);
-       Data load(const Data& token);
-       void remove(const Data& token);
+       BinaryData store(const BinaryData& key);
+       BinaryData load(const BinaryData& token);
+       void remove(const BinaryData& token);
 
 private:
        void* so;
@@ -113,7 +111,7 @@ KeyStoragePlugin::~KeyStoragePlugin()
        ::dlclose(so);
 }
 
-KeyStoragePlugin::Data KeyStoragePlugin::store(const KeyStoragePlugin::Data& key)
+BinaryData KeyStoragePlugin::store(const BinaryData& key)
 {
        unsigned char* token = NULL;
        size_t token_len = 0;
@@ -122,12 +120,12 @@ KeyStoragePlugin::Data KeyStoragePlugin::store(const KeyStoragePlugin::Data& key
                throw runtime::Exception(std::string("Storing the key failed with ") +
                                                                 std::to_string(ret));
 
-       Data tokenVector(token, token + token_len);
+       BinaryData tokenVector(token, token + token_len);
        free(token);
        return tokenVector;
 }
 
-KeyStoragePlugin::Data KeyStoragePlugin::load(const KeyStoragePlugin::Data& token)
+BinaryData KeyStoragePlugin::load(const BinaryData& token)
 {
        unsigned char* key = NULL;
        size_t key_len = 0;
@@ -136,12 +134,12 @@ KeyStoragePlugin::Data KeyStoragePlugin::load(const KeyStoragePlugin::Data& toke
                throw runtime::Exception(std::string("Loading the key failed with ") +
                                                                 std::to_string(ret));
 
-       Data keyVector(key, key + key_len);
+       BinaryData keyVector(key, key + key_len);
        free(key);
        return keyVector;
 }
 
-void KeyStoragePlugin::remove(const KeyStoragePlugin::Data& token)
+void KeyStoragePlugin::remove(const BinaryData& token)
 {
        int ret = removeFn(token.data(), token.size());
        if (ret != 0)
@@ -157,7 +155,7 @@ std::string getTokenFileName(const std::string &device)
        return std::string("/opt/etc/.ode_token") + filename;
 }
 
-void readToken(runtime::File &file, KeyStoragePlugin::Data& token)
+void readToken(runtime::File &file, BinaryData& token)
 {
        size_t tokenSize;
 
@@ -170,7 +168,7 @@ void readToken(runtime::File &file, KeyStoragePlugin::Data& token)
        file.close();
 }
 
-void writeToken(runtime::File &file, const KeyStoragePlugin::Data& token)
+void writeToken(runtime::File &file, const BinaryData& token)
 {
        size_t tokenSize(token.size());
 
@@ -186,7 +184,7 @@ void writeToken(runtime::File &file, const KeyStoragePlugin::Data& token)
 
 namespace UpgradeSupport {
 
-void storeMasterKey(const std::string &device, const KeyStoragePlugin::Data& key)
+void storeMasterKey(const std::string &device, const BinaryData& key)
 {
        std::lock_guard<std::mutex> lock(opGuard);
 
@@ -198,9 +196,9 @@ void storeMasterKey(const std::string &device, const KeyStoragePlugin::Data& key
        writeToken(file, token);
 }
 
-KeyStoragePlugin::Data loadMasterKey(const std::string &device)
+BinaryData loadMasterKey(const std::string &device)
 {
-       KeyStoragePlugin::Data token;
+       BinaryData token;
 
        std::lock_guard<std::mutex> lock(opGuard);
 
@@ -213,7 +211,7 @@ KeyStoragePlugin::Data loadMasterKey(const std::string &device)
 
 void removeMasterKey(const std::string &device)
 {
-       KeyStoragePlugin::Data token;
+       BinaryData token;
 
        std::lock_guard<std::mutex> lock(opGuard);
 
index 509576f..4a029b8 100644 (file)
 
 #include <string>
 
-#include <rmi/key.h>
+#include "rmi/common.h"
 
 namespace ode {
 
 namespace UpgradeSupport {
 
-void storeMasterKey(const std::string &device, const Key::KeyData& key);
-Key::KeyData loadMasterKey(const std::string &device);
+void storeMasterKey(const std::string &device, const BinaryData& key);
+BinaryData loadMasterKey(const std::string &device);
 void removeMasterKey(const std::string &device);
 
 } // namespace UpgradeSupport
index 72650e9..d0d093c 100755 (executable)
@@ -40,7 +40,7 @@ PKG_CHECK_MODULES(TEST_DEPS REQUIRED  klay
                                                                                vconf
 )
 
-INCLUDE_DIRECTORIES(SYSTEM ${TEST_DEPS_INCLUDE_DIRS} ../server)
+INCLUDE_DIRECTORIES(SYSTEM ${TEST_DEPS_INCLUDE_DIRS} ../server ../)
 
 TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${TEST_DEPS_LIBRARIES})
 
index 10a57cc..ffd6c44 100644 (file)
 #include <klay/testbench.h>
 #include <klay/process.h>
 
-#include "../server/key-manager/anti-forensics.h"
+#include "server/key-manager/anti-forensics.h"
 
 #define LUKS_STRIPES 3 // this constans will be placed in key-manager
 
+using namespace ode;
+
 TESTCASE(AntiForensics)
 {
        try {
-               typedef ode::AntiForensics::data data;
-
                std::string Key = "Master_Key";
-               data MasterKey(Key.begin(), Key.end());
-               data SplittedMasterKey = ode::AntiForensics::AFSplit(MasterKey, MasterKey.size(), LUKS_STRIPES);
-               data UnSplittedMasterKey = ode::AntiForensics::AFMerge(SplittedMasterKey, MasterKey.size(), LUKS_STRIPES);
+               BinaryData MasterKey(Key.begin(), Key.end());
+               BinaryData SplittedMasterKey = AntiForensics::AFSplit(MasterKey, MasterKey.size(), LUKS_STRIPES);
+               BinaryData UnSplittedMasterKey = AntiForensics::AFMerge(SplittedMasterKey, MasterKey.size(), LUKS_STRIPES);
 
                // check size
                TEST_EXPECT(MasterKey.size(), UnSplittedMasterKey.size());
@@ -61,4 +61,4 @@ TESTCASE(AntiForensics)
        } catch (...) {
                TEST_FAIL("UNKNOWN ERROR");
        }
-}
\ No newline at end of file
+}
index d355291..74c207f 100644 (file)
 #include <klay/process.h>
 
 #include "../server/engine/encryption/dmcrypt-engine.cpp"
+#include "rmi/common.h"
 
 #define TEST_USERDATA_NAME  "userdata"
 #define TEST_USERDATA_PATH     "/opt/usr"
 
+using namespace ode;
+
 namespace {
 
-ode::ProgressBar progress([](int v) {});
+ProgressBar progress([](int v) {});
 
 }
 
 TESTCASE(DMCryptConvertKeyToHexAsciiSingle16byte)
 {
        try {
-               const ode::DMCryptEngine::data master_key = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
-               const std::string master_key_ascii = ode::convertToHex(master_key);
+               const BinaryData master_key = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
+               const std::string master_key_ascii = convertToHex(master_key);
                // the string master_key_ascii is equal to "000102030405060708090a0b0c0d0e0f" after conversion
 
-               ode::DMCryptEngine::data answer = {0x30, 0x30, 0x30, 0x31, 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x30, 0x37,
-                                                                                  0x30, 0x38, 0x30, 0x39, 0x30, 0x61, 0x30, 0x62, 0x30, 0x63, 0x30, 0x64, 0x30, 0x65, 0x30, 0x66
-                                                                                 };
+               BinaryData answer = {0x30, 0x30, 0x30, 0x31, 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x30, 0x37,
+                                                        0x30, 0x38, 0x30, 0x39, 0x30, 0x61, 0x30, 0x62, 0x30, 0x63, 0x30, 0x64, 0x30, 0x65, 0x30, 0x66
+                                                       };
                // the answer is a set of hex codes of ASCII symbols in master_key_ascii string, e.g. hex(a) = 0x61
 
                // check
@@ -67,14 +70,11 @@ TESTCASE(DMCryptConvertKeyToHexAsciiSingle16byte)
 TESTCASE(DMCryptConvertKeyToHexAsciiDouble16byte)
 {
        try {
-               const ode::DMCryptEngine::data master_key = {0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87, 0x78, 0x69, 0x5A, 0x4B, 0x3C, 0x2D, 0x1E, 0x0F};
-               const std::string master_key_ascii = ode::convertToHex(master_key);
-               // the string master_key_ascii is equal to "f0e1d2c3b4a5968778695a4b3c2d1e0f" after conversion
-
-               ode::DMCryptEngine::data answer = {0x66, 0x30, 0x65, 0x31, 0x64, 0x32, 0x63, 0x33, 0x62, 0x34, 0x61, 0x35, 0x39, 0x36, 0x38, 0x37,
-                                                                                  0x37, 0x38, 0x36, 0x39, 0x35, 0x61, 0x34, 0x62, 0x33, 0x63, 0x32, 0x64, 0x31, 0x65, 0x30, 0x66
-                                                                                 };
-               // the answer is a set of hex codes of ASCII symbols in master_key_ascii string, e.g. hex(a) = 0x61
+               const BinaryData master_key = {0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87, 0x78, 0x69, 0x5A, 0x4B, 0x3C, 0x2D, 0x1E, 0x0F};
+               BinaryData answer = {0x46, 0x30, 0x45, 0x31, 0x44, 0x32, 0x43, 0x33, 0x42, 0x34, 0x41, 0x35, 0x39, 0x36, 0x38, 0x37,
+                                                        0x37, 0x38, 0x36, 0x39, 0x35, 0x41, 0x34, 0x42, 0x33, 0x43, 0x32, 0x44, 0x31, 0x45, 0x30, 0x46
+                                                       };
+               const std::string master_key_ascii = convertToHex(master_key);
 
                // check
                for (unsigned int i = 0; i < master_key.size() * 2; i++)
@@ -92,10 +92,11 @@ TESTCASE(DMCryptSanitizeKey)
                // -- expected: cause exception
                {
                        const std::string keystring = "SMALLER_THAN_32BYTES______";
-                       const ode::DMCryptEngine::data shortKey(keystring.begin(), keystring.end());
+                       const BinaryData keySmall32bit(keystring.begin(), keystring.end());
                        try {
-                               ode::sanitizeKey(shortKey);
-                               TEST_FAIL("small key should cause an exception");
+                               sanitizeKey(keySmall32bit);
+                               //
+                               TEST_FAIL("small key should be cause exception");
                        } catch (runtime::Exception &e) {
                                // expected result: it's good
                        }
@@ -105,8 +106,8 @@ TESTCASE(DMCryptSanitizeKey)
                // -- expected: returned key data is same with 32 elements from first of big key
                {
                        const std::string keystring = "01020304050607080910111213141516";
-                       const ode::DMCryptEngine::data key32bit(keystring.begin(), keystring.end());
-                       const ode::DMCryptEngine::data sanitized_key = ode::sanitizeKey(key32bit);
+                       const BinaryData key32bit(keystring.begin(), keystring.end());
+                       const BinaryData sanitized_key = sanitizeKey(key32bit);
 
                        // check size
                        TEST_EXPECT(sanitized_key.size(), key32bit.size());
@@ -121,8 +122,8 @@ TESTCASE(DMCryptSanitizeKey)
                // -- expected: returned key data is same with 32 elements from first of big key
                {
                        const std::string keystring = "01020304050607080910111213141516_MAKES_BIG";
-                       const ode::DMCryptEngine::data keyBig(keystring.begin(), keystring.end());
-                       const ode::DMCryptEngine::data sanitized_key = ode::sanitizeKey(keyBig);
+                       const BinaryData keyBig(keystring.begin(), keystring.end());
+                       const BinaryData sanitized_key = sanitizeKey(keyBig);
 
                        // check size
                        TEST_EXPECT(sanitized_key.size(), (unsigned)DM_KEY_MIN_LEN_BYTE);
@@ -141,7 +142,7 @@ TESTCASE(DMCryptSanitizeKey)
 TESTCASE(DMCryptGetPathTest)
 {
        try {
-               ode::DMCryptEngine engine("/dev/mmcblk0p1", TEST_USERDATA_PATH, progress);
+               DMCryptEngine engine("/dev/mmcblk0p1", TEST_USERDATA_PATH, progress);
                if (engine.getSource() != "/dev/mmcblk0p1") {
                        throw runtime::Exception("Source doen't match");
                }