rc4.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "rc4.h"
00020 
00021 #define S_SWAP(a,b) do { u8 t = S[a]; S[a] = S[b]; S[b] = t; } while(0)
00022 
00036 void rc4_skip(const u8 *key, size_t keylen, size_t skip,
00037               u8 *data, size_t data_len)
00038 {
00039         u32 i, j, k;
00040         u8 S[256], *pos;
00041         size_t kpos;
00042 
00043         /* Setup RC4 state */
00044         for (i = 0; i < 256; i++)
00045                 S[i] = i;
00046         j = 0;
00047         kpos = 0;
00048         for (i = 0; i < 256; i++) {
00049                 j = (j + S[i] + key[kpos]) & 0xff;
00050                 kpos++;
00051                 if (kpos >= keylen)
00052                         kpos = 0;
00053                 S_SWAP(i, j);
00054         }
00055 
00056         /* Skip the start of the stream */
00057         i = j = 0;
00058         for (k = 0; k < skip; k++) {
00059                 i = (i + 1) & 0xff;
00060                 j = (j + S[i]) & 0xff;
00061                 S_SWAP(i, j);
00062         }
00063 
00064         /* Apply RC4 to data */
00065         pos = data;
00066         for (k = 0; k < data_len; k++) {
00067                 i = (i + 1) & 0xff;
00068                 j = (j + S[i]) & 0xff;
00069                 S_SWAP(i, j);
00070                 *pos++ ^= S[(S[i] + S[j]) & 0xff];
00071         }
00072 }
00073 
00074 
00086 void rc4(u8 *buf, size_t len, const u8 *key, size_t key_len)
00087 {
00088         rc4_skip(key, key_len, 0, buf, len);
00089 }
00090 

Generated on Sun Dec 31 13:43:28 2006 for hostapd by  doxygen 1.4.2