summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/des/des_enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/des/des_enc.c')
-rw-r--r--src/lib/libcrypto/des/des_enc.c165
1 files changed, 35 insertions, 130 deletions
diff --git a/src/lib/libcrypto/des/des_enc.c b/src/lib/libcrypto/des/des_enc.c
index e4db09299e..1c37ab96d3 100644
--- a/src/lib/libcrypto/des/des_enc.c
+++ b/src/lib/libcrypto/des/des_enc.c
@@ -58,14 +58,11 @@
58 58
59#include "des_locl.h" 59#include "des_locl.h"
60 60
61void des_encrypt(data, ks, enc) 61void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
62DES_LONG *data;
63des_key_schedule ks;
64int enc;
65 { 62 {
66 register DES_LONG l,r,t,u; 63 register DES_LONG l,r,t,u;
67#ifdef DES_PTR 64#ifdef DES_PTR
68 register unsigned char *des_SP=(unsigned char *)des_SPtrans; 65 register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
69#endif 66#endif
70#ifndef DES_UNROLL 67#ifndef DES_UNROLL
71 register int i; 68 register int i;
@@ -78,7 +75,7 @@ int enc;
78 IP(r,l); 75 IP(r,l);
79 /* Things have been modified so that the initial rotate is 76 /* Things have been modified so that the initial rotate is
80 * done outside the loop. This required the 77 * done outside the loop. This required the
81 * des_SPtrans values in sp.h to be rotated 1 bit to the right. 78 * DES_SPtrans values in sp.h to be rotated 1 bit to the right.
82 * One perl script later and things have a 5% speed up on a sparc2. 79 * One perl script later and things have a 5% speed up on a sparc2.
83 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> 80 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
84 * for pointing this out. */ 81 * for pointing this out. */
@@ -87,7 +84,7 @@ int enc;
87 r=ROTATE(r,29)&0xffffffffL; 84 r=ROTATE(r,29)&0xffffffffL;
88 l=ROTATE(l,29)&0xffffffffL; 85 l=ROTATE(l,29)&0xffffffffL;
89 86
90 s=(DES_LONG *)ks; 87 s=ks->ks->deslong;
91 /* I don't know if it is worth the effort of loop unrolling the 88 /* I don't know if it is worth the effort of loop unrolling the
92 * inner loop */ 89 * inner loop */
93 if (enc) 90 if (enc)
@@ -159,14 +156,11 @@ int enc;
159 l=r=t=u=0; 156 l=r=t=u=0;
160 } 157 }
161 158
162void des_encrypt2(data, ks, enc) 159void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
163DES_LONG *data;
164des_key_schedule ks;
165int enc;
166 { 160 {
167 register DES_LONG l,r,t,u; 161 register DES_LONG l,r,t,u;
168#ifdef DES_PTR 162#ifdef DES_PTR
169 register unsigned char *des_SP=(unsigned char *)des_SPtrans; 163 register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
170#endif 164#endif
171#ifndef DES_UNROLL 165#ifndef DES_UNROLL
172 register int i; 166 register int i;
@@ -178,7 +172,7 @@ int enc;
178 172
179 /* Things have been modified so that the initial rotate is 173 /* Things have been modified so that the initial rotate is
180 * done outside the loop. This required the 174 * done outside the loop. This required the
181 * des_SPtrans values in sp.h to be rotated 1 bit to the right. 175 * DES_SPtrans values in sp.h to be rotated 1 bit to the right.
182 * One perl script later and things have a 5% speed up on a sparc2. 176 * One perl script later and things have a 5% speed up on a sparc2.
183 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> 177 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
184 * for pointing this out. */ 178 * for pointing this out. */
@@ -186,7 +180,7 @@ int enc;
186 r=ROTATE(r,29)&0xffffffffL; 180 r=ROTATE(r,29)&0xffffffffL;
187 l=ROTATE(l,29)&0xffffffffL; 181 l=ROTATE(l,29)&0xffffffffL;
188 182
189 s=(DES_LONG *)ks; 183 s=ks->ks->deslong;
190 /* I don't know if it is worth the effort of loop unrolling the 184 /* I don't know if it is worth the effort of loop unrolling the
191 * inner loop */ 185 * inner loop */
192 if (enc) 186 if (enc)
@@ -253,11 +247,8 @@ int enc;
253 l=r=t=u=0; 247 l=r=t=u=0;
254 } 248 }
255 249
256void des_encrypt3(data,ks1,ks2,ks3) 250void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
257DES_LONG *data; 251 DES_key_schedule *ks2, DES_key_schedule *ks3)
258des_key_schedule ks1;
259des_key_schedule ks2;
260des_key_schedule ks3;
261 { 252 {
262 register DES_LONG l,r; 253 register DES_LONG l,r;
263 254
@@ -266,9 +257,9 @@ des_key_schedule ks3;
266 IP(l,r); 257 IP(l,r);
267 data[0]=l; 258 data[0]=l;
268 data[1]=r; 259 data[1]=r;
269 des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT); 260 DES_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
270 des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT); 261 DES_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
271 des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT); 262 DES_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
272 l=data[0]; 263 l=data[0];
273 r=data[1]; 264 r=data[1];
274 FP(r,l); 265 FP(r,l);
@@ -276,11 +267,8 @@ des_key_schedule ks3;
276 data[1]=r; 267 data[1]=r;
277 } 268 }
278 269
279void des_decrypt3(data,ks1,ks2,ks3) 270void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
280DES_LONG *data; 271 DES_key_schedule *ks2, DES_key_schedule *ks3)
281des_key_schedule ks1;
282des_key_schedule ks2;
283des_key_schedule ks3;
284 { 272 {
285 register DES_LONG l,r; 273 register DES_LONG l,r;
286 274
@@ -289,9 +277,9 @@ des_key_schedule ks3;
289 IP(l,r); 277 IP(l,r);
290 data[0]=l; 278 data[0]=l;
291 data[1]=r; 279 data[1]=r;
292 des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT); 280 DES_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
293 des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT); 281 DES_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
294 des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT); 282 DES_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
295 l=data[0]; 283 l=data[0];
296 r=data[1]; 284 r=data[1];
297 FP(r,l); 285 FP(r,l);
@@ -301,108 +289,25 @@ des_key_schedule ks3;
301 289
302#ifndef DES_DEFAULT_OPTIONS 290#ifndef DES_DEFAULT_OPTIONS
303 291
304void des_ncbc_encrypt(input, output, length, schedule, ivec, enc) 292#undef CBC_ENC_C__DONT_UPDATE_IV
305des_cblock (*input); 293#include "ncbc_enc.c" /* DES_ncbc_encrypt */
306des_cblock (*output);
307long length;
308des_key_schedule schedule;
309des_cblock (*ivec);
310int enc;
311 {
312 register DES_LONG tin0,tin1;
313 register DES_LONG tout0,tout1,xor0,xor1;
314 register unsigned char *in,*out;
315 register long l=length;
316 DES_LONG tin[2];
317 unsigned char *iv;
318
319 in=(unsigned char *)input;
320 out=(unsigned char *)output;
321 iv=(unsigned char *)ivec;
322
323 if (enc)
324 {
325 c2l(iv,tout0);
326 c2l(iv,tout1);
327 for (l-=8; l>=0; l-=8)
328 {
329 c2l(in,tin0);
330 c2l(in,tin1);
331 tin0^=tout0; tin[0]=tin0;
332 tin1^=tout1; tin[1]=tin1;
333 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
334 tout0=tin[0]; l2c(tout0,out);
335 tout1=tin[1]; l2c(tout1,out);
336 }
337 if (l != -8)
338 {
339 c2ln(in,tin0,tin1,l+8);
340 tin0^=tout0; tin[0]=tin0;
341 tin1^=tout1; tin[1]=tin1;
342 des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT);
343 tout0=tin[0]; l2c(tout0,out);
344 tout1=tin[1]; l2c(tout1,out);
345 }
346 iv=(unsigned char *)ivec;
347 l2c(tout0,iv);
348 l2c(tout1,iv);
349 }
350 else
351 {
352 c2l(iv,xor0);
353 c2l(iv,xor1);
354 for (l-=8; l>=0; l-=8)
355 {
356 c2l(in,tin0); tin[0]=tin0;
357 c2l(in,tin1); tin[1]=tin1;
358 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
359 tout0=tin[0]^xor0;
360 tout1=tin[1]^xor1;
361 l2c(tout0,out);
362 l2c(tout1,out);
363 xor0=tin0;
364 xor1=tin1;
365 }
366 if (l != -8)
367 {
368 c2l(in,tin0); tin[0]=tin0;
369 c2l(in,tin1); tin[1]=tin1;
370 des_encrypt((DES_LONG *)tin,schedule,DES_DECRYPT);
371 tout0=tin[0]^xor0;
372 tout1=tin[1]^xor1;
373 l2cn(tout0,tout1,out,l+8);
374 xor0=tin0;
375 xor1=tin1;
376 }
377
378 iv=(unsigned char *)ivec;
379 l2c(xor0,iv);
380 l2c(xor1,iv);
381 }
382 tin0=tin1=tout0=tout1=xor0=xor1=0;
383 tin[0]=tin[1]=0;
384 }
385 294
386void des_ede3_cbc_encrypt(input, output, length, ks1, ks2, ks3, ivec, enc) 295void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
387des_cblock (*input); 296 long length, DES_key_schedule *ks1,
388des_cblock (*output); 297 DES_key_schedule *ks2, DES_key_schedule *ks3,
389long length; 298 DES_cblock *ivec, int enc)
390des_key_schedule ks1;
391des_key_schedule ks2;
392des_key_schedule ks3;
393des_cblock (*ivec);
394int enc;
395 { 299 {
396 register DES_LONG tin0,tin1; 300 register DES_LONG tin0,tin1;
397 register DES_LONG tout0,tout1,xor0,xor1; 301 register DES_LONG tout0,tout1,xor0,xor1;
398 register unsigned char *in,*out; 302 register const unsigned char *in;
303 unsigned char *out;
399 register long l=length; 304 register long l=length;
400 DES_LONG tin[2]; 305 DES_LONG tin[2];
401 unsigned char *iv; 306 unsigned char *iv;
402 307
403 in=(unsigned char *)input; 308 in=input;
404 out=(unsigned char *)output; 309 out=output;
405 iv=(unsigned char *)ivec; 310 iv = &(*ivec)[0];
406 311
407 if (enc) 312 if (enc)
408 { 313 {
@@ -417,7 +322,7 @@ int enc;
417 322
418 tin[0]=tin0; 323 tin[0]=tin0;
419 tin[1]=tin1; 324 tin[1]=tin1;
420 des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3); 325 DES_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
421 tout0=tin[0]; 326 tout0=tin[0];
422 tout1=tin[1]; 327 tout1=tin[1];
423 328
@@ -432,14 +337,14 @@ int enc;
432 337
433 tin[0]=tin0; 338 tin[0]=tin0;
434 tin[1]=tin1; 339 tin[1]=tin1;
435 des_encrypt3((DES_LONG *)tin,ks1,ks2,ks3); 340 DES_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
436 tout0=tin[0]; 341 tout0=tin[0];
437 tout1=tin[1]; 342 tout1=tin[1];
438 343
439 l2c(tout0,out); 344 l2c(tout0,out);
440 l2c(tout1,out); 345 l2c(tout1,out);
441 } 346 }
442 iv=(unsigned char *)ivec; 347 iv = &(*ivec)[0];
443 l2c(tout0,iv); 348 l2c(tout0,iv);
444 l2c(tout1,iv); 349 l2c(tout1,iv);
445 } 350 }
@@ -459,7 +364,7 @@ int enc;
459 364
460 tin[0]=tin0; 365 tin[0]=tin0;
461 tin[1]=tin1; 366 tin[1]=tin1;
462 des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3); 367 DES_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
463 tout0=tin[0]; 368 tout0=tin[0];
464 tout1=tin[1]; 369 tout1=tin[1];
465 370
@@ -480,7 +385,7 @@ int enc;
480 385
481 tin[0]=tin0; 386 tin[0]=tin0;
482 tin[1]=tin1; 387 tin[1]=tin1;
483 des_decrypt3((DES_LONG *)tin,ks1,ks2,ks3); 388 DES_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
484 tout0=tin[0]; 389 tout0=tin[0];
485 tout1=tin[1]; 390 tout1=tin[1];
486 391
@@ -491,7 +396,7 @@ int enc;
491 xor1=t1; 396 xor1=t1;
492 } 397 }
493 398
494 iv=(unsigned char *)ivec; 399 iv = &(*ivec)[0];
495 l2c(xor0,iv); 400 l2c(xor0,iv);
496 l2c(xor1,iv); 401 l2c(xor1,iv);
497 } 402 }