Adapt to new EncryptedBlockStore

This commit is contained in:
Sebastian Messmer 2015-04-24 18:14:53 +02:00
parent 89a9f1e3d2
commit 22323a0a03
4 changed files with 30 additions and 16 deletions

View File

@ -7,13 +7,12 @@
namespace bf = boost::filesystem;
using boost::property_tree::ptree;
using blockstore::encrypted::EncryptionKey;
using std::string;
namespace cryfs {
CryConfig::CryConfig(const bf::path &configfile)
:_configfile(configfile), _rootBlob(""), _encKey(EncryptionKey::CreateRandom()) {
:_configfile(configfile), _rootBlob(""), _encKey("") {
if (bf::exists(_configfile)) {
load();
}
@ -24,18 +23,14 @@ void CryConfig::load() {
read_json(_configfile.native(), pt);
_rootBlob = pt.get("cryfs.rootblob", "");
string key = pt.get("cryfs.key", "");
if (key != "") {
_encKey = EncryptionKey::FromString(key);
}
_encKey = pt.get("cryfs.key", "");
}
void CryConfig::save() const {
ptree pt;
pt.put("cryfs.rootblob", _rootBlob);
pt.put("cryfs.key", _encKey.ToString());
pt.put("cryfs.key", _encKey);
write_json(_configfile.native(), pt);
}
@ -48,10 +43,14 @@ void CryConfig::SetRootBlob(const std::string &value) {
_rootBlob = value;
}
const blockstore::encrypted::EncryptionKey &CryConfig::EncryptionKey() const {
const string &CryConfig::EncryptionKey() const {
return _encKey;
}
void CryConfig::SetEncryptionKey(const std::string &value) {
_encKey = value;
}
CryConfig::~CryConfig() {
save();
}

View File

@ -5,7 +5,6 @@
#include <boost/filesystem/path.hpp>
#include "messmer/cpp-utils/macros.h"
#include "messmer/blockstore/implementations/encrypted/EncryptionKey.h"
namespace cryfs {
@ -17,7 +16,8 @@ public:
const std::string &RootBlob() const;
void SetRootBlob(const std::string &value);
const blockstore::encrypted::EncryptionKey &EncryptionKey() const;
const std::string &EncryptionKey() const;
void SetEncryptionKey(const std::string &value);
private:
boost::filesystem::path _configfile;
@ -26,7 +26,7 @@ private:
void save() const;
std::string _rootBlob;
blockstore::encrypted::EncryptionKey _encKey;
std::string _encKey;
DISALLOW_COPY_AND_ASSIGN(CryConfig);
};

View File

@ -1,4 +1,5 @@
#include <messmer/blockstore/implementations/caching/CachingBlockStore.h>
#include <messmer/blockstore/implementations/encrypted/ciphers/AES256_CFB.h>
#include "impl/DirBlob.h"
#include "CryDevice.h"
@ -22,6 +23,7 @@ using fspp::fuse::FuseErrnoException;
using blockstore::BlockStore;
using blockstore::Key;
using blockstore::encrypted::EncryptedBlockStore;
using blockstore::encrypted::AES256_CFB;
using blobstore::onblocks::BlobStoreOnBlocks;
using blobstore::onblocks::BlobOnBlocks;
using blockstore::caching::CachingBlockStore;
@ -31,20 +33,29 @@ namespace cryfs {
constexpr uint32_t CryDevice::BLOCKSIZE_BYTES;
CryDevice::CryDevice(unique_ptr<CryConfig> config, unique_ptr<BlockStore> blockStore)
: _blobStore(make_unique<BlobStoreOnBlocks>(make_unique<CachingBlockStore>(make_unique<EncryptedBlockStore>(std::move(blockStore), config->EncryptionKey())), BLOCKSIZE_BYTES)), _rootKey(GetOrCreateRootKey(config.get())) {
: _blobStore(make_unique<BlobStoreOnBlocks>(make_unique<CachingBlockStore>(make_unique<EncryptedBlockStore<Cipher>>(std::move(blockStore), GetOrCreateEncryptionKey(config.get()))), BLOCKSIZE_BYTES)), _rootKey(GetOrCreateRootKey(config.get())) {
}
Key CryDevice::GetOrCreateRootKey(CryConfig *config) {
string root_key = config->RootBlob();
if (root_key == "") {
auto key = CreateRootBlobAndReturnKey();
config->SetRootBlob(key.ToString());
return key;
auto new_key = CreateRootBlobAndReturnKey();
config->SetRootBlob(new_key.ToString());
return new_key;
}
return Key::FromString(root_key);
}
CryDevice::Cipher::EncryptionKey CryDevice::GetOrCreateEncryptionKey(CryConfig *config) {
string encryption_key = config->EncryptionKey();
if (encryption_key == "") {
auto new_key = Cipher::EncryptionKey::CreateRandom();
config->SetEncryptionKey(new_key.ToString());
return new_key;
}
}
Key CryDevice::CreateRootBlobAndReturnKey() {
auto rootBlob = _blobStore->create();
Key rootBlobKey = rootBlob->key();

View File

@ -7,6 +7,7 @@
#include "CryConfig.h"
#include <boost/filesystem.hpp>
#include <messmer/blockstore/implementations/encrypted/ciphers/AES256_CFB.h>
#include <messmer/fspp/fs_interface/Device.h>
#include "messmer/cpp-utils/macros.h"
@ -20,6 +21,8 @@ class CryDevice: public fspp::Device {
public:
static constexpr uint32_t BLOCKSIZE_BYTES = 32 * 1024;
using Cipher = blockstore::encrypted::AES256_CFB;
CryDevice(std::unique_ptr<CryConfig> config, std::unique_ptr<blockstore::BlockStore> blockStore);
virtual ~CryDevice();
@ -35,6 +38,7 @@ public:
private:
blockstore::Key GetOrCreateRootKey(CryConfig *config);
Cipher::EncryptionKey GetOrCreateEncryptionKey(CryConfig *config);
blockstore::Key CreateRootBlobAndReturnKey();
std::unique_ptr<blobstore::BlobStore> _blobStore;