libcryfs/test/cpp-utils/crypto/kdf/SCryptTest.cpp

70 lines
2.8 KiB
C++
Raw Normal View History

#include <gtest/gtest.h>
2016-02-11 12:53:42 +01:00
#include "cpp-utils/crypto/kdf/Scrypt.h"
2015-10-27 23:19:11 +01:00
using namespace cpputils;
using std::string;
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;
}
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());
}
};
TEST_F(SCryptTest, GeneratedKeyIsReproductible_448) {
auto derivedKey = scryptForNewKey->deriveKey(56, "mypassword");
auto rederivedKey = scryptForExistingKey->deriveKey(56, "mypassword");
EXPECT_TRUE(keyEquals(derivedKey, rederivedKey));
}
TEST_F(SCryptTest, GeneratedKeyIsReproductible_256) {
auto derivedKey = scryptForNewKey->deriveKey(32, "mypassword");
auto rederivedKey = scryptForExistingKey->deriveKey(32, "mypassword");
EXPECT_TRUE(keyEquals(derivedKey, rederivedKey));
}
TEST_F(SCryptTest, GeneratedKeyIsReproductible_128) {
auto derivedKey = scryptForNewKey->deriveKey(16, "mypassword");
auto rederivedKey = scryptForExistingKey->deriveKey(16, "mypassword");
EXPECT_TRUE(keyEquals(derivedKey, rederivedKey));
}
TEST_F(SCryptTest, GeneratedKeyIsReproductible_DefaultSettings) {
auto derivedKey = scryptForNewKey->deriveKey(16, "mypassword");
auto rederivedKey = scryptForExistingKey->deriveKey(16, "mypassword");
EXPECT_TRUE(keyEquals(derivedKey, rederivedKey));
}
TEST_F(SCryptTest, DifferentPasswordResultsInDifferentKey) {
auto derivedKey = scryptForNewKey->deriveKey(16, "mypassword");
auto rederivedKey = scryptForExistingKey->deriveKey(16, "mypassword2");
EXPECT_FALSE(keyEquals(derivedKey, rederivedKey));
}
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());
}
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());
}