comphelper/qa/unit/CryptoTest.cxx |   98 +++++++++++++++++++++-----------------
 1 file changed, 56 insertions(+), 42 deletions(-)

New commits:
commit 31f90734ce40a86836a27dd524e91cdeda2c09f8
Author:     Tomaž Vajngerl <[email protected]>
AuthorDate: Tue Nov 26 10:14:17 2024 +0900
Commit:     Miklos Vajna <[email protected]>
CommitDate: Fri Nov 29 14:46:45 2024 +0100

    comphelper: fix test - key should be 32 bytes, not 16 bytes
    
    Issue spotted when trying encryption and decryption with OpenSSL,
    which doesn't handle the shorter keys correctly.
    
    Also can't use the same instance of Encrypt after an attempt that
    failed  (0 bytes written) as the blocks are inter-dependent and
    it looks like OpenSSL already changes the state.
    
    Change-Id: I061764e6727c7fbe600e11c5a5a6b8889d15c4e7
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/177304
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-by: Miklos Vajna <[email protected]>

diff --git a/comphelper/qa/unit/CryptoTest.cxx 
b/comphelper/qa/unit/CryptoTest.cxx
index 8bdda3b384b1..4c7603d88eb6 100644
--- a/comphelper/qa/unit/CryptoTest.cxx
+++ b/comphelper/qa/unit/CryptoTest.cxx
@@ -89,8 +89,9 @@ CPPUNIT_TEST_FIXTURE(CryptoTest, testRoundUp)
 
 CPPUNIT_TEST_FIXTURE(CryptoTest, testEncrypt_AES256_CBC)
 {
-    std::vector<sal_uInt8> key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
0x08,
-                                   0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 
0x16 };
+    std::vector<sal_uInt8> key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
0x08, 0x09, 0x10, 0x11,
+                                   0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 
0x19, 0x20, 0x21, 0x22,
+                                   0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 
0x30, 0x31, 0x32 };
 
     std::vector<sal_uInt8> iv = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
0x08,
                                   0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 
0x16 };
@@ -99,40 +100,47 @@ CPPUNIT_TEST_FIXTURE(CryptoTest, testEncrypt_AES256_CBC)
 
     std::vector<sal_uInt8> encrypted(original.size());
 
-    sal_uInt32 nWrittenSize = 0;
+    {
+        sal_uInt32 nWrittenSize = 0;
+        comphelper::Encrypt aEncryptor(key, iv, 
comphelper::CryptoType::AES_256_CBC);
+        nWrittenSize = aEncryptor.update(encrypted, original);
 
-    comphelper::Encrypt aEncryptor(key, iv, 
comphelper::CryptoType::AES_256_CBC);
-    nWrittenSize = aEncryptor.update(encrypted, original);
+        // nothing should be written as the size of the input is not a 
multiple of block size
+        CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), nWrittenSize);
+    }
 
-    // nothing should be written as the size of the input is not a multiple of 
block size
-    CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), nWrittenSize);
+    {
+        sal_uInt32 nWrittenSize = 0;
+        comphelper::Encrypt aEncryptor(key, iv, 
comphelper::CryptoType::AES_256_CBC);
 
-    original.resize(16, 0); // apply padding to make it multiple of block size
-    encrypted.resize(16, 0);
+        original.resize(16, 0); // apply padding to make it multiple of block 
size
+        encrypted.resize(16, 0);
 
-    CPPUNIT_ASSERT_EQUAL(std::string("73656372657400000000000000000000"),
-                         comphelper::hashToString(original));
+        CPPUNIT_ASSERT_EQUAL(std::string("73656372657400000000000000000000"),
+                             comphelper::hashToString(original));
 
-    nWrittenSize = aEncryptor.update(encrypted, original);
-    CPPUNIT_ASSERT_EQUAL(sal_uInt32(16), nWrittenSize);
+        nWrittenSize = aEncryptor.update(encrypted, original);
+        CPPUNIT_ASSERT_EQUAL(sal_uInt32(16), nWrittenSize);
 
-    CPPUNIT_ASSERT_EQUAL(std::string("181fd8e8e33d2e0b06abc41c2b90f6e5"),
-                         comphelper::hashToString(encrypted));
+        CPPUNIT_ASSERT_EQUAL(std::string("e75cb91a34377c09c354c24fcef345a6"),
+                             comphelper::hashToString(encrypted));
 
-    std::vector<sal_uInt8> decrypted(encrypted.size());
+        std::vector<sal_uInt8> decrypted(encrypted.size(), 0);
 
-    comphelper::Decrypt aDecryptor(key, iv, 
comphelper::CryptoType::AES_256_CBC);
-    nWrittenSize = aDecryptor.update(decrypted, encrypted);
-    CPPUNIT_ASSERT_EQUAL(sal_uInt32(16), nWrittenSize);
+        comphelper::Decrypt aDecryptor(key, iv, 
comphelper::CryptoType::AES_256_CBC);
+        nWrittenSize = aDecryptor.update(decrypted, encrypted);
+        CPPUNIT_ASSERT_EQUAL(sal_uInt32(16), nWrittenSize);
 
-    CPPUNIT_ASSERT_EQUAL(std::string("73656372657400000000000000000000"),
-                         comphelper::hashToString(decrypted));
+        CPPUNIT_ASSERT_EQUAL(std::string("73656372657400000000000000000000"),
+                             comphelper::hashToString(decrypted));
+    }
 }
 
 CPPUNIT_TEST_FIXTURE(CryptoTest, testEncrypt_AES256_ECB)
 {
-    std::vector<sal_uInt8> key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
0x08,
-                                   0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 
0x16 };
+    std::vector<sal_uInt8> key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
0x08, 0x09, 0x10, 0x11,
+                                   0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 
0x19, 0x20, 0x21, 0x22,
+                                   0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 
0x30, 0x31, 0x32 };
 
     std::vector<sal_uInt8> iv = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
