Blame SOURCES/0001-Fix-endianness-issues-in-OOX-crypto-routines.patch

be40d7
From 96b088a62174a70441ebe959495756e9d86203a2 Mon Sep 17 00:00:00 2001
be40d7
From: Stephan Bergmann <sbergman@redhat.com>
be40d7
Date: Thu, 24 Sep 2020 14:51:16 +0200
be40d7
Subject: [PATCH] Fix endianness issues in OOX crypto routines
be40d7
be40d7
...without which CppunitTest_sw_ooxmlencryption failed on (big-endian) s390x:
be40d7
be40d7
* The 32-bit segment counter in AgileEngine::de-/encrypt apparently needs to be
be40d7
  stored in LSB format (at least, if it is, CppunitTest_sw_ooxmlencryption
be40d7
  ultimately succeeded, whereas otherwise it failed).
be40d7
be40d7
* The UTF-16 string in Standard2007Engine::calculateEncryptionKey apparently
be40d7
  needs to be in LSB format (at least, if it is, CppunitTest_sw_ooxmlencryption
be40d7
  ultimately succeeded, whereas otherwise it failed).
be40d7
be40d7
* The various 32-bit values in the EncryptionStandardHeader and
be40d7
  EncryptionVerifierAES data structures apparently need to be written out in LSB
be40d7
  format in Standard2007Engine::writeEncryptionInfo, given that they are always
be40d7
  read in LSB format in Standard2007Engine::readEncryptionInfo.
be40d7
be40d7
Change-Id: I3a1efbfe324b1bbd539b88dc5d40bb44f9676ffa
be40d7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103315
be40d7
Tested-by: Jenkins
be40d7
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
be40d7
(cherry picked from commit 646a69757b928aeaf6e0d0d41c4b30c02803a3a3)
be40d7
---
be40d7
 oox/source/crypto/AgileEngine.cxx        | 16 +++++++++-----
be40d7
 oox/source/crypto/Standard2007Engine.cxx | 28 +++++++++++++++++-------
be40d7
 2 files changed, 30 insertions(+), 14 deletions(-)
