/src/libreoffice/include/oox/crypto/AgileEngine.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | */ |
10 | | |
11 | | #pragma once |
12 | | |
13 | | #include <vector> |
14 | | |
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> |
20 | | |
21 | | namespace oox::crypto { |
22 | | |
23 | | struct OOX_DLLPUBLIC AgileEncryptionInfo |
24 | | { |
25 | | sal_Int32 spinCount; |
26 | | sal_Int32 saltSize; |
27 | | sal_Int32 keyBits; |
28 | | sal_Int32 hashSize; |
29 | | sal_Int32 blockSize; |
30 | | |
31 | | OUString cipherAlgorithm; |
32 | | OUString cipherChaining; |
33 | | OUString hashAlgorithm; |
34 | | |
35 | | std::vector<sal_uInt8> keyDataSalt; |
36 | | |
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; |
42 | | |
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 |
49 | | }; |
50 | | |
51 | | struct OOX_DLLPUBLIC AgileEncryptionParameters |
52 | | { |
53 | | sal_Int32 spinCount; |
54 | | sal_Int32 saltSize; |
55 | | sal_Int32 keyBits; |
56 | | sal_Int32 hashSize; |
57 | | sal_Int32 blockSize; |
58 | | |
59 | | OUString cipherAlgorithm; |
60 | | OUString cipherChaining; |
61 | | OUString hashAlgorithm; |
62 | | }; |
63 | | |
64 | | enum class AgileEncryptionPreset |
65 | | { |
66 | | AES_128_SHA1, |
67 | | AES_128_SHA384, |
68 | | AES_192_SHA384, |
69 | | AES_256_SHA512, |
70 | | }; |
71 | | |
72 | | class OOX_DLLPUBLIC AgileEngine final : public CryptoEngine |
73 | | { |
74 | | private: |
75 | | AgileEncryptionInfo mInfo; |
76 | | AgileEncryptionPreset meEncryptionPreset; |
77 | | |
78 | | void calculateHashFinal(std::u16string_view rPassword, std::vector<sal_uInt8>& aHashFinal); |
79 | | |
80 | | void calculateBlock( |
81 | | std::vector<sal_uInt8> const & rBlock, |
82 | | std::vector<sal_uInt8>& rHashFinal, |
83 | | std::vector<sal_uInt8>& rInput, |
84 | | std::vector<sal_uInt8>& rOutput); |
85 | | |
86 | | void encryptBlock( |
87 | | std::vector<sal_uInt8> const & rBlock, |
88 | | std::vector<sal_uInt8>& rHashFinal, |
89 | | std::vector<sal_uInt8>& rInput, |
90 | | std::vector<sal_uInt8>& rOutput); |
91 | | |
92 | | static comphelper::CryptoType cryptoType(const AgileEncryptionInfo& rInfo); |
93 | | |
94 | | public: |
95 | | AgileEngine(); |
96 | | |
97 | 0 | AgileEncryptionInfo& getInfo() { return mInfo;} |
98 | | |
99 | | void setPreset(AgileEncryptionPreset ePreset) |
100 | 0 | { |
101 | 0 | meEncryptionPreset = ePreset; |
102 | 0 | } |
103 | | |
104 | | // Decryption |
105 | | |
106 | | void decryptEncryptionKey(std::u16string_view rPassword); |
107 | | bool decryptAndCheckVerifierHash(std::u16string_view rPassword); |
108 | | |
109 | | bool generateEncryptionKey(std::u16string_view rPassword) override; |
110 | | bool readEncryptionInfo(css::uno::Reference<css::io::XInputStream> & rxInputStream) override; |
111 | | bool decrypt(BinaryXInputStream& aInputStream, |
112 | | BinaryXOutputStream& aOutputStream) override; |
113 | | |
114 | | bool checkDataIntegrity() override; |
115 | | |
116 | | bool decryptHmacKey(); |
117 | | bool decryptHmacValue(); |
118 | | |
119 | | // Encryption |
120 | | |
121 | | void writeEncryptionInfo(BinaryXOutputStream& rStream) override; |
122 | | |
123 | | void encrypt(const css::uno::Reference<css::io::XInputStream>& rxInputStream, |
124 | | css::uno::Reference<css::io::XOutputStream>& rxOutputStream, |
125 | | sal_uInt32 nSize) override; |
126 | | |
127 | | bool setupEncryption(OUString const & rPassword) override; |
128 | | |
129 | | bool generateAndEncryptVerifierHash(std::u16string_view rPassword); |
130 | | |
131 | | bool encryptHmacKey(); |
132 | | bool encryptHmacValue(); |
133 | | |
134 | | bool encryptEncryptionKey(std::u16string_view rPassword); |
135 | | void setupEncryptionParameters(AgileEncryptionParameters const & rAgileEncryptionParameters); |
136 | | bool setupEncryptionKey(std::u16string_view rPassword); |
137 | | }; |
138 | | |
139 | | } // namespace comphelper::crypto |
140 | | |
141 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |