diff options
Diffstat (limited to 'src/lib/libcrypto/bf/bf_enc.c')
-rw-r--r-- | src/lib/libcrypto/bf/bf_enc.c | 129 |
1 files changed, 97 insertions, 32 deletions
diff --git a/src/lib/libcrypto/bf/bf_enc.c b/src/lib/libcrypto/bf/bf_enc.c index 66a8604c59..b380acf959 100644 --- a/src/lib/libcrypto/bf/bf_enc.c +++ b/src/lib/libcrypto/bf/bf_enc.c | |||
@@ -56,24 +56,24 @@ | |||
56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
57 | */ | 57 | */ |
58 | 58 | ||
59 | #include "blowfish.h" | 59 | #include <openssl/blowfish.h> |
60 | #include "bf_locl.h" | 60 | #include "bf_locl.h" |
61 | 61 | ||
62 | /* Blowfish as implemented from 'Blowfish: Springer-Verlag paper' | 62 | /* Blowfish as implemented from 'Blowfish: Springer-Verlag paper' |
63 | * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, | 63 | * (From LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, |
64 | * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) | 64 | * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) |
65 | */ | 65 | */ |
66 | 66 | ||
67 | #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) | 67 | #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) |
68 | If you set BF_ROUNDS to some value other than 16 or 20, you will have | 68 | #error If you set BF_ROUNDS to some value other than 16 or 20, you will have \ |
69 | to modify the code. | 69 | to modify the code. |
70 | #endif | 70 | #endif |
71 | 71 | ||
72 | void BF_encrypt(data,key) | 72 | void BF_encrypt(BF_LONG *data, const BF_KEY *key) |
73 | BF_LONG *data; | ||
74 | BF_KEY *key; | ||
75 | { | 73 | { |
76 | register BF_LONG l,r,*p,*s; | 74 | #ifndef BF_PTR2 |
75 | register BF_LONG l,r; | ||
76 | const register BF_LONG *p,*s; | ||
77 | 77 | ||
78 | p=key->P; | 78 | p=key->P; |
79 | s= &(key->S[0]); | 79 | s= &(key->S[0]); |
@@ -107,15 +107,50 @@ BF_KEY *key; | |||
107 | 107 | ||
108 | data[1]=l&0xffffffffL; | 108 | data[1]=l&0xffffffffL; |
109 | data[0]=r&0xffffffffL; | 109 | data[0]=r&0xffffffffL; |
110 | #else | ||
111 | register BF_LONG l,r,t,*k; | ||
112 | |||
113 | l=data[0]; | ||
114 | r=data[1]; | ||
115 | k=(BF_LONG*)key; | ||
116 | |||
117 | l^=k[0]; | ||
118 | BF_ENC(r,l,k, 1); | ||
119 | BF_ENC(l,r,k, 2); | ||
120 | BF_ENC(r,l,k, 3); | ||
121 | BF_ENC(l,r,k, 4); | ||
122 | BF_ENC(r,l,k, 5); | ||
123 | BF_ENC(l,r,k, 6); | ||
124 | BF_ENC(r,l,k, 7); | ||
125 | BF_ENC(l,r,k, 8); | ||
126 | BF_ENC(r,l,k, 9); | ||
127 | BF_ENC(l,r,k,10); | ||
128 | BF_ENC(r,l,k,11); | ||
129 | BF_ENC(l,r,k,12); | ||
130 | BF_ENC(r,l,k,13); | ||
131 | BF_ENC(l,r,k,14); | ||
132 | BF_ENC(r,l,k,15); | ||
133 | BF_ENC(l,r,k,16); | ||
134 | #if BF_ROUNDS == 20 | ||
135 | BF_ENC(r,l,k,17); | ||
136 | BF_ENC(l,r,k,18); | ||
137 | BF_ENC(r,l,k,19); | ||
138 | BF_ENC(l,r,k,20); | ||
139 | #endif | ||
140 | r^=k[BF_ROUNDS+1]; | ||
141 | |||
142 | data[1]=l&0xffffffffL; | ||
143 | data[0]=r&0xffffffffL; | ||
144 | #endif | ||
110 | } | 145 | } |
111 | 146 | ||
112 | #ifndef BF_DEFAULT_OPTIONS | 147 | #ifndef BF_DEFAULT_OPTIONS |
113 | 148 | ||
114 | void BF_decrypt(data,key) | 149 | void BF_decrypt(BF_LONG *data, const BF_KEY *key) |
115 | BF_LONG *data; | ||
116 | BF_KEY *key; | ||
117 | { | 150 | { |
118 | register BF_LONG l,r,*p,*s; | 151 | #ifndef BF_PTR2 |
152 | register BF_LONG l,r; | ||
153 | const register BF_LONG *p,*s; | ||
119 | 154 | ||
120 | p=key->P; | 155 | p=key->P; |
121 | s= &(key->S[0]); | 156 | s= &(key->S[0]); |
@@ -149,15 +184,45 @@ BF_KEY *key; | |||
149 | 184 | ||
150 | data[1]=l&0xffffffffL; | 185 | data[1]=l&0xffffffffL; |
151 | data[0]=r&0xffffffffL; | 186 | data[0]=r&0xffffffffL; |
187 | #else | ||
188 | register BF_LONG l,r,t,*k; | ||
189 | |||
190 | l=data[0]; | ||
191 | r=data[1]; | ||
192 | k=(BF_LONG *)key; | ||
193 | |||
194 | l^=k[BF_ROUNDS+1]; | ||
195 | #if BF_ROUNDS == 20 | ||
196 | BF_ENC(r,l,k,20); | ||
197 | BF_ENC(l,r,k,19); | ||
198 | BF_ENC(r,l,k,18); | ||
199 | BF_ENC(l,r,k,17); | ||
200 | #endif | ||
201 | BF_ENC(r,l,k,16); | ||
202 | BF_ENC(l,r,k,15); | ||
203 | BF_ENC(r,l,k,14); | ||
204 | BF_ENC(l,r,k,13); | ||
205 | BF_ENC(r,l,k,12); | ||
206 | BF_ENC(l,r,k,11); | ||
207 | BF_ENC(r,l,k,10); | ||
208 | BF_ENC(l,r,k, 9); | ||
209 | BF_ENC(r,l,k, 8); | ||
210 | BF_ENC(l,r,k, 7); | ||
211 | BF_ENC(r,l,k, 6); | ||
212 | BF_ENC(l,r,k, 5); | ||
213 | BF_ENC(r,l,k, 4); | ||
214 | BF_ENC(l,r,k, 3); | ||
215 | BF_ENC(r,l,k, 2); | ||
216 | BF_ENC(l,r,k, 1); | ||
217 | r^=k[0]; | ||
218 | |||
219 | data[1]=l&0xffffffffL; | ||
220 | data[0]=r&0xffffffffL; | ||
221 | #endif | ||
152 | } | 222 | } |
153 | 223 | ||
154 | void BF_cbc_encrypt(in, out, length, ks, iv, encrypt) | 224 | void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, |
155 | unsigned char *in; | 225 | const BF_KEY *schedule, unsigned char *ivec, int encrypt) |
156 | unsigned char *out; | ||
157 | long length; | ||
158 | BF_KEY *ks; | ||
159 | unsigned char *iv; | ||
160 | int encrypt; | ||
161 | { | 226 | { |
162 | register BF_LONG tin0,tin1; | 227 | register BF_LONG tin0,tin1; |
163 | register BF_LONG tout0,tout1,xor0,xor1; | 228 | register BF_LONG tout0,tout1,xor0,xor1; |
@@ -166,9 +231,9 @@ int encrypt; | |||
166 | 231 | ||
167 | if (encrypt) | 232 | if (encrypt) |
168 | { | 233 | { |
169 | n2l(iv,tout0); | 234 | n2l(ivec,tout0); |
170 | n2l(iv,tout1); | 235 | n2l(ivec,tout1); |
171 | iv-=8; | 236 | ivec-=8; |
172 | for (l-=8; l>=0; l-=8) | 237 | for (l-=8; l>=0; l-=8) |
173 | { | 238 | { |
174 | n2l(in,tin0); | 239 | n2l(in,tin0); |
@@ -177,7 +242,7 @@ int encrypt; | |||
177 | tin1^=tout1; | 242 | tin1^=tout1; |
178 | tin[0]=tin0; | 243 | tin[0]=tin0; |
179 | tin[1]=tin1; | 244 | tin[1]=tin1; |
180 | BF_encrypt(tin,ks); | 245 | BF_encrypt(tin,schedule); |
181 | tout0=tin[0]; | 246 | tout0=tin[0]; |
182 | tout1=tin[1]; | 247 | tout1=tin[1]; |
183 | l2n(tout0,out); | 248 | l2n(tout0,out); |
@@ -190,27 +255,27 @@ int encrypt; | |||
190 | tin1^=tout1; | 255 | tin1^=tout1; |
191 | tin[0]=tin0; | 256 | tin[0]=tin0; |
192 | tin[1]=tin1; | 257 | tin[1]=tin1; |
193 | BF_encrypt(tin,ks); | 258 | BF_encrypt(tin,schedule); |
194 | tout0=tin[0]; | 259 | tout0=tin[0]; |
195 | tout1=tin[1]; | 260 | tout1=tin[1]; |
196 | l2n(tout0,out); | 261 | l2n(tout0,out); |
197 | l2n(tout1,out); | 262 | l2n(tout1,out); |
198 | } | 263 | } |
199 | l2n(tout0,iv); | 264 | l2n(tout0,ivec); |
200 | l2n(tout1,iv); | 265 | l2n(tout1,ivec); |
201 | } | 266 | } |
202 | else | 267 | else |
203 | { | 268 | { |
204 | n2l(iv,xor0); | 269 | n2l(ivec,xor0); |
205 | n2l(iv,xor1); | 270 | n2l(ivec,xor1); |
206 | iv-=8; | 271 | ivec-=8; |
207 | for (l-=8; l>=0; l-=8) | 272 | for (l-=8; l>=0; l-=8) |
208 | { | 273 | { |
209 | n2l(in,tin0); | 274 | n2l(in,tin0); |
210 | n2l(in,tin1); | 275 | n2l(in,tin1); |
211 | tin[0]=tin0; | 276 | tin[0]=tin0; |
212 | tin[1]=tin1; | 277 | tin[1]=tin1; |
213 | BF_decrypt(tin,ks); | 278 | BF_decrypt(tin,schedule); |
214 | tout0=tin[0]^xor0; | 279 | tout0=tin[0]^xor0; |
215 | tout1=tin[1]^xor1; | 280 | tout1=tin[1]^xor1; |
216 | l2n(tout0,out); | 281 | l2n(tout0,out); |
@@ -224,15 +289,15 @@ int encrypt; | |||
224 | n2l(in,tin1); | 289 | n2l(in,tin1); |
225 | tin[0]=tin0; | 290 | tin[0]=tin0; |
226 | tin[1]=tin1; | 291 | tin[1]=tin1; |
227 | BF_decrypt(tin,ks); | 292 | BF_decrypt(tin,schedule); |
228 | tout0=tin[0]^xor0; | 293 | tout0=tin[0]^xor0; |
229 | tout1=tin[1]^xor1; | 294 | tout1=tin[1]^xor1; |
230 | l2nn(tout0,tout1,out,l+8); | 295 | l2nn(tout0,tout1,out,l+8); |
231 | xor0=tin0; | 296 | xor0=tin0; |
232 | xor1=tin1; | 297 | xor1=tin1; |
233 | } | 298 | } |
234 | l2n(xor0,iv); | 299 | l2n(xor0,ivec); |
235 | l2n(xor1,iv); | 300 | l2n(xor1,ivec); |
236 | } | 301 | } |
237 | tin0=tin1=tout0=tout1=xor0=xor1=0; | 302 | tin0=tin1=tout0=tout1=xor0=xor1=0; |
238 | tin[0]=tin[1]=0; | 303 | tin[0]=tin[1]=0; |