2019-11-04 01:36:53 +00:00
|
|
|
#include <qpdf/QPDFCrypto_native.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2019-11-04 02:22:03 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
2020-04-06 14:19:37 +00:00
|
|
|
#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
|
2022-07-26 11:37:50 +00:00
|
|
|
static RandomDataProvider* insecure_random_data_provider = nullptr;
|
2020-04-06 14:19:37 +00:00
|
|
|
#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
|
2022-07-26 11:37:50 +00:00
|
|
|
: nullptr);
|
2020-04-06 14:19:37 +00:00
|
|
|
|
2022-07-26 11:37:50 +00:00
|
|
|
if (provider == nullptr) {
|
2020-04-06 14:19:37 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-04 02:22:03 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-04 14:04:25 +00:00
|
|
|
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
|
2022-05-03 21:43:07 +00:00
|
|
|
QPDFCrypto_native::RC4_process(unsigned char const* in_data, size_t len, unsigned char* out_data)
|
2019-11-04 14:04:25 +00:00
|
|
|
{
|
|
|
|
this->rc4->process(in_data, len, out_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFCrypto_native::RC4_finalize()
|
|
|
|
{
|
|
|
|
}
|
2019-11-04 14:55:43 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2019-11-04 18:59:19 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
QPDFCrypto_native::rijndael_init(
|
2019-11-04 19:41:10 +00:00
|
|
|
bool encrypt,
|
|
|
|
unsigned char const* key_data,
|
|
|
|
size_t key_len,
|
|
|
|
bool cbc_mode,
|
|
|
|
unsigned char* cbc_block)
|
|
|
|
|
2019-11-04 18:59:19 +00:00
|
|
|
{
|
|
|
|
this->aes_pdf =
|
2019-11-04 19:41:10 +00:00
|
|
|
std::make_shared<AES_PDF_native>(encrypt, key_data, key_len, cbc_mode, cbc_block);
|
2019-11-04 18:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|