2015-04-09 19:22:09 +02:00
|
|
|
#pragma once
|
|
|
|
#ifndef BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCK_H_
|
|
|
|
#define BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCK_H_
|
|
|
|
|
|
|
|
#include "../../interface/Block.h"
|
2015-04-09 20:14:12 +02:00
|
|
|
#include "EncryptionKey.h"
|
2015-04-09 21:17:28 +02:00
|
|
|
#include "../../utils/Data.h"
|
2015-04-18 14:47:12 +02:00
|
|
|
#include "../../interface/BlockStore.h"
|
2015-04-09 19:22:09 +02:00
|
|
|
|
|
|
|
#include "messmer/cpp-utils/macros.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace encrypted {
|
|
|
|
class EncryptedBlockStore;
|
|
|
|
|
|
|
|
class EncryptedBlock: public Block {
|
|
|
|
public:
|
|
|
|
//TODO Storing key twice (in parent class and in object pointed to). Once would be enough.
|
2015-04-09 20:14:12 +02:00
|
|
|
EncryptedBlock(std::unique_ptr<Block> baseBlock, const EncryptionKey &encKey);
|
2015-04-18 15:10:42 +02:00
|
|
|
EncryptedBlock(std::unique_ptr<Block> baseBlock, const EncryptionKey &encKey, Data plaintextData);
|
2015-04-09 21:17:28 +02:00
|
|
|
virtual ~EncryptedBlock();
|
|
|
|
|
2015-04-18 14:47:12 +02:00
|
|
|
static std::unique_ptr<EncryptedBlock> TryCreateNew(BlockStore *baseBlockStore, const Key &key, Data data, const EncryptionKey &encKey);
|
2015-04-09 19:22:09 +02:00
|
|
|
|
|
|
|
const void *data() const override;
|
|
|
|
void write(const void *source, uint64_t offset, uint64_t size) override;
|
|
|
|
void flush() override;
|
|
|
|
|
|
|
|
size_t size() const override;
|
|
|
|
|
2015-04-09 21:17:28 +02:00
|
|
|
static constexpr unsigned int BASE_BLOCK_SIZE(unsigned int useableBlockSize) {
|
|
|
|
return useableBlockSize + IV_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr unsigned int USEABLE_BLOCK_SIZE(unsigned int baseBlockSize) {
|
|
|
|
return baseBlockSize - IV_SIZE;
|
|
|
|
}
|
|
|
|
|
2015-04-09 19:22:09 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<Block> _baseBlock;
|
2015-04-09 21:17:28 +02:00
|
|
|
Data _plaintextData;
|
2015-04-09 20:14:12 +02:00
|
|
|
EncryptionKey _encKey;
|
2015-04-09 21:17:28 +02:00
|
|
|
bool _dataChanged;
|
|
|
|
|
|
|
|
static constexpr unsigned int IV_SIZE = CryptoPP::AES::BLOCKSIZE;
|
|
|
|
|
|
|
|
byte *baseBlockIV();
|
|
|
|
byte *baseBlockData();
|
|
|
|
|
|
|
|
void _encryptToBaseBlock();
|
|
|
|
void _decryptFromBaseBlock();
|
2015-04-09 19:22:09 +02:00
|
|
|
|
2015-04-18 14:47:12 +02:00
|
|
|
static Data _encrypt(const Data &plaintext, const EncryptionKey &encKey);
|
|
|
|
|
2015-04-09 19:22:09 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(EncryptedBlock);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|