1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
13 #include <comphelper/comphelperdllapi.h>
14 #include <sal/types.h>
21 /** Rounds up the input to the nearest multiple
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)
33 return ((input
/ multiple
) * multiple
) + multiple
;
36 enum class CryptoHashType
53 class ICryptoImplementation
56 static std::shared_ptr
<ICryptoImplementation
> createInstance();
58 virtual void setupDecryptContext(std::vector
<sal_uInt8
>& key
, std::vector
<sal_uInt8
>& iv
,
61 virtual void setupEncryptContext(std::vector
<sal_uInt8
>& key
, std::vector
<sal_uInt8
>& iv
,
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
)
69 virtual sal_uInt32
encryptUpdate(std::vector
<sal_uInt8
>& output
, std::vector
<sal_uInt8
>& input
,
70 sal_uInt32 inputLength
)
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
79 std::shared_ptr
<ICryptoImplementation
> mpImpl
;
87 /** Decrypt vector of bytes with AES encryption */
88 class COMPHELPER_DLLPUBLIC Decrypt final
: public Crypto
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
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
;
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: */