cid#1640468 Dereference after null check
[LibreOffice.git] / include / oox / crypto / AgileEngine.hxx
blobe3daf7f971a9e31a801ab5191e660c05304d7708
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #pragma once
13 #include <vector>
15 #include <oox/dllapi.h>
16 #include <oox/crypto/CryptoEngine.hxx>
17 #include <rtl/ustring.hxx>
18 #include <sal/types.h>
19 #include <comphelper/crypto/Crypto.hxx>
21 namespace oox::crypto {
23 struct OOX_DLLPUBLIC AgileEncryptionInfo
25 sal_Int32 spinCount;
26 sal_Int32 saltSize;
27 sal_Int32 keyBits;
28 sal_Int32 hashSize;
29 sal_Int32 blockSize;
31 OUString cipherAlgorithm;
32 OUString cipherChaining;
33 OUString hashAlgorithm;
35 std::vector<sal_uInt8> keyDataSalt;
37 // Key Encryptor
38 std::vector<sal_uInt8> saltValue;
39 std::vector<sal_uInt8> encryptedVerifierHashInput;
40 std::vector<sal_uInt8> encryptedVerifierHashValue;
41 std::vector<sal_uInt8> encryptedKeyValue;
43 // HMAC
44 std::vector<sal_uInt8> hmacKey;
45 std::vector<sal_uInt8> hmacHash;
46 std::vector<sal_uInt8> hmacCalculatedHash;
47 std::vector<sal_uInt8> hmacEncryptedKey; // encrypted Key
48 std::vector<sal_uInt8> hmacEncryptedValue; // encrypted Hash
51 struct OOX_DLLPUBLIC AgileEncryptionParameters
53 sal_Int32 spinCount;
54 sal_Int32 saltSize;
55 sal_Int32 keyBits;
56 sal_Int32 hashSize;
57 sal_Int32 blockSize;
59 OUString cipherAlgorithm;
60 OUString cipherChaining;
61 OUString hashAlgorithm;
64 enum class AgileEncryptionPreset
66 AES_128_SHA1,
67 AES_128_SHA384,
68 AES_256_SHA512,
71 class OOX_DLLPUBLIC AgileEngine final : public CryptoEngine
73 private:
74 AgileEncryptionInfo mInfo;
75 AgileEncryptionPreset meEncryptionPreset;
77 void calculateHashFinal(const OUString& rPassword, std::vector<sal_uInt8>& aHashFinal);
79 void calculateBlock(
80 std::vector<sal_uInt8> const & rBlock,
81 std::vector<sal_uInt8>& rHashFinal,
82 std::vector<sal_uInt8>& rInput,
83 std::vector<sal_uInt8>& rOutput);
85 void encryptBlock(
86 std::vector<sal_uInt8> const & rBlock,
87 std::vector<sal_uInt8>& rHashFinal,
88 std::vector<sal_uInt8>& rInput,
89 std::vector<sal_uInt8>& rOutput);
91 static comphelper::CryptoType cryptoType(const AgileEncryptionInfo& rInfo);
93 public:
94 AgileEngine();
96 AgileEncryptionInfo& getInfo() { return mInfo;}
98 void setPreset(AgileEncryptionPreset ePreset)
100 meEncryptionPreset = ePreset;
103 // Decryption
105 void decryptEncryptionKey(OUString const & rPassword);
106 bool decryptAndCheckVerifierHash(OUString const & rPassword);
108 bool generateEncryptionKey(OUString const & rPassword) override;
109 bool readEncryptionInfo(css::uno::Reference<css::io::XInputStream> & rxInputStream) override;
110 bool decrypt(BinaryXInputStream& aInputStream,
111 BinaryXOutputStream& aOutputStream) override;
113 bool checkDataIntegrity() override;
115 bool decryptHmacKey();
116 bool decryptHmacValue();
118 // Encryption
120 void writeEncryptionInfo(BinaryXOutputStream& rStream) override;
122 void encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream,
123 css::uno::Reference<css::io::XOutputStream>& rxOutputStream,
124 sal_uInt32 nSize) override;
126 bool setupEncryption(OUString const & rPassword) override;
128 bool generateAndEncryptVerifierHash(OUString const & rPassword);
130 bool encryptHmacKey();
131 bool encryptHmacValue();
133 bool encryptEncryptionKey(OUString const & rPassword);
134 void setupEncryptionParameters(AgileEncryptionParameters const & rAgileEncryptionParameters);
135 bool setupEncryptionKey(OUString const & rPassword);
138 } // namespace comphelper::crypto
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */