os/ossrv/ssl/libcrypto/src/crypto/des/des_enc.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/des/des_enc.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
#include "des_locl.h"
sl@0
    60
sl@0
    61
EXPORT_C void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
sl@0
    62
	{
sl@0
    63
	register DES_LONG l,r,t,u;
sl@0
    64
#ifdef DES_PTR
sl@0
    65
	register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
sl@0
    66
#endif
sl@0
    67
#ifndef DES_UNROLL
sl@0
    68
	register int i;
sl@0
    69
#endif
sl@0
    70
	register DES_LONG *s;
sl@0
    71
sl@0
    72
	r=data[0];
sl@0
    73
	l=data[1];
sl@0
    74
sl@0
    75
	IP(r,l);
sl@0
    76
	/* Things have been modified so that the initial rotate is
sl@0
    77
	 * done outside the loop.  This required the
sl@0
    78
	 * DES_SPtrans values in sp.h to be rotated 1 bit to the right.
sl@0
    79
	 * One perl script later and things have a 5% speed up on a sparc2.
sl@0
    80
	 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
sl@0
    81
	 * for pointing this out. */
sl@0
    82
	/* clear the top bits on machines with 8byte longs */
sl@0
    83
	/* shift left by 2 */
sl@0
    84
	r=ROTATE(r,29)&0xffffffffL;
sl@0
    85
	l=ROTATE(l,29)&0xffffffffL;
sl@0
    86
sl@0
    87
	s=ks->ks->deslong;
sl@0
    88
	/* I don't know if it is worth the effort of loop unrolling the
sl@0
    89
	 * inner loop */
sl@0
    90
	if (enc)
sl@0
    91
		{
sl@0
    92
#ifdef DES_UNROLL
sl@0
    93
		D_ENCRYPT(l,r, 0); /*  1 */
sl@0
    94
		D_ENCRYPT(r,l, 2); /*  2 */
sl@0
    95
		D_ENCRYPT(l,r, 4); /*  3 */
sl@0
    96
		D_ENCRYPT(r,l, 6); /*  4 */
sl@0
    97
		D_ENCRYPT(l,r, 8); /*  5 */
sl@0
    98
		D_ENCRYPT(r,l,10); /*  6 */
sl@0
    99
		D_ENCRYPT(l,r,12); /*  7 */
sl@0
   100
		D_ENCRYPT(r,l,14); /*  8 */
sl@0
   101
		D_ENCRYPT(l,r,16); /*  9 */
sl@0
   102
		D_ENCRYPT(r,l,18); /*  10 */
sl@0
   103
		D_ENCRYPT(l,r,20); /*  11 */
sl@0
   104
		D_ENCRYPT(r,l,22); /*  12 */
sl@0
   105
		D_ENCRYPT(l,r,24); /*  13 */
sl@0
   106
		D_ENCRYPT(r,l,26); /*  14 */
sl@0
   107
		D_ENCRYPT(l,r,28); /*  15 */
sl@0
   108
		D_ENCRYPT(r,l,30); /*  16 */
sl@0
   109
#else
sl@0
   110
		for (i=0; i<32; i+=8)
sl@0
   111
			{
sl@0
   112
			D_ENCRYPT(l,r,i+0); /*  1 */
sl@0
   113
			D_ENCRYPT(r,l,i+2); /*  2 */
sl@0
   114
			D_ENCRYPT(l,r,i+4); /*  3 */
sl@0
   115
			D_ENCRYPT(r,l,i+6); /*  4 */
sl@0
   116
			}
sl@0
   117
#endif
sl@0
   118
		}
sl@0
   119
	else
sl@0
   120
		{
sl@0
   121
#ifdef DES_UNROLL
sl@0
   122
		D_ENCRYPT(l,r,30); /* 16 */
sl@0
   123
		D_ENCRYPT(r,l,28); /* 15 */
sl@0
   124
		D_ENCRYPT(l,r,26); /* 14 */
sl@0
   125
		D_ENCRYPT(r,l,24); /* 13 */
sl@0
   126
		D_ENCRYPT(l,r,22); /* 12 */
sl@0
   127
		D_ENCRYPT(r,l,20); /* 11 */
sl@0
   128
		D_ENCRYPT(l,r,18); /* 10 */
sl@0
   129
		D_ENCRYPT(r,l,16); /*  9 */
sl@0
   130
		D_ENCRYPT(l,r,14); /*  8 */
sl@0
   131
		D_ENCRYPT(r,l,12); /*  7 */
sl@0
   132
		D_ENCRYPT(l,r,10); /*  6 */
sl@0
   133
		D_ENCRYPT(r,l, 8); /*  5 */
sl@0
   134
		D_ENCRYPT(l,r, 6); /*  4 */
sl@0
   135
		D_ENCRYPT(r,l, 4); /*  3 */
sl@0
   136
		D_ENCRYPT(l,r, 2); /*  2 */
sl@0
   137
		D_ENCRYPT(r,l, 0); /*  1 */
sl@0
   138
#else
sl@0
   139
		for (i=30; i>0; i-=8)
sl@0
   140
			{
sl@0
   141
			D_ENCRYPT(l,r,i-0); /* 16 */
sl@0
   142
			D_ENCRYPT(r,l,i-2); /* 15 */
sl@0
   143
			D_ENCRYPT(l,r,i-4); /* 14 */
sl@0
   144
			D_ENCRYPT(r,l,i-6); /* 13 */
sl@0
   145
			}
sl@0
   146
#endif
sl@0
   147
		}
sl@0
   148
sl@0
   149
	/* rotate and clear the top bits on machines with 8byte longs */
sl@0
   150
	l=ROTATE(l,3)&0xffffffffL;
sl@0
   151
	r=ROTATE(r,3)&0xffffffffL;
sl@0
   152
sl@0
   153
	FP(r,l);
sl@0
   154
	data[0]=l;
sl@0
   155
	data[1]=r;
sl@0
   156
	l=r=t=u=0;
sl@0
   157
	}
