libcryfs/implementations/encrypted/EncryptedBlock.h
Sebastian Meßmer 417a701636 - BlockStore::create() gets the data of the new block as a parameter
- Fixed numBlocks() in OnDiskBlockStore, FakeBlockStore, CachingBlockStore, ...
- CachingBlockStore caches created blocks and doesn't directly create them in the underlying blockstore
2015-04-18 14:47:12 +02:00

62 lines
1.6 KiB
C++

#pragma once
#ifndef BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCK_H_
#define BLOCKSTORE_IMPLEMENTATIONS_ENCRYPTED_ENCRYPTEDBLOCK_H_
#include "../../interface/Block.h"
#include "EncryptionKey.h"
#include "../../utils/Data.h"
#include "../../interface/BlockStore.h"
#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.
EncryptedBlock(std::unique_ptr<Block> baseBlock, const EncryptionKey &encKey);
virtual ~EncryptedBlock();
static std::unique_ptr<EncryptedBlock> TryCreateNew(BlockStore *baseBlockStore, const Key &key, Data data, const EncryptionKey &encKey);
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;
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;
}
private:
std::unique_ptr<Block> _baseBlock;
Data _plaintextData;
EncryptionKey _encKey;
bool _dataChanged;
static constexpr unsigned int IV_SIZE = CryptoPP::AES::BLOCKSIZE;
byte *baseBlockIV();
byte *baseBlockData();
void _encryptToBaseBlock();
void _decryptFromBaseBlock();
static Data _encrypt(const Data &plaintext, const EncryptionKey &encKey);
DISALLOW_COPY_AND_ASSIGN(EncryptedBlock);
};
}
}
#endif