summaryrefslogtreecommitdiff
path: root/comphelper/source/crypto/Crypto.cxx
blob: faf1da7307f9c0eba4ce8319276157fe8371e993 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <comphelper/crypto/Crypto.hxx>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <sal/types.h>
#include <config_oox.h>

namespace comphelper
{
Crypto::Crypto()
    : mpImpl(ICryptoImplementation::createInstance())
{
}
Crypto::~Crypto() = default;

// DECRYPT

Decrypt::Decrypt(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, CryptoType type)
{
    mpImpl->setupDecryptContext(key, iv, type);
}

sal_uInt32 Decrypt::update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
                           sal_uInt32 inputLength)
{
    sal_uInt32 actualInputLength
        = inputLength == 0 || inputLength > input.size() ? input.size() : inputLength;
    return mpImpl->decryptUpdate(output, input, actualInputLength);
}

sal_uInt32 Decrypt::aes128ecb(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
                              std::vector<sal_uInt8>& key)
{
    sal_uInt32 outputLength = 0;
    std::vector<sal_uInt8> iv;
    Decrypt crypto(key, iv, CryptoType::AES_128_ECB);
    outputLength = crypto.update(output, input);
    return outputLength;
}

// ENCRYPT

Encrypt::Encrypt(std::vector<sal_uInt8>& key, std::vector<sal_uInt8>& iv, CryptoType type)
{
    mpImpl->setupEncryptContext(key, iv, type);
}

sal_uInt32 Encrypt::update(std::vector<sal_uInt8>& output, std::vector<sal_uInt8>& input,
                           sal_uInt32 inputLength)
{
    sal_uInt32 actualInputLength
        = inputLength == 0 || inputLength > input.size() ? input.size() : inputLength;
    return mpImpl->encryptUpdate(output, input, actualInputLength);
}

// CryptoHash - HMAC

namespace
{
sal_Int32 getSizeForHashType(CryptoHashType eType)
{
    switch (eType)
    {
        case CryptoHashType::SHA1:
            return 20;
        case CryptoHashType::SHA256:
            return 32;
        case CryptoHashType::SHA384:
            return 48;
        case CryptoHashType::SHA512:
            return 64;
    }
    return 0;
}

} // end anonymous namespace

CryptoHash::CryptoHash(std::vector<sal_uInt8>& rKey, CryptoHashType eType)
    : mnHashSize(getSizeForHashType(eType))
{
    mpImpl->setupCryptoHashContext(rKey, eType);
}

bool CryptoHash::update(std::vector<sal_uInt8>& rInput, sal_uInt32 nInputLength)
{
    sal_uInt32 nActualInputLength
        = (nInputLength == 0 || nInputLength > rInput.size()) ? rInput.size() : nInputLength;
    return mpImpl->cryptoHashUpdate(rInput, nActualInputLength);
}

std::vector<sal_uInt8> CryptoHash::finalize()
{
    std::vector<sal_uInt8> aHash(mnHashSize, 0);
    mpImpl->cryptoHashFinalize(aHash);
    return aHash;
}

} // namespace comphelper

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */