diff --git a/implementations/encrypted/EncryptionKey.cpp b/implementations/encrypted/EncryptionKey.cpp new file mode 100644 index 00000000..8b58adc5 --- /dev/null +++ b/implementations/encrypted/EncryptionKey.cpp @@ -0,0 +1,14 @@ +#include "EncryptionKey.h" + +namespace blockstore { +namespace encrypted { + +EncryptionKey::EncryptionKey() { + +} + +EncryptionKey::~EncryptionKey() { +} + +} +} diff --git a/implementations/encrypted/EncryptionKey.h b/implementations/encrypted/EncryptionKey.h new file mode 100644 index 00000000..730c525b --- /dev/null +++ b/implementations/encrypted/EncryptionKey.h @@ -0,0 +1,19 @@ +#pragma once +#ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTIONKEY_H_ +#define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTIONKEY_H_ + +namespace blockstore { +namespace encrypted { + +class EncryptionKey { +public: + EncryptionKey(); + static EncryptionKey +private: + byte key[32]; +}; + +} +} + +#endif diff --git a/interface/helpers/BlockStoreWithRandomKeys.cpp b/interface/helpers/BlockStoreWithRandomKeys.cpp index 14fa6a19..d5d81885 100644 --- a/interface/helpers/BlockStoreWithRandomKeys.cpp +++ b/interface/helpers/BlockStoreWithRandomKeys.cpp @@ -14,7 +14,7 @@ unique_ptr BlockStoreWithRandomKeys::create(size_t size) { } unique_ptr BlockStoreWithRandomKeys::tryCreate(size_t size) { - Key key = Key::CreateRandomKey(); + Key key = Key::CreateRandom(); return create(key, size); } diff --git a/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp b/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp index 5e7a5c76..2b4d926f 100644 --- a/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp +++ b/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp @@ -30,7 +30,7 @@ public: class BlockMock: public Block { public: - BlockMock(): Block(Key::CreateRandomKey()) {} + BlockMock(): Block(Key::CreateRandom()) {} MOCK_CONST_METHOD0(data, const void*()); MOCK_METHOD3(write, void(const void*, uint64_t, uint64_t)); MOCK_METHOD0(flush, void()); @@ -62,7 +62,7 @@ TEST_F(BlockStoreWithRandomKeysTest, SizeIsPassedThrough1024) { TEST_F(BlockStoreWithRandomKeysTest, KeyHasCorrectSize) { EXPECT_CALL(blockStoreMock, do_create(_, _)).WillOnce(Invoke([](const Key &key, size_t) { - EXPECT_EQ(Key::KEYLENGTH_STRING, key.ToString().size()); + EXPECT_EQ(Key::STRING_LENGTH, key.ToString().size()); return new BlockMock; })); diff --git a/test/utils/KeyTest.cpp b/test/utils/KeyTest.cpp index e54f4157..b1f6f82e 100644 --- a/test/utils/KeyTest.cpp +++ b/test/utils/KeyTest.cpp @@ -20,7 +20,7 @@ public: const DataBlockFixture KEY3_AS_BINARY; const DataBlockFixture KEY4_AS_BINARY; - KeyTest() : KEY3_AS_BINARY(Key::KEYLENGTH_BINARY, 1), KEY4_AS_BINARY(Key::KEYLENGTH_BINARY, 2) {} + KeyTest() : KEY3_AS_BINARY(Key::BINARY_LENGTH, 1), KEY4_AS_BINARY(Key::BINARY_LENGTH, 2) {} void EXPECT_DATA_EQ(const DataBlockFixture &expected, const Data &actual) { EXPECT_EQ(expected.size(), actual.size()); @@ -29,12 +29,12 @@ public: }; TEST_F(KeyTest, CanGenerateRandomKeysWithoutCrashing) { - Key result = Key::CreateRandomKey(); + Key result = Key::CreateRandom(); } TEST_F(KeyTest, CreatedRandomKeysHaveCorrectLength) { - Key key = Key::CreateRandomKey(); - EXPECT_EQ(Key::KEYLENGTH_STRING, key.ToString().size()); + Key key = Key::CreateRandom(); + EXPECT_EQ(Key::STRING_LENGTH, key.ToString().size()); } TEST_F(KeyTest, EqualsTrue) { @@ -88,20 +88,20 @@ public: static const DataBlockFixture VALUE1; static const DataBlockFixture VALUE2; }; -const DataBlockFixture KeyTestWithBinaryKeyParam::VALUE1(Key::KEYLENGTH_BINARY, 3); -const DataBlockFixture KeyTestWithBinaryKeyParam::VALUE2(Key::KEYLENGTH_BINARY, 4); +const DataBlockFixture KeyTestWithBinaryKeyParam::VALUE1(Key::BINARY_LENGTH, 3); +const DataBlockFixture KeyTestWithBinaryKeyParam::VALUE2(Key::BINARY_LENGTH, 4); INSTANTIATE_TEST_CASE_P(KeyTestWithBinaryKeyParam, KeyTestWithBinaryKeyParam, Values(&KeyTestWithBinaryKeyParam::VALUE1, &KeyTestWithBinaryKeyParam::VALUE2)); TEST_P(KeyTestWithBinaryKeyParam, FromAndToBinary) { Key key = Key::FromBinary((uint8_t*)GetParam()->data()); - Data keydata(Key::KEYLENGTH_BINARY); + Data keydata(Key::BINARY_LENGTH); key.ToBinary(keydata.data()); EXPECT_DATA_EQ(*GetParam(), keydata); } TEST_P(KeyTestWithBinaryKeyParam, ToAndFromBinary) { Key key = Key::FromBinary((uint8_t*)GetParam()->data()); - Data stored(Key::KEYLENGTH_BINARY); + Data stored(Key::BINARY_LENGTH); key.ToBinary(stored.data()); Key loaded = Key::FromBinary(stored.data()); EXPECT_EQ(key, loaded); @@ -138,5 +138,5 @@ TEST_F(KeyTest, AssignmentDoesntChangeSource) { // This tests that a Key object is very lightweight // (we will often pass keys around) TEST_F(KeyTest, KeyIsLightweightObject) { - EXPECT_EQ(Key::KEYLENGTH_BINARY, sizeof(Key)); + EXPECT_EQ(Key::BINARY_LENGTH, sizeof(Key)); } diff --git a/utils/FixedSizeData.h b/utils/FixedSizeData.h new file mode 100644 index 00000000..9c62b5e4 --- /dev/null +++ b/utils/FixedSizeData.h @@ -0,0 +1,121 @@ +#pragma once +#ifndef BLOCKSTORE_UTILS_data_H_ +#define BLOCKSTORE_UTILS_data_H_ + +#include +#include +#include +#include + +namespace blockstore { + +template +class FixedSizeData { +public: + //Non-virtual destructor because we want objects to be small + ~FixedSizeData() {} + + static constexpr unsigned int BINARY_LENGTH = SIZE; + static constexpr unsigned int STRING_LENGTH = 2 * BINARY_LENGTH; // Hex encoding + + static FixedSizeData CreateRandom(); + + static FixedSizeData FromString(const std::string &data); + std::string ToString() const; + + static FixedSizeData FromBinary(const void *source); + void ToBinary(void *target) const; + + const unsigned char *data() const; + +private: + FixedSizeData() {} + + static CryptoPP::AutoSeededRandomPool &RandomPool(); + + unsigned char _data[BINARY_LENGTH]; +}; + +template bool operator==(const FixedSizeData &lhs, const FixedSizeData &rhs); +template bool operator!=(const FixedSizeData &lhs, const FixedSizeData &rhs); + +//operator< is defined, so that FixedSizeData objects can be used in std::map and std::set +template bool operator<(const FixedSizeData &lhs, const FixedSizeData &rhs); + +// ----- Implementation ----- + +template constexpr unsigned int FixedSizeData::BINARY_LENGTH; +template constexpr unsigned int FixedSizeData::STRING_LENGTH; + +template +CryptoPP::AutoSeededRandomPool &FixedSizeData::RandomPool() { + static CryptoPP::AutoSeededRandomPool singleton; + return singleton; +} + +template +FixedSizeData FixedSizeData::CreateRandom() { + FixedSizeData result; + RandomPool().GenerateBlock(result._data, BINARY_LENGTH); + return result; +} + +template +FixedSizeData FixedSizeData::FromString(const std::string &data) { + assert(data.size() == STRING_LENGTH); + FixedSizeData result; + CryptoPP::StringSource(data, true, + new CryptoPP::HexDecoder( + new CryptoPP::ArraySink(result._data, BINARY_LENGTH) + ) + ); + return result; +} + +template +std::string FixedSizeData::ToString() const { + std::string result; + CryptoPP::ArraySource(_data, BINARY_LENGTH, true, + new CryptoPP::HexEncoder( + new CryptoPP::StringSink(result) + ) + ); + assert(result.size() == STRING_LENGTH); + return result; +} + +template +const unsigned char *FixedSizeData::data() const { + return _data; +} + +template +void FixedSizeData::ToBinary(void *target) const { + std::memcpy(target, _data, BINARY_LENGTH); +} + +template +FixedSizeData FixedSizeData::FromBinary(const void *source) { + FixedSizeData result; + std::memcpy(result._data, source, BINARY_LENGTH); + return result; +} + +template +bool operator==(const FixedSizeData &lhs, const FixedSizeData &rhs) { + return 0 == std::memcmp(lhs.data(), rhs.data(), FixedSizeData::BINARY_LENGTH); +} + +template +bool operator!=(const FixedSizeData &lhs, const FixedSizeData &rhs) { + return !operator==(lhs, rhs); +} + +template +bool operator<(const FixedSizeData &lhs, const FixedSizeData &rhs) { + return 0 > std::memcmp(lhs.data(), rhs.data(), FixedSizeData::BINARY_LENGTH); +} + +} + +#endif diff --git a/utils/Key.cpp b/utils/Key.cpp index 038bb004..f22451aa 100644 --- a/utils/Key.cpp +++ b/utils/Key.cpp @@ -1,83 +1 @@ -#include -#include #include "Key.h" - -#include - -using CryptoPP::ArraySource; -using CryptoPP::ArraySink; -using CryptoPP::StringSink; -using CryptoPP::StringSource; -using CryptoPP::HexEncoder; -using CryptoPP::HexDecoder; -using CryptoPP::AutoSeededRandomPool; - -using std::string; - -namespace blockstore { - -constexpr unsigned int Key::KEYLENGTH_BINARY; -constexpr unsigned int Key::KEYLENGTH_STRING; - -Key::Key() { -} - -Key::~Key() { -} - -AutoSeededRandomPool &RandomPool() { - static AutoSeededRandomPool singleton; - return singleton; -} - -Key Key::CreateRandomKey() { - Key result; - RandomPool().GenerateBlock(result._key, KEYLENGTH_BINARY); - return result; -} - -Key Key::FromString(const std::string &key) { - assert(key.size() == KEYLENGTH_STRING); - Key result; - StringSource(key, true, - new HexDecoder(new ArraySink(result._key, KEYLENGTH_BINARY)) - ); - return result; -} - -string Key::ToString() const { - string result; - ArraySource(_key, KEYLENGTH_BINARY, true, - new HexEncoder(new StringSink(result)) - ); - assert(result.size() == KEYLENGTH_STRING); - return result; -} - -const unsigned char *Key::data() const { - return _key; -} - -bool operator==(const Key &lhs, const Key &rhs) { - return 0 == std::memcmp(lhs.data(), rhs.data(), Key::KEYLENGTH_BINARY); -} - -bool operator!=(const Key &lhs, const Key &rhs) { - return !operator==(lhs, rhs); -} - -bool operator<(const Key &lhs, const Key &rhs) { - return 0 > std::memcmp(lhs.data(), rhs.data(), Key::KEYLENGTH_BINARY); -} - -void Key::ToBinary(void *target) const { - std::memcpy(target, _key, KEYLENGTH_BINARY); -} - -Key Key::FromBinary(const void *source) { - Key result; - std::memcpy(result._key, source, KEYLENGTH_BINARY); - return result; -} - -} diff --git a/utils/Key.h b/utils/Key.h index 072e8466..45ea411f 100644 --- a/utils/Key.h +++ b/utils/Key.h @@ -3,39 +3,12 @@ #define BLOCKSTORE_UTILS_KEY_H_ #include +#include "FixedSizeData.h" namespace blockstore { // A key here is NOT a key for encryption, but a key as used in key->value mappings ("access handle for a block"). -class Key { -public: - //Non-virtual destructor because we want Key objects to be small - ~Key(); - - static constexpr unsigned int KEYLENGTH_BINARY = 16; - static constexpr unsigned int KEYLENGTH_STRING = 2 * KEYLENGTH_BINARY; // Hex encoding - - static Key CreateRandomKey(); - - static Key FromString(const std::string &key); - std::string ToString() const; - - static Key FromBinary(const void *source); - void ToBinary(void *target) const; - - const unsigned char *data() const; - -private: - Key(); - - unsigned char _key[KEYLENGTH_BINARY]; -}; - -bool operator==(const Key &lhs, const Key &rhs); -bool operator!=(const Key &lhs, const Key &rhs); - -//operator< is defined, so that Key objects can be used in std::map and std::set -bool operator<(const Key &lhs, const Key &rhs); +using Key = FixedSizeData<16>; }