Commit 1acab96d974a1b9f35cbc901f68ef00653d18738
1 parent
6e780c7a7b
Exists in
v2017.01-smarct4x
and in
30 other branches
Add rivest cipher 4 (rc4) implementation
Add an implementation of RC4. This will be used by Rockchip booting but may be useful in other situations. Signed-off-by: Simon Glass <sjg@chromium.org>
Showing 3 changed files with 71 additions and 0 deletions Side-by-side Diff
include/rc4.h
1 | +/* | |
2 | + * (C) Copyright 2015 Google, Inc | |
3 | + * | |
4 | + * (C) Copyright 2008-2014 Rockchip Electronics | |
5 | + * | |
6 | + * SPDX-License-Identifier: GPL-2.0+ | |
7 | + */ | |
8 | + | |
9 | +#ifndef __RC4_H | |
10 | +#define __RC4_H | |
11 | + | |
12 | +/** | |
13 | + * rc4_encode() - encode a buf with the RC4 cipher | |
14 | + * | |
15 | + * @buf: Buffer to encode (it is overwrite in the process | |
16 | + * @len: Length of buffer in bytes | |
17 | + * @key: 16-byte key to use | |
18 | + */ | |
19 | +void rc4_encode(unsigned char *buf, unsigned int len, unsigned char key[16]); | |
20 | + | |
21 | +#endif |
lib/Makefile
lib/rc4.c
1 | +/* | |
2 | + * (C) Copyright 2015 Google, Inc | |
3 | + * | |
4 | + * (C) Copyright 2008-2014 Rockchip Electronics | |
5 | + * | |
6 | + * Rivest Cipher 4 (RC4) implementation | |
7 | + * | |
8 | + * SPDX-License-Identifier: GPL-2.0+ | |
9 | + */ | |
10 | + | |
11 | +#ifndef USE_HOSTCC | |
12 | +#include <common.h> | |
13 | +#endif | |
14 | +#include <rc4.h> | |
15 | + | |
16 | +void rc4_encode(unsigned char *buf, unsigned int len, unsigned char key[16]) | |
17 | +{ | |
18 | + unsigned char s[256], k[256], temp; | |
19 | + unsigned short i, j, t; | |
20 | + int ptr; | |
21 | + | |
22 | + j = 0; | |
23 | + for (i = 0; i < 256; i++) { | |
24 | + s[i] = (unsigned char)i; | |
25 | + j &= 0x0f; | |
26 | + k[i] = key[j]; | |
27 | + j++; | |
28 | + } | |
29 | + | |
30 | + j = 0; | |
31 | + for (i = 0; i < 256; i++) { | |
32 | + j = (j + s[i] + k[i]) % 256; | |
33 | + temp = s[i]; | |
34 | + s[i] = s[j]; | |
35 | + s[j] = temp; | |
36 | + } | |
37 | + | |
38 | + i = 0; | |
39 | + j = 0; | |
40 | + for (ptr = 0; ptr < len; ptr++) { | |
41 | + i = (i + 1) % 256; | |
42 | + j = (j + s[i]) % 256; | |
43 | + temp = s[i]; | |
44 | + s[i] = s[j]; | |
45 | + s[j] = temp; | |
46 | + t = (s[i] + (s[j] % 256)) % 256; | |
47 | + buf[ptr] = buf[ptr] ^ s[t]; | |
48 | + } | |
49 | +} |