-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtea.cpp
329 lines (287 loc) · 6.24 KB
/
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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void strcopy(char * dest, char const * source, size_t size = 0)
{
if (0 == size) size = strlen(source);
while (size > 0)
{
*dest = *source;
++source;
++dest;
--size;
}
*dest = 0;
}
typedef enum {ENCRYPT, DECRYPT} Mode;
static int32_t calc_v(int32_t const * v, int32_t const * k, int n, int32_t sum)
{
return ((v[1-n]<<4) + k[2*n]) ^ (v[1-n] + sum) ^ ((v[1-n]>>5) + k[2*n+1]);
}
void crypt(int32_t* v, int32_t const * k, Mode mode, unsigned int number_of_iteration)
{
const int32_t delta = 0x9e3779b9;
int orderVar;
int32_t sum, multiplier;
multiplier = (mode == ENCRYPT) ? 1 : -1;
orderVar = (mode == ENCRYPT) ? 0 : 1;
sum = (mode == ENCRYPT) ? delta : delta * number_of_iteration;
for (int i=0; i < number_of_iteration; i++)
{
v[orderVar] += multiplier * calc_v(v, k, orderVar, sum);
v[1 - orderVar] += multiplier * calc_v(v, k, 1 - orderVar, sum);
sum += multiplier * delta;
}
}
class IDataSource
{
public:
virtual int8_t getc() = 0;
virtual bool hasNext() = 0;
};
class IDataTarget
{
public:
//return no error indicator
virtual bool putc(int8_t) = 0;
};
class FileSource : public IDataSource
{
private:
FILE * file;
char nextChar; //we need this to make possible correct work of hasNext and not waste of speed simultaneously
public:
FileSource(char const* filename)
{
file = fopen(filename, "rb");
nextChar = fgetc(file);
}
~FileSource()
{
fclose(file);
}
int8_t getc()
{
int8_t result = (int8_t)nextChar;
nextChar = fgetc(file);
return result;
}
bool hasNext()
{
return !feof(file);
}
};
class FileTarget : public IDataTarget
{
private:
FILE * file;
public:
FileTarget(char const* filename)
{
file = fopen(filename, "wb");
}
bool putc(int8_t c)
{
fputc(c, file);
return !ferror(file);
}
~FileTarget()
{
fclose(file);
}
void rewindFile()
{
rewind(file);
}
};
class RandomSource : public IDataSource
{
private:
int leftData;
public:
RandomSource(int dataSize)
{
srand(time(NULL));
leftData = dataSize;
}
int8_t getc()
{
--leftData;
return (int8_t)rand()%256;
}
bool hasNext()
{
return (leftData > 0);
}
};
class NullTarget : public IDataTarget
{
public:
bool putc(int8_t c)
{
return true;
}
};
class StringSource : public IDataSource
{
private:
char * string;
size_t size;
public:
char * getValue()
{
return string;
}
size_t getSize()
{
return size;
}
void setValue(char const* s, size_t _size=0)
{
size = _size ? _size : strlen(s);
string = new char[size + 1];
strcopy(string, s, size);
}
StringSource(char const * s, size_t _size=0)
{
setValue(s, _size);
}
~StringSource()
{
delete[] string;
}
bool hasNext()
{
return (size > 0);
}
int8_t getc()
{
char r = *string;
if (size > 0) {
++string;
--size;
}
return r;
}
};
class StringTarget: public IDataTarget
{
private:
char * string;
size_t size, iter;
public:
void reset()
{
string[0] = 0;
iter=0;
}
StringTarget(size_t stringSize)
{
size = stringSize;
string = new char[stringSize+1];
reset();
}
~StringTarget()
{
delete[] string;
}
bool putc(int8_t c)
{
if (iter<size)
{
string[iter] = c;
string[++iter] = 0;
return true;
}
else
{
return false;
}
}
char * getValue()
{
char * r = new char[size+1];
strcopy(r, string, iter);
return r;
}
size_t getSize()
{
return iter;
}
};
// returns number of bytes, that was readed
size_t readBytes(IDataSource * source, int8_t * target, size_t n)
{
int i;
for(i=0; i<n && source->hasNext(); i++)
{
target[i] = source->getc();
}
for(int j=i; j<n; j++)
{
target[j] = 0;
}
return i;
}
//returns no error indicator
bool putBytes(IDataTarget *target, int8_t const* source, size_t n)
{
bool result = true;
for(int i=0; i<n && result; i++)
{
result = target->putc(source[i]);
}
return result;
}
//return number of bytes, that was readed during the last iteration
size_t engine(IDataSource * source, IDataTarget * target, IDataSource * keySource, Mode mode,
size_t numberOfBytesInTheLastBlock = 8, size_t numberOfIterations = 32)
{
int8_t v[8], key[16];
bool doNotExitCycle = source->hasNext();
readBytes(keySource, key, 16);
size_t result;
while(doNotExitCycle)
{
result = readBytes(source, v, 8);
crypt((int32_t*)v, (int32_t*)key, mode, numberOfIterations);
doNotExitCycle = source->hasNext();
putBytes(target, v, doNotExitCycle? 8 : numberOfBytesInTheLastBlock);
}
return result;
}
void generateKeyToFile(char const* keyFile)
{
const size_t keySize = 16;
IDataSource* source = new RandomSource(keySize);
IDataTarget* target = new FileTarget(keyFile);
int8_t key[keySize];
readBytes(source, key, keySize);
putBytes(target, key, keySize);
delete source;
delete target;
}
void cryptFile(char const * source, char const * target, char const * keyFile, Mode mode)
{
const size_t keySize = 16;
int8_t key[keySize];
IDataSource* keySource = new FileSource(keyFile);
readBytes(keySource, key, keySize);
delete keySource;
int8_t bytesInLastBlock = 8;
FileSource* _source = new FileSource(source);
FileTarget* _target = new FileTarget(target);
if (mode == ENCRYPT)
putBytes(_target, &bytesInLastBlock, 1);//to allocate space at the beginning of file
else
readBytes(_source, &bytesInLastBlock, 1);
bytesInLastBlock = engine(_source, _target, new StringSource((char*)key, keySize), mode, bytesInLastBlock);
if (mode == ENCRYPT){
_target->rewindFile();
putBytes(_target, &bytesInLastBlock, 1);
}
delete _source;
delete _target;
}