diff options
author | Jay Berkenbilt <ejb@ql.org> | 2019-11-04 15:04:25 +0100 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2019-11-09 14:18:02 +0100 |
commit | 4287fcc002488327ddea203365410a2361ae1e0b (patch) | |
tree | 45009d3f5aad477c582c9fcecde3a126bf2eb8c4 | |
parent | 0cdcd10228b1845a1c66ba97527e612eed1f5e2f (diff) | |
download | qpdf-4287fcc002488327ddea203365410a2361ae1e0b.tar.zst |
RC4: switch to pluggable crypto
-rw-r--r-- | include/qpdf/QPDFCryptoImpl.hh | 10 | ||||
-rw-r--r-- | libqpdf/QPDFCrypto_native.cc | 17 | ||||
-rw-r--r-- | libqpdf/RC4.cc | 16 | ||||
-rw-r--r-- | libqpdf/RC4_native.cc | 6 | ||||
-rw-r--r-- | libqpdf/build.mk | 1 | ||||
-rw-r--r-- | libqpdf/qpdf/QPDFCrypto_native.hh | 7 | ||||
-rw-r--r-- | libqpdf/qpdf/RC4.hh | 24 | ||||
-rw-r--r-- | libqpdf/qpdf/RC4_native.hh | 12 |
8 files changed, 84 insertions, 9 deletions
diff --git a/include/qpdf/QPDFCryptoImpl.hh b/include/qpdf/QPDFCryptoImpl.hh index 8fa8b21e..b19f0112 100644 --- a/include/qpdf/QPDFCryptoImpl.hh +++ b/include/qpdf/QPDFCryptoImpl.hh @@ -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 diff --git a/libqpdf/QPDFCrypto_native.cc b/libqpdf/QPDFCrypto_native.cc index 1ee73782..a6ccee9b 100644 --- a/libqpdf/QPDFCrypto_native.cc +++ b/libqpdf/QPDFCrypto_native.cc @@ -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() +{ +} diff --git a/libqpdf/RC4.cc b/libqpdf/RC4.cc new file mode 100644 index 00000000..ae16f7f0 --- /dev/null +++ b/libqpdf/RC4.cc @@ -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); +} diff --git a/libqpdf/RC4_native.cc b/libqpdf/RC4_native.cc index 8ab242a0..1e5c50f9 100644 --- a/libqpdf/RC4_native.cc +++ b/libqpdf/RC4_native.cc @@ -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) { diff --git a/libqpdf/build.mk b/libqpdf/build.mk index 9f1244d0..883132ae 100644 --- a/libqpdf/build.mk +++ b/libqpdf/build.mk @@ -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 \ diff --git a/libqpdf/qpdf/QPDFCrypto_native.hh b/libqpdf/qpdf/QPDFCrypto_native.hh index 77179075..5cca0264 100644 --- a/libqpdf/qpdf/QPDFCrypto_native.hh +++ b/libqpdf/qpdf/QPDFCrypto_native.hh @@ -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 diff --git a/libqpdf/qpdf/RC4.hh b/libqpdf/qpdf/RC4.hh new file mode 100644 index 00000000..22c43b36 --- /dev/null +++ b/libqpdf/qpdf/RC4.hh @@ -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 diff --git a/libqpdf/qpdf/RC4_native.hh b/libqpdf/qpdf/RC4_native.hh index a2aa5dce..08b3fc68 100644 --- a/libqpdf/qpdf/RC4_native.hh +++ b/libqpdf/qpdf/RC4_native.hh @@ -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 |