1 |
1626 |
jcastillo |
/* lib/rc4/rc4_enc.c */
|
2 |
|
|
/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* This file is part of an SSL implementation written
|
6 |
|
|
* by Eric Young (eay@mincom.oz.au).
|
7 |
|
|
* The implementation was written so as to conform with Netscapes SSL
|
8 |
|
|
* specification. This library and applications are
|
9 |
|
|
* FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
|
10 |
|
|
* as long as the following conditions are aheared to.
|
11 |
|
|
*
|
12 |
|
|
* Copyright remains Eric Young's, and as such any Copyright notices in
|
13 |
|
|
* the code are not to be removed. If this code is used in a product,
|
14 |
|
|
* Eric Young should be given attribution as the author of the parts used.
|
15 |
|
|
* This can be in the form of a textual message at program startup or
|
16 |
|
|
* in documentation (online or textual) provided with the package.
|
17 |
|
|
*
|
18 |
|
|
* Redistribution and use in source and binary forms, with or without
|
19 |
|
|
* modification, are permitted provided that the following conditions
|
20 |
|
|
* are met:
|
21 |
|
|
* 1. Redistributions of source code must retain the copyright
|
22 |
|
|
* notice, this list of conditions and the following disclaimer.
|
23 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
24 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
25 |
|
|
* documentation and/or other materials provided with the distribution.
|
26 |
|
|
* 3. All advertising materials mentioning features or use of this software
|
27 |
|
|
* must display the following acknowledgement:
|
28 |
|
|
* This product includes software developed by Eric Young (eay@mincom.oz.au)
|
29 |
|
|
*
|
30 |
|
|
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
31 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
32 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
33 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
34 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
35 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
36 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
37 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
38 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
39 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
40 |
|
|
* SUCH DAMAGE.
|
41 |
|
|
*
|
42 |
|
|
* The licence and distribution terms for any publically available version or
|
43 |
|
|
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
44 |
|
|
* copied and put under another distribution licence
|
45 |
|
|
* [including the GNU Public Licence.]
|
46 |
|
|
*/
|
47 |
|
|
|
48 |
|
|
/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
|
49 |
|
|
*
|
50 |
|
|
* Always modify rc2_enc.org since rc2_enc.c is automatically generated from
|
51 |
|
|
* it during SSLeay configuration.
|
52 |
|
|
* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
|
53 |
|
|
*/
|
54 |
|
|
|
55 |
|
|
#include "rc4.h"
|
56 |
|
|
|
57 |
|
|
/* if this is defined data[i] is used instead of *data, this is a %20
|
58 |
|
|
* speedup on x86 */
|
59 |
|
|
#undef RC4_INDEX
|
60 |
|
|
|
61 |
|
|
char *RC4_version="RC4 part of SSLeay 0.6.6a 24-Jun-1998";
|
62 |
|
|
|
63 |
|
|
char *RC4_options()
|
64 |
|
|
{
|
65 |
|
|
#ifdef RC4_INDEX
|
66 |
|
|
if (sizeof(RC4_INT) == 1)
|
67 |
|
|
return("rc4(idx,char)");
|
68 |
|
|
else
|
69 |
|
|
return("rc4(idx,int)");
|
70 |
|
|
#else
|
71 |
|
|
if (sizeof(RC4_INT) == 1)
|
72 |
|
|
return("rc4(ptr,char)");
|
73 |
|
|
else
|
74 |
|
|
return("rc4(ptr,int)");
|
75 |
|
|
#endif
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
/* RC4 as implemented from a posting from
|
79 |
|
|
* Newsgroups: sci.crypt
|
80 |
|
|
* From: sterndark@netcom.com (David Sterndark)
|
81 |
|
|
* Subject: RC4 Algorithm revealed.
|
82 |
|
|
* Message-ID: <sternCvKL4B.Hyy@netcom.com>
|
83 |
|
|
* Date: Wed, 14 Sep 1994 06:35:31 GMT
|
84 |
|
|
*/
|
85 |
|
|
|
86 |
|
|
void RC4_set_key(key, len, data)
|
87 |
|
|
RC4_KEY *key;
|
88 |
|
|
int len;
|
89 |
|
|
register unsigned char *data;
|
90 |
|
|
{
|
91 |
|
|
register RC4_INT tmp;
|
92 |
|
|
register int id1,id2;
|
93 |
|
|
register RC4_INT *d;
|
94 |
|
|
unsigned int i;
|
95 |
|
|
|
96 |
|
|
d= &(key->data[0]);
|
97 |
|
|
for (i=0; i<256; i++)
|
98 |
|
|
d[i]=i;
|
99 |
|
|
key->x = 0;
|
100 |
|
|
key->y = 0;
|
101 |
|
|
id1=id2=0;
|
102 |
|
|
|
103 |
|
|
#define SK_LOOP(n) { \
|
104 |
|
|
tmp=d[(n)]; \
|
105 |
|
|
id2 = (data[id1] + tmp + id2) & 0xff; \
|
106 |
|
|
if (++id1 == len) id1=0; \
|
107 |
|
|
d[(n)]=d[id2]; \
|
108 |
|
|
d[id2]=tmp; }
|
109 |
|
|
|
110 |
|
|
for (i=0; i < 256; i+=4)
|
111 |
|
|
{
|
112 |
|
|
SK_LOOP(i+0);
|
113 |
|
|
SK_LOOP(i+1);
|
114 |
|
|
SK_LOOP(i+2);
|
115 |
|
|
SK_LOOP(i+3);
|
116 |
|
|
}
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
void RC4(key, len, indata, outdata)
|
120 |
|
|
RC4_KEY *key;
|
121 |
|
|
unsigned long len;
|
122 |
|
|
unsigned char *indata;
|
123 |
|
|
unsigned char *outdata;
|
124 |
|
|
{
|
125 |
|
|
register RC4_INT *d;
|
126 |
|
|
register RC4_INT x,y,tx;
|
127 |
|
|
int i;
|
128 |
|
|
|
129 |
|
|
x=key->x;
|
130 |
|
|
y=key->y;
|
131 |
|
|
d=key->data;
|
132 |
|
|
|
133 |
|
|
#define LOOP(in,out) \
|
134 |
|
|
x=((x+1)&0xff); \
|
135 |
|
|
tx=d[x]; \
|
136 |
|
|
y=(tx+y)&0xff; \
|
137 |
|
|
d[x]=d[y]; \
|
138 |
|
|
d[y]=tx; \
|
139 |
|
|
(out) = d[(tx+d[x])&0xff]^ (in);
|
140 |
|
|
|
141 |
|
|
#ifndef RC4_INDEX
|
142 |
|
|
#define RC4_LOOP(a,b) LOOP(*((a)++),*((b)++))
|
143 |
|
|
#else
|
144 |
|
|
#define RC4_LOOP(a,b) LOOP(a[i],b[i])
|
145 |
|
|
indata+=len;
|
146 |
|
|
outdata+=len;
|
147 |
|
|
#endif
|
148 |
|
|
|
149 |
|
|
i= -(int)len;
|
150 |
|
|
for (;;)
|
151 |
|
|
{
|
152 |
|
|
RC4_LOOP(indata,outdata); if (++i == 0) break;
|
153 |
|
|
RC4_LOOP(indata,outdata); if (++i == 0) break;
|
154 |
|
|
RC4_LOOP(indata,outdata); if (++i == 0) break;
|
155 |
|
|
RC4_LOOP(indata,outdata); if (++i == 0) break;
|
156 |
|
|
RC4_LOOP(indata,outdata); if (++i == 0) break;
|
157 |
|
|
RC4_LOOP(indata,outdata); if (++i == 0) break;
|
158 |
|
|
RC4_LOOP(indata,outdata); if (++i == 0) break;
|
159 |
|
|
RC4_LOOP(indata,outdata); if (++i == 0) break;
|
160 |
|
|
}
|
161 |
|
|
key->x=x;
|
162 |
|
|
key->y=y;
|
163 |
|
|
}
|