tdf#162786, tdf#161947: Add support for setuptools and pip
[LibreOffice.git] / include / comphelper / crypto / Crypto.hxx
blobabebf9440a5abb8bed98b5f22e367289bd461650
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 <comphelper/comphelperdllapi.h>
14 #include <sal/types.h>
16 #include <vector>
17 #include <memory>
19 namespace comphelper
21 /** Rounds up the input to the nearest multiple
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
29 template <typename T> T roundUp(T input, T multiple)
31 if (input % multiple == 0)
32 return input;
33 return ((input / multiple) * multiple) + multiple;
36 enum class CryptoHashType
38 SHA1,
39 SHA256,
40 SHA384,
41 SHA512
44 enum class CryptoType
46 UNKNOWN,
47 AES_128_ECB,
48 AES_128_CBC,
49 AES_256_ECB,
50 AES_256_CBC,
53 class ICryptoImplementation
55 public:
56 static std::shared_ptr<ICryptoImplementation> createInstance();
58 virtual void setupDecryptContext(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv,
59 CryptoType eType)
60 = 0;
61 virtual void setupEncryptContext(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv,
62 CryptoType type)
63 = 0;
64 virtual void setupCryptoHashContext(std::vector<sal_uInt8>& rKey, CryptoHashType eType) = 0;
66 virtual sal_uInt32 decryptUpdate(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
67 sal_uInt32 inputLength)
68 = 0;
69 virtual sal_uInt32 encryptUpdate(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
70 sal_uInt32 inputLength)
71 = 0;
72 virtual bool cryptoHashUpdate(std::vector<sal_uInt8>& rInput, sal_uInt32 nInputLength) = 0;
73 virtual bool cryptoHashFinalize(std::vector<sal_uInt8>& rHash) = 0;
76 class COMPHELPER_DLLPUBLIC Crypto
78 protected:
79 std::shared_ptr<ICryptoImplementation> mpImpl;
81 Crypto();
83 public:
84 virtual ~Crypto();
87 /** Decrypt vector of bytes with AES encryption */
88 class COMPHELPER_DLLPUBLIC Decrypt final : public Crypto
90 public:
91 Decrypt(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, CryptoType type);
93 sal_uInt32 update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
94 sal_uInt32 inputLength = 0);
96 static sal_uInt32 aes128ecb(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
97 std::vector<sal_uInt8>& key);
100 /** Encrypt vector of bytes with AES encryption */
101 class COMPHELPER_DLLPUBLIC Encrypt final : public Crypto
103 public:
104 /** Initialize encryption for key, init vector and encryption type.
106 * key - encryption key, key size should be the same as block size
107 * iv - init vector: it can be empty - will not be used (init vector will be 0)
109 Encrypt(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, CryptoType type);
111 /** Encrypt the input and write into output
113 * inputLength - size from the input to be encrypted (0 means to use the size of the vector)
115 sal_uInt32 update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
116 sal_uInt32 inputLength = 0);
119 class COMPHELPER_DLLPUBLIC CryptoHash final : public Crypto
121 sal_Int32 mnHashSize;
123 public:
124 CryptoHash(std::vector<sal_uInt8>& rKey, CryptoHashType eType);
125 bool update(std::vector<sal_uInt8>& rInput, sal_uInt32 nInputLength = 0);
126 std::vector<sal_uInt8> finalize();
129 } // namespace comphelper
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */