RC4: switch to pluggable crypto

This commit is contained in:
Jay Berkenbilt 2019-11-04 09:04:25 -05:00
parent 0cdcd10228
commit 4287fcc002
8 changed files with 84 additions and 9 deletions

View File

@ -50,6 +50,16 @@ class QPDF_DLL_CLASS QPDFCryptoImpl
virtual void MD5_finalize() = 0;
QPDF_DLL
virtual void MD5_digest(MD5_Digest) = 0;
// key_len of -1 means treat key_data as a null-terminated string
QPDF_DLL
virtual void RC4_init(unsigned char const* key_data, int key_len = -1) = 0;
// out_data = 0 means to encrypt/decrypt in place
QPDF_DLL
virtual void RC4_process(unsigned char* in_data, size_t len,
unsigned char* out_data = 0) = 0;
QPDF_DLL
virtual void RC4_finalize() = 0;
};
#endif // QPDFCRYPTOIMPL_HH

View File

@ -25,3 +25,20 @@ 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* in_data, size_t len,
unsigned char* out_data)
{
this->rc4->process(in_data, len, out_data);
}
void
QPDFCrypto_native::RC4_finalize()
{
}

16
libqpdf/RC4.cc Normal file
View File

@ -0,0 +1,16 @@
#include <qpdf/RC4.hh>
#include <qpdf/QPDFCryptoProvider.hh>
#include <string.h>
RC4::RC4(unsigned char const* key_data, int key_len) :
crypto(QPDFCryptoProvider::getImpl())
{
this->crypto->RC4_init(key_data, key_len);
}
void
RC4::process(unsigned char *in_data, size_t len, unsigned char* out_data)
{
this->crypto->RC4_process(in_data, len, out_data);
}

View File

@ -1,4 +1,4 @@
#include <qpdf/RC4.hh>
#include <qpdf/RC4_native.hh>
#include <qpdf/QIntC.hh>
#include <string.h>
@ -12,7 +12,7 @@ static void swap_byte(unsigned char &a, unsigned char &b)
b = t;
}
RC4::RC4(unsigned char const* key_data, int key_len)
RC4_native::RC4_native(unsigned char const* key_data, int key_len)
{
if (key_len == -1)
{
@ -38,7 +38,7 @@ RC4::RC4(unsigned char const* key_data, int key_len)
}
void
RC4::process(unsigned char *in_data, size_t len, unsigned char* out_data)
RC4_native::process(unsigned char *in_data, size_t len, unsigned char* out_data)
{
if (out_data == 0)
{

View File

@ -78,6 +78,7 @@ SRCS_libqpdf = \
libqpdf/QTC.cc \
libqpdf/QUtil.cc \
libqpdf/RC4.cc \
libqpdf/RC4_native.cc \
libqpdf/SecureRandomDataProvider.cc \
libqpdf/SparseOHArray.cc \
libqpdf/qpdf-c.cc \

View File

@ -4,6 +4,7 @@
#include <qpdf/DLL.h>
#include <qpdf/QPDFCryptoImpl.hh>
#include <qpdf/MD5_native.hh>
#include <qpdf/RC4_native.hh>
#include <memory>
class QPDFCrypto_native: public QPDFCryptoImpl
@ -19,8 +20,14 @@ class QPDFCrypto_native: public QPDFCryptoImpl
virtual void MD5_finalize();
virtual void MD5_digest(MD5_Digest);
virtual void RC4_init(unsigned char const* key_data, int key_len = -1);
virtual void RC4_process(unsigned char* in_data, size_t len,
unsigned char* out_data = 0);
virtual void RC4_finalize();
private:
std::shared_ptr<MD5_native> md5;
std::shared_ptr<RC4_native> rc4;
};
#endif // QPDFCRYPTO_NATIVE_HH

24
libqpdf/qpdf/RC4.hh Normal file
View File

@ -0,0 +1,24 @@
#ifndef RC4_HH
#define RC4_HH
#include <qpdf/QPDFCryptoImpl.hh>
#include <memory>
#include <cstring>
class RC4
{
public:
// key_len of -1 means treat key_data as a null-terminated string
QPDF_DLL
RC4(unsigned char const* key_data, int key_len = -1);
// out_data = 0 means to encrypt/decrypt in place
QPDF_DLL
void process(unsigned char* in_data, size_t len,
unsigned char* out_data = 0);
private:
std::shared_ptr<QPDFCryptoImpl> crypto;
};
#endif // RC4_HH

View File

@ -1,13 +1,13 @@
#ifndef RC4_HH
#define RC4_HH
#ifndef RC4_NATIVE_HH
#define RC4_NATIVE_HH
#include <stddef.h>
#include <cstring>
class RC4
class RC4_native
{
public:
// key_len of -1 means treat key_data as a null-terminated string
RC4(unsigned char const* key_data, int key_len = -1);
RC4_native(unsigned char const* key_data, int key_len = -1);
// out_data = 0 means to encrypt/decrypt in place
void process(unsigned char* in_data, size_t len,
@ -25,4 +25,4 @@ class RC4
RC4Key key;
};
#endif // RC4_HH
#endif // RC4_NATIVE_HH