This source file includes following definitions.
- rc4_keysetup
- rc4_crypt
- rc4_skip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <sys/types.h>
19
20 #include <crypto/arc4.h>
21
22 #define RC4SWAP(x,y) \
23 do { \
24 u_int8_t t = ctx->state[x]; \
25 ctx->state[x] = ctx->state[y]; \
26 ctx->state[y] = t; \
27 } while(0)
28
29 void
30 rc4_keysetup(struct rc4_ctx *ctx, u_char *key, u_int32_t klen)
31 {
32 u_int8_t x, y;
33 u_int32_t i;
34
35 x = y = 0;
36 for (i = 0; i < RC4STATE; i++)
37 ctx->state[i] = i;
38 for (i = 0; i < RC4STATE; i++) {
39 y = (key[x] + ctx->state[i] + y) % RC4STATE;
40 RC4SWAP(i, y);
41 x = (x + 1) % klen;
42 }
43 ctx->x = ctx->y = 0;
44 }
45
46 void
47 rc4_crypt(struct rc4_ctx *ctx, u_char *src, u_char *dst,
48 u_int32_t len)
49 {
50 u_int32_t i;
51
52 for (i = 0; i < len; i++) {
53 ctx->x = (ctx->x + 1) % RC4STATE;
54 ctx->y = (ctx->state[ctx->x] + ctx->y) % RC4STATE;
55 RC4SWAP(ctx->x, ctx->y);
56 dst[i] = src[i] ^ ctx->state[
57 (ctx->state[ctx->x] + ctx->state[ctx->y]) % RC4STATE];
58 }
59 }
60
61 void
62 rc4_skip(struct rc4_ctx *ctx, u_int32_t len)
63 {
64 for (; len > 0; len--) {
65 ctx->x = (ctx->x + 1) % RC4STATE;
66 ctx->y = (ctx->state[ctx->x] + ctx->y) % RC4STATE;
67 RC4SWAP(ctx->x, ctx->y);
68 }
69 }