~ubuntu-branches/ubuntu/karmic/notecase/karmic

« back to all changes in this revision

Viewing changes to src/lib/blowfish.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Vijay(Vijay)
  • Date: 2007-06-14 00:13:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070614001348-z9e2vbgtenb9nhoo
Tags: 1.5.6-0ubuntu1
* New Upstream release 
*  The libgnomevfs2-dev is also added to Build-Depends 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
////////////////////////////////////////////////////////////////////////////
2
 
// NoteCase notes manager project <http://notecase.sf.net>
3
 
//
4
 
// This code is licensed under BSD license.See "license.txt" for more details.
5
 
//
6
 
// File: C++ class implementation of the BLOWFISH encryption algorithm
7
 
////////////////////////////////////////////////////////////////////////////
8
 
 
9
 
// _THE BLOWFISH ENCRYPTION ALGORITHM_
10
 
// by Bruce Schneier
11
 
// Revised code--3/20/94
12
 
// Converted to C++ class 5/96, Jim Conger
13
 
 
14
 
#include "blowfish.h"
15
 
#include "blowfish2.h"  // holds the random digit tables
16
 
 
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])
20
 
 
21
 
// choose a byte order for your hardware
22
 
#define ORDER_DCBA      // chosing Intel in this case
23
 
 
24
 
#ifdef ORDER_DCBA       // DCBA - little endian - intel
25
 
 
26
 
        union aword {
27
 
          DWORD dword;
28
 
          BYTE byte [4];
29
 
          struct {
30
 
            unsigned int byte3:8;
31
 
            unsigned int byte2:8;
32
 
            unsigned int byte1:8;
33
 
            unsigned int byte0:8;
34
 
          } w;
35
 
        };
36
 
 
37
 
#elif ORDER_ABCD        // ABCD - big endian - motorola
38
 
 
39
 
        union aword {
40
 
          DWORD dword;
41
 
          BYTE byte [4];
42
 
          struct {
43
 
            unsigned int byte0:8;
44
 
            unsigned int byte1:8;
45
 
            unsigned int byte2:8;
46
 
            unsigned int byte3:8;
47
 
          } w;
48
 
        };
49
 
 
50
 
#elif ORDER_BADC        // BADC - vax
51
 
 
52
 
        union aword {
53
 
          DWORD dword;
54
 
          BYTE byte [4];
55
 
          struct {
56
 
            unsigned int byte1:8;
57
 
            unsigned int byte0:8;
58
 
            unsigned int byte3:8;
59
 
            unsigned int byte2:8;
60
 
          } w;
61
 
        };
62
 
 
63
 
#endif
64
 
 
65
 
CBlowFish::CBlowFish ()
66
 
{
67
 
        PArray = new DWORD [18] ;
68
 
        SBoxes = new DWORD [4][256] ;
69
 
}
70
 
 
71
 
CBlowFish::~CBlowFish ()
72
 
{
73
 
        delete PArray ;
74
 
        delete [] SBoxes ;
75
 
}
76
 
 
77
 
        // the low level (private) encryption function
78
 
void CBlowFish::Blowfish_encipher (DWORD *xl, DWORD *xr)
79
 
{
80
 
        union aword  Xl, Xr ;
81
 
 
82
 
        Xl.dword = *xl ;
83
 
        Xr.dword = *xr ;
84
 
 
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] ;
95
 
 
96
 
        *xr = Xl.dword ;
97
 
        *xl = Xr.dword ;
98
 
}
99
 
 
100
 
        // the low level (private) decryption function
101
 
void CBlowFish::Blowfish_decipher (DWORD *xl, DWORD *xr)
102
 
{
103
 
   union aword  Xl ;
104
 
   union aword  Xr ;
105
 
 
106
 
   Xl.dword = *xl ;
107
 
   Xr.dword = *xr ;
108
 
 
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];
119
 
 
120
 
   *xl = Xr.dword;
121
 
   *xr = Xl.dword;
122
 
}
123
 
 
124
 
 
125
 
        // constructs the enctryption sieve
126
 
void CBlowFish::Initialize (BYTE key[], int keybytes)
127
 
{
128
 
        int             i, j ;
129
 
        DWORD           data, datal, datar ;
130
 
        union aword temp ;
131
 
 
132
 
        // first fill arrays from data tables
133
 
        for (i = 0 ; i < 18 ; i++)
134
 
                PArray [i] = bf_P [i] ;
135
 
 
136
 
        for (i = 0 ; i < 4 ; i++)
137
 
        {
138
 
                for (j = 0 ; j < 256 ; j++)
139
 
                        SBoxes [i][j] = bf_S [i][j] ;
140
 
        }
141
 
 
142
 
 
143
 
        j = 0 ;
144
 
        for (i = 0 ; i < NPASS + 2 ; ++i)
145
 
        {
146
 
                temp.dword = 0 ;
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] ;
151
 
                data = temp.dword ;
152
 
                PArray [i] ^= data ;
153
 
                j = (j + 4) % keybytes ;
154
 
        }
