aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/RC4.cc
blob: 7639a3641cb439eeb6911c1b4bc70c8feef78737 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <qpdf/RC4.hh>

#include <string.h>

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 = strlen(reinterpret_cast<char const*>(key_data));
    }

    for (int i = 0; i < 256; ++i)
    {
        key.state[i] = 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, int len, unsigned char* out_data)
{
    if (out_data == 0)
    {
	// Convert in place
	out_data = in_data;
    }

    for (int i = 0; i < len; ++i)
    {
	key.x = (key.x + 1) % 256;
	key.y = (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];
    }
}