-
Notifications
You must be signed in to change notification settings - Fork 20
/
twister.cpp
265 lines (211 loc) · 4.55 KB
/
twister.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
#include <stdio.h>
#include <stdlib.h>
#include "twister.h"
#define FREAD(buf,siz,cnt,fil) if ( fread(buf,siz,cnt,fil) != cnt ) exit(-1)
struct TWISTER
{
unsigned int x;
unsigned int y;
unsigned int z;
};
static unsigned int crc=0;
static TWISTER state[3]=
{
{
123456789,
362436069,
521288629
},
{
123456789,
362436069,
521288629
},
{
123456789,
362436069,
521288629
}
};
void dbg_get_twister_state(unsigned int s[6])
{
s[0]=state[0].x; s[1]=state[0].y; s[2]=state[0].z;
s[3]=state[2].x; s[4]=state[2].y; s[5]=state[2].z;
}
static TWISTER* twister = state;
void twister_switch(int i)
{
twister = state+(i&1);
}
int twister_write(FILE* f)
{
fwrite(&twister->x,4,1,f);
fwrite(&twister->y,4,1,f);
fwrite(&twister->z,4,1,f);
return 12;
}
int twister_read(FILE* f)
{
FREAD(&twister->x,4,1,f);
FREAD(&twister->y,4,1,f);
FREAD(&twister->z,4,1,f);
return 12;
}
void twister_seed(unsigned int s)
{
twister->x = s;
twister->y = 362436069;
twister->z = 521288629;
twister_rand();
twister_rand();
twister_rand();
}
void crypt_ini()
{
twister[2].x = 0x09d4eceb ^ twister->z;
twister[2].y = 0x7b1f5a0a ^ twister->x;
twister[2].z = 0x5acb246e ^ twister->y;
crc = 0xef561513;
}
void crypt_enc(int* i)
{
unsigned int c = crc;
// update crc before xor
crc ^= (crc-*i) << 16;
crc ^= (crc+*i) >> 5;
crc ^= crc << 1;
unsigned int t;
twister[2].x ^= twister[2].x << 16;
twister[2].x ^= twister[2].x >> 5;
twister[2].x ^= twister[2].x << 1;
t = twister[2].x;
twister[2].x = twister[2].y;
twister[2].y = twister[2].z;
twister[2].z = t ^ twister[2].x ^ twister[2].y;
*i ^= twister[2].z;
*i ^= c;
}
void crypt_dec(int* i)
{
unsigned int t;
twister[2].x ^= twister[2].x << 16;
twister[2].x ^= twister[2].x >> 5;
twister[2].x ^= twister[2].x << 1;
t = twister[2].x;
twister[2].x = twister[2].y;
twister[2].y = twister[2].z;
twister[2].z = t ^ twister[2].x ^ twister[2].y;
*i ^= twister[2].z;
*i ^= crc;
// update crc after xor
crc ^= (crc-*i) << 16;
crc ^= (crc+*i) >> 5;
crc ^= crc << 1;
}
void crypt_enc(unsigned char* i)
{
unsigned int c = crc;
// update crc before xor
crc ^= (crc-*i) << 16;
crc ^= (crc+*i) >> 5;
crc ^= crc << 1;
unsigned int t;
twister[2].x ^= twister[2].x << 16;
twister[2].x ^= twister[2].x >> 5;
twister[2].x ^= twister[2].x << 1;
t = twister[2].x;
twister[2].x = twister[2].y;
twister[2].y = twister[2].z;
twister[2].z = t ^ twister[2].x ^ twister[2].y;
*i ^= twister[2].z;
*i ^= c;
}
void crypt_dec(unsigned char* i)
{
unsigned int t;
twister[2].x ^= twister[2].x << 16;
twister[2].x ^= twister[2].x >> 5;
twister[2].x ^= twister[2].x << 1;
t = twister[2].x;
twister[2].x = twister[2].y;
twister[2].y = twister[2].z;
twister[2].z = t ^ twister[2].x ^ twister[2].y;
*i ^= twister[2].z;
*i ^= crc;
// update crc after xor
crc ^= (crc-*i) << 16;
crc ^= (crc+*i) >> 5;
crc ^= crc << 1;
}
unsigned int crypt_chk()
{
return crc;
}
int twister_rand()
{
unsigned int t;
twister->x ^= twister->x << 16;
twister->x ^= twister->x >> 5;
twister->x ^= twister->x << 1;
t = twister->x;
twister->x = twister->y;
twister->y = twister->z;
twister->z = t ^ twister->x ^ twister->y;
return twister->z & 0x7fffffff;
}
#if 0
static const unsigned long SIZE = 624;
static const unsigned long PERIOD = 397;
static const unsigned long DIFF = SIZE-PERIOD;
static unsigned long MT[SIZE];
static unsigned long index = 0;
#define M32(x) (0x80000000 & x) // 32nd Most Significant Bit
#define L31(x) (0x7FFFFFFF & x) // 31 Least Significant Bits
#define ODD(x) (x & 1) // Check if number is odd
#define UNROLL(expr) y = M32(MT[i]) | L31(MT[i+1]); MT[i] = MT[expr] ^ (y>>1) ^ MATRIX[ODD(y)]; ++i;
void twister_seed(unsigned long s)
{
MT[0] = s;
index = 0;
for ( unsigned i=1; i<SIZE; ++i )
MT[i] = 0x6c078965*(MT[i-1] ^ MT[i-1]>>30) + i;
}
int twister_rand()
{
if ( !index )
{
static const unsigned long MATRIX[2] = {0, 0x9908b0df};
unsigned long y, i=0;
while ( i<(DIFF-1) )
{
UNROLL(i+PERIOD);
UNROLL(i+PERIOD);
}
UNROLL((i+PERIOD) % SIZE);
while ( i<(SIZE-1) )
{
UNROLL(i-DIFF);
UNROLL(i-DIFF);
UNROLL(i-DIFF);
UNROLL(i-DIFF);
UNROLL(i-DIFF);
UNROLL(i-DIFF);
UNROLL(i-DIFF);
UNROLL(i-DIFF);
UNROLL(i-DIFF);
UNROLL(i-DIFF);
UNROLL(i-DIFF);
}
y = M32(MT[SIZE-1]) | L31(MT[0]);
MT[SIZE-1] = MT[PERIOD-1] ^ (y>>1) ^ MATRIX[ODD(y)];
}
unsigned long y = MT[index];
y ^= y>>11;
y ^= y<< 7 & 0x9d2c5680;
y ^= y<<15 & 0xefc60000;
y ^= y>>18;
if ( ++index == SIZE )
index = 0;
return (int)(y&~(1<<31));
}
#endif