rc4.c revision 59108
1271093Sbr
2271093Sbr/*
3271093Sbr * rc4.c
4271093Sbr *
5271093Sbr * Copyright (c) 1996-2000 Whistle Communications, Inc.
6271093Sbr * All rights reserved.
7271093Sbr *
8271093Sbr * Subject to the following obligations and disclaimer of warranty, use and
9271093Sbr * redistribution of this software, in source or object code forms, with or
10271093Sbr * without modifications are expressly permitted by Whistle Communications;
11271093Sbr * provided, however, that:
12271093Sbr * 1. Any and all reproductions of the source or object code must include the
13271093Sbr *    copyright notice above and the following disclaimer of warranties; and
14271093Sbr * 2. No rights are granted, in any manner or form, to use Whistle
15271093Sbr *    Communications, Inc. trademarks, including the mark "WHISTLE
16271093Sbr *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17271093Sbr *    such appears in the above copyright notice or in the software.
18271093Sbr *
19271093Sbr * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20271093Sbr * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21271093Sbr * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22271093Sbr * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23271093Sbr * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24271093Sbr * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25271093Sbr * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26271093Sbr * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27271093Sbr * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28271093Sbr * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29271093Sbr * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30271093Sbr * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31271093Sbr * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32271093Sbr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33271093Sbr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34271093Sbr * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35271093Sbr * OF SUCH DAMAGE.
36271093Sbr *
37271093Sbr * $FreeBSD: head/sys/crypto/rc4/rc4.c 59108 2000-04-09 21:01:01Z archie $
38271093Sbr */
39271093Sbr
40271093Sbr#include <sys/types.h>
41271093Sbr#include <crypto/rc4/rc4.h>
42271093Sbr
43271093Sbrstatic __inline void
44271093Sbrswap_bytes(u_char *a, u_char *b)
45271093Sbr{
46271093Sbr	u_char temp;
47271093Sbr
48271093Sbr	temp = *a;
49271093Sbr	*a = *b;
50271093Sbr	*b = temp;
51271093Sbr}
52271093Sbr
53271093Sbr/*
54271093Sbr * Initialize an RC4 state buffer using the supplied key,
55271093Sbr * which can have arbitrary length.
56271093Sbr */
57271093Sbrvoid
58271093Sbrrc4_init(struct rc4_state *const state, const u_char *key, int keylen)
59271093Sbr{
60271093Sbr	u_char j;
61271093Sbr	int i;
62271093Sbr
63271093Sbr	/* Initialize state with identity permutation */
64271093Sbr	for (i = 0; i < 256; i++)
65271093Sbr		state->perm[i] = (u_char)i;
66271093Sbr	state->index1 = 0;
67271093Sbr	state->index2 = 0;
68271093Sbr
69271093Sbr	/* Randomize the permutation using key data */
70271093Sbr	for (j = i = 0; i < 256; i++) {
71271093Sbr		j += state->perm[i] + key[i % keylen];
72271186Sbr		swap_bytes(&state->perm[i], &state->perm[j]);
73271186Sbr	}
74271186Sbr}
75271186Sbr
76271186Sbr/*
77271186Sbr * Encrypt some data using the supplied RC4 state buffer.
78271186Sbr * The input and output buffers may be the same buffer.
79271186Sbr * Since RC4 is a stream cypher, this function is used
80271093Sbr * for both encryption and decryption.
81271093Sbr */
82271093Sbrvoid
83271093Sbrrc4_crypt(struct rc4_state *const state,
84271093Sbr	const u_char *inbuf, u_char *outbuf, int buflen)
85271093Sbr{
86271093Sbr	int i;
87271093Sbr	u_char j;
88271093Sbr
89271093Sbr	for (i = 0; i < buflen; i++) {
90271093Sbr
91271093Sbr		/* Update modification indicies */
92271093Sbr		state->index1++;
93271093Sbr		state->index2 += state->perm[state->index1];
94271093Sbr
95271093Sbr		/* Modify permutation */
96271093Sbr		swap_bytes(&state->perm[state->index1],
97271093Sbr		    &state->perm[state->index2]);
98271093Sbr
99271093Sbr		/* Encrypt/decrypt next byte */
100271093Sbr		j = state->perm[state->index1] + state->perm[state->index2];
101271093Sbr		outbuf[i] = inbuf[i] ^ state->perm[j];
102271093Sbr	}
103271093Sbr}
104271093Sbr
105271093Sbr