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 37ad39da78SDavid E. O'Brien #include <sys/cdefs.h> 38ad39da78SDavid E. O'Brien __FBSDID("$FreeBSD$"); 39ad39da78SDavid E. O'Brien 4050b25cd7SSam Leffler #include <sys/param.h> 4150b25cd7SSam Leffler #include <sys/kernel.h> 4250b25cd7SSam Leffler #include <sys/module.h> 43018204afSArchie Cobbs #include <sys/types.h> 44018204afSArchie Cobbs #include <crypto/rc4/rc4.h> 45018204afSArchie Cobbs 46018204afSArchie Cobbs static __inline void 47018204afSArchie Cobbs swap_bytes(u_char *a, u_char *b) 48018204afSArchie Cobbs { 49018204afSArchie Cobbs u_char temp; 50018204afSArchie Cobbs 51018204afSArchie Cobbs temp = *a; 52018204afSArchie Cobbs *a = *b; 53018204afSArchie Cobbs *b = temp; 54018204afSArchie Cobbs } 55018204afSArchie Cobbs 56018204afSArchie Cobbs /* 57018204afSArchie Cobbs * Initialize an RC4 state buffer using the supplied key, 58018204afSArchie Cobbs * which can have arbitrary length. 59018204afSArchie Cobbs */ 60018204afSArchie Cobbs void 61018204afSArchie Cobbs rc4_init(struct rc4_state *const state, const u_char *key, int keylen) 62018204afSArchie Cobbs { 63018204afSArchie Cobbs u_char j; 64f2831a95SAlexander Motin int i, k; 65018204afSArchie Cobbs 66018204afSArchie Cobbs /* Initialize state with identity permutation */ 67018204afSArchie Cobbs for (i = 0; i < 256; i++) 68018204afSArchie Cobbs state->perm[i] = (u_char)i; 69018204afSArchie Cobbs state->index1 = 0; 70018204afSArchie Cobbs state->index2 = 0; 71018204afSArchie Cobbs 72018204afSArchie Cobbs /* Randomize the permutation using key data */ 73f2831a95SAlexander Motin for (j = i = k = 0; i < 256; i++) { 74f2831a95SAlexander Motin j += state->perm[i] + key[k]; 75018204afSArchie Cobbs swap_bytes(&state->perm[i], &state->perm[j]); 76f2831a95SAlexander Motin if (++k >= keylen) 77f2831a95SAlexander Motin k = 0; 78018204afSArchie Cobbs } 79018204afSArchie Cobbs } 80018204afSArchie Cobbs 81018204afSArchie Cobbs /* 82018204afSArchie Cobbs * Encrypt some data using the supplied RC4 state buffer. 83018204afSArchie Cobbs * The input and output buffers may be the same buffer. 84018204afSArchie Cobbs * Since RC4 is a stream cypher, this function is used 85018204afSArchie Cobbs * for both encryption and decryption. 86018204afSArchie Cobbs */ 87018204afSArchie Cobbs void 88018204afSArchie Cobbs rc4_crypt(struct rc4_state *const state, 89018204afSArchie Cobbs const u_char *inbuf, u_char *outbuf, int buflen) 90018204afSArchie Cobbs { 91018204afSArchie Cobbs int i; 92018204afSArchie Cobbs u_char j; 93018204afSArchie Cobbs 94018204afSArchie Cobbs for (i = 0; i < buflen; i++) { 95018204afSArchie Cobbs 96018204afSArchie Cobbs /* Update modification indicies */ 97018204afSArchie Cobbs state->index1++; 98018204afSArchie Cobbs state->index2 += state->perm[state->index1]; 99018204afSArchie Cobbs 100018204afSArchie Cobbs /* Modify permutation */ 101018204afSArchie Cobbs swap_bytes(&state->perm[state->index1], 102018204afSArchie Cobbs &state->perm[state->index2]); 103018204afSArchie Cobbs 104018204afSArchie Cobbs /* Encrypt/decrypt next byte */ 105018204afSArchie Cobbs j = state->perm[state->index1] + state->perm[state->index2]; 106018204afSArchie Cobbs outbuf[i] = inbuf[i] ^ state->perm[j]; 107018204afSArchie Cobbs } 108018204afSArchie Cobbs } 109018204afSArchie Cobbs 11050b25cd7SSam Leffler static int 11150b25cd7SSam Leffler rc4_modevent(module_t mod, int type, void *unused) 11250b25cd7SSam Leffler { 11350b25cd7SSam Leffler switch (type) { 11450b25cd7SSam Leffler case MOD_LOAD: 11550b25cd7SSam Leffler return 0; 11650b25cd7SSam Leffler case MOD_UNLOAD: 11750b25cd7SSam Leffler return 0; 11850b25cd7SSam Leffler } 11950b25cd7SSam Leffler return EINVAL; 12050b25cd7SSam Leffler } 12150b25cd7SSam Leffler 12250b25cd7SSam Leffler static moduledata_t rc4_mod = { 12350b25cd7SSam Leffler "rc4", 12450b25cd7SSam Leffler rc4_modevent, 12550b25cd7SSam Leffler 0 12650b25cd7SSam Leffler }; 12750b25cd7SSam Leffler DECLARE_MODULE(rc4, rc4_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 12850b25cd7SSam Leffler MODULE_VERSION(rc4, 1); 129