xref: /dflybsd-src/sys/crypto/rc4/rc4.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /*
286d7f5d3SJohn Marino  * rc4.c
386d7f5d3SJohn Marino  *
486d7f5d3SJohn Marino  * Copyright (c) 1996-2000 Whistle Communications, Inc.
586d7f5d3SJohn Marino  * All rights reserved.
686d7f5d3SJohn Marino  *
786d7f5d3SJohn Marino  * Subject to the following obligations and disclaimer of warranty, use and
886d7f5d3SJohn Marino  * redistribution of this software, in source or object code forms, with or
986d7f5d3SJohn Marino  * without modifications are expressly permitted by Whistle Communications;
1086d7f5d3SJohn Marino  * provided, however, that:
1186d7f5d3SJohn Marino  * 1. Any and all reproductions of the source or object code must include the
1286d7f5d3SJohn Marino  *    copyright notice above and the following disclaimer of warranties; and
1386d7f5d3SJohn Marino  * 2. No rights are granted, in any manner or form, to use Whistle
1486d7f5d3SJohn Marino  *    Communications, Inc. trademarks, including the mark "WHISTLE
1586d7f5d3SJohn Marino  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
1686d7f5d3SJohn Marino  *    such appears in the above copyright notice or in the software.
1786d7f5d3SJohn Marino  *
1886d7f5d3SJohn Marino  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
1986d7f5d3SJohn Marino  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
2086d7f5d3SJohn Marino  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
2186d7f5d3SJohn Marino  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
2286d7f5d3SJohn Marino  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
2386d7f5d3SJohn Marino  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
2486d7f5d3SJohn Marino  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
2586d7f5d3SJohn Marino  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
2686d7f5d3SJohn Marino  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
2786d7f5d3SJohn Marino  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
2886d7f5d3SJohn Marino  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
2986d7f5d3SJohn Marino  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
3086d7f5d3SJohn Marino  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
3186d7f5d3SJohn Marino  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3286d7f5d3SJohn Marino  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3386d7f5d3SJohn Marino  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
3486d7f5d3SJohn Marino  * OF SUCH DAMAGE.
3586d7f5d3SJohn Marino  *
3686d7f5d3SJohn Marino  * $FreeBSD: src/sys/crypto/rc4/rc4.c,v 1.6 2008/12/16 13:58:37 mav Exp $
3786d7f5d3SJohn Marino  */
3886d7f5d3SJohn Marino 
3986d7f5d3SJohn Marino #include <sys/kernel.h>
4086d7f5d3SJohn Marino #include <sys/module.h>
4186d7f5d3SJohn Marino #include <sys/types.h>
4286d7f5d3SJohn Marino #include <crypto/rc4/rc4.h>
4386d7f5d3SJohn Marino 
4486d7f5d3SJohn Marino static __inline void
swap_bytes(u_char * a,u_char * b)4586d7f5d3SJohn Marino swap_bytes(u_char *a, u_char *b)
4686d7f5d3SJohn Marino {
4786d7f5d3SJohn Marino 	u_char temp;
4886d7f5d3SJohn Marino 
4986d7f5d3SJohn Marino 	temp = *a;
5086d7f5d3SJohn Marino 	*a = *b;
5186d7f5d3SJohn Marino 	*b = temp;
5286d7f5d3SJohn Marino }
5386d7f5d3SJohn Marino 
5486d7f5d3SJohn Marino /*
5586d7f5d3SJohn Marino  * Initialize an RC4 state buffer using the supplied key,
5686d7f5d3SJohn Marino  * which can have arbitrary length.
5786d7f5d3SJohn Marino  */
5886d7f5d3SJohn Marino void
rc4_init(struct rc4_state * const state,const u_char * key,int keylen)5986d7f5d3SJohn Marino rc4_init(struct rc4_state *const state, const u_char *key, int keylen)
6086d7f5d3SJohn Marino {
6186d7f5d3SJohn Marino 	u_char j;
6286d7f5d3SJohn Marino 	int i, k;
6386d7f5d3SJohn Marino 
6486d7f5d3SJohn Marino 	/* Initialize state with identity permutation */
6586d7f5d3SJohn Marino 	for (i = 0; i < 256; i++)
6686d7f5d3SJohn Marino 		state->perm[i] = (u_char)i;
6786d7f5d3SJohn Marino 	state->index1 = 0;
6886d7f5d3SJohn Marino 	state->index2 = 0;
6986d7f5d3SJohn Marino 
7086d7f5d3SJohn Marino 	/* Randomize the permutation using key data */
7186d7f5d3SJohn Marino 	for (j = i = k = 0; i < 256; i++) {
7286d7f5d3SJohn Marino 		j += state->perm[i] + key[k];
7386d7f5d3SJohn Marino 		swap_bytes(&state->perm[i], &state->perm[j]);
7486d7f5d3SJohn Marino 		if (++k >= keylen)
7586d7f5d3SJohn Marino 			k = 0;
7686d7f5d3SJohn Marino 	}
7786d7f5d3SJohn Marino }
7886d7f5d3SJohn Marino 
7986d7f5d3SJohn Marino /*
8086d7f5d3SJohn Marino  * Encrypt some data using the supplied RC4 state buffer.
8186d7f5d3SJohn Marino  * The input and output buffers may be the same buffer.
8286d7f5d3SJohn Marino  * Since RC4 is a stream cypher, this function is used
8386d7f5d3SJohn Marino  * for both encryption and decryption.
8486d7f5d3SJohn Marino  */
8586d7f5d3SJohn Marino void
rc4_crypt(struct rc4_state * const state,const u_char * inbuf,u_char * outbuf,int buflen)8686d7f5d3SJohn Marino rc4_crypt(struct rc4_state *const state,
8786d7f5d3SJohn Marino 	const u_char *inbuf, u_char *outbuf, int buflen)
8886d7f5d3SJohn Marino {
8986d7f5d3SJohn Marino 	int i;
9086d7f5d3SJohn Marino 	u_char j;
9186d7f5d3SJohn Marino 
9286d7f5d3SJohn Marino 	for (i = 0; i < buflen; i++) {
9386d7f5d3SJohn Marino 
9486d7f5d3SJohn Marino 		/* Update modification indicies */
9586d7f5d3SJohn Marino 		state->index1++;
9686d7f5d3SJohn Marino 		state->index2 += state->perm[state->index1];
9786d7f5d3SJohn Marino 
9886d7f5d3SJohn Marino 		/* Modify permutation */
9986d7f5d3SJohn Marino 		swap_bytes(&state->perm[state->index1],
10086d7f5d3SJohn Marino 		    &state->perm[state->index2]);
10186d7f5d3SJohn Marino 
10286d7f5d3SJohn Marino 		/* Encrypt/decrypt next byte */
10386d7f5d3SJohn Marino 		j = state->perm[state->index1] + state->perm[state->index2];
10486d7f5d3SJohn Marino 		outbuf[i] = inbuf[i] ^ state->perm[j];
10586d7f5d3SJohn Marino 	}
10686d7f5d3SJohn Marino }
10786d7f5d3SJohn Marino 
10886d7f5d3SJohn Marino static int
rc4_modevent(module_t mod,int type,void * unused)10986d7f5d3SJohn Marino rc4_modevent(module_t mod, int type, void *unused)
11086d7f5d3SJohn Marino {
11186d7f5d3SJohn Marino 	switch (type) {
11286d7f5d3SJohn Marino 	case MOD_LOAD:
11386d7f5d3SJohn Marino 		return 0;
11486d7f5d3SJohn Marino 	case MOD_UNLOAD:
11586d7f5d3SJohn Marino 		return 0;
11686d7f5d3SJohn Marino 	}
11786d7f5d3SJohn Marino 	return EINVAL;
11886d7f5d3SJohn Marino }
11986d7f5d3SJohn Marino 
12086d7f5d3SJohn Marino static moduledata_t rc4_mod = {
12186d7f5d3SJohn Marino 	"rc4",
12286d7f5d3SJohn Marino 	rc4_modevent,
12386d7f5d3SJohn Marino 	0
12486d7f5d3SJohn Marino };
12586d7f5d3SJohn Marino DECLARE_MODULE(rc4, rc4_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
12686d7f5d3SJohn Marino MODULE_VERSION(rc4, 1);
127