Home | History | Annotate | Line # | Download | only in des
des_enc.c revision 1.1
      1  1.1  tls /* crypto/des/des_enc.c */
      2  1.1  tls /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com)
      3  1.1  tls  * All rights reserved.
      4  1.1  tls  *
      5  1.1  tls  * This package is an SSL implementation written
      6  1.1  tls  * by Eric Young (eay (at) cryptsoft.com).
      7  1.1  tls  * The implementation was written so as to conform with Netscapes SSL.
      8  1.1  tls  *
      9  1.1  tls  * This library is free for commercial and non-commercial use as long as
     10  1.1  tls  * the following conditions are aheared to.  The following conditions
     11  1.1  tls  * apply to all code found in this distribution, be it the RC4, RSA,
     12  1.1  tls  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     13  1.1  tls  * included with this distribution is covered by the same copyright terms
     14  1.1  tls  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     15  1.1  tls  *
     16  1.1  tls  * Copyright remains Eric Young's, and as such any Copyright notices in
     17  1.1  tls  * the code are not to be removed.
     18  1.1  tls  * If this package is used in a product, Eric Young should be given attribution
     19  1.1  tls  * as the author of the parts of the library used.
     20  1.1  tls  * This can be in the form of a textual message at program startup or
     21  1.1  tls  * in documentation (online or textual) provided with the package.
     22  1.1  tls  *
     23  1.1  tls  * Redistribution and use in source and binary forms, with or without
     24  1.1  tls  * modification, are permitted provided that the following conditions
     25  1.1  tls  * are met:
     26  1.1  tls  * 1. Redistributions of source code must retain the copyright
     27  1.1  tls  *    notice, this list of conditions and the following disclaimer.
     28  1.1  tls  * 2. Redistributions in binary form must reproduce the above copyright
     29  1.1  tls  *    notice, this list of conditions and the following disclaimer in the
     30  1.1  tls  *    documentation and/or other materials provided with the distribution.
     31  1.1  tls  * 3. All advertising materials mentioning features or use of this software
     32  1.1  tls  *    must display the following acknowledgement:
     33  1.1  tls  *    "This product includes cryptographic software written by
     34  1.1  tls  *     Eric Young (eay (at) cryptsoft.com)"
     35  1.1  tls  *    The word 'cryptographic' can be left out if the rouines from the library
     36  1.1  tls  *    being used are not cryptographic related :-).
     37  1.1  tls  * 4. If you include any Windows specific code (or a derivative thereof) from
     38  1.1  tls  *    the apps directory (application code) you must include an acknowledgement:
     39  1.1  tls  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     40  1.1  tls  *
     41  1.1  tls  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     42  1.1  tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     43  1.1  tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     44  1.1  tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     45  1.1  tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     46  1.1  tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     47  1.1  tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     48  1.1  tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     49  1.1  tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     50  1.1  tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     51  1.1  tls  * SUCH DAMAGE.
     52  1.1  tls  *
     53  1.1  tls  * The licence and distribution terms for any publically available version or
     54  1.1  tls  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     55  1.1  tls  * copied and put under another distribution licence
     56  1.1  tls  * [including the GNU Public Licence.]
     57  1.1  tls  */
     58  1.1  tls 
     59  1.1  tls #include <sys/types.h>
     60  1.1  tls #include <crypto/des/des_locl.h>
     61  1.1  tls 
     62  1.1  tls extern	const DES_LONG des_SPtrans[8][64];
     63  1.1  tls 
     64  1.1  tls void des_encrypt1(DES_LONG *data, des_key_schedule ks, int enc)
     65  1.1  tls {
     66  1.1  tls 	register DES_LONG l,r,t,u;
     67  1.1  tls #ifdef DES_PTR
     68  1.1  tls 	register const unsigned char *des_SP=(const unsigned char *)des_SPtrans;
     69  1.1  tls #endif
     70  1.1  tls #ifndef DES_UNROLL
     71  1.1  tls 	register int i;
     72  1.1  tls #endif
     73  1.1  tls 	register DES_LONG *s;
     74  1.1  tls 
     75  1.1  tls 	r=data[0];
     76  1.1  tls 	l=data[1];
     77  1.1  tls 
     78  1.1  tls 	IP(r,l);
     79  1.1  tls 	/* Things have been modified so that the initial rotate is
     80  1.1  tls 	 * done outside the loop.  This required the
     81  1.1  tls 	 * des_SPtrans values in sp.h to be rotated 1 bit to the right.
     82  1.1  tls 	 * One perl script later and things have a 5% speed up on a sparc2.
     83  1.1  tls 	 * Thanks to Richard Outerbridge <71755.204 (at) CompuServe.COM>
     84  1.1  tls 	 * for pointing this out. */
     85  1.1  tls 	/* clear the top bits on machines with 8byte longs */
     86  1.1  tls 	/* shift left by 2 */
     87  1.1  tls 	r=ROTATE(r,29)&0xffffffffL;
     88  1.1  tls 	l=ROTATE(l,29)&0xffffffffL;
     89  1.1  tls 
     90  1.1  tls 	s=ks->ks.deslong;
     91  1.1  tls 	/* I don't know if it is worth the effort of loop unrolling the
     92  1.1  tls 	 * inner loop */
     93  1.1  tls 	if (enc)
     94  1.1  tls 		{
     95  1.1  tls #ifdef DES_UNROLL
     96  1.1  tls 		D_ENCRYPT(l,r, 0); /*  1 */
     97  1.1  tls 		D_ENCRYPT(r,l, 2); /*  2 */
     98  1.1  tls 		D_ENCRYPT(l,r, 4); /*  3 */
     99  1.1  tls 		D_ENCRYPT(r,l, 6); /*  4 */
    100  1.1  tls 		D_ENCRYPT(l,r, 8); /*  5 */
    101  1.1  tls 		D_ENCRYPT(r,l,10); /*  6 */
    102  1.1  tls 		D_ENCRYPT(l,r,12); /*  7 */
    103  1.1  tls 		D_ENCRYPT(r,l,14); /*  8 */
    104  1.1  tls 		D_ENCRYPT(l,r,16); /*  9 */
    105  1.1  tls 		D_ENCRYPT(r,l,18); /*  10 */
    106  1.1  tls 		D_ENCRYPT(l,r,20); /*  11 */
    107  1.1  tls 		D_ENCRYPT(r,l,22); /*  12 */
    108  1.1  tls 		D_ENCRYPT(l,r,24); /*  13 */
    109  1.1  tls 		D_ENCRYPT(r,l,26); /*  14 */
    110  1.1  tls 		D_ENCRYPT(l,r,28); /*  15 */
    111  1.1  tls 		D_ENCRYPT(r,l,30); /*  16 */
    112  1.1  tls #else
    113  1.1  tls 		for (i=0; i<32; i+=8)
    114  1.1  tls 			{
    115  1.1  tls 			D_ENCRYPT(l,r,i+0); /*  1 */
    116  1.1  tls 			D_ENCRYPT(r,l,i+2); /*  2 */
    117  1.1  tls 			D_ENCRYPT(l,r,i+4); /*  3 */
    118  1.1  tls 			D_ENCRYPT(r,l,i+6); /*  4 */
    119  1.1  tls 			}
    120  1.1  tls #endif
    121  1.1  tls 		}
    122  1.1  tls 	else
    123  1.1  tls 		{
    124  1.1  tls #ifdef DES_UNROLL
    125  1.1  tls 		D_ENCRYPT(l,r,30); /* 16 */
    126  1.1  tls 		D_ENCRYPT(r,l,28); /* 15 */
    127  1.1  tls 		D_ENCRYPT(l,r,26); /* 14 */
    128  1.1  tls 		D_ENCRYPT(r,l,24); /* 13 */
    129  1.1  tls 		D_ENCRYPT(l,r,22); /* 12 */
    130  1.1  tls 		D_ENCRYPT(r,l,20); /* 11 */
    131  1.1  tls 		D_ENCRYPT(l,r,18); /* 10 */
    132  1.1  tls 		D_ENCRYPT(r,l,16); /*  9 */
    133  1.1  tls 		D_ENCRYPT(l,r,14); /*  8 */
    134  1.1  tls 		D_ENCRYPT(r,l,12); /*  7 */
    135  1.1  tls 		D_ENCRYPT(l,r,10); /*  6 */
    136  1.1  tls 		D_ENCRYPT(r,l, 8); /*  5 */
    137  1.1  tls 		D_ENCRYPT(l,r, 6); /*  4 */
    138  1.1  tls 		D_ENCRYPT(r,l, 4); /*  3 */
    139  1.1  tls 		D_ENCRYPT(l,r, 2); /*  2 */
    140  1.1  tls 		D_ENCRYPT(r,l, 0); /*  1 */
    141  1.1  tls #else
    142  1.1  tls 		for (i=30; i>0; i-=8)
    143  1.1  tls 			{
    144  1.1  tls 			D_ENCRYPT(l,r,i-0); /* 16 */
    145  1.1  tls 			D_ENCRYPT(r,l,i-2); /* 15 */
    146  1.1  tls 			D_ENCRYPT(l,r,i-4); /* 14 */
    147  1.1  tls 			D_ENCRYPT(r,l,i-6); /* 13 */
    148  1.1  tls 			}
    149  1.1  tls #endif
    150  1.1  tls 		}
    151  1.1  tls 
    152  1.1  tls 	/* rotate and clear the top bits on machines with 8byte longs */
    153  1.1  tls 	l=ROTATE(l,3)&0xffffffffL;
    154  1.1  tls 	r=ROTATE(r,3)&0xffffffffL;
    155  1.1  tls 
    156  1.1  tls 	FP(r,l);
    157  1.1  tls 	data[0]=l;
    158  1.1  tls 	data[1]=r;
    159  1.1  tls 	l=r=t=u=0;
    160  1.1  tls }
    161  1.1  tls 
    162  1.1  tls void des_encrypt2(DES_LONG *data, des_key_schedule ks, int enc)
    163  1.1  tls {
    164  1.1  tls 	register DES_LONG l,r,t,u;
    165  1.1  tls #ifdef DES_PTR
    166  1.1  tls 	register const unsigned char *des_SP=(const unsigned char *)des_SPtrans;
    167  1.1  tls #endif
    168  1.1  tls #ifndef DES_UNROLL
    169  1.1  tls 	register int i;
    170  1.1  tls #endif
    171  1.1  tls 	register DES_LONG *s;
    172  1.1  tls 
    173  1.1  tls 	r=data[0];
    174  1.1  tls 	l=data[1];
    175  1.1  tls 
    176  1.1  tls 	/* Things have been modified so that the initial rotate is
    177  1.1  tls 	 * done outside the loop.  This required the
    178  1.1  tls 	 * des_SPtrans values in sp.h to be rotated 1 bit to the right.
    179  1.1  tls 	 * One perl script later and things have a 5% speed up on a sparc2.
    180  1.1  tls 	 * Thanks to Richard Outerbridge <71755.204 (at) CompuServe.COM>
    181  1.1  tls 	 * for pointing this out. */
    182  1.1  tls 	/* clear the top bits on machines with 8byte longs */
    183  1.1  tls 	r=ROTATE(r,29)&0xffffffffL;
    184  1.1  tls 	l=ROTATE(l,29)&0xffffffffL;
    185  1.1  tls 
    186  1.1  tls 	s=ks->ks.deslong;
    187  1.1  tls 	/* I don't know if it is worth the effort of loop unrolling the
    188  1.1  tls 	 * inner loop */
    189  1.1  tls 	if (enc)
    190  1.1  tls 		{
    191  1.1  tls #ifdef DES_UNROLL
    192  1.1  tls 		D_ENCRYPT(l,r, 0); /*  1 */
    193  1.1  tls 		D_ENCRYPT(r,l, 2); /*  2 */
    194  1.1  tls 		D_ENCRYPT(l,r, 4); /*  3 */
    195  1.1  tls 		D_ENCRYPT(r,l, 6); /*  4 */
    196  1.1  tls 		D_ENCRYPT(l,r, 8); /*  5 */
    197  1.1  tls 		D_ENCRYPT(r,l,10); /*  6 */
    198  1.1  tls 		D_ENCRYPT(l,r,12); /*  7 */
    199  1.1  tls 		D_ENCRYPT(r,l,14); /*  8 */
    200  1.1  tls 		D_ENCRYPT(l,r,16); /*  9 */
    201  1.1  tls 		D_ENCRYPT(r,l,18); /*  10 */
    202  1.1  tls 		D_ENCRYPT(l,r,20); /*  11 */
    203  1.1  tls 		D_ENCRYPT(r,l,22); /*  12 */
    204  1.1  tls 		D_ENCRYPT(l,r,24); /*  13 */
    205  1.1  tls 		D_ENCRYPT(r,l,26); /*  14 */
    206  1.1  tls 		D_ENCRYPT(l,r,28); /*  15 */
    207  1.1  tls 		D_ENCRYPT(r,l,30); /*  16 */
    208  1.1  tls #else
    209  1.1  tls 		for (i=0; i<32; i+=8)
    210  1.1  tls 			{
    211  1.1  tls 			D_ENCRYPT(l,r,i+0); /*  1 */
    212  1.1  tls 			D_ENCRYPT(r,l,i+2); /*  2 */
    213  1.1  tls 			D_ENCRYPT(l,r,i+4); /*  3 */
    214  1.1  tls 			D_ENCRYPT(r,l,i+6); /*  4 */
    215  1.1  tls 			}
    216  1.1  tls #endif
    217  1.1  tls 		}
    218  1.1  tls 	else
    219  1.1  tls 		{
    220  1.1  tls #ifdef DES_UNROLL
    221  1.1  tls 		D_ENCRYPT(l,r,30); /* 16 */
    222  1.1  tls 		D_ENCRYPT(r,l,28); /* 15 */
    223  1.1  tls 		D_ENCRYPT(l,r,26); /* 14 */
    224  1.1  tls 		D_ENCRYPT(r,l,24); /* 13 */
    225  1.1  tls 		D_ENCRYPT(l,r,22); /* 12 */
    226  1.1  tls 		D_ENCRYPT(r,l,20); /* 11 */
    227  1.1  tls 		D_ENCRYPT(l,r,18); /* 10 */
    228  1.1  tls 		D_ENCRYPT(r,l,16); /*  9 */
    229  1.1  tls 		D_ENCRYPT(l,r,14); /*  8 */
    230  1.1  tls 		D_ENCRYPT(r,l,12); /*  7 */
    231  1.1  tls 		D_ENCRYPT(l,r,10); /*  6 */
    232  1.1  tls 		D_ENCRYPT(r,l, 8); /*  5 */
    233  1.1  tls 		D_ENCRYPT(l,r, 6); /*  4 */
    234  1.1  tls 		D_ENCRYPT(r,l, 4); /*  3 */
    235  1.1  tls 		D_ENCRYPT(l,r, 2); /*  2 */
    236  1.1  tls 		D_ENCRYPT(r,l, 0); /*  1 */
    237  1.1  tls #else
    238  1.1  tls 		for (i=30; i>0; i-=8)
    239  1.1  tls 			{
    240  1.1  tls 			D_ENCRYPT(l,r,i-0); /* 16 */
    241  1.1  tls 			D_ENCRYPT(r,l,i-2); /* 15 */
    242  1.1  tls 			D_ENCRYPT(l,r,i-4); /* 14 */
    243  1.1  tls 			D_ENCRYPT(r,l,i-6); /* 13 */
    244  1.1  tls 			}
    245  1.1  tls #endif
    246  1.1  tls 		}
    247  1.1  tls 	/* rotate and clear the top bits on machines with 8byte longs */
    248  1.1  tls 	data[0]=ROTATE(l,3)&0xffffffffL;
    249  1.1  tls 	data[1]=ROTATE(r,3)&0xffffffffL;
    250  1.1  tls 	l=r=t=u=0;
    251  1.1  tls }
    252  1.1  tls 
    253  1.1  tls void des_encrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2,
    254  1.1  tls 	     des_key_schedule ks3)
    255  1.1  tls {
    256  1.1  tls 	register DES_LONG l,r;
    257  1.1  tls 
    258  1.1  tls 	l=data[0];
    259  1.1  tls 	r=data[1];
    260  1.1  tls 	IP(l,r);
    261  1.1  tls 	data[0]=l;
    262  1.1  tls 	data[1]=r;
    263  1.1  tls 	des_encrypt2((DES_LONG *)data,ks1,DES_ENCRYPT);
    264  1.1  tls 	des_encrypt2((DES_LONG *)data,ks2,DES_DECRYPT);
    265  1.1  tls 	des_encrypt2((DES_LONG *)data,ks3,DES_ENCRYPT);
    266  1.1  tls 	l=data[0];
    267  1.1  tls 	r=data[1];
    268  1.1  tls 	FP(r,l);
    269  1.1  tls 	data[0]=l;
    270  1.1  tls 	data[1]=r;
    271  1.1  tls }
    272  1.1  tls 
    273  1.1  tls void des_decrypt3(DES_LONG *data, des_key_schedule ks1, des_key_schedule ks2,
    274  1.1  tls 	     des_key_schedule ks3)
    275  1.1  tls {
    276  1.1  tls 	register DES_LONG l,r;
    277  1.1  tls 
    278  1.1  tls 	l=data[0];
    279  1.1  tls 	r=data[1];
    280  1.1  tls 	IP(l,r);
    281  1.1  tls 	data[0]=l;
    282  1.1  tls 	data[1]=r;
    283  1.1  tls 	des_encrypt2((DES_LONG *)data,ks3,DES_DECRYPT);
    284  1.1  tls 	des_encrypt2((DES_LONG *)data,ks2,DES_ENCRYPT);
    285  1.1  tls 	des_encrypt2((DES_LONG *)data,ks1,DES_DECRYPT);
    286  1.1  tls 	l=data[0];
    287  1.1  tls 	r=data[1];
    288  1.1  tls 	FP(r,l);
    289  1.1  tls 	data[0]=l;
    290  1.1  tls 	data[1]=r;
    291  1.1  tls }
    292