From 8170bb21f05f6679144b17a412d2114a93aee471 Mon Sep 17 00:00:00 2001 From: Dario Date: Mon, 8 Jun 2020 12:54:07 +0200 Subject: [PATCH] Add test for TestAuthenticatedEncryption and update code to work with test --- src/cpp/Crypto/AuthenticatedEncryption.cpp | 7 ++- src/cpp/Crypto/AuthenticatedEncryption.h | 2 +- .../crypto/TestAuthenticatedEncryption.cpp | 49 +++++++++++++++++++ .../test/crypto/TestAuthenticatedEncryption.h | 12 +++++ src/cpp/test/main.cpp | 6 ++- 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/cpp/test/crypto/TestAuthenticatedEncryption.cpp create mode 100644 src/cpp/test/crypto/TestAuthenticatedEncryption.h diff --git a/src/cpp/Crypto/AuthenticatedEncryption.cpp b/src/cpp/Crypto/AuthenticatedEncryption.cpp index 0c2378eb6..87464f778 100644 --- a/src/cpp/Crypto/AuthenticatedEncryption.cpp +++ b/src/cpp/Crypto/AuthenticatedEncryption.cpp @@ -30,6 +30,8 @@ AuthenticatedEncryption::ResultType AuthenticatedEncryption::createKey(const std auto mm = MemoryManager::getInstance(); auto app_secret = ServerConfig::g_CryptoAppSecret; + assert(app_secret); + std::unique_lock _lock(mWorkingMutex); // use hash512 because existing data where calculated with that, but could be also changed to hash256 @@ -45,7 +47,9 @@ AuthenticatedEncryption::ResultType AuthenticatedEncryption::createKey(const std //unsigned char* key = (unsigned char *)malloc(crypto_box_SEEDBYTES); // 32U //ObfusArray* key = new ObfusArray(crypto_box_SEEDBYTES); - auto mEncryptionKey = mm->getFreeMemory(crypto_box_SEEDBYTES); + if (!mEncryptionKey) { + mEncryptionKey = mm->getFreeMemory(crypto_box_SEEDBYTES); + } //Bin32Bytes* key = mm->get32Bytes(); // generate encryption key, should take a bit longer to make brute force attacks hard @@ -58,6 +62,7 @@ AuthenticatedEncryption::ResultType AuthenticatedEncryption::createKey(const std // generate hash from key for compare assert(sizeof(KeyHashed) >= crypto_shorthash_BYTES); + assert(ServerConfig::g_ServerCryptoKey); crypto_shorthash((unsigned char*)&mEncryptionKeyHash, *mEncryptionKey, crypto_box_SEEDBYTES, *ServerConfig::g_ServerCryptoKey); return AUTH_ENCRYPT_OK; diff --git a/src/cpp/Crypto/AuthenticatedEncryption.h b/src/cpp/Crypto/AuthenticatedEncryption.h index 9474137dd..e11b0cc3f 100644 --- a/src/cpp/Crypto/AuthenticatedEncryption.h +++ b/src/cpp/Crypto/AuthenticatedEncryption.h @@ -47,7 +47,7 @@ public: return mEncryptionKeyHash == b.getKeyHashed(); } - inline bool hasKey() const { std::shared_lock _lock(mWorkingMutex); return !mEncryptionKey; } + inline bool hasKey() const { std::shared_lock _lock(mWorkingMutex); return mEncryptionKey != nullptr; } //! \brief generate encryption key, with default parameter use ca. 300 ms //! diff --git a/src/cpp/test/crypto/TestAuthenticatedEncryption.cpp b/src/cpp/test/crypto/TestAuthenticatedEncryption.cpp new file mode 100644 index 000000000..22c6a9dfe --- /dev/null +++ b/src/cpp/test/crypto/TestAuthenticatedEncryption.cpp @@ -0,0 +1,49 @@ +#include "TestAuthenticatedEncryption.h" + +#include "../../Crypto/AuthenticatedEncryption.h" + +#include "../../lib/Profiler.h" +#include "../../lib/DataTypeConverter.h" + +#include "../ServerConfig.h" + +void TestAuthenticatedEncryption::SetUp() +{ + if (!ServerConfig::g_CryptoAppSecret) { + ServerConfig::g_CryptoAppSecret = DataTypeConverter::hexToBin("21ffbbc616fe"); + } + if (!ServerConfig::g_ServerCryptoKey) { + auto serverKey = DataTypeConverter::hexToBin("a51ef8ac7ef1abf162fb7a65261acd7a"); + ServerConfig::g_ServerCryptoKey = new ObfusArray(serverKey->size(), *serverKey); + } +} + +TEST_F(TestAuthenticatedEncryption, encryptDecryptTest) { + AuthenticatedEncryption authenticated_encryption; + EXPECT_FALSE(authenticated_encryption.hasKey()); + EXPECT_EQ(authenticated_encryption.getKeyHashed(), 0); + + Profiler time_used; + EXPECT_EQ(authenticated_encryption.createKey("dariofrodo@gmx.de", "r3an7d_spassw"), AuthenticatedEncryption::AUTH_ENCRYPT_OK); + printf("create key duration: %s\n", time_used.string().data()); + + EXPECT_TRUE(authenticated_encryption.hasKey()); + + std::string test_message = "Dies ist eine Test Message zur Encryption"; + auto mm = MemoryManager::getInstance(); + auto test_message_bin = mm->getFreeMemory(test_message.size()); + MemoryBin* encrypted_message = nullptr; + memcpy(*test_message_bin, test_message.data(), test_message.size()); + + time_used.reset(); + EXPECT_EQ(authenticated_encryption.encrypt(test_message_bin, &encrypted_message), AuthenticatedEncryption::AUTH_ENCRYPT_OK); + printf("encrypt message duration: %s\n", time_used.string().data()); + + MemoryBin* decrypted_message = nullptr; + time_used.reset(); + EXPECT_EQ(authenticated_encryption.decrypt(encrypted_message, &decrypted_message), AuthenticatedEncryption::AUTH_DECRYPT_OK); + printf("decrypt message duration: %s\n", time_used.string().data()); + + EXPECT_EQ(std::string((const char*)*decrypted_message, decrypted_message->size()), test_message); +// */ +} \ No newline at end of file diff --git a/src/cpp/test/crypto/TestAuthenticatedEncryption.h b/src/cpp/test/crypto/TestAuthenticatedEncryption.h new file mode 100644 index 000000000..9f4427f85 --- /dev/null +++ b/src/cpp/test/crypto/TestAuthenticatedEncryption.h @@ -0,0 +1,12 @@ +#ifndef __GRADIDO_LOGIN_SERVER_TEST_CRYPTO_TEST_AUTHENTICATED_ENCRYPTION_H +#define __GRADIDO_LOGIN_SERVER_TEST_CRYPTO_TEST_AUTHENTICATED_ENCRYPTION_H + +#include "gtest/gtest.h" + +class TestAuthenticatedEncryption : public ::testing::Test +{ +protected: + void SetUp() override; +}; + +#endif //__GRADIDO_LOGIN_SERVER_TEST_CRYPTO_TEST_AUTHENTICATED_ENCRYPTION_H \ No newline at end of file diff --git a/src/cpp/test/main.cpp b/src/cpp/test/main.cpp index 09bdb6395..1d6182b7f 100644 --- a/src/cpp/test/main.cpp +++ b/src/cpp/test/main.cpp @@ -56,6 +56,8 @@ int main(int argc, char** argv) run(); ende(); ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); - //return 42; + + auto result = RUN_ALL_TESTS(); + ServerConfig::unload(); + return result; }