155
 
 
156
 
        datal = 0 ;
157
 
        datar = 0 ;
158
 
 
159
 
        for (i = 0 ; i < NPASS + 2 ; i += 2)
160
 
        {
161
 
                Blowfish_encipher (&datal, &datar) ;
162
 
                PArray [i] = datal ;
163
 
                PArray [i + 1] = datar ;
164
 
        }
165
 
 
166
 
        for (i = 0 ; i < 4 ; ++i)
167
 
        {
168
 
                for (j = 0 ; j < 256 ; j += 2)
169
 
                {
170
 
                  Blowfish_encipher (&datal, &datar) ;
171
 
                  SBoxes [i][j] = datal ;
172
 
                  SBoxes [i][j + 1] = datar ;
173
 
                }
174
 
        }
175
 
}
176
 
 
177
 
        // get output length, which must be even MOD 8
178
 
DWORD CBlowFish::GetOutputLength (DWORD lInputLong)
179
 
{
180
 
        DWORD   lVal ;
181
 
 
182
 
        lVal = lInputLong % 8 ; // find out if uneven number of bytes at the end
183
 
        if (lVal != 0)
184
 
                return lInputLong + 8 - lVal ;
185
 
        else
186
 
                return lInputLong;
187
 
}
188
 
 
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)
193
 
{
194
 
        DWORD   lCount, lOutSize, lGoodBytes ;
195
 
        BYTE    *pi, *po ;
196
 
        int             i, j ;
197
 
        int             SameDest = (pInput == pOutput ? 1 : 0) ;
198
 
 
199
 
        lOutSize = GetOutputLength (lSize) ;
200
 
        for (lCount = 0 ; lCount < lOutSize ; lCount += 8)
201
 
        {
202
 
                if (SameDest)   // if encoded data is being written into input buffer
203
 
                {
204
 
                        if (lCount < lSize - 7) // if not dealing with uneven bytes at end
205
 
                        {
206
 
                                Blowfish_encipher ((DWORD *) pInput,
207
 
                                        (DWORD *) (pInput + 4)) ;
208
 
                        }
209
 
                        else            // pad end of data with null bytes to complete encryption
210
 
                        {
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++)
214
 
                                        *po++ = 0 ;
215
 
                                Blowfish_encipher ((DWORD *) pInput,
216
 
                                        (DWORD *) (pInput + 4)) ;
217
 
                        }
218
 
                        pInput += 8 ;
219
 
                }
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
223
 
                        {
224
 
                                pi = pInput ;
225
 
                                po = pOutput ;
226
 
                                for (i = 0 ; i < 8 ; i++)
227
 
// copy bytes to output
228
 
                                        *po++ = *pi++ ;
229
 
                                Blowfish_encipher ((DWORD *) pOutput,   // now encrypt them
230
 
                                        (DWORD *) (pOutput + 4)) ;
231
 
                        }
232
 
                        else            // pad end of data with null bytes to complete encryption
233
 
                        {
234
 
                                lGoodBytes = lSize - lCount ;   // number of remaining data bytes
235
 
                                po = pOutput ;
236
 
                                for (i = 0 ; i < (int) lGoodBytes ; i++)
237
 
                                        *po++ = *pInput++ ;
238
 
                                for (j = i ; j < 8 ; j++)
239
 
                                        *po++ = 0 ;
240
 
                                Blowfish_encipher ((DWORD *) pOutput,
241
 
                                        (DWORD *) (pOutput + 4)) ;
242
 
                        }
243
 
                        pInput += 8 ;
244
 
                        pOutput += 8 ;
245
 
                }
246
 
        }
247
 
        return lOutSize ;
248
 
 }
249
 
 
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)
253
 
{
254
 
        DWORD   lCount ;
255
 
        BYTE    *pi, *po ;
256
 
        int             i ;
257
 
        int             SameDest = (pInput == pOutput ? 1 : 0) ;
258
 
 
259
 
        for (lCount = 0 ; lCount < lSize ; lCount += 8)
260
 
        {
261
 
                if (SameDest)   // if encoded data is being written into input buffer
262
 
                {
263
 
                        Blowfish_decipher ((DWORD *) pInput,
264
 
                                (DWORD *) (pInput + 4)) ;
265
 
                        pInput += 8 ;
266
 
                }
267
 
                else                    // output buffer not equal to input buffer
268
 
                {               // so copy input to output before decoding
269
 
                        pi = pInput ;
270
 
                        po = pOutput ;
271
 
                        for (i = 0 ; i < 8 ; i++)
272
 
                                *po++ = *pi++ ;
273
 
                        Blowfish_decipher ((DWORD *) pOutput,
274
 
                                (DWORD *) (pOutput + 4)) ;
275
 
                        pInput += 8 ;
276
 
                        pOutput += 8 ;
277
 
                }
278
 
        }
279
 
}
 
