summaryrefslogtreecommitdiff
path: root/lib/rc4.c
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2015-06-23 15:39:07 -0600
committerSimon Glass <sjg@chromium.org>2015-07-21 17:39:28 -0600
commit1acab96d974a1b9f35cbc901f68ef00653d18738 (patch)
treef21187eeb91fb8c77461203b0def9ebec1d7526a /lib/rc4.c
parent6e780c7a7bfea05429d29fd30cdbf213583e03f6 (diff)
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>
Diffstat (limited to 'lib/rc4.c')
-rw-r--r--lib/rc4.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/rc4.c b/lib/rc4.c
new file mode 100644
index 00000000000..89d15f3c821
--- /dev/null
+++ b/lib/rc4.c
@@ -0,0 +1,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];
+ }
+}