Skip to content

Commit

Permalink
tests: add tests for AES encryption and key streching
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Béraud authored and aberaud committed Nov 28, 2023
1 parent 24e7b00 commit 09046ec
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/cryptotester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,54 @@ void CryptoTester::testOcsp() {
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
CryptoTester::tearDown() {

Expand Down
5 changes: 5 additions & 0 deletions tests/cryptotester.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class CryptoTester : public CppUnit::TestFixture {
CPPUNIT_TEST(testCertificateRequest);
CPPUNIT_TEST(testCertificateSerialNumber);
CPPUNIT_TEST(testOcsp);
CPPUNIT_TEST(testAesEncryption);
CPPUNIT_TEST_SUITE_END();

public:
Expand Down Expand Up @@ -64,6 +65,10 @@ class CryptoTester : public CppUnit::TestFixture {
* Test OCSP
*/
void testOcsp();
/**
* Test key streching and aes encryption/decryption
*/
void testAesEncryption();
};

} // namespace test

0 comments on commit 09046ec

Please sign in to comment.