Commit 4287fcc002488327ddea203365410a2361ae1e0b

Authored by Jay Berkenbilt
1 parent 0cdcd102

RC4: switch to pluggable crypto

include/qpdf/QPDFCryptoImpl.hh
@@ -50,6 +50,16 @@ class QPDF_DLL_CLASS QPDFCryptoImpl @@ -50,6 +50,16 @@ class QPDF_DLL_CLASS QPDFCryptoImpl
50 virtual void MD5_finalize() = 0; 50 virtual void MD5_finalize() = 0;
51 QPDF_DLL 51 QPDF_DLL
52 virtual void MD5_digest(MD5_Digest) = 0; 52 virtual void MD5_digest(MD5_Digest) = 0;
  53 +
  54 + // key_len of -1 means treat key_data as a null-terminated string
  55 + QPDF_DLL
  56 + virtual void RC4_init(unsigned char const* key_data, int key_len = -1) = 0;
  57 + // out_data = 0 means to encrypt/decrypt in place
  58 + QPDF_DLL
  59 + virtual void RC4_process(unsigned char* in_data, size_t len,
  60 + unsigned char* out_data = 0) = 0;
  61 + QPDF_DLL
  62 + virtual void RC4_finalize() = 0;
53 }; 63 };
54 64
55 #endif // QPDFCRYPTOIMPL_HH 65 #endif // QPDFCRYPTOIMPL_HH
libqpdf/QPDFCrypto_native.cc
@@ -25,3 +25,20 @@ QPDFCrypto_native::MD5_digest(MD5_Digest d) @@ -25,3 +25,20 @@ QPDFCrypto_native::MD5_digest(MD5_Digest d)
25 this->md5->digest(d); 25 this->md5->digest(d);
26 } 26 }
27 27
  28 +void
  29 +QPDFCrypto_native::RC4_init(unsigned char const* key_data, int key_len)
  30 +{
  31 + this->rc4 = std::make_shared<RC4_native>(key_data, key_len);
  32 +}
  33 +
  34 +void
  35 +QPDFCrypto_native::RC4_process(unsigned char* in_data, size_t len,
  36 + unsigned char* out_data)
  37 +{
  38 + this->rc4->process(in_data, len, out_data);
  39 +}
  40 +
  41 +void
  42 +QPDFCrypto_native::RC4_finalize()
  43 +{
  44 +}
libqpdf/RC4.cc 0 → 100644
  1 +#include <qpdf/RC4.hh>
  2 +#include <qpdf/QPDFCryptoProvider.hh>
  3 +
  4 +#include <string.h>
  5 +
  6 +RC4::RC4(unsigned char const* key_data, int key_len) :
  7 + crypto(QPDFCryptoProvider::getImpl())
  8 +{
  9 + this->crypto->RC4_init(key_data, key_len);
  10 +}
  11 +
  12 +void
  13 +RC4::process(unsigned char *in_data, size_t len, unsigned char* out_data)
  14 +{
  15 + this->crypto->RC4_process(in_data, len, out_data);
  16 +}
