cryptlib
3.4.1
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Macros
crypt
bfenc.c
Go to the documentation of this file.
1
/* crypto/bf/bf_enc.c */
2
/* Copyright (C) 1995-1998 Eric Young (
[email protected]
)
3
* All rights reserved.
4
*
5
* This package is an SSL implementation written
6
* by Eric Young (
[email protected]
).
7
* The implementation was written so as to conform with Netscapes SSL.
8
*
9
* This library is free for commercial and non-commercial use as long as
10
* the following conditions are aheared to. The following conditions
11
* apply to all code found in this distribution, be it the RC4, RSA,
12
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
13
* included with this distribution is covered by the same copyright terms
14
* except that the holder is Tim Hudson (
[email protected]
).
15
*
16
* Copyright remains Eric Young's, and as such any Copyright notices in
17
* the code are not to be removed.
18
* If this package is used in a product, Eric Young should be given attribution
19
* as the author of the parts of the library used.
20
* This can be in the form of a textual message at program startup or
21
* in documentation (online or textual) provided with the package.
22
*
23
* Redistribution and use in source and binary forms, with or without
24
* modification, are permitted provided that the following conditions
25
* are met:
26
* 1. Redistributions of source code must retain the copyright
27
* notice, this list of conditions and the following disclaimer.
28
* 2. Redistributions in binary form must reproduce the above copyright
29
* notice, this list of conditions and the following disclaimer in the
30
* documentation and/or other materials provided with the distribution.
31
* 3. All advertising materials mentioning features or use of this software
32
* must display the following acknowledgement:
33
* "This product includes cryptographic software written by
34
* Eric Young (
[email protected]
)"
35
* The word 'cryptographic' can be left out if the rouines from the library
36
* being used are not cryptographic related :-).
37
* 4. If you include any Windows specific code (or a derivative thereof) from
38
* the apps directory (application code) you must include an acknowledgement:
39
* "This product includes software written by Tim Hudson (
[email protected]
)"
40
*
41
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
* SUCH DAMAGE.
52
*
53
* The licence and distribution terms for any publically available version or
54
* derivative of this code cannot be changed. i.e. this code cannot simply be
55
* copied and put under another distribution licence
56
* [including the GNU Public Licence.]
57
*/
58
59
#if defined( INC_ALL )
60
#include "
osconfig.h
"
61
#include "
blowfish.h
"
62
#include "
bflocl.h
"
63
#else
64
#include "
crypt/osconfig.h
"
65
#include "
crypt/blowfish.h
"
66
#include "
crypt/bflocl.h
"
67
#endif
/* Compiler-specific includes */
68
69
#ifndef USE_ASM
70
71
/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
72
* (From LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
73
* CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
74
*/
75
76
#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
77
#error If you set BF_ROUNDS to some value other than 16 or 20, you will have \
78
to modify the code.
79
#endif
80
81
void
BF_encrypt
(
BF_LONG
*
data
,
const
BF_KEY
*
key
)
82
{
83
#ifndef BF_PTR2
84
register
BF_LONG
l,r;
85
const
register
BF_LONG
*
p
,*
s
;
86
87
p=key->
P
;
88
s= &(key->
S
[0]);
89
l=data[0];
90
r=data[1];
91
92
l^=p[0];
93
BF_ENC
(r,l,s,p[ 1]);
94
BF_ENC
(l,r,s,p[ 2]);
95
BF_ENC
(r,l,s,p[ 3]);
96
BF_ENC
(l,r,s,p[ 4]);
97
BF_ENC
(r,l,s,p[ 5]);
98
BF_ENC
(l,r,s,p[ 6]);
99
BF_ENC
(r,l,s,p[ 7]);
100
BF_ENC
(l,r,s,p[ 8]);
101
BF_ENC
(r,l,s,p[ 9]);
102
BF_ENC
(l,r,s,p[10]);
103
BF_ENC
(r,l,s,p[11]);
104
BF_ENC
(l,r,s,p[12]);
105
BF_ENC
(r,l,s,p[13]);
106
BF_ENC
(l,r,s,p[14]);
107
BF_ENC
(r,l,s,p[15]);
108
BF_ENC
(l,r,s,p[16]);
109
#if BF_ROUNDS == 20
110
BF_ENC
(r,l,s,p[17]);
111
BF_ENC
(l,r,s,p[18]);
112
BF_ENC
(r,l,s,p[19]);
113
BF_ENC
(l,r,s,p[20]);
114
#endif
115
r^=p[
BF_ROUNDS
+1];
116
117
data[1]=l&0xffffffffL;
118
data[0]=r&0xffffffffL;
119
#else
120
register
BF_LONG
l,r,t,*k;
121
122
l=data[0];
123
r=data[1];
124
k=(
BF_LONG
*)key;
125
126
l^=k[0];
127
BF_ENC
(r,l,k, 1);
128
BF_ENC
(l,r,k, 2);
129
BF_ENC
(r,l,k, 3);
130
BF_ENC
(l,r,k, 4);
131
BF_ENC
(r,l,k, 5);
132
BF_ENC
(l,r,k, 6);
133
BF_ENC
(r,l,k, 7);
134
BF_ENC
(l,r,k, 8);
135
BF_ENC
(r,l,k, 9);
136
BF_ENC
(l,r,k,10);
137
BF_ENC
(r,l,k,11);
138
BF_ENC
(l,r,k,12);
139
BF_ENC
(r,l,k,13);
140
BF_ENC
(l,r,k,14);
141
BF_ENC
(r,l,k,15);
142
BF_ENC
(l,r,k,16);
143
#if BF_ROUNDS == 20
144
BF_ENC
(r,l,k,17);
145
BF_ENC
(l,r,k,18);
146
BF_ENC
(r,l,k,19);
147
BF_ENC
(l,r,k,20);
148
#endif
149
r^=k[
BF_ROUNDS
+1];
150
151
data[1]=l&0xffffffffL;
152
data[0]=r&0xffffffffL;
153
#endif
154
}
155
156
#ifndef BF_DEFAULT_OPTIONS
157
158
void
BF_decrypt
(
BF_LONG
*
data
,
const
BF_KEY
*
key
)
159
{
160
#ifndef BF_PTR2
161
register
BF_LONG
l,r;
162
const
register
BF_LONG
*
p
,*
s
;
163
164
p=key->
P
;
165
s= &(key->
S
[0]);
166
l=data[0];
167
r=data[1];
168
169
l^=p[
BF_ROUNDS
+1];
170
#if BF_ROUNDS == 20
171
BF_ENC
(r,l,s,p[20]);
172
BF_ENC
(l,r,s,p[19]);
173
BF_ENC
(r,l,s,p[18]);
174
BF_ENC
(l,r,s,p[17]);
175
#endif
176
BF_ENC
(r,l,s,p[16]);
177
BF_ENC
(l,r,s,p[15]);
178
BF_ENC
(r,l,s,p[14]);
179
BF_ENC
(l,r,s,p[13]);
180
BF_ENC
(r,l,s,p[12]);
181
BF_ENC
(l,r,s,p[11]);
182
BF_ENC
(r,l,s,p[10]);
183
BF_ENC
(l,r,s,p[ 9]);
184
BF_ENC
(r,l,s,p[ 8]);
185
BF_ENC
(l,r,s,p[ 7]);
186
BF_ENC
(r,l,s,p[ 6]);
187
BF_ENC
(l,r,s,p[ 5]);
188
BF_ENC
(r,l,s,p[ 4]);
189
BF_ENC
(l,r,s,p[ 3]);
190
BF_ENC
(r,l,s,p[ 2]);
191
BF_ENC
(l,r,s,p[ 1]);
192
r^=p[0];
193
194
data[1]=l&0xffffffffL;
195
data[0]=r&0xffffffffL;
196
#else
197
register
BF_LONG
l,r,t,*k;
198
199
l=data[0];
200
r=data[1];
201
k=(
BF_LONG
*)key;
202
203
l^=k[
BF_ROUNDS
+1];
204
#if BF_ROUNDS == 20
205
BF_ENC
(r,l,k,20);
206
BF_ENC
(l,r,k,19);
207
BF_ENC
(r,l,k,18);
208
BF_ENC
(l,r,k,17);
209
#endif
210
BF_ENC
(r,l,k,16);
211
BF_ENC
(l,r,k,15);
212
BF_ENC
(r,l,k,14);
213
BF_ENC
(l,r,k,13);
214
BF_ENC
(r,l,k,12);
215
BF_ENC
(l,r,k,11);
216
BF_ENC
(r,l,k,10);
217
BF_ENC
(l,r,k, 9);
218
BF_ENC
(r,l,k, 8);
219
BF_ENC
(l,r,k, 7);
220
BF_ENC
(r,l,k, 6);
221
BF_ENC
(l,r,k, 5);
222
BF_ENC
(r,l,k, 4);
223
BF_ENC
(l,r,k, 3);
224
BF_ENC
(r,l,k, 2);
225
BF_ENC
(l,r,k, 1);
226
r^=k[0];
227
228
data[1]=l&0xffffffffL;
229
data[0]=r&0xffffffffL;
230
#endif
231
}
232
233
void
BF_cbc_encrypt
(
const
unsigned
char
*in,
unsigned
char
*out,
long
length
,
234
const
BF_KEY
*schedule,
unsigned
char
*ivec,
int
encrypt)
235
{
236
register
BF_LONG
tin0,tin1;
237
register
BF_LONG
tout0,tout1,xor0,xor1;
238
register
long
l=
length
;
239
BF_LONG
tin[2];
240
241
if
(encrypt)
242
{
243
n2l
(ivec,tout0);
244
n2l
(ivec,tout1);
245
ivec-=8;
246
for
(l-=8; l>=0; l-=8)
247
{
248
n2l
(in,tin0);
249
n2l
(in,tin1);
250
tin0^=tout0;
251
tin1^=tout1;
252
tin[0]=tin0;
253
tin[1]=tin1;
254
BF_encrypt
(tin,schedule);
255
tout0=tin[0];
256
tout1=tin[1];
257
l2n
(tout0,out);
258
l2n
(tout1,out);
259
}
260
if
(l != -8)
261
{
262
n2ln
(in,tin0,tin1,l+8);
263
tin0^=tout0;
264
tin1^=tout1;
265
tin[0]=tin0;
266
tin[1]=tin1;
267
BF_encrypt
(tin,schedule);
268
tout0=tin[0];
269
tout1=tin[1];
270
l2n
(tout0,out);
271
l2n
(tout1,out);
272
}
273
l2n
(tout0,ivec);
274
l2n
(tout1,ivec);
275
}
276
else
277
{
278
n2l
(ivec,xor0);
279
n2l
(ivec,xor1);
280
ivec-=8;
281
for
(l-=8; l>=0; l-=8)
282
{
283
n2l
(in,tin0);
284
n2l
(in,tin1);
285
tin[0]=tin0;
286
tin[1]=tin1;
287
BF_decrypt
(tin,schedule);
288
tout0=tin[0]^xor0;
289
tout1=tin[1]^xor1;
290
l2n
(tout0,out);
291
l2n
(tout1,out);
292
xor0=tin0;
293
xor1=tin1;
294
}
295
if
(l != -8)
296
{
297
n2l
(in,tin0);
298
n2l
(in,tin1);
299
tin[0]=tin0;
300
tin[1]=tin1;
301
BF_decrypt
(tin,schedule);
302
tout0=tin[0]^xor0;
303
tout1=tin[1]^xor1;
304
l2nn
(tout0,tout1,out,l+8);
305
xor0=tin0;
306
xor1=tin1;
307
}
308
l2n
(xor0,ivec);
309
l2n
(xor1,ivec);
310
}
311
tin0=tin1=tout0=tout1=xor0=xor1=0;
312
tin[0]=tin[1]=0;
313
}
314
315
#endif
316
317
#endif
/* USE_ASM */
Generated on Thu Jan 10 2013 10:00:45 for cryptlib by
1.8.2