diff --git a/src/CryConfigLoader.cpp b/src/CryConfigLoader.cpp index 7bc58542..224544dd 100644 --- a/src/CryConfigLoader.cpp +++ b/src/CryConfigLoader.cpp @@ -27,6 +27,11 @@ void CryConfigLoader::_initializeConfig(CryConfig *config) { _generateRootBlobKey(config); } +void CryConfigLoader::_initializeConfigWithWeakKey(CryConfig *config) { + _generateWeakEncKey(config); + _generateRootBlobKey(config); +} + void CryConfigLoader::_generateEncKey(CryConfig *config) { printf("Generating secure encryption key..."); fflush(stdout); @@ -36,6 +41,11 @@ void CryConfigLoader::_generateEncKey(CryConfig *config) { fflush(stdout); } +void CryConfigLoader::_generateWeakEncKey(CryConfig *config) { + auto new_key = Cipher::EncryptionKey::CreatePseudoRandom(); + config->SetEncryptionKey(new_key.ToString()); +} + void CryConfigLoader::_generateRootBlobKey(CryConfig *config) { //An empty root blob entry will tell CryDevice to create a new root blob config->SetRootBlob(""); @@ -48,4 +58,19 @@ unique_ptr CryConfigLoader::loadExisting(const bf::path &filename) { return nullptr; } +unique_ptr CryConfigLoader::loadOrCreateWithWeakKey(const bf::path &filename) { + auto config = loadExisting(filename); + if (config.get() != nullptr) { + return config; + } + return createNewWithWeakKey(filename); +} + +unique_ptr CryConfigLoader::createNewWithWeakKey(const bf::path &filename) { + auto config = make_unique(filename); + _initializeConfigWithWeakKey(config.get()); + config->save(); + return config; +} + } diff --git a/src/CryConfigLoader.h b/src/CryConfigLoader.h index 6c14e6d1..7cc53af2 100644 --- a/src/CryConfigLoader.h +++ b/src/CryConfigLoader.h @@ -18,10 +18,17 @@ public: static std::unique_ptr createNew(const boost::filesystem::path &filename); static std::unique_ptr loadExisting(const boost::filesystem::path &filename); + //This method is only for testing purposes, because creating weak keys is much faster than creating strong keys. + static std::unique_ptr loadOrCreateWithWeakKey(const boost::filesystem::path &filename); + static std::unique_ptr createNewWithWeakKey(const boost::filesystem::path &filename); + private: static void _initializeConfig(CryConfig *config); static void _generateEncKey(CryConfig *config); static void _generateRootBlobKey(CryConfig *config); + + static void _initializeConfigWithWeakKey(CryConfig *config); + static void _generateWeakEncKey(CryConfig *config); }; } diff --git a/test/CryFsTest.cpp b/test/CryFsTest.cpp index d980970b..e5685fdf 100644 --- a/test/CryFsTest.cpp +++ b/test/CryFsTest.cpp @@ -29,9 +29,27 @@ public: TEST_F(CryFsTest, CreatedRootdirIsLoadableAfterClosing) { { - CryDevice dev(make_unique(config.path()), make_unique(rootdir.path())); + CryDevice dev(CryConfigLoader::createNewWithWeakKey(config.path()), make_unique(rootdir.path())); } - CryDevice dev(make_unique(config.path()), make_unique(rootdir.path())); + CryDevice dev(CryConfigLoader::loadExisting(config.path()), make_unique(rootdir.path())); + auto root = dev.Load(bf::path("/")); + dynamic_pointer_move(root)->children(); +} + +TEST_F(CryFsTest, UsingStrongKey1_CreatedRootdirIsLoadableAfterClosing) { + { + CryDevice dev(CryConfigLoader::createNew(config.path()), make_unique(rootdir.path())); + } + CryDevice dev(CryConfigLoader::loadExisting(config.path()), make_unique(rootdir.path())); + auto root = dev.Load(bf::path("/")); + dynamic_pointer_move(root)->children(); +} + +TEST_F(CryFsTest, UsingStrongKey2_CreatedRootdirIsLoadableAfterClosing) { + { + CryDevice dev(CryConfigLoader::loadOrCreate(config.path()), make_unique(rootdir.path())); + } + CryDevice dev(CryConfigLoader::loadOrCreate(config.path()), make_unique(rootdir.path())); auto root = dev.Load(bf::path("/")); dynamic_pointer_move(root)->children(); } diff --git a/test/FileSystemTest.cpp b/test/FileSystemTest.cpp index 4e8f12b9..4dd28b26 100644 --- a/test/FileSystemTest.cpp +++ b/test/FileSystemTest.cpp @@ -21,7 +21,7 @@ public: unique_ptr createDevice() override { auto blockStore = make_unique(); - auto config = make_unique(configFile.path()); + auto config = CryConfigLoader::loadOrCreateWithWeakKey(configFile.path()); return make_unique(std::move(config), std::move(blockStore)); }