comphelper/qa/unit/CryptoTest.cxx | 44 +++++++++++++++++++++++++++++++++++ include/comphelper/crypto/Crypto.hxx | 10 +++++++ include/comphelper/hash.hxx | 5 +++ 3 files changed, 59 insertions(+)
New commits: commit 626c184f77e77227217a23694176f950b80b7c44 Author: Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk> AuthorDate: Thu Nov 7 14:07:37 2024 +0100 Commit: Tomaž Vajngerl <qui...@gmail.com> CommitDate: Fri Nov 22 07:19:30 2024 +0100 crypto: add AES256 test, document Encrypt, Hash conv. function This adds AES256 test of encryption and decryption - an example on how the Encrypt and Decrypt classes are used. Also add a convenience function for Hash, accepting vector of bytes as this is used in a lot of places. Change-Id: I9b395c7afb8fac45cae8d7d8bd983f5daaafd64b Reviewed-on: https://gerrit.libreoffice.org/c/core/+/176887 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <qui...@gmail.com> diff --git a/comphelper/qa/unit/CryptoTest.cxx b/comphelper/qa/unit/CryptoTest.cxx index 2b0dac47416f..b13887040667 100644 --- a/comphelper/qa/unit/CryptoTest.cxx +++ b/comphelper/qa/unit/CryptoTest.cxx @@ -33,10 +33,12 @@ public: void testCryptoHash(); void testRoundUp(); + void testEncrypt_AES256(); CPPUNIT_TEST_SUITE(CryptoTest); CPPUNIT_TEST(testCryptoHash); CPPUNIT_TEST(testRoundUp); + CPPUNIT_TEST(testEncrypt_AES256); CPPUNIT_TEST_SUITE_END(); }; @@ -95,6 +97,48 @@ void CryptoTest::testRoundUp() CPPUNIT_ASSERT_EQUAL(32, comphelper::roundUp(31, 16)); } +void CryptoTest::testEncrypt_AES256() +{ + 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> iv = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; + + std::vector<sal_uInt8> original = { 's', 'e', 'c', 'r', 'e', 't', ' + + std::vector<sal_uInt8> encrypted(original.size()); + + sal_uInt32 nWrittenSize = 0; + + 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); + + 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)); + + nWrittenSize = aEncryptor.update(encrypted, original); + CPPUNIT_ASSERT_EQUAL(sal_uInt32(16), nWrittenSize); + + CPPUNIT_ASSERT_EQUAL(std::string("181fd8e8e33d2e0b06abc41c2b90f6e5"), + comphelper::hashToString(encrypted)); + + std::vector<sal_uInt8> decrypted(encrypted.size()); + + 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_TEST_SUITE_REGISTRATION(CryptoTest); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/comphelper/crypto/Crypto.hxx b/include/comphelper/crypto/Crypto.hxx index aba89642652a..b2530f02b558 100644 --- a/include/comphelper/crypto/Crypto.hxx +++ b/include/comphelper/crypto/Crypto.hxx @@ -75,11 +75,21 @@ public: std::vector<sal_uInt8>& key); }; +/** Encrypt vector of bytes with AES encryption */ class COMPHELPER_DLLPUBLIC Encrypt final : public Crypto { public: + /** Initialize encryption for key, init vector and encryption type. + * + * key - encryption key, key size should be the same as block size + * iv - init vector: it can be empty - will not be used (init vector will be 0) + */ Encrypt(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, CryptoType type); + /** Encrypt the input and write into output + * + * inputLength - size from the input to be encrypted (0 means to use the size of the vector) + */ sal_uInt32 update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input, sal_uInt32 inputLength = 0); }; diff --git a/include/comphelper/hash.hxx b/include/comphelper/hash.hxx index e148276abfb3..9567904e6080 100644 --- a/include/comphelper/hash.hxx +++ b/include/comphelper/hash.hxx @@ -61,6 +61,11 @@ public: void update(const unsigned char* pInput, size_t length); + void update(std::vector<unsigned char> const& rInput) + { + update(rInput.data(), rInput.size()); + } + std::vector<unsigned char> finalize(); static std::vector<unsigned char> calculateHash(const unsigned char* pInput, size_t length, HashType eType);