summaryrefslogtreecommitdiff
path: root/mpq-crypto.c
blob: 7ddbad5c297c281d7980c8be074c9dbca41560ad (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <assert.h>
#include <string.h>

#include "mpq-crypto.h"

static int crypt_table_initialized = 0;
static uint32_t crypt_table[0x500];

/****TODO****/
/* Re-implement various endianess fixes. */

const uint32_t *__mpqlib_get_cryptography_table()
{
    assert(crypt_table_initialized);
    return crypt_table;
}

void __mpqlib_init_cryptography()
{
   // Prepare crypt_table
    uint32_t seed = 0x00100001;
    uint32_t index1 = 0;
    uint32_t index2 = 0;
    int32_t i;

    if (!crypt_table_initialized) {
        crypt_table_initialized = 1;

        for (index1 = 0; index1 < 0x100; index1++) {
            for (index2 = index1, i = 0; i < 5; i++, index2 += 0x100) {
                uint32_t temp1, temp2;

                seed = (seed * 125 + 3) % 0x2AAAAB;
                temp1 = (seed & 0xFFFF) << 0x10;

                seed = (seed * 125 + 3) % 0x2AAAAB;
                temp2 = (seed & 0xFFFF);

                crypt_table[index2] = (temp1 | temp2);
            }
        }
    }
}

void __mpqlib_encrypt(void *_data, uint32_t length, uint32_t key, char disable_input_swapping)
{
    char * data = (char *) _data;
    uint32_t *buffer32 = (uint32_t *) data;
    uint32_t seed = 0xEEEEEEEE;
    uint32_t ch;
    
    assert(crypt_table_initialized);
    assert(data);

   // Round to 4 bytes
    length = length / 4;

   // We duplicate the loop to avoid costly branches
    if (disable_input_swapping) {
        while (length-- > 0) {
            seed += crypt_table[0x400 + (key & 0xFF)];
            ch = *buffer32 ^ (key + seed);

            key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B);
            seed = *buffer32 + seed + (seed << 5) + 3;

            *buffer32++ = ch;
        }
    } else {
        while (length-- > 0) {
            seed += crypt_table[0x400 + (key & 0xFF)];
            ch = *buffer32 ^ (key + seed);

            key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B);
            seed = *buffer32 + seed + (seed << 5) + 3;

            *buffer32++ = ch;
        }
    }
}

void __mpqlib_decrypt(void *_data, uint32_t length, uint32_t key, char disable_output_swapping)
{
    char * data = (char *) _data;
    uint32_t *buffer32 = (uint32_t *) data;
    uint32_t seed = 0xEEEEEEEE;
    uint32_t ch;

    assert(crypt_table_initialized);
    assert(data);

   // Round to 4 bytes
    length = length / 4;

    if (disable_output_swapping) {
        while (length-- > 0) {
            ch = *buffer32;

            seed += crypt_table[0x400 + (key & 0xFF)];
            ch = ch ^ (key + seed);

            key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B);
            seed = ch + seed + (seed << 5) + 3;

            *buffer32++ = ch;
        }

    } else {
        while (length-- > 0) {
            ch = *buffer32;

            seed += crypt_table[0x400 + (key & 0xFF)];
            ch = ch ^ (key + seed);

            key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B);
            seed = ch + seed + (seed << 5) + 3;

            *buffer32++ = ch;
        }
    }
}

uint32_t __mpqlib_hash_cstring(const char *string, uint32_t type)
{
    uint32_t seed1 = 0x7FED7FED;
    uint32_t seed2 = 0xEEEEEEEE;
    uint32_t shifted_type = (type << 8);
    int32_t ch;

    assert(crypt_table_initialized);
    assert(string);

    while (*string != 0) {
        ch = *string++;
        if (ch > 0x60 && ch < 0x7b)
            ch -= 0x20;

        seed1 = crypt_table[shifted_type + ch] ^ (seed1 + seed2);
        seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3;
    }

    return seed1;
}

uint32_t __mpqlib_hash_data(const char *data, uint32_t length, uint32_t type)
{
    uint32_t seed1 = 0x7FED7FED;
    uint32_t seed2 = 0xEEEEEEEE;
    uint32_t shifted_type = (type << 8);
    int32_t ch;

    assert(crypt_table_initialized);
    assert(data);

    while (length > 0) {
        ch = *data++;

        seed1 = crypt_table[shifted_type + ch] ^ (seed1 + seed2);
        seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3;
        length--;
    }

    return seed1;
}