/*1* rc4.c2*3* Copyright (c) 1996-2000 Whistle Communications, Inc.4* All rights reserved.5*6* Subject to the following obligations and disclaimer of warranty, use and7* redistribution of this software, in source or object code forms, with or8* without modifications are expressly permitted by Whistle Communications;9* provided, however, that:10* 1. Any and all reproductions of the source or object code must include the11* copyright notice above and the following disclaimer of warranties; and12* 2. No rights are granted, in any manner or form, to use Whistle13* Communications, Inc. trademarks, including the mark "WHISTLE14* COMMUNICATIONS" on advertising, endorsements, or otherwise except as15* such appears in the above copyright notice or in the software.16*17* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND18* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO19* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,20* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.22* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY23* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS24* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.25* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES26* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING27* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,28* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR29* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY30* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT31* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF32* THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY33* OF SUCH DAMAGE.34*/3536#include <sys/param.h>37#include <sys/kernel.h>38#include <sys/module.h>39#include <sys/types.h>40#include <crypto/rc4/rc4.h>4142static __inline void43swap_bytes(u_char *a, u_char *b)44{45u_char temp;4647temp = *a;48*a = *b;49*b = temp;50}5152/*53* Initialize an RC4 state buffer using the supplied key,54* which can have arbitrary length.55*/56void57rc4_init(struct rc4_state *const state, const u_char *key, int keylen)58{59u_char j;60int i, k;6162/* Initialize state with identity permutation */63for (i = 0; i < 256; i++)64state->perm[i] = (u_char)i;65state->index1 = 0;66state->index2 = 0;6768/* Randomize the permutation using key data */69for (j = i = k = 0; i < 256; i++) {70j += state->perm[i] + key[k];71swap_bytes(&state->perm[i], &state->perm[j]);72if (++k >= keylen)73k = 0;74}75}7677/*78* Encrypt some data using the supplied RC4 state buffer.79* The input and output buffers may be the same buffer.80* Since RC4 is a stream cypher, this function is used81* for both encryption and decryption.82*/83void84rc4_crypt(struct rc4_state *const state,85const u_char *inbuf, u_char *outbuf, int buflen)86{87int i;88u_char j;8990for (i = 0; i < buflen; i++) {9192/* Update modification indicies */93state->index1++;94state->index2 += state->perm[state->index1];9596/* Modify permutation */97swap_bytes(&state->perm[state->index1],98&state->perm[state->index2]);99100/* Encrypt/decrypt next byte */101j = state->perm[state->index1] + state->perm[state->index2];102outbuf[i] = inbuf[i] ^ state->perm[j];103}104}105106static int107rc4_modevent(module_t mod, int type, void *unused)108{109switch (type) {110case MOD_LOAD:111return 0;112case MOD_UNLOAD:113return 0;114}115return EINVAL;116}117118static moduledata_t rc4_mod = {119"rc4",120rc4_modevent,1210122};123DECLARE_MODULE(rc4, rc4_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);124MODULE_VERSION(rc4, 1);125126127