2015-04-09 19:22:09 +02:00
|
|
|
#pragma once
|
|
|
|
#ifndef BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCKSTORE_H_
|
|
|
|
#define BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCKSTORE_H_
|
|
|
|
|
|
|
|
#include "../../interface/BlockStore.h"
|
|
|
|
#include <messmer/cpp-utils/macros.h>
|
2015-04-24 18:14:25 +02:00
|
|
|
#include <messmer/cpp-utils/pointer.h>
|
|
|
|
#include "EncryptedBlock.h"
|
2015-04-24 21:08:36 +02:00
|
|
|
#include <iostream>
|
2015-04-09 19:22:09 +02:00
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace encrypted {
|
|
|
|
|
2015-04-24 18:14:25 +02:00
|
|
|
template<class Cipher>
|
2015-04-09 19:22:09 +02:00
|
|
|
class EncryptedBlockStore: public BlockStore {
|
|
|
|
public:
|
2015-04-24 18:14:25 +02:00
|
|
|
EncryptedBlockStore(std::unique_ptr<BlockStore> baseBlockStore, const typename Cipher::EncryptionKey &encKey);
|
2015-04-09 19:22:09 +02:00
|
|
|
|
2015-04-18 14:47:12 +02:00
|
|
|
Key createKey() override;
|
|
|
|
std::unique_ptr<Block> tryCreate(const Key &key, Data data) override;
|
2015-04-09 19:22:09 +02:00
|
|
|
std::unique_ptr<Block> load(const Key &key) override;
|
|
|
|
void remove(std::unique_ptr<Block> block) override;
|
|
|
|
uint64_t numBlocks() const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<BlockStore> _baseBlockStore;
|
2015-04-24 18:14:25 +02:00
|
|
|
typename Cipher::EncryptionKey _encKey;
|
2015-04-09 19:22:09 +02:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(EncryptedBlockStore);
|
|
|
|
};
|
|
|
|
|
2015-04-24 18:14:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
template<class Cipher>
|
|
|
|
EncryptedBlockStore<Cipher>::EncryptedBlockStore(std::unique_ptr<BlockStore> baseBlockStore, const typename Cipher::EncryptionKey &encKey)
|
|
|
|
: _baseBlockStore(std::move(baseBlockStore)), _encKey(encKey) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Cipher>
|
|
|
|
Key EncryptedBlockStore<Cipher>::createKey() {
|
|
|
|
return _baseBlockStore->createKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Cipher>
|
|
|
|
std::unique_ptr<Block> EncryptedBlockStore<Cipher>::tryCreate(const Key &key, Data data) {
|
|
|
|
return EncryptedBlock<Cipher>::TryCreateNew(_baseBlockStore.get(), key, std::move(data), _encKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Cipher>
|
|
|
|
std::unique_ptr<Block> EncryptedBlockStore<Cipher>::load(const Key &key) {
|
|
|
|
auto block = _baseBlockStore->load(key);
|
|
|
|
if (block.get() == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-04-24 21:08:36 +02:00
|
|
|
auto encBlock = EncryptedBlock<Cipher>::TryDecrypt(std::move(block), _encKey);
|
|
|
|
if (encBlock.get() == nullptr) {
|
|
|
|
//TODO Think about how to do logging
|
|
|
|
|
|
|
|
}
|
|
|
|
return std::move(encBlock);
|
2015-04-24 18:14:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class Cipher>
|
|
|
|
void EncryptedBlockStore<Cipher>::remove(std::unique_ptr<Block> block) {
|
|
|
|
auto baseBlock = cpputils::dynamic_pointer_move<EncryptedBlock<Cipher>>(block)->releaseBlock();
|
|
|
|
return _baseBlockStore->remove(std::move(baseBlock));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Cipher>
|
|
|
|
uint64_t EncryptedBlockStore<Cipher>::numBlocks() const {
|
|
|
|
return _baseBlockStore->numBlocks();
|
|
|
|
}
|
|
|
|
|
2015-04-09 19:22:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|