/src/libreoffice/comphelper/source/crypto/Crypto.cxx
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 | | #include <comphelper/crypto/Crypto.hxx> |
11 | | #include <sal/types.h> |
12 | | |
13 | | namespace comphelper |
14 | | { |
15 | | Crypto::Crypto() |
16 | 103 | : mpImpl(ICryptoImplementation::createInstance()) |
17 | 103 | { |
18 | 103 | } |
19 | 103 | Crypto::~Crypto() = default; |
20 | | |
21 | | // DECRYPT |
22 | | |
23 | | Decrypt::Decrypt(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, CryptoType type) |
24 | 103 | { |
25 | 103 | mpImpl->setupDecryptContext(key, iv, type); |
26 | 103 | } |
27 | | |
28 | | sal_uInt32 Decrypt::update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input, |
29 | | sal_uInt32 inputLength) |
30 | 131 | { |
31 | 131 | sal_uInt32 actualInputLength |
32 | 131 | = inputLength == 0 || inputLength > input.size() ? input.size() : inputLength; |
33 | 131 | return mpImpl->decryptUpdate(output, input, actualInputLength); |
34 | 131 | } |
35 | | |
36 | | sal_uInt32 Decrypt::aes128ecb(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input, |
37 | | std::vector<sal_uInt8>& key) |
38 | 74 | { |
39 | 74 | sal_uInt32 outputLength = 0; |
40 | 74 | std::vector<sal_uInt8> iv; |
41 | 74 | Decrypt crypto(key, iv, CryptoType::AES_128_ECB); |
42 | 74 | outputLength = crypto.update(output, input); |
43 | 74 | return outputLength; |
44 | 74 | } |
45 | | |
46 | | // ENCRYPT |
47 | | |
48 | | Encrypt::Encrypt(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, CryptoType type) |
49 | 0 | { |
50 | 0 | mpImpl->setupEncryptContext(key, iv, type); |
51 | 0 | } |
52 | | |
53 | | sal_uInt32 Encrypt::update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input, |
54 | | sal_uInt32 inputLength) |
55 | 0 | { |
56 | 0 | sal_uInt32 actualInputLength |
57 | 0 | = inputLength == 0 || inputLength > input.size() ? input.size() : inputLength; |
58 | 0 | return mpImpl->encryptUpdate(output, input, actualInputLength); |
59 | 0 | } |
60 | | |
61 | | // CryptoHash - HMAC |
62 | | |
63 | | namespace |
64 | | { |
65 | | sal_Int32 getSizeForHashType(CryptoHashType eType) |
66 | 0 | { |
67 | 0 | switch (eType) |
68 | 0 | { |
69 | 0 | case CryptoHashType::SHA1: |
70 | 0 | return 20; |
71 | 0 | case CryptoHashType::SHA256: |
72 | 0 | return 32; |
73 | 0 | case CryptoHashType::SHA384: |
74 | 0 | return 48; |
75 | 0 | case CryptoHashType::SHA512: |
76 | 0 | return 64; |
77 | 0 | } |
78 | 0 | return 0; |
79 | 0 | } |
80 | | |
81 | | } // end anonymous namespace |
82 | | |
83 | | CryptoHash::CryptoHash(std::vector<sal_uInt8>& rKey, CryptoHashType eType) |
84 | 0 | : mnHashSize(getSizeForHashType(eType)) |
85 | 0 | { |
86 | 0 | mpImpl->setupCryptoHashContext(rKey, eType); |
87 | 0 | } |
88 | | |
89 | | bool CryptoHash::update(std::vector<sal_uInt8>& rInput, sal_uInt32 nInputLength) |
90 | 0 | { |
91 | 0 | sal_uInt32 nActualInputLength |
92 | 0 | = (nInputLength == 0 || nInputLength > rInput.size()) ? rInput.size() : nInputLength; |
93 | 0 | return mpImpl->cryptoHashUpdate(rInput, nActualInputLength); |
94 | 0 | } |
95 | | |
96 | | std::vector<sal_uInt8> CryptoHash::finalize() |
97 | 0 | { |
98 | 0 | std::vector<sal_uInt8> aHash(mnHashSize, 0); |
99 | 0 | mpImpl->cryptoHashFinalize(aHash); |
100 | 0 | return aHash; |
101 | 0 | } |
102 | | |
103 | | } // namespace comphelper |
104 | | |
105 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |