]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/rc4.c
This commit was generated by cvs2svn to compensate for changes in r149749,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / rc4.c
1 /*
2  * Host AP (software wireless LAN access point) user space daemon for
3  * Host AP kernel driver / RC4
4  * Copyright (c) 2002-2004, Jouni Malinen <jkmaline@cc.hut.fi>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include <stdio.h>
17 #include "common.h"
18 #include "rc4.h"
19
20 #define S_SWAP(a,b) do { u8 t = S[a]; S[a] = S[b]; S[b] = t; } while(0)
21
22 void rc4_skip(u8 *key, size_t keylen, size_t skip, u8 *data, size_t data_len)
23 {
24         u32 i, j, k;
25         u8 S[256], *pos;
26         int kpos;
27
28         /* Setup RC4 state */
29         for (i = 0; i < 256; i++)
30                 S[i] = i;
31         j = 0;
32         kpos = 0;
33         for (i = 0; i < 256; i++) {
34                 j = (j + S[i] + key[kpos]) & 0xff;
35                 kpos++;
36                 if (kpos >= keylen)
37                         kpos = 0;
38                 S_SWAP(i, j);
39         }
40
41         /* Skip the start of the stream */
42         i = j = 0;
43         for (k = 0; k < skip; k++) {
44                 i = (i + 1) & 0xff;
45                 j = (j + S[i]) & 0xff;
46                 S_SWAP(i, j);
47         }
48
49         /* Apply RC4 to data */
50         pos = data;
51         for (k = 0; k < data_len; k++) {
52                 i = (i + 1) & 0xff;
53                 j = (j + S[i]) & 0xff;
54                 S_SWAP(i, j);
55                 *pos++ ^= S[(S[i] + S[j]) & 0xff];
56         }
57 }
58
59
60 void rc4(u8 *buf, size_t len, u8 *key, size_t key_len)
61 {
62         rc4_skip(key, key_len, 0, buf, len);
63 }