-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathphp_xxtea.c
383 lines (321 loc) · 11.2 KB
/
php_xxtea.c
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**********************************************************\
| |
| php_xxtea.c |
| |
| XXTEA for pecl source file. |
| |
| Encryption Algorithm Authors: |
| David J. Wheeler |
| Roger M. Needham |
| |
| Code Author: Ma Bingyao <[email protected]> |
| LastModified: Feb 7, 2016 |
| |
\**********************************************************/
#include "php_xxtea.h"
#include "ext/standard/info.h" /* for phpinfo() functions */
#if defined(_MSC_VER) && _MSC_VER < 1920
#include "win32/php_stdint.h"
#elif defined(__FreeBSD__) && __FreeBSD__ < 5
/* FreeBSD 4 doesn't have stdint.h file */
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#include <sys/types.h> /* This will likely define BYTE_ORDER */
#ifndef BYTE_ORDER
#if (BSD >= 199103)
# include <machine/endian.h>
#else
#if defined(linux) || defined(__linux__)
# include <endian.h>
#else
#define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */
#define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */
#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp)*/
#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
defined(vax) || defined(ns32000) || defined(sun386) || \
defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \
defined(__alpha__) || defined(__alpha)
#define BYTE_ORDER LITTLE_ENDIAN
#endif
#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \
defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \
defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\
defined(apollo) || defined(__convex__) || defined(_CRAY) || \
defined(__hppa) || defined(__hp9000) || \
defined(__hp9000s300) || defined(__hp9000s700) || \
defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)
#define BYTE_ORDER BIG_ENDIAN
#endif
#endif /* linux */
#endif /* BSD */
#endif /* BYTE_ORDER */
#ifndef BYTE_ORDER
#ifdef __BYTE_ORDER
#if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN __LITTLE_ENDIAN
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN __BIG_ENDIAN
#endif
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
#define BYTE_ORDER LITTLE_ENDIAN
#else
#define BYTE_ORDER BIG_ENDIAN
#endif
#endif
#endif
#endif
#if PHP_MAJOR_VERSION < 7
typedef int length_t;
#define __RETURN_STRINGL(s, l) RETURN_STRINGL(s, l, 0)
#define __add_assoc_string(arg, key, str) add_assoc_string(arg, key, str, 1)
#else
typedef size_t length_t;
#define __RETURN_STRINGL(s, l) RETVAL_STRINGL(s, l); efree(s); return;
#define __add_assoc_string(arg, key, str) add_assoc_string(arg, key, str)
#endif
#define MX (((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))
#define DELTA 0x9e3779b9
static uint32_t * xxtea_to_uint_array(const uint8_t * data, size_t len, int inc_len, size_t * out_len) {
uint32_t *out;
#if !(defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN))
size_t i;
#endif
size_t n;
n = (((len & 3) == 0) ? (len >> 2) : ((len >> 2) + 1));
if (inc_len) {
out = ecalloc(n + 1, sizeof(uint32_t));
out[n] = (uint32_t)len;
*out_len = n + 1;
}
else {
out = ecalloc(n, sizeof(uint32_t));
*out_len = n;
}
#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
memcpy(out, data, len);
#else
for (i = 0; i < len; ++i) {
out[i >> 2] |= (uint32_t)data[i] << ((i & 3) << 3);
}
#endif
return out;
}
static uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, int inc_len, size_t * out_len) {
uint8_t *out;
#if !(defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN))
size_t i;
#endif
size_t m, n;
n = len << 2;
if (inc_len) {
m = data[len - 1];
n -= 4;
if ((m < n - 3) || (m > n)) return NULL;
n = m;
}
out = (uint8_t *)emalloc(n + 1);
#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
memcpy(out, data, n);
#else
for (i = 0; i < n; ++i) {
out[i] = (uint8_t)(data[i >> 2] >> ((i & 3) << 3));
}
#endif
out[n] = '\0';
*out_len = n;
return out;
}
static uint32_t * xxtea_uint_encrypt(uint32_t * data, size_t len, uint32_t * key) {
uint32_t n = (uint32_t)len - 1;
uint32_t z = data[n], y, p, q = 6 + 52 / (n + 1), sum = 0, e;
if (n < 1) return data;
while (0 < q--) {
sum += DELTA;
e = sum >> 2 & 3;
for (p = 0; p < n; p++) {
y = data[p + 1];
z = data[p] += MX;
}
y = data[0];
z = data[n] += MX;
}
return data;
}
static uint32_t * xxtea_uint_decrypt(uint32_t * data, size_t len, uint32_t * key) {
uint32_t n = (uint32_t)len - 1;
uint32_t z, y = data[0], p, q = 6 + 52 / (n + 1), sum = q * DELTA, e;
if (n < 1) return data;
while (sum != 0) {
e = sum >> 2 & 3;
for (p = n; p > 0; p--) {
z = data[p - 1];
y = data[p] -= MX;
}
z = data[n];
y = data[0] -= MX;
sum -= DELTA;
}
return data;
}
static uint8_t * xxtea_ubyte_encrypt(const uint8_t * data, size_t len, const uint8_t * key, size_t * out_len) {
uint8_t *out;
uint32_t *data_array, *key_array;
size_t data_len, key_len;
if (!len) return NULL;
data_array = xxtea_to_uint_array(data, len, 1, &data_len);
key_array = xxtea_to_uint_array(key, 16, 0, &key_len);
out = xxtea_to_ubyte_array(xxtea_uint_encrypt(data_array, data_len, key_array), data_len, 0, out_len);
efree(data_array);
efree(key_array);
return out;
}
static uint8_t * xxtea_ubyte_decrypt(const uint8_t * data, size_t len, const uint8_t * key, size_t * out_len) {
uint8_t *out;
uint32_t *data_array, *key_array;
size_t data_len, key_len;
if (!len) return NULL;
data_array = xxtea_to_uint_array(data, len, 0, &data_len);
key_array = xxtea_to_uint_array(key, 16, 0, &key_len);
out = xxtea_to_ubyte_array(xxtea_uint_decrypt(data_array, data_len, key_array), data_len, 1, out_len);
efree(data_array);
efree(key_array);
return out;
}
ZEND_BEGIN_ARG_INFO_EX(xxtea_encrypt_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(xxtea_decrypt_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, key)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(xxtea_info_arginfo, 0, 0, 0)
ZEND_END_ARG_INFO()
#ifndef ZEND_FE_END
#define ZEND_FE_END { NULL, NULL, NULL }
#endif
/* compiled function list so Zend knows what's in this module */
zend_function_entry xxtea_functions[] = {
ZEND_FE(xxtea_encrypt, xxtea_encrypt_arginfo)
ZEND_FE(xxtea_decrypt, xxtea_decrypt_arginfo)
ZEND_FE(xxtea_info, xxtea_info_arginfo)
ZEND_FE_END
};
/* compiled module information */
zend_module_entry xxtea_module_entry = {
STANDARD_MODULE_HEADER,
PHP_XXTEA_MODULE_NAME,
xxtea_functions,
ZEND_MINIT(xxtea),
ZEND_MSHUTDOWN(xxtea),
NULL,
NULL,
ZEND_MINFO(xxtea),
PHP_XXTEA_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* implement standard "stub" routine to introduce ourselves to Zend */
#if defined(COMPILE_DL_XXTEA)
ZEND_GET_MODULE(xxtea)
#endif
/* {{{ proto string xxtea_encrypt(string data, string key)
Encrypt string using XXTEA algorithm */
ZEND_FUNCTION(xxtea_encrypt) {
char *data, *key;
char *result;
length_t data_len, key_len;
size_t i, ret_length;
uint8_t fixed_key[16];
#ifdef TSRMLS_CC
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &data, &data_len, &key, &key_len) == FAILURE) {
#else
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &data, &data_len, &key, &key_len) == FAILURE) {
#endif
return;
}
if (data_len == 0) {
RETURN_EMPTY_STRING();
}
if (key_len < 16) {
memcpy(fixed_key, key, key_len);
for (i = key_len; i < 16; ++i) fixed_key[i] = 0;
}
else if (key_len >= 16) {
memcpy(fixed_key, key, 16);
}
result = (char *)xxtea_ubyte_encrypt((const uint8_t *)data, (size_t)data_len, fixed_key, &ret_length);
if (result == NULL) {
RETURN_FALSE;
}
__RETURN_STRINGL(result, ret_length);
}
/* }}} */
/* {{{ proto string xxtea_decrypt(string data, string key)
Decrypt string using XXTEA algorithm */
ZEND_FUNCTION(xxtea_decrypt) {
char *data, *key;
char *result;
length_t data_len, key_len;
size_t i, ret_length;
uint8_t fixed_key[16];
#ifdef TSRMLS_CC
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &data, &data_len, &key, &key_len) == FAILURE) {
#else
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &data, &data_len, &key, &key_len) == FAILURE) {
#endif
return;
}
if (data_len == 0) {
RETURN_EMPTY_STRING();
}
if (key_len < 16) {
memcpy(fixed_key, key, key_len);
for (i = key_len; i < 16; ++i) fixed_key[i] = 0;
}
else if (key_len >= 16) {
memcpy(fixed_key, key, 16);
}
result = (char *)xxtea_ubyte_decrypt((const uint8_t *)data, (size_t)data_len, fixed_key, &ret_length);
if (result == NULL) {
RETURN_FALSE;
}
__RETURN_STRINGL(result, ret_length);
}
/* }}} */
zend_class_entry *xxtea_ce;
static zend_function_entry xxtea_method[] = {
ZEND_ME_MAPPING(encrypt, xxtea_encrypt, xxtea_encrypt_arginfo, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
ZEND_ME_MAPPING(decrypt, xxtea_decrypt, xxtea_decrypt_arginfo, ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
ZEND_FE_END
};
ZEND_MINIT_FUNCTION(xxtea) {
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "XXTEA", xxtea_method);
#ifdef TSRMLS_CC
xxtea_ce = zend_register_internal_class(&ce TSRMLS_CC);
#else
xxtea_ce = zend_register_internal_class(&ce);
#endif
return SUCCESS;
}
ZEND_MSHUTDOWN_FUNCTION(xxtea) {
return SUCCESS;
}
ZEND_MINFO_FUNCTION(xxtea) {
php_info_print_table_start();
php_info_print_table_row(2, "xxtea support", "enabled");
php_info_print_table_row(2, "xxtea version", PHP_XXTEA_VERSION);
php_info_print_table_row(2, "xxtea author", PHP_XXTEA_AUTHOR);
php_info_print_table_row(2, "xxtea homepage", PHP_XXTEA_HOMEPAGE);
php_info_print_table_end();
}
ZEND_FUNCTION(xxtea_info) {
array_init(return_value);
__add_assoc_string(return_value, "ext_version", PHP_XXTEA_VERSION);
__add_assoc_string(return_value, "ext_build_date", PHP_XXTEA_BUILD_DATE);
__add_assoc_string(return_value, "ext_author", PHP_XXTEA_AUTHOR);
}