xref: /freebsd/sys/crypto/rc4/rc4.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1018204afSArchie Cobbs /*
2018204afSArchie Cobbs  * rc4.c
3018204afSArchie Cobbs  *
4018204afSArchie Cobbs  * Copyright (c) 1996-2000 Whistle Communications, Inc.
5018204afSArchie Cobbs  * All rights reserved.
6018204afSArchie Cobbs  *
7018204afSArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
8018204afSArchie Cobbs  * redistribution of this software, in source or object code forms, with or
9018204afSArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
10018204afSArchie Cobbs  * provided, however, that:
11018204afSArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
12018204afSArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
13018204afSArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
14018204afSArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
15018204afSArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16018204afSArchie Cobbs  *    such appears in the above copyright notice or in the software.
17018204afSArchie Cobbs  *
18018204afSArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19018204afSArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20018204afSArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21018204afSArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22018204afSArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23018204afSArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24018204afSArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25018204afSArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26018204afSArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27018204afSArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28018204afSArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29018204afSArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30018204afSArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31018204afSArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32018204afSArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33018204afSArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
34018204afSArchie Cobbs  * OF SUCH DAMAGE.
35018204afSArchie Cobbs  */
36018204afSArchie Cobbs 
3750b25cd7SSam Leffler #include <sys/param.h>
3850b25cd7SSam Leffler #include <sys/kernel.h>
3950b25cd7SSam Leffler #include <sys/module.h>
40018204afSArchie Cobbs #include <sys/types.h>
41018204afSArchie Cobbs #include <crypto/rc4/rc4.h>
42018204afSArchie Cobbs 
43018204afSArchie Cobbs static __inline void
swap_bytes(u_char * a,u_char * b)44018204afSArchie Cobbs swap_bytes(u_char *a, u_char *b)
45018204afSArchie Cobbs {
46018204afSArchie Cobbs 	u_char temp;
47018204afSArchie Cobbs 
48018204afSArchie Cobbs 	temp = *a;
49018204afSArchie Cobbs 	*a = *b;
50018204afSArchie Cobbs 	*b = temp;
51018204afSArchie Cobbs }
52018204afSArchie Cobbs 
53018204afSArchie Cobbs /*
54018204afSArchie Cobbs  * Initialize an RC4 state buffer using the supplied key,
55018204afSArchie Cobbs  * which can have arbitrary length.
56018204afSArchie Cobbs  */
57018204afSArchie Cobbs void
rc4_init(struct rc4_state * const state,const u_char * key,int keylen)58018204afSArchie Cobbs rc4_init(struct rc4_state *const state, const u_char *key, int keylen)
59018204afSArchie Cobbs {
60018204afSArchie Cobbs 	u_char j;
61f2831a95SAlexander Motin 	int i, k;
62018204afSArchie Cobbs 
63018204afSArchie Cobbs 	/* Initialize state with identity permutation */
64018204afSArchie Cobbs 	for (i = 0; i < 256; i++)
65018204afSArchie Cobbs 		state->perm[i] = (u_char)i;
66018204afSArchie Cobbs 	state->index1 = 0;
67018204afSArchie Cobbs 	state->index2 = 0;
68018204afSArchie Cobbs 
69018204afSArchie Cobbs 	/* Randomize the permutation using key data */
70f2831a95SAlexander Motin 	for (j = i = k = 0; i < 256; i++) {
71f2831a95SAlexander Motin 		j += state->perm[i] + key[k];
72018204afSArchie Cobbs 		swap_bytes(&state->perm[i], &state->perm[j]);
73f2831a95SAlexander Motin 		if (++k >= keylen)
74f2831a95SAlexander Motin 			k = 0;
75018204afSArchie Cobbs 	}
76018204afSArchie Cobbs }
77018204afSArchie Cobbs 
78018204afSArchie Cobbs /*
79018204afSArchie Cobbs  * Encrypt some data using the supplied RC4 state buffer.
80018204afSArchie Cobbs  * The input and output buffers may be the same buffer.
81018204afSArchie Cobbs  * Since RC4 is a stream cypher, this function is used
82018204afSArchie Cobbs  * for both encryption and decryption.
83018204afSArchie Cobbs  */
84018204afSArchie Cobbs void
rc4_crypt(struct rc4_state * const state,const u_char * inbuf,u_char * outbuf,int buflen)85018204afSArchie Cobbs rc4_crypt(struct rc4_state *const state,
86018204afSArchie Cobbs 	const u_char *inbuf, u_char *outbuf, int buflen)
87018204afSArchie Cobbs {
88018204afSArchie Cobbs 	int i;
89018204afSArchie Cobbs 	u_char j;
90018204afSArchie Cobbs 
91018204afSArchie Cobbs 	for (i = 0; i < buflen; i++) {
92018204afSArchie Cobbs 
93018204afSArchie Cobbs 		/* Update modification indicies */
94018204afSArchie Cobbs 		state->index1++;
95018204afSArchie Cobbs 		state->index2 += state->perm[state->index1];
96018204afSArchie Cobbs 
97018204afSArchie Cobbs 		/* Modify permutation */
98018204afSArchie Cobbs 		swap_bytes(&state->perm[state->index1],
99018204afSArchie Cobbs 		    &state->perm[state->index2]);
100018204afSArchie Cobbs 
101018204afSArchie Cobbs 		/* Encrypt/decrypt next byte */
102018204afSArchie Cobbs 		j = state->perm[state->index1] + state->perm[state->index2];
103018204afSArchie Cobbs 		outbuf[i] = inbuf[i] ^ state->perm[j];
104018204afSArchie Cobbs 	}
105018204afSArchie Cobbs }
106018204afSArchie Cobbs 
10750b25cd7SSam Leffler static int
rc4_modevent(module_t mod,int type,void * unused)10850b25cd7SSam Leffler rc4_modevent(module_t mod, int type, void *unused)
10950b25cd7SSam Leffler {
11050b25cd7SSam Leffler 	switch (type) {
11150b25cd7SSam Leffler 	case MOD_LOAD:
11250b25cd7SSam Leffler 		return 0;
11350b25cd7SSam Leffler 	case MOD_UNLOAD:
11450b25cd7SSam Leffler 		return 0;
11550b25cd7SSam Leffler 	}
11650b25cd7SSam Leffler 	return EINVAL;
11750b25cd7SSam Leffler }
11850b25cd7SSam Leffler 
11950b25cd7SSam Leffler static moduledata_t rc4_mod = {
12050b25cd7SSam Leffler 	"rc4",
12150b25cd7SSam Leffler 	rc4_modevent,
122*9823d527SKevin Lo 	0
12350b25cd7SSam Leffler };
12450b25cd7SSam Leffler DECLARE_MODULE(rc4, rc4_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
12550b25cd7SSam Leffler MODULE_VERSION(rc4, 1);
126