Unit tests on PasswordManager are now much faster because the tests use a much

lower iterations count now then the default setting.
This commit is contained in:
eelke 2017-12-16 21:42:41 +01:00
parent 0b088a2723
commit 8f1ba8130c
5 changed files with 30 additions and 12 deletions

View file

@ -31,7 +31,7 @@ namespace {
const size_t MAC_KEY_LEN = 32; const size_t MAC_KEY_LEN = 32;
const size_t MAC_OUTPUT_LEN = 20; const size_t MAC_OUTPUT_LEN = 20;
const size_t PBKDF_SALT_LEN = 10; const size_t PBKDF_SALT_LEN = 10;
const size_t PBKDF_ITERATIONS = 8 * 1024; //const size_t PBKDF_ITERATIONS = 8 * 1024;
const size_t PBKDF_OUTPUT_LEN = CIPHER_KEY_LEN + CIPHER_IV_LEN + MAC_KEY_LEN; const size_t PBKDF_OUTPUT_LEN = CIPHER_KEY_LEN + CIPHER_IV_LEN + MAC_KEY_LEN;
@ -41,14 +41,15 @@ namespace {
StrengthenedKey generateKey(const std::string &passphrase, const uint8_t *salt, int saltlength) StrengthenedKey generateKey(const std::string &passphrase, const uint8_t *salt,
int saltlength, int iterations)
{ {
PKCS5_PBKDF2 pbkdf(new HMAC(new SHA_512)); PKCS5_PBKDF2 pbkdf(new HMAC(new SHA_512));
OctetString master_key = pbkdf.derive_key( OctetString master_key = pbkdf.derive_key(
PBKDF_OUTPUT_LEN, PBKDF_OUTPUT_LEN,
passphrase, passphrase,
salt, saltlength, salt, saltlength,
PBKDF_ITERATIONS); iterations);
const uint8_t* mk = master_key.begin(); const uint8_t* mk = master_key.begin();
@ -146,9 +147,9 @@ namespace {
PasswordManager::PasswordManager() PasswordManager::PasswordManager(int iterations)
: m_iterations(iterations)
{ {
} }
Expected<bool> PasswordManager::unlock(const std::string &master_password) Expected<bool> PasswordManager::unlock(const std::string &master_password)
@ -158,7 +159,8 @@ Expected<bool> PasswordManager::unlock(const std::string &master_password)
if (m_masterHash.length() == 0 && master_password.empty()) { if (m_masterHash.length() == 0 && master_password.empty()) {
result = true; result = true;
} else { } else {
StrengthenedKey key = generateKey(master_password, m_keySalt.begin(), m_keySalt.length()); StrengthenedKey key = generateKey(master_password, m_keySalt.begin(),
m_keySalt.length(), m_iterations);
OctetString hash = hashStrengthenedKey(key, m_hashSalt); OctetString hash = hashStrengthenedKey(key, m_hashSalt);
BOOST_ASSERT_MSG(hash.length() == m_masterHash.length(), "Both hashes should have the same length! Versioning error?"); BOOST_ASSERT_MSG(hash.length() == m_masterHash.length(), "Both hashes should have the same length! Versioning error?");
@ -182,7 +184,7 @@ Expected<bool> PasswordManager::changeMasterPassword(const std::string &old_mast
if (m_masterHash.length() == 0 && old_master_password.empty()) { if (m_masterHash.length() == 0 && old_master_password.empty()) {
// Nothing set yet so we initialize for first use // Nothing set yet so we initialize for first use
m_keySalt = OctetString(m_rng, v1_consts.pbkdf_salt_len); m_keySalt = OctetString(m_rng, v1_consts.pbkdf_salt_len);
m_masterKey = generateKey(new_master_password, m_keySalt.begin(), m_keySalt.length()); m_masterKey = generateKey(new_master_password, m_keySalt.begin(), m_keySalt.length(), m_iterations);
m_hashSalt = OctetString(m_rng, v1_consts.pbkdf_salt_len); m_hashSalt = OctetString(m_rng, v1_consts.pbkdf_salt_len);
m_masterHash = hashStrengthenedKey(m_masterKey, m_hashSalt); m_masterHash = hashStrengthenedKey(m_masterKey, m_hashSalt);

View file

@ -29,7 +29,7 @@ public:
// static PasswordManager create(const std::string &file_name); // static PasswordManager create(const std::string &file_name);
PasswordManager(); explicit PasswordManager(int iterations = 8192);
/** Unlocks the passwords of the connections. /** Unlocks the passwords of the connections.
* *
* \return Normally it return a bool specifying if the password was accepted. * \return Normally it return a bool specifying if the password was accepted.
@ -49,6 +49,7 @@ public:
Expected<bool> getPassword(const std::string &key, std::string &out); Expected<bool> getPassword(const std::string &key, std::string &out);
private: private:
int m_iterations;
Botan::AutoSeeded_RNG m_rng; Botan::AutoSeeded_RNG m_rng;
Botan::OctetString m_keySalt; // salt for generating crypto key Botan::OctetString m_keySalt; // salt for generating crypto key
StrengthenedKey m_masterKey; // crypto key StrengthenedKey m_masterKey; // crypto key

View file

@ -10,6 +10,8 @@ TARGET = core
TEMPLATE = lib TEMPLATE = lib
CONFIG += staticlib c++14 CONFIG += staticlib c++14
QMAKE_CXXFLAGS += /std:c++17
INCLUDEPATH += C:\prog\include \ INCLUDEPATH += C:\prog\include \
C:\Prog\include\pgsql \ C:\Prog\include\pgsql \
C:\VSproj\boost32\include\boost-1_65_1 C:\VSproj\boost32\include\boost-1_65_1

View file

@ -47,3 +47,16 @@ else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../.
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../pgsql/release/pgsql.lib else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../pgsql/release/pgsql.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../pgsql/debug/pgsql.lib else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../pgsql/debug/pgsql.lib
else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../../../pgsql/libpgsql.a else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../../../pgsql/libpgsql.a
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../../pglablib/release/ -lpglablib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../../pglablib/debug/ -lpglablib
else:unix:!macx: LIBS += -L$$OUT_PWD/../../../pglablib/ -lpglablib
INCLUDEPATH += $$PWD/../../../pglablib
DEPENDPATH += $$PWD/../../../pglablib
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../pglablib/release/libpglablib.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../pglablib/debug/libpglablib.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../pglablib/release/pglablib.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../../pglablib/debug/pglablib.lib
else:unix:!macx: PRE_TARGETDEPS += $$OUT_PWD/../../../pglablib/libpglablib.a

View file

@ -7,7 +7,7 @@ using namespace testing;
TEST(PasswordManager, initial_changeMasterPassword_returns_true) TEST(PasswordManager, initial_changeMasterPassword_returns_true)
{ {
PasswordManager pwm; PasswordManager pwm(10);
auto res = pwm.changeMasterPassword("", "my test passphrase"); auto res = pwm.changeMasterPassword("", "my test passphrase");
ASSERT_NO_THROW( res.get() ); ASSERT_NO_THROW( res.get() );
@ -16,7 +16,7 @@ TEST(PasswordManager, initial_changeMasterPassword_returns_true)
TEST(PasswordManager, unlock_succeeds) TEST(PasswordManager, unlock_succeeds)
{ {
PasswordManager pwm; PasswordManager pwm(10);
std::string passphrase = "my test passphrase"; std::string passphrase = "my test passphrase";
@ -31,7 +31,7 @@ TEST(PasswordManager, unlock_succeeds)
TEST(PasswordManager, unlock_fails) TEST(PasswordManager, unlock_fails)
{ {
PasswordManager pwm; PasswordManager pwm(10);
std::string passphrase = "my test passphrase"; std::string passphrase = "my test passphrase";
@ -46,7 +46,7 @@ TEST(PasswordManager, unlock_fails)
TEST(PasswordManager, test_save_get) TEST(PasswordManager, test_save_get)
{ {
PasswordManager pwm; PasswordManager pwm(10);
std::string passphrase = "my test passphrase"; std::string passphrase = "my test passphrase";