From 415e5b56551122a2e6a484336c0cf6ea3d38dbb7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <>
Date: Mon, 27 Nov 2023 15:16:38 -0500
Subject: [PATCH] tests: add tests for AES encryption and key streching

---
 tests/cryptotester.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++
 tests/cryptotester.h   |  5 +++++
 2 files changed, 53 insertions(+)

diff --git a/tests/cryptotester.cpp b/tests/cryptotester.cpp
index 4f1b2f98..81b2c5c5 100644
--- a/tests/cryptotester.cpp
+++ b/tests/cryptotester.cpp
@@ -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() {
 
diff --git a/tests/cryptotester.h b/tests/cryptotester.h
index e56d15a5..89019057 100644
--- a/tests/cryptotester.h
+++ b/tests/cryptotester.h
@@ -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:
@@ -64,6 +65,10 @@ class CryptoTester : public CppUnit::TestFixture {
      * Test OCSP
      */
     void testOcsp();
+    /**
+     * Test key streching and aes encryption/decryption
+     */
+    void testAesEncryption();
 };
 
 }  // namespace test
-- 
GitLab