-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathtc_tea.cpp
executable file
·468 lines (354 loc) · 12.7 KB
/
tc_tea.cpp
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "util/tc_tea.h"
#include <iostream>
#include <sstream>
#include <stdlib.h>
#if TARGET_PLATFORM_LINUX
#include <arpa/inet.h>
#endif
#if TARGET_PLATFORM_WINDOWS
#pragma comment(lib,"ws2_32.lib")
#endif
namespace tars
{
#ifndef WORD32
typedef unsigned int WORD32;
#endif
int oi_symmetry_encrypt2_len(int nInBufLen);
void oi_symmetry_encrypt2(const char *pInBuf, int nInBufLen, const char *pKey, char *pOutBuf, size_t *pOutBufLen);
bool oi_symmetry_decrypt2(const char *pInBuf, int nInBufLen, const char *pKey, char *pOutBuf, size_t *pOutBufLen);
void TC_Tea::encrypt(const char *key, const char *sIn, size_t iLength, vector<char> &buffer)
{
size_t outlen = oi_symmetry_encrypt2_len(iLength);
if (buffer.capacity() < outlen * 2)
{
buffer.resize(outlen * 2);
}
oi_symmetry_encrypt2(sIn, iLength, key, buffer.data(), &outlen);
buffer.resize(outlen);
}
bool TC_Tea::decrypt(const char *key, const char *sIn, size_t iLength, vector<char> &buffer)
{
size_t outlen = iLength;
if (buffer.capacity() < outlen * 2)
{
buffer.resize(outlen * 2);
}
buffer.resize(outlen * 2);
if (!oi_symmetry_decrypt2(sIn, iLength, key, buffer.data(), &outlen))
{
return false;
}
buffer.resize(outlen);
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////
///
const WORD32 DELTA = 0x9e3779b9;
#define ROUNDS 16
#define LOG_ROUNDS 4
#define SALT_LEN 2
#define ZERO_LEN 7
/*pOutBuffer、pInBuffer均为8byte, pKey为16byte*/
void TeaEncryptECB(const char *pInBuf, const char *pKey, char *pOutBuf)
{
WORD32 y, z;
WORD32 sum;
WORD32 k[4];
int i;
/*plain-text is TCP/IP-endian;*/
/*GetBlockBigEndian(in, y, z);*/
y = ntohl(*((WORD32 *)pInBuf));
z = ntohl(*((WORD32 *)(pInBuf + 4)));
/*TCP/IP network byte order (which is big-endian).*/
for (i = 0; i < 4; i++)
{
/*now key is TCP/IP-endian;*/
k[i] = ntohl(*((WORD32 *)(pKey + i * 4)));
}
sum = 0;
for (i = 0; i < ROUNDS; i++)
{
sum += DELTA;
y += ((z << 4) + k[0]) ^ (z + sum) ^ ((z >> 5) + k[1]);
z += ((y << 4) + k[2]) ^ (y + sum) ^ ((y >> 5) + k[3]);
}
*((WORD32 *)pOutBuf) = htonl(y);
*((WORD32 *)(pOutBuf + 4)) = htonl(z);
/*now encrypted buf is TCP/IP-endian;*/
}
/*pOutBuffer、pInBuffer均为8byte, pKey为16byte*/
void TeaDecryptECB(const char *pInBuf, const char *pKey, char *pOutBuf)
{
WORD32 y, z, sum;
WORD32 k[4];
int i;
/*now encrypted buf is TCP/IP-endian;*/
/*TCP/IP network byte order (which is big-endian).*/
y = ntohl(*((WORD32 *)pInBuf));
z = ntohl(*((WORD32 *)(pInBuf + 4)));
for (i = 0; i < 4; i++)
{
/*key is TCP/IP-endian;*/
k[i] = ntohl(*((WORD32 *)(pKey + i * 4)));
}
sum = DELTA << LOG_ROUNDS;
for (i = 0; i < ROUNDS; i++)
{
z -= ((y << 4) + k[2]) ^ (y + sum) ^ ((y >> 5) + k[3]);
y -= ((z << 4) + k[0]) ^ (z + sum) ^ ((z >> 5) + k[1]);
sum -= DELTA;
}
*((WORD32 *)pOutBuf) = htonl(y);
*((WORD32 *)(pOutBuf + 4)) = htonl(z);
/*now plain-text is TCP/IP-endian;*/
}
/*pKey为16byte*/
/*
输入:nInBufLen为需加密的明文部分(Body)长度;
输出:返回为加密后的长度(是8byte的倍数);
*/
/*TEA加密算法,CBC模式*/
/*密文格式:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
int oi_symmetry_encrypt2_len(int nInBufLen)
{
int nPadSaltBodyZeroLen/*PadLen(1byte)+Salt+Body+Zero的长度*/;
int nPadlen;
/*根据Body长度计算PadLen,最小必需长度必需为8byte的整数倍*/
nPadSaltBodyZeroLen = nInBufLen/*Body长度*/ + 1 + SALT_LEN + ZERO_LEN/*PadLen(1byte)+Salt(2byte)+Zero(7byte)*/;
if ((nPadlen = nPadSaltBodyZeroLen % 8)) /*len=nSaltBodyZeroLen%8*/
{
/*模8余0需补0,余1补7,余2补6,...,余7补1*/
nPadlen = 8 - nPadlen;
}
return nPadSaltBodyZeroLen + nPadlen;
}
/*pKey为16byte*/
/*
输入:pInBuf为需加密的明文部分(Body),nInBufLen为pInBuf长度;
输出:pOutBuf为密文格式,pOutBufLen为pOutBuf的长度是8byte的倍数;
*/
/*TEA加密算法,CBC模式*/
/*密文格式:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
void oi_symmetry_encrypt2(const char *pInBuf, int nInBufLen, const char *pKey, char *pOutBuf, size_t *pOutBufLen)
{
int nPadSaltBodyZeroLen/*PadLen(1byte)+Salt+Body+Zero的长度*/;
int nPadlen;
char src_buf[8], iv_plain[8], *iv_crypt;
int src_i, i, j;
/*根据Body长度计算PadLen,最小必需长度必需为8byte的整数倍*/
nPadSaltBodyZeroLen = nInBufLen/*Body长度*/ + 1 + SALT_LEN + ZERO_LEN/*PadLen(1byte)+Salt(2byte)+Zero(7byte)*/;
if ((nPadlen = nPadSaltBodyZeroLen % 8)) /*len=nSaltBodyZeroLen%8*/
{
/*模8余0需补0,余1补7,余2补6,...,余7补1*/
nPadlen = 8 - nPadlen;
}
/*srand( (unsigned)time( NULL ) ); 初始化随机数*/
/*加密第一块数据(8byte),取前面10byte*/
src_buf[0] = (((char)rand()) & 0x0f8)/*最低三位存PadLen,清零*/ | (char)nPadlen;
src_i = 1; /*src_i指向src_buf下一个位置*/
while (nPadlen--) src_buf[src_i++] = (char)rand(); /*Padding*/
/*come here, src_i must <= 8*/
for (i = 0; i < 8; i++) iv_plain[i] = 0;
iv_crypt = iv_plain; /*make zero iv*/
*pOutBufLen = 0; /*init OutBufLen*/
for (i = 1; i <= SALT_LEN;) /*Salt(2byte)*/
{
if (src_i < 8)
{
src_buf[src_i++] = (char)rand();
i++; /*i inc in here*/
}
if (src_i == 8)
{
/*src_i==8*/
for (j = 0; j < 8; j++) /*加密前异或前8个byte的密文(iv_crypt指向的)*/
src_buf[j] ^= iv_crypt[j];
/*pOutBuffer、pInBuffer均为8byte, pKey为16byte*/
/*加密*/
TeaEncryptECB(src_buf, pKey, pOutBuf);
for (j = 0; j < 8; j++) /*加密后异或前8个byte的明文(iv_plain指向的)*/
pOutBuf[j] ^= iv_plain[j];
/*保存当前的iv_plain*/
for (j = 0; j < 8; j++) iv_plain[j] = src_buf[j];
/*更新iv_crypt*/
src_i = 0;
iv_crypt = pOutBuf;
*pOutBufLen += 8;
pOutBuf += 8;
}
}
/*src_i指向src_buf下一个位置*/
while (nInBufLen)
{
if (src_i < 8)
{
src_buf[src_i++] = *(pInBuf++);
nInBufLen--;
}
if (src_i == 8)
{
/*src_i==8*/
for (j = 0; j < 8; j++) /*加密前异或前8个byte的密文(iv_crypt指向的)*/
src_buf[j] ^= iv_crypt[j];
/*pOutBuffer、pInBuffer均为8byte, pKey为16byte*/
TeaEncryptECB(src_buf, pKey, pOutBuf);
for (j = 0; j < 8; j++) /*加密后异或前8个byte的明文(iv_plain指向的)*/
pOutBuf[j] ^= iv_plain[j];
/*保存当前的iv_plain*/
for (j = 0; j < 8; j++) iv_plain[j] = src_buf[j];
src_i = 0;
iv_crypt = pOutBuf;
*pOutBufLen += 8;
pOutBuf += 8;
}
}
/*src_i指向src_buf下一个位置*/
for (i = 1; i <= ZERO_LEN;)
{
if (src_i < 8)
{
src_buf[src_i++] = 0;
i++; /*i inc in here*/
}
if (src_i == 8)
{
/*src_i==8*/
for (j = 0; j < 8; j++) /*加密前异或前8个byte的密文(iv_crypt指向的)*/
src_buf[j] ^= iv_crypt[j];
/*pOutBuffer、pInBuffer均为8byte, pKey为16byte*/
TeaEncryptECB(src_buf, pKey, pOutBuf);
for (j = 0; j < 8; j++) /*加密后异或前8个byte的明文(iv_plain指向的)*/
pOutBuf[j] ^= iv_plain[j];
/*保存当前的iv_plain*/
for (j = 0; j < 8; j++) iv_plain[j] = src_buf[j];
src_i = 0;
iv_crypt = pOutBuf;
*pOutBufLen += 8;
pOutBuf += 8;
}
}
}
/*pKey为16byte*/
/*
输入:pInBuf为密文格式,nInBufLen为pInBuf的长度是8byte的倍数; *pOutBufLen为接收缓冲区的长度
特别注意*pOutBufLen应预置接收缓冲区的长度!
输出:pOutBuf为明文(Body),pOutBufLen为pOutBuf的长度,至少应预留nInBufLen-10;
返回值:如果格式正确返回true;
*/
/*TEA解密算法,CBC模式*/
/*密文格式:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
bool oi_symmetry_decrypt2(const char *pInBuf, int nInBufLen, const char *pKey, char *pOutBuf, size_t *pOutBufLen)
{
int nPadLen, nPlainLen;
char dest_buf[8], zero_buf[8];
const char *iv_pre_crypt, *iv_cur_crypt;
int dest_i, i, j;
// const char *pInBufBoundary;
int nBufPos;
nBufPos = 0;
if ((nInBufLen % 8) || (nInBufLen < 16)) return false;
TeaDecryptECB(pInBuf, pKey, dest_buf);
nPadLen = dest_buf[0] & 0x7/*只要最低三位*/;
/*密文格式:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
i = nInBufLen - 1/*PadLen(1byte)*/ - nPadLen - SALT_LEN - ZERO_LEN; /*明文长度*/
if ((*pOutBufLen < (size_t)i) || (i < 0)) return false;
*pOutBufLen = i;
// pInBufBoundary = pInBuf + nInBufLen; /*输入缓冲区的边界,下面不能pInBuf>=pInBufBoundary*/
for (i = 0; i < 8; i++) zero_buf[i] = 0;
iv_pre_crypt = zero_buf;
iv_cur_crypt = pInBuf; /*init iv*/
pInBuf += 8;
nBufPos += 8;
dest_i = 1; /*dest_i指向dest_buf下一个位置*/
/*把Padding滤掉*/
dest_i += nPadLen;
/*dest_i must <=8*/
/*把Salt滤掉*/
for (i = 1; i <= SALT_LEN;)
{
if (dest_i < 8)
{
dest_i++;
i++;
}
else if (dest_i == 8)
{
/*解开一个新的加密块*/
/*改变前一个加密块的指针*/
iv_pre_crypt = iv_cur_crypt;
iv_cur_crypt = pInBuf;
/*异或前一块明文(在dest_buf[]中)*/
for (j = 0; j < 8; j++)
{
if ((nBufPos + j) >= nInBufLen) return false;
dest_buf[j] ^= pInBuf[j];
}
/*dest_i==8*/
TeaDecryptECB(dest_buf, pKey, dest_buf);
/*在取出的时候才异或前一块密文(iv_pre_crypt)*/
pInBuf += 8;
nBufPos += 8;
dest_i = 0; /*dest_i指向dest_buf下一个位置*/
}
}
/*还原明文*/
nPlainLen = *pOutBufLen;
while (nPlainLen)
{
if (dest_i < 8)
{
*(pOutBuf++) = dest_buf[dest_i] ^ iv_pre_crypt[dest_i];
dest_i++;
nPlainLen--;
}
else if (dest_i == 8)
{
/*dest_i==8*/
/*改变前一个加密块的指针*/
iv_pre_crypt = iv_cur_crypt;
iv_cur_crypt = pInBuf;
/*解开一个新的加密块*/
/*异或前一块明文(在dest_buf[]中)*/
for (j = 0; j < 8; j++)
{
if ((nBufPos + j) >= nInBufLen) return false;
dest_buf[j] ^= pInBuf[j];
}
TeaDecryptECB(dest_buf, pKey, dest_buf);
/*在取出的时候才异或前一块密文(iv_pre_crypt)*/
pInBuf += 8;
nBufPos += 8;
dest_i = 0; /*dest_i指向dest_buf下一个位置*/
}
}
/*校验Zero*/
for (i = 1; i <= ZERO_LEN;)
{
if (dest_i < 8)
{
if (dest_buf[dest_i] ^ iv_pre_crypt[dest_i]) return false;
dest_i++;
i++;
}
else if (dest_i == 8)
{
/*改变前一个加密块的指针*/
iv_pre_crypt = iv_cur_crypt;
iv_cur_crypt = pInBuf;
/*解开一个新的加密块*/
/*异或前一块明文(在dest_buf[]中)*/
for (j = 0; j < 8; j++)
{
if ((nBufPos + j) >= nInBufLen) return false;
dest_buf[j] ^= pInBuf[j];
}
TeaDecryptECB(dest_buf, pKey, dest_buf);
/*在取出的时候才异或前一块密文(iv_pre_crypt)*/
pInBuf += 8;
nBufPos += 8;
dest_i = 0; /*dest_i指向dest_buf下一个位置*/
}
}
return true;
}
}