Add resident.conf(5) and varsym.conf(5) manual pages.
[dragonfly/vkernel-mp.git] / contrib / hostapd-0.4.9 / rc4.c
blob4cf14d91d1af33e7b6ce08c3fd61ee3028d3c749
1 /*
2 * RC4 stream cipher
3 * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.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
10 * license.
12 * See README and COPYING for more details.
15 #include <stdio.h>
16 #include "common.h"
17 #include "rc4.h"
19 #define S_SWAP(a,b) do { u8 t = S[a]; S[a] = S[b]; S[b] = t; } while(0)
21 /**
22 * rc4 - XOR RC4 stream to given data with skip-stream-start
23 * @key: RC4 key
24 * @keylen: RC4 key length
25 * @skip: number of bytes to skip from the beginning of the RC4 stream
26 * @data: data to be XOR'ed with RC4 stream
27 * @data_len: buf length
29 * Generate RC4 pseudo random stream for the given key, skip beginning of the
30 * stream, and XOR the end result with the data buffer to perform RC4
31 * encryption/decryption.
33 void rc4_skip(const u8 *key, size_t keylen, size_t skip,
34 u8 *data, size_t data_len)
36 u32 i, j, k;
37 u8 S[256], *pos;
38 int kpos;
40 /* Setup RC4 state */
41 for (i = 0; i < 256; i++)
42 S[i] = i;
43 j = 0;
44 kpos = 0;
45 for (i = 0; i < 256; i++) {
46 j = (j + S[i] + key[kpos]) & 0xff;
47 kpos++;
48 if (kpos >= keylen)
49 kpos = 0;
50 S_SWAP(i, j);
53 /* Skip the start of the stream */
54 i = j = 0;
55 for (k = 0; k < skip; k++) {
56 i = (i + 1) & 0xff;
57 j = (j + S[i]) & 0xff;
58 S_SWAP(i, j);
61 /* Apply RC4 to data */
62 pos = data;
63 for (k = 0; k < data_len; k++) {
64 i = (i + 1) & 0xff;
65 j = (j + S[i]) & 0xff;
66 S_SWAP(i, j);
67 *pos++ ^= S[(S[i] + S[j]) & 0xff];
72 /**
73 * rc4 - XOR RC4 stream to given data
74 * @buf: data to be XOR'ed with RC4 stream
75 * @len: buf length
76 * @key: RC4 key
77 * @key_len: RC4 key length
79 * Generate RC4 pseudo random stream for the given key and XOR this with the
80 * data buffer to perform RC4 encryption/decryption.
82 void rc4(u8 *buf, size_t len, const u8 *key, size_t key_len)
84 rc4_skip(key, key_len, 0, buf, len);