sl@0
   158
sl@0
   159
EXPORT_C void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
sl@0
   160
	{
sl@0
   161
	register DES_LONG l,r,t,u;
sl@0
   162
#ifdef DES_PTR
sl@0
   163
	register const unsigned char *des_SP=(const unsigned char *)DES_SPtrans;
sl@0
   164
#endif
sl@0
   165
#ifndef DES_UNROLL
sl@0
   166
	register int i;
sl@0
   167
#endif
sl@0
   168
	register DES_LONG *s;
sl@0
   169
sl@0
   170
	r=data[0];
sl@0
   171
	l=data[1];
sl@0
   172
sl@0
   173
	/* Things have been modified so that the initial rotate is
sl@0
   174
	 * done outside the loop.  This required the
sl@0
   175
	 * DES_SPtrans values in sp.h to be rotated 1 bit to the right.
sl@0
   176
	 * One perl script later and things have a 5% speed up on a sparc2.
sl@0
   177
	 * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
sl@0
   178
	 * for pointing this out. */
sl@0
   179
	/* clear the top bits on machines with 8byte longs */
sl@0
   180
	r=ROTATE(r,29)&0xffffffffL;
sl@0
   181
	l=ROTATE(l,29)&0xffffffffL;
sl@0
   182
sl@0
   183
	s=ks->ks->deslong;
sl@0
   184
	/* I don't know if it is worth the effort of loop unrolling the
sl@0
   185
	 * inner loop */
sl@0
   186
	if (enc)
sl@0
   187
		{
sl@0
   188
#ifdef DES_UNROLL
sl@0
   189
		D_ENCRYPT(l,r, 0); /*  1 */
sl@0
   190
		D_ENCRYPT(r,l, 2); /*  2 */
sl@0
   191
		D_ENCRYPT(l,r, 4); /*  3 */
sl@0
   192
		D_ENCRYPT(r,l, 6); /*  4 */
sl@0
   193
		D_ENCRYPT(l,r, 8); /*  5 */
sl@0
   194
		D_ENCRYPT(r,l,10); /*  6 */
sl@0
   195
		D_ENCRYPT(l,r,12); /*  7 */
sl@0
   196
		D_ENCRYPT(r,l,14); /*  8 */
sl@0
   197
		D_ENCRYPT(l,r,16); /*  9 */
sl@0
   198
		D_ENCRYPT(r,l,18); /*  10 */
sl@0
   199
		D_ENCRYPT(l,r,20); /*  11 */
sl@0
   200
		D_ENCRYPT(r,l,22); /*  12 */
sl@0
   201
		D_ENCRYPT(l,r,24); /*  13 */
sl@0
   202
		D_ENCRYPT(r,l,26); /*  14 */
sl@0
   203
		D_ENCRYPT(l,r,28); /*  15 */
sl@0
   204
		D_ENCRYPT(r,l,30); /*  16 */
sl@0
   205
#else
sl@0
   206
		for (i=0; i<32; i+=8)
sl@0
   207
			{
sl@0
   208
			D_ENCRYPT(l,r,i+0); /*  1 */
sl@0
   209
			D_ENCRYPT(r,l,i+2); /*  2 */
sl@0
   210
			D_ENCRYPT(l,r,i+4); /*  3 */
sl@0
   211
			D_ENCRYPT(r,l,i+6); /*  4 */
sl@0
   212
			}
sl@0
   213
#endif
sl@0
   214
		}
sl@0
   215
	else
sl@0
   216
		{
sl@0
   217
#ifdef DES_UNROLL
sl@0
   218
		D_ENCRYPT(l,r,30); /* 16 */
sl@0
   219
		D_ENCRYPT(r,l,28); /* 15 */
sl@0
   220
		D_ENCRYPT(l,r,26); /* 14 */
sl@0
   221
		D_ENCRYPT(r,l,24); /* 13 */
sl@0
   222
		D_ENCRYPT(l,r,22); /* 12 */
sl@0
   223
		D_ENCRYPT(r,l,20); /* 11 */
sl@0
   224
		D_ENCRYPT(l,r,18); /* 10 */
sl@0
   225
		D_ENCRYPT(r,l,16); /*  9 */
sl@0
   226
		D_ENCRYPT(l,r,14); /*  8 */
sl@0
   227
		D_ENCRYPT(r,l,12); /*  7 */
sl@0
   228
		D_ENCRYPT(l,r,10); /*  6 */
sl@0
   229
		D_ENCRYPT(r,l, 8); /*  5 */
sl@0
   230
		D_ENCRYPT(l,r, 6); /*  4 */
sl@0
   231
		D_ENCRYPT(r,l, 4); /*  3 */
sl@0
   232
		D_ENCRYPT(l,r, 2); /*  2 */
sl@0
   233
		D_ENCRYPT(r,l, 0); /*  1 */
sl@0
   234
#else
sl@0
   235
		for (i=30; i>0; i-=8)
sl@0
   236
			{
sl@0
   237
			D_ENCRYPT(l,r,i-0); /* 16 */
sl@0
   238
			D_ENCRYPT(r,l,i-2); /* 15 */
sl@0
   239
			D_ENCRYPT(l,r,i-4); /* 14 */
sl@0
   240
			D_ENCRYPT(r,l,i-6); /* 13 */
sl@0
   241
			}
sl@0
   242
#endif
sl@0
   243
		}
sl@0
   244
	/* rotate and clear the top bits on machines with 8byte longs */
sl@0
   245
	data[0]=ROTATE(l,3)&0xffffffffL;
sl@0
   246
	data[1]=ROTATE(r,3)&0xffffffffL;
sl@0
   247
	l=r=t=u=0;
sl@0
   248
	}
