Added serialization/deserialization of DerivedKeyConfig

This commit is contained in:
Sebastian Messmer 2015-10-24 12:25:49 +02:00
parent 8741853eef
commit 7988cc406d
3 changed files with 80 additions and 8 deletions

View File

@ -1 +1,32 @@
#include "DerivedKeyConfig.h"
#include <messmer/cpp-utils/assert/assert.h>
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<uint8_t>::max(), "We don't support salts bigger than 256 byte");
stream.write(reinterpret_cast<const char *>(&saltSize), sizeof(saltSize));
stream.write(static_cast<const char *>(_salt.data()), saltSize);
stream.write(reinterpret_cast<const char *>(&_N), sizeof(_N));
stream.write(reinterpret_cast<const char *>(&_r), sizeof(_r));
stream.write(reinterpret_cast<const char *>(&_p), sizeof(_p));
}
DerivedKeyConfig DerivedKeyConfig::load(istream &stream) {
uint8_t saltSize;
stream.read(reinterpret_cast<char *>(&saltSize), sizeof(saltSize));
Data salt(saltSize);
stream.read(static_cast<char *>(salt.data()), saltSize);
decltype(_N) N;
stream.read(reinterpret_cast<char *>(&N), sizeof(_N));
decltype(_r) r;
stream.read(reinterpret_cast<char *>(&r), sizeof(_r));
decltype(_p) p;
stream.read(reinterpret_cast<char *>(&p), sizeof(_p));
return DerivedKeyConfig(std::move(salt), N, r, p);
}
}

View File

@ -3,6 +3,7 @@
#define MESSMER_CRYFS_SRC_CONFIG_CRYPTO_KEYCONFIG_H
#include <messmer/cpp-utils/data/Data.h>
#include <iostream>
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;

View File

@ -1,51 +1,87 @@
#include <google/gtest/gtest.h>
#include "../../../src/config/crypto/DerivedKeyConfig.h"
#include <messmer/cpp-utils/data/DataFixture.h>
#include <sstream>
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());
}