From 0cdcd10228b1845a1c66ba97527e612eed1f5e2f Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Mon, 4 Nov 2019 08:57:40 -0500 Subject: [PATCH] Rename RC4 implementation (non-bisectable) --- libqpdf/RC4.cc | 57 --------------------------------------------------------- libqpdf/RC4_native.cc | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ libqpdf/qpdf/RC4.hh | 28 ---------------------------- libqpdf/qpdf/RC4_native.hh | 28 ++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 85 deletions(-) delete mode 100644 libqpdf/RC4.cc create mode 100644 libqpdf/RC4_native.cc delete mode 100644 libqpdf/qpdf/RC4.hh create mode 100644 libqpdf/qpdf/RC4_native.hh diff --git a/libqpdf/RC4.cc b/libqpdf/RC4.cc deleted file mode 100644 index 8ab242a..0000000 --- a/libqpdf/RC4.cc +++ /dev/null @@ -1,57 +0,0 @@ -#include -#include - -#include - -static void swap_byte(unsigned char &a, unsigned char &b) -{ - unsigned char t; - - t = a; - a = b; - b = t; -} - -RC4::RC4(unsigned char const* key_data, int key_len) -{ - if (key_len == -1) - { - key_len = QIntC::to_int( - strlen(reinterpret_cast(key_data))); - } - - for (int i = 0; i < 256; ++i) - { - key.state[i] = static_cast(i); - } - key.x = 0; - key.y = 0; - - int i1 = 0; - int i2 = 0; - for (int i = 0; i < 256; ++i) - { - i2 = (key_data[i1] + key.state[i] + i2) % 256; - swap_byte(key.state[i], key.state[i2]); - i1 = (i1 + 1) % key_len; - } -} - -void -RC4::process(unsigned char *in_data, size_t len, unsigned char* out_data) -{ - if (out_data == 0) - { - // Convert in place - out_data = in_data; - } - - for (size_t i = 0; i < len; ++i) - { - key.x = static_cast((key.x + 1) % 256); - key.y = static_cast((key.state[key.x] + key.y) % 256); - swap_byte(key.state[key.x], key.state[key.y]); - int xor_index = (key.state[key.x] + key.state[key.y]) % 256; - out_data[i] = in_data[i] ^ key.state[xor_index]; - } -} diff --git a/libqpdf/RC4_native.cc b/libqpdf/RC4_native.cc new file mode 100644 index 0000000..8ab242a --- /dev/null +++ b/libqpdf/RC4_native.cc @@ -0,0 +1,57 @@ +#include +#include + +#include + +static void swap_byte(unsigned char &a, unsigned char &b) +{ + unsigned char t; + + t = a; + a = b; + b = t; +} + +RC4::RC4(unsigned char const* key_data, int key_len) +{ + if (key_len == -1) + { + key_len = QIntC::to_int( + strlen(reinterpret_cast(key_data))); + } + + for (int i = 0; i < 256; ++i) + { + key.state[i] = static_cast(i); + } + key.x = 0; + key.y = 0; + + int i1 = 0; + int i2 = 0; + for (int i = 0; i < 256; ++i) + { + i2 = (key_data[i1] + key.state[i] + i2) % 256; + swap_byte(key.state[i], key.state[i2]); + i1 = (i1 + 1) % key_len; + } +} + +void +RC4::process(unsigned char *in_data, size_t len, unsigned char* out_data) +{ + if (out_data == 0) + { + // Convert in place + out_data = in_data; + } + + for (size_t i = 0; i < len; ++i) + { + key.x = static_cast((key.x + 1) % 256); + key.y = static_cast((key.state[key.x] + key.y) % 256); + swap_byte(key.state[key.x], key.state[key.y]); + int xor_index = (key.state[key.x] + key.state[key.y]) % 256; + out_data[i] = in_data[i] ^ key.state[xor_index]; + } +} diff --git a/libqpdf/qpdf/RC4.hh b/libqpdf/qpdf/RC4.hh deleted file mode 100644 index a2aa5dc..0000000 --- a/libqpdf/qpdf/RC4.hh +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef RC4_HH -#define RC4_HH - -#include - -class RC4 -{ - public: - // key_len of -1 means treat key_data as a null-terminated string - RC4(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, - unsigned char* out_data = 0); - - private: - class RC4Key - { - public: - unsigned char state[256]; - unsigned char x; - unsigned char y; - }; - - RC4Key key; -}; - -#endif // RC4_HH diff --git a/libqpdf/qpdf/RC4_native.hh b/libqpdf/qpdf/RC4_native.hh new file mode 100644 index 0000000..a2aa5dc --- /dev/null +++ b/libqpdf/qpdf/RC4_native.hh @@ -0,0 +1,28 @@ +#ifndef RC4_HH +#define RC4_HH + +#include + +class RC4 +{ + public: + // key_len of -1 means treat key_data as a null-terminated string + RC4(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, + unsigned char* out_data = 0); + + private: + class RC4Key + { + public: + unsigned char state[256]; + unsigned char x; + unsigned char y; + }; + + RC4Key key; +}; + +#endif // RC4_HH -- libgit2 0.21.4