sl@0
   249
sl@0
   250
EXPORT_C void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
sl@0
   251
		  DES_key_schedule *ks2, DES_key_schedule *ks3)
sl@0
   252
	{
sl@0
   253
	register DES_LONG l,r;
sl@0
   254
sl@0
   255
	l=data[0];
sl@0
   256
	r=data[1];
sl@0
   257
	IP(l,r);
sl@0
   258
	data[0]=l;
sl@0
   259
	data[1]=r;
sl@0
   260
	DES_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
sl@0
   261
	DES_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
sl@0
   262
	DES_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
sl@0
   263
	l=data[0];
sl@0
   264
	r=data[1];
sl@0
   265
	FP(r,l);
sl@0
   266
	data[0]=l;
sl@0
   267
	data[1]=r;
sl@0
   268
	}
sl@0
   269
sl@0
   270
EXPORT_C void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
sl@0
   271
		  DES_key_schedule *ks2, DES_key_schedule *ks3)
sl@0
   272
	{
sl@0
   273
	register DES_LONG l,r;
sl@0
   274
sl@0
   275
	l=data[0];
sl@0
   276
	r=data[1];
sl@0
   277
	IP(l,r);
sl@0
   278
	data[0]=l;
sl@0
   279
	data[1]=r;
sl@0
   280
	DES_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
sl@0
   281
	DES_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
sl@0
   282
	DES_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
sl@0
   283
	l=data[0];
sl@0
   284
	r=data[1];
sl@0
   285
	FP(r,l);
sl@0
   286
	data[0]=l;
sl@0
   287
	data[1]=r;
sl@0
   288
	}
sl@0
   289
sl@0
   290
#ifndef DES_DEFAULT_OPTIONS
sl@0
   291
sl@0
   292
#undef CBC_ENC_C__DONT_UPDATE_IV
sl@0
   293
#include "ncbc_enc.c" /* DES_ncbc_encrypt */
sl@0
   294
sl@0
   295
EXPORT_C void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
sl@0
   296
			  long length, DES_key_schedule *ks1,
sl@0
   297
			  DES_key_schedule *ks2, DES_key_schedule *ks3,
sl@0
   298
			  DES_cblock *ivec, int enc)
