mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Add test for TestAuthenticatedEncryption and update code to work with test
This commit is contained in:
parent
ea18b3ca0a
commit
8170bb21f0
@ -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<std::shared_mutex> _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;
|
||||
|
||||
@ -47,7 +47,7 @@ public:
|
||||
return mEncryptionKeyHash == b.getKeyHashed();
|
||||
}
|
||||
|
||||
inline bool hasKey() const { std::shared_lock<std::shared_mutex> _lock(mWorkingMutex); return !mEncryptionKey; }
|
||||
inline bool hasKey() const { std::shared_lock<std::shared_mutex> _lock(mWorkingMutex); return mEncryptionKey != nullptr; }
|
||||
|
||||
//! \brief generate encryption key, with default parameter use ca. 300 ms
|
||||
//!
|
||||
|
||||
49
src/cpp/test/crypto/TestAuthenticatedEncryption.cpp
Normal file
49
src/cpp/test/crypto/TestAuthenticatedEncryption.cpp
Normal file
@ -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);
|
||||
// */
|
||||
}
|
||||
12
src/cpp/test/crypto/TestAuthenticatedEncryption.h
Normal file
12
src/cpp/test/crypto/TestAuthenticatedEncryption.h
Normal file
@ -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
|
||||
@ -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;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user