From 62549eeae65cf058de1ad15163b58ea3f46c4428 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Thu, 22 Oct 2015 17:41:43 +0200 Subject: [PATCH] Adapt to new cpputils::Random interface --- implementations/caching/cache/Cache.h | 1 + implementations/caching/cache/QueueMap.h | 5 +++++ implementations/encrypted/ciphers/CFB_Cipher.h | 12 +++++++++++- implementations/encrypted/ciphers/Cipher.h | 7 ++++--- implementations/encrypted/ciphers/GCM_Cipher.h | 12 +++++++++++- interface/helpers/BlockStoreWithRandomKeys.h | 3 ++- .../encrypted/testutils/FakeAuthenticatedCipher.h | 11 ++++++++--- .../helpers/BlockStoreWithRandomKeysTest.cpp | 2 +- utils/Key.h | 6 +++--- 9 files changed, 46 insertions(+), 13 deletions(-) diff --git a/implementations/caching/cache/Cache.h b/implementations/caching/cache/Cache.h index 22408482..0c06b73c 100644 --- a/implementations/caching/cache/Cache.h +++ b/implementations/caching/cache/Cache.h @@ -17,6 +17,7 @@ namespace caching { template class Cache { public: + //TODO Current MAX_LIFETIME_SEC only considers time since the element was last pushed to the Cache. Also insert a real MAX_LIFETIME_SEC that forces resync of entries that have been pushed/popped often (e.g. the root blob) //TODO Experiment with good values static constexpr double PURGE_LIFETIME_SEC = 0.5; //When an entry has this age, it will be purged from the cache static constexpr double PURGE_INTERVAL = 0.5; // With this interval, we check for entries to purge diff --git a/implementations/caching/cache/QueueMap.h b/implementations/caching/cache/QueueMap.h index e3575cfd..4851c192 100644 --- a/implementations/caching/cache/QueueMap.h +++ b/implementations/caching/cache/QueueMap.h @@ -12,6 +12,11 @@ namespace blockstore { namespace caching { +//TODO FreeList for performance +//TODO Single linked list with pointer to last element (for insertion) should be enough for a queue. No double linked list needed. +// But then, popping arbitrary elements needs to be rewritten so that _removeFromQueue() is _removeSuccessorFromQueue() +// and the map doesn't store the element itself, but its predecessor. That is, popping might be a bit slower. Test with experiments! + // A class that is a queue and a map at the same time. We could also see it as an addressable queue. template class QueueMap { diff --git a/implementations/encrypted/ciphers/CFB_Cipher.h b/implementations/encrypted/ciphers/CFB_Cipher.h index 4189c282..470004d1 100644 --- a/implementations/encrypted/ciphers/CFB_Cipher.h +++ b/implementations/encrypted/ciphers/CFB_Cipher.h @@ -4,6 +4,7 @@ #include #include +#include #include #include #include "Cipher.h" @@ -18,6 +19,15 @@ public: using EncryptionKey = cpputils::FixedSizeData; + static EncryptionKey CreateKey() { + return cpputils::Random::OSRandom().getFixedSize(); + } + + // Used in test cases for fast key creation + static EncryptionKey CreatePseudoRandomKey() { + return cpputils::Random::PseudoRandom().getFixedSize(); + } + static constexpr unsigned int ciphertextSize(unsigned int plaintextBlockSize) { return plaintextBlockSize + IV_SIZE; } @@ -35,7 +45,7 @@ private: template cpputils::Data CFB_Cipher::encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { - auto iv = cpputils::FixedSizeData::CreatePseudoRandom(); + cpputils::FixedSizeData iv = cpputils::Random::PseudoRandom().getFixedSize(); auto encryption = typename CryptoPP::CFB_Mode::Encryption(encKey.data(), encKey.BINARY_LENGTH, iv.data()); cpputils::Data ciphertext(ciphertextSize(plaintextSize)); std::memcpy(ciphertext.data(), iv.data(), IV_SIZE); diff --git a/implementations/encrypted/ciphers/Cipher.h b/implementations/encrypted/ciphers/Cipher.h index 65ec3554..3a3ec9e1 100644 --- a/implementations/encrypted/ciphers/Cipher.h +++ b/implementations/encrypted/ciphers/Cipher.h @@ -15,9 +15,10 @@ public: BOOST_CONCEPT_USAGE(CipherConcept) { same_type(UINT32_C(0), X::ciphertextSize(UINT32_C(5))); same_type(UINT32_C(0), X::plaintextSize(UINT32_C(5))); - typename X::EncryptionKey key = X::EncryptionKey::CreateOSRandom(); - same_type(cpputils::Data(0), X::encrypt((uint8_t*)nullptr, UINT32_C(0), key)); - same_type(boost::optional(cpputils::Data(0)), X::decrypt((uint8_t*)nullptr, UINT32_C(0), key)); + typename X::EncryptionKey key1 = X::CreateKey(); + typename X::EncryptionKey key2 = X::CreatePseudoRandomKey(); + same_type(cpputils::Data(0), X::encrypt((uint8_t*)nullptr, UINT32_C(0), key1)); + same_type(boost::optional(cpputils::Data(0)), X::decrypt((uint8_t*)nullptr, UINT32_C(0), key2)); } private: diff --git a/implementations/encrypted/ciphers/GCM_Cipher.h b/implementations/encrypted/ciphers/GCM_Cipher.h index 3bc7418c..cd5b0b1e 100644 --- a/implementations/encrypted/ciphers/GCM_Cipher.h +++ b/implementations/encrypted/ciphers/GCM_Cipher.h @@ -4,6 +4,7 @@ #include #include +#include #include #include "Cipher.h" @@ -17,6 +18,15 @@ public: using EncryptionKey = cpputils::FixedSizeData; + static EncryptionKey CreateKey() { + return cpputils::Random::OSRandom().getFixedSize(); + } + + // Used in test cases for fast key creation + static EncryptionKey CreatePseudoRandomKey() { + return cpputils::Random::PseudoRandom().getFixedSize(); + } + static constexpr unsigned int ciphertextSize(unsigned int plaintextBlockSize) { return plaintextBlockSize + IV_SIZE + TAG_SIZE; } @@ -35,7 +45,7 @@ private: template cpputils::Data GCM_Cipher::encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { - auto iv = cpputils::FixedSizeData::CreatePseudoRandom(); + cpputils::FixedSizeData iv = cpputils::Random::PseudoRandom().getFixedSize(); typename CryptoPP::GCM::Encryption encryption; encryption.SetKeyWithIV(encKey.data(), encKey.BINARY_LENGTH, iv.data(), IV_SIZE); cpputils::Data ciphertext(ciphertextSize(plaintextSize)); diff --git a/interface/helpers/BlockStoreWithRandomKeys.h b/interface/helpers/BlockStoreWithRandomKeys.h index a90edbce..9230cabd 100644 --- a/interface/helpers/BlockStoreWithRandomKeys.h +++ b/interface/helpers/BlockStoreWithRandomKeys.h @@ -4,6 +4,7 @@ #include "../BlockStore.h" #include "../Block.h" +#include namespace blockstore { @@ -13,7 +14,7 @@ namespace blockstore { class BlockStoreWithRandomKeys: public BlockStore { public: Key createKey() final { - return Key::CreatePseudoRandom(); + return cpputils::Random::PseudoRandom().getFixedSize(); } }; diff --git a/test/implementations/encrypted/testutils/FakeAuthenticatedCipher.h b/test/implementations/encrypted/testutils/FakeAuthenticatedCipher.h index 6ece03d9..73519029 100644 --- a/test/implementations/encrypted/testutils/FakeAuthenticatedCipher.h +++ b/test/implementations/encrypted/testutils/FakeAuthenticatedCipher.h @@ -6,9 +6,6 @@ #include struct FakeKey { - static FakeKey CreateOSRandom() { - return FakeKey{(uint8_t)rand()}; - } static FakeKey FromBinary(const void *data) { return FakeKey{*(uint8_t*)data}; } @@ -24,6 +21,14 @@ public: using EncryptionKey = FakeKey; + static EncryptionKey CreateKey() { + return FakeKey{(uint8_t)rand()}; + } + + static EncryptionKey CreatePseudoRandomKey() { + return FakeKey{(uint8_t)rand()}; + } + static EncryptionKey Key1() { return FakeKey{5}; } diff --git a/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp b/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp index b88a5add..b503931e 100644 --- a/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp +++ b/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp @@ -34,7 +34,7 @@ public: class BlockMock: public Block { public: - BlockMock(): Block(Key::CreatePseudoRandom()) {} + BlockMock(): Block(cpputils::Random::PseudoRandom().getFixedSize()) {} MOCK_CONST_METHOD0(data, const void*()); MOCK_METHOD3(write, void(const void*, uint64_t, uint64_t)); MOCK_METHOD0(flush, void()); diff --git a/utils/Key.h b/utils/Key.h index 17270d12..c395d2ec 100644 --- a/utils/Key.h +++ b/utils/Key.h @@ -7,9 +7,9 @@ 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"). -using Key = cpputils::FixedSizeData<16>; - + // A key here is NOT a key for encryption, but a key as used in key->value mappings ("access handle for a block"). + //TODO Rename to BlockId/BlobId and make it a class containing a FixedSizeData<> member + using Key = cpputils::FixedSizeData<16>; } namespace std {