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:
|
|
|
|
unique_ref<SCrypt> scryptForNewKey = SCrypt::forNewKey(SCrypt::TestSettings);
|
|
|
|
unique_ref<SCrypt> scryptForExistingKey = SCrypt::forExistingKey(scryptForNewKey->kdfParameters());
|
|
|
|
|
|
|
|
SCryptParameters kdfParameters(const SCrypt &scrypt) {
|
|
|
|
SCryptParameters result = SCryptParameters::deserialize(scrypt.kdfParameters());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(SCryptTest, GeneratedKeyIsReproductible_448) {
|
|
|
|
auto derivedKey = scryptForNewKey->deriveKey<56>("mypassword");
|
|
|
|
auto rederivedKey = scryptForExistingKey->deriveKey<56>("mypassword");
|
|
|
|
EXPECT_EQ(derivedKey, rederivedKey);
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, GeneratedKeyIsReproductible_256) {
|
|
|
|
auto derivedKey = scryptForNewKey->deriveKey<32>("mypassword");
|
|
|
|
auto rederivedKey = scryptForExistingKey->deriveKey<32>("mypassword");
|
|
|
|
EXPECT_EQ(derivedKey, rederivedKey);
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, GeneratedKeyIsReproductible_128) {
|
|
|
|
auto derivedKey = scryptForNewKey->deriveKey<16>("mypassword");
|
|
|
|
auto rederivedKey = scryptForExistingKey->deriveKey<16>("mypassword");
|
|
|
|
EXPECT_EQ(derivedKey, rederivedKey);
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, GeneratedKeyIsReproductible_DefaultSettings) {
|
|
|
|
auto derivedKey = scryptForNewKey->deriveKey<16>("mypassword");
|
|
|
|
auto rederivedKey = scryptForExistingKey->deriveKey<16>("mypassword");
|
|
|
|
EXPECT_EQ(derivedKey, rederivedKey);
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, DifferentPasswordResultsInDifferentKey) {
|
|
|
|
auto derivedKey = scryptForNewKey->deriveKey<16>("mypassword");
|
|
|
|
auto rederivedKey = scryptForExistingKey->deriveKey<16>("mypassword2");
|
|
|
|
EXPECT_NE(derivedKey, rederivedKey);
|
2015-10-27 22:19:06 +01:00
|
|
|
}
|
|
|
|
|
2016-02-22 02:38:06 +01:00
|
|
|
TEST_F(SCryptTest, UsesCorrectSettings) {
|
|
|
|
auto scrypt = SCrypt::forNewKey(SCrypt::TestSettings);
|
|
|
|
SCryptParameters parameters = kdfParameters(*scrypt);
|
|
|
|
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) {
|
|
|
|
auto scrypt = SCrypt::forNewKey(SCrypt::DefaultSettings);
|
|
|
|
SCryptParameters parameters = kdfParameters(*scrypt);
|
|
|
|
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
|
|
|
}
|