1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: C++ class implementation of the BLOWFISH encryption algorithm
7
////////////////////////////////////////////////////////////////////////////
9
// _THE BLOWFISH ENCRYPTION ALGORITHM_
11
// Revised code--3/20/94
12
// Converted to C++ class 5/96, Jim Conger
15
#include "blowfish2.h" // holds the random digit tables
17
#define S(x,i) (SBoxes[i][x.w.byte##i])
18
#define bf_F(x) (((S(x,0) + S(x,1)) ^ S(x,2)) + S(x,3))
19
#define ROUND(a,b,n) (a.dword ^= bf_F(b) ^ PArray[n])
21
// choose a byte order for your hardware
22
#define ORDER_DCBA // chosing Intel in this case
24
#ifdef ORDER_DCBA // DCBA - little endian - intel
37
#elif ORDER_ABCD // ABCD - big endian - motorola
50
#elif ORDER_BADC // BADC - vax
65
CBlowFish::CBlowFish ()
67
PArray = new DWORD [18] ;
68
SBoxes = new DWORD [4][256] ;
71
CBlowFish::~CBlowFish ()
77
// the low level (private) encryption function
78
void CBlowFish::Blowfish_encipher (DWORD *xl, DWORD *xr)
85
Xl.dword ^= PArray [0];
86
ROUND (Xr, Xl, 1) ; ROUND (Xl, Xr, 2) ;
87
ROUND (Xr, Xl, 3) ; ROUND (Xl, Xr, 4) ;
88
ROUND (Xr, Xl, 5) ; ROUND (Xl, Xr, 6) ;
89
ROUND (Xr, Xl, 7) ; ROUND (Xl, Xr, 8) ;
90
ROUND (Xr, Xl, 9) ; ROUND (Xl, Xr, 10) ;
91
ROUND (Xr, Xl, 11) ; ROUND (Xl, Xr, 12) ;
92
ROUND (Xr, Xl, 13) ; ROUND (Xl, Xr, 14) ;
93
ROUND (Xr, Xl, 15) ; ROUND (Xl, Xr, 16) ;
94
Xr.dword ^= PArray [17] ;
100
// the low level (private) decryption function
101
void CBlowFish::Blowfish_decipher (DWORD *xl, DWORD *xr)
109
Xl.dword ^= PArray [17] ;
110
ROUND (Xr, Xl, 16) ; ROUND (Xl, Xr, 15) ;
111
ROUND (Xr, Xl, 14) ; ROUND (Xl, Xr, 13) ;
112
ROUND (Xr, Xl, 12) ; ROUND (Xl, Xr, 11) ;
113
ROUND (Xr, Xl, 10) ; ROUND (Xl, Xr, 9) ;
114
ROUND (Xr, Xl, 8) ; ROUND (Xl, Xr, 7) ;
115
ROUND (Xr, Xl, 6) ; ROUND (Xl, Xr, 5) ;
116
ROUND (Xr, Xl, 4) ; ROUND (Xl, Xr, 3) ;
117
ROUND (Xr, Xl, 2) ; ROUND (Xl, Xr, 1) ;
118
Xr.dword ^= PArray[0];
125
// constructs the enctryption sieve
126
void CBlowFish::Initialize (BYTE key[], int keybytes)
129
DWORD data, datal, datar ;
132
// first fill arrays from data tables
133
for (i = 0 ; i < 18 ; i++)
134
PArray [i] = bf_P [i] ;
136
for (i = 0 ; i < 4 ; i++)
138
for (j = 0 ; j < 256 ; j++)
139
SBoxes [i][j] = bf_S [i][j] ;
144
for (i = 0 ; i < NPASS + 2 ; ++i)
147
temp.w.byte0 = key[j];
148
temp.w.byte1 = key[(j+1) % keybytes] ;
149
temp.w.byte2 = key[(j+2) % keybytes] ;
150
temp.w.byte3 = key[(j+3) % keybytes] ;
153
j = (j + 4) % keybytes ;
159
for (i = 0 ; i < NPASS + 2 ; i += 2)
161
Blowfish_encipher (&datal, &datar) ;
163
PArray [i + 1] = datar ;
166
for (i = 0 ; i < 4 ; ++i)
168
for (j = 0 ; j < 256 ; j += 2)
170
Blowfish_encipher (&datal, &datar) ;
171
SBoxes [i][j] = datal ;
172
SBoxes [i][j + 1] = datar ;
177
// get output length, which must be even MOD 8
178
DWORD CBlowFish::GetOutputLength (DWORD lInputLong)
182
lVal = lInputLong % 8 ; // find out if uneven number of bytes at the end
184
return lInputLong + 8 - lVal ;
189
// Encode pIntput into pOutput. Input length in lSize. Returned value
190
// is length of output which will be even MOD 8 bytes. Input buffer and
191
// output buffer can be the same, but be sure buffer length is even MOD8.
192
DWORD CBlowFish::Encode (BYTE * pInput, BYTE * pOutput, DWORD lSize)
194
DWORD lCount, lOutSize, lGoodBytes ;
197
int SameDest = (pInput == pOutput ? 1 : 0) ;
199
lOutSize = GetOutputLength (lSize) ;
200
for (lCount = 0 ; lCount < lOutSize ; lCount += 8)
202
if (SameDest) // if encoded data is being written into input buffer
204
if (lCount < lSize - 7) // if not dealing with uneven bytes at end
206
Blowfish_encipher ((DWORD *) pInput,
207
(DWORD *) (pInput + 4)) ;
209
else // pad end of data with null bytes to complete encryption
211
po = pInput + lSize ; // point at byte past the end of actual data
212
j = (int) (lOutSize - lSize) ; // number of bytes to set to null
213
for (i = 0 ; i < j ; i++)
215
Blowfish_encipher ((DWORD *) pInput,
216
(DWORD *) (pInput + 4)) ;
220
else // output buffer not equal to input buffer, so must copy
221
{ // input to output buffer prior to encrypting
222
if (lCount < lSize - 7) // if not dealing with uneven bytes at end
226
for (i = 0 ; i < 8 ; i++)
227
// copy bytes to output
229
Blowfish_encipher ((DWORD *) pOutput, // now encrypt them
230
(DWORD *) (pOutput + 4)) ;
232
else // pad end of data with null bytes to complete encryption
234
lGoodBytes = lSize - lCount ; // number of remaining data bytes
236
for (i = 0 ; i < (int) lGoodBytes ; i++)
238
for (j = i ; j < 8 ; j++)
240
Blowfish_encipher ((DWORD *) pOutput,
241
(DWORD *) (pOutput + 4)) ;
250
// Decode pIntput into pOutput. Input length in lSize. Input buffer and
251
// output buffer can be the same, but be sure buffer length is even MOD8.
252
void CBlowFish::Decode (BYTE * pInput, BYTE * pOutput, DWORD lSize)
257
int SameDest = (pInput == pOutput ? 1 : 0) ;
259
for (lCount = 0 ; lCount < lSize ; lCount += 8)
261
if (SameDest) // if encoded data is being written into input buffer
263
Blowfish_decipher ((DWORD *) pInput,
264
(DWORD *) (pInput + 4)) ;
267
else // output buffer not equal to input buffer
268
{ // so copy input to output before decoding
271
for (i = 0 ; i < 8 ; i++)
273
Blowfish_decipher ((DWORD *) pOutput,
274
(DWORD *) (pOutput + 4)) ;
1
////////////////////////////////////////////////////////////////////////////
2
// NoteCase notes manager project <http://notecase.sf.net>
4
// This code is licensed under BSD license.See "license.txt" for more details.
6
// File: C++ class implementation of the BLOWFISH encryption algorithm
7
////////////////////////////////////////////////////////////////////////////
9
// _THE BLOWFISH ENCRYPTION ALGORITHM_
11
// Revised code--3/20/94
12
// Converted to C++ class 5/96, Jim Conger
13
// - fixed deleting array in destructor (Miroslav Rajcic)
14
// - fixed DWORD definition (was invalid for 64bit build) (Miroslav Rajcic)
17
#include "blowfish2.h" // holds the random digit tables
20
#define S(x,i) (SBoxes[i][x.w.byte##i])
21
#define bf_F(x) (((S(x,0) + S(x,1)) ^ S(x,2)) + S(x,3))
22
#define ROUND(a,b,n) (a.dword ^= bf_F(b) ^ PArray[n])
24
// choose a byte order for your hardware
25
#define ORDER_DCBA // choosing Intel in this case
27
#ifdef ORDER_DCBA // DCBA - little endian - intel
40
#elif ORDER_ABCD // ABCD - big endian - motorola
53
#elif ORDER_BADC // BADC - vax
68
CBlowFish::CBlowFish ()
70
ASSERT(sizeof(WORD) == 2);
71
ASSERT(sizeof(DWORD) == 4);
72
ASSERT(sizeof(union aword) == sizeof(DWORD));
74
PArray = new DWORD [18] ;
75
SBoxes = new DWORD [4][256] ;
78
CBlowFish::~CBlowFish ()
84
// the low level (private) encryption function
85
void CBlowFish::Blowfish_encipher (DWORD *xl, DWORD *xr)
92
Xl.dword ^= PArray [0];
93
ROUND (Xr, Xl, 1) ; ROUND (Xl, Xr, 2) ;
94
ROUND (Xr, Xl, 3) ; ROUND (Xl, Xr, 4) ;
95
ROUND (Xr, Xl, 5) ; ROUND (Xl, Xr, 6) ;
96
ROUND (Xr, Xl, 7) ; ROUND (Xl, Xr, 8) ;
97
ROUND (Xr, Xl, 9) ; ROUND (Xl, Xr, 10) ;
98
ROUND (Xr, Xl, 11) ; ROUND (Xl, Xr, 12) ;
99
ROUND (Xr, Xl, 13) ; ROUND (Xl, Xr, 14) ;
100
ROUND (Xr, Xl, 15) ; ROUND (Xl, Xr, 16) ;
101
Xr.dword ^= PArray [17] ;
107
// the low level (private) decryption function
108
void CBlowFish::Blowfish_decipher (DWORD *xl, DWORD *xr)
116
Xl.dword ^= PArray [17] ;
117
ROUND (Xr, Xl, 16) ; ROUND (Xl, Xr, 15) ;
118
ROUND (Xr, Xl, 14) ; ROUND (Xl, Xr, 13) ;
119
ROUND (Xr, Xl, 12) ; ROUND (Xl, Xr, 11) ;
120
ROUND (Xr, Xl, 10) ; ROUND (Xl, Xr, 9) ;
121
ROUND (Xr, Xl, 8) ; ROUND (Xl, Xr, 7) ;
122
ROUND (Xr, Xl, 6) ; ROUND (Xl, Xr, 5) ;
123
ROUND (Xr, Xl, 4) ; ROUND (Xl, Xr, 3) ;
124
ROUND (Xr, Xl, 2) ; ROUND (Xl, Xr, 1) ;
125
Xr.dword ^= PArray[0];
131
// constructs the enctryption sieve
132
void CBlowFish::Initialize (BYTE key[], int keybytes)
135
DWORD data, datal, datar ;
138
// first fill arrays from data tables
139
for (i = 0 ; i < 18 ; i++)
140
PArray [i] = bf_P [i] ;
142
for (i = 0 ; i < 4 ; i++)
144
for (j = 0 ; j < 256 ; j++)
145
SBoxes [i][j] = bf_S [i][j] ;
150
for (i = 0 ; i < NPASS + 2 ; ++i)
153
temp.w.byte0 = key[j];
154
temp.w.byte1 = key[(j+1) % keybytes] ;
155
temp.w.byte2 = key[(j+2) % keybytes] ;
156
temp.w.byte3 = key[(j+3) % keybytes] ;
159
j = (j + 4) % keybytes ;
165
for (i = 0 ; i < NPASS + 2 ; i += 2)
167
Blowfish_encipher (&datal, &datar) ;
169
PArray [i + 1] = datar ;
172
for (i = 0 ; i < 4 ; ++i)
174
for (j = 0 ; j < 256 ; j += 2)
176
Blowfish_encipher (&datal, &datar) ;
177
SBoxes [i][j] = datal ;
178
SBoxes [i][j + 1] = datar ;
183
// get output length, which must be even MOD 8
184
DWORD CBlowFish::GetOutputLength (DWORD lInputLong)
188
lVal = lInputLong % 8 ; // find out if uneven number of bytes at the end
190
return lInputLong + 8 - lVal ;
195
// Encode pIntput into pOutput. Input length in lSize. Returned value
196
// is length of output which will be even MOD 8 bytes. Input buffer and
197
// output buffer can be the same, but be sure buffer length is even MOD8.
198
DWORD CBlowFish::Encode (BYTE * pInput, BYTE * pOutput, DWORD lSize)
200
DWORD lCount, lOutSize, lGoodBytes;
203
int SameDest = (pInput == pOutput ? 1 : 0);
205
lOutSize = GetOutputLength (lSize);
206
for (lCount = 0; lCount < lOutSize; lCount += 8)
208
if (SameDest) // if encoded data is being written into input buffer
210
if (lCount < lSize - 7) // if not dealing with uneven bytes at end
212
Blowfish_encipher ((DWORD *) pInput, (DWORD *) (pInput + 4)) ;
214
else // pad end of data with null bytes to complete encryption
216
po = pInput + lSize; // point at byte past the end of actual data
217
j = (int) (lOutSize - lSize); // number of bytes to set to null
218
for (i = 0; i < j; i++)
220
Blowfish_encipher ((DWORD *) pInput, (DWORD *) (pInput + 4)) ;
224
else // output buffer not equal to input buffer, so must copy
225
{ // input to output buffer prior to encrypting
226
if (lCount < lSize - 7) // if not dealing with uneven bytes at end
230
for (i = 0 ; i < 8 ; i++) // copy bytes to output
232
Blowfish_encipher ((DWORD *) pOutput, (DWORD *) (pOutput + 4)); // now encrypt them
234
else // pad end of data with null bytes to complete encryption
236
lGoodBytes = lSize - lCount; // number of remaining data bytes
238
for (i = 0; i < (int) lGoodBytes ; i++)
240
for (j = i; j < 8; j++)
242
Blowfish_encipher ((DWORD *) pOutput, (DWORD *) (pOutput + 4));
251
// Decode pIntput into pOutput. Input length in lSize. Input buffer and
252
// output buffer can be the same, but be sure buffer length is even MOD8.
253
void CBlowFish::Decode (BYTE * pInput, BYTE * pOutput, DWORD lSize)
258
int SameDest = (pInput == pOutput ? 1 : 0) ;
260
for (lCount = 0 ; lCount < lSize ; lCount += 8)
262
if (SameDest) // if encoded data is being written into input buffer
264
Blowfish_decipher ((DWORD *) pInput, (DWORD *) (pInput + 4));
267
else // output buffer not equal to input buffer
268
{ // so copy input to output before decoding
271
for (i = 0 ; i < 8 ; i++)
273
Blowfish_decipher ((DWORD *) pOutput, (DWORD *) (pOutput + 4));