-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblowfish.cpp
More file actions
363 lines (289 loc) · 8.48 KB
/
blowfish.cpp
File metadata and controls
363 lines (289 loc) · 8.48 KB
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
#include "blowfish.h"
#include <cstdlib>
#include <ctime>
BlowfishCipher::BlowfishCipher()
{
// Randomize key
uint32 hkey[14];
srand(unsigned(time(NULL)));
for(uint8 i=0; i<14; ++i)
{
hkey[i] = rand(); // 16 bit random
hkey[i] <<= 16;
hkey[i] |= rand();
}
SetKey(hkey);
// Clear message buffer
for(uint i=0; i < MAX_MSG_LEN/4; ++i)
{
buffer.halfblocks[i].binary = 0x00000000;
}
}
BlowfishCipher::~BlowfishCipher()
{
}
uint32* BlowfishCipher::GetKey()
{
return key;
}
func_status BlowfishCipher::SetKey(uint32 k[14])
{
for(uint8 i=0; i<14; ++i)
key[i]=k[i];
InitializeSubkeys (key);
return FUNC_STATUS_OK;
}
func_status BlowfishCipher::SetBuffer(char* buf)
{
for(int i=0; i < MAX_MSG_LEN; ++i)
{
buffer.bytes[i] = buf[i];
}
return FUNC_STATUS_OK;
}
func_status BlowfishCipher::SetBuffer(std::string str)
{
for(int i=0; i < MAX_MSG_LEN; ++i)
{
buffer.bytes[i] = str[i];
}
if (str.length() > MAX_MSG_LEN)
return STR_OVER_MAX;
else
return FUNC_STATUS_OK;
}
char* BlowfishCipher::GetBuffer()
{
return buffer.bytes;
}
func_status BlowfishCipher::Encipher()
{
if (buffer.bytes[0] =='\0')
return NULL_MSG;
/* If necessary, pad the buffer up to MAX_MSG_LEN with random bytes. */
unsigned int i=0;
while(buffer.bytes[++i] != '\0');
while(i++ < MAX_MSG_LEN)
{
buffer.bytes[i] = rand();
}
/* Done padding */
/* ENCRYPT */
message hbuffer;
for(int i=0; i<MAX_MSG_LEN; ++i)
hbuffer.bytes[i] = buffer.bytes[i];
for(int i=0; i<MAX_MSG_LEN/8; i++)
{
EncryptBlock(&(hbuffer.halfblocks[2*i].binary), &(hbuffer.halfblocks[2*i+1].binary));
}
for(int i=0; i<MAX_MSG_LEN; ++i)
buffer.bytes[i] = hbuffer.bytes[i];
/* DONE ENCRYPTING */
return FUNC_STATUS_OK;
}
// currently in ECB mode. This isn't very secure (relatively speaking)
func_status BlowfishCipher::Decipher()
{
if (buffer.bytes[0] =='\0')
return NULL_MSG;
message hbuffer;
for(int i=0; i<MAX_MSG_LEN; ++i)
hbuffer.bytes[i] = buffer.bytes[i];
for(int i=0; i<MAX_MSG_LEN/8; ++i)
{
DecryptBlock(&(hbuffer.halfblocks[2*i].binary), &(hbuffer.halfblocks[2*i+1].binary));
}
for(int i=0; i<MAX_MSG_LEN; ++i)
buffer.bytes[i] = hbuffer.bytes[i];
return FUNC_STATUS_OK;
}
func_status BlowfishCipher::InitializeSubkeys(uint32* key)
{
/* This is a seven step process as described in Schneier's "Applied Cryptography."
The steps are given here in his words for your reference. */
/* (1) Initialize first the P-array and the four S-boxes, in order, with a fixed string.
This string consists of the hexadecimal digits of pi. */
// See bfinit.h for definitions of P,S1,S2,S3,S4.
unsigned int i=0;
for(i = 0; i<18; ++i)
{
sk.P[i] = P[i];
}
for(i = 0; i<18; ++i) // all of these loops need to be unrolled:
{
sk.S[0][i] = S1[i];
}
for(i = 0; i<18; ++i)
{
sk.S[1][i] = S2[i];
}
for(i = 0; i<18; ++i)
{
sk.S[2][i] = S3[i];
}
for(i = 0; i<18; ++i)
{
sk.S[3][i] = S4[i];
} // ^^^ all of those loops need to be unrolled ^^
/* (2) XOR P[0] with the first 32 bits of the key, XOR P[1] with the second 32 bits of the key,
and so on for all bits of the key (up to P[17]). Repeatedly cycle through the key bits
until the entire P-array has been XORed with key bits. */
sk.P[0] ^= key[0]; // bits 1-32 (Remember the key is always 448 bits in this implementation.)
sk.P[1] ^= key[1]; // bits -64
sk.P[2] ^= key[2]; // bits -96
sk.P[3] ^= key[3]; // bits -128
sk.P[4] ^= key[4]; // bits -160
sk.P[5] ^= key[5]; // bits -192
sk.P[6] ^= key[6]; // bits -224
sk.P[7] ^= key[7]; // bits -256
sk.P[8] ^= key[8]; // bits -288
sk.P[9] ^= key[9]; // bits -320
sk.P[10] ^= key[11]; // bits -352
sk.P[11] ^= key[12]; // bits -384
sk.P[12] ^= key[13]; // bits -416
sk.P[13] ^= key[0]; // bits 1 -32
sk.P[14] ^= key[1]; // bits 33-64 (When you run out of key bits, wrap.)
sk.P[15] ^= key[2]; // bits 65-96
sk.P[16] ^= key[3]; // bits 97-128
sk.P[17] ^= key[4]; // bits 129-160
/* (3) Encrypt the all-zero string with the Blowfish algorithm, using the subkeys
described in steps (1) and (2) above. */
uint32 dataleft = 0x00000000, dataright = 0x00000000;
EncryptBlock(&dataleft, &dataright);
/* (4) Replace P[0] and P[1] with the output of step (3). */
sk.P[0] = dataleft;
sk.P[1] = dataright;
/* (5) Encrypt the output of step(3) using the Blowfish algorith with the modified subkeys. */
EncryptBlock(&dataleft, &dataright);
/* (6) Replace P[2] and P[3] with the output of step (5). */
sk.P[2] = dataleft;
sk.P[3] = dataright;
/* (7) Continue the process, replacing all elements of the P-array, and then all four S-boxes
in order, with the output of the continuously changing Blowfish algorithm. */
EncryptBlock(&dataleft, &dataright);
sk.P[4] = dataleft;
sk.P[5] = dataright;
EncryptBlock(&dataleft, &dataright);
sk.P[6] = dataleft;
sk.P[7] = dataright;
EncryptBlock(&dataleft, &dataright);
sk.P[8] = dataleft;
sk.P[9] = dataright;
EncryptBlock(&dataleft, &dataright);
sk.P[10] = dataleft;
sk.P[11] = dataright;
EncryptBlock(&dataleft, &dataright);
sk.P[12] = dataleft;
sk.P[13] = dataright;
EncryptBlock(&dataleft, &dataright);
sk.P[14] = dataleft;
sk.P[15] = dataright;
EncryptBlock(&dataleft, &dataright);
sk.P[16] = dataleft;
sk.P[17] = dataright;
for (i=0; i<256; ++i) // I will unroll this loop soon.. I promise.
{
EncryptBlock(&dataleft, &dataright);
sk.S[0][i] = dataleft;
sk.S[0][i] = dataright;
}
for (i=0; i<256; ++i) // I will unroll this loop soon.. I promise.
{
EncryptBlock(&dataleft, &dataright);
sk.S[1][i] = dataleft;
sk.S[1][i] = dataright;
}
for (i=0; i<256; ++i) // I will unroll this loop soon.. I promise.
{
EncryptBlock(&dataleft, &dataright);
sk.S[2][i] = dataleft;
sk.S[2][i] = dataright;
}
for (i=0; i<256; ++i) // I will unroll this loop soon.. I promise.
{
EncryptBlock(&dataleft, &dataright);
sk.S[3][i] = dataleft;
sk.S[3][i] = dataright;
}
return FUNC_STATUS_OK;
}
func_status BlowfishCipher::EncryptBlock(uint32* left, uint32* right)
{
#ifndef XOR_SWAP_TRICK
uint32 temp = 0;
#endif
for (int i=0; i<N; ++i)
{
*left = *left ^ sk.P[i];
*right = F(*left) ^ *right;
#ifdef XOR_SWAP_TRICK // one way or another...
*left ^= *right;
*right ^= *left; // swap
*left ^= *right;
#endif // the
#ifndef XOR_SWAP_TRICK
temp = *right; // left
*right = *left; // and
*left = temp; // right
#endif
}
#ifdef XOR_SWAP_TRICK // undo the last swap
*left ^= *right;
*right ^= *left;
*left ^= *right;
#endif
#ifndef XOR_SWAP_TRICK
temp = *right;
*right = *left;
*left = temp;
#endif
*right = *right ^ sk.P[N];
*left = *left ^ sk.P[N+1];
return FUNC_STATUS_OK;
}
func_status BlowfishCipher::DecryptBlock(uint32* left, uint32* right)
{
#ifndef XOR_SWAP_TRICK
uint32 temp = 0;
#endif
for (int i=N+1; i>1; --i)
{
*left = *left ^ sk.P[i];
*right = F(*left) ^ *right;
#ifdef XOR_SWAP_TRICK // one way or another...
*left ^= *right;
*right ^= *left; // swap
*left ^= *right;
#endif // the
#ifndef XOR_SWAP_TRICK
temp = *right; // left
*right = *left; // and
*left = temp; // right
#endif
}
#ifdef XOR_SWAP_TRICK // undo the last swap
*left ^= *right;
*right ^= *left;
*left ^= *right;
#endif
#ifndef XOR_SWAP_TRICK
temp = *right;
*right = *left;
*left = temp;
#endif
*right = *right ^ sk.P[1];
*left = *left ^ sk.P[0];
return FUNC_STATUS_OK;
}
uint32 BlowfishCipher::F (uint32 data)
{
/* Divide data into four 8bit quarters named a,b,c,d */
uint8 a = data & 0xFF;
data = data >> 8;
uint8 b = data & 0xFF;
data = data >> 8;
uint8 c = data & 0xFF;
data = data >> 8;
uint8 d = data & 0xFF;
return (((sk.S[0][a] + sk.S[1][b]) ^ sk.S[2][c]) + sk.S[3][d]);
}