forked from 0x0d/ixi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrc4.c
More file actions
74 lines (63 loc) · 974 Bytes
/
rc4.c
File metadata and controls
74 lines (63 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* $Id: rc4.c, the RC4 stream-cipher
*/
#ifndef __KERNEL__
#include <string.h>
#endif
#include "rc4.h"
static inline void xchg(uchar *a, uchar *b)
{
uchar c = *a;
*a = *b;
*b = c;
}
#ifdef __KERNEL__
void k_rc4_init
#else
void rc4_init
#endif
(uchar *key, int len, rc4_ctx *ctx)
{
uchar index1, index2;
uchar *state = ctx->state;
uchar i;
i = 0;
do {
state[i] = i;
i++;
} while (i);
ctx->x = ctx->y = 0;
index1 = index2 = 0;
do {
index2 = key[index1] + state[i] + index2;
xchg(&state[i], &state[index2]);
index1++;
if (index1 >= len)
index1 = 0;
i++;
} while (i);
}
#ifdef __KERNEL__
void k_rc4
#else
void rc4
#endif
(uchar *data, int len, rc4_ctx *ctx)
{
#if 1
uchar *state = ctx->state;
uchar x = ctx->x;
uchar y = ctx->y;
int i;
for (i = 0; i < len; i++) {
uchar xor;
x++;
y = state[x] + y;
xchg(&state[x], &state[y]);
xor = state[x] + state[y];
data[i] ^= state[xor];
}
ctx->x = x;
ctx->y = y;
#endif
}