155714Skris/* crypto/rc4/rc4_skey.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296465Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296465Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296465Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296465Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296465Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <openssl/rc4.h> 6055714Skris#include "rc4_locl.h" 6155714Skris#include <openssl/opensslv.h> 62194206Ssimon#include <openssl/crypto.h> 63194206Ssimon#ifdef OPENSSL_FIPS 64296465Sdelphij# include <openssl/fips.h> 65194206Ssimon#endif 6655714Skris 67296465Sdelphijconst char RC4_version[] = "RC4" OPENSSL_VERSION_PTEXT; 68194206Ssimon 6955714Skrisconst char *RC4_options(void) 70296465Sdelphij{ 7155714Skris#ifdef RC4_INDEX 72296465Sdelphij if (sizeof(RC4_INT) == 1) 73296465Sdelphij return ("rc4(idx,char)"); 74296465Sdelphij else 75296465Sdelphij return ("rc4(idx,int)"); 7655714Skris#else 77296465Sdelphij if (sizeof(RC4_INT) == 1) 78296465Sdelphij return ("rc4(ptr,char)"); 79296465Sdelphij else 80296465Sdelphij return ("rc4(ptr,int)"); 8155714Skris#endif 82296465Sdelphij} 8355714Skris 84296465Sdelphij/*- 85296465Sdelphij * RC4 as implemented from a posting from 8655714Skris * Newsgroups: sci.crypt 8755714Skris * From: sterndark@netcom.com (David Sterndark) 8855714Skris * Subject: RC4 Algorithm revealed. 8955714Skris * Message-ID: <sternCvKL4B.Hyy@netcom.com> 9055714Skris * Date: Wed, 14 Sep 1994 06:35:31 GMT 9155714Skris */ 9255714Skris 93194206Ssimon#ifdef OPENSSL_FIPS 94194206Ssimonvoid private_RC4_set_key(RC4_KEY *key, int len, const unsigned char *data) 95194206Ssimon#else 9659191Skrisvoid RC4_set_key(RC4_KEY *key, int len, const unsigned char *data) 97194206Ssimon#endif 98296465Sdelphij{ 99296465Sdelphij register RC4_INT tmp; 100296465Sdelphij register int id1, id2; 101296465Sdelphij register RC4_INT *d; 102296465Sdelphij unsigned int i; 10355714Skris 104296465Sdelphij d = &(key->data[0]); 105296465Sdelphij key->x = 0; 106296465Sdelphij key->y = 0; 107296465Sdelphij id1 = id2 = 0; 108296465Sdelphij 109160814Ssimon#define SK_LOOP(d,n) { \ 110296465Sdelphij tmp=d[(n)]; \ 111296465Sdelphij id2 = (data[id1] + tmp + id2) & 0xff; \ 112296465Sdelphij if (++id1 == len) id1=0; \ 113296465Sdelphij d[(n)]=d[id2]; \ 114296465Sdelphij d[id2]=tmp; } 11555714Skris 116160814Ssimon#if defined(OPENSSL_CPUID_OBJ) && !defined(OPENSSL_NO_ASM) 117296465Sdelphij# if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ 118296465Sdelphij defined(__INTEL__) || \ 119296465Sdelphij defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) 120296465Sdelphij if (sizeof(RC4_INT) > 1) { 121296465Sdelphij /* 122296465Sdelphij * Unlike all other x86 [and x86_64] implementations, 123296465Sdelphij * Intel P4 core [including EM64T] was found to perform 124296465Sdelphij * poorly with wider RC4_INT. Performance improvement 125296465Sdelphij * for IA-32 hand-coded assembler turned out to be 2.8x 126296465Sdelphij * if re-coded for RC4_CHAR! It's however inappropriate 127296465Sdelphij * to just switch to RC4_CHAR for x86[_64], as non-P4 128296465Sdelphij * implementations suffer from significant performance 129296465Sdelphij * losses then, e.g. PIII exhibits >2x deterioration, 130296465Sdelphij * and so does Opteron. In order to assure optimal 131296465Sdelphij * all-round performance, we detect P4 at run-time by 132296465Sdelphij * checking upon reserved bit 20 in CPU capability 133296465Sdelphij * vector and set up compressed key schedule, which is 134296465Sdelphij * recognized by correspondingly updated assembler 135296465Sdelphij * module... Bit 20 is set up by OPENSSL_ia32_cpuid. 136296465Sdelphij * 137296465Sdelphij * <appro@fy.chalmers.se> 138296465Sdelphij */ 139296465Sdelphij# ifdef OPENSSL_FIPS 140296465Sdelphij unsigned long *ia32cap_ptr = OPENSSL_ia32cap_loc(); 141296465Sdelphij if (ia32cap_ptr && (*ia32cap_ptr & (1 << 20))) { 142296465Sdelphij# else 143296465Sdelphij if (OPENSSL_ia32cap_P & (1 << 20)) { 144296465Sdelphij# endif 145296465Sdelphij unsigned char *cp = (unsigned char *)d; 146160814Ssimon 147296465Sdelphij for (i = 0; i < 256; i++) 148296465Sdelphij cp[i] = i; 149296465Sdelphij for (i = 0; i < 256; i++) 150296465Sdelphij SK_LOOP(cp, i); 151296465Sdelphij /* mark schedule as compressed! */ 152296465Sdelphij d[256 / sizeof(RC4_INT)] = -1; 153296465Sdelphij return; 154296465Sdelphij } 155296465Sdelphij } 156160814Ssimon# endif 157160814Ssimon#endif 158296465Sdelphij for (i = 0; i < 256; i++) 159296465Sdelphij d[i] = i; 160296465Sdelphij for (i = 0; i < 256; i += 4) { 161296465Sdelphij SK_LOOP(d, i + 0); 162296465Sdelphij SK_LOOP(d, i + 1); 163296465Sdelphij SK_LOOP(d, i + 2); 164296465Sdelphij SK_LOOP(d, i + 3); 165296465Sdelphij } 166296465Sdelphij} 167