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/+/177305 Reviewed-by: Tomaž Vajngerl <quikee@gmail.com> Tested-by: Jenkins
This commit is contained in:
parent
5e0c670e65
commit
8f2bf78a55
1 changed files with 56 additions and 42 deletions
|
@ -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: */
|
||||
|
|
Loading…
Reference in a new issue