1
////////////////////////////////////////////////////////////////////////////
 
2
// NoteCase notes manager project <http://notecase.sf.net>
 
3
//
 
4
// This code is licensed under BSD license.See "license.txt" for more details.
 
5
//
 
6
// File: C++ class implementation of the BLOWFISH encryption algorithm
 
7
////////////////////////////////////////////////////////////////////////////
 
8
 
 
9
// _THE BLOWFISH ENCRYPTION ALGORITHM_
 
10
// by Bruce Schneier
 
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)
 
15
 
 
16
#include "blowfish.h"
 
17
#include "blowfish2.h"  // holds the random digit tables
 
18
#include "debug.h"
 
19
 
 
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])
 
23
 
 
24
// choose a byte order for your hardware
 
25
#define ORDER_DCBA      // choosing Intel in this case
 
26
 
 
27
#ifdef ORDER_DCBA       // DCBA - little endian - intel
 
28
 
 
29
        union aword {
 
30
          DWORD dword;
 
31
          BYTE byte [4];
 
32
          struct {
 
33
            unsigned int byte3:8;
 
34
            unsigned int byte2:8;
 
35
            unsigned int byte1:8;
 
36
            unsigned int byte0:8;
 
37
          } w;
 
38
        };
 
39
 
 
40
#elif ORDER_ABCD        // ABCD - big endian - motorola
 
41
 
 
42
        union aword {
 
43
          DWORD dword;
 
44
          BYTE byte [4];
 
45
          struct {
 
46
            unsigned int byte0:8;
 
47
            unsigned int byte1:8;
 
48
            unsigned int byte2:8;
 
49
            unsigned int byte3:8;
 
50
          } w;
 
51
        };
 
52
 
 
53
#elif ORDER_BADC        // BADC - vax
 
54
 
 
55
        union aword {
 
56
          DWORD dword;
 
57
          BYTE byte [4];
 
58
          struct {
 
59
            unsigned int byte1:8;
 
60
            unsigned int byte0:8;
 
61
            unsigned int byte3:8;
 
62
            unsigned int byte2:8;
 
63
          } w;
 
64
        };
 
65
 
 
66
#endif
 
67
 
 
68
CBlowFish::CBlowFish ()
 
69
{
 
70
        ASSERT(sizeof(WORD) == 2);
 
71
        ASSERT(sizeof(DWORD) == 4);
 
72
        ASSERT(sizeof(union aword) == sizeof(DWORD));
 
73
 
 
74
        PArray = new DWORD [18] ;
 
75
        SBoxes = new DWORD [4][256] ;
 
76
}
 
77
 
 
78
CBlowFish::~CBlowFish ()
 
79
{
 
80
        delete [] PArray ;
 
81
        delete [] SBoxes ;
 
82
}
 
83
 
 
84
        // the low level (private) encryption function
 
85
void CBlowFish::Blowfish_encipher (DWORD *xl, DWORD *xr)
 
86
{
 
87
        union aword  Xl, Xr ;
 
88
 
 
89
        Xl.dword = *xl ;
 
90
        Xr.dword = *xr ;
 
91
 
 
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] ;
 
102
 
 
103
        *xr = Xl.dword ;
 
104
        *xl = Xr.dword ;
 
105
}
 
106
 
 
107
        // the low level (private) decryption function
 
108
void CBlowFish::Blowfish_decipher (DWORD *xl, DWORD *xr)
 
109
{
 
110
   union aword  Xl ;
 
111
   union aword  Xr ;
 
112
 
 
113
   Xl.dword = *xl ;
 
114
   Xr.dword = *xr ;
 
115
 
 
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];
 
126
 
 
127
   *xl = Xr.dword;
 
128
   *xr = Xl.dword;
 
129
}
 
130
 
 
131
        // constructs the enctryption sieve
 
132
void CBlowFish::Initialize (BYTE key[], int keybytes)
 
133
{
 
134
        int             i, j ;
 
135
        DWORD           data, datal, datar ;
 
136
        union aword temp ;
 
137
 
 
138
        // first fill arrays from data tables
 
139
        for (i = 0 ; i < 18 ; i++)
 
140
                PArray [i] = bf_P [i] ;
 
141
 
 
142
        for (i = 0 ; i < 4 ; i++)
 
143
        {
 
144
                for (j = 0 ; j < 256 ; j++)
 
145
                        SBoxes [i][j] = bf_S [i][j] ;
 
146
        }
 
147
 
 
148
 
 
149
        j = 0 ;
 
150
        for (i = 0 ; i < NPASS + 2 ; ++i)
 
151
        {
 
152
                temp.dword = 0 ;
 
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] ;
 
157
                data = temp.dword ;
 
158
                PArray [i] ^= data ;
 
159
                j = (j + 4) % keybytes ;
 
160
        }
 