0x08,
                                   0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 
0x16 };
@@ -141,34 +149,40 @@ CPPUNIT_TEST_FIXTURE(CryptoTest, testEncrypt_AES256_ECB)
 
     std::vector<sal_uInt8> encrypted(original.size());
 
-    sal_uInt32 nWrittenSize = 0;
+    {
+        sal_uInt32 nWrittenSize = 0;
+        comphelper::Encrypt aEncryptor(key, iv, 
comphelper::CryptoType::AES_256_ECB);
+        nWrittenSize = aEncryptor.update(encrypted, original);
 
-    comphelper::Encrypt aEncryptor(key, iv, 
comphelper::CryptoType::AES_256_ECB);
-    nWrittenSize = aEncryptor.update(encrypted, original);
+        // nothing should be written as the size of the input is not a 
multiple of block size
+        CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), nWrittenSize);
+    }
 
-    // nothing should be written as the size of the input is not a multiple of 
block size
-    CPPUNIT_ASSERT_EQUAL(sal_uInt32(0), nWrittenSize);
+    {
+        sal_uInt32 nWrittenSize = 0;
+        comphelper::Encrypt aEncryptor(key, iv, 
comphelper::CryptoType::AES_256_ECB);
 
-    original.resize(16, 0); // apply padding to make it multiple of block size
-    encrypted.resize(16, 0);
+        original.resize(16, 0); // apply padding to make it multiple of block 
size
+        encrypted.resize(16, 0);
 
-    CPPUNIT_ASSERT_EQUAL(std::string("73656372657400000000000000000000"),
-                         comphelper::hashToString(original));
+        CPPUNIT_ASSERT_EQUAL(std::string("73656372657400000000000000000000"),
+                             comphelper::hashToString(original));
 
-    nWrittenSize = aEncryptor.update(encrypted, original);
-    CPPUNIT_ASSERT_EQUAL(sal_uInt32(16), nWrittenSize);
+        nWrittenSize = aEncryptor.update(encrypted, original);
+        CPPUNIT_ASSERT_EQUAL(sal_uInt32(16), nWrittenSize);
 
-    CPPUNIT_ASSERT_EQUAL(std::string("11c380204c0bae9f18a795177e28d842"),
-                         comphelper::hashToString(encrypted));
+        CPPUNIT_ASSERT_EQUAL(std::string("abf7abec9a6b58c089e902397c47ac49"),
+                             comphelper::hashToString(encrypted));
 
-    std::vector<sal_uInt8> decrypted(encrypted.size());
+        std::vector<sal_uInt8> decrypted(encrypted.size(), 0);
 
-    comphelper::Decrypt aDecryptor(key, iv, 
comphelper::CryptoType::AES_256_ECB);
-    nWrittenSize = aDecryptor.update(decrypted, encrypted);
-    CPPUNIT_ASSERT_EQUAL(sal_uInt32(16), nWrittenSize);
+        comphelper::Decrypt aDecryptor(key, iv, 
comphelper::CryptoType::AES_256_ECB);
+        nWrittenSize = aDecryptor.update(decrypted, encrypted);
+        CPPUNIT_ASSERT_EQUAL(sal_uInt32(16), nWrittenSize);
 
-    CPPUNIT_ASSERT_EQUAL(std::string("73656372657400000000000000000000"),
-                         comphelper::hashToString(decrypted));
+        CPPUNIT_ASSERT_EQUAL(std::string("73656372657400000000000000000000"),
+                             comphelper::hashToString(decrypted));
+    }
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to