summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bf/bf_enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/bf/bf_enc.c')
-rw-r--r--src/lib/libcrypto/bf/bf_enc.c406
1 files changed, 201 insertions, 205 deletions
diff --git a/src/lib/libcrypto/bf/bf_enc.c b/src/lib/libcrypto/bf/bf_enc.c
index 2cf1c86063..464d36d345 100644
--- a/src/lib/libcrypto/bf/bf_enc.c
+++ b/src/lib/libcrypto/bf/bf_enc.c
@@ -1,25 +1,25 @@
1/* $OpenBSD: bf_enc.c,v 1.6 2014/10/28 07:35:58 jsg Exp $ */ 1/* $OpenBSD: bf_enc.c,v 1.7 2022/11/11 12:08:29 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 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 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 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 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. 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 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. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 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: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 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 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 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 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -69,238 +69,234 @@
69to modify the code. 69to modify the code.
70#endif 70#endif
71 71
72void BF_encrypt(BF_LONG *data, const BF_KEY *key) 72void
73 { 73BF_encrypt(BF_LONG *data, const BF_KEY *key)
74{
74#ifndef BF_PTR2 75#ifndef BF_PTR2
75 BF_LONG l,r; 76 BF_LONG l, r;
76 const BF_LONG *p,*s; 77 const BF_LONG *p, *s;
77 78
78 p=key->P; 79 p = key->P;
79 s= &(key->S[0]); 80 s = &(key->S[0]);
80 l=data[0]; 81 l = data[0];
81 r=data[1]; 82 r = data[1];
82 83
83 l^=p[0]; 84 l ^= p[0];
84 BF_ENC(r,l,s,p[ 1]); 85 BF_ENC(r, l,s, p[1]);
85 BF_ENC(l,r,s,p[ 2]); 86 BF_ENC(l, r,s, p[2]);
86 BF_ENC(r,l,s,p[ 3]); 87 BF_ENC(r, l,s, p[3]);
87 BF_ENC(l,r,s,p[ 4]); 88 BF_ENC(l, r,s, p[4]);
88 BF_ENC(r,l,s,p[ 5]); 89 BF_ENC(r, l,s, p[5]);
89 BF_ENC(l,r,s,p[ 6]); 90 BF_ENC(l, r,s, p[6]);
90 BF_ENC(r,l,s,p[ 7]); 91 BF_ENC(r, l,s, p[7]);
91 BF_ENC(l,r,s,p[ 8]); 92 BF_ENC(l, r,s, p[8]);
92 BF_ENC(r,l,s,p[ 9]); 93 BF_ENC(r, l,s, p[9]);
93 BF_ENC(l,r,s,p[10]); 94 BF_ENC(l, r,s, p[10]);
94 BF_ENC(r,l,s,p[11]); 95 BF_ENC(r, l,s, p[11]);
95 BF_ENC(l,r,s,p[12]); 96 BF_ENC(l, r,s, p[12]);
96 BF_ENC(r,l,s,p[13]); 97 BF_ENC(r, l,s, p[13]);
97 BF_ENC(l,r,s,p[14]); 98 BF_ENC(l, r,s, p[14]);
98 BF_ENC(r,l,s,p[15]); 99 BF_ENC(r, l,s, p[15]);
99 BF_ENC(l,r,s,p[16]); 100 BF_ENC(l, r,s, p[16]);
100#if BF_ROUNDS == 20 101#if BF_ROUNDS == 20
101 BF_ENC(r,l,s,p[17]); 102 BF_ENC(r, l,s, p[17]);
102 BF_ENC(l,r,s,p[18]); 103 BF_ENC(l, r,s, p[18]);
103 BF_ENC(r,l,s,p[19]); 104 BF_ENC(r, l,s, p[19]);
104 BF_ENC(l,r,s,p[20]); 105 BF_ENC(l, r,s, p[20]);
105#endif 106#endif
106 r^=p[BF_ROUNDS+1]; 107 r ^= p[BF_ROUNDS + 1];
107 108
108 data[1]=l&0xffffffffL; 109 data[1] = l&0xffffffffL;
109 data[0]=r&0xffffffffL; 110 data[0] = r&0xffffffffL;
110#else 111#else
111 BF_LONG l,r,t,*k; 112 BF_LONG l, r,t, *k;
112 113
113 l=data[0]; 114 l = data[0];
114 r=data[1]; 115 r = data[1];
115 k=(BF_LONG*)key; 116 k = (BF_LONG*)key;
116 117
117 l^=k[0]; 118 l ^= k[0];
118 BF_ENC(r,l,k, 1); 119 BF_ENC(r, l, k, 1);
119 BF_ENC(l,r,k, 2); 120 BF_ENC(l, r, k, 2);
120 BF_ENC(r,l,k, 3); 121 BF_ENC(r, l, k, 3);
121 BF_ENC(l,r,k, 4); 122 BF_ENC(l, r, k, 4);
122 BF_ENC(r,l,k, 5); 123 BF_ENC(r, l, k, 5);
123 BF_ENC(l,r,k, 6); 124 BF_ENC(l, r, k, 6);
124 BF_ENC(r,l,k, 7); 125 BF_ENC(r, l, k, 7);
125 BF_ENC(l,r,k, 8); 126 BF_ENC(l, r, k, 8);
126 BF_ENC(r,l,k, 9); 127 BF_ENC(r, l, k, 9);
127 BF_ENC(l,r,k,10); 128 BF_ENC(l, r,k, 10);
128 BF_ENC(r,l,k,11); 129 BF_ENC(r, l,k, 11);
129 BF_ENC(l,r,k,12); 130 BF_ENC(l, r,k, 12);
130 BF_ENC(r,l,k,13); 131 BF_ENC(r, l,k, 13);
131 BF_ENC(l,r,k,14); 132 BF_ENC(l, r,k, 14);
132 BF_ENC(r,l,k,15); 133 BF_ENC(r, l,k, 15);
133 BF_ENC(l,r,k,16); 134 BF_ENC(l, r,k, 16);
134#if BF_ROUNDS == 20 135#if BF_ROUNDS == 20
135 BF_ENC(r,l,k,17); 136 BF_ENC(r, l,k, 17);
136 BF_ENC(l,r,k,18); 137 BF_ENC(l, r,k, 18);
137 BF_ENC(r,l,k,19); 138 BF_ENC(r, l,k, 19);
138 BF_ENC(l,r,k,20); 139 BF_ENC(l, r,k, 20);
139#endif 140#endif
140 r^=k[BF_ROUNDS+1]; 141 r ^= k[BF_ROUNDS + 1];
141 142
142 data[1]=l&0xffffffffL; 143 data[1] = l&0xffffffffL;
143 data[0]=r&0xffffffffL; 144 data[0] = r&0xffffffffL;
144#endif 145#endif
145 } 146}
146 147
147#ifndef BF_DEFAULT_OPTIONS 148#ifndef BF_DEFAULT_OPTIONS
148 149
149void BF_decrypt(BF_LONG *data, const BF_KEY *key) 150void
150 { 151BF_decrypt(BF_LONG *data, const BF_KEY *key)
152{
151#ifndef BF_PTR2 153#ifndef BF_PTR2
152 BF_LONG l,r; 154 BF_LONG l, r;
153 const BF_LONG *p,*s; 155 const BF_LONG *p, *s;
154 156
155 p=key->P; 157 p = key->P;
156 s= &(key->S[0]); 158 s = &(key->S[0]);
157 l=data[0]; 159 l = data[0];
158 r=data[1]; 160 r = data[1];
159 161
160 l^=p[BF_ROUNDS+1]; 162 l ^= p[BF_ROUNDS + 1];
161#if BF_ROUNDS == 20 163#if BF_ROUNDS == 20
162 BF_ENC(r,l,s,p[20]); 164 BF_ENC(r, l,s, p[20]);
163 BF_ENC(l,r,s,p[19]); 165 BF_ENC(l, r,s, p[19]);
164 BF_ENC(r,l,s,p[18]); 166 BF_ENC(r, l,s, p[18]);
165 BF_ENC(l,r,s,p[17]); 167 BF_ENC(l, r,s, p[17]);
166#endif 168#endif
167 BF_ENC(r,l,s,p[16]); 169 BF_ENC(r, l,s, p[16]);
168 BF_ENC(l,r,s,p[15]); 170 BF_ENC(l, r,s, p[15]);
169 BF_ENC(r,l,s,p[14]); 171 BF_ENC(r, l,s, p[14]);
170 BF_ENC(l,r,s,p[13]); 172 BF_ENC(l, r,s, p[13]);
171 BF_ENC(r,l,s,p[12]); 173 BF_ENC(r, l,s, p[12]);
172 BF_ENC(l,r,s,p[11]); 174 BF_ENC(l, r,s, p[11]);
173 BF_ENC(r,l,s,p[10]); 175 BF_ENC(r, l,s, p[10]);
174 BF_ENC(l,r,s,p[ 9]); 176 BF_ENC(l, r,s, p[9]);
175 BF_ENC(r,l,s,p[ 8]); 177 BF_ENC(r, l,s, p[8]);
176 BF_ENC(l,r,s,p[ 7]); 178 BF_ENC(l, r,s, p[7]);
177 BF_ENC(r,l,s,p[ 6]); 179 BF_ENC(r, l,s, p[6]);
178 BF_ENC(l,r,s,p[ 5]); 180 BF_ENC(l, r,s, p[5]);
179 BF_ENC(r,l,s,p[ 4]); 181 BF_ENC(r, l,s, p[4]);
180 BF_ENC(l,r,s,p[ 3]); 182 BF_ENC(l, r,s, p[3]);
181 BF_ENC(r,l,s,p[ 2]); 183 BF_ENC(r, l,s, p[2]);
182 BF_ENC(l,r,s,p[ 1]); 184 BF_ENC(l, r,s, p[1]);
183 r^=p[0]; 185 r ^= p[0];
184 186
185 data[1]=l&0xffffffffL; 187 data[1] = l&0xffffffffL;
186 data[0]=r&0xffffffffL; 188 data[0] = r&0xffffffffL;
187#else 189#else
188 BF_LONG l,r,t,*k; 190 BF_LONG l, r,t, *k;
189 191
190 l=data[0]; 192 l = data[0];
191 r=data[1]; 193 r = data[1];
192 k=(BF_LONG *)key; 194 k = (BF_LONG *)key;
193 195
194 l^=k[BF_ROUNDS+1]; 196 l ^= k[BF_ROUNDS + 1];
195#if BF_ROUNDS == 20 197#if BF_ROUNDS == 20
196 BF_ENC(r,l,k,20); 198 BF_ENC(r, l,k, 20);
197 BF_ENC(l,r,k,19); 199 BF_ENC(l, r,k, 19);
198 BF_ENC(r,l,k,18); 200 BF_ENC(r, l,k, 18);
199 BF_ENC(l,r,k,17); 201 BF_ENC(l, r,k, 17);
200#endif 202#endif
201 BF_ENC(r,l,k,16); 203 BF_ENC(r, l,k, 16);
202 BF_ENC(l,r,k,15); 204 BF_ENC(l, r,k, 15);
203 BF_ENC(r,l,k,14); 205 BF_ENC(r, l,k, 14);
204 BF_ENC(l,r,k,13); 206 BF_ENC(l, r,k, 13);
205 BF_ENC(r,l,k,12); 207 BF_ENC(r, l,k, 12);
206 BF_ENC(l,r,k,11); 208 BF_ENC(l, r,k, 11);
207 BF_ENC(r,l,k,10); 209 BF_ENC(r, l,k, 10);
208 BF_ENC(l,r,k, 9); 210 BF_ENC(l, r, k, 9);
209 BF_ENC(r,l,k, 8); 211 BF_ENC(r, l, k, 8);
210 BF_ENC(l,r,k, 7); 212 BF_ENC(l, r, k, 7);
211 BF_ENC(r,l,k, 6); 213 BF_ENC(r, l, k, 6);
212 BF_ENC(l,r,k, 5); 214 BF_ENC(l, r, k, 5);
213 BF_ENC(r,l,k, 4); 215 BF_ENC(r, l, k, 4);
214 BF_ENC(l,r,k, 3); 216 BF_ENC(l, r, k, 3);
215 BF_ENC(r,l,k, 2); 217 BF_ENC(r, l, k, 2);
216 BF_ENC(l,r,k, 1); 218 BF_ENC(l, r, k, 1);
217 r^=k[0]; 219 r ^= k[0];
218 220
219 data[1]=l&0xffffffffL; 221 data[1] = l&0xffffffffL;
220 data[0]=r&0xffffffffL; 222 data[0] = r&0xffffffffL;
221#endif 223#endif
222 } 224}
223 225
224void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, 226void
225 const BF_KEY *schedule, unsigned char *ivec, int encrypt) 227BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
226 { 228 const BF_KEY *schedule, unsigned char *ivec, int encrypt)
227 BF_LONG tin0,tin1; 229{
228 BF_LONG tout0,tout1,xor0,xor1; 230 BF_LONG tin0, tin1;
229 long l=length; 231 BF_LONG tout0, tout1, xor0, xor1;
232 long l = length;
230 BF_LONG tin[2]; 233 BF_LONG tin[2];
231 234
232 if (encrypt) 235 if (encrypt) {
233 { 236 n2l(ivec, tout0);
234 n2l(ivec,tout0); 237 n2l(ivec, tout1);
235 n2l(ivec,tout1); 238 ivec -= 8;
236 ivec-=8; 239 for (l -= 8; l >= 0; l -= 8) {
237 for (l-=8; l>=0; l-=8) 240 n2l(in, tin0);
238 { 241 n2l(in, tin1);
239 n2l(in,tin0); 242 tin0 ^= tout0;
240 n2l(in,tin1); 243 tin1 ^= tout1;
241 tin0^=tout0; 244 tin[0] = tin0;
242 tin1^=tout1; 245 tin[1] = tin1;
243 tin[0]=tin0; 246 BF_encrypt(tin, schedule);
244 tin[1]=tin1; 247 tout0 = tin[0];
245 BF_encrypt(tin,schedule); 248 tout1 = tin[1];
246 tout0=tin[0]; 249 l2n(tout0, out);
247 tout1=tin[1]; 250 l2n(tout1, out);
248 l2n(tout0,out); 251 }
249 l2n(tout1,out); 252 if (l != -8) {
250 } 253 n2ln(in, tin0, tin1, l + 8);
251 if (l != -8) 254 tin0 ^= tout0;
252 { 255 tin1 ^= tout1;
253 n2ln(in,tin0,tin1,l+8); 256 tin[0] = tin0;
254 tin0^=tout0; 257 tin[1] = tin1;
255 tin1^=tout1; 258 BF_encrypt(tin, schedule);
256 tin[0]=tin0; 259 tout0 = tin[0];
257 tin[1]=tin1; 260 tout1 = tin[1];
258 BF_encrypt(tin,schedule); 261 l2n(tout0, out);
259 tout0=tin[0]; 262 l2n(tout1, out);
260 tout1=tin[1]; 263 }
261 l2n(tout0,out); 264 l2n(tout0, ivec);
262 l2n(tout1,out); 265 l2n(tout1, ivec);
263 } 266 } else {
264 l2n(tout0,ivec); 267 n2l(ivec, xor0);
265 l2n(tout1,ivec); 268 n2l(ivec, xor1);
269 ivec -= 8;
270 for (l -= 8; l >= 0; l -= 8) {
271 n2l(in, tin0);
272 n2l(in, tin1);
273 tin[0] = tin0;
274 tin[1] = tin1;
275 BF_decrypt(tin, schedule);
276 tout0 = tin[0]^xor0;
277 tout1 = tin[1]^xor1;
278 l2n(tout0, out);
279 l2n(tout1, out);
280 xor0 = tin0;
281 xor1 = tin1;
266 } 282 }
267 else 283 if (l != -8) {
268 { 284 n2l(in, tin0);
269 n2l(ivec,xor0); 285 n2l(in, tin1);
270 n2l(ivec,xor1); 286 tin[0] = tin0;
271 ivec-=8; 287 tin[1] = tin1;
272 for (l-=8; l>=0; l-=8) 288 BF_decrypt(tin, schedule);
273 { 289 tout0 = tin[0]^xor0;
274 n2l(in,tin0); 290 tout1 = tin[1]^xor1;
275 n2l(in,tin1); 291 l2nn(tout0, tout1, out, l + 8);
276 tin[0]=tin0; 292 xor0 = tin0;
277 tin[1]=tin1; 293 xor1 = tin1;
278 BF_decrypt(tin,schedule);
279 tout0=tin[0]^xor0;
280 tout1=tin[1]^xor1;
281 l2n(tout0,out);
282 l2n(tout1,out);
283 xor0=tin0;
284 xor1=tin1;
285 }
286 if (l != -8)
287 {
288 n2l(in,tin0);
289 n2l(in,tin1);
290 tin[0]=tin0;
291 tin[1]=tin1;
292 BF_decrypt(tin,schedule);
293 tout0=tin[0]^xor0;
294 tout1=tin[1]^xor1;
295 l2nn(tout0,tout1,out,l+8);
296 xor0=tin0;
297 xor1=tin1;
298 }
299 l2n(xor0,ivec);
300 l2n(xor1,ivec);
301 } 294 }
302 tin0=tin1=tout0=tout1=xor0=xor1=0; 295 l2n(xor0, ivec);
303 tin[0]=tin[1]=0; 296 l2n(xor1, ivec);
304 } 297 }
298 tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
299 tin[0] = tin[1] = 0;
300}
305 301
306#endif 302#endif