summaryrefslogtreecommitdiffstats
path: root/lib/rc4.c
blob: 89d15f3c821be7ba7f79e82c6630a3f16951b825 (plain)
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
/*
 * (C) Copyright 2015 Google, Inc
 *
 * (C) Copyright 2008-2014 Rockchip Electronics
 *
 * Rivest Cipher 4 (RC4) implementation
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#ifndef USE_HOSTCC
#include <common.h>
#endif
#include <rc4.h>

void rc4_encode(unsigned char *buf, unsigned int len, unsigned char key[16])
{
	unsigned char s[256], k[256], temp;
	unsigned short i, j, t;
	int ptr;

	j = 0;
	for (i = 0; i < 256; i++) {
		s[i] = (unsigned char)i;
		j &= 0x0f;
		k[i] = key[j];
		j++;
	}

	j = 0;
	for (i = 0; i < 256; i++) {
		j = (j + s[i] + k[i]) % 256;
		temp = s[i];
		s[i] = s[j];
		s[j] = temp;
	}

	i = 0;
	j = 0;
	for (ptr = 0; ptr < len; ptr++) {
		i = (i + 1) % 256;
		j = (j + s[i]) % 256;
		temp = s[i];
		s[i] = s[j];
		s[j] = temp;
		t = (s[i] + (s[j] % 256)) % 256;
		buf[ptr] = buf[ptr] ^ s[t];
	}
}
OpenPOWER on IntegriCloud