diff options
author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2017-04-21 06:28:14 +0200 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2017-04-29 07:56:26 +0200 |
commit | 0a3ded1def24194b01c9f2849ed91af4954fbb5f (patch) | |
tree | a2d43051e46c108ac146b7813a0230e123bc8f26 | |
parent | 5bd7713a6b7aa3ad0eaa7f535cd932c2a316acb5 (diff) |
replace oox internal hashing code with new comphelper code
Change-Id: I0e9363f6aa6f9d1011bc917645122408f9728ca2
Reviewed-on: https://gerrit.libreoffice.org/36794
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Markus Mohrhard <markus.mohrhard@googlemail.com>
-rw-r--r-- | include/oox/crypto/CryptTools.hxx | 34 | ||||
-rw-r--r-- | oox/source/crypto/AgileEngine.cxx | 14 | ||||
-rw-r--r-- | oox/source/crypto/CryptTools.cxx | 131 | ||||
-rw-r--r-- | oox/source/crypto/Standard2007Engine.cxx | 18 |
4 files changed, 20 insertions, 177 deletions
diff --git a/include/oox/crypto/CryptTools.hxx b/include/oox/crypto/CryptTools.hxx index 84e4c485cb9d..d5bc5b95bda3 100644 --- a/include/oox/crypto/CryptTools.hxx +++ b/include/oox/crypto/CryptTools.hxx @@ -113,40 +113,6 @@ public: sal_uInt32 inputLength = 0) override; }; -class Digest final -{ -public: - enum DigestType - { - UNKNOWN, - SHA1, - SHA512 - }; - -private: - DigestType meType; - -#if USE_TLS_OPENSSL - EVP_MD_CTX* mpContext; -#endif - -#if USE_TLS_NSS - HASHContext* mpContext; -#endif - -public: - Digest(DigestType eType); - ~Digest(); - - void update(std::vector<sal_uInt8>& input); - void finalize(std::vector<sal_uInt8>& digest); - - sal_uInt32 getLength(); - - static bool sha1( std::vector<sal_uInt8>& digest, std::vector<sal_uInt8>& input); - static bool sha512(std::vector<sal_uInt8>& digest, std::vector<sal_uInt8>& input); -}; - } // namespace core } // namespace oox diff --git a/oox/source/crypto/AgileEngine.cxx b/oox/source/crypto/AgileEngine.cxx index 9c1095a01f4d..72539509c965 100644 --- a/oox/source/crypto/AgileEngine.cxx +++ b/oox/source/crypto/AgileEngine.cxx @@ -13,6 +13,8 @@ #include <oox/helper/binaryinputstream.hxx> #include <oox/helper/binaryoutputstream.hxx> +#include <comphelper/hash.hxx> + namespace oox { namespace core { @@ -25,9 +27,17 @@ bool hashCalc(std::vector<sal_uInt8>& output, const OUString& sAlgorithm ) { if (sAlgorithm == "SHA1") - return Digest::sha1(output, input); + { + std::vector<unsigned char> out = comphelper::Hash::calculateHash(input.data(), input.size(), comphelper::HashType::SHA1); + output = out; + return true; + } else if (sAlgorithm == "SHA512") - return Digest::sha512(output, input); + { + std::vector<unsigned char> out = comphelper::Hash::calculateHash(input.data(), input.size(), comphelper::HashType::SHA512); + output = out; + return true; + } return false; } diff --git a/oox/source/crypto/CryptTools.cxx b/oox/source/crypto/CryptTools.cxx index b1e3345c5453..b23d3ebbf57b 100644 --- a/oox/source/crypto/CryptTools.cxx +++ b/oox/source/crypto/CryptTools.cxx @@ -193,137 +193,6 @@ sal_uInt32 Encrypt::update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8 return static_cast<sal_uInt32>(outputLength); } -// Digest - -namespace -{ - -#if USE_TLS_OPENSSL -const EVP_MD* lclOpenSSLgetEngine(Digest::DigestType eType) -{ - switch(eType) - { - case Digest::SHA1: - return EVP_sha1(); - case Digest::SHA512: - return EVP_sha512(); - default: - break; - } - return NULL; -} -#endif - -#if USE_TLS_NSS -HASH_HashType lclNSSgetHashType(Digest::DigestType eType) -{ - switch(eType) - { - case Digest::SHA1: - return HASH_AlgSHA1; - case Digest::SHA512: - return HASH_AlgSHA512; - default: - break; - } - return HASH_AlgNULL; -} -#endif - -} - -Digest::Digest(DigestType eType) : - meType(eType) -{ -#if USE_TLS_OPENSSL - mpContext = EVP_MD_CTX_create(); - EVP_DigestInit_ex(mpContext, lclOpenSSLgetEngine(eType), NULL); -#endif - -#if USE_TLS_NSS - NSS_NoDB_Init(nullptr); - mpContext = HASH_Create(lclNSSgetHashType(eType)); - HASH_Begin(mpContext); -#endif -} - -Digest::~Digest() -{ -#if USE_TLS_OPENSSL - if(mpContext) - EVP_MD_CTX_destroy(mpContext); -#endif - -#if USE_TLS_NSS - if(mpContext) - HASH_Destroy(mpContext); -#endif -} - -sal_uInt32 Digest::getLength() -{ - switch(meType) - { - case SHA1: - return msfilter::SHA1_HASH_LENGTH; - case SHA512: - return msfilter::SHA512_HASH_LENGTH; - default: - break; - } - return 0; -} - -void Digest::update(std::vector<sal_uInt8>& input) -{ -#if USE_TLS_OPENSSL - EVP_DigestUpdate(mpContext, input.data(), input.size()); -#endif -#if USE_TLS_NSS - HASH_Update(mpContext, input.data(), input.size()); -#endif -} - -void Digest::finalize(std::vector<sal_uInt8>& digest) -{ - digest.clear(); - -#if USE_TLS_OPENSSL - unsigned int digestWrittenLength; - digest.resize(getLength(), 0); - EVP_DigestFinal_ex(mpContext, digest.data(), &digestWrittenLength); -#endif - -#if USE_TLS_NSS - unsigned int digestWrittenLength; - unsigned int digestLength = static_cast<unsigned int>(getLength()); - digest.resize(digestLength, 0); - HASH_End(mpContext, digest.data(), &digestWrittenLength, digestLength); -#endif -} - -bool Digest::sha1(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input) -{ - bool aResult = false; - - Digest aDigest(SHA1); - aDigest.update(input); - aDigest.finalize(output); - aResult = true; - return aResult; -} - -bool Digest::sha512(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input) -{ - bool aResult = false; - - Digest aDigest(SHA512); - aDigest.update(input); - aDigest.finalize(output); - aResult = true; - return aResult; -} - } // namespace core } // namespace oox diff --git a/oox/source/crypto/Standard2007Engine.cxx b/oox/source/crypto/Standard2007Engine.cxx index d95f3153f564..b9c259fa02ac 100644 --- a/oox/source/crypto/Standard2007Engine.cxx +++ b/oox/source/crypto/Standard2007Engine.cxx @@ -17,6 +17,8 @@ #include <rtl/digest.h> #include <rtl/random.h> +#include <comphelper/hash.hxx> + namespace oox { namespace core { @@ -57,9 +59,8 @@ bool Standard2007Engine::generateVerifier() return false; std::copy(encryptedVerifier.begin(), encryptedVerifier.end(), mInfo.verifier.encryptedVerifier); - std::vector<sal_uInt8> hash(msfilter::SHA1_HASH_LENGTH, 0); mInfo.verifier.encryptedVerifierHashSize = msfilter::SHA1_HASH_LENGTH; - Digest::sha1(hash, verifier); + std::vector<sal_uInt8> hash = comphelper::Hash::calculateHash(verifier.data(), verifier.size(), comphelper::HashType::SHA1); hash.resize(msfilter::SHA256_HASH_LENGTH, 0); std::vector<sal_uInt8> encryptedHash(msfilter::SHA256_HASH_LENGTH, 0); @@ -89,10 +90,8 @@ bool Standard2007Engine::calculateEncryptionKey(const OUString& rPassword) initialData.begin() + saltSize); // use "hash" vector for result of sha1 hashing - std::vector<sal_uInt8> hash(msfilter::SHA1_HASH_LENGTH, 0); - // calculate SHA1 hash of initialData - Digest::sha1(hash, initialData); + std::vector<sal_uInt8> hash = comphelper::Hash::calculateHash(initialData.data(), initialData.size(), comphelper::HashType::SHA1); // data = iterator (4bytes) + hash std::vector<sal_uInt8> data(msfilter::SHA1_HASH_LENGTH + 4, 0); @@ -101,19 +100,19 @@ bool Standard2007Engine::calculateEncryptionKey(const OUString& rPassword) { ByteOrderConverter::writeLittleEndian(data.data(), i); std::copy(hash.begin(), hash.end(), data.begin() + 4); - Digest::sha1(hash, data); + hash = comphelper::Hash::calculateHash(data.data(), data.size(), comphelper::HashType::SHA1); } std::copy(hash.begin(), hash.end(), data.begin() ); std::fill(data.begin() + msfilter::SHA1_HASH_LENGTH, data.end(), 0 ); - Digest::sha1(hash, data); + hash = comphelper::Hash::calculateHash(data.data(), data.size(), comphelper::HashType::SHA1); // derive key std::vector<sal_uInt8> buffer(64, 0x36); for (size_t i = 0; i < hash.size(); ++i) buffer[i] ^= hash[i]; - Digest::sha1(hash, buffer); + hash = comphelper::Hash::calculateHash(buffer.data(), buffer.size(), comphelper::HashType::SHA1); std::copy(hash.begin(), hash.begin() + mKey.size(), mKey.begin()); return true; @@ -144,8 +143,7 @@ bool Standard2007Engine::generateEncryptionKey(const OUString& password) std::vector<sal_uInt8> verifierHash(encryptedHash.size(), 0); Decrypt::aes128ecb(verifierHash, encryptedHash, mKey); - std::vector<sal_uInt8> hash(msfilter::SHA1_HASH_LENGTH, 0); - Digest::sha1(hash, verifier); + std::vector<sal_uInt8> hash = comphelper::Hash::calculateHash(verifier.data(), verifier.size(), comphelper::HashType::SHA1); return std::equal(hash.begin(), hash.end(), verifierHash.begin()); } |