rc4_skey.c revision 59191
1139823Simp/* crypto/rc4/rc4_skey.c */
2116808Sharti/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3116808Sharti * All rights reserved.
4116808Sharti *
5116808Sharti * This package is an SSL implementation written
6116808Sharti * by Eric Young (eay@cryptsoft.com).
7116808Sharti * The implementation was written so as to conform with Netscapes SSL.
8116808Sharti *
9116808Sharti * This library is free for commercial and non-commercial use as long as
10116808Sharti * the following conditions are aheared to.  The following conditions
11116808Sharti * apply to all code found in this distribution, be it the RC4, RSA,
12116808Sharti * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13116808Sharti * included with this distribution is covered by the same copyright terms
14116808Sharti * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15116808Sharti *
16116808Sharti * Copyright remains Eric Young's, and as such any Copyright notices in
17116808Sharti * the code are not to be removed.
18116808Sharti * If this package is used in a product, Eric Young should be given attribution
19116808Sharti * as the author of the parts of the library used.
20116808Sharti * This can be in the form of a textual message at program startup or
21116808Sharti * in documentation (online or textual) provided with the package.
22116808Sharti *
23116808Sharti * Redistribution and use in source and binary forms, with or without
24116808Sharti * modification, are permitted provided that the following conditions
25116808Sharti * are met:
26116808Sharti * 1. Redistributions of source code must retain the copyright
27116808Sharti *    notice, this list of conditions and the following disclaimer.
28139823Simp * 2. Redistributions in binary form must reproduce the above copyright
29139823Simp *    notice, this list of conditions and the following disclaimer in the
30139823Simp *    documentation and/or other materials provided with the distribution.
31116808Sharti * 3. All advertising materials mentioning features or use of this software
32116808Sharti *    must display the following acknowledgement:
33116808Sharti *    "This product includes cryptographic software written by
34116808Sharti *     Eric Young (eay@cryptsoft.com)"
35116808Sharti *    The word 'cryptographic' can be left out if the rouines from the library
36116808Sharti *    being used are not cryptographic related :-).
37116808Sharti * 4. If you include any Windows specific code (or a derivative thereof) from
38116808Sharti *    the apps directory (application code) you must include an acknowledgement:
39116808Sharti *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40116808Sharti *
41116808Sharti * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42116808Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43116808Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44116808Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45116808Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46116808Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47116808Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48116808Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49116808Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50116808Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51116808Sharti * SUCH DAMAGE.
52116808Sharti *
53116808Sharti * The licence and distribution terms for any publically available version or
54116808Sharti * derivative of this code cannot be changed.  i.e. this code cannot simply be
55116808Sharti * copied and put under another distribution licence
56185571Sbz * [including the GNU Public Licence.]
57116808Sharti */
58116808Sharti
59116808Sharti#include <openssl/rc4.h>
60116808Sharti#include "rc4_locl.h"
61116808Sharti#include <openssl/opensslv.h>
62116808Sharti
63116808Sharticonst char *RC4_version="RC4" OPENSSL_VERSION_PTEXT;
64116808Sharti
65116808Sharticonst char *RC4_options(void)
66116808Sharti	{
67116808Sharti#ifdef RC4_INDEX
68116808Sharti	if (sizeof(RC4_INT) == 1)
69116808Sharti		return("rc4(idx,char)");
70116808Sharti	else
71116808Sharti		return("rc4(idx,int)");
72116808Sharti#else
73118175Sharti	if (sizeof(RC4_INT) == 1)
74116808Sharti		return("rc4(ptr,char)");
75116808Sharti	else
76116808Sharti		return("rc4(ptr,int)");
77116808Sharti#endif
78227309Sed	}
79227309Sed
80116808Sharti/* RC4 as implemented from a posting from
81116808Sharti * Newsgroups: sci.crypt
82116808Sharti * From: sterndark@netcom.com (David Sterndark)
83116808Sharti * Subject: RC4 Algorithm revealed.
84116808Sharti * Message-ID: <sternCvKL4B.Hyy@netcom.com>
85116808Sharti * Date: Wed, 14 Sep 1994 06:35:31 GMT
86116808Sharti */
87116808Sharti
88116808Shartivoid RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
89116808Sharti	{
90116808Sharti        register RC4_INT tmp;
91116808Sharti        register int id1,id2;
92116808Sharti        register RC4_INT *d;
93116808Sharti        unsigned int i;
94116808Sharti
95116808Sharti        d= &(key->data[0]);
96116808Sharti	for (i=0; i<256; i++)
97116808Sharti		d[i]=i;
98116808Sharti        key->x = 0;
99117157Sharti        key->y = 0;
100116808Sharti        id1=id2=0;
101116808Sharti
102116808Sharti#define SK_LOOP(n) { \
103116808Sharti		tmp=d[(n)]; \
104116808Sharti		id2 = (data[id1] + tmp + id2) & 0xff; \
105116808Sharti		if (++id1 == len) id1=0; \
106116808Sharti		d[(n)]=d[id2]; \
107116808Sharti		d[id2]=tmp; }
108116808Sharti
109116808Sharti	for (i=0; i < 256; i+=4)
110116808Sharti		{
111116808Sharti		SK_LOOP(i+0);
112116808Sharti		SK_LOOP(i+1);
113116808Sharti		SK_LOOP(i+2);
114116808Sharti		SK_LOOP(i+3);
115116808Sharti		}
116116808Sharti	}
117116808Sharti
118116808Sharti