sl@0
   299
	{
sl@0
   300
	register DES_LONG tin0,tin1;
sl@0
   301
	register DES_LONG tout0,tout1,xor0,xor1;
sl@0
   302
	register const unsigned char *in;
sl@0
   303
	unsigned char *out;
sl@0
   304
	register long l=length;
sl@0
   305
	DES_LONG tin[2];
sl@0
   306
	unsigned char *iv;
sl@0
   307
sl@0
   308
	in=input;
sl@0
   309
	out=output;
sl@0
   310
	iv = &(*ivec)[0];
sl@0
   311
sl@0
   312
	if (enc)
sl@0
   313
		{
sl@0
   314
		c2l(iv,tout0);
sl@0
   315
		c2l(iv,tout1);
sl@0
   316
		for (l-=8; l>=0; l-=8)
sl@0
   317
			{
sl@0
   318
			c2l(in,tin0);
sl@0
   319
			c2l(in,tin1);
sl@0
   320
			tin0^=tout0;
sl@0
   321
			tin1^=tout1;
sl@0
   322
sl@0
   323
			tin[0]=tin0;
sl@0
   324
			tin[1]=tin1;
sl@0
   325
			DES_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
sl@0
   326
			tout0=tin[0];
sl@0
   327
			tout1=tin[1];
sl@0
   328
sl@0
   329
			l2c(tout0,out);
sl@0
   330
			l2c(tout1,out);
sl@0
   331
			}
sl@0
   332
		if (l != -8)
sl@0
   333
			{
sl@0
   334
			c2ln(in,tin0,tin1,l+8);
sl@0
   335
			tin0^=tout0;
sl@0
   336
			tin1^=tout1;
sl@0
   337
sl@0
   338
			tin[0]=tin0;
sl@0
   339
			tin[1]=tin1;
sl@0
   340
			DES_encrypt3((DES_LONG *)tin,ks1,ks2,ks3);
sl@0
   341
			tout0=tin[0];
sl@0
   342
			tout1=tin[1];
sl@0
   343
sl@0
   344
			l2c(tout0,out);
sl@0
   345
			l2c(tout1,out);
sl@0
   346
			}
sl@0
   347
		iv = &(*ivec)[0];
sl@0
   348
		l2c(tout0,iv);
sl@0
   349
		l2c(tout1,iv);
sl@0
   350
		}
sl@0
   351
	else
sl@0
   352
		{
sl@0
   353
		register DES_LONG t0,t1;
sl@0
   354
sl@0
   355
		c2l(iv,xor0);
sl@0
   356
		c2l(iv,xor1);
sl@0
   357
		for (l-=8; l>=0; l-=8)
sl@0
   358
			{
sl@0
   359
			c2l(in,tin0);
sl@0
   360
			c2l(in,tin1);
sl@0
   361
sl@0
   362
			t0=tin0;
sl@0
   363
			t1=tin1;
sl@0
   364
sl@0
   365
			tin[0]=tin0;
sl@0
   366
			tin[1]=tin1;
sl@0
   367
			DES_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
sl@0
   368
			tout0=tin[0];
sl@0
   369
			tout1=tin[1];
sl@0
   370
sl@0
   371
			tout0^=xor0;
sl@0
   372
			tout1^=xor1;
sl@0
   373
			l2c(tout0,out);
sl@0
   374
			l2c(tout1,out);
sl@0
   375
			xor0=t0;
sl@0
   376
			xor1=t1;
sl@0
   377
			}
sl@0
   378
		if (l != -8)
sl@0
   379
			{
sl@0
   380
			c2l(in,tin0);
sl@0
   381
			c2l(in,tin1);
sl@0
   382
			
sl@0
   383
			t0=tin0;
sl@0
   384
			t1=tin1;
sl@0
   385
sl@0
   386
			tin[0]=tin0;
sl@0
   387
			tin[1]=tin1;
sl@0
   388
			DES_decrypt3((DES_LONG *)tin,ks1,ks2,ks3);
sl@0
   389
			tout0=tin[0];
sl@0
   390
			tout1=tin[1];
sl@0
   391
		
sl@0
   392
			tout0^=xor0;
sl@0
   393
			tout1^=xor1;
sl@0
   394
			l2cn(tout0,tout1,out,l+8);
sl@0
   395
			xor0=t0;
sl@0
   396
			xor1=t1;
sl@0
   397
			}
sl@0
   398
sl@0
   399
		iv = &(*ivec)[0];
sl@0
   400
		l2c(xor0,iv);
sl@0
   401
		l2c(xor1,iv);
sl@0
   402
		}
sl@0
   403
	tin0=tin1=tout0=tout1=xor0=xor1=0;
sl@0
   404
	tin[0]=tin[1]=0;
sl@0
   405
	}
sl@0
   406
sl@0
   407
#endif /* DES_DEFAULT_OPTIONS */