source: rtems-libbsd/freebsd/contrib/wpa/src/crypto/rc4.c @ 9c9d11b

55-freebsd-126-freebsd-12
Last change on this file since 9c9d11b was 9c9d11b, checked in by Sichen Zhao <1473996754@…>, on 08/01/17 at 12:43:41

Import wpa from FreeBSD

  • Property mode set to 100644
File size: 1.0 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * RC4 stream cipher
5 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
6 *
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
9 */
10
11#include "includes.h"
12
13#include "common.h"
14#include "crypto.h"
15
16#define S_SWAP(a,b) do { u8 t = S[a]; S[a] = S[b]; S[b] = t; } while(0)
17
18int rc4_skip(const u8 *key, size_t keylen, size_t skip,
19             u8 *data, size_t data_len)
20{
21        u32 i, j, k;
22        u8 S[256], *pos;
23        size_t kpos;
24
25        /* Setup RC4 state */
26        for (i = 0; i < 256; i++)
27                S[i] = i;
28        j = 0;
29        kpos = 0;
30        for (i = 0; i < 256; i++) {
31                j = (j + S[i] + key[kpos]) & 0xff;
32                kpos++;
33                if (kpos >= keylen)
34                        kpos = 0;
35                S_SWAP(i, j);
36        }
37
38        /* Skip the start of the stream */
39        i = j = 0;
40        for (k = 0; k < skip; k++) {
41                i = (i + 1) & 0xff;
42                j = (j + S[i]) & 0xff;
43                S_SWAP(i, j);
44        }
45
46        /* Apply RC4 to data */
47        pos = data;
48        for (k = 0; k < data_len; k++) {
49                i = (i + 1) & 0xff;
50                j = (j + S[i]) & 0xff;
51                S_SWAP(i, j);
52                *pos++ ^= S[(S[i] + S[j]) & 0xff];
53        }
54
55        return 0;
56}
Note: See TracBrowser for help on using the repository browser.