$search
00001 /* 00002 * RC4 stream cipher 00003 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation. 00008 * 00009 * Alternatively, this software may be distributed under the terms of BSD 00010 * license. 00011 * 00012 * See README and COPYING for more details. 00013 */ 00014 00015 #include "includes.h" 00016 00017 #include "common.h" 00018 #include "crypto.h" 00019 00020 #define S_SWAP(a,b) do { u8 t = S[a]; S[a] = S[b]; S[b] = t; } while(0) 00021 00022 int rc4_skip(const u8 *key, size_t keylen, size_t skip, 00023 u8 *data, size_t data_len) 00024 { 00025 u32 i, j, k; 00026 u8 S[256], *pos; 00027 size_t kpos; 00028 00029 /* Setup RC4 state */ 00030 for (i = 0; i < 256; i++) 00031 S[i] = i; 00032 j = 0; 00033 kpos = 0; 00034 for (i = 0; i < 256; i++) { 00035 j = (j + S[i] + key[kpos]) & 0xff; 00036 kpos++; 00037 if (kpos >= keylen) 00038 kpos = 0; 00039 S_SWAP(i, j); 00040 } 00041 00042 /* Skip the start of the stream */ 00043 i = j = 0; 00044 for (k = 0; k < skip; k++) { 00045 i = (i + 1) & 0xff; 00046 j = (j + S[i]) & 0xff; 00047 S_SWAP(i, j); 00048 } 00049 00050 /* Apply RC4 to data */ 00051 pos = data; 00052 for (k = 0; k < data_len; k++) { 00053 i = (i + 1) & 0xff; 00054 j = (j + S[i]) & 0xff; 00055 S_SWAP(i, j); 00056 *pos++ ^= S[(S[i] + S[j]) & 0xff]; 00057 } 00058 00059 return 0; 00060 }