aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QPDFCrypto_native.cc
blob: 9e9cba5197e31a88623c1dc957b7bf63cc8bd74a (plain) (blame)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <qpdf/QPDFCrypto_native.hh>

#include <qpdf/QUtil.hh>

#ifdef USE_INSECURE_RANDOM
# include <qpdf/InsecureRandomDataProvider.hh>
#endif
#include <qpdf/SecureRandomDataProvider.hh>

static RandomDataProvider*
getRandomProvider()
{
#ifdef USE_INSECURE_RANDOM
    static RandomDataProvider* insecure_random_data_provider =
        InsecureRandomDataProvider::getInstance();
#else
    static RandomDataProvider* insecure_random_data_provider = nullptr;
#endif
    static RandomDataProvider* secure_random_data_provider =
        SecureRandomDataProvider::getInstance();

    static RandomDataProvider* provider =
        (secure_random_data_provider         ? secure_random_data_provider
             : insecure_random_data_provider ? insecure_random_data_provider
                                             : nullptr);

    if (provider == nullptr) {
        throw std::logic_error("QPDFCrypto_native has no random data provider");
    }

    return provider;
}

void
QPDFCrypto_native::provideRandomData(unsigned char* data, size_t len)
{
    getRandomProvider()->provideRandomData(data, len);
}

void
QPDFCrypto_native::MD5_init()
{
    this->md5 = std::make_shared<MD5_native>();
}

void
QPDFCrypto_native::MD5_update(unsigned char const* data, size_t len)
{
    this->md5->update(const_cast<unsigned char*>(data), len);
}

void
QPDFCrypto_native::MD5_finalize()
{
    this->md5->finalize();
}

void
QPDFCrypto_native::MD5_digest(MD5_Digest d)
{
    this->md5->digest(d);
}

void
QPDFCrypto_native::RC4_init(unsigned char const* key_data, int key_len)
{
    this->rc4 = std::make_shared<RC4_native>(key_data, key_len);
}

void
QPDFCrypto_native::RC4_process(unsigned char const* in_data, size_t len, unsigned char* out_data)
{
    this->rc4->process(in_data, len, out_data);
}

void
QPDFCrypto_native::RC4_finalize()
{
}

void
QPDFCrypto_native::SHA2_init(int bits)
{
    this->sha2 = std::make_shared<SHA2_native>(bits);
}

void
QPDFCrypto_native::SHA2_update(unsigned char const* data, size_t len)
{
    this->sha2->update(data, len);
}

void
QPDFCrypto_native::SHA2_finalize()
{
    this->sha2->finalize();
}

std::string
QPDFCrypto_native::SHA2_digest()
{
    return this->sha2->getRawDigest();
}

void
QPDFCrypto_native::rijndael_init(
    bool encrypt,
    unsigned char const* key_data,
    size_t key_len,
    bool cbc_mode,
    unsigned char* cbc_block)

{
    this->aes_pdf =
        std::make_shared<AES_PDF_native>(encrypt, key_data, key_len, cbc_mode, cbc_block);
}

void
QPDFCrypto_native::rijndael_process(unsigned char* in_data, unsigned char* out_data)
{
    this->aes_pdf->update(in_data, out_data);
}

void
QPDFCrypto_native::rijndael_finalize()
{
}