libqpdf/RC4_native.cc
1 -#include <qpdf/RC4.hh> 1 +#include <qpdf/RC4_native.hh>
2 #include <qpdf/QIntC.hh> 2 #include <qpdf/QIntC.hh>
3 3
4 #include <string.h> 4 #include <string.h>
@@ -12,7 +12,7 @@ static void swap_byte(unsigned char &amp;a, unsigned char &amp;b) @@ -12,7 +12,7 @@ static void swap_byte(unsigned char &amp;a, unsigned char &amp;b)
12 b = t; 12 b = t;
13 } 13 }
14 14
15 -RC4::RC4(unsigned char const* key_data, int key_len) 15 +RC4_native::RC4_native(unsigned char const* key_data, int key_len)
16 { 16 {
17 if (key_len == -1) 17 if (key_len == -1)
18 { 18 {
@@ -38,7 +38,7 @@ RC4::RC4(unsigned char const* key_data, int key_len) @@ -38,7 +38,7 @@ RC4::RC4(unsigned char const* key_data, int key_len)
38 } 38 }
39 39
40 void 40 void
41 -RC4::process(unsigned char *in_data, size_t len, unsigned char* out_data) 41 +RC4_native::process(unsigned char *in_data, size_t len, unsigned char* out_data)
42 { 42 {
43 if (out_data == 0) 43 if (out_data == 0)
44 { 44 {
libqpdf/build.mk
@@ -78,6 +78,7 @@ SRCS_libqpdf = \ @@ -78,6 +78,7 @@ SRCS_libqpdf = \
78 libqpdf/QTC.cc \ 78 libqpdf/QTC.cc \
79 libqpdf/QUtil.cc \ 79 libqpdf/QUtil.cc \
80 libqpdf/RC4.cc \ 80 libqpdf/RC4.cc \
  81 + libqpdf/RC4_native.cc \
81 libqpdf/SecureRandomDataProvider.cc \ 82 libqpdf/SecureRandomDataProvider.cc \
82 libqpdf/SparseOHArray.cc \ 83 libqpdf/SparseOHArray.cc \
83 libqpdf/qpdf-c.cc \ 84 libqpdf/qpdf-c.cc \
libqpdf/qpdf/QPDFCrypto_native.hh
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 #include <qpdf/DLL.h> 4 #include <qpdf/DLL.h>
5 #include <qpdf/QPDFCryptoImpl.hh> 5 #include <qpdf/QPDFCryptoImpl.hh>
6 #include <qpdf/MD5_native.hh> 6 #include <qpdf/MD5_native.hh>
  7 +#include <qpdf/RC4_native.hh>
7 #include <memory> 8 #include <memory>
8 9
9 class QPDFCrypto_native: public QPDFCryptoImpl 10 class QPDFCrypto_native: public QPDFCryptoImpl
@@ -19,8 +20,14 @@ class QPDFCrypto_native: public QPDFCryptoImpl @@ -19,8 +20,14 @@ class QPDFCrypto_native: public QPDFCryptoImpl
19 virtual void MD5_finalize(); 20 virtual void MD5_finalize();
20 virtual void MD5_digest(MD5_Digest); 21 virtual void MD5_digest(MD5_Digest);
21 22
  23 + virtual void RC4_init(unsigned char const* key_data, int key_len = -1);
  24 + virtual void RC4_process(unsigned char* in_data, size_t len,
  25 + unsigned char* out_data = 0);
  26 + virtual void RC4_finalize();
  27 +
22 private: 28 private:
23 std::shared_ptr<MD5_native> md5; 29 std::shared_ptr<MD5_native> md5;
  30 + std::shared_ptr<RC4_native> rc4;
24 }; 31 };
25 32
26 #endif // QPDFCRYPTO_NATIVE_HH 33 #endif // QPDFCRYPTO_NATIVE_HH
libqpdf/qpdf/RC4.hh 0 → 100644
  1 +#ifndef RC4_HH
  2 +#define RC4_HH
  3 +
  4 +#include <qpdf/QPDFCryptoImpl.hh>
  5 +#include <memory>
  6 +#include <cstring>
  7 +
  8 +class RC4
  9 +{
  10 + public:
  11 + // key_len of -1 means treat key_data as a null-terminated string
  12 + QPDF_DLL
  13 + RC4(unsigned char const* key_data, int key_len = -1);
  14 +
  15 + // out_data = 0 means to encrypt/decrypt in place
  16 + QPDF_DLL
  17 + void process(unsigned char* in_data, size_t len,
  18 + unsigned char* out_data = 0);
  19 +
  20 + private:
  21 + std::shared_ptr<QPDFCryptoImpl> crypto;
  22 +};
  23 +
  24 +#endif // RC4_HH
libqpdf/qpdf/RC4_native.hh
1 -#ifndef RC4_HH  
2 -#define RC4_HH 1 +#ifndef RC4_NATIVE_HH
  2 +#define RC4_NATIVE_HH
3 3
4 -#include <stddef.h> 4 +#include <cstring>
5 5
6 -class RC4 6 +class RC4_native
7 { 7 {
8 public: 8 public:
9 // key_len of -1 means treat key_data as a null-terminated string 9 // key_len of -1 means treat key_data as a null-terminated string
10 - RC4(unsigned char const* key_data, int key_len = -1); 10 + RC4_native(unsigned char const* key_data, int key_len = -1);
11 11
12 // out_data = 0 means to encrypt/decrypt in place 12 // out_data = 0 means to encrypt/decrypt in place
13 void process(unsigned char* in_data, size_t len, 13 void process(unsigned char* in_data, size_t len,
@@ -25,4 +25,4 @@ class RC4 @@ -25,4 +25,4 @@ class RC4
25 RC4Key key; 25 RC4Key key;
26 }; 26 };
27 27
28 -#endif // RC4_HH 28 +#endif // RC4_NATIVE_HH