be40d7
be40d7
diff --git a/oox/source/crypto/AgileEngine.cxx b/oox/source/crypto/AgileEngine.cxx
be40d7
index 7c2a0e9c93d2..0fc972bf2ca5 100644
be40d7
--- a/oox/source/crypto/AgileEngine.cxx
be40d7
+++ b/oox/source/crypto/AgileEngine.cxx
be40d7
@@ -457,9 +457,11 @@ bool AgileEngine::decrypt(BinaryXInputStream& aInputStream,
be40d7
 
be40d7
     while ((inputLength = aInputStream.readMemory(inputBuffer.data(), inputBuffer.size())) > 0)
be40d7
     {
be40d7
-        sal_uInt8* segmentBegin = reinterpret_cast<sal_uInt8*>(&segment);
be40d7
-        sal_uInt8* segmentEnd   = segmentBegin + sizeof(segment);
be40d7
-        std::copy(segmentBegin, segmentEnd, saltWithBlockKey.begin() + saltSize);
be40d7
+        auto p = saltWithBlockKey.begin() + saltSize;
be40d7
+        p[0] = segment & 0xFF;
be40d7
+        p[1] = (segment >> 8) & 0xFF;
be40d7
+        p[2] = (segment >> 16) & 0xFF;
be40d7
+        p[3] = segment >> 24;
be40d7
 
be40d7
         hashCalc(hash, saltWithBlockKey, mInfo.hashAlgorithm);
be40d7
 
be40d7
@@ -800,9 +802,11 @@ void AgileEngine::encrypt(css::uno::Reference<css::io::XInputStream> &  rxInputS
be40d7
                         inputLength : oox::core::roundUp(inputLength, sal_uInt32(mInfo.blockSize));
be40d7
 
be40d7
         // Update Key
be40d7
-        sal_uInt8* segmentBegin = reinterpret_cast<sal_uInt8*>(&nSegment);
be40d7
-        sal_uInt8* segmentEnd   = segmentBegin + nSegmentByteSize;
be40d7
-        std::copy(segmentBegin, segmentEnd, saltWithBlockKey.begin() + saltSize);
be40d7
+        auto p = saltWithBlockKey.begin() + saltSize;
be40d7
+        p[0] = nSegment & 0xFF;
be40d7
+        p[1] = (nSegment >> 8) & 0xFF;
be40d7
+        p[2] = (nSegment >> 16) & 0xFF;
be40d7
+        p[3] = nSegment >> 24;
be40d7
 
be40d7
         hashCalc(hash, saltWithBlockKey, mInfo.hashAlgorithm);
be40d7
 
be40d7
diff --git a/oox/source/crypto/Standard2007Engine.cxx b/oox/source/crypto/Standard2007Engine.cxx
be40d7
index 38c4e03baf15..e96fc8f841f2 100644
be40d7
--- a/oox/source/crypto/Standard2007Engine.cxx
be40d7
+++ b/oox/source/crypto/Standard2007Engine.cxx
be40d7
@@ -79,12 +79,12 @@ bool Standard2007Engine::calculateEncryptionKey(const OUString& rPassword)
be40d7
     std::vector<sal_uInt8> initialData(saltSize + passwordByteLength);
be40d7
     std::copy(saltArray, saltArray + saltSize, initialData.begin());
be40d7
 
be40d7
-    const sal_uInt8* passwordByteArray = reinterpret_cast<const sal_uInt8*>(rPassword.getStr());
be40d7
-
be40d7
-    std::copy(
be40d7
-        passwordByteArray,
be40d7
-        passwordByteArray + passwordByteLength,
be40d7
-        initialData.begin() + saltSize);
be40d7
+    auto p = initialData.begin() + saltSize;
be40d7
+    for (sal_Int32 i = 0; i != rPassword.getLength(); ++i) {
be40d7
+        auto c = rPassword[i];
be40d7
+        *p++ = c & 0xFF;
be40d7
+        *p++ = c >> 8;
be40d7
+    }
be40d7
 
be40d7
     // use "hash" vector for result of sha1 hashing
be40d7
     // calculate SHA1 hash of initialData
be40d7
@@ -223,11 +223,23 @@ void Standard2007Engine::writeEncryptionInfo(BinaryXOutputStream& rStream)
be40d7
     sal_uInt32 headerSize = encryptionHeaderSize + cspNameSize;
be40d7
     rStream.WriteUInt32(headerSize);
be40d7
 
be40d7
-    rStream.writeMemory(&mInfo.header, encryptionHeaderSize);
be40d7
+    rStream.WriteUInt32(mInfo.header.flags);
be40d7
+    rStream.WriteUInt32(mInfo.header.sizeExtra);
be40d7
+    rStream.WriteUInt32(mInfo.header.algId);
be40d7
+    rStream.WriteUInt32(mInfo.header.algIdHash);
be40d7
+    rStream.WriteUInt32(mInfo.header.keyBits);
be40d7
+    rStream.WriteUInt32(mInfo.header.providedType);
be40d7
+    rStream.WriteUInt32(mInfo.header.reserved1);
be40d7
+    rStream.WriteUInt32(mInfo.header.reserved2);
be40d7
     rStream.writeUnicodeArray(lclCspName);
be40d7
     rStream.WriteUInt16(0);
be40d7
 
be40d7
-    rStream.writeMemory(&mInfo.verifier, sizeof(msfilter::EncryptionVerifierAES));
be40d7
+    rStream.WriteUInt32(mInfo.verifier.saltSize);
be40d7
+    rStream.writeMemory(&mInfo.verifier.salt, sizeof mInfo.verifier.salt);
be40d7
+    rStream.writeMemory(&mInfo.verifier.encryptedVerifier, sizeof mInfo.verifier.encryptedVerifier);
be40d7
+    rStream.WriteUInt32(mInfo.verifier.encryptedVerifierHashSize);
be40d7
+    rStream.writeMemory(
be40d7
+        &mInfo.verifier.encryptedVerifierHash, sizeof mInfo.verifier.encryptedVerifierHash);
be40d7
 }
be40d7
 
be40d7
 void Standard2007Engine::encrypt(css::uno::Reference<css::io::XInputStream> &  rxInputStream,
be40d7
-- 
be40d7
2.33.1
be40d7