2016-02-10 00:58:03 +01:00
|
|
|
#include <gtest/gtest.h>
|
2016-02-11 12:53:42 +01:00
|
|
|
#include "cpp-utils/crypto/kdf/Scrypt.h"
|
2015-10-27 22:19:06 +01:00
|
|
|
|
2015-10-27 23:19:11 +01:00
|
|
|
using namespace cpputils;
|
2016-02-22 02:38:06 +01:00
|
|
|
using std::string;
|
2015-10-27 22:19:06 +01:00
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
class SCryptTest : public ::testing::Test {
|
|
|
|
public:
|
2018-10-21 22:53:53 +02:00
|
|
|
bool keyEquals(const EncryptionKey& lhs, const EncryptionKey& rhs) {
|
|
|
|
ASSERT(lhs.binaryLength() == rhs.binaryLength(), "Keys must have equal size to be comparable");
|
|
|
|
return 0 == std::memcmp(lhs.data(), rhs.data(), lhs.binaryLength());
|
2018-09-12 03:53:21 +02:00
|
|
|
}
|
2016-02-22 02:38:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SCryptTest, GeneratedKeyIsReproductible_448) {
|
2018-10-22 04:31:08 +02:00
|
|
|
SCrypt scrypt(SCrypt::TestSettings);
|
|
|
|
auto derivedKey = scrypt.deriveNewKey(56, "mypassword");
|
|
|
|
auto rederivedKey = scrypt.deriveExistingKey(56, "mypassword", derivedKey.kdfParameters);
|
|
|
|
EXPECT_TRUE(keyEquals(derivedKey.key, rederivedKey));
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, GeneratedKeyIsReproductible_256) {
|
2018-10-22 04:31:08 +02:00
|
|
|
SCrypt scrypt(SCrypt::TestSettings);
|
|
|
|
auto derivedKey = scrypt.deriveNewKey(32, "mypassword");
|
|
|
|
auto rederivedKey = scrypt.deriveExistingKey(32, "mypassword", derivedKey.kdfParameters);
|
|
|
|
EXPECT_TRUE(keyEquals(derivedKey.key, rederivedKey));
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, GeneratedKeyIsReproductible_128) {
|
2018-10-22 04:31:08 +02:00
|
|
|
SCrypt scrypt(SCrypt::TestSettings);
|
|
|
|
auto derivedKey = scrypt.deriveNewKey(16, "mypassword");
|
|
|
|
auto rederivedKey = scrypt.deriveExistingKey(16, "mypassword", derivedKey.kdfParameters);
|
|
|
|
EXPECT_TRUE(keyEquals(derivedKey.key, rederivedKey));
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, GeneratedKeyIsReproductible_DefaultSettings) {
|
2018-10-22 04:31:08 +02:00
|
|
|
SCrypt scrypt(SCrypt::TestSettings);
|
|
|
|
auto derivedKey = scrypt.deriveNewKey(16, "mypassword");
|
|
|
|
auto rederivedKey = scrypt.deriveExistingKey(16, "mypassword", derivedKey.kdfParameters);
|
|
|
|
EXPECT_TRUE(keyEquals(derivedKey.key, rederivedKey));
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, DifferentPasswordResultsInDifferentKey) {
|
2018-10-22 04:31:08 +02:00
|
|
|
SCrypt scrypt(SCrypt::TestSettings);
|
|
|
|
auto derivedKey = scrypt.deriveNewKey(16, "mypassword");
|
|
|
|
auto rederivedKey = scrypt.deriveExistingKey(16, "mypassword2", derivedKey.kdfParameters);
|
|
|
|
EXPECT_FALSE(keyEquals(derivedKey.key, rederivedKey));
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, UsesCorrectSettings) {
|
2018-10-22 04:31:08 +02:00
|
|
|
SCrypt scrypt(SCrypt::TestSettings);
|
|
|
|
auto derivedKey = scrypt.deriveNewKey(16, "mypassword");
|
|
|
|
auto parameters = SCryptParameters::deserialize(derivedKey.kdfParameters);
|
2016-02-22 02:38:06 +01:00
|
|
|
EXPECT_EQ(SCrypt::TestSettings.SALT_LEN, parameters.salt().size());
|
|
|
|
EXPECT_EQ(SCrypt::TestSettings.N, parameters.N());
|
|
|
|
EXPECT_EQ(SCrypt::TestSettings.r, parameters.r());
|
|
|
|
EXPECT_EQ(SCrypt::TestSettings.p, parameters.p());
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, UsesCorrectDefaultSettings) {
|
2018-10-22 04:31:08 +02:00
|
|
|
SCrypt scrypt(SCrypt::DefaultSettings);
|
|
|
|
auto derivedKey = scrypt.deriveNewKey(16, "mypassword");
|
|
|
|
auto parameters = SCryptParameters::deserialize(derivedKey.kdfParameters);
|
2016-02-22 02:38:06 +01:00
|
|
|
EXPECT_EQ(SCrypt::DefaultSettings.SALT_LEN, parameters.salt().size());
|
|
|
|
EXPECT_EQ(SCrypt::DefaultSettings.N, parameters.N());
|
|
|
|
EXPECT_EQ(SCrypt::DefaultSettings.r, parameters.r());
|
|
|
|
EXPECT_EQ(SCrypt::DefaultSettings.p, parameters.p());
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|