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