rc4.c
Go to the documentation of this file.00001
00016 #include "includes.h"
00017
00018 #include "common.h"
00019 #include "crypto.h"
00020
00021 #define S_SWAP(a,b) do { u8 t = S[a]; S[a] = S[b]; S[b] = t; } while(0)
00022
00023 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
00024 u8 *data, size_t data_len)
00025 {
00026 u32 i, j, k;
00027 u8 S[256], *pos;
00028 size_t kpos;
00029
00030
00031 for (i = 0; i < 256; i++)
00032 S[i] = i;
00033 j = 0;
00034 kpos = 0;
00035 for (i = 0; i < 256; i++) {
00036 j = (j + S[i] + key[kpos]) & 0xff;
00037 kpos++;
00038 if (kpos >= keylen)
00039 kpos = 0;
00040 S_SWAP(i, j);
00041 }
00042
00043
00044 i = j = 0;
00045 for (k = 0; k < skip; k++) {
00046 i = (i + 1) & 0xff;
00047 j = (j + S[i]) & 0xff;
00048 S_SWAP(i, j);
00049 }
00050
00051
00052 pos = data;
00053 for (k = 0; k < data_len; k++) {
00054 i = (i + 1) & 0xff;
00055 j = (j + S[i]) & 0xff;
00056 S_SWAP(i, j);
00057 *pos++ ^= S[(S[i] + S[j]) & 0xff];
00058 }
00059
00060 return 0;
00061 }
00062