5
* Copyright (C) 2011, Broadcom Corporation. All Rights Reserved.
7
* Permission to use, copy, modify, and/or distribute this software for any
8
* purpose with or without fee is hereby granted, provided that the above
9
* copyright notice and this permission notice appear in all copies.
11
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
14
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
16
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
* $Id: rc4.c 326246 2012-04-06 19:08:11Z $
23
#include <bcmcrypto/rc4.h>
26
BCMROMFN(prepare_key)(uint8 *key_data, int key_data_len, rc4_ks_t *ks)
28
unsigned int counter, index1 = 0, index2 = 0;
30
uint8 *key_state = ks->state;
32
for (counter = 0; counter < RC4_STATE_NBYTES; counter++) {
33
key_state[counter] = (uint8) counter;
36
for (counter = 0; counter < RC4_STATE_NBYTES; counter++) {
37
key_byte = key_data[index1];
38
index2 = (key_byte + key_state[counter] + index2) % RC4_STATE_NBYTES;
39
temp = key_state[counter];
40
key_state[counter] = key_state[index2];
41
key_state[index2] = temp;
42
index1 = (index1 + 1) % key_data_len;
50
BCMROMFN(rc4)(uint8 *buf, int data_len, rc4_ks_t *ks)
53
uint8 xor_ind, x = ks->x, y = ks->y, *key_state = ks->state;
56
for (i = 0; i < data_len; i++) {
59
key_state[x] = key_state[y];
61
xor_ind = key_state[x] + key_state[y];
62
buf[i] ^= key_state[xor_ind];