/src/libreoffice/include/comphelper/crypto/Crypto.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 <comphelper/comphelperdllapi.h> |
14 | | #include <sal/types.h> |
15 | | |
16 | | #include <vector> |
17 | | #include <memory> |
18 | | |
19 | | namespace comphelper |
20 | | { |
21 | | /** Rounds up the input to the nearest multiple |
22 | | * |
23 | | * For example: |
24 | | * input 1, multiple 16 = 16 |
25 | | * input 16, multiple 16 = 16 |
26 | | * input 17, multiple 16 = 32 |
27 | | * input 31, multiple 16 = 32 |
28 | | */ |
29 | | template <typename T> T roundUp(T input, T multiple) |
30 | 1 | { |
31 | 1 | if (input % multiple == 0) |
32 | 1 | return input; |
33 | 0 | return ((input / multiple) * multiple) + multiple; |
34 | 1 | } Unexecuted instantiation: unsigned long comphelper::roundUp<unsigned long>(unsigned long, unsigned long) int comphelper::roundUp<int>(int, int) Line | Count | Source | 30 | 1 | { | 31 | 1 | if (input % multiple == 0) | 32 | 1 | return input; | 33 | 0 | return ((input / multiple) * multiple) + multiple; | 34 | 1 | } |
Unexecuted instantiation: unsigned int comphelper::roundUp<unsigned int>(unsigned int, unsigned int) |
35 | | |
36 | | enum class CryptoHashType |
37 | | { |
38 | | SHA1, |
39 | | SHA256, |
40 | | SHA384, |
41 | | SHA512 |
42 | | }; |
43 | | |
44 | | enum class CryptoType |
45 | | { |
46 | | UNKNOWN, |
47 | | AES_128_ECB, |
48 | | AES_128_CBC, |
49 | | AES_192_ECB, |
50 | | AES_192_CBC, |
51 | | AES_256_ECB, |
52 | | AES_256_CBC, |
53 | | }; |
54 | | |
55 | | class ICryptoImplementation |
56 | | { |
57 | | public: |
58 | | static std::shared_ptr<ICryptoImplementation> createInstance(); |
59 | | |
60 | | virtual void setupDecryptContext(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, |
61 | | CryptoType eType) |
62 | | = 0; |
63 | | virtual void setupEncryptContext(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, |
64 | | CryptoType type) |
65 | | = 0; |
66 | | virtual void setupCryptoHashContext(std::vector<sal_uInt8>& rKey, CryptoHashType eType) = 0; |
67 | | |
68 | | virtual sal_uInt32 decryptUpdate(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input, |
69 | | sal_uInt32 inputLength) |
70 | | = 0; |
71 | | virtual sal_uInt32 encryptUpdate(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input, |
72 | | sal_uInt32 inputLength) |
73 | | = 0; |
74 | | virtual bool cryptoHashUpdate(std::vector<sal_uInt8>& rInput, sal_uInt32 nInputLength) = 0; |
75 | | virtual bool cryptoHashFinalize(std::vector<sal_uInt8>& rHash) = 0; |
76 | | }; |
77 | | |
78 | | class COMPHELPER_DLLPUBLIC Crypto |
79 | | { |
80 | | protected: |
81 | | std::shared_ptr<ICryptoImplementation> mpImpl; |
82 | | |
83 | | Crypto(); |
84 | | |
85 | | public: |
86 | | virtual ~Crypto(); |
87 | | }; |
88 | | |
89 | | /** Decrypt vector of bytes with AES encryption */ |
90 | | class COMPHELPER_DLLPUBLIC Decrypt final : public Crypto |
91 | | { |
92 | | public: |
93 | | Decrypt(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, CryptoType type); |
94 | | |
95 | | sal_uInt32 update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input, |
96 | | sal_uInt32 inputLength = 0); |
97 | | |
98 | | static sal_uInt32 aes128ecb(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input, |
99 | | std::vector<sal_uInt8>& key); |
100 | | }; |
101 | | |
102 | | /** Encrypt vector of bytes with AES encryption */ |
103 | | class COMPHELPER_DLLPUBLIC Encrypt final : public Crypto |
104 | | { |
105 | | public: |
106 | | /** Initialize encryption for key, init vector and encryption type. |
107 | | * |
108 | | * key - encryption key, key size should be the same as block size |
109 | | * iv - init vector: it can be empty - will not be used (init vector will be 0) |
110 | | */ |
111 | | Encrypt(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, CryptoType type); |
112 | | |
113 | | /** Encrypt the input and write into output |
114 | | * |
115 | | * inputLength - size from the input to be encrypted (0 means to use the size of the vector) |
116 | | */ |
117 | | sal_uInt32 update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input, |
118 | | sal_uInt32 inputLength = 0); |
119 | | }; |
120 | | |
121 | | class COMPHELPER_DLLPUBLIC CryptoHash final : public Crypto |
122 | | { |
123 | | sal_Int32 mnHashSize; |
124 | | |
125 | | public: |
126 | | CryptoHash(std::vector<sal_uInt8>& rKey, CryptoHashType eType); |
127 | | bool update(std::vector<sal_uInt8>& rInput, sal_uInt32 nInputLength = 0); |
128 | | std::vector<sal_uInt8> finalize(); |
129 | | }; |
130 | | |
131 | | } // namespace comphelper |
132 | | |
133 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |