xref: /freebsd/sys/crypto/rc4/rc4.c (revision ecf4106237505fa9459ae871793b754334989c17)
1 /*
2  * rc4.c
3  *
4  * Copyright (c) 1996-2000 Whistle Communications, Inc.
5  * All rights reserved.
6  *
7  * Subject to the following obligations and disclaimer of warranty, use and
8  * redistribution of this software, in source or object code forms, with or
9  * without modifications are expressly permitted by Whistle Communications;
10  * provided, however, that:
11  * 1. Any and all reproductions of the source or object code must include the
12  *    copyright notice above and the following disclaimer of warranties; and
13  * 2. No rights are granted, in any manner or form, to use Whistle
14  *    Communications, Inc. trademarks, including the mark "WHISTLE
15  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16  *    such appears in the above copyright notice or in the software.
17  *
18  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
34  * OF SUCH DAMAGE.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/types.h>
41 #include <crypto/rc4/rc4.h>
42 
43 static __inline void
44 swap_bytes(u_char *a, u_char *b)
45 {
46 	u_char temp;
47 
48 	temp = *a;
49 	*a = *b;
50 	*b = temp;
51 }
52 
53 /*
54  * Initialize an RC4 state buffer using the supplied key,
55  * which can have arbitrary length.
56  */
57 void
58 rc4_init(struct rc4_state *const state, const u_char *key, int keylen)
59 {
60 	u_char j;
61 	int i, k;
62 
63 	/* Initialize state with identity permutation */
64 	for (i = 0; i < 256; i++)
65 		state->perm[i] = (u_char)i;
66 	state->index1 = 0;
67 	state->index2 = 0;
68 
69 	/* Randomize the permutation using key data */
70 	for (j = i = k = 0; i < 256; i++) {
71 		j += state->perm[i] + key[k];
72 		swap_bytes(&state->perm[i], &state->perm[j]);
73 		if (++k >= keylen)
74 			k = 0;
75 	}
76 }
77 
78 /*
79  * Encrypt some data using the supplied RC4 state buffer.
80  * The input and output buffers may be the same buffer.
81  * Since RC4 is a stream cypher, this function is used
82  * for both encryption and decryption.
83  */
84 void
85 rc4_crypt(struct rc4_state *const state,
86 	const u_char *inbuf, u_char *outbuf, int buflen)
87 {
88 	int i;
89 	u_char j;
90 
91 	for (i = 0; i < buflen; i++) {
92 
93 		/* Update modification indicies */
94 		state->index1++;
95 		state->index2 += state->perm[state->index1];
96 
97 		/* Modify permutation */
98 		swap_bytes(&state->perm[state->index1],
99 		    &state->perm[state->index2]);
100 
101 		/* Encrypt/decrypt next byte */
102 		j = state->perm[state->index1] + state->perm[state->index2];
103 		outbuf[i] = inbuf[i] ^ state->perm[j];
104 	}
105 }
106 
107 static int
108 rc4_modevent(module_t mod, int type, void *unused)
109 {
110 	switch (type) {
111 	case MOD_LOAD:
112 		return 0;
113 	case MOD_UNLOAD:
114 		return 0;
115 	}
116 	return EINVAL;
117 }
118 
119 static moduledata_t rc4_mod = {
120 	"rc4",
121 	rc4_modevent,
122 	0
123 };
124 DECLARE_MODULE(rc4, rc4_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
125 MODULE_VERSION(rc4, 1);
126