xref: /dragonfly/crypto/libressl/crypto/rc4/rc4_enc.c (revision 6f5ec8b5)
1 /* $OpenBSD: rc4_enc.c,v 1.17 2021/11/09 18:40:21 bcook Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
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
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
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.
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.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
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:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
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
51  * SUCH DAMAGE.
52  *
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
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <endian.h>
60 
61 #include <openssl/rc4.h>
62 #include "rc4_locl.h"
63 
64 /* RC4 as implemented from a posting from
65  * Newsgroups: sci.crypt
66  * From: sterndark@netcom.com (David Sterndark)
67  * Subject: RC4 Algorithm revealed.
68  * Message-ID: <sternCvKL4B.Hyy@netcom.com>
69  * Date: Wed, 14 Sep 1994 06:35:31 GMT
70  */
71 
72 void
73 RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
74     unsigned char *outdata)
75 {
76 	RC4_INT *d;
77 	RC4_INT x, y,tx, ty;
78 	size_t i;
79 
80 	x = key->x;
81 	y = key->y;
82 	d = key->data;
83 
84 #if defined(RC4_CHUNK)
85 	/*
86 	 * The original reason for implementing this(*) was the fact that
87 	 * pre-21164a Alpha CPUs don't have byte load/store instructions
88 	 * and e.g. a byte store has to be done with 64-bit load, shift,
89 	 * and, or and finally 64-bit store. Peaking data and operating
90 	 * at natural word size made it possible to reduce amount of
91 	 * instructions as well as to perform early read-ahead without
92 	 * suffering from RAW (read-after-write) hazard. This resulted
93 	 * in ~40%(**) performance improvement on 21064 box with gcc.
94 	 * But it's not only Alpha users who win here:-) Thanks to the
95 	 * early-n-wide read-ahead this implementation also exhibits
96 	 * >40% speed-up on SPARC and 20-30% on 64-bit MIPS (depending
97 	 * on sizeof(RC4_INT)).
98 	 *
99 	 * (*)	"this" means code which recognizes the case when input
100 	 *	and output pointers appear to be aligned at natural CPU
101 	 *	word boundary
102 	 * (**)	i.e. according to 'apps/openssl speed rc4' benchmark,
103 	 *	crypto/rc4/rc4speed.c exhibits almost 70% speed-up...
104 	 *
105 	 * Caveats.
106 	 *
107 	 * - RC4_CHUNK="unsigned long long" should be a #1 choice for
108 	 *   UltraSPARC. Unfortunately gcc generates very slow code
109 	 *   (2.5-3 times slower than one generated by Sun's WorkShop
110 	 *   C) and therefore gcc (at least 2.95 and earlier) should
111 	 *   always be told that RC4_CHUNK="unsigned long".
112 	 *
113 	 *					<appro@fy.chalmers.se>
114 	 */
115 
116 # define RC4_STEP	( \
117 			x=(x+1) &0xff,	\
118 			tx=d[x],	\
119 			y=(tx+y)&0xff,	\
120 			ty=d[y],	\
121 			d[y]=tx,	\
122 			d[x]=ty,	\
123 			(RC4_CHUNK)d[(tx+ty)&0xff]\
124 			)
125 
126 	if ((((size_t)indata & (sizeof(RC4_CHUNK) - 1)) |
127 	    ((size_t)outdata & (sizeof(RC4_CHUNK) - 1))) == 0 ) {
128 		RC4_CHUNK ichunk, otp;
129 
130 		/*
131 		 * I reckon we can afford to implement both endian
132 		 * cases and to decide which way to take at run-time
133 		 * because the machine code appears to be very compact
134 		 * and redundant 1-2KB is perfectly tolerable (i.e.
135 		 * in case the compiler fails to eliminate it:-). By
136 		 * suggestion from Terrel Larson <terr@terralogic.net>.
137 		 *
138 		 * Special notes.
139 		 *
140 		 * - compilers (those I've tried) don't seem to have
141 		 *   problems eliminating either the operators guarded
142 		 *   by "if (sizeof(RC4_CHUNK)==8)" or the condition
143 		 *   expressions themselves so I've got 'em to replace
144 		 *   corresponding #ifdefs from the previous version;
145 		 * - I chose to let the redundant switch cases when
146 		 *   sizeof(RC4_CHUNK)!=8 be (were also #ifdefed
147 		 *   before);
148 		 * - in case you wonder "&(sizeof(RC4_CHUNK)*8-1)" in
149 		 *   [LB]ESHFT guards against "shift is out of range"
150 		 *   warnings when sizeof(RC4_CHUNK)!=8
151 		 *
152 		 *			<appro@fy.chalmers.se>
153 		 */
154 #if BYTE_ORDER == BIG_ENDIAN
155 # define BESHFT(c)	(((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1))
156 		for (; len & (0 - sizeof(RC4_CHUNK)); len -= sizeof(RC4_CHUNK)) {
157 			ichunk  = *(RC4_CHUNK *)indata;
158 			otp = RC4_STEP << BESHFT(0);
159 			otp |= RC4_STEP << BESHFT(1);
160 			otp |= RC4_STEP << BESHFT(2);
161 			otp |= RC4_STEP << BESHFT(3);
162 			if (sizeof(RC4_CHUNK) == 8) {
163 				otp |= RC4_STEP << BESHFT(4);
164 				otp |= RC4_STEP << BESHFT(5);
165 				otp |= RC4_STEP << BESHFT(6);
166 				otp |= RC4_STEP << BESHFT(7);
167 			}
168 			*(RC4_CHUNK *)outdata = otp^ichunk;
169 			indata += sizeof(RC4_CHUNK);
170 			outdata += sizeof(RC4_CHUNK);
171 		}
172 #else
173 # define LESHFT(c)	(((c)*8)&(sizeof(RC4_CHUNK)*8-1))
174 		for (; len & (0 - sizeof(RC4_CHUNK)); len -= sizeof(RC4_CHUNK)) {
175 			ichunk = *(RC4_CHUNK *)indata;
176 			otp = RC4_STEP;
177 			otp |= RC4_STEP << 8;
178 			otp |= RC4_STEP << 16;
179 			otp |= RC4_STEP << 24;
180 			if (sizeof(RC4_CHUNK) == 8) {
181 				otp |= RC4_STEP << LESHFT(4);
182 				otp |= RC4_STEP << LESHFT(5);
183 				otp |= RC4_STEP << LESHFT(6);
184 				otp |= RC4_STEP << LESHFT(7);
185 			}
186 			*(RC4_CHUNK *)outdata = otp ^ ichunk;
187 			indata += sizeof(RC4_CHUNK);
188 			outdata += sizeof(RC4_CHUNK);
189 		}
190 #endif
191 	}
192 #endif
193 #define LOOP(in,out) \
194 		x=((x+1)&0xff); \
195 		tx=d[x]; \
196 		y=(tx+y)&0xff; \
197 		d[x]=ty=d[y]; \
198 		d[y]=tx; \
199 		(out) = d[(tx+ty)&0xff]^ (in);
200 
201 #ifndef RC4_INDEX
202 #define RC4_LOOP(a,b,i)	LOOP(*((a)++),*((b)++))
203 #else
204 #define RC4_LOOP(a,b,i)	LOOP(a[i],b[i])
205 #endif
206 
207 	i = len >> 3;
208 	if (i) {
209 		for (;;) {
210 			RC4_LOOP(indata, outdata, 0);
211 			RC4_LOOP(indata, outdata, 1);
212 			RC4_LOOP(indata, outdata, 2);
213 			RC4_LOOP(indata, outdata, 3);
214 			RC4_LOOP(indata, outdata, 4);
215 			RC4_LOOP(indata, outdata, 5);
216 			RC4_LOOP(indata, outdata, 6);
217 			RC4_LOOP(indata, outdata, 7);
218 #ifdef RC4_INDEX
219 			indata += 8;
220 			outdata += 8;
221 #endif
222 			if (--i == 0)
223 				break;
224 		}
225 	}
226 	i = len&0x07;
227 	if (i) {
228 		for (;;) {
229 			RC4_LOOP(indata, outdata, 0);
230 			if (--i == 0)
231 				break;
232 			RC4_LOOP(indata, outdata, 1);
233 			if (--i == 0)
234 				break;
235 			RC4_LOOP(indata, outdata, 2);
236 			if (--i == 0)
237 				break;
238 			RC4_LOOP(indata, outdata, 3);
239 			if (--i == 0)
240 				break;
241 			RC4_LOOP(indata, outdata, 4);
242 			if (--i == 0)
243 				break;
244 			RC4_LOOP(indata, outdata, 5);
245 			if (--i == 0)
246 				break;
247 			RC4_LOOP(indata, outdata, 6);
248 			if (--i == 0)
249 				break;
250 		}
251 	}
252 	key->x = x;
253 	key->y = y;
254 }
255