161
 
 
162
        datal = 0 ;
 
163
        datar = 0 ;
 
164
 
 
165
        for (i = 0 ; i < NPASS + 2 ; i += 2)
 
166
        {
 
167
                Blowfish_encipher (&datal, &datar) ;
 
168
                PArray [i] = datal ;
 
169
                PArray [i + 1] = datar ;
 
170
        }
 
171
 
 
172
        for (i = 0 ; i < 4 ; ++i)
 
173
        {
 
174
                for (j = 0 ; j < 256 ; j += 2)
 
175
                {
 
176
                  Blowfish_encipher (&datal, &datar) ;
 
177
                  SBoxes [i][j] = datal ;
 
178
                  SBoxes [i][j + 1] = datar ;
 
179
                }
 
180
        }
 
181
}
 
182
 
 
183
        // get output length, which must be even MOD 8
 
184
DWORD CBlowFish::GetOutputLength (DWORD lInputLong)
 
185
{
 
186
        DWORD   lVal ;
 
187
 
 
188
        lVal = lInputLong % 8 ; // find out if uneven number of bytes at the end
 
189
        if (lVal != 0)
 
190
                return lInputLong + 8 - lVal ;
 
191
        else
 
192
                return lInputLong;
 
193
}
 
194
 
 
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)
 
199
{
 
200
        DWORD   lCount, lOutSize, lGoodBytes;
 
201
        BYTE    *pi, *po;
 
202
        int             i, j;
 
203
        int             SameDest = (pInput == pOutput ? 1 : 0);
 
204
 
 
205
        lOutSize = GetOutputLength (lSize);
 
206
        for (lCount = 0; lCount < lOutSize; lCount += 8)
 
207
        {
 
208
                if (SameDest)   // if encoded data is being written into input buffer
 
209
                {
 
210
                        if (lCount < lSize - 7) // if not dealing with uneven bytes at end
 
211
                        {
 
212
                                Blowfish_encipher ((DWORD *) pInput, (DWORD *) (pInput + 4)) ;
 
213
                        }
 
214
                        else            // pad end of data with null bytes to complete encryption
 
215
                        {
 
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++)
 
219
                                        *po++ = 0;
 
220
                                Blowfish_encipher ((DWORD *) pInput, (DWORD *) (pInput + 4)) ;
 
221
                        }
 
222
                        pInput += 8;
 
223
                }
 
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
 
227
                        {
 
228
                                pi = pInput;
 
229
                                po = pOutput;
 
230
                                for (i = 0 ; i < 8 ; i++)                               // copy bytes to output
 
231
                                        *po++ = *pi++;
 
232
                                Blowfish_encipher ((DWORD *) pOutput, (DWORD *) (pOutput + 4)); // now encrypt them
 
233
                        }
 
234
                        else            // pad end of data with null bytes to complete encryption
 
235
                        {
 
236
                                lGoodBytes = lSize - lCount;    // number of remaining data bytes
 
237
                                po = pOutput;
 
238
                                for (i = 0; i < (int) lGoodBytes ; i++)
 
239
                                        *po++ = *pInput++;
 
240
                                for (j = i; j < 8; j++)
 
241
                                        *po++ = 0;
 
242
                                Blowfish_encipher ((DWORD *) pOutput, (DWORD *) (pOutput + 4));
 
243
                        }
 
244
                        pInput += 8;
 
245
                        pOutput += 8;
 
246
                }
 
247
        }
 
248
        return lOutSize;
 
249
}
 
250
 
 
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)
 
254
{
 
255
        DWORD   lCount ;
 
256
        BYTE    *pi, *po ;
 
257
        int             i ;
 
258
        int             SameDest = (pInput == pOutput ? 1 : 0) ;
 
259
 
 
260
        for (lCount = 0 ; lCount < lSize ; lCount += 8)
 
261
        {
 
262
                if (SameDest)   // if encoded data is being written into input buffer
 
263
                {
 
264
                        Blowfish_decipher ((DWORD *) pInput, (DWORD *) (pInput + 4));
 
265
                        pInput += 8 ;
 
266
                }
 
267
                else                    // output buffer not equal to input buffer
 
268
                {               // so copy input to output before decoding
 
269
                        pi = pInput ;
 
270
                        po = pOutput ;
 
271
                        for (i = 0 ; i < 8 ; i++)
 
272
                                *po++ = *pi++ ;
 
273
                        Blowfish_decipher ((DWORD *) pOutput, (DWORD *) (pOutput + 4));
 
274
                        pInput += 8 ;
 
275
                        pOutput += 8 ;
 
276
                }
 
277
        }
 
278
}