Skip to content
Snippets Groups Projects
Commit 415e5b56 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

tests: add tests for AES encryption and key streching

parent 790bcc81
No related branches found
No related tags found
No related merge requests found
...@@ -173,6 +173,54 @@ void CryptoTester::testOcsp() { ...@@ -173,6 +173,54 @@ void CryptoTester::testOcsp() {
CPPUNIT_ASSERT(ocspRequest.second == req.getNonce()); CPPUNIT_ASSERT(ocspRequest.second == req.getNonce());
} }
void CryptoTester::testAesEncryption() {
auto password = "this is a password 123414!@#%@#$?" + std::to_string(rand());
std::vector<uint8_t> data1 {5, 10};
std::vector<uint8_t> data2(128 * 1024 + 13, 10);
auto encrypted1 = dht::crypto::aesEncrypt(data1, password);
auto encrypted2 = dht::crypto::aesEncrypt(data2, password);
auto decrypted1 = dht::crypto::aesDecrypt(encrypted1, password);
auto decrypted2 = dht::crypto::aesDecrypt(encrypted2, password);
CPPUNIT_ASSERT(data1 != encrypted1);
CPPUNIT_ASSERT(data2 != encrypted2);
CPPUNIT_ASSERT(data1 == decrypted1);
CPPUNIT_ASSERT(data2 == decrypted2);
auto key1 = dht::crypto::aesGetKey(encrypted1, password);
auto key2 = dht::crypto::aesGetKey(encrypted2, password);
auto encrypted1_data = dht::crypto::aesGetEncrypted(encrypted1);
auto encrypted2_data = dht::crypto::aesGetEncrypted(encrypted2);
CPPUNIT_ASSERT(key1 != key2);
decrypted1 = dht::crypto::aesDecrypt(encrypted1_data, key1);
decrypted2 = dht::crypto::aesDecrypt(encrypted2_data, key2);
CPPUNIT_ASSERT(data1 == decrypted1);
CPPUNIT_ASSERT(data2 == decrypted2);
auto salt1 = dht::crypto::aesGetSalt(encrypted1);
auto salt2 = dht::crypto::aesGetSalt(encrypted2);
CPPUNIT_ASSERT(salt1 != salt2);
auto key12 = dht::crypto::stretchKey(password, salt1, 256/8);
auto key22 = dht::crypto::stretchKey(password, salt2, 256/8);
CPPUNIT_ASSERT(key1 == key12);
CPPUNIT_ASSERT(key2 == key22);
decrypted1 = dht::crypto::aesDecrypt(encrypted1_data, key12);
decrypted2 = dht::crypto::aesDecrypt(encrypted2_data, key22);
CPPUNIT_ASSERT(data1 == decrypted1);
CPPUNIT_ASSERT(data2 == decrypted2);
}
void void
CryptoTester::tearDown() { CryptoTester::tearDown() {
......
...@@ -33,6 +33,7 @@ class CryptoTester : public CppUnit::TestFixture { ...@@ -33,6 +33,7 @@ class CryptoTester : public CppUnit::TestFixture {
CPPUNIT_TEST(testCertificateRequest); CPPUNIT_TEST(testCertificateRequest);
CPPUNIT_TEST(testCertificateSerialNumber); CPPUNIT_TEST(testCertificateSerialNumber);
CPPUNIT_TEST(testOcsp); CPPUNIT_TEST(testOcsp);
CPPUNIT_TEST(testAesEncryption);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
public: public:
...@@ -64,6 +65,10 @@ class CryptoTester : public CppUnit::TestFixture { ...@@ -64,6 +65,10 @@ class CryptoTester : public CppUnit::TestFixture {
* Test OCSP * Test OCSP
*/ */
void testOcsp(); void testOcsp();
/**
* Test key streching and aes encryption/decryption
*/
void testAesEncryption();
}; };
} // namespace test } // namespace test
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment