xref: /onnv-gate/usr/src/common/openssl/crypto/rc4/rc4_enc.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/rc4/rc4_enc.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <openssl/rc4.h>
600Sstevel@tonic-gate #include "rc4_locl.h"
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /* RC4 as implemented from a posting from
630Sstevel@tonic-gate  * Newsgroups: sci.crypt
640Sstevel@tonic-gate  * From: sterndark@netcom.com (David Sterndark)
650Sstevel@tonic-gate  * Subject: RC4 Algorithm revealed.
660Sstevel@tonic-gate  * Message-ID: <sternCvKL4B.Hyy@netcom.com>
670Sstevel@tonic-gate  * Date: Wed, 14 Sep 1994 06:35:31 GMT
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate 
RC4(RC4_KEY * key,unsigned long len,const unsigned char * indata,unsigned char * outdata)700Sstevel@tonic-gate void RC4(RC4_KEY *key, unsigned long len, const unsigned char *indata,
710Sstevel@tonic-gate 	     unsigned char *outdata)
720Sstevel@tonic-gate 	{
730Sstevel@tonic-gate         register RC4_INT *d;
740Sstevel@tonic-gate         register RC4_INT x,y,tx,ty;
750Sstevel@tonic-gate 	int i;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate         x=key->x;
780Sstevel@tonic-gate         y=key->y;
790Sstevel@tonic-gate         d=key->data;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #if defined(RC4_CHUNK)
820Sstevel@tonic-gate 	/*
830Sstevel@tonic-gate 	 * The original reason for implementing this(*) was the fact that
840Sstevel@tonic-gate 	 * pre-21164a Alpha CPUs don't have byte load/store instructions
850Sstevel@tonic-gate 	 * and e.g. a byte store has to be done with 64-bit load, shift,
860Sstevel@tonic-gate 	 * and, or and finally 64-bit store. Peaking data and operating
870Sstevel@tonic-gate 	 * at natural word size made it possible to reduce amount of
880Sstevel@tonic-gate 	 * instructions as well as to perform early read-ahead without
890Sstevel@tonic-gate 	 * suffering from RAW (read-after-write) hazard. This resulted
900Sstevel@tonic-gate 	 * in ~40%(**) performance improvement on 21064 box with gcc.
910Sstevel@tonic-gate 	 * But it's not only Alpha users who win here:-) Thanks to the
920Sstevel@tonic-gate 	 * early-n-wide read-ahead this implementation also exhibits
930Sstevel@tonic-gate 	 * >40% speed-up on SPARC and 20-30% on 64-bit MIPS (depending
940Sstevel@tonic-gate 	 * on sizeof(RC4_INT)).
950Sstevel@tonic-gate 	 *
960Sstevel@tonic-gate 	 * (*)	"this" means code which recognizes the case when input
970Sstevel@tonic-gate 	 *	and output pointers appear to be aligned at natural CPU
980Sstevel@tonic-gate 	 *	word boundary
990Sstevel@tonic-gate 	 * (**)	i.e. according to 'apps/openssl speed rc4' benchmark,
1000Sstevel@tonic-gate 	 *	crypto/rc4/rc4speed.c exhibits almost 70% speed-up...
1010Sstevel@tonic-gate 	 *
1020Sstevel@tonic-gate 	 * Cavets.
1030Sstevel@tonic-gate 	 *
1040Sstevel@tonic-gate 	 * - RC4_CHUNK="unsigned long long" should be a #1 choice for
1050Sstevel@tonic-gate 	 *   UltraSPARC. Unfortunately gcc generates very slow code
1060Sstevel@tonic-gate 	 *   (2.5-3 times slower than one generated by Sun's WorkShop
1070Sstevel@tonic-gate 	 *   C) and therefore gcc (at least 2.95 and earlier) should
1080Sstevel@tonic-gate 	 *   always be told that RC4_CHUNK="unsigned long".
1090Sstevel@tonic-gate 	 *
1100Sstevel@tonic-gate 	 *					<appro@fy.chalmers.se>
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate # define RC4_STEP	( \
1140Sstevel@tonic-gate 			x=(x+1) &0xff,	\
1150Sstevel@tonic-gate 			tx=d[x],	\
1160Sstevel@tonic-gate 			y=(tx+y)&0xff,	\
1170Sstevel@tonic-gate 			ty=d[y],	\
1180Sstevel@tonic-gate 			d[y]=tx,	\
1190Sstevel@tonic-gate 			d[x]=ty,	\
1200Sstevel@tonic-gate 			(RC4_CHUNK)d[(tx+ty)&0xff]\
1210Sstevel@tonic-gate 			)
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if ( ( ((unsigned long)indata  & (sizeof(RC4_CHUNK)-1)) |
1240Sstevel@tonic-gate 	       ((unsigned long)outdata & (sizeof(RC4_CHUNK)-1)) ) == 0 )
1250Sstevel@tonic-gate 		{
1260Sstevel@tonic-gate 		RC4_CHUNK ichunk,otp;
1270Sstevel@tonic-gate 		const union { long one; char little; } is_endian = {1};
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		/*
1300Sstevel@tonic-gate 		 * I reckon we can afford to implement both endian
1310Sstevel@tonic-gate 		 * cases and to decide which way to take at run-time
1320Sstevel@tonic-gate 		 * because the machine code appears to be very compact
1330Sstevel@tonic-gate 		 * and redundant 1-2KB is perfectly tolerable (i.e.
1340Sstevel@tonic-gate 		 * in case the compiler fails to eliminate it:-). By
1350Sstevel@tonic-gate 		 * suggestion from Terrel Larson <terr@terralogic.net>
1360Sstevel@tonic-gate 		 * who also stands for the is_endian union:-)
1370Sstevel@tonic-gate 		 *
1380Sstevel@tonic-gate 		 * Special notes.
1390Sstevel@tonic-gate 		 *
1400Sstevel@tonic-gate 		 * - is_endian is declared automatic as doing otherwise
1410Sstevel@tonic-gate 		 *   (declaring static) prevents gcc from eliminating
1420Sstevel@tonic-gate 		 *   the redundant code;
1430Sstevel@tonic-gate 		 * - compilers (those I've tried) don't seem to have
1440Sstevel@tonic-gate 		 *   problems eliminating either the operators guarded
1450Sstevel@tonic-gate 		 *   by "if (sizeof(RC4_CHUNK)==8)" or the condition
1460Sstevel@tonic-gate 		 *   expressions themselves so I've got 'em to replace
1470Sstevel@tonic-gate 		 *   corresponding #ifdefs from the previous version;
1480Sstevel@tonic-gate 		 * - I chose to let the redundant switch cases when
1490Sstevel@tonic-gate 		 *   sizeof(RC4_CHUNK)!=8 be (were also #ifdefed
1500Sstevel@tonic-gate 		 *   before);
1510Sstevel@tonic-gate 		 * - in case you wonder "&(sizeof(RC4_CHUNK)*8-1)" in
1520Sstevel@tonic-gate 		 *   [LB]ESHFT guards against "shift is out of range"
1530Sstevel@tonic-gate 		 *   warnings when sizeof(RC4_CHUNK)!=8
1540Sstevel@tonic-gate 		 *
1550Sstevel@tonic-gate 		 *			<appro@fy.chalmers.se>
1560Sstevel@tonic-gate 		 */
1570Sstevel@tonic-gate 		if (!is_endian.little)
1580Sstevel@tonic-gate 			{	/* BIG-ENDIAN CASE */
1590Sstevel@tonic-gate # define BESHFT(c)	(((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1))
160*2139Sjp161948 			for (;len&~(sizeof(RC4_CHUNK)-1);len-=sizeof(RC4_CHUNK))
1610Sstevel@tonic-gate 				{
1620Sstevel@tonic-gate 				ichunk  = *(RC4_CHUNK *)indata;
1630Sstevel@tonic-gate 				otp  = RC4_STEP<<BESHFT(0);
1640Sstevel@tonic-gate 				otp |= RC4_STEP<<BESHFT(1);
1650Sstevel@tonic-gate 				otp |= RC4_STEP<<BESHFT(2);
1660Sstevel@tonic-gate 				otp |= RC4_STEP<<BESHFT(3);
1670Sstevel@tonic-gate 				if (sizeof(RC4_CHUNK)==8)
1680Sstevel@tonic-gate 					{
1690Sstevel@tonic-gate 					otp |= RC4_STEP<<BESHFT(4);
1700Sstevel@tonic-gate 					otp |= RC4_STEP<<BESHFT(5);
1710Sstevel@tonic-gate 					otp |= RC4_STEP<<BESHFT(6);
1720Sstevel@tonic-gate 					otp |= RC4_STEP<<BESHFT(7);
1730Sstevel@tonic-gate 					}
1740Sstevel@tonic-gate 				*(RC4_CHUNK *)outdata = otp^ichunk;
1750Sstevel@tonic-gate 				indata  += sizeof(RC4_CHUNK);
1760Sstevel@tonic-gate 				outdata += sizeof(RC4_CHUNK);
1770Sstevel@tonic-gate 				}
1780Sstevel@tonic-gate 			if (len)
1790Sstevel@tonic-gate 				{
1800Sstevel@tonic-gate 				RC4_CHUNK mask=(RC4_CHUNK)-1, ochunk;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 				ichunk = *(RC4_CHUNK *)indata;
1830Sstevel@tonic-gate 				ochunk = *(RC4_CHUNK *)outdata;
1840Sstevel@tonic-gate 				otp = 0;
1850Sstevel@tonic-gate 				i = BESHFT(0);
1860Sstevel@tonic-gate 				mask <<= (sizeof(RC4_CHUNK)-len)<<3;
1870Sstevel@tonic-gate 				switch (len&(sizeof(RC4_CHUNK)-1))
1880Sstevel@tonic-gate 					{
1890Sstevel@tonic-gate 					case 7:	otp  = RC4_STEP<<i, i-=8;
1900Sstevel@tonic-gate 					case 6:	otp |= RC4_STEP<<i, i-=8;
1910Sstevel@tonic-gate 					case 5:	otp |= RC4_STEP<<i, i-=8;
1920Sstevel@tonic-gate 					case 4:	otp |= RC4_STEP<<i, i-=8;
1930Sstevel@tonic-gate 					case 3:	otp |= RC4_STEP<<i, i-=8;
1940Sstevel@tonic-gate 					case 2:	otp |= RC4_STEP<<i, i-=8;
1950Sstevel@tonic-gate 					case 1:	otp |= RC4_STEP<<i, i-=8;
1960Sstevel@tonic-gate 					case 0: ; /*
1970Sstevel@tonic-gate 						   * it's never the case,
1980Sstevel@tonic-gate 						   * but it has to be here
1990Sstevel@tonic-gate 						   * for ultrix?
2000Sstevel@tonic-gate 						   */
2010Sstevel@tonic-gate 					}
2020Sstevel@tonic-gate 				ochunk &= ~mask;
2030Sstevel@tonic-gate 				ochunk |= (otp^ichunk) & mask;
2040Sstevel@tonic-gate 				*(RC4_CHUNK *)outdata = ochunk;
2050Sstevel@tonic-gate 				}
2060Sstevel@tonic-gate 			key->x=x;
2070Sstevel@tonic-gate 			key->y=y;
2080Sstevel@tonic-gate 			return;
2090Sstevel@tonic-gate 			}
2100Sstevel@tonic-gate 		else
2110Sstevel@tonic-gate 			{	/* LITTLE-ENDIAN CASE */
2120Sstevel@tonic-gate # define LESHFT(c)	(((c)*8)&(sizeof(RC4_CHUNK)*8-1))
213*2139Sjp161948 			for (;len&~(sizeof(RC4_CHUNK)-1);len-=sizeof(RC4_CHUNK))
2140Sstevel@tonic-gate 				{
2150Sstevel@tonic-gate 				ichunk  = *(RC4_CHUNK *)indata;
2160Sstevel@tonic-gate 				otp  = RC4_STEP;
2170Sstevel@tonic-gate 				otp |= RC4_STEP<<8;
2180Sstevel@tonic-gate 				otp |= RC4_STEP<<16;
2190Sstevel@tonic-gate 				otp |= RC4_STEP<<24;
2200Sstevel@tonic-gate 				if (sizeof(RC4_CHUNK)==8)
2210Sstevel@tonic-gate 					{
2220Sstevel@tonic-gate 					otp |= RC4_STEP<<LESHFT(4);
2230Sstevel@tonic-gate 					otp |= RC4_STEP<<LESHFT(5);
2240Sstevel@tonic-gate 					otp |= RC4_STEP<<LESHFT(6);
2250Sstevel@tonic-gate 					otp |= RC4_STEP<<LESHFT(7);
2260Sstevel@tonic-gate 					}
2270Sstevel@tonic-gate 				*(RC4_CHUNK *)outdata = otp^ichunk;
2280Sstevel@tonic-gate 				indata  += sizeof(RC4_CHUNK);
2290Sstevel@tonic-gate 				outdata += sizeof(RC4_CHUNK);
2300Sstevel@tonic-gate 				}
2310Sstevel@tonic-gate 			if (len)
2320Sstevel@tonic-gate 				{
2330Sstevel@tonic-gate 				RC4_CHUNK mask=(RC4_CHUNK)-1, ochunk;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 				ichunk = *(RC4_CHUNK *)indata;
2360Sstevel@tonic-gate 				ochunk = *(RC4_CHUNK *)outdata;
2370Sstevel@tonic-gate 				otp = 0;
2380Sstevel@tonic-gate 				i   = 0;
2390Sstevel@tonic-gate 				mask >>= (sizeof(RC4_CHUNK)-len)<<3;
2400Sstevel@tonic-gate 				switch (len&(sizeof(RC4_CHUNK)-1))
2410Sstevel@tonic-gate 					{
2420Sstevel@tonic-gate 					case 7:	otp  = RC4_STEP,    i+=8;
2430Sstevel@tonic-gate 					case 6:	otp |= RC4_STEP<<i, i+=8;
2440Sstevel@tonic-gate 					case 5:	otp |= RC4_STEP<<i, i+=8;
2450Sstevel@tonic-gate 					case 4:	otp |= RC4_STEP<<i, i+=8;
2460Sstevel@tonic-gate 					case 3:	otp |= RC4_STEP<<i, i+=8;
2470Sstevel@tonic-gate 					case 2:	otp |= RC4_STEP<<i, i+=8;
2480Sstevel@tonic-gate 					case 1:	otp |= RC4_STEP<<i, i+=8;
2490Sstevel@tonic-gate 					case 0: ; /*
2500Sstevel@tonic-gate 						   * it's never the case,
2510Sstevel@tonic-gate 						   * but it has to be here
2520Sstevel@tonic-gate 						   * for ultrix?
2530Sstevel@tonic-gate 						   */
2540Sstevel@tonic-gate 					}
2550Sstevel@tonic-gate 				ochunk &= ~mask;
2560Sstevel@tonic-gate 				ochunk |= (otp^ichunk) & mask;
2570Sstevel@tonic-gate 				*(RC4_CHUNK *)outdata = ochunk;
2580Sstevel@tonic-gate 				}
2590Sstevel@tonic-gate 			key->x=x;
2600Sstevel@tonic-gate 			key->y=y;
2610Sstevel@tonic-gate 			return;
2620Sstevel@tonic-gate 			}
2630Sstevel@tonic-gate 		}
2640Sstevel@tonic-gate #endif
2650Sstevel@tonic-gate #define LOOP(in,out) \
2660Sstevel@tonic-gate 		x=((x+1)&0xff); \
2670Sstevel@tonic-gate 		tx=d[x]; \
2680Sstevel@tonic-gate 		y=(tx+y)&0xff; \
2690Sstevel@tonic-gate 		d[x]=ty=d[y]; \
2700Sstevel@tonic-gate 		d[y]=tx; \
2710Sstevel@tonic-gate 		(out) = d[(tx+ty)&0xff]^ (in);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate #ifndef RC4_INDEX
2740Sstevel@tonic-gate #define RC4_LOOP(a,b,i)	LOOP(*((a)++),*((b)++))
2750Sstevel@tonic-gate #else
2760Sstevel@tonic-gate #define RC4_LOOP(a,b,i)	LOOP(a[i],b[i])
2770Sstevel@tonic-gate #endif
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	i=(int)(len>>3L);
2800Sstevel@tonic-gate 	if (i)
2810Sstevel@tonic-gate 		{
2820Sstevel@tonic-gate 		for (;;)
2830Sstevel@tonic-gate 			{
2840Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,0);
2850Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,1);
2860Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,2);
2870Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,3);
2880Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,4);
2890Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,5);
2900Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,6);
2910Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,7);
2920Sstevel@tonic-gate #ifdef RC4_INDEX
2930Sstevel@tonic-gate 			indata+=8;
2940Sstevel@tonic-gate 			outdata+=8;
2950Sstevel@tonic-gate #endif
2960Sstevel@tonic-gate 			if (--i == 0) break;
2970Sstevel@tonic-gate 			}
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 	i=(int)len&0x07;
3000Sstevel@tonic-gate 	if (i)
3010Sstevel@tonic-gate 		{
3020Sstevel@tonic-gate 		for (;;)
3030Sstevel@tonic-gate 			{
3040Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,0); if (--i == 0) break;
3050Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,1); if (--i == 0) break;
3060Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,2); if (--i == 0) break;
3070Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,3); if (--i == 0) break;
3080Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,4); if (--i == 0) break;
3090Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,5); if (--i == 0) break;
3100Sstevel@tonic-gate 			RC4_LOOP(indata,outdata,6); if (--i == 0) break;
3110Sstevel@tonic-gate 			}
3120Sstevel@tonic-gate 		}
3130Sstevel@tonic-gate 	key->x=x;
3140Sstevel@tonic-gate 	key->y=y;
3150Sstevel@tonic-gate 	}
316