-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstesc.cpp
274 lines (211 loc) · 6.11 KB
/
stesc.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
/* stesc.cpp
Copyright (c) 2000 Philip Hunt
Program to create a stes ciphertext file.
Usage:
stesc ctf k1 pt1 k2 pt2 ... kn ptn
where:
ctf = ciphertext filename (to be created)
k1, pt1 = first key, filename of first plaintext
(then next key/plaintext-filename pairs)
Last altered: 16-Jul-2000
History:
12-Jul-2000 PH: created
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <vector.h>
#include "crypt.h"
#include "checkitem.h"
#include "dataitem.h"
#define DEBUG 0
/*****
If set, this causes all CA information to be output, decrypted,
to the file ``CA.plain''. This obviously renders encryption useless
& is intended for debugging only.
*****/
#define DEBUG_OUTPUT_CA 1
//--------------------------------------------------------------------
/* global variables */
int numKeys = 0;
char* cipherTextFilename;
vector<char*> keys;
vector<char*> plainTextFilenames;
/* which CIs are currently used/unused? */
SetSI usedCIs(CA_ITEMS);
/* information about CIs */
CheckItem ci[CA_ITEMS];
/* information about DIs */
DataItem di[DA_ITEMS];
/* which DIs are currently used/unused? */
SetSI usedDIs(DA_ITEMS);
/* which CI is key (k) using? */
int kUsesCI[CA_ITEMS];
//--------------------------------------------------------------------
void initializeVars(){
//int i;
usedCIs.unsetAll();
usedDIs.unsetAll();
}
//--------------------------------------------------------------------
void usage(){
//...to do...
}
void decodeArgs(int argc, char** argv){
#if DEBUG
for (int ix = 0; ix < argc; ix++){
printf("argv[%d]={%s}\n", ix, argv[ix]);
}
#endif
cipherTextFilename = argv[1];
int nextArg = 2;
while (nextArg < argc){
keys.push_back(argv[nextArg++]);
plainTextFilenames.push_back(argv[nextArg++]);
#if DEBUG
printf("nextArg=%d keys[%d]={%s} pTFn[%d]={%s}\n",
nextArg, numKeys, keys[numKeys],
numKeys, plainTextFilenames[numKeys]);
#endif
numKeys++;
}//while
#if DEBUG
printf("numKeys=%d\n", numKeys);
#endif
}
//--------------------------------------------------------------------
/* allocate a free CI. Return its index number. */
int allocCI(){
int ciLoc;
do {
ciLoc = randInt_o(CA_ITEMS);
#if 0
printf("allocCI(), ciLoc=%d (%d %d/%d)\n", ciLoc, CA_ITEMS,
rand(), RAND_MAX);
#endif
} while (usedCIs.isSet(ciLoc));
usedCIs.set(ciLoc); // it's being used now
return ciLoc;
}
/* allocate a free DI. Return its index number. */
int allocDI(){
int diLoc;
do {
diLoc = randInt_o(DA_ITEMS);
} while (usedDIs.isSet(diLoc));
usedDIs.set(diLoc); // it's being used now
return diLoc;
}
//--------------------------------------------------------------------
/* allocate all the DIs necessay for key (k) */
void allocDiForKey(int k){
int diLoc;
/* find out how big the file is */
struct stat fileStatistics;
stat(plainTextFilenames[k], &fileStatistics);
int fileSize = fileStatistics.st_size;
/* tell the CI this size */
int ciLoc = kUsesCI[k];
CheckItem& thisCI = ci[ciLoc];
thisCI.dataSize = fileSize;
/* work out how many DIs in the DA we need to allocate to
hold this */
int diNeeded = fileSize / DI_SIZE;
int partialBytes = fileSize % DI_SIZE;
if (partialBytes > 0) diNeeded += 1;
#if DEBUG
printf("allocDiForKey(%d), filename [%s] fileSize=%d diNeeded=%d\n",
k, plainTextFilenames[k], fileSize, diNeeded);
#endif
/* allocate DIs; tell CI which DIs have been allocated */
for (int i = 0; i < diNeeded; i++){
diLoc = allocDI();
thisCI.diset.set(diLoc);
di[diLoc].key = keys[k];
di[diLoc].loc = diLoc;
}
/* allocate data to the DIs for this key */
if (fileSize <= 0) return;
FILE* ptFile = fopen(plainTextFilenames[k], "r");
int dataToAllocate = fileSize;
int nextDI = 0; //the next unused DI
while (dataToAllocate > 0){
int allocNow = DI_SIZE;
if (dataToAllocate < allocNow) allocNow = dataToAllocate;
diLoc = thisCI.diset.getNext(nextDI);
nextDI = diLoc + 1;
di[diLoc].setDataSize(allocNow);
di[diLoc].loadFromPt(ptFile);
dataToAllocate -= DI_SIZE;
}//while
}
//--------------------------------------------------------------------
/* write (n) random bytes to file (f). */
void fwriteRandom(int n, FILE* f){
uchar b;
for (int i = 0; i < n; i++){
b = randInt(0, 255);
fwrite(&b, 1, 1, f);
}
}
//--------------------------------------------------------------------
int main(int argc, char** argv){
int i;
initializeVars();
decodeArgs(argc, argv);
for (int keyNum = 0; keyNum < numKeys; keyNum++){
#if DEBUG
printf("main. (1) keyNum=%d\n", keyNum);
#endif
/* get an unused CI location for the key */
int ciLoc = allocCI();
kUsesCI[keyNum] = ciLoc;
/* tell the CI its key */
ci[ciLoc].key = keys[keyNum];
#if DEBUG
printf("main. (1.1) keyNum=%d\n", keyNum);
#endif
/* allocate DIs */
allocDiForKey(keyNum);
#if DEBUG
printf("for keyNum=%d, ci is %d\n", keyNum, ciLoc);
#endif
#if DEBUG
printf("main. (2)\n");
#endif
}//for keyNum
/* create ciphertext file */
FILE* ctFile = fopen(cipherTextFilename, "w");
#if DEBUG_OUTPUT_CA
OFile caPlain("CA.plain");
caPlain << "*** writing debug info from stesc ***\n\n";
#endif
/* write CIs to ciphertext file */
for (i = 0; i < CA_ITEMS; i++){
#if DEBUG
printf("main. (4) i=%d\n", i);
#endif
if (usedCIs.isSet(i)){
fwrite(ci[i].getCtBytes(), 1, CheckItem::byteSize(), ctFile);
} else {
/* unused CI, so do random bytes */
fwriteRandom(CheckItem::byteSize(), ctFile);
}
#if DEBUG_OUTPUT_CA
caPlain << "\nCI #" << i << ":\n";
ci[i].outDebugInfo(caPlain);
#endif
}//for
/* write DIs to ciphertext file */
for (i = 0; i < DA_ITEMS; i++){
di[i].writeCtBytes(ctFile);
}
/* close ct file */
fclose(ctFile);
#if DEBUG_OUTPUT_CA
caPlain.flush();
#endif
return 0;
}
//--------------------------------------------------------------------
/* end stesc.cpp */