Adapt to new cpputils::Random interface

This commit is contained in:
Sebastian Messmer 2015-10-22 17:41:43 +02:00
parent a4ce9f1c97
commit 62549eeae6
9 changed files with 46 additions and 13 deletions

View File

@ -17,6 +17,7 @@ namespace caching {
template<class Key, class Value, uint32_t MAX_ENTRIES> template<class Key, class Value, uint32_t MAX_ENTRIES>
class Cache { class Cache {
public: 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 //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_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 static constexpr double PURGE_INTERVAL = 0.5; // With this interval, we check for entries to purge

View File

@ -12,6 +12,11 @@
namespace blockstore { namespace blockstore {
namespace caching { 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. // A class that is a queue and a map at the same time. We could also see it as an addressable queue.
template<class Key, class Value> template<class Key, class Value>
class QueueMap { class QueueMap {

View File

@ -4,6 +4,7 @@
#include <messmer/cpp-utils/data/FixedSizeData.h> #include <messmer/cpp-utils/data/FixedSizeData.h>
#include <messmer/cpp-utils/data/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include <messmer/cpp-utils/random/Random.h>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <cryptopp/cryptopp/modes.h> #include <cryptopp/cryptopp/modes.h>
#include "Cipher.h" #include "Cipher.h"
@ -18,6 +19,15 @@ public:
using EncryptionKey = cpputils::FixedSizeData<KeySize>; using EncryptionKey = cpputils::FixedSizeData<KeySize>;
static EncryptionKey CreateKey() {
return cpputils::Random::OSRandom().getFixedSize<EncryptionKey::BINARY_LENGTH>();
}
// Used in test cases for fast key creation
static EncryptionKey CreatePseudoRandomKey() {
return cpputils::Random::PseudoRandom().getFixedSize<EncryptionKey::BINARY_LENGTH>();
}
static constexpr unsigned int ciphertextSize(unsigned int plaintextBlockSize) { static constexpr unsigned int ciphertextSize(unsigned int plaintextBlockSize) {
return plaintextBlockSize + IV_SIZE; return plaintextBlockSize + IV_SIZE;
} }
@ -35,7 +45,7 @@ private:
template<typename BlockCipher, unsigned int KeySize> template<typename BlockCipher, unsigned int KeySize>
cpputils::Data CFB_Cipher<BlockCipher, KeySize>::encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { cpputils::Data CFB_Cipher<BlockCipher, KeySize>::encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) {
auto iv = cpputils::FixedSizeData<IV_SIZE>::CreatePseudoRandom(); cpputils::FixedSizeData<IV_SIZE> iv = cpputils::Random::PseudoRandom().getFixedSize<IV_SIZE>();
auto encryption = typename CryptoPP::CFB_Mode<BlockCipher>::Encryption(encKey.data(), encKey.BINARY_LENGTH, iv.data()); auto encryption = typename CryptoPP::CFB_Mode<BlockCipher>::Encryption(encKey.data(), encKey.BINARY_LENGTH, iv.data());
cpputils::Data ciphertext(ciphertextSize(plaintextSize)); cpputils::Data ciphertext(ciphertextSize(plaintextSize));
std::memcpy(ciphertext.data(), iv.data(), IV_SIZE); std::memcpy(ciphertext.data(), iv.data(), IV_SIZE);

View File

@ -15,9 +15,10 @@ public:
BOOST_CONCEPT_USAGE(CipherConcept) { BOOST_CONCEPT_USAGE(CipherConcept) {
same_type(UINT32_C(0), X::ciphertextSize(UINT32_C(5))); same_type(UINT32_C(0), X::ciphertextSize(UINT32_C(5)));
same_type(UINT32_C(0), X::plaintextSize(UINT32_C(5))); same_type(UINT32_C(0), X::plaintextSize(UINT32_C(5)));
typename X::EncryptionKey key = X::EncryptionKey::CreateOSRandom(); typename X::EncryptionKey key1 = X::CreateKey();
same_type(cpputils::Data(0), X::encrypt((uint8_t*)nullptr, UINT32_C(0), key)); typename X::EncryptionKey key2 = X::CreatePseudoRandomKey();
same_type(boost::optional<cpputils::Data>(cpputils::Data(0)), X::decrypt((uint8_t*)nullptr, UINT32_C(0), key)); same_type(cpputils::Data(0), X::encrypt((uint8_t*)nullptr, UINT32_C(0), key1));
same_type(boost::optional<cpputils::Data>(cpputils::Data(0)), X::decrypt((uint8_t*)nullptr, UINT32_C(0), key2));
} }
private: private:

View File

@ -4,6 +4,7 @@
#include <messmer/cpp-utils/data/FixedSizeData.h> #include <messmer/cpp-utils/data/FixedSizeData.h>
#include <messmer/cpp-utils/data/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include <messmer/cpp-utils/random/Random.h>
#include <cryptopp/cryptopp/gcm.h> #include <cryptopp/cryptopp/gcm.h>
#include "Cipher.h" #include "Cipher.h"
@ -17,6 +18,15 @@ public:
using EncryptionKey = cpputils::FixedSizeData<KeySize>; using EncryptionKey = cpputils::FixedSizeData<KeySize>;
static EncryptionKey CreateKey() {
return cpputils::Random::OSRandom().getFixedSize<EncryptionKey::BINARY_LENGTH>();
}
// Used in test cases for fast key creation
static EncryptionKey CreatePseudoRandomKey() {
return cpputils::Random::PseudoRandom().getFixedSize<EncryptionKey::BINARY_LENGTH>();
}
static constexpr unsigned int ciphertextSize(unsigned int plaintextBlockSize) { static constexpr unsigned int ciphertextSize(unsigned int plaintextBlockSize) {
return plaintextBlockSize + IV_SIZE + TAG_SIZE; return plaintextBlockSize + IV_SIZE + TAG_SIZE;
} }
@ -35,7 +45,7 @@ private:
template<typename BlockCipher, unsigned int KeySize> template<typename BlockCipher, unsigned int KeySize>
cpputils::Data GCM_Cipher<BlockCipher, KeySize>::encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) { cpputils::Data GCM_Cipher<BlockCipher, KeySize>::encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) {
auto iv = cpputils::FixedSizeData<IV_SIZE>::CreatePseudoRandom(); cpputils::FixedSizeData<IV_SIZE> iv = cpputils::Random::PseudoRandom().getFixedSize<IV_SIZE>();
typename CryptoPP::GCM<BlockCipher, CryptoPP::GCM_64K_Tables>::Encryption encryption; typename CryptoPP::GCM<BlockCipher, CryptoPP::GCM_64K_Tables>::Encryption encryption;
encryption.SetKeyWithIV(encKey.data(), encKey.BINARY_LENGTH, iv.data(), IV_SIZE); encryption.SetKeyWithIV(encKey.data(), encKey.BINARY_LENGTH, iv.data(), IV_SIZE);
cpputils::Data ciphertext(ciphertextSize(plaintextSize)); cpputils::Data ciphertext(ciphertextSize(plaintextSize));

View File

@ -4,6 +4,7 @@
#include "../BlockStore.h" #include "../BlockStore.h"
#include "../Block.h" #include "../Block.h"
#include <messmer/cpp-utils/random/Random.h>
namespace blockstore { namespace blockstore {
@ -13,7 +14,7 @@ namespace blockstore {
class BlockStoreWithRandomKeys: public BlockStore { class BlockStoreWithRandomKeys: public BlockStore {
public: public:
Key createKey() final { Key createKey() final {
return Key::CreatePseudoRandom(); return cpputils::Random::PseudoRandom().getFixedSize<Key::BINARY_LENGTH>();
} }
}; };

View File

@ -6,9 +6,6 @@
#include <messmer/cpp-utils/data/FixedSizeData.h> #include <messmer/cpp-utils/data/FixedSizeData.h>
struct FakeKey { struct FakeKey {
static FakeKey CreateOSRandom() {
return FakeKey{(uint8_t)rand()};
}
static FakeKey FromBinary(const void *data) { static FakeKey FromBinary(const void *data) {
return FakeKey{*(uint8_t*)data}; return FakeKey{*(uint8_t*)data};
} }
@ -24,6 +21,14 @@ public:
using EncryptionKey = FakeKey; using EncryptionKey = FakeKey;
static EncryptionKey CreateKey() {
return FakeKey{(uint8_t)rand()};
}
static EncryptionKey CreatePseudoRandomKey() {
return FakeKey{(uint8_t)rand()};
}
static EncryptionKey Key1() { static EncryptionKey Key1() {
return FakeKey{5}; return FakeKey{5};
} }

View File

@ -34,7 +34,7 @@ public:
class BlockMock: public Block { class BlockMock: public Block {
public: public:
BlockMock(): Block(Key::CreatePseudoRandom()) {} BlockMock(): Block(cpputils::Random::PseudoRandom().getFixedSize<Key::BINARY_LENGTH>()) {}
MOCK_CONST_METHOD0(data, const void*()); MOCK_CONST_METHOD0(data, const void*());
MOCK_METHOD3(write, void(const void*, uint64_t, uint64_t)); MOCK_METHOD3(write, void(const void*, uint64_t, uint64_t));
MOCK_METHOD0(flush, void()); MOCK_METHOD0(flush, void());

View File

@ -7,9 +7,9 @@
namespace blockstore { 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"). // 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>; //TODO Rename to BlockId/BlobId and make it a class containing a FixedSizeData<> member
using Key = cpputils::FixedSizeData<16>;
} }
namespace std { namespace std {