From 7988cc406d452ce6f22bcb7c32023e9e758bd7a0 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sat, 24 Oct 2015 12:25:49 +0200 Subject: [PATCH] Added serialization/deserialization of DerivedKeyConfig --- src/config/crypto/DerivedKeyConfig.cpp | 31 ++++++++++++ src/config/crypto/DerivedKeyConfig.h | 5 ++ test/config/crypto/DerivedKeyConfigTest.cpp | 52 +++++++++++++++++---- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/src/config/crypto/DerivedKeyConfig.cpp b/src/config/crypto/DerivedKeyConfig.cpp index 12fde801..b5acf89c 100644 --- a/src/config/crypto/DerivedKeyConfig.cpp +++ b/src/config/crypto/DerivedKeyConfig.cpp @@ -1 +1,32 @@ #include "DerivedKeyConfig.h" +#include + +using std::istream; +using std::ostream; +using cpputils::Data; + +namespace cryfs { + void DerivedKeyConfig::save(ostream &stream) const { + uint8_t saltSize = _salt.size(); + ASSERT(saltSize < std::numeric_limits::max(), "We don't support salts bigger than 256 byte"); + stream.write(reinterpret_cast(&saltSize), sizeof(saltSize)); + stream.write(static_cast(_salt.data()), saltSize); + stream.write(reinterpret_cast(&_N), sizeof(_N)); + stream.write(reinterpret_cast(&_r), sizeof(_r)); + stream.write(reinterpret_cast(&_p), sizeof(_p)); + } + + DerivedKeyConfig DerivedKeyConfig::load(istream &stream) { + uint8_t saltSize; + stream.read(reinterpret_cast(&saltSize), sizeof(saltSize)); + Data salt(saltSize); + stream.read(static_cast(salt.data()), saltSize); + decltype(_N) N; + stream.read(reinterpret_cast(&N), sizeof(_N)); + decltype(_r) r; + stream.read(reinterpret_cast(&r), sizeof(_r)); + decltype(_p) p; + stream.read(reinterpret_cast(&p), sizeof(_p)); + return DerivedKeyConfig(std::move(salt), N, r, p); + } +} diff --git a/src/config/crypto/DerivedKeyConfig.h b/src/config/crypto/DerivedKeyConfig.h index 55f86dc1..af62d15c 100644 --- a/src/config/crypto/DerivedKeyConfig.h +++ b/src/config/crypto/DerivedKeyConfig.h @@ -3,6 +3,7 @@ #define MESSMER_CRYFS_SRC_CONFIG_CRYPTO_KEYCONFIG_H #include +#include namespace cryfs { @@ -30,6 +31,10 @@ namespace cryfs { return _p; } + void save(std::ostream &stream) const; + + static DerivedKeyConfig load(std::istream &stream); + private: cpputils::Data _salt; uint64_t _N; diff --git a/test/config/crypto/DerivedKeyConfigTest.cpp b/test/config/crypto/DerivedKeyConfigTest.cpp index e4c92e4c..de48054f 100644 --- a/test/config/crypto/DerivedKeyConfigTest.cpp +++ b/test/config/crypto/DerivedKeyConfigTest.cpp @@ -1,51 +1,87 @@ #include #include "../../../src/config/crypto/DerivedKeyConfig.h" #include +#include using namespace cryfs; using cpputils::DataFixture; using cpputils::Data; +using std::stringstream; -TEST(DerivedKeyConfigTest, Salt) { +class DerivedKeyConfigTest : public ::testing::Test { +public: + DerivedKeyConfig SaveAndLoad(const DerivedKeyConfig &source) { + stringstream str; + source.save(str); + return DerivedKeyConfig::load(str); + } +}; + +TEST_F(DerivedKeyConfigTest, Salt) { DerivedKeyConfig cfg(DataFixture::generate(32), 0, 0, 0); EXPECT_EQ(DataFixture::generate(32), cfg.salt()); } -TEST(DerivedKeyConfigTest, Salt_Move) { +TEST_F(DerivedKeyConfigTest, Salt_Move) { DerivedKeyConfig cfg(DataFixture::generate(32), 0, 0, 0); DerivedKeyConfig moved = std::move(cfg); EXPECT_EQ(DataFixture::generate(32), moved.salt()); } -TEST(DerivedKeyConfigTest, N) { +TEST_F(DerivedKeyConfigTest, Salt_SaveAndLoad) { + DerivedKeyConfig cfg(DataFixture::generate(32), 0, 0, 0); + DerivedKeyConfig loaded = SaveAndLoad(cfg); + EXPECT_EQ(DataFixture::generate(32), loaded.salt()); +} + +TEST_F(DerivedKeyConfigTest, N) { DerivedKeyConfig cfg(Data(0), 1024, 0, 0); EXPECT_EQ(1024, cfg.N()); } -TEST(DerivedKeyConfigTest, N_Move) { +TEST_F(DerivedKeyConfigTest, N_Move) { DerivedKeyConfig cfg(Data(0), 1024, 0, 0); DerivedKeyConfig moved = std::move(cfg); EXPECT_EQ(1024, moved.N()); } -TEST(DerivedKeyConfigTest, r) { +TEST_F(DerivedKeyConfigTest, N_SaveAndLoad) { + DerivedKeyConfig cfg(Data(0), 1024, 0, 0); + DerivedKeyConfig loaded = SaveAndLoad(cfg); + EXPECT_EQ(1024, loaded.N()); +} + +TEST_F(DerivedKeyConfigTest, r) { DerivedKeyConfig cfg(Data(0), 0, 8, 0); EXPECT_EQ(8, cfg.r()); } -TEST(DerivedKeyConfigTest, r_Move) { +TEST_F(DerivedKeyConfigTest, r_Move) { DerivedKeyConfig cfg(Data(0), 0, 8, 0); DerivedKeyConfig moved = std::move(cfg); EXPECT_EQ(8, moved.r()); } -TEST(DerivedKeyConfigTest, p) { +TEST_F(DerivedKeyConfigTest, r_SaveAndLoad) { + DerivedKeyConfig cfg(Data(0), 0, 8, 0); + DerivedKeyConfig loaded = SaveAndLoad(cfg); + EXPECT_EQ(8, loaded.r()); +} + +TEST_F(DerivedKeyConfigTest, p) { DerivedKeyConfig cfg(Data(0), 0, 0, 16); EXPECT_EQ(16, cfg.p()); } -TEST(DerivedKeyConfigTest, p_Move) { +TEST_F(DerivedKeyConfigTest, p_Move) { DerivedKeyConfig cfg(Data(0), 0, 0, 16); DerivedKeyConfig moved = std::move(cfg); EXPECT_EQ(16, moved.p()); } + + +TEST_F(DerivedKeyConfigTest, p_SaveAndLoad) { + DerivedKeyConfig cfg(Data(0), 0, 0, 16); + DerivedKeyConfig loaded = SaveAndLoad(cfg); + EXPECT_EQ(16, loaded.p()); +} \ No newline at end of file