3 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #define S_SWAP(a,b) do { u8 t = S[a]; S[a] = S[b]; S[b] = t; } while(0)
23 * rc4 - XOR RC4 stream to given data with skip-stream-start
25 * @keylen: RC4 key length
26 * @skip: number of bytes to skip from the beginning of the RC4 stream
27 * @data: data to be XOR'ed with RC4 stream
28 * @data_len: buf length
30 * Generate RC4 pseudo random stream for the given key, skip beginning of the
31 * stream, and XOR the end result with the data buffer to perform RC4
32 * encryption/decryption.
34 void rc4_skip(const u8
*key
, size_t keylen
, size_t skip
,
35 u8
*data
, size_t data_len
)
42 for (i
= 0; i
< 256; i
++)
46 for (i
= 0; i
< 256; i
++) {
47 j
= (j
+ S
[i
] + key
[kpos
]) & 0xff;
54 /* Skip the start of the stream */
56 for (k
= 0; k
< skip
; k
++) {
58 j
= (j
+ S
[i
]) & 0xff;
62 /* Apply RC4 to data */
64 for (k
= 0; k
< data_len
; k
++) {
66 j
= (j
+ S
[i
]) & 0xff;
68 *pos
++ ^= S
[(S
[i
] + S
[j
]) & 0xff];
74 * rc4 - XOR RC4 stream to given data
75 * @buf: data to be XOR'ed with RC4 stream
78 * @key_len: RC4 key length
80 * Generate RC4 pseudo random stream for the given key and XOR this with the
81 * data buffer to perform RC4 encryption/decryption.
83 void rc4(u8
*buf
, size_t len
, const u8
*key
, size_t key_len
)
85 rc4_skip(key
, key_len
, 0